2 * Copyright (C) 2011-2012 Red Hat UK.
4 * This file is released under the GPL.
7 #include "dm-thin-metadata.h"
8 #include "dm-bio-prison.h"
11 #include <linux/device-mapper.h>
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/list.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #define DM_MSG_PREFIX "thin"
24 #define ENDIO_HOOK_POOL_SIZE 1024
25 #define MAPPING_POOL_SIZE 1024
26 #define PRISON_CELLS 1024
27 #define COMMIT_PERIOD HZ
30 * The block size of the device holding pool data must be
31 * between 64KB and 1GB.
33 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
34 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
37 * Device id is restricted to 24 bits.
39 #define MAX_DEV_ID ((1 << 24) - 1)
42 * How do we handle breaking sharing of data blocks?
43 * =================================================
45 * We use a standard copy-on-write btree to store the mappings for the
46 * devices (note I'm talking about copy-on-write of the metadata here, not
47 * the data). When you take an internal snapshot you clone the root node
48 * of the origin btree. After this there is no concept of an origin or a
49 * snapshot. They are just two device trees that happen to point to the
52 * When we get a write in we decide if it's to a shared data block using
53 * some timestamp magic. If it is, we have to break sharing.
55 * Let's say we write to a shared block in what was the origin. The
58 * i) plug io further to this physical block. (see bio_prison code).
60 * ii) quiesce any read io to that shared data block. Obviously
61 * including all devices that share this block. (see dm_deferred_set code)
63 * iii) copy the data block to a newly allocate block. This step can be
64 * missed out if the io covers the block. (schedule_copy).
66 * iv) insert the new mapping into the origin's btree
67 * (process_prepared_mapping). This act of inserting breaks some
68 * sharing of btree nodes between the two devices. Breaking sharing only
69 * effects the btree of that specific device. Btrees for the other
70 * devices that share the block never change. The btree for the origin
71 * device as it was after the last commit is untouched, ie. we're using
72 * persistent data structures in the functional programming sense.
74 * v) unplug io to this physical block, including the io that triggered
75 * the breaking of sharing.
77 * Steps (ii) and (iii) occur in parallel.
79 * The metadata _doesn't_ need to be committed before the io continues. We
80 * get away with this because the io is always written to a _new_ block.
81 * If there's a crash, then:
83 * - The origin mapping will point to the old origin block (the shared
84 * one). This will contain the data as it was before the io that triggered
85 * the breaking of sharing came in.
87 * - The snap mapping still points to the old block. As it would after
90 * The downside of this scheme is the timestamp magic isn't perfect, and
91 * will continue to think that data block in the snapshot device is shared
92 * even after the write to the origin has broken sharing. I suspect data
93 * blocks will typically be shared by many different devices, so we're
94 * breaking sharing n + 1 times, rather than n, where n is the number of
95 * devices that reference this data block. At the moment I think the
96 * benefits far, far outweigh the disadvantages.
99 /*----------------------------------------------------------------*/
104 static void build_data_key(struct dm_thin_device
*td
,
105 dm_block_t b
, struct dm_cell_key
*key
)
108 key
->dev
= dm_thin_dev_id(td
);
112 static void build_virtual_key(struct dm_thin_device
*td
, dm_block_t b
,
113 struct dm_cell_key
*key
)
116 key
->dev
= dm_thin_dev_id(td
);
120 /*----------------------------------------------------------------*/
123 * A pool device ties together a metadata device and a data device. It
124 * also provides the interface for creating and destroying internal
127 struct dm_thin_new_mapping
;
130 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
133 PM_WRITE
, /* metadata may be changed */
134 PM_READ_ONLY
, /* metadata may not be changed */
135 PM_FAIL
, /* all I/O fails */
138 struct pool_features
{
141 bool zero_new_blocks
:1;
142 bool discard_enabled
:1;
143 bool discard_passdown
:1;
147 typedef void (*process_bio_fn
)(struct thin_c
*tc
, struct bio
*bio
);
148 typedef void (*process_mapping_fn
)(struct dm_thin_new_mapping
*m
);
151 struct list_head list
;
152 struct dm_target
*ti
; /* Only set if a pool target is bound */
154 struct mapped_device
*pool_md
;
155 struct block_device
*md_dev
;
156 struct dm_pool_metadata
*pmd
;
158 dm_block_t low_water_blocks
;
159 uint32_t sectors_per_block
;
160 int sectors_per_block_shift
;
162 struct pool_features pf
;
163 unsigned low_water_triggered
:1; /* A dm event has been sent */
164 unsigned no_free_space
:1; /* A -ENOSPC warning has been issued */
166 struct dm_bio_prison
*prison
;
167 struct dm_kcopyd_client
*copier
;
169 struct workqueue_struct
*wq
;
170 struct work_struct worker
;
171 struct delayed_work waker
;
173 unsigned long last_commit_jiffies
;
177 struct bio_list deferred_bios
;
178 struct bio_list deferred_flush_bios
;
179 struct list_head prepared_mappings
;
180 struct list_head prepared_discards
;
182 struct bio_list retry_on_resume_list
;
184 struct dm_deferred_set
*shared_read_ds
;
185 struct dm_deferred_set
*all_io_ds
;
187 struct dm_thin_new_mapping
*next_mapping
;
188 mempool_t
*mapping_pool
;
190 process_bio_fn process_bio
;
191 process_bio_fn process_discard
;
193 process_mapping_fn process_prepared_mapping
;
194 process_mapping_fn process_prepared_discard
;
197 static enum pool_mode
get_pool_mode(struct pool
*pool
);
198 static void set_pool_mode(struct pool
*pool
, enum pool_mode mode
);
201 * Target context for a pool.
204 struct dm_target
*ti
;
206 struct dm_dev
*data_dev
;
207 struct dm_dev
*metadata_dev
;
208 struct dm_target_callbacks callbacks
;
210 dm_block_t low_water_blocks
;
211 struct pool_features requested_pf
; /* Features requested during table load */
212 struct pool_features adjusted_pf
; /* Features used after adjusting for constituent devices */
216 * Target context for a thin.
219 struct dm_dev
*pool_dev
;
220 struct dm_dev
*origin_dev
;
224 struct dm_thin_device
*td
;
227 /*----------------------------------------------------------------*/
230 * A global list of pools that uses a struct mapped_device as a key.
232 static struct dm_thin_pool_table
{
234 struct list_head pools
;
235 } dm_thin_pool_table
;
237 static void pool_table_init(void)
239 mutex_init(&dm_thin_pool_table
.mutex
);
240 INIT_LIST_HEAD(&dm_thin_pool_table
.pools
);
243 static void __pool_table_insert(struct pool
*pool
)
245 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
246 list_add(&pool
->list
, &dm_thin_pool_table
.pools
);
249 static void __pool_table_remove(struct pool
*pool
)
251 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
252 list_del(&pool
->list
);
255 static struct pool
*__pool_table_lookup(struct mapped_device
*md
)
257 struct pool
*pool
= NULL
, *tmp
;
259 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
261 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
262 if (tmp
->pool_md
== md
) {
271 static struct pool
*__pool_table_lookup_metadata_dev(struct block_device
*md_dev
)
273 struct pool
*pool
= NULL
, *tmp
;
275 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
277 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
278 if (tmp
->md_dev
== md_dev
) {
287 /*----------------------------------------------------------------*/
289 struct dm_thin_endio_hook
{
291 struct dm_deferred_entry
*shared_read_entry
;
292 struct dm_deferred_entry
*all_io_entry
;
293 struct dm_thin_new_mapping
*overwrite_mapping
;
296 static void __requeue_bio_list(struct thin_c
*tc
, struct bio_list
*master
)
299 struct bio_list bios
;
301 bio_list_init(&bios
);
302 bio_list_merge(&bios
, master
);
303 bio_list_init(master
);
305 while ((bio
= bio_list_pop(&bios
))) {
306 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
309 bio_endio(bio
, DM_ENDIO_REQUEUE
);
311 bio_list_add(master
, bio
);
315 static void requeue_io(struct thin_c
*tc
)
317 struct pool
*pool
= tc
->pool
;
320 spin_lock_irqsave(&pool
->lock
, flags
);
321 __requeue_bio_list(tc
, &pool
->deferred_bios
);
322 __requeue_bio_list(tc
, &pool
->retry_on_resume_list
);
323 spin_unlock_irqrestore(&pool
->lock
, flags
);
327 * This section of code contains the logic for processing a thin device's IO.
328 * Much of the code depends on pool object resources (lists, workqueues, etc)
329 * but most is exclusively called from the thin target rather than the thin-pool
333 static bool block_size_is_power_of_two(struct pool
*pool
)
335 return pool
->sectors_per_block_shift
>= 0;
338 static dm_block_t
get_bio_block(struct thin_c
*tc
, struct bio
*bio
)
340 struct pool
*pool
= tc
->pool
;
341 sector_t block_nr
= bio
->bi_sector
;
343 if (block_size_is_power_of_two(pool
))
344 block_nr
>>= pool
->sectors_per_block_shift
;
346 (void) sector_div(block_nr
, pool
->sectors_per_block
);
351 static void remap(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
)
353 struct pool
*pool
= tc
->pool
;
354 sector_t bi_sector
= bio
->bi_sector
;
356 bio
->bi_bdev
= tc
->pool_dev
->bdev
;
357 if (block_size_is_power_of_two(pool
))
358 bio
->bi_sector
= (block
<< pool
->sectors_per_block_shift
) |
359 (bi_sector
& (pool
->sectors_per_block
- 1));
361 bio
->bi_sector
= (block
* pool
->sectors_per_block
) +
362 sector_div(bi_sector
, pool
->sectors_per_block
);
365 static void remap_to_origin(struct thin_c
*tc
, struct bio
*bio
)
367 bio
->bi_bdev
= tc
->origin_dev
->bdev
;
370 static int bio_triggers_commit(struct thin_c
*tc
, struct bio
*bio
)
372 return (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) &&
373 dm_thin_changed_this_transaction(tc
->td
);
376 static void inc_all_io_entry(struct pool
*pool
, struct bio
*bio
)
378 struct dm_thin_endio_hook
*h
;
380 if (bio
->bi_rw
& REQ_DISCARD
)
383 h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
384 h
->all_io_entry
= dm_deferred_entry_inc(pool
->all_io_ds
);
387 static void issue(struct thin_c
*tc
, struct bio
*bio
)
389 struct pool
*pool
= tc
->pool
;
392 if (!bio_triggers_commit(tc
, bio
)) {
393 generic_make_request(bio
);
398 * Complete bio with an error if earlier I/O caused changes to
399 * the metadata that can't be committed e.g, due to I/O errors
400 * on the metadata device.
402 if (dm_thin_aborted_changes(tc
->td
)) {
408 * Batch together any bios that trigger commits and then issue a
409 * single commit for them in process_deferred_bios().
411 spin_lock_irqsave(&pool
->lock
, flags
);
412 bio_list_add(&pool
->deferred_flush_bios
, bio
);
413 spin_unlock_irqrestore(&pool
->lock
, flags
);
416 static void remap_to_origin_and_issue(struct thin_c
*tc
, struct bio
*bio
)
418 remap_to_origin(tc
, bio
);
422 static void remap_and_issue(struct thin_c
*tc
, struct bio
*bio
,
425 remap(tc
, bio
, block
);
430 * wake_worker() is used when new work is queued and when pool_resume is
431 * ready to continue deferred IO processing.
433 static void wake_worker(struct pool
*pool
)
435 queue_work(pool
->wq
, &pool
->worker
);
438 /*----------------------------------------------------------------*/
441 * Bio endio functions.
443 struct dm_thin_new_mapping
{
444 struct list_head list
;
448 unsigned pass_discard
:1;
451 dm_block_t virt_block
;
452 dm_block_t data_block
;
453 struct dm_bio_prison_cell
*cell
, *cell2
;
457 * If the bio covers the whole area of a block then we can avoid
458 * zeroing or copying. Instead this bio is hooked. The bio will
459 * still be in the cell, so care has to be taken to avoid issuing
463 bio_end_io_t
*saved_bi_end_io
;
466 static void __maybe_add_mapping(struct dm_thin_new_mapping
*m
)
468 struct pool
*pool
= m
->tc
->pool
;
470 if (m
->quiesced
&& m
->prepared
) {
471 list_add(&m
->list
, &pool
->prepared_mappings
);
476 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
479 struct dm_thin_new_mapping
*m
= context
;
480 struct pool
*pool
= m
->tc
->pool
;
482 m
->err
= read_err
|| write_err
? -EIO
: 0;
484 spin_lock_irqsave(&pool
->lock
, flags
);
486 __maybe_add_mapping(m
);
487 spin_unlock_irqrestore(&pool
->lock
, flags
);
490 static void overwrite_endio(struct bio
*bio
, int err
)
493 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
494 struct dm_thin_new_mapping
*m
= h
->overwrite_mapping
;
495 struct pool
*pool
= m
->tc
->pool
;
499 spin_lock_irqsave(&pool
->lock
, flags
);
501 __maybe_add_mapping(m
);
502 spin_unlock_irqrestore(&pool
->lock
, flags
);
505 /*----------------------------------------------------------------*/
512 * Prepared mapping jobs.
516 * This sends the bios in the cell back to the deferred_bios list.
518 static void cell_defer(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
520 struct pool
*pool
= tc
->pool
;
523 spin_lock_irqsave(&pool
->lock
, flags
);
524 dm_cell_release(cell
, &pool
->deferred_bios
);
525 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
531 * Same as cell_defer except it omits the original holder of the cell.
533 static void cell_defer_no_holder(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
535 struct pool
*pool
= tc
->pool
;
538 spin_lock_irqsave(&pool
->lock
, flags
);
539 dm_cell_release_no_holder(cell
, &pool
->deferred_bios
);
540 spin_unlock_irqrestore(&pool
->lock
, flags
);
545 static void process_prepared_mapping_fail(struct dm_thin_new_mapping
*m
)
548 m
->bio
->bi_end_io
= m
->saved_bi_end_io
;
549 dm_cell_error(m
->cell
);
551 mempool_free(m
, m
->tc
->pool
->mapping_pool
);
553 static void process_prepared_mapping(struct dm_thin_new_mapping
*m
)
555 struct thin_c
*tc
= m
->tc
;
561 bio
->bi_end_io
= m
->saved_bi_end_io
;
564 dm_cell_error(m
->cell
);
569 * Commit the prepared block into the mapping btree.
570 * Any I/O for this block arriving after this point will get
571 * remapped to it directly.
573 r
= dm_thin_insert_block(tc
->td
, m
->virt_block
, m
->data_block
);
575 DMERR_LIMIT("dm_thin_insert_block() failed");
576 dm_cell_error(m
->cell
);
581 * Release any bios held while the block was being provisioned.
582 * If we are processing a write bio that completely covers the block,
583 * we already processed it so can ignore it now when processing
584 * the bios in the cell.
587 cell_defer_no_holder(tc
, m
->cell
);
590 cell_defer(tc
, m
->cell
);
594 mempool_free(m
, tc
->pool
->mapping_pool
);
597 static void process_prepared_discard_fail(struct dm_thin_new_mapping
*m
)
599 struct thin_c
*tc
= m
->tc
;
601 bio_io_error(m
->bio
);
602 cell_defer_no_holder(tc
, m
->cell
);
603 cell_defer_no_holder(tc
, m
->cell2
);
604 mempool_free(m
, tc
->pool
->mapping_pool
);
607 static void process_prepared_discard_passdown(struct dm_thin_new_mapping
*m
)
609 struct thin_c
*tc
= m
->tc
;
611 inc_all_io_entry(tc
->pool
, m
->bio
);
612 cell_defer_no_holder(tc
, m
->cell
);
613 cell_defer_no_holder(tc
, m
->cell2
);
616 remap_and_issue(tc
, m
->bio
, m
->data_block
);
618 bio_endio(m
->bio
, 0);
620 mempool_free(m
, tc
->pool
->mapping_pool
);
623 static void process_prepared_discard(struct dm_thin_new_mapping
*m
)
626 struct thin_c
*tc
= m
->tc
;
628 r
= dm_thin_remove_block(tc
->td
, m
->virt_block
);
630 DMERR_LIMIT("dm_thin_remove_block() failed");
632 process_prepared_discard_passdown(m
);
635 static void process_prepared(struct pool
*pool
, struct list_head
*head
,
636 process_mapping_fn
*fn
)
639 struct list_head maps
;
640 struct dm_thin_new_mapping
*m
, *tmp
;
642 INIT_LIST_HEAD(&maps
);
643 spin_lock_irqsave(&pool
->lock
, flags
);
644 list_splice_init(head
, &maps
);
645 spin_unlock_irqrestore(&pool
->lock
, flags
);
647 list_for_each_entry_safe(m
, tmp
, &maps
, list
)
654 static int io_overlaps_block(struct pool
*pool
, struct bio
*bio
)
656 return bio
->bi_size
== (pool
->sectors_per_block
<< SECTOR_SHIFT
);
659 static int io_overwrites_block(struct pool
*pool
, struct bio
*bio
)
661 return (bio_data_dir(bio
) == WRITE
) &&
662 io_overlaps_block(pool
, bio
);
665 static void save_and_set_endio(struct bio
*bio
, bio_end_io_t
**save
,
668 *save
= bio
->bi_end_io
;
672 static int ensure_next_mapping(struct pool
*pool
)
674 if (pool
->next_mapping
)
677 pool
->next_mapping
= mempool_alloc(pool
->mapping_pool
, GFP_ATOMIC
);
679 return pool
->next_mapping
? 0 : -ENOMEM
;
682 static struct dm_thin_new_mapping
*get_next_mapping(struct pool
*pool
)
684 struct dm_thin_new_mapping
*r
= pool
->next_mapping
;
686 BUG_ON(!pool
->next_mapping
);
688 pool
->next_mapping
= NULL
;
693 static void schedule_copy(struct thin_c
*tc
, dm_block_t virt_block
,
694 struct dm_dev
*origin
, dm_block_t data_origin
,
695 dm_block_t data_dest
,
696 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
699 struct pool
*pool
= tc
->pool
;
700 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
702 INIT_LIST_HEAD(&m
->list
);
706 m
->virt_block
= virt_block
;
707 m
->data_block
= data_dest
;
712 if (!dm_deferred_set_add_work(pool
->shared_read_ds
, &m
->list
))
716 * IO to pool_dev remaps to the pool target's data_dev.
718 * If the whole block of data is being overwritten, we can issue the
719 * bio immediately. Otherwise we use kcopyd to clone the data first.
721 if (io_overwrites_block(pool
, bio
)) {
722 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
724 h
->overwrite_mapping
= m
;
726 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
727 inc_all_io_entry(pool
, bio
);
728 remap_and_issue(tc
, bio
, data_dest
);
730 struct dm_io_region from
, to
;
732 from
.bdev
= origin
->bdev
;
733 from
.sector
= data_origin
* pool
->sectors_per_block
;
734 from
.count
= pool
->sectors_per_block
;
736 to
.bdev
= tc
->pool_dev
->bdev
;
737 to
.sector
= data_dest
* pool
->sectors_per_block
;
738 to
.count
= pool
->sectors_per_block
;
740 r
= dm_kcopyd_copy(pool
->copier
, &from
, 1, &to
,
741 0, copy_complete
, m
);
743 mempool_free(m
, pool
->mapping_pool
);
744 DMERR_LIMIT("dm_kcopyd_copy() failed");
750 static void schedule_internal_copy(struct thin_c
*tc
, dm_block_t virt_block
,
751 dm_block_t data_origin
, dm_block_t data_dest
,
752 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
754 schedule_copy(tc
, virt_block
, tc
->pool_dev
,
755 data_origin
, data_dest
, cell
, bio
);
758 static void schedule_external_copy(struct thin_c
*tc
, dm_block_t virt_block
,
759 dm_block_t data_dest
,
760 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
762 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
763 virt_block
, data_dest
, cell
, bio
);
766 static void schedule_zero(struct thin_c
*tc
, dm_block_t virt_block
,
767 dm_block_t data_block
, struct dm_bio_prison_cell
*cell
,
770 struct pool
*pool
= tc
->pool
;
771 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
773 INIT_LIST_HEAD(&m
->list
);
777 m
->virt_block
= virt_block
;
778 m
->data_block
= data_block
;
784 * If the whole block of data is being overwritten or we are not
785 * zeroing pre-existing data, we can issue the bio immediately.
786 * Otherwise we use kcopyd to zero the data first.
788 if (!pool
->pf
.zero_new_blocks
)
789 process_prepared_mapping(m
);
791 else if (io_overwrites_block(pool
, bio
)) {
792 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
794 h
->overwrite_mapping
= m
;
796 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
797 inc_all_io_entry(pool
, bio
);
798 remap_and_issue(tc
, bio
, data_block
);
801 struct dm_io_region to
;
803 to
.bdev
= tc
->pool_dev
->bdev
;
804 to
.sector
= data_block
* pool
->sectors_per_block
;
805 to
.count
= pool
->sectors_per_block
;
807 r
= dm_kcopyd_zero(pool
->copier
, 1, &to
, 0, copy_complete
, m
);
809 mempool_free(m
, pool
->mapping_pool
);
810 DMERR_LIMIT("dm_kcopyd_zero() failed");
816 static int commit(struct pool
*pool
)
820 r
= dm_pool_commit_metadata(pool
->pmd
);
822 DMERR_LIMIT("commit failed: error = %d", r
);
828 * A non-zero return indicates read_only or fail_io mode.
829 * Many callers don't care about the return value.
831 static int commit_or_fallback(struct pool
*pool
)
835 if (get_pool_mode(pool
) != PM_WRITE
)
840 set_pool_mode(pool
, PM_READ_ONLY
);
845 static int alloc_data_block(struct thin_c
*tc
, dm_block_t
*result
)
848 dm_block_t free_blocks
;
850 struct pool
*pool
= tc
->pool
;
852 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
856 if (free_blocks
<= pool
->low_water_blocks
&& !pool
->low_water_triggered
) {
857 DMWARN("%s: reached low water mark, sending event.",
858 dm_device_name(pool
->pool_md
));
859 spin_lock_irqsave(&pool
->lock
, flags
);
860 pool
->low_water_triggered
= 1;
861 spin_unlock_irqrestore(&pool
->lock
, flags
);
862 dm_table_event(pool
->ti
->table
);
866 if (pool
->no_free_space
)
870 * Try to commit to see if that will free up some
873 (void) commit_or_fallback(pool
);
875 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
880 * If we still have no space we set a flag to avoid
881 * doing all this checking and return -ENOSPC.
884 DMWARN("%s: no free space available.",
885 dm_device_name(pool
->pool_md
));
886 spin_lock_irqsave(&pool
->lock
, flags
);
887 pool
->no_free_space
= 1;
888 spin_unlock_irqrestore(&pool
->lock
, flags
);
894 r
= dm_pool_alloc_data_block(pool
->pmd
, result
);
902 * If we have run out of space, queue bios until the device is
903 * resumed, presumably after having been reloaded with more space.
905 static void retry_on_resume(struct bio
*bio
)
907 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
908 struct thin_c
*tc
= h
->tc
;
909 struct pool
*pool
= tc
->pool
;
912 spin_lock_irqsave(&pool
->lock
, flags
);
913 bio_list_add(&pool
->retry_on_resume_list
, bio
);
914 spin_unlock_irqrestore(&pool
->lock
, flags
);
917 static void no_space(struct dm_bio_prison_cell
*cell
)
920 struct bio_list bios
;
922 bio_list_init(&bios
);
923 dm_cell_release(cell
, &bios
);
925 while ((bio
= bio_list_pop(&bios
)))
926 retry_on_resume(bio
);
929 static void process_discard(struct thin_c
*tc
, struct bio
*bio
)
933 struct pool
*pool
= tc
->pool
;
934 struct dm_bio_prison_cell
*cell
, *cell2
;
935 struct dm_cell_key key
, key2
;
936 dm_block_t block
= get_bio_block(tc
, bio
);
937 struct dm_thin_lookup_result lookup_result
;
938 struct dm_thin_new_mapping
*m
;
940 build_virtual_key(tc
->td
, block
, &key
);
941 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell
))
944 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
948 * Check nobody is fiddling with this pool block. This can
949 * happen if someone's in the process of breaking sharing
952 build_data_key(tc
->td
, lookup_result
.block
, &key2
);
953 if (dm_bio_detain(tc
->pool
->prison
, &key2
, bio
, &cell2
)) {
954 cell_defer_no_holder(tc
, cell
);
958 if (io_overlaps_block(pool
, bio
)) {
960 * IO may still be going to the destination block. We must
961 * quiesce before we can do the removal.
963 m
= get_next_mapping(pool
);
965 m
->pass_discard
= (!lookup_result
.shared
) && pool
->pf
.discard_passdown
;
966 m
->virt_block
= block
;
967 m
->data_block
= lookup_result
.block
;
973 if (!dm_deferred_set_add_work(pool
->all_io_ds
, &m
->list
)) {
974 spin_lock_irqsave(&pool
->lock
, flags
);
975 list_add(&m
->list
, &pool
->prepared_discards
);
976 spin_unlock_irqrestore(&pool
->lock
, flags
);
980 inc_all_io_entry(pool
, bio
);
981 cell_defer_no_holder(tc
, cell
);
982 cell_defer_no_holder(tc
, cell2
);
985 * The DM core makes sure that the discard doesn't span
986 * a block boundary. So we submit the discard of a
987 * partial block appropriately.
989 if ((!lookup_result
.shared
) && pool
->pf
.discard_passdown
)
990 remap_and_issue(tc
, bio
, lookup_result
.block
);
998 * It isn't provisioned, just forget it.
1000 cell_defer_no_holder(tc
, cell
);
1005 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1007 cell_defer_no_holder(tc
, cell
);
1013 static void break_sharing(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1014 struct dm_cell_key
*key
,
1015 struct dm_thin_lookup_result
*lookup_result
,
1016 struct dm_bio_prison_cell
*cell
)
1019 dm_block_t data_block
;
1021 r
= alloc_data_block(tc
, &data_block
);
1024 schedule_internal_copy(tc
, block
, lookup_result
->block
,
1025 data_block
, cell
, bio
);
1033 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1035 dm_cell_error(cell
);
1040 static void process_shared_bio(struct thin_c
*tc
, struct bio
*bio
,
1042 struct dm_thin_lookup_result
*lookup_result
)
1044 struct dm_bio_prison_cell
*cell
;
1045 struct pool
*pool
= tc
->pool
;
1046 struct dm_cell_key key
;
1049 * If cell is already occupied, then sharing is already in the process
1050 * of being broken so we have nothing further to do here.
1052 build_data_key(tc
->td
, lookup_result
->block
, &key
);
1053 if (dm_bio_detain(pool
->prison
, &key
, bio
, &cell
))
1056 if (bio_data_dir(bio
) == WRITE
&& bio
->bi_size
)
1057 break_sharing(tc
, bio
, block
, &key
, lookup_result
, cell
);
1059 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1061 h
->shared_read_entry
= dm_deferred_entry_inc(pool
->shared_read_ds
);
1062 inc_all_io_entry(pool
, bio
);
1063 cell_defer_no_holder(tc
, cell
);
1065 remap_and_issue(tc
, bio
, lookup_result
->block
);
1069 static void provision_block(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1070 struct dm_bio_prison_cell
*cell
)
1073 dm_block_t data_block
;
1076 * Remap empty bios (flushes) immediately, without provisioning.
1078 if (!bio
->bi_size
) {
1079 inc_all_io_entry(tc
->pool
, bio
);
1080 cell_defer_no_holder(tc
, cell
);
1082 remap_and_issue(tc
, bio
, 0);
1087 * Fill read bios with zeroes and complete them immediately.
1089 if (bio_data_dir(bio
) == READ
) {
1091 cell_defer_no_holder(tc
, cell
);
1096 r
= alloc_data_block(tc
, &data_block
);
1100 schedule_external_copy(tc
, block
, data_block
, cell
, bio
);
1102 schedule_zero(tc
, block
, data_block
, cell
, bio
);
1110 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1112 set_pool_mode(tc
->pool
, PM_READ_ONLY
);
1113 dm_cell_error(cell
);
1118 static void process_bio(struct thin_c
*tc
, struct bio
*bio
)
1121 dm_block_t block
= get_bio_block(tc
, bio
);
1122 struct dm_bio_prison_cell
*cell
;
1123 struct dm_cell_key key
;
1124 struct dm_thin_lookup_result lookup_result
;
1127 * If cell is already occupied, then the block is already
1128 * being provisioned so we have nothing further to do here.
1130 build_virtual_key(tc
->td
, block
, &key
);
1131 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell
))
1134 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1137 if (lookup_result
.shared
) {
1138 process_shared_bio(tc
, bio
, block
, &lookup_result
);
1139 cell_defer_no_holder(tc
, cell
);
1141 inc_all_io_entry(tc
->pool
, bio
);
1142 cell_defer_no_holder(tc
, cell
);
1144 remap_and_issue(tc
, bio
, lookup_result
.block
);
1149 if (bio_data_dir(bio
) == READ
&& tc
->origin_dev
) {
1150 inc_all_io_entry(tc
->pool
, bio
);
1151 cell_defer_no_holder(tc
, cell
);
1153 remap_to_origin_and_issue(tc
, bio
);
1155 provision_block(tc
, bio
, block
, cell
);
1159 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1161 cell_defer_no_holder(tc
, cell
);
1167 static void process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
)
1170 int rw
= bio_data_dir(bio
);
1171 dm_block_t block
= get_bio_block(tc
, bio
);
1172 struct dm_thin_lookup_result lookup_result
;
1174 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1177 if (lookup_result
.shared
&& (rw
== WRITE
) && bio
->bi_size
)
1180 inc_all_io_entry(tc
->pool
, bio
);
1181 remap_and_issue(tc
, bio
, lookup_result
.block
);
1191 if (tc
->origin_dev
) {
1192 inc_all_io_entry(tc
->pool
, bio
);
1193 remap_to_origin_and_issue(tc
, bio
);
1202 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1209 static void process_bio_fail(struct thin_c
*tc
, struct bio
*bio
)
1214 static int need_commit_due_to_time(struct pool
*pool
)
1216 return jiffies
< pool
->last_commit_jiffies
||
1217 jiffies
> pool
->last_commit_jiffies
+ COMMIT_PERIOD
;
1220 static void process_deferred_bios(struct pool
*pool
)
1222 unsigned long flags
;
1224 struct bio_list bios
;
1226 bio_list_init(&bios
);
1228 spin_lock_irqsave(&pool
->lock
, flags
);
1229 bio_list_merge(&bios
, &pool
->deferred_bios
);
1230 bio_list_init(&pool
->deferred_bios
);
1231 spin_unlock_irqrestore(&pool
->lock
, flags
);
1233 while ((bio
= bio_list_pop(&bios
))) {
1234 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1235 struct thin_c
*tc
= h
->tc
;
1238 * If we've got no free new_mapping structs, and processing
1239 * this bio might require one, we pause until there are some
1240 * prepared mappings to process.
1242 if (ensure_next_mapping(pool
)) {
1243 spin_lock_irqsave(&pool
->lock
, flags
);
1244 bio_list_merge(&pool
->deferred_bios
, &bios
);
1245 spin_unlock_irqrestore(&pool
->lock
, flags
);
1250 if (bio
->bi_rw
& REQ_DISCARD
)
1251 pool
->process_discard(tc
, bio
);
1253 pool
->process_bio(tc
, bio
);
1257 * If there are any deferred flush bios, we must commit
1258 * the metadata before issuing them.
1260 bio_list_init(&bios
);
1261 spin_lock_irqsave(&pool
->lock
, flags
);
1262 bio_list_merge(&bios
, &pool
->deferred_flush_bios
);
1263 bio_list_init(&pool
->deferred_flush_bios
);
1264 spin_unlock_irqrestore(&pool
->lock
, flags
);
1266 if (bio_list_empty(&bios
) && !need_commit_due_to_time(pool
))
1269 if (commit_or_fallback(pool
)) {
1270 while ((bio
= bio_list_pop(&bios
)))
1274 pool
->last_commit_jiffies
= jiffies
;
1276 while ((bio
= bio_list_pop(&bios
)))
1277 generic_make_request(bio
);
1280 static void do_worker(struct work_struct
*ws
)
1282 struct pool
*pool
= container_of(ws
, struct pool
, worker
);
1284 process_prepared(pool
, &pool
->prepared_mappings
, &pool
->process_prepared_mapping
);
1285 process_prepared(pool
, &pool
->prepared_discards
, &pool
->process_prepared_discard
);
1286 process_deferred_bios(pool
);
1290 * We want to commit periodically so that not too much
1291 * unwritten data builds up.
1293 static void do_waker(struct work_struct
*ws
)
1295 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
, waker
);
1297 queue_delayed_work(pool
->wq
, &pool
->waker
, COMMIT_PERIOD
);
1300 /*----------------------------------------------------------------*/
1302 static enum pool_mode
get_pool_mode(struct pool
*pool
)
1304 return pool
->pf
.mode
;
1307 static void set_pool_mode(struct pool
*pool
, enum pool_mode mode
)
1311 pool
->pf
.mode
= mode
;
1315 DMERR("switching pool to failure mode");
1316 pool
->process_bio
= process_bio_fail
;
1317 pool
->process_discard
= process_bio_fail
;
1318 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1319 pool
->process_prepared_discard
= process_prepared_discard_fail
;
1323 DMERR("switching pool to read-only mode");
1324 r
= dm_pool_abort_metadata(pool
->pmd
);
1326 DMERR("aborting transaction failed");
1327 set_pool_mode(pool
, PM_FAIL
);
1329 dm_pool_metadata_read_only(pool
->pmd
);
1330 pool
->process_bio
= process_bio_read_only
;
1331 pool
->process_discard
= process_discard
;
1332 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1333 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1338 pool
->process_bio
= process_bio
;
1339 pool
->process_discard
= process_discard
;
1340 pool
->process_prepared_mapping
= process_prepared_mapping
;
1341 pool
->process_prepared_discard
= process_prepared_discard
;
1346 /*----------------------------------------------------------------*/
1349 * Mapping functions.
1353 * Called only while mapping a thin bio to hand it over to the workqueue.
1355 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
)
1357 unsigned long flags
;
1358 struct pool
*pool
= tc
->pool
;
1360 spin_lock_irqsave(&pool
->lock
, flags
);
1361 bio_list_add(&pool
->deferred_bios
, bio
);
1362 spin_unlock_irqrestore(&pool
->lock
, flags
);
1367 static void thin_hook_bio(struct thin_c
*tc
, struct bio
*bio
)
1369 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1372 h
->shared_read_entry
= NULL
;
1373 h
->all_io_entry
= NULL
;
1374 h
->overwrite_mapping
= NULL
;
1378 * Non-blocking function called from the thin target's map function.
1380 static int thin_bio_map(struct dm_target
*ti
, struct bio
*bio
)
1383 struct thin_c
*tc
= ti
->private;
1384 dm_block_t block
= get_bio_block(tc
, bio
);
1385 struct dm_thin_device
*td
= tc
->td
;
1386 struct dm_thin_lookup_result result
;
1387 struct dm_bio_prison_cell
*cell1
, *cell2
;
1388 struct dm_cell_key key
;
1390 thin_hook_bio(tc
, bio
);
1392 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
1394 return DM_MAPIO_SUBMITTED
;
1397 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)) {
1398 thin_defer_bio(tc
, bio
);
1399 return DM_MAPIO_SUBMITTED
;
1402 r
= dm_thin_find_block(td
, block
, 0, &result
);
1405 * Note that we defer readahead too.
1409 if (unlikely(result
.shared
)) {
1411 * We have a race condition here between the
1412 * result.shared value returned by the lookup and
1413 * snapshot creation, which may cause new
1416 * To avoid this always quiesce the origin before
1417 * taking the snap. You want to do this anyway to
1418 * ensure a consistent application view
1421 * More distant ancestors are irrelevant. The
1422 * shared flag will be set in their case.
1424 thin_defer_bio(tc
, bio
);
1425 return DM_MAPIO_SUBMITTED
;
1428 build_virtual_key(tc
->td
, block
, &key
);
1429 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell1
))
1430 return DM_MAPIO_SUBMITTED
;
1432 build_data_key(tc
->td
, result
.block
, &key
);
1433 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell2
)) {
1434 cell_defer_no_holder(tc
, cell1
);
1435 return DM_MAPIO_SUBMITTED
;
1438 inc_all_io_entry(tc
->pool
, bio
);
1439 cell_defer_no_holder(tc
, cell2
);
1440 cell_defer_no_holder(tc
, cell1
);
1442 remap(tc
, bio
, result
.block
);
1443 return DM_MAPIO_REMAPPED
;
1446 if (get_pool_mode(tc
->pool
) == PM_READ_ONLY
) {
1448 * This block isn't provisioned, and we have no way
1449 * of doing so. Just error it.
1452 return DM_MAPIO_SUBMITTED
;
1458 * In future, the failed dm_thin_find_block above could
1459 * provide the hint to load the metadata into cache.
1461 thin_defer_bio(tc
, bio
);
1462 return DM_MAPIO_SUBMITTED
;
1466 * Must always call bio_io_error on failure.
1467 * dm_thin_find_block can fail with -EINVAL if the
1468 * pool is switched to fail-io mode.
1471 return DM_MAPIO_SUBMITTED
;
1475 static int pool_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
1478 unsigned long flags
;
1479 struct pool_c
*pt
= container_of(cb
, struct pool_c
, callbacks
);
1481 spin_lock_irqsave(&pt
->pool
->lock
, flags
);
1482 r
= !bio_list_empty(&pt
->pool
->retry_on_resume_list
);
1483 spin_unlock_irqrestore(&pt
->pool
->lock
, flags
);
1486 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1487 r
= bdi_congested(&q
->backing_dev_info
, bdi_bits
);
1493 static void __requeue_bios(struct pool
*pool
)
1495 bio_list_merge(&pool
->deferred_bios
, &pool
->retry_on_resume_list
);
1496 bio_list_init(&pool
->retry_on_resume_list
);
1499 /*----------------------------------------------------------------
1500 * Binding of control targets to a pool object
1501 *--------------------------------------------------------------*/
1502 static bool data_dev_supports_discard(struct pool_c
*pt
)
1504 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1506 return q
&& blk_queue_discard(q
);
1510 * If discard_passdown was enabled verify that the data device
1511 * supports discards. Disable discard_passdown if not.
1513 static void disable_passdown_if_not_supported(struct pool_c
*pt
)
1515 struct pool
*pool
= pt
->pool
;
1516 struct block_device
*data_bdev
= pt
->data_dev
->bdev
;
1517 struct queue_limits
*data_limits
= &bdev_get_queue(data_bdev
)->limits
;
1518 sector_t block_size
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
1519 const char *reason
= NULL
;
1520 char buf
[BDEVNAME_SIZE
];
1522 if (!pt
->adjusted_pf
.discard_passdown
)
1525 if (!data_dev_supports_discard(pt
))
1526 reason
= "discard unsupported";
1528 else if (data_limits
->max_discard_sectors
< pool
->sectors_per_block
)
1529 reason
= "max discard sectors smaller than a block";
1531 else if (data_limits
->discard_granularity
> block_size
)
1532 reason
= "discard granularity larger than a block";
1534 else if (block_size
& (data_limits
->discard_granularity
- 1))
1535 reason
= "discard granularity not a factor of block size";
1538 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev
, buf
), reason
);
1539 pt
->adjusted_pf
.discard_passdown
= false;
1543 static int bind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1545 struct pool_c
*pt
= ti
->private;
1548 * We want to make sure that degraded pools are never upgraded.
1550 enum pool_mode old_mode
= pool
->pf
.mode
;
1551 enum pool_mode new_mode
= pt
->adjusted_pf
.mode
;
1553 if (old_mode
> new_mode
)
1554 new_mode
= old_mode
;
1557 pool
->low_water_blocks
= pt
->low_water_blocks
;
1558 pool
->pf
= pt
->adjusted_pf
;
1560 set_pool_mode(pool
, new_mode
);
1565 static void unbind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1571 /*----------------------------------------------------------------
1573 *--------------------------------------------------------------*/
1574 /* Initialize pool features. */
1575 static void pool_features_init(struct pool_features
*pf
)
1577 pf
->mode
= PM_WRITE
;
1578 pf
->zero_new_blocks
= true;
1579 pf
->discard_enabled
= true;
1580 pf
->discard_passdown
= true;
1583 static void __pool_destroy(struct pool
*pool
)
1585 __pool_table_remove(pool
);
1587 if (dm_pool_metadata_close(pool
->pmd
) < 0)
1588 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
1590 dm_bio_prison_destroy(pool
->prison
);
1591 dm_kcopyd_client_destroy(pool
->copier
);
1594 destroy_workqueue(pool
->wq
);
1596 if (pool
->next_mapping
)
1597 mempool_free(pool
->next_mapping
, pool
->mapping_pool
);
1598 mempool_destroy(pool
->mapping_pool
);
1599 dm_deferred_set_destroy(pool
->shared_read_ds
);
1600 dm_deferred_set_destroy(pool
->all_io_ds
);
1604 static struct kmem_cache
*_new_mapping_cache
;
1606 static struct pool
*pool_create(struct mapped_device
*pool_md
,
1607 struct block_device
*metadata_dev
,
1608 unsigned long block_size
,
1609 int read_only
, char **error
)
1614 struct dm_pool_metadata
*pmd
;
1615 bool format_device
= read_only
? false : true;
1617 pmd
= dm_pool_metadata_open(metadata_dev
, block_size
, format_device
);
1619 *error
= "Error creating metadata object";
1620 return (struct pool
*)pmd
;
1623 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
1625 *error
= "Error allocating memory for pool";
1626 err_p
= ERR_PTR(-ENOMEM
);
1631 pool
->sectors_per_block
= block_size
;
1632 if (block_size
& (block_size
- 1))
1633 pool
->sectors_per_block_shift
= -1;
1635 pool
->sectors_per_block_shift
= __ffs(block_size
);
1636 pool
->low_water_blocks
= 0;
1637 pool_features_init(&pool
->pf
);
1638 pool
->prison
= dm_bio_prison_create(PRISON_CELLS
);
1639 if (!pool
->prison
) {
1640 *error
= "Error creating pool's bio prison";
1641 err_p
= ERR_PTR(-ENOMEM
);
1645 pool
->copier
= dm_kcopyd_client_create();
1646 if (IS_ERR(pool
->copier
)) {
1647 r
= PTR_ERR(pool
->copier
);
1648 *error
= "Error creating pool's kcopyd client";
1650 goto bad_kcopyd_client
;
1654 * Create singlethreaded workqueue that will service all devices
1655 * that use this metadata.
1657 pool
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
1659 *error
= "Error creating pool's workqueue";
1660 err_p
= ERR_PTR(-ENOMEM
);
1664 INIT_WORK(&pool
->worker
, do_worker
);
1665 INIT_DELAYED_WORK(&pool
->waker
, do_waker
);
1666 spin_lock_init(&pool
->lock
);
1667 bio_list_init(&pool
->deferred_bios
);
1668 bio_list_init(&pool
->deferred_flush_bios
);
1669 INIT_LIST_HEAD(&pool
->prepared_mappings
);
1670 INIT_LIST_HEAD(&pool
->prepared_discards
);
1671 pool
->low_water_triggered
= 0;
1672 pool
->no_free_space
= 0;
1673 bio_list_init(&pool
->retry_on_resume_list
);
1675 pool
->shared_read_ds
= dm_deferred_set_create();
1676 if (!pool
->shared_read_ds
) {
1677 *error
= "Error creating pool's shared read deferred set";
1678 err_p
= ERR_PTR(-ENOMEM
);
1679 goto bad_shared_read_ds
;
1682 pool
->all_io_ds
= dm_deferred_set_create();
1683 if (!pool
->all_io_ds
) {
1684 *error
= "Error creating pool's all io deferred set";
1685 err_p
= ERR_PTR(-ENOMEM
);
1689 pool
->next_mapping
= NULL
;
1690 pool
->mapping_pool
= mempool_create_slab_pool(MAPPING_POOL_SIZE
,
1691 _new_mapping_cache
);
1692 if (!pool
->mapping_pool
) {
1693 *error
= "Error creating pool's mapping mempool";
1694 err_p
= ERR_PTR(-ENOMEM
);
1695 goto bad_mapping_pool
;
1698 pool
->ref_count
= 1;
1699 pool
->last_commit_jiffies
= jiffies
;
1700 pool
->pool_md
= pool_md
;
1701 pool
->md_dev
= metadata_dev
;
1702 __pool_table_insert(pool
);
1707 dm_deferred_set_destroy(pool
->all_io_ds
);
1709 dm_deferred_set_destroy(pool
->shared_read_ds
);
1711 destroy_workqueue(pool
->wq
);
1713 dm_kcopyd_client_destroy(pool
->copier
);
1715 dm_bio_prison_destroy(pool
->prison
);
1719 if (dm_pool_metadata_close(pmd
))
1720 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
1725 static void __pool_inc(struct pool
*pool
)
1727 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
1731 static void __pool_dec(struct pool
*pool
)
1733 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
1734 BUG_ON(!pool
->ref_count
);
1735 if (!--pool
->ref_count
)
1736 __pool_destroy(pool
);
1739 static struct pool
*__pool_find(struct mapped_device
*pool_md
,
1740 struct block_device
*metadata_dev
,
1741 unsigned long block_size
, int read_only
,
1742 char **error
, int *created
)
1744 struct pool
*pool
= __pool_table_lookup_metadata_dev(metadata_dev
);
1747 if (pool
->pool_md
!= pool_md
) {
1748 *error
= "metadata device already in use by a pool";
1749 return ERR_PTR(-EBUSY
);
1754 pool
= __pool_table_lookup(pool_md
);
1756 if (pool
->md_dev
!= metadata_dev
) {
1757 *error
= "different pool cannot replace a pool";
1758 return ERR_PTR(-EINVAL
);
1763 pool
= pool_create(pool_md
, metadata_dev
, block_size
, read_only
, error
);
1771 /*----------------------------------------------------------------
1772 * Pool target methods
1773 *--------------------------------------------------------------*/
1774 static void pool_dtr(struct dm_target
*ti
)
1776 struct pool_c
*pt
= ti
->private;
1778 mutex_lock(&dm_thin_pool_table
.mutex
);
1780 unbind_control_target(pt
->pool
, ti
);
1781 __pool_dec(pt
->pool
);
1782 dm_put_device(ti
, pt
->metadata_dev
);
1783 dm_put_device(ti
, pt
->data_dev
);
1786 mutex_unlock(&dm_thin_pool_table
.mutex
);
1789 static int parse_pool_features(struct dm_arg_set
*as
, struct pool_features
*pf
,
1790 struct dm_target
*ti
)
1794 const char *arg_name
;
1796 static struct dm_arg _args
[] = {
1797 {0, 3, "Invalid number of pool feature arguments"},
1801 * No feature arguments supplied.
1806 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
1810 while (argc
&& !r
) {
1811 arg_name
= dm_shift_arg(as
);
1814 if (!strcasecmp(arg_name
, "skip_block_zeroing"))
1815 pf
->zero_new_blocks
= false;
1817 else if (!strcasecmp(arg_name
, "ignore_discard"))
1818 pf
->discard_enabled
= false;
1820 else if (!strcasecmp(arg_name
, "no_discard_passdown"))
1821 pf
->discard_passdown
= false;
1823 else if (!strcasecmp(arg_name
, "read_only"))
1824 pf
->mode
= PM_READ_ONLY
;
1827 ti
->error
= "Unrecognised pool feature requested";
1837 * thin-pool <metadata dev> <data dev>
1838 * <data block size (sectors)>
1839 * <low water mark (blocks)>
1840 * [<#feature args> [<arg>]*]
1842 * Optional feature arguments are:
1843 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
1844 * ignore_discard: disable discard
1845 * no_discard_passdown: don't pass discards down to the data device
1847 static int pool_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1849 int r
, pool_created
= 0;
1852 struct pool_features pf
;
1853 struct dm_arg_set as
;
1854 struct dm_dev
*data_dev
;
1855 unsigned long block_size
;
1856 dm_block_t low_water_blocks
;
1857 struct dm_dev
*metadata_dev
;
1858 sector_t metadata_dev_size
;
1859 char b
[BDEVNAME_SIZE
];
1862 * FIXME Remove validation from scope of lock.
1864 mutex_lock(&dm_thin_pool_table
.mutex
);
1867 ti
->error
= "Invalid argument count";
1874 r
= dm_get_device(ti
, argv
[0], FMODE_READ
| FMODE_WRITE
, &metadata_dev
);
1876 ti
->error
= "Error opening metadata block device";
1880 metadata_dev_size
= i_size_read(metadata_dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
1881 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS_WARNING
)
1882 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1883 bdevname(metadata_dev
->bdev
, b
), THIN_METADATA_MAX_SECTORS
);
1885 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &data_dev
);
1887 ti
->error
= "Error getting data device";
1891 if (kstrtoul(argv
[2], 10, &block_size
) || !block_size
||
1892 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
1893 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
1894 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
1895 ti
->error
= "Invalid block size";
1900 if (kstrtoull(argv
[3], 10, (unsigned long long *)&low_water_blocks
)) {
1901 ti
->error
= "Invalid low water mark";
1907 * Set default pool features.
1909 pool_features_init(&pf
);
1911 dm_consume_args(&as
, 4);
1912 r
= parse_pool_features(&as
, &pf
, ti
);
1916 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
1922 pool
= __pool_find(dm_table_get_md(ti
->table
), metadata_dev
->bdev
,
1923 block_size
, pf
.mode
== PM_READ_ONLY
, &ti
->error
, &pool_created
);
1930 * 'pool_created' reflects whether this is the first table load.
1931 * Top level discard support is not allowed to be changed after
1932 * initial load. This would require a pool reload to trigger thin
1935 if (!pool_created
&& pf
.discard_enabled
!= pool
->pf
.discard_enabled
) {
1936 ti
->error
= "Discard support cannot be disabled once enabled";
1938 goto out_flags_changed
;
1943 pt
->metadata_dev
= metadata_dev
;
1944 pt
->data_dev
= data_dev
;
1945 pt
->low_water_blocks
= low_water_blocks
;
1946 pt
->adjusted_pf
= pt
->requested_pf
= pf
;
1947 ti
->num_flush_bios
= 1;
1950 * Only need to enable discards if the pool should pass
1951 * them down to the data device. The thin device's discard
1952 * processing will cause mappings to be removed from the btree.
1954 if (pf
.discard_enabled
&& pf
.discard_passdown
) {
1955 ti
->num_discard_bios
= 1;
1958 * Setting 'discards_supported' circumvents the normal
1959 * stacking of discard limits (this keeps the pool and
1960 * thin devices' discard limits consistent).
1962 ti
->discards_supported
= true;
1963 ti
->discard_zeroes_data_unsupported
= true;
1967 pt
->callbacks
.congested_fn
= pool_is_congested
;
1968 dm_table_add_target_callbacks(ti
->table
, &pt
->callbacks
);
1970 mutex_unlock(&dm_thin_pool_table
.mutex
);
1979 dm_put_device(ti
, data_dev
);
1981 dm_put_device(ti
, metadata_dev
);
1983 mutex_unlock(&dm_thin_pool_table
.mutex
);
1988 static int pool_map(struct dm_target
*ti
, struct bio
*bio
)
1991 struct pool_c
*pt
= ti
->private;
1992 struct pool
*pool
= pt
->pool
;
1993 unsigned long flags
;
1996 * As this is a singleton target, ti->begin is always zero.
1998 spin_lock_irqsave(&pool
->lock
, flags
);
1999 bio
->bi_bdev
= pt
->data_dev
->bdev
;
2000 r
= DM_MAPIO_REMAPPED
;
2001 spin_unlock_irqrestore(&pool
->lock
, flags
);
2007 * Retrieves the number of blocks of the data device from
2008 * the superblock and compares it to the actual device size,
2009 * thus resizing the data device in case it has grown.
2011 * This both copes with opening preallocated data devices in the ctr
2012 * being followed by a resume
2014 * calling the resume method individually after userspace has
2015 * grown the data device in reaction to a table event.
2017 static int pool_preresume(struct dm_target
*ti
)
2020 struct pool_c
*pt
= ti
->private;
2021 struct pool
*pool
= pt
->pool
;
2022 sector_t data_size
= ti
->len
;
2023 dm_block_t sb_data_size
;
2026 * Take control of the pool object.
2028 r
= bind_control_target(pool
, ti
);
2032 (void) sector_div(data_size
, pool
->sectors_per_block
);
2034 r
= dm_pool_get_data_dev_size(pool
->pmd
, &sb_data_size
);
2036 DMERR("failed to retrieve data device size");
2040 if (data_size
< sb_data_size
) {
2041 DMERR("pool target too small, is %llu blocks (expected %llu)",
2042 (unsigned long long)data_size
, sb_data_size
);
2045 } else if (data_size
> sb_data_size
) {
2046 r
= dm_pool_resize_data_dev(pool
->pmd
, data_size
);
2048 DMERR("failed to resize data device");
2049 /* FIXME Stricter than necessary: Rollback transaction instead here */
2050 set_pool_mode(pool
, PM_READ_ONLY
);
2054 (void) commit_or_fallback(pool
);
2060 static void pool_resume(struct dm_target
*ti
)
2062 struct pool_c
*pt
= ti
->private;
2063 struct pool
*pool
= pt
->pool
;
2064 unsigned long flags
;
2066 spin_lock_irqsave(&pool
->lock
, flags
);
2067 pool
->low_water_triggered
= 0;
2068 pool
->no_free_space
= 0;
2069 __requeue_bios(pool
);
2070 spin_unlock_irqrestore(&pool
->lock
, flags
);
2072 do_waker(&pool
->waker
.work
);
2075 static void pool_postsuspend(struct dm_target
*ti
)
2077 struct pool_c
*pt
= ti
->private;
2078 struct pool
*pool
= pt
->pool
;
2080 cancel_delayed_work(&pool
->waker
);
2081 flush_workqueue(pool
->wq
);
2082 (void) commit_or_fallback(pool
);
2085 static int check_arg_count(unsigned argc
, unsigned args_required
)
2087 if (argc
!= args_required
) {
2088 DMWARN("Message received with %u arguments instead of %u.",
2089 argc
, args_required
);
2096 static int read_dev_id(char *arg
, dm_thin_id
*dev_id
, int warning
)
2098 if (!kstrtoull(arg
, 10, (unsigned long long *)dev_id
) &&
2099 *dev_id
<= MAX_DEV_ID
)
2103 DMWARN("Message received with invalid device id: %s", arg
);
2108 static int process_create_thin_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2113 r
= check_arg_count(argc
, 2);
2117 r
= read_dev_id(argv
[1], &dev_id
, 1);
2121 r
= dm_pool_create_thin(pool
->pmd
, dev_id
);
2123 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2131 static int process_create_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2134 dm_thin_id origin_dev_id
;
2137 r
= check_arg_count(argc
, 3);
2141 r
= read_dev_id(argv
[1], &dev_id
, 1);
2145 r
= read_dev_id(argv
[2], &origin_dev_id
, 1);
2149 r
= dm_pool_create_snap(pool
->pmd
, dev_id
, origin_dev_id
);
2151 DMWARN("Creation of new snapshot %s of device %s failed.",
2159 static int process_delete_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2164 r
= check_arg_count(argc
, 2);
2168 r
= read_dev_id(argv
[1], &dev_id
, 1);
2172 r
= dm_pool_delete_thin_device(pool
->pmd
, dev_id
);
2174 DMWARN("Deletion of thin device %s failed.", argv
[1]);
2179 static int process_set_transaction_id_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2181 dm_thin_id old_id
, new_id
;
2184 r
= check_arg_count(argc
, 3);
2188 if (kstrtoull(argv
[1], 10, (unsigned long long *)&old_id
)) {
2189 DMWARN("set_transaction_id message: Unrecognised id %s.", argv
[1]);
2193 if (kstrtoull(argv
[2], 10, (unsigned long long *)&new_id
)) {
2194 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv
[2]);
2198 r
= dm_pool_set_metadata_transaction_id(pool
->pmd
, old_id
, new_id
);
2200 DMWARN("Failed to change transaction id from %s to %s.",
2208 static int process_reserve_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2212 r
= check_arg_count(argc
, 1);
2216 (void) commit_or_fallback(pool
);
2218 r
= dm_pool_reserve_metadata_snap(pool
->pmd
);
2220 DMWARN("reserve_metadata_snap message failed.");
2225 static int process_release_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2229 r
= check_arg_count(argc
, 1);
2233 r
= dm_pool_release_metadata_snap(pool
->pmd
);
2235 DMWARN("release_metadata_snap message failed.");
2241 * Messages supported:
2242 * create_thin <dev_id>
2243 * create_snap <dev_id> <origin_id>
2245 * trim <dev_id> <new_size_in_sectors>
2246 * set_transaction_id <current_trans_id> <new_trans_id>
2247 * reserve_metadata_snap
2248 * release_metadata_snap
2250 static int pool_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
2253 struct pool_c
*pt
= ti
->private;
2254 struct pool
*pool
= pt
->pool
;
2256 if (!strcasecmp(argv
[0], "create_thin"))
2257 r
= process_create_thin_mesg(argc
, argv
, pool
);
2259 else if (!strcasecmp(argv
[0], "create_snap"))
2260 r
= process_create_snap_mesg(argc
, argv
, pool
);
2262 else if (!strcasecmp(argv
[0], "delete"))
2263 r
= process_delete_mesg(argc
, argv
, pool
);
2265 else if (!strcasecmp(argv
[0], "set_transaction_id"))
2266 r
= process_set_transaction_id_mesg(argc
, argv
, pool
);
2268 else if (!strcasecmp(argv
[0], "reserve_metadata_snap"))
2269 r
= process_reserve_metadata_snap_mesg(argc
, argv
, pool
);
2271 else if (!strcasecmp(argv
[0], "release_metadata_snap"))
2272 r
= process_release_metadata_snap_mesg(argc
, argv
, pool
);
2275 DMWARN("Unrecognised thin pool target message received: %s", argv
[0]);
2278 (void) commit_or_fallback(pool
);
2283 static void emit_flags(struct pool_features
*pf
, char *result
,
2284 unsigned sz
, unsigned maxlen
)
2286 unsigned count
= !pf
->zero_new_blocks
+ !pf
->discard_enabled
+
2287 !pf
->discard_passdown
+ (pf
->mode
== PM_READ_ONLY
);
2288 DMEMIT("%u ", count
);
2290 if (!pf
->zero_new_blocks
)
2291 DMEMIT("skip_block_zeroing ");
2293 if (!pf
->discard_enabled
)
2294 DMEMIT("ignore_discard ");
2296 if (!pf
->discard_passdown
)
2297 DMEMIT("no_discard_passdown ");
2299 if (pf
->mode
== PM_READ_ONLY
)
2300 DMEMIT("read_only ");
2305 * <transaction id> <used metadata sectors>/<total metadata sectors>
2306 * <used data sectors>/<total data sectors> <held metadata root>
2308 static void pool_status(struct dm_target
*ti
, status_type_t type
,
2309 unsigned status_flags
, char *result
, unsigned maxlen
)
2313 uint64_t transaction_id
;
2314 dm_block_t nr_free_blocks_data
;
2315 dm_block_t nr_free_blocks_metadata
;
2316 dm_block_t nr_blocks_data
;
2317 dm_block_t nr_blocks_metadata
;
2318 dm_block_t held_root
;
2319 char buf
[BDEVNAME_SIZE
];
2320 char buf2
[BDEVNAME_SIZE
];
2321 struct pool_c
*pt
= ti
->private;
2322 struct pool
*pool
= pt
->pool
;
2325 case STATUSTYPE_INFO
:
2326 if (get_pool_mode(pool
) == PM_FAIL
) {
2331 /* Commit to ensure statistics aren't out-of-date */
2332 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
))
2333 (void) commit_or_fallback(pool
);
2335 r
= dm_pool_get_metadata_transaction_id(pool
->pmd
, &transaction_id
);
2337 DMERR("dm_pool_get_metadata_transaction_id returned %d", r
);
2341 r
= dm_pool_get_free_metadata_block_count(pool
->pmd
, &nr_free_blocks_metadata
);
2343 DMERR("dm_pool_get_free_metadata_block_count returned %d", r
);
2347 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &nr_blocks_metadata
);
2349 DMERR("dm_pool_get_metadata_dev_size returned %d", r
);
2353 r
= dm_pool_get_free_block_count(pool
->pmd
, &nr_free_blocks_data
);
2355 DMERR("dm_pool_get_free_block_count returned %d", r
);
2359 r
= dm_pool_get_data_dev_size(pool
->pmd
, &nr_blocks_data
);
2361 DMERR("dm_pool_get_data_dev_size returned %d", r
);
2365 r
= dm_pool_get_metadata_snap(pool
->pmd
, &held_root
);
2367 DMERR("dm_pool_get_metadata_snap returned %d", r
);
2371 DMEMIT("%llu %llu/%llu %llu/%llu ",
2372 (unsigned long long)transaction_id
,
2373 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
2374 (unsigned long long)nr_blocks_metadata
,
2375 (unsigned long long)(nr_blocks_data
- nr_free_blocks_data
),
2376 (unsigned long long)nr_blocks_data
);
2379 DMEMIT("%llu ", held_root
);
2383 if (pool
->pf
.mode
== PM_READ_ONLY
)
2388 if (!pool
->pf
.discard_enabled
)
2389 DMEMIT("ignore_discard");
2390 else if (pool
->pf
.discard_passdown
)
2391 DMEMIT("discard_passdown");
2393 DMEMIT("no_discard_passdown");
2397 case STATUSTYPE_TABLE
:
2398 DMEMIT("%s %s %lu %llu ",
2399 format_dev_t(buf
, pt
->metadata_dev
->bdev
->bd_dev
),
2400 format_dev_t(buf2
, pt
->data_dev
->bdev
->bd_dev
),
2401 (unsigned long)pool
->sectors_per_block
,
2402 (unsigned long long)pt
->low_water_blocks
);
2403 emit_flags(&pt
->requested_pf
, result
, sz
, maxlen
);
2412 static int pool_iterate_devices(struct dm_target
*ti
,
2413 iterate_devices_callout_fn fn
, void *data
)
2415 struct pool_c
*pt
= ti
->private;
2417 return fn(ti
, pt
->data_dev
, 0, ti
->len
, data
);
2420 static int pool_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
2421 struct bio_vec
*biovec
, int max_size
)
2423 struct pool_c
*pt
= ti
->private;
2424 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
2426 if (!q
->merge_bvec_fn
)
2429 bvm
->bi_bdev
= pt
->data_dev
->bdev
;
2431 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
2434 static void set_discard_limits(struct pool_c
*pt
, struct queue_limits
*limits
)
2436 struct pool
*pool
= pt
->pool
;
2437 struct queue_limits
*data_limits
;
2439 limits
->max_discard_sectors
= pool
->sectors_per_block
;
2442 * discard_granularity is just a hint, and not enforced.
2444 if (pt
->adjusted_pf
.discard_passdown
) {
2445 data_limits
= &bdev_get_queue(pt
->data_dev
->bdev
)->limits
;
2446 limits
->discard_granularity
= data_limits
->discard_granularity
;
2448 limits
->discard_granularity
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
2451 static void pool_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
2453 struct pool_c
*pt
= ti
->private;
2454 struct pool
*pool
= pt
->pool
;
2456 blk_limits_io_min(limits
, 0);
2457 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
2460 * pt->adjusted_pf is a staging area for the actual features to use.
2461 * They get transferred to the live pool in bind_control_target()
2462 * called from pool_preresume().
2464 if (!pt
->adjusted_pf
.discard_enabled
)
2467 disable_passdown_if_not_supported(pt
);
2469 set_discard_limits(pt
, limits
);
2472 static struct target_type pool_target
= {
2473 .name
= "thin-pool",
2474 .features
= DM_TARGET_SINGLETON
| DM_TARGET_ALWAYS_WRITEABLE
|
2475 DM_TARGET_IMMUTABLE
,
2476 .version
= {1, 6, 1},
2477 .module
= THIS_MODULE
,
2481 .postsuspend
= pool_postsuspend
,
2482 .preresume
= pool_preresume
,
2483 .resume
= pool_resume
,
2484 .message
= pool_message
,
2485 .status
= pool_status
,
2486 .merge
= pool_merge
,
2487 .iterate_devices
= pool_iterate_devices
,
2488 .io_hints
= pool_io_hints
,
2491 /*----------------------------------------------------------------
2492 * Thin target methods
2493 *--------------------------------------------------------------*/
2494 static void thin_dtr(struct dm_target
*ti
)
2496 struct thin_c
*tc
= ti
->private;
2498 mutex_lock(&dm_thin_pool_table
.mutex
);
2500 __pool_dec(tc
->pool
);
2501 dm_pool_close_thin_device(tc
->td
);
2502 dm_put_device(ti
, tc
->pool_dev
);
2504 dm_put_device(ti
, tc
->origin_dev
);
2507 mutex_unlock(&dm_thin_pool_table
.mutex
);
2511 * Thin target parameters:
2513 * <pool_dev> <dev_id> [origin_dev]
2515 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2516 * dev_id: the internal device identifier
2517 * origin_dev: a device external to the pool that should act as the origin
2519 * If the pool device has discards disabled, they get disabled for the thin
2522 static int thin_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2526 struct dm_dev
*pool_dev
, *origin_dev
;
2527 struct mapped_device
*pool_md
;
2529 mutex_lock(&dm_thin_pool_table
.mutex
);
2531 if (argc
!= 2 && argc
!= 3) {
2532 ti
->error
= "Invalid argument count";
2537 tc
= ti
->private = kzalloc(sizeof(*tc
), GFP_KERNEL
);
2539 ti
->error
= "Out of memory";
2545 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &origin_dev
);
2547 ti
->error
= "Error opening origin device";
2548 goto bad_origin_dev
;
2550 tc
->origin_dev
= origin_dev
;
2553 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &pool_dev
);
2555 ti
->error
= "Error opening pool device";
2558 tc
->pool_dev
= pool_dev
;
2560 if (read_dev_id(argv
[1], (unsigned long long *)&tc
->dev_id
, 0)) {
2561 ti
->error
= "Invalid device id";
2566 pool_md
= dm_get_md(tc
->pool_dev
->bdev
->bd_dev
);
2568 ti
->error
= "Couldn't get pool mapped device";
2573 tc
->pool
= __pool_table_lookup(pool_md
);
2575 ti
->error
= "Couldn't find pool object";
2577 goto bad_pool_lookup
;
2579 __pool_inc(tc
->pool
);
2581 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
2582 ti
->error
= "Couldn't open thin device, Pool is in fail mode";
2586 r
= dm_pool_open_thin_device(tc
->pool
->pmd
, tc
->dev_id
, &tc
->td
);
2588 ti
->error
= "Couldn't open thin internal device";
2592 r
= dm_set_target_max_io_len(ti
, tc
->pool
->sectors_per_block
);
2596 ti
->num_flush_bios
= 1;
2597 ti
->flush_supported
= true;
2598 ti
->per_bio_data_size
= sizeof(struct dm_thin_endio_hook
);
2600 /* In case the pool supports discards, pass them on. */
2601 if (tc
->pool
->pf
.discard_enabled
) {
2602 ti
->discards_supported
= true;
2603 ti
->num_discard_bios
= 1;
2604 ti
->discard_zeroes_data_unsupported
= true;
2605 /* Discard bios must be split on a block boundary */
2606 ti
->split_discard_bios
= true;
2611 mutex_unlock(&dm_thin_pool_table
.mutex
);
2616 __pool_dec(tc
->pool
);
2620 dm_put_device(ti
, tc
->pool_dev
);
2623 dm_put_device(ti
, tc
->origin_dev
);
2627 mutex_unlock(&dm_thin_pool_table
.mutex
);
2632 static int thin_map(struct dm_target
*ti
, struct bio
*bio
)
2634 bio
->bi_sector
= dm_target_offset(ti
, bio
->bi_sector
);
2636 return thin_bio_map(ti
, bio
);
2639 static int thin_endio(struct dm_target
*ti
, struct bio
*bio
, int err
)
2641 unsigned long flags
;
2642 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
2643 struct list_head work
;
2644 struct dm_thin_new_mapping
*m
, *tmp
;
2645 struct pool
*pool
= h
->tc
->pool
;
2647 if (h
->shared_read_entry
) {
2648 INIT_LIST_HEAD(&work
);
2649 dm_deferred_entry_dec(h
->shared_read_entry
, &work
);
2651 spin_lock_irqsave(&pool
->lock
, flags
);
2652 list_for_each_entry_safe(m
, tmp
, &work
, list
) {
2655 __maybe_add_mapping(m
);
2657 spin_unlock_irqrestore(&pool
->lock
, flags
);
2660 if (h
->all_io_entry
) {
2661 INIT_LIST_HEAD(&work
);
2662 dm_deferred_entry_dec(h
->all_io_entry
, &work
);
2663 if (!list_empty(&work
)) {
2664 spin_lock_irqsave(&pool
->lock
, flags
);
2665 list_for_each_entry_safe(m
, tmp
, &work
, list
)
2666 list_add(&m
->list
, &pool
->prepared_discards
);
2667 spin_unlock_irqrestore(&pool
->lock
, flags
);
2675 static void thin_postsuspend(struct dm_target
*ti
)
2677 if (dm_noflush_suspending(ti
))
2678 requeue_io((struct thin_c
*)ti
->private);
2682 * <nr mapped sectors> <highest mapped sector>
2684 static void thin_status(struct dm_target
*ti
, status_type_t type
,
2685 unsigned status_flags
, char *result
, unsigned maxlen
)
2689 dm_block_t mapped
, highest
;
2690 char buf
[BDEVNAME_SIZE
];
2691 struct thin_c
*tc
= ti
->private;
2693 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
2702 case STATUSTYPE_INFO
:
2703 r
= dm_thin_get_mapped_count(tc
->td
, &mapped
);
2705 DMERR("dm_thin_get_mapped_count returned %d", r
);
2709 r
= dm_thin_get_highest_mapped_block(tc
->td
, &highest
);
2711 DMERR("dm_thin_get_highest_mapped_block returned %d", r
);
2715 DMEMIT("%llu ", mapped
* tc
->pool
->sectors_per_block
);
2717 DMEMIT("%llu", ((highest
+ 1) *
2718 tc
->pool
->sectors_per_block
) - 1);
2723 case STATUSTYPE_TABLE
:
2725 format_dev_t(buf
, tc
->pool_dev
->bdev
->bd_dev
),
2726 (unsigned long) tc
->dev_id
);
2728 DMEMIT(" %s", format_dev_t(buf
, tc
->origin_dev
->bdev
->bd_dev
));
2739 static int thin_iterate_devices(struct dm_target
*ti
,
2740 iterate_devices_callout_fn fn
, void *data
)
2743 struct thin_c
*tc
= ti
->private;
2744 struct pool
*pool
= tc
->pool
;
2747 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2748 * we follow a more convoluted path through to the pool's target.
2751 return 0; /* nothing is bound */
2753 blocks
= pool
->ti
->len
;
2754 (void) sector_div(blocks
, pool
->sectors_per_block
);
2756 return fn(ti
, tc
->pool_dev
, 0, pool
->sectors_per_block
* blocks
, data
);
2761 static struct target_type thin_target
= {
2763 .version
= {1, 7, 1},
2764 .module
= THIS_MODULE
,
2768 .end_io
= thin_endio
,
2769 .postsuspend
= thin_postsuspend
,
2770 .status
= thin_status
,
2771 .iterate_devices
= thin_iterate_devices
,
2774 /*----------------------------------------------------------------*/
2776 static int __init
dm_thin_init(void)
2782 r
= dm_register_target(&thin_target
);
2786 r
= dm_register_target(&pool_target
);
2788 goto bad_pool_target
;
2792 _new_mapping_cache
= KMEM_CACHE(dm_thin_new_mapping
, 0);
2793 if (!_new_mapping_cache
)
2794 goto bad_new_mapping_cache
;
2798 bad_new_mapping_cache
:
2799 dm_unregister_target(&pool_target
);
2801 dm_unregister_target(&thin_target
);
2806 static void dm_thin_exit(void)
2808 dm_unregister_target(&thin_target
);
2809 dm_unregister_target(&pool_target
);
2811 kmem_cache_destroy(_new_mapping_cache
);
2814 module_init(dm_thin_init
);
2815 module_exit(dm_thin_exit
);
2817 MODULE_DESCRIPTION(DM_NAME
" thin provisioning target");
2818 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2819 MODULE_LICENSE("GPL");