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/jiffies.h>
15 #include <linux/log2.h>
16 #include <linux/list.h>
17 #include <linux/rculist.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/vmalloc.h>
22 #include <linux/sort.h>
23 #include <linux/rbtree.h>
25 #define DM_MSG_PREFIX "thin"
30 #define ENDIO_HOOK_POOL_SIZE 1024
31 #define MAPPING_POOL_SIZE 1024
32 #define COMMIT_PERIOD HZ
33 #define NO_SPACE_TIMEOUT_SECS 60
35 static unsigned no_space_timeout_secs
= NO_SPACE_TIMEOUT_SECS
;
37 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle
,
38 "A percentage of time allocated for copy on write");
41 * The block size of the device holding pool data must be
42 * between 64KB and 1GB.
44 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
45 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
48 * Device id is restricted to 24 bits.
50 #define MAX_DEV_ID ((1 << 24) - 1)
53 * How do we handle breaking sharing of data blocks?
54 * =================================================
56 * We use a standard copy-on-write btree to store the mappings for the
57 * devices (note I'm talking about copy-on-write of the metadata here, not
58 * the data). When you take an internal snapshot you clone the root node
59 * of the origin btree. After this there is no concept of an origin or a
60 * snapshot. They are just two device trees that happen to point to the
63 * When we get a write in we decide if it's to a shared data block using
64 * some timestamp magic. If it is, we have to break sharing.
66 * Let's say we write to a shared block in what was the origin. The
69 * i) plug io further to this physical block. (see bio_prison code).
71 * ii) quiesce any read io to that shared data block. Obviously
72 * including all devices that share this block. (see dm_deferred_set code)
74 * iii) copy the data block to a newly allocate block. This step can be
75 * missed out if the io covers the block. (schedule_copy).
77 * iv) insert the new mapping into the origin's btree
78 * (process_prepared_mapping). This act of inserting breaks some
79 * sharing of btree nodes between the two devices. Breaking sharing only
80 * effects the btree of that specific device. Btrees for the other
81 * devices that share the block never change. The btree for the origin
82 * device as it was after the last commit is untouched, ie. we're using
83 * persistent data structures in the functional programming sense.
85 * v) unplug io to this physical block, including the io that triggered
86 * the breaking of sharing.
88 * Steps (ii) and (iii) occur in parallel.
90 * The metadata _doesn't_ need to be committed before the io continues. We
91 * get away with this because the io is always written to a _new_ block.
92 * If there's a crash, then:
94 * - The origin mapping will point to the old origin block (the shared
95 * one). This will contain the data as it was before the io that triggered
96 * the breaking of sharing came in.
98 * - The snap mapping still points to the old block. As it would after
101 * The downside of this scheme is the timestamp magic isn't perfect, and
102 * will continue to think that data block in the snapshot device is shared
103 * even after the write to the origin has broken sharing. I suspect data
104 * blocks will typically be shared by many different devices, so we're
105 * breaking sharing n + 1 times, rather than n, where n is the number of
106 * devices that reference this data block. At the moment I think the
107 * benefits far, far outweigh the disadvantages.
110 /*----------------------------------------------------------------*/
115 static void build_data_key(struct dm_thin_device
*td
,
116 dm_block_t b
, struct dm_cell_key
*key
)
119 key
->dev
= dm_thin_dev_id(td
);
120 key
->block_begin
= b
;
121 key
->block_end
= b
+ 1ULL;
124 static void build_virtual_key(struct dm_thin_device
*td
, dm_block_t b
,
125 struct dm_cell_key
*key
)
128 key
->dev
= dm_thin_dev_id(td
);
129 key
->block_begin
= b
;
130 key
->block_end
= b
+ 1ULL;
133 /*----------------------------------------------------------------*/
135 #define THROTTLE_THRESHOLD (1 * HZ)
138 struct rw_semaphore lock
;
139 unsigned long threshold
;
140 bool throttle_applied
;
143 static void throttle_init(struct throttle
*t
)
145 init_rwsem(&t
->lock
);
146 t
->throttle_applied
= false;
149 static void throttle_work_start(struct throttle
*t
)
151 t
->threshold
= jiffies
+ THROTTLE_THRESHOLD
;
154 static void throttle_work_update(struct throttle
*t
)
156 if (!t
->throttle_applied
&& jiffies
> t
->threshold
) {
157 down_write(&t
->lock
);
158 t
->throttle_applied
= true;
162 static void throttle_work_complete(struct throttle
*t
)
164 if (t
->throttle_applied
) {
165 t
->throttle_applied
= false;
170 static void throttle_lock(struct throttle
*t
)
175 static void throttle_unlock(struct throttle
*t
)
180 /*----------------------------------------------------------------*/
183 * A pool device ties together a metadata device and a data device. It
184 * also provides the interface for creating and destroying internal
187 struct dm_thin_new_mapping
;
190 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
193 PM_WRITE
, /* metadata may be changed */
194 PM_OUT_OF_DATA_SPACE
, /* metadata may be changed, though data may not be allocated */
195 PM_READ_ONLY
, /* metadata may not be changed */
196 PM_FAIL
, /* all I/O fails */
199 struct pool_features
{
202 bool zero_new_blocks
:1;
203 bool discard_enabled
:1;
204 bool discard_passdown
:1;
205 bool error_if_no_space
:1;
209 typedef void (*process_bio_fn
)(struct thin_c
*tc
, struct bio
*bio
);
210 typedef void (*process_cell_fn
)(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
);
211 typedef void (*process_mapping_fn
)(struct dm_thin_new_mapping
*m
);
213 #define CELL_SORT_ARRAY_SIZE 8192
216 struct list_head list
;
217 struct dm_target
*ti
; /* Only set if a pool target is bound */
219 struct mapped_device
*pool_md
;
220 struct block_device
*md_dev
;
221 struct dm_pool_metadata
*pmd
;
223 dm_block_t low_water_blocks
;
224 uint32_t sectors_per_block
;
225 int sectors_per_block_shift
;
227 struct pool_features pf
;
228 bool low_water_triggered
:1; /* A dm event has been sent */
231 struct dm_bio_prison
*prison
;
232 struct dm_kcopyd_client
*copier
;
234 struct workqueue_struct
*wq
;
235 struct throttle throttle
;
236 struct work_struct worker
;
237 struct delayed_work waker
;
238 struct delayed_work no_space_timeout
;
240 unsigned long last_commit_jiffies
;
244 struct bio_list deferred_flush_bios
;
245 struct list_head prepared_mappings
;
246 struct list_head prepared_discards
;
247 struct list_head active_thins
;
249 struct dm_deferred_set
*shared_read_ds
;
250 struct dm_deferred_set
*all_io_ds
;
252 struct dm_thin_new_mapping
*next_mapping
;
253 mempool_t
*mapping_pool
;
255 process_bio_fn process_bio
;
256 process_bio_fn process_discard
;
258 process_cell_fn process_cell
;
259 process_cell_fn process_discard_cell
;
261 process_mapping_fn process_prepared_mapping
;
262 process_mapping_fn process_prepared_discard
;
264 struct dm_bio_prison_cell
**cell_sort_array
;
267 static enum pool_mode
get_pool_mode(struct pool
*pool
);
268 static void metadata_operation_failed(struct pool
*pool
, const char *op
, int r
);
271 * Target context for a pool.
274 struct dm_target
*ti
;
276 struct dm_dev
*data_dev
;
277 struct dm_dev
*metadata_dev
;
278 struct dm_target_callbacks callbacks
;
280 dm_block_t low_water_blocks
;
281 struct pool_features requested_pf
; /* Features requested during table load */
282 struct pool_features adjusted_pf
; /* Features used after adjusting for constituent devices */
286 * Target context for a thin.
289 struct list_head list
;
290 struct dm_dev
*pool_dev
;
291 struct dm_dev
*origin_dev
;
292 sector_t origin_size
;
296 struct dm_thin_device
*td
;
297 struct mapped_device
*thin_md
;
301 struct list_head deferred_cells
;
302 struct bio_list deferred_bio_list
;
303 struct bio_list retry_on_resume_list
;
304 struct rb_root sort_bio_list
; /* sorted list of deferred bios */
307 * Ensures the thin is not destroyed until the worker has finished
308 * iterating the active_thins list.
311 struct completion can_destroy
;
314 /*----------------------------------------------------------------*/
317 * wake_worker() is used when new work is queued and when pool_resume is
318 * ready to continue deferred IO processing.
320 static void wake_worker(struct pool
*pool
)
322 queue_work(pool
->wq
, &pool
->worker
);
325 /*----------------------------------------------------------------*/
327 static int bio_detain(struct pool
*pool
, struct dm_cell_key
*key
, struct bio
*bio
,
328 struct dm_bio_prison_cell
**cell_result
)
331 struct dm_bio_prison_cell
*cell_prealloc
;
334 * Allocate a cell from the prison's mempool.
335 * This might block but it can't fail.
337 cell_prealloc
= dm_bio_prison_alloc_cell(pool
->prison
, GFP_NOIO
);
339 r
= dm_bio_detain(pool
->prison
, key
, bio
, cell_prealloc
, cell_result
);
342 * We reused an old cell; we can get rid of
345 dm_bio_prison_free_cell(pool
->prison
, cell_prealloc
);
350 static void cell_release(struct pool
*pool
,
351 struct dm_bio_prison_cell
*cell
,
352 struct bio_list
*bios
)
354 dm_cell_release(pool
->prison
, cell
, bios
);
355 dm_bio_prison_free_cell(pool
->prison
, cell
);
358 static void cell_visit_release(struct pool
*pool
,
359 void (*fn
)(void *, struct dm_bio_prison_cell
*),
361 struct dm_bio_prison_cell
*cell
)
363 dm_cell_visit_release(pool
->prison
, fn
, context
, cell
);
364 dm_bio_prison_free_cell(pool
->prison
, cell
);
367 static void cell_release_no_holder(struct pool
*pool
,
368 struct dm_bio_prison_cell
*cell
,
369 struct bio_list
*bios
)
371 dm_cell_release_no_holder(pool
->prison
, cell
, bios
);
372 dm_bio_prison_free_cell(pool
->prison
, cell
);
375 static void cell_error_with_code(struct pool
*pool
,
376 struct dm_bio_prison_cell
*cell
, int error_code
)
378 dm_cell_error(pool
->prison
, cell
, error_code
);
379 dm_bio_prison_free_cell(pool
->prison
, cell
);
382 static void cell_error(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
384 cell_error_with_code(pool
, cell
, -EIO
);
387 static void cell_success(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
389 cell_error_with_code(pool
, cell
, 0);
392 static void cell_requeue(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
394 cell_error_with_code(pool
, cell
, DM_ENDIO_REQUEUE
);
397 /*----------------------------------------------------------------*/
400 * A global list of pools that uses a struct mapped_device as a key.
402 static struct dm_thin_pool_table
{
404 struct list_head pools
;
405 } dm_thin_pool_table
;
407 static void pool_table_init(void)
409 mutex_init(&dm_thin_pool_table
.mutex
);
410 INIT_LIST_HEAD(&dm_thin_pool_table
.pools
);
413 static void __pool_table_insert(struct pool
*pool
)
415 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
416 list_add(&pool
->list
, &dm_thin_pool_table
.pools
);
419 static void __pool_table_remove(struct pool
*pool
)
421 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
422 list_del(&pool
->list
);
425 static struct pool
*__pool_table_lookup(struct mapped_device
*md
)
427 struct pool
*pool
= NULL
, *tmp
;
429 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
431 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
432 if (tmp
->pool_md
== md
) {
441 static struct pool
*__pool_table_lookup_metadata_dev(struct block_device
*md_dev
)
443 struct pool
*pool
= NULL
, *tmp
;
445 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
447 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
448 if (tmp
->md_dev
== md_dev
) {
457 /*----------------------------------------------------------------*/
459 struct dm_thin_endio_hook
{
461 struct dm_deferred_entry
*shared_read_entry
;
462 struct dm_deferred_entry
*all_io_entry
;
463 struct dm_thin_new_mapping
*overwrite_mapping
;
464 struct rb_node rb_node
;
467 static void __merge_bio_list(struct bio_list
*bios
, struct bio_list
*master
)
469 bio_list_merge(bios
, master
);
470 bio_list_init(master
);
473 static void error_bio_list(struct bio_list
*bios
, int error
)
477 while ((bio
= bio_list_pop(bios
)))
478 bio_endio(bio
, error
);
481 static void error_thin_bio_list(struct thin_c
*tc
, struct bio_list
*master
, int error
)
483 struct bio_list bios
;
486 bio_list_init(&bios
);
488 spin_lock_irqsave(&tc
->lock
, flags
);
489 __merge_bio_list(&bios
, master
);
490 spin_unlock_irqrestore(&tc
->lock
, flags
);
492 error_bio_list(&bios
, error
);
495 static void requeue_deferred_cells(struct thin_c
*tc
)
497 struct pool
*pool
= tc
->pool
;
499 struct list_head cells
;
500 struct dm_bio_prison_cell
*cell
, *tmp
;
502 INIT_LIST_HEAD(&cells
);
504 spin_lock_irqsave(&tc
->lock
, flags
);
505 list_splice_init(&tc
->deferred_cells
, &cells
);
506 spin_unlock_irqrestore(&tc
->lock
, flags
);
508 list_for_each_entry_safe(cell
, tmp
, &cells
, user_list
)
509 cell_requeue(pool
, cell
);
512 static void requeue_io(struct thin_c
*tc
)
514 struct bio_list bios
;
517 bio_list_init(&bios
);
519 spin_lock_irqsave(&tc
->lock
, flags
);
520 __merge_bio_list(&bios
, &tc
->deferred_bio_list
);
521 __merge_bio_list(&bios
, &tc
->retry_on_resume_list
);
522 spin_unlock_irqrestore(&tc
->lock
, flags
);
524 error_bio_list(&bios
, DM_ENDIO_REQUEUE
);
525 requeue_deferred_cells(tc
);
528 static void error_retry_list(struct pool
*pool
)
533 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
)
534 error_thin_bio_list(tc
, &tc
->retry_on_resume_list
, -EIO
);
539 * This section of code contains the logic for processing a thin device's IO.
540 * Much of the code depends on pool object resources (lists, workqueues, etc)
541 * but most is exclusively called from the thin target rather than the thin-pool
545 static bool block_size_is_power_of_two(struct pool
*pool
)
547 return pool
->sectors_per_block_shift
>= 0;
550 static dm_block_t
get_bio_block(struct thin_c
*tc
, struct bio
*bio
)
552 struct pool
*pool
= tc
->pool
;
553 sector_t block_nr
= bio
->bi_iter
.bi_sector
;
555 if (block_size_is_power_of_two(pool
))
556 block_nr
>>= pool
->sectors_per_block_shift
;
558 (void) sector_div(block_nr
, pool
->sectors_per_block
);
563 static void remap(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
)
565 struct pool
*pool
= tc
->pool
;
566 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
568 bio
->bi_bdev
= tc
->pool_dev
->bdev
;
569 if (block_size_is_power_of_two(pool
))
570 bio
->bi_iter
.bi_sector
=
571 (block
<< pool
->sectors_per_block_shift
) |
572 (bi_sector
& (pool
->sectors_per_block
- 1));
574 bio
->bi_iter
.bi_sector
= (block
* pool
->sectors_per_block
) +
575 sector_div(bi_sector
, pool
->sectors_per_block
);
578 static void remap_to_origin(struct thin_c
*tc
, struct bio
*bio
)
580 bio
->bi_bdev
= tc
->origin_dev
->bdev
;
583 static int bio_triggers_commit(struct thin_c
*tc
, struct bio
*bio
)
585 return (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) &&
586 dm_thin_changed_this_transaction(tc
->td
);
589 static void inc_all_io_entry(struct pool
*pool
, struct bio
*bio
)
591 struct dm_thin_endio_hook
*h
;
593 if (bio
->bi_rw
& REQ_DISCARD
)
596 h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
597 h
->all_io_entry
= dm_deferred_entry_inc(pool
->all_io_ds
);
600 static void issue(struct thin_c
*tc
, struct bio
*bio
)
602 struct pool
*pool
= tc
->pool
;
605 if (!bio_triggers_commit(tc
, bio
)) {
606 generic_make_request(bio
);
611 * Complete bio with an error if earlier I/O caused changes to
612 * the metadata that can't be committed e.g, due to I/O errors
613 * on the metadata device.
615 if (dm_thin_aborted_changes(tc
->td
)) {
621 * Batch together any bios that trigger commits and then issue a
622 * single commit for them in process_deferred_bios().
624 spin_lock_irqsave(&pool
->lock
, flags
);
625 bio_list_add(&pool
->deferred_flush_bios
, bio
);
626 spin_unlock_irqrestore(&pool
->lock
, flags
);
629 static void remap_to_origin_and_issue(struct thin_c
*tc
, struct bio
*bio
)
631 remap_to_origin(tc
, bio
);
635 static void remap_and_issue(struct thin_c
*tc
, struct bio
*bio
,
638 remap(tc
, bio
, block
);
642 /*----------------------------------------------------------------*/
645 * Bio endio functions.
647 struct dm_thin_new_mapping
{
648 struct list_head list
;
651 bool definitely_not_shared
:1;
654 * Track quiescing, copying and zeroing preparation actions. When this
655 * counter hits zero the block is prepared and can be inserted into the
658 atomic_t prepare_actions
;
662 dm_block_t virt_block
;
663 dm_block_t data_block
;
664 struct dm_bio_prison_cell
*cell
, *cell2
;
667 * If the bio covers the whole area of a block then we can avoid
668 * zeroing or copying. Instead this bio is hooked. The bio will
669 * still be in the cell, so care has to be taken to avoid issuing
673 bio_end_io_t
*saved_bi_end_io
;
676 static void __complete_mapping_preparation(struct dm_thin_new_mapping
*m
)
678 struct pool
*pool
= m
->tc
->pool
;
680 if (atomic_dec_and_test(&m
->prepare_actions
)) {
681 list_add_tail(&m
->list
, &pool
->prepared_mappings
);
686 static void complete_mapping_preparation(struct dm_thin_new_mapping
*m
)
689 struct pool
*pool
= m
->tc
->pool
;
691 spin_lock_irqsave(&pool
->lock
, flags
);
692 __complete_mapping_preparation(m
);
693 spin_unlock_irqrestore(&pool
->lock
, flags
);
696 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
698 struct dm_thin_new_mapping
*m
= context
;
700 m
->err
= read_err
|| write_err
? -EIO
: 0;
701 complete_mapping_preparation(m
);
704 static void overwrite_endio(struct bio
*bio
, int err
)
706 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
707 struct dm_thin_new_mapping
*m
= h
->overwrite_mapping
;
710 complete_mapping_preparation(m
);
713 /*----------------------------------------------------------------*/
720 * Prepared mapping jobs.
724 * This sends the bios in the cell, except the original holder, back
725 * to the deferred_bios list.
727 static void cell_defer_no_holder(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
729 struct pool
*pool
= tc
->pool
;
732 spin_lock_irqsave(&tc
->lock
, flags
);
733 cell_release_no_holder(pool
, cell
, &tc
->deferred_bio_list
);
734 spin_unlock_irqrestore(&tc
->lock
, flags
);
739 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
);
743 struct bio_list defer_bios
;
744 struct bio_list issue_bios
;
747 static void __inc_remap_and_issue_cell(void *context
,
748 struct dm_bio_prison_cell
*cell
)
750 struct remap_info
*info
= context
;
753 while ((bio
= bio_list_pop(&cell
->bios
))) {
754 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
))
755 bio_list_add(&info
->defer_bios
, bio
);
757 inc_all_io_entry(info
->tc
->pool
, bio
);
760 * We can't issue the bios with the bio prison lock
761 * held, so we add them to a list to issue on
762 * return from this function.
764 bio_list_add(&info
->issue_bios
, bio
);
769 static void inc_remap_and_issue_cell(struct thin_c
*tc
,
770 struct dm_bio_prison_cell
*cell
,
774 struct remap_info info
;
777 bio_list_init(&info
.defer_bios
);
778 bio_list_init(&info
.issue_bios
);
781 * We have to be careful to inc any bios we're about to issue
782 * before the cell is released, and avoid a race with new bios
783 * being added to the cell.
785 cell_visit_release(tc
->pool
, __inc_remap_and_issue_cell
,
788 while ((bio
= bio_list_pop(&info
.defer_bios
)))
789 thin_defer_bio(tc
, bio
);
791 while ((bio
= bio_list_pop(&info
.issue_bios
)))
792 remap_and_issue(info
.tc
, bio
, block
);
795 static void process_prepared_mapping_fail(struct dm_thin_new_mapping
*m
)
798 m
->bio
->bi_end_io
= m
->saved_bi_end_io
;
799 atomic_inc(&m
->bio
->bi_remaining
);
801 cell_error(m
->tc
->pool
, m
->cell
);
803 mempool_free(m
, m
->tc
->pool
->mapping_pool
);
806 static void process_prepared_mapping(struct dm_thin_new_mapping
*m
)
808 struct thin_c
*tc
= m
->tc
;
809 struct pool
*pool
= tc
->pool
;
815 bio
->bi_end_io
= m
->saved_bi_end_io
;
816 atomic_inc(&bio
->bi_remaining
);
820 cell_error(pool
, m
->cell
);
825 * Commit the prepared block into the mapping btree.
826 * Any I/O for this block arriving after this point will get
827 * remapped to it directly.
829 r
= dm_thin_insert_block(tc
->td
, m
->virt_block
, m
->data_block
);
831 metadata_operation_failed(pool
, "dm_thin_insert_block", r
);
832 cell_error(pool
, m
->cell
);
837 * Release any bios held while the block was being provisioned.
838 * If we are processing a write bio that completely covers the block,
839 * we already processed it so can ignore it now when processing
840 * the bios in the cell.
843 inc_remap_and_issue_cell(tc
, m
->cell
, m
->data_block
);
846 inc_all_io_entry(tc
->pool
, m
->cell
->holder
);
847 remap_and_issue(tc
, m
->cell
->holder
, m
->data_block
);
848 inc_remap_and_issue_cell(tc
, m
->cell
, m
->data_block
);
853 mempool_free(m
, pool
->mapping_pool
);
856 static void process_prepared_discard_fail(struct dm_thin_new_mapping
*m
)
858 struct thin_c
*tc
= m
->tc
;
860 bio_io_error(m
->bio
);
861 cell_defer_no_holder(tc
, m
->cell
);
862 cell_defer_no_holder(tc
, m
->cell2
);
863 mempool_free(m
, tc
->pool
->mapping_pool
);
866 static void process_prepared_discard_passdown(struct dm_thin_new_mapping
*m
)
868 struct thin_c
*tc
= m
->tc
;
870 inc_all_io_entry(tc
->pool
, m
->bio
);
871 cell_defer_no_holder(tc
, m
->cell
);
872 cell_defer_no_holder(tc
, m
->cell2
);
875 if (m
->definitely_not_shared
)
876 remap_and_issue(tc
, m
->bio
, m
->data_block
);
879 if (dm_pool_block_is_used(tc
->pool
->pmd
, m
->data_block
, &used
) || used
)
880 bio_endio(m
->bio
, 0);
882 remap_and_issue(tc
, m
->bio
, m
->data_block
);
885 bio_endio(m
->bio
, 0);
887 mempool_free(m
, tc
->pool
->mapping_pool
);
890 static void process_prepared_discard(struct dm_thin_new_mapping
*m
)
893 struct thin_c
*tc
= m
->tc
;
895 r
= dm_thin_remove_block(tc
->td
, m
->virt_block
);
897 DMERR_LIMIT("dm_thin_remove_block() failed");
899 process_prepared_discard_passdown(m
);
902 static void process_prepared(struct pool
*pool
, struct list_head
*head
,
903 process_mapping_fn
*fn
)
906 struct list_head maps
;
907 struct dm_thin_new_mapping
*m
, *tmp
;
909 INIT_LIST_HEAD(&maps
);
910 spin_lock_irqsave(&pool
->lock
, flags
);
911 list_splice_init(head
, &maps
);
912 spin_unlock_irqrestore(&pool
->lock
, flags
);
914 list_for_each_entry_safe(m
, tmp
, &maps
, list
)
921 static int io_overlaps_block(struct pool
*pool
, struct bio
*bio
)
923 return bio
->bi_iter
.bi_size
==
924 (pool
->sectors_per_block
<< SECTOR_SHIFT
);
927 static int io_overwrites_block(struct pool
*pool
, struct bio
*bio
)
929 return (bio_data_dir(bio
) == WRITE
) &&
930 io_overlaps_block(pool
, bio
);
933 static void save_and_set_endio(struct bio
*bio
, bio_end_io_t
**save
,
936 *save
= bio
->bi_end_io
;
940 static int ensure_next_mapping(struct pool
*pool
)
942 if (pool
->next_mapping
)
945 pool
->next_mapping
= mempool_alloc(pool
->mapping_pool
, GFP_ATOMIC
);
947 return pool
->next_mapping
? 0 : -ENOMEM
;
950 static struct dm_thin_new_mapping
*get_next_mapping(struct pool
*pool
)
952 struct dm_thin_new_mapping
*m
= pool
->next_mapping
;
954 BUG_ON(!pool
->next_mapping
);
956 memset(m
, 0, sizeof(struct dm_thin_new_mapping
));
957 INIT_LIST_HEAD(&m
->list
);
960 pool
->next_mapping
= NULL
;
965 static void ll_zero(struct thin_c
*tc
, struct dm_thin_new_mapping
*m
,
966 sector_t begin
, sector_t end
)
969 struct dm_io_region to
;
971 to
.bdev
= tc
->pool_dev
->bdev
;
973 to
.count
= end
- begin
;
975 r
= dm_kcopyd_zero(tc
->pool
->copier
, 1, &to
, 0, copy_complete
, m
);
977 DMERR_LIMIT("dm_kcopyd_zero() failed");
978 copy_complete(1, 1, m
);
982 static void remap_and_issue_overwrite(struct thin_c
*tc
, struct bio
*bio
,
983 dm_block_t data_block
,
984 struct dm_thin_new_mapping
*m
)
986 struct pool
*pool
= tc
->pool
;
987 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
989 h
->overwrite_mapping
= m
;
991 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
992 inc_all_io_entry(pool
, bio
);
993 remap_and_issue(tc
, bio
, data_block
);
997 * A partial copy also needs to zero the uncopied region.
999 static void schedule_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1000 struct dm_dev
*origin
, dm_block_t data_origin
,
1001 dm_block_t data_dest
,
1002 struct dm_bio_prison_cell
*cell
, struct bio
*bio
,
1006 struct pool
*pool
= tc
->pool
;
1007 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
1010 m
->virt_block
= virt_block
;
1011 m
->data_block
= data_dest
;
1015 * quiesce action + copy action + an extra reference held for the
1016 * duration of this function (we may need to inc later for a
1019 atomic_set(&m
->prepare_actions
, 3);
1021 if (!dm_deferred_set_add_work(pool
->shared_read_ds
, &m
->list
))
1022 complete_mapping_preparation(m
); /* already quiesced */
1025 * IO to pool_dev remaps to the pool target's data_dev.
1027 * If the whole block of data is being overwritten, we can issue the
1028 * bio immediately. Otherwise we use kcopyd to clone the data first.
1030 if (io_overwrites_block(pool
, bio
))
1031 remap_and_issue_overwrite(tc
, bio
, data_dest
, m
);
1033 struct dm_io_region from
, to
;
1035 from
.bdev
= origin
->bdev
;
1036 from
.sector
= data_origin
* pool
->sectors_per_block
;
1039 to
.bdev
= tc
->pool_dev
->bdev
;
1040 to
.sector
= data_dest
* pool
->sectors_per_block
;
1043 r
= dm_kcopyd_copy(pool
->copier
, &from
, 1, &to
,
1044 0, copy_complete
, m
);
1046 DMERR_LIMIT("dm_kcopyd_copy() failed");
1047 copy_complete(1, 1, m
);
1050 * We allow the zero to be issued, to simplify the
1051 * error path. Otherwise we'd need to start
1052 * worrying about decrementing the prepare_actions
1058 * Do we need to zero a tail region?
1060 if (len
< pool
->sectors_per_block
&& pool
->pf
.zero_new_blocks
) {
1061 atomic_inc(&m
->prepare_actions
);
1063 data_dest
* pool
->sectors_per_block
+ len
,
1064 (data_dest
+ 1) * pool
->sectors_per_block
);
1068 complete_mapping_preparation(m
); /* drop our ref */
1071 static void schedule_internal_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1072 dm_block_t data_origin
, dm_block_t data_dest
,
1073 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
1075 schedule_copy(tc
, virt_block
, tc
->pool_dev
,
1076 data_origin
, data_dest
, cell
, bio
,
1077 tc
->pool
->sectors_per_block
);
1080 static void schedule_zero(struct thin_c
*tc
, dm_block_t virt_block
,
1081 dm_block_t data_block
, struct dm_bio_prison_cell
*cell
,
1084 struct pool
*pool
= tc
->pool
;
1085 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
1087 atomic_set(&m
->prepare_actions
, 1); /* no need to quiesce */
1089 m
->virt_block
= virt_block
;
1090 m
->data_block
= data_block
;
1094 * If the whole block of data is being overwritten or we are not
1095 * zeroing pre-existing data, we can issue the bio immediately.
1096 * Otherwise we use kcopyd to zero the data first.
1098 if (!pool
->pf
.zero_new_blocks
)
1099 process_prepared_mapping(m
);
1101 else if (io_overwrites_block(pool
, bio
))
1102 remap_and_issue_overwrite(tc
, bio
, data_block
, m
);
1106 data_block
* pool
->sectors_per_block
,
1107 (data_block
+ 1) * pool
->sectors_per_block
);
1110 static void schedule_external_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1111 dm_block_t data_dest
,
1112 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
1114 struct pool
*pool
= tc
->pool
;
1115 sector_t virt_block_begin
= virt_block
* pool
->sectors_per_block
;
1116 sector_t virt_block_end
= (virt_block
+ 1) * pool
->sectors_per_block
;
1118 if (virt_block_end
<= tc
->origin_size
)
1119 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
1120 virt_block
, data_dest
, cell
, bio
,
1121 pool
->sectors_per_block
);
1123 else if (virt_block_begin
< tc
->origin_size
)
1124 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
1125 virt_block
, data_dest
, cell
, bio
,
1126 tc
->origin_size
- virt_block_begin
);
1129 schedule_zero(tc
, virt_block
, data_dest
, cell
, bio
);
1132 static void set_pool_mode(struct pool
*pool
, enum pool_mode new_mode
);
1134 static void check_for_space(struct pool
*pool
)
1139 if (get_pool_mode(pool
) != PM_OUT_OF_DATA_SPACE
)
1142 r
= dm_pool_get_free_block_count(pool
->pmd
, &nr_free
);
1147 set_pool_mode(pool
, PM_WRITE
);
1151 * A non-zero return indicates read_only or fail_io mode.
1152 * Many callers don't care about the return value.
1154 static int commit(struct pool
*pool
)
1158 if (get_pool_mode(pool
) >= PM_READ_ONLY
)
1161 r
= dm_pool_commit_metadata(pool
->pmd
);
1163 metadata_operation_failed(pool
, "dm_pool_commit_metadata", r
);
1165 check_for_space(pool
);
1170 static void check_low_water_mark(struct pool
*pool
, dm_block_t free_blocks
)
1172 unsigned long flags
;
1174 if (free_blocks
<= pool
->low_water_blocks
&& !pool
->low_water_triggered
) {
1175 DMWARN("%s: reached low water mark for data device: sending event.",
1176 dm_device_name(pool
->pool_md
));
1177 spin_lock_irqsave(&pool
->lock
, flags
);
1178 pool
->low_water_triggered
= true;
1179 spin_unlock_irqrestore(&pool
->lock
, flags
);
1180 dm_table_event(pool
->ti
->table
);
1184 static int alloc_data_block(struct thin_c
*tc
, dm_block_t
*result
)
1187 dm_block_t free_blocks
;
1188 struct pool
*pool
= tc
->pool
;
1190 if (WARN_ON(get_pool_mode(pool
) != PM_WRITE
))
1193 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
1195 metadata_operation_failed(pool
, "dm_pool_get_free_block_count", r
);
1199 check_low_water_mark(pool
, free_blocks
);
1203 * Try to commit to see if that will free up some
1210 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
1212 metadata_operation_failed(pool
, "dm_pool_get_free_block_count", r
);
1217 set_pool_mode(pool
, PM_OUT_OF_DATA_SPACE
);
1222 r
= dm_pool_alloc_data_block(pool
->pmd
, result
);
1224 metadata_operation_failed(pool
, "dm_pool_alloc_data_block", r
);
1232 * If we have run out of space, queue bios until the device is
1233 * resumed, presumably after having been reloaded with more space.
1235 static void retry_on_resume(struct bio
*bio
)
1237 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1238 struct thin_c
*tc
= h
->tc
;
1239 unsigned long flags
;
1241 spin_lock_irqsave(&tc
->lock
, flags
);
1242 bio_list_add(&tc
->retry_on_resume_list
, bio
);
1243 spin_unlock_irqrestore(&tc
->lock
, flags
);
1246 static int should_error_unserviceable_bio(struct pool
*pool
)
1248 enum pool_mode m
= get_pool_mode(pool
);
1252 /* Shouldn't get here */
1253 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1256 case PM_OUT_OF_DATA_SPACE
:
1257 return pool
->pf
.error_if_no_space
? -ENOSPC
: 0;
1263 /* Shouldn't get here */
1264 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1269 static void handle_unserviceable_bio(struct pool
*pool
, struct bio
*bio
)
1271 int error
= should_error_unserviceable_bio(pool
);
1274 bio_endio(bio
, error
);
1276 retry_on_resume(bio
);
1279 static void retry_bios_on_resume(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
1282 struct bio_list bios
;
1285 error
= should_error_unserviceable_bio(pool
);
1287 cell_error_with_code(pool
, cell
, error
);
1291 bio_list_init(&bios
);
1292 cell_release(pool
, cell
, &bios
);
1294 while ((bio
= bio_list_pop(&bios
)))
1295 retry_on_resume(bio
);
1298 static void process_discard_cell(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
1301 struct bio
*bio
= cell
->holder
;
1302 struct pool
*pool
= tc
->pool
;
1303 struct dm_bio_prison_cell
*cell2
;
1304 struct dm_cell_key key2
;
1305 dm_block_t block
= get_bio_block(tc
, bio
);
1306 struct dm_thin_lookup_result lookup_result
;
1307 struct dm_thin_new_mapping
*m
;
1309 if (tc
->requeue_mode
) {
1310 cell_requeue(pool
, cell
);
1314 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1318 * Check nobody is fiddling with this pool block. This can
1319 * happen if someone's in the process of breaking sharing
1322 build_data_key(tc
->td
, lookup_result
.block
, &key2
);
1323 if (bio_detain(tc
->pool
, &key2
, bio
, &cell2
)) {
1324 cell_defer_no_holder(tc
, cell
);
1328 if (io_overlaps_block(pool
, bio
)) {
1330 * IO may still be going to the destination block. We must
1331 * quiesce before we can do the removal.
1333 m
= get_next_mapping(pool
);
1335 m
->pass_discard
= pool
->pf
.discard_passdown
;
1336 m
->definitely_not_shared
= !lookup_result
.shared
;
1337 m
->virt_block
= block
;
1338 m
->data_block
= lookup_result
.block
;
1343 if (!dm_deferred_set_add_work(pool
->all_io_ds
, &m
->list
))
1344 pool
->process_prepared_discard(m
);
1347 inc_all_io_entry(pool
, bio
);
1348 cell_defer_no_holder(tc
, cell
);
1349 cell_defer_no_holder(tc
, cell2
);
1352 * The DM core makes sure that the discard doesn't span
1353 * a block boundary. So we submit the discard of a
1354 * partial block appropriately.
1356 if ((!lookup_result
.shared
) && pool
->pf
.discard_passdown
)
1357 remap_and_issue(tc
, bio
, lookup_result
.block
);
1365 * It isn't provisioned, just forget it.
1367 cell_defer_no_holder(tc
, cell
);
1372 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1374 cell_defer_no_holder(tc
, cell
);
1380 static void process_discard_bio(struct thin_c
*tc
, struct bio
*bio
)
1382 struct dm_bio_prison_cell
*cell
;
1383 struct dm_cell_key key
;
1384 dm_block_t block
= get_bio_block(tc
, bio
);
1386 build_virtual_key(tc
->td
, block
, &key
);
1387 if (bio_detain(tc
->pool
, &key
, bio
, &cell
))
1390 process_discard_cell(tc
, cell
);
1393 static void break_sharing(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1394 struct dm_cell_key
*key
,
1395 struct dm_thin_lookup_result
*lookup_result
,
1396 struct dm_bio_prison_cell
*cell
)
1399 dm_block_t data_block
;
1400 struct pool
*pool
= tc
->pool
;
1402 r
= alloc_data_block(tc
, &data_block
);
1405 schedule_internal_copy(tc
, block
, lookup_result
->block
,
1406 data_block
, cell
, bio
);
1410 retry_bios_on_resume(pool
, cell
);
1414 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1416 cell_error(pool
, cell
);
1421 static void __remap_and_issue_shared_cell(void *context
,
1422 struct dm_bio_prison_cell
*cell
)
1424 struct remap_info
*info
= context
;
1427 while ((bio
= bio_list_pop(&cell
->bios
))) {
1428 if ((bio_data_dir(bio
) == WRITE
) ||
1429 (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)))
1430 bio_list_add(&info
->defer_bios
, bio
);
1432 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));;
1434 h
->shared_read_entry
= dm_deferred_entry_inc(info
->tc
->pool
->shared_read_ds
);
1435 inc_all_io_entry(info
->tc
->pool
, bio
);
1436 bio_list_add(&info
->issue_bios
, bio
);
1441 static void remap_and_issue_shared_cell(struct thin_c
*tc
,
1442 struct dm_bio_prison_cell
*cell
,
1446 struct remap_info info
;
1449 bio_list_init(&info
.defer_bios
);
1450 bio_list_init(&info
.issue_bios
);
1452 cell_visit_release(tc
->pool
, __remap_and_issue_shared_cell
,
1455 while ((bio
= bio_list_pop(&info
.defer_bios
)))
1456 thin_defer_bio(tc
, bio
);
1458 while ((bio
= bio_list_pop(&info
.issue_bios
)))
1459 remap_and_issue(tc
, bio
, block
);
1462 static void process_shared_bio(struct thin_c
*tc
, struct bio
*bio
,
1464 struct dm_thin_lookup_result
*lookup_result
,
1465 struct dm_bio_prison_cell
*virt_cell
)
1467 struct dm_bio_prison_cell
*data_cell
;
1468 struct pool
*pool
= tc
->pool
;
1469 struct dm_cell_key key
;
1472 * If cell is already occupied, then sharing is already in the process
1473 * of being broken so we have nothing further to do here.
1475 build_data_key(tc
->td
, lookup_result
->block
, &key
);
1476 if (bio_detain(pool
, &key
, bio
, &data_cell
)) {
1477 cell_defer_no_holder(tc
, virt_cell
);
1481 if (bio_data_dir(bio
) == WRITE
&& bio
->bi_iter
.bi_size
) {
1482 break_sharing(tc
, bio
, block
, &key
, lookup_result
, data_cell
);
1483 cell_defer_no_holder(tc
, virt_cell
);
1485 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1487 h
->shared_read_entry
= dm_deferred_entry_inc(pool
->shared_read_ds
);
1488 inc_all_io_entry(pool
, bio
);
1489 remap_and_issue(tc
, bio
, lookup_result
->block
);
1491 remap_and_issue_shared_cell(tc
, data_cell
, lookup_result
->block
);
1492 remap_and_issue_shared_cell(tc
, virt_cell
, lookup_result
->block
);
1496 static void provision_block(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1497 struct dm_bio_prison_cell
*cell
)
1500 dm_block_t data_block
;
1501 struct pool
*pool
= tc
->pool
;
1504 * Remap empty bios (flushes) immediately, without provisioning.
1506 if (!bio
->bi_iter
.bi_size
) {
1507 inc_all_io_entry(pool
, bio
);
1508 cell_defer_no_holder(tc
, cell
);
1510 remap_and_issue(tc
, bio
, 0);
1515 * Fill read bios with zeroes and complete them immediately.
1517 if (bio_data_dir(bio
) == READ
) {
1519 cell_defer_no_holder(tc
, cell
);
1524 r
= alloc_data_block(tc
, &data_block
);
1528 schedule_external_copy(tc
, block
, data_block
, cell
, bio
);
1530 schedule_zero(tc
, block
, data_block
, cell
, bio
);
1534 retry_bios_on_resume(pool
, cell
);
1538 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1540 cell_error(pool
, cell
);
1545 static void process_cell(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
1548 struct pool
*pool
= tc
->pool
;
1549 struct bio
*bio
= cell
->holder
;
1550 dm_block_t block
= get_bio_block(tc
, bio
);
1551 struct dm_thin_lookup_result lookup_result
;
1553 if (tc
->requeue_mode
) {
1554 cell_requeue(pool
, cell
);
1558 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1561 if (lookup_result
.shared
)
1562 process_shared_bio(tc
, bio
, block
, &lookup_result
, cell
);
1564 inc_all_io_entry(pool
, bio
);
1565 remap_and_issue(tc
, bio
, lookup_result
.block
);
1566 inc_remap_and_issue_cell(tc
, cell
, lookup_result
.block
);
1571 if (bio_data_dir(bio
) == READ
&& tc
->origin_dev
) {
1572 inc_all_io_entry(pool
, bio
);
1573 cell_defer_no_holder(tc
, cell
);
1575 if (bio_end_sector(bio
) <= tc
->origin_size
)
1576 remap_to_origin_and_issue(tc
, bio
);
1578 else if (bio
->bi_iter
.bi_sector
< tc
->origin_size
) {
1580 bio
->bi_iter
.bi_size
= (tc
->origin_size
- bio
->bi_iter
.bi_sector
) << SECTOR_SHIFT
;
1581 remap_to_origin_and_issue(tc
, bio
);
1588 provision_block(tc
, bio
, block
, cell
);
1592 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1594 cell_defer_no_holder(tc
, cell
);
1600 static void process_bio(struct thin_c
*tc
, struct bio
*bio
)
1602 struct pool
*pool
= tc
->pool
;
1603 dm_block_t block
= get_bio_block(tc
, bio
);
1604 struct dm_bio_prison_cell
*cell
;
1605 struct dm_cell_key key
;
1608 * If cell is already occupied, then the block is already
1609 * being provisioned so we have nothing further to do here.
1611 build_virtual_key(tc
->td
, block
, &key
);
1612 if (bio_detain(pool
, &key
, bio
, &cell
))
1615 process_cell(tc
, cell
);
1618 static void __process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
,
1619 struct dm_bio_prison_cell
*cell
)
1622 int rw
= bio_data_dir(bio
);
1623 dm_block_t block
= get_bio_block(tc
, bio
);
1624 struct dm_thin_lookup_result lookup_result
;
1626 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1629 if (lookup_result
.shared
&& (rw
== WRITE
) && bio
->bi_iter
.bi_size
) {
1630 handle_unserviceable_bio(tc
->pool
, bio
);
1632 cell_defer_no_holder(tc
, cell
);
1634 inc_all_io_entry(tc
->pool
, bio
);
1635 remap_and_issue(tc
, bio
, lookup_result
.block
);
1637 inc_remap_and_issue_cell(tc
, cell
, lookup_result
.block
);
1643 cell_defer_no_holder(tc
, cell
);
1645 handle_unserviceable_bio(tc
->pool
, bio
);
1649 if (tc
->origin_dev
) {
1650 inc_all_io_entry(tc
->pool
, bio
);
1651 remap_to_origin_and_issue(tc
, bio
);
1660 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1663 cell_defer_no_holder(tc
, cell
);
1669 static void process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
)
1671 __process_bio_read_only(tc
, bio
, NULL
);
1674 static void process_cell_read_only(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
1676 __process_bio_read_only(tc
, cell
->holder
, cell
);
1679 static void process_bio_success(struct thin_c
*tc
, struct bio
*bio
)
1684 static void process_bio_fail(struct thin_c
*tc
, struct bio
*bio
)
1689 static void process_cell_success(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
1691 cell_success(tc
->pool
, cell
);
1694 static void process_cell_fail(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
1696 cell_error(tc
->pool
, cell
);
1700 * FIXME: should we also commit due to size of transaction, measured in
1703 static int need_commit_due_to_time(struct pool
*pool
)
1705 return !time_in_range(jiffies
, pool
->last_commit_jiffies
,
1706 pool
->last_commit_jiffies
+ COMMIT_PERIOD
);
1709 #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1710 #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1712 static void __thin_bio_rb_add(struct thin_c
*tc
, struct bio
*bio
)
1714 struct rb_node
**rbp
, *parent
;
1715 struct dm_thin_endio_hook
*pbd
;
1716 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
1718 rbp
= &tc
->sort_bio_list
.rb_node
;
1722 pbd
= thin_pbd(parent
);
1724 if (bi_sector
< thin_bio(pbd
)->bi_iter
.bi_sector
)
1725 rbp
= &(*rbp
)->rb_left
;
1727 rbp
= &(*rbp
)->rb_right
;
1730 pbd
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1731 rb_link_node(&pbd
->rb_node
, parent
, rbp
);
1732 rb_insert_color(&pbd
->rb_node
, &tc
->sort_bio_list
);
1735 static void __extract_sorted_bios(struct thin_c
*tc
)
1737 struct rb_node
*node
;
1738 struct dm_thin_endio_hook
*pbd
;
1741 for (node
= rb_first(&tc
->sort_bio_list
); node
; node
= rb_next(node
)) {
1742 pbd
= thin_pbd(node
);
1743 bio
= thin_bio(pbd
);
1745 bio_list_add(&tc
->deferred_bio_list
, bio
);
1746 rb_erase(&pbd
->rb_node
, &tc
->sort_bio_list
);
1749 WARN_ON(!RB_EMPTY_ROOT(&tc
->sort_bio_list
));
1752 static void __sort_thin_deferred_bios(struct thin_c
*tc
)
1755 struct bio_list bios
;
1757 bio_list_init(&bios
);
1758 bio_list_merge(&bios
, &tc
->deferred_bio_list
);
1759 bio_list_init(&tc
->deferred_bio_list
);
1761 /* Sort deferred_bio_list using rb-tree */
1762 while ((bio
= bio_list_pop(&bios
)))
1763 __thin_bio_rb_add(tc
, bio
);
1766 * Transfer the sorted bios in sort_bio_list back to
1767 * deferred_bio_list to allow lockless submission of
1770 __extract_sorted_bios(tc
);
1773 static void process_thin_deferred_bios(struct thin_c
*tc
)
1775 struct pool
*pool
= tc
->pool
;
1776 unsigned long flags
;
1778 struct bio_list bios
;
1779 struct blk_plug plug
;
1782 if (tc
->requeue_mode
) {
1783 error_thin_bio_list(tc
, &tc
->deferred_bio_list
, DM_ENDIO_REQUEUE
);
1787 bio_list_init(&bios
);
1789 spin_lock_irqsave(&tc
->lock
, flags
);
1791 if (bio_list_empty(&tc
->deferred_bio_list
)) {
1792 spin_unlock_irqrestore(&tc
->lock
, flags
);
1796 __sort_thin_deferred_bios(tc
);
1798 bio_list_merge(&bios
, &tc
->deferred_bio_list
);
1799 bio_list_init(&tc
->deferred_bio_list
);
1801 spin_unlock_irqrestore(&tc
->lock
, flags
);
1803 blk_start_plug(&plug
);
1804 while ((bio
= bio_list_pop(&bios
))) {
1806 * If we've got no free new_mapping structs, and processing
1807 * this bio might require one, we pause until there are some
1808 * prepared mappings to process.
1810 if (ensure_next_mapping(pool
)) {
1811 spin_lock_irqsave(&tc
->lock
, flags
);
1812 bio_list_add(&tc
->deferred_bio_list
, bio
);
1813 bio_list_merge(&tc
->deferred_bio_list
, &bios
);
1814 spin_unlock_irqrestore(&tc
->lock
, flags
);
1818 if (bio
->bi_rw
& REQ_DISCARD
)
1819 pool
->process_discard(tc
, bio
);
1821 pool
->process_bio(tc
, bio
);
1823 if ((count
++ & 127) == 0) {
1824 throttle_work_update(&pool
->throttle
);
1825 dm_pool_issue_prefetches(pool
->pmd
);
1828 blk_finish_plug(&plug
);
1831 static int cmp_cells(const void *lhs
, const void *rhs
)
1833 struct dm_bio_prison_cell
*lhs_cell
= *((struct dm_bio_prison_cell
**) lhs
);
1834 struct dm_bio_prison_cell
*rhs_cell
= *((struct dm_bio_prison_cell
**) rhs
);
1836 BUG_ON(!lhs_cell
->holder
);
1837 BUG_ON(!rhs_cell
->holder
);
1839 if (lhs_cell
->holder
->bi_iter
.bi_sector
< rhs_cell
->holder
->bi_iter
.bi_sector
)
1842 if (lhs_cell
->holder
->bi_iter
.bi_sector
> rhs_cell
->holder
->bi_iter
.bi_sector
)
1848 static unsigned sort_cells(struct pool
*pool
, struct list_head
*cells
)
1851 struct dm_bio_prison_cell
*cell
, *tmp
;
1853 list_for_each_entry_safe(cell
, tmp
, cells
, user_list
) {
1854 if (count
>= CELL_SORT_ARRAY_SIZE
)
1857 pool
->cell_sort_array
[count
++] = cell
;
1858 list_del(&cell
->user_list
);
1861 sort(pool
->cell_sort_array
, count
, sizeof(cell
), cmp_cells
, NULL
);
1866 static void process_thin_deferred_cells(struct thin_c
*tc
)
1868 struct pool
*pool
= tc
->pool
;
1869 unsigned long flags
;
1870 struct list_head cells
;
1871 struct dm_bio_prison_cell
*cell
;
1872 unsigned i
, j
, count
;
1874 INIT_LIST_HEAD(&cells
);
1876 spin_lock_irqsave(&tc
->lock
, flags
);
1877 list_splice_init(&tc
->deferred_cells
, &cells
);
1878 spin_unlock_irqrestore(&tc
->lock
, flags
);
1880 if (list_empty(&cells
))
1884 count
= sort_cells(tc
->pool
, &cells
);
1886 for (i
= 0; i
< count
; i
++) {
1887 cell
= pool
->cell_sort_array
[i
];
1888 BUG_ON(!cell
->holder
);
1891 * If we've got no free new_mapping structs, and processing
1892 * this bio might require one, we pause until there are some
1893 * prepared mappings to process.
1895 if (ensure_next_mapping(pool
)) {
1896 for (j
= i
; j
< count
; j
++)
1897 list_add(&pool
->cell_sort_array
[j
]->user_list
, &cells
);
1899 spin_lock_irqsave(&tc
->lock
, flags
);
1900 list_splice(&cells
, &tc
->deferred_cells
);
1901 spin_unlock_irqrestore(&tc
->lock
, flags
);
1905 if (cell
->holder
->bi_rw
& REQ_DISCARD
)
1906 pool
->process_discard_cell(tc
, cell
);
1908 pool
->process_cell(tc
, cell
);
1910 } while (!list_empty(&cells
));
1913 static void thin_get(struct thin_c
*tc
);
1914 static void thin_put(struct thin_c
*tc
);
1917 * We can't hold rcu_read_lock() around code that can block. So we
1918 * find a thin with the rcu lock held; bump a refcount; then drop
1921 static struct thin_c
*get_first_thin(struct pool
*pool
)
1923 struct thin_c
*tc
= NULL
;
1926 if (!list_empty(&pool
->active_thins
)) {
1927 tc
= list_entry_rcu(pool
->active_thins
.next
, struct thin_c
, list
);
1935 static struct thin_c
*get_next_thin(struct pool
*pool
, struct thin_c
*tc
)
1937 struct thin_c
*old_tc
= tc
;
1940 list_for_each_entry_continue_rcu(tc
, &pool
->active_thins
, list
) {
1952 static void process_deferred_bios(struct pool
*pool
)
1954 unsigned long flags
;
1956 struct bio_list bios
;
1959 tc
= get_first_thin(pool
);
1961 process_thin_deferred_cells(tc
);
1962 process_thin_deferred_bios(tc
);
1963 tc
= get_next_thin(pool
, tc
);
1967 * If there are any deferred flush bios, we must commit
1968 * the metadata before issuing them.
1970 bio_list_init(&bios
);
1971 spin_lock_irqsave(&pool
->lock
, flags
);
1972 bio_list_merge(&bios
, &pool
->deferred_flush_bios
);
1973 bio_list_init(&pool
->deferred_flush_bios
);
1974 spin_unlock_irqrestore(&pool
->lock
, flags
);
1976 if (bio_list_empty(&bios
) &&
1977 !(dm_pool_changed_this_transaction(pool
->pmd
) && need_commit_due_to_time(pool
)))
1981 while ((bio
= bio_list_pop(&bios
)))
1985 pool
->last_commit_jiffies
= jiffies
;
1987 while ((bio
= bio_list_pop(&bios
)))
1988 generic_make_request(bio
);
1991 static void do_worker(struct work_struct
*ws
)
1993 struct pool
*pool
= container_of(ws
, struct pool
, worker
);
1995 throttle_work_start(&pool
->throttle
);
1996 dm_pool_issue_prefetches(pool
->pmd
);
1997 throttle_work_update(&pool
->throttle
);
1998 process_prepared(pool
, &pool
->prepared_mappings
, &pool
->process_prepared_mapping
);
1999 throttle_work_update(&pool
->throttle
);
2000 process_prepared(pool
, &pool
->prepared_discards
, &pool
->process_prepared_discard
);
2001 throttle_work_update(&pool
->throttle
);
2002 process_deferred_bios(pool
);
2003 throttle_work_complete(&pool
->throttle
);
2007 * We want to commit periodically so that not too much
2008 * unwritten data builds up.
2010 static void do_waker(struct work_struct
*ws
)
2012 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
, waker
);
2014 queue_delayed_work(pool
->wq
, &pool
->waker
, COMMIT_PERIOD
);
2018 * We're holding onto IO to allow userland time to react. After the
2019 * timeout either the pool will have been resized (and thus back in
2020 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
2022 static void do_no_space_timeout(struct work_struct
*ws
)
2024 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
,
2027 if (get_pool_mode(pool
) == PM_OUT_OF_DATA_SPACE
&& !pool
->pf
.error_if_no_space
)
2028 set_pool_mode(pool
, PM_READ_ONLY
);
2031 /*----------------------------------------------------------------*/
2034 struct work_struct worker
;
2035 struct completion complete
;
2038 static struct pool_work
*to_pool_work(struct work_struct
*ws
)
2040 return container_of(ws
, struct pool_work
, worker
);
2043 static void pool_work_complete(struct pool_work
*pw
)
2045 complete(&pw
->complete
);
2048 static void pool_work_wait(struct pool_work
*pw
, struct pool
*pool
,
2049 void (*fn
)(struct work_struct
*))
2051 INIT_WORK_ONSTACK(&pw
->worker
, fn
);
2052 init_completion(&pw
->complete
);
2053 queue_work(pool
->wq
, &pw
->worker
);
2054 wait_for_completion(&pw
->complete
);
2057 /*----------------------------------------------------------------*/
2059 struct noflush_work
{
2060 struct pool_work pw
;
2064 static struct noflush_work
*to_noflush(struct work_struct
*ws
)
2066 return container_of(to_pool_work(ws
), struct noflush_work
, pw
);
2069 static void do_noflush_start(struct work_struct
*ws
)
2071 struct noflush_work
*w
= to_noflush(ws
);
2072 w
->tc
->requeue_mode
= true;
2074 pool_work_complete(&w
->pw
);
2077 static void do_noflush_stop(struct work_struct
*ws
)
2079 struct noflush_work
*w
= to_noflush(ws
);
2080 w
->tc
->requeue_mode
= false;
2081 pool_work_complete(&w
->pw
);
2084 static void noflush_work(struct thin_c
*tc
, void (*fn
)(struct work_struct
*))
2086 struct noflush_work w
;
2089 pool_work_wait(&w
.pw
, tc
->pool
, fn
);
2092 /*----------------------------------------------------------------*/
2094 static enum pool_mode
get_pool_mode(struct pool
*pool
)
2096 return pool
->pf
.mode
;
2099 static void notify_of_pool_mode_change(struct pool
*pool
, const char *new_mode
)
2101 dm_table_event(pool
->ti
->table
);
2102 DMINFO("%s: switching pool to %s mode",
2103 dm_device_name(pool
->pool_md
), new_mode
);
2106 static void set_pool_mode(struct pool
*pool
, enum pool_mode new_mode
)
2108 struct pool_c
*pt
= pool
->ti
->private;
2109 bool needs_check
= dm_pool_metadata_needs_check(pool
->pmd
);
2110 enum pool_mode old_mode
= get_pool_mode(pool
);
2111 unsigned long no_space_timeout
= ACCESS_ONCE(no_space_timeout_secs
) * HZ
;
2114 * Never allow the pool to transition to PM_WRITE mode if user
2115 * intervention is required to verify metadata and data consistency.
2117 if (new_mode
== PM_WRITE
&& needs_check
) {
2118 DMERR("%s: unable to switch pool to write mode until repaired.",
2119 dm_device_name(pool
->pool_md
));
2120 if (old_mode
!= new_mode
)
2121 new_mode
= old_mode
;
2123 new_mode
= PM_READ_ONLY
;
2126 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2127 * not going to recover without a thin_repair. So we never let the
2128 * pool move out of the old mode.
2130 if (old_mode
== PM_FAIL
)
2131 new_mode
= old_mode
;
2135 if (old_mode
!= new_mode
)
2136 notify_of_pool_mode_change(pool
, "failure");
2137 dm_pool_metadata_read_only(pool
->pmd
);
2138 pool
->process_bio
= process_bio_fail
;
2139 pool
->process_discard
= process_bio_fail
;
2140 pool
->process_cell
= process_cell_fail
;
2141 pool
->process_discard_cell
= process_cell_fail
;
2142 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
2143 pool
->process_prepared_discard
= process_prepared_discard_fail
;
2145 error_retry_list(pool
);
2149 if (old_mode
!= new_mode
)
2150 notify_of_pool_mode_change(pool
, "read-only");
2151 dm_pool_metadata_read_only(pool
->pmd
);
2152 pool
->process_bio
= process_bio_read_only
;
2153 pool
->process_discard
= process_bio_success
;
2154 pool
->process_cell
= process_cell_read_only
;
2155 pool
->process_discard_cell
= process_cell_success
;
2156 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
2157 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
2159 error_retry_list(pool
);
2162 case PM_OUT_OF_DATA_SPACE
:
2164 * Ideally we'd never hit this state; the low water mark
2165 * would trigger userland to extend the pool before we
2166 * completely run out of data space. However, many small
2167 * IOs to unprovisioned space can consume data space at an
2168 * alarming rate. Adjust your low water mark if you're
2169 * frequently seeing this mode.
2171 if (old_mode
!= new_mode
)
2172 notify_of_pool_mode_change(pool
, "out-of-data-space");
2173 pool
->process_bio
= process_bio_read_only
;
2174 pool
->process_discard
= process_discard_bio
;
2175 pool
->process_cell
= process_cell_read_only
;
2176 pool
->process_discard_cell
= process_discard_cell
;
2177 pool
->process_prepared_mapping
= process_prepared_mapping
;
2178 pool
->process_prepared_discard
= process_prepared_discard
;
2180 if (!pool
->pf
.error_if_no_space
&& no_space_timeout
)
2181 queue_delayed_work(pool
->wq
, &pool
->no_space_timeout
, no_space_timeout
);
2185 if (old_mode
!= new_mode
)
2186 notify_of_pool_mode_change(pool
, "write");
2187 dm_pool_metadata_read_write(pool
->pmd
);
2188 pool
->process_bio
= process_bio
;
2189 pool
->process_discard
= process_discard_bio
;
2190 pool
->process_cell
= process_cell
;
2191 pool
->process_discard_cell
= process_discard_cell
;
2192 pool
->process_prepared_mapping
= process_prepared_mapping
;
2193 pool
->process_prepared_discard
= process_prepared_discard
;
2197 pool
->pf
.mode
= new_mode
;
2199 * The pool mode may have changed, sync it so bind_control_target()
2200 * doesn't cause an unexpected mode transition on resume.
2202 pt
->adjusted_pf
.mode
= new_mode
;
2205 static void abort_transaction(struct pool
*pool
)
2207 const char *dev_name
= dm_device_name(pool
->pool_md
);
2209 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name
);
2210 if (dm_pool_abort_metadata(pool
->pmd
)) {
2211 DMERR("%s: failed to abort metadata transaction", dev_name
);
2212 set_pool_mode(pool
, PM_FAIL
);
2215 if (dm_pool_metadata_set_needs_check(pool
->pmd
)) {
2216 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name
);
2217 set_pool_mode(pool
, PM_FAIL
);
2221 static void metadata_operation_failed(struct pool
*pool
, const char *op
, int r
)
2223 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2224 dm_device_name(pool
->pool_md
), op
, r
);
2226 abort_transaction(pool
);
2227 set_pool_mode(pool
, PM_READ_ONLY
);
2230 /*----------------------------------------------------------------*/
2233 * Mapping functions.
2237 * Called only while mapping a thin bio to hand it over to the workqueue.
2239 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
)
2241 unsigned long flags
;
2242 struct pool
*pool
= tc
->pool
;
2244 spin_lock_irqsave(&tc
->lock
, flags
);
2245 bio_list_add(&tc
->deferred_bio_list
, bio
);
2246 spin_unlock_irqrestore(&tc
->lock
, flags
);
2251 static void thin_defer_bio_with_throttle(struct thin_c
*tc
, struct bio
*bio
)
2253 struct pool
*pool
= tc
->pool
;
2255 throttle_lock(&pool
->throttle
);
2256 thin_defer_bio(tc
, bio
);
2257 throttle_unlock(&pool
->throttle
);
2260 static void thin_defer_cell(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
2262 unsigned long flags
;
2263 struct pool
*pool
= tc
->pool
;
2265 throttle_lock(&pool
->throttle
);
2266 spin_lock_irqsave(&tc
->lock
, flags
);
2267 list_add_tail(&cell
->user_list
, &tc
->deferred_cells
);
2268 spin_unlock_irqrestore(&tc
->lock
, flags
);
2269 throttle_unlock(&pool
->throttle
);
2274 static void thin_hook_bio(struct thin_c
*tc
, struct bio
*bio
)
2276 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
2279 h
->shared_read_entry
= NULL
;
2280 h
->all_io_entry
= NULL
;
2281 h
->overwrite_mapping
= NULL
;
2285 * Non-blocking function called from the thin target's map function.
2287 static int thin_bio_map(struct dm_target
*ti
, struct bio
*bio
)
2290 struct thin_c
*tc
= ti
->private;
2291 dm_block_t block
= get_bio_block(tc
, bio
);
2292 struct dm_thin_device
*td
= tc
->td
;
2293 struct dm_thin_lookup_result result
;
2294 struct dm_bio_prison_cell
*virt_cell
, *data_cell
;
2295 struct dm_cell_key key
;
2297 thin_hook_bio(tc
, bio
);
2299 if (tc
->requeue_mode
) {
2300 bio_endio(bio
, DM_ENDIO_REQUEUE
);
2301 return DM_MAPIO_SUBMITTED
;
2304 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
2306 return DM_MAPIO_SUBMITTED
;
2309 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)) {
2310 thin_defer_bio_with_throttle(tc
, bio
);
2311 return DM_MAPIO_SUBMITTED
;
2315 * We must hold the virtual cell before doing the lookup, otherwise
2316 * there's a race with discard.
2318 build_virtual_key(tc
->td
, block
, &key
);
2319 if (bio_detain(tc
->pool
, &key
, bio
, &virt_cell
))
2320 return DM_MAPIO_SUBMITTED
;
2322 r
= dm_thin_find_block(td
, block
, 0, &result
);
2325 * Note that we defer readahead too.
2329 if (unlikely(result
.shared
)) {
2331 * We have a race condition here between the
2332 * result.shared value returned by the lookup and
2333 * snapshot creation, which may cause new
2336 * To avoid this always quiesce the origin before
2337 * taking the snap. You want to do this anyway to
2338 * ensure a consistent application view
2341 * More distant ancestors are irrelevant. The
2342 * shared flag will be set in their case.
2344 thin_defer_cell(tc
, virt_cell
);
2345 return DM_MAPIO_SUBMITTED
;
2348 build_data_key(tc
->td
, result
.block
, &key
);
2349 if (bio_detain(tc
->pool
, &key
, bio
, &data_cell
)) {
2350 cell_defer_no_holder(tc
, virt_cell
);
2351 return DM_MAPIO_SUBMITTED
;
2354 inc_all_io_entry(tc
->pool
, bio
);
2355 cell_defer_no_holder(tc
, data_cell
);
2356 cell_defer_no_holder(tc
, virt_cell
);
2358 remap(tc
, bio
, result
.block
);
2359 return DM_MAPIO_REMAPPED
;
2363 thin_defer_cell(tc
, virt_cell
);
2364 return DM_MAPIO_SUBMITTED
;
2368 * Must always call bio_io_error on failure.
2369 * dm_thin_find_block can fail with -EINVAL if the
2370 * pool is switched to fail-io mode.
2373 cell_defer_no_holder(tc
, virt_cell
);
2374 return DM_MAPIO_SUBMITTED
;
2378 static int pool_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
2380 struct pool_c
*pt
= container_of(cb
, struct pool_c
, callbacks
);
2381 struct request_queue
*q
;
2383 if (get_pool_mode(pt
->pool
) == PM_OUT_OF_DATA_SPACE
)
2386 q
= bdev_get_queue(pt
->data_dev
->bdev
);
2387 return bdi_congested(&q
->backing_dev_info
, bdi_bits
);
2390 static void requeue_bios(struct pool
*pool
)
2392 unsigned long flags
;
2396 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
) {
2397 spin_lock_irqsave(&tc
->lock
, flags
);
2398 bio_list_merge(&tc
->deferred_bio_list
, &tc
->retry_on_resume_list
);
2399 bio_list_init(&tc
->retry_on_resume_list
);
2400 spin_unlock_irqrestore(&tc
->lock
, flags
);
2405 /*----------------------------------------------------------------
2406 * Binding of control targets to a pool object
2407 *--------------------------------------------------------------*/
2408 static bool data_dev_supports_discard(struct pool_c
*pt
)
2410 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
2412 return q
&& blk_queue_discard(q
);
2415 static bool is_factor(sector_t block_size
, uint32_t n
)
2417 return !sector_div(block_size
, n
);
2421 * If discard_passdown was enabled verify that the data device
2422 * supports discards. Disable discard_passdown if not.
2424 static void disable_passdown_if_not_supported(struct pool_c
*pt
)
2426 struct pool
*pool
= pt
->pool
;
2427 struct block_device
*data_bdev
= pt
->data_dev
->bdev
;
2428 struct queue_limits
*data_limits
= &bdev_get_queue(data_bdev
)->limits
;
2429 sector_t block_size
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
2430 const char *reason
= NULL
;
2431 char buf
[BDEVNAME_SIZE
];
2433 if (!pt
->adjusted_pf
.discard_passdown
)
2436 if (!data_dev_supports_discard(pt
))
2437 reason
= "discard unsupported";
2439 else if (data_limits
->max_discard_sectors
< pool
->sectors_per_block
)
2440 reason
= "max discard sectors smaller than a block";
2442 else if (data_limits
->discard_granularity
> block_size
)
2443 reason
= "discard granularity larger than a block";
2445 else if (!is_factor(block_size
, data_limits
->discard_granularity
))
2446 reason
= "discard granularity not a factor of block size";
2449 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev
, buf
), reason
);
2450 pt
->adjusted_pf
.discard_passdown
= false;
2454 static int bind_control_target(struct pool
*pool
, struct dm_target
*ti
)
2456 struct pool_c
*pt
= ti
->private;
2459 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
2461 enum pool_mode old_mode
= get_pool_mode(pool
);
2462 enum pool_mode new_mode
= pt
->adjusted_pf
.mode
;
2465 * Don't change the pool's mode until set_pool_mode() below.
2466 * Otherwise the pool's process_* function pointers may
2467 * not match the desired pool mode.
2469 pt
->adjusted_pf
.mode
= old_mode
;
2472 pool
->pf
= pt
->adjusted_pf
;
2473 pool
->low_water_blocks
= pt
->low_water_blocks
;
2475 set_pool_mode(pool
, new_mode
);
2480 static void unbind_control_target(struct pool
*pool
, struct dm_target
*ti
)
2486 /*----------------------------------------------------------------
2488 *--------------------------------------------------------------*/
2489 /* Initialize pool features. */
2490 static void pool_features_init(struct pool_features
*pf
)
2492 pf
->mode
= PM_WRITE
;
2493 pf
->zero_new_blocks
= true;
2494 pf
->discard_enabled
= true;
2495 pf
->discard_passdown
= true;
2496 pf
->error_if_no_space
= false;
2499 static void __pool_destroy(struct pool
*pool
)
2501 __pool_table_remove(pool
);
2503 vfree(pool
->cell_sort_array
);
2504 if (dm_pool_metadata_close(pool
->pmd
) < 0)
2505 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
2507 dm_bio_prison_destroy(pool
->prison
);
2508 dm_kcopyd_client_destroy(pool
->copier
);
2511 destroy_workqueue(pool
->wq
);
2513 if (pool
->next_mapping
)
2514 mempool_free(pool
->next_mapping
, pool
->mapping_pool
);
2515 mempool_destroy(pool
->mapping_pool
);
2516 dm_deferred_set_destroy(pool
->shared_read_ds
);
2517 dm_deferred_set_destroy(pool
->all_io_ds
);
2521 static struct kmem_cache
*_new_mapping_cache
;
2523 static struct pool
*pool_create(struct mapped_device
*pool_md
,
2524 struct block_device
*metadata_dev
,
2525 unsigned long block_size
,
2526 int read_only
, char **error
)
2531 struct dm_pool_metadata
*pmd
;
2532 bool format_device
= read_only
? false : true;
2534 pmd
= dm_pool_metadata_open(metadata_dev
, block_size
, format_device
);
2536 *error
= "Error creating metadata object";
2537 return (struct pool
*)pmd
;
2540 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
2542 *error
= "Error allocating memory for pool";
2543 err_p
= ERR_PTR(-ENOMEM
);
2548 pool
->sectors_per_block
= block_size
;
2549 if (block_size
& (block_size
- 1))
2550 pool
->sectors_per_block_shift
= -1;
2552 pool
->sectors_per_block_shift
= __ffs(block_size
);
2553 pool
->low_water_blocks
= 0;
2554 pool_features_init(&pool
->pf
);
2555 pool
->prison
= dm_bio_prison_create();
2556 if (!pool
->prison
) {
2557 *error
= "Error creating pool's bio prison";
2558 err_p
= ERR_PTR(-ENOMEM
);
2562 pool
->copier
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2563 if (IS_ERR(pool
->copier
)) {
2564 r
= PTR_ERR(pool
->copier
);
2565 *error
= "Error creating pool's kcopyd client";
2567 goto bad_kcopyd_client
;
2571 * Create singlethreaded workqueue that will service all devices
2572 * that use this metadata.
2574 pool
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
2576 *error
= "Error creating pool's workqueue";
2577 err_p
= ERR_PTR(-ENOMEM
);
2581 throttle_init(&pool
->throttle
);
2582 INIT_WORK(&pool
->worker
, do_worker
);
2583 INIT_DELAYED_WORK(&pool
->waker
, do_waker
);
2584 INIT_DELAYED_WORK(&pool
->no_space_timeout
, do_no_space_timeout
);
2585 spin_lock_init(&pool
->lock
);
2586 bio_list_init(&pool
->deferred_flush_bios
);
2587 INIT_LIST_HEAD(&pool
->prepared_mappings
);
2588 INIT_LIST_HEAD(&pool
->prepared_discards
);
2589 INIT_LIST_HEAD(&pool
->active_thins
);
2590 pool
->low_water_triggered
= false;
2591 pool
->suspended
= true;
2593 pool
->shared_read_ds
= dm_deferred_set_create();
2594 if (!pool
->shared_read_ds
) {
2595 *error
= "Error creating pool's shared read deferred set";
2596 err_p
= ERR_PTR(-ENOMEM
);
2597 goto bad_shared_read_ds
;
2600 pool
->all_io_ds
= dm_deferred_set_create();
2601 if (!pool
->all_io_ds
) {
2602 *error
= "Error creating pool's all io deferred set";
2603 err_p
= ERR_PTR(-ENOMEM
);
2607 pool
->next_mapping
= NULL
;
2608 pool
->mapping_pool
= mempool_create_slab_pool(MAPPING_POOL_SIZE
,
2609 _new_mapping_cache
);
2610 if (!pool
->mapping_pool
) {
2611 *error
= "Error creating pool's mapping mempool";
2612 err_p
= ERR_PTR(-ENOMEM
);
2613 goto bad_mapping_pool
;
2616 pool
->cell_sort_array
= vmalloc(sizeof(*pool
->cell_sort_array
) * CELL_SORT_ARRAY_SIZE
);
2617 if (!pool
->cell_sort_array
) {
2618 *error
= "Error allocating cell sort array";
2619 err_p
= ERR_PTR(-ENOMEM
);
2620 goto bad_sort_array
;
2623 pool
->ref_count
= 1;
2624 pool
->last_commit_jiffies
= jiffies
;
2625 pool
->pool_md
= pool_md
;
2626 pool
->md_dev
= metadata_dev
;
2627 __pool_table_insert(pool
);
2632 mempool_destroy(pool
->mapping_pool
);
2634 dm_deferred_set_destroy(pool
->all_io_ds
);
2636 dm_deferred_set_destroy(pool
->shared_read_ds
);
2638 destroy_workqueue(pool
->wq
);
2640 dm_kcopyd_client_destroy(pool
->copier
);
2642 dm_bio_prison_destroy(pool
->prison
);
2646 if (dm_pool_metadata_close(pmd
))
2647 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
2652 static void __pool_inc(struct pool
*pool
)
2654 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2658 static void __pool_dec(struct pool
*pool
)
2660 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2661 BUG_ON(!pool
->ref_count
);
2662 if (!--pool
->ref_count
)
2663 __pool_destroy(pool
);
2666 static struct pool
*__pool_find(struct mapped_device
*pool_md
,
2667 struct block_device
*metadata_dev
,
2668 unsigned long block_size
, int read_only
,
2669 char **error
, int *created
)
2671 struct pool
*pool
= __pool_table_lookup_metadata_dev(metadata_dev
);
2674 if (pool
->pool_md
!= pool_md
) {
2675 *error
= "metadata device already in use by a pool";
2676 return ERR_PTR(-EBUSY
);
2681 pool
= __pool_table_lookup(pool_md
);
2683 if (pool
->md_dev
!= metadata_dev
) {
2684 *error
= "different pool cannot replace a pool";
2685 return ERR_PTR(-EINVAL
);
2690 pool
= pool_create(pool_md
, metadata_dev
, block_size
, read_only
, error
);
2698 /*----------------------------------------------------------------
2699 * Pool target methods
2700 *--------------------------------------------------------------*/
2701 static void pool_dtr(struct dm_target
*ti
)
2703 struct pool_c
*pt
= ti
->private;
2705 mutex_lock(&dm_thin_pool_table
.mutex
);
2707 unbind_control_target(pt
->pool
, ti
);
2708 __pool_dec(pt
->pool
);
2709 dm_put_device(ti
, pt
->metadata_dev
);
2710 dm_put_device(ti
, pt
->data_dev
);
2713 mutex_unlock(&dm_thin_pool_table
.mutex
);
2716 static int parse_pool_features(struct dm_arg_set
*as
, struct pool_features
*pf
,
2717 struct dm_target
*ti
)
2721 const char *arg_name
;
2723 static struct dm_arg _args
[] = {
2724 {0, 4, "Invalid number of pool feature arguments"},
2728 * No feature arguments supplied.
2733 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
2737 while (argc
&& !r
) {
2738 arg_name
= dm_shift_arg(as
);
2741 if (!strcasecmp(arg_name
, "skip_block_zeroing"))
2742 pf
->zero_new_blocks
= false;
2744 else if (!strcasecmp(arg_name
, "ignore_discard"))
2745 pf
->discard_enabled
= false;
2747 else if (!strcasecmp(arg_name
, "no_discard_passdown"))
2748 pf
->discard_passdown
= false;
2750 else if (!strcasecmp(arg_name
, "read_only"))
2751 pf
->mode
= PM_READ_ONLY
;
2753 else if (!strcasecmp(arg_name
, "error_if_no_space"))
2754 pf
->error_if_no_space
= true;
2757 ti
->error
= "Unrecognised pool feature requested";
2766 static void metadata_low_callback(void *context
)
2768 struct pool
*pool
= context
;
2770 DMWARN("%s: reached low water mark for metadata device: sending event.",
2771 dm_device_name(pool
->pool_md
));
2773 dm_table_event(pool
->ti
->table
);
2776 static sector_t
get_dev_size(struct block_device
*bdev
)
2778 return i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
;
2781 static void warn_if_metadata_device_too_big(struct block_device
*bdev
)
2783 sector_t metadata_dev_size
= get_dev_size(bdev
);
2784 char buffer
[BDEVNAME_SIZE
];
2786 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS_WARNING
)
2787 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2788 bdevname(bdev
, buffer
), THIN_METADATA_MAX_SECTORS
);
2791 static sector_t
get_metadata_dev_size(struct block_device
*bdev
)
2793 sector_t metadata_dev_size
= get_dev_size(bdev
);
2795 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS
)
2796 metadata_dev_size
= THIN_METADATA_MAX_SECTORS
;
2798 return metadata_dev_size
;
2801 static dm_block_t
get_metadata_dev_size_in_blocks(struct block_device
*bdev
)
2803 sector_t metadata_dev_size
= get_metadata_dev_size(bdev
);
2805 sector_div(metadata_dev_size
, THIN_METADATA_BLOCK_SIZE
);
2807 return metadata_dev_size
;
2811 * When a metadata threshold is crossed a dm event is triggered, and
2812 * userland should respond by growing the metadata device. We could let
2813 * userland set the threshold, like we do with the data threshold, but I'm
2814 * not sure they know enough to do this well.
2816 static dm_block_t
calc_metadata_threshold(struct pool_c
*pt
)
2819 * 4M is ample for all ops with the possible exception of thin
2820 * device deletion which is harmless if it fails (just retry the
2821 * delete after you've grown the device).
2823 dm_block_t quarter
= get_metadata_dev_size_in_blocks(pt
->metadata_dev
->bdev
) / 4;
2824 return min((dm_block_t
)1024ULL /* 4M */, quarter
);
2828 * thin-pool <metadata dev> <data dev>
2829 * <data block size (sectors)>
2830 * <low water mark (blocks)>
2831 * [<#feature args> [<arg>]*]
2833 * Optional feature arguments are:
2834 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
2835 * ignore_discard: disable discard
2836 * no_discard_passdown: don't pass discards down to the data device
2837 * read_only: Don't allow any changes to be made to the pool metadata.
2838 * error_if_no_space: error IOs, instead of queueing, if no space.
2840 static int pool_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2842 int r
, pool_created
= 0;
2845 struct pool_features pf
;
2846 struct dm_arg_set as
;
2847 struct dm_dev
*data_dev
;
2848 unsigned long block_size
;
2849 dm_block_t low_water_blocks
;
2850 struct dm_dev
*metadata_dev
;
2851 fmode_t metadata_mode
;
2854 * FIXME Remove validation from scope of lock.
2856 mutex_lock(&dm_thin_pool_table
.mutex
);
2859 ti
->error
= "Invalid argument count";
2868 * Set default pool features.
2870 pool_features_init(&pf
);
2872 dm_consume_args(&as
, 4);
2873 r
= parse_pool_features(&as
, &pf
, ti
);
2877 metadata_mode
= FMODE_READ
| ((pf
.mode
== PM_READ_ONLY
) ? 0 : FMODE_WRITE
);
2878 r
= dm_get_device(ti
, argv
[0], metadata_mode
, &metadata_dev
);
2880 ti
->error
= "Error opening metadata block device";
2883 warn_if_metadata_device_too_big(metadata_dev
->bdev
);
2885 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &data_dev
);
2887 ti
->error
= "Error getting data device";
2891 if (kstrtoul(argv
[2], 10, &block_size
) || !block_size
||
2892 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
2893 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
2894 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
2895 ti
->error
= "Invalid block size";
2900 if (kstrtoull(argv
[3], 10, (unsigned long long *)&low_water_blocks
)) {
2901 ti
->error
= "Invalid low water mark";
2906 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
2912 pool
= __pool_find(dm_table_get_md(ti
->table
), metadata_dev
->bdev
,
2913 block_size
, pf
.mode
== PM_READ_ONLY
, &ti
->error
, &pool_created
);
2920 * 'pool_created' reflects whether this is the first table load.
2921 * Top level discard support is not allowed to be changed after
2922 * initial load. This would require a pool reload to trigger thin
2925 if (!pool_created
&& pf
.discard_enabled
!= pool
->pf
.discard_enabled
) {
2926 ti
->error
= "Discard support cannot be disabled once enabled";
2928 goto out_flags_changed
;
2933 pt
->metadata_dev
= metadata_dev
;
2934 pt
->data_dev
= data_dev
;
2935 pt
->low_water_blocks
= low_water_blocks
;
2936 pt
->adjusted_pf
= pt
->requested_pf
= pf
;
2937 ti
->num_flush_bios
= 1;
2940 * Only need to enable discards if the pool should pass
2941 * them down to the data device. The thin device's discard
2942 * processing will cause mappings to be removed from the btree.
2944 ti
->discard_zeroes_data_unsupported
= true;
2945 if (pf
.discard_enabled
&& pf
.discard_passdown
) {
2946 ti
->num_discard_bios
= 1;
2949 * Setting 'discards_supported' circumvents the normal
2950 * stacking of discard limits (this keeps the pool and
2951 * thin devices' discard limits consistent).
2953 ti
->discards_supported
= true;
2957 r
= dm_pool_register_metadata_threshold(pt
->pool
->pmd
,
2958 calc_metadata_threshold(pt
),
2959 metadata_low_callback
,
2962 goto out_flags_changed
;
2964 pt
->callbacks
.congested_fn
= pool_is_congested
;
2965 dm_table_add_target_callbacks(ti
->table
, &pt
->callbacks
);
2967 mutex_unlock(&dm_thin_pool_table
.mutex
);
2976 dm_put_device(ti
, data_dev
);
2978 dm_put_device(ti
, metadata_dev
);
2980 mutex_unlock(&dm_thin_pool_table
.mutex
);
2985 static int pool_map(struct dm_target
*ti
, struct bio
*bio
)
2988 struct pool_c
*pt
= ti
->private;
2989 struct pool
*pool
= pt
->pool
;
2990 unsigned long flags
;
2993 * As this is a singleton target, ti->begin is always zero.
2995 spin_lock_irqsave(&pool
->lock
, flags
);
2996 bio
->bi_bdev
= pt
->data_dev
->bdev
;
2997 r
= DM_MAPIO_REMAPPED
;
2998 spin_unlock_irqrestore(&pool
->lock
, flags
);
3003 static int maybe_resize_data_dev(struct dm_target
*ti
, bool *need_commit
)
3006 struct pool_c
*pt
= ti
->private;
3007 struct pool
*pool
= pt
->pool
;
3008 sector_t data_size
= ti
->len
;
3009 dm_block_t sb_data_size
;
3011 *need_commit
= false;
3013 (void) sector_div(data_size
, pool
->sectors_per_block
);
3015 r
= dm_pool_get_data_dev_size(pool
->pmd
, &sb_data_size
);
3017 DMERR("%s: failed to retrieve data device size",
3018 dm_device_name(pool
->pool_md
));
3022 if (data_size
< sb_data_size
) {
3023 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3024 dm_device_name(pool
->pool_md
),
3025 (unsigned long long)data_size
, sb_data_size
);
3028 } else if (data_size
> sb_data_size
) {
3029 if (dm_pool_metadata_needs_check(pool
->pmd
)) {
3030 DMERR("%s: unable to grow the data device until repaired.",
3031 dm_device_name(pool
->pool_md
));
3036 DMINFO("%s: growing the data device from %llu to %llu blocks",
3037 dm_device_name(pool
->pool_md
),
3038 sb_data_size
, (unsigned long long)data_size
);
3039 r
= dm_pool_resize_data_dev(pool
->pmd
, data_size
);
3041 metadata_operation_failed(pool
, "dm_pool_resize_data_dev", r
);
3045 *need_commit
= true;
3051 static int maybe_resize_metadata_dev(struct dm_target
*ti
, bool *need_commit
)
3054 struct pool_c
*pt
= ti
->private;
3055 struct pool
*pool
= pt
->pool
;
3056 dm_block_t metadata_dev_size
, sb_metadata_dev_size
;
3058 *need_commit
= false;
3060 metadata_dev_size
= get_metadata_dev_size_in_blocks(pool
->md_dev
);
3062 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &sb_metadata_dev_size
);
3064 DMERR("%s: failed to retrieve metadata device size",
3065 dm_device_name(pool
->pool_md
));
3069 if (metadata_dev_size
< sb_metadata_dev_size
) {
3070 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3071 dm_device_name(pool
->pool_md
),
3072 metadata_dev_size
, sb_metadata_dev_size
);
3075 } else if (metadata_dev_size
> sb_metadata_dev_size
) {
3076 if (dm_pool_metadata_needs_check(pool
->pmd
)) {
3077 DMERR("%s: unable to grow the metadata device until repaired.",
3078 dm_device_name(pool
->pool_md
));
3082 warn_if_metadata_device_too_big(pool
->md_dev
);
3083 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3084 dm_device_name(pool
->pool_md
),
3085 sb_metadata_dev_size
, metadata_dev_size
);
3086 r
= dm_pool_resize_metadata_dev(pool
->pmd
, metadata_dev_size
);
3088 metadata_operation_failed(pool
, "dm_pool_resize_metadata_dev", r
);
3092 *need_commit
= true;
3099 * Retrieves the number of blocks of the data device from
3100 * the superblock and compares it to the actual device size,
3101 * thus resizing the data device in case it has grown.
3103 * This both copes with opening preallocated data devices in the ctr
3104 * being followed by a resume
3106 * calling the resume method individually after userspace has
3107 * grown the data device in reaction to a table event.
3109 static int pool_preresume(struct dm_target
*ti
)
3112 bool need_commit1
, need_commit2
;
3113 struct pool_c
*pt
= ti
->private;
3114 struct pool
*pool
= pt
->pool
;
3117 * Take control of the pool object.
3119 r
= bind_control_target(pool
, ti
);
3123 r
= maybe_resize_data_dev(ti
, &need_commit1
);
3127 r
= maybe_resize_metadata_dev(ti
, &need_commit2
);
3131 if (need_commit1
|| need_commit2
)
3132 (void) commit(pool
);
3137 static void pool_suspend_active_thins(struct pool
*pool
)
3141 /* Suspend all active thin devices */
3142 tc
= get_first_thin(pool
);
3144 dm_internal_suspend_noflush(tc
->thin_md
);
3145 tc
= get_next_thin(pool
, tc
);
3149 static void pool_resume_active_thins(struct pool
*pool
)
3153 /* Resume all active thin devices */
3154 tc
= get_first_thin(pool
);
3156 dm_internal_resume(tc
->thin_md
);
3157 tc
= get_next_thin(pool
, tc
);
3161 static void pool_resume(struct dm_target
*ti
)
3163 struct pool_c
*pt
= ti
->private;
3164 struct pool
*pool
= pt
->pool
;
3165 unsigned long flags
;
3168 * Must requeue active_thins' bios and then resume
3169 * active_thins _before_ clearing 'suspend' flag.
3172 pool_resume_active_thins(pool
);
3174 spin_lock_irqsave(&pool
->lock
, flags
);
3175 pool
->low_water_triggered
= false;
3176 pool
->suspended
= false;
3177 spin_unlock_irqrestore(&pool
->lock
, flags
);
3179 do_waker(&pool
->waker
.work
);
3182 static void pool_presuspend(struct dm_target
*ti
)
3184 struct pool_c
*pt
= ti
->private;
3185 struct pool
*pool
= pt
->pool
;
3186 unsigned long flags
;
3188 spin_lock_irqsave(&pool
->lock
, flags
);
3189 pool
->suspended
= true;
3190 spin_unlock_irqrestore(&pool
->lock
, flags
);
3192 pool_suspend_active_thins(pool
);
3195 static void pool_presuspend_undo(struct dm_target
*ti
)
3197 struct pool_c
*pt
= ti
->private;
3198 struct pool
*pool
= pt
->pool
;
3199 unsigned long flags
;
3201 pool_resume_active_thins(pool
);
3203 spin_lock_irqsave(&pool
->lock
, flags
);
3204 pool
->suspended
= false;
3205 spin_unlock_irqrestore(&pool
->lock
, flags
);
3208 static void pool_postsuspend(struct dm_target
*ti
)
3210 struct pool_c
*pt
= ti
->private;
3211 struct pool
*pool
= pt
->pool
;
3213 cancel_delayed_work_sync(&pool
->waker
);
3214 cancel_delayed_work_sync(&pool
->no_space_timeout
);
3215 flush_workqueue(pool
->wq
);
3216 (void) commit(pool
);
3219 static int check_arg_count(unsigned argc
, unsigned args_required
)
3221 if (argc
!= args_required
) {
3222 DMWARN("Message received with %u arguments instead of %u.",
3223 argc
, args_required
);
3230 static int read_dev_id(char *arg
, dm_thin_id
*dev_id
, int warning
)
3232 if (!kstrtoull(arg
, 10, (unsigned long long *)dev_id
) &&
3233 *dev_id
<= MAX_DEV_ID
)
3237 DMWARN("Message received with invalid device id: %s", arg
);
3242 static int process_create_thin_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3247 r
= check_arg_count(argc
, 2);
3251 r
= read_dev_id(argv
[1], &dev_id
, 1);
3255 r
= dm_pool_create_thin(pool
->pmd
, dev_id
);
3257 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3265 static int process_create_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3268 dm_thin_id origin_dev_id
;
3271 r
= check_arg_count(argc
, 3);
3275 r
= read_dev_id(argv
[1], &dev_id
, 1);
3279 r
= read_dev_id(argv
[2], &origin_dev_id
, 1);
3283 r
= dm_pool_create_snap(pool
->pmd
, dev_id
, origin_dev_id
);
3285 DMWARN("Creation of new snapshot %s of device %s failed.",
3293 static int process_delete_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3298 r
= check_arg_count(argc
, 2);
3302 r
= read_dev_id(argv
[1], &dev_id
, 1);
3306 r
= dm_pool_delete_thin_device(pool
->pmd
, dev_id
);
3308 DMWARN("Deletion of thin device %s failed.", argv
[1]);
3313 static int process_set_transaction_id_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3315 dm_thin_id old_id
, new_id
;
3318 r
= check_arg_count(argc
, 3);
3322 if (kstrtoull(argv
[1], 10, (unsigned long long *)&old_id
)) {
3323 DMWARN("set_transaction_id message: Unrecognised id %s.", argv
[1]);
3327 if (kstrtoull(argv
[2], 10, (unsigned long long *)&new_id
)) {
3328 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv
[2]);
3332 r
= dm_pool_set_metadata_transaction_id(pool
->pmd
, old_id
, new_id
);
3334 DMWARN("Failed to change transaction id from %s to %s.",
3342 static int process_reserve_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3346 r
= check_arg_count(argc
, 1);
3350 (void) commit(pool
);
3352 r
= dm_pool_reserve_metadata_snap(pool
->pmd
);
3354 DMWARN("reserve_metadata_snap message failed.");
3359 static int process_release_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3363 r
= check_arg_count(argc
, 1);
3367 r
= dm_pool_release_metadata_snap(pool
->pmd
);
3369 DMWARN("release_metadata_snap message failed.");
3375 * Messages supported:
3376 * create_thin <dev_id>
3377 * create_snap <dev_id> <origin_id>
3379 * set_transaction_id <current_trans_id> <new_trans_id>
3380 * reserve_metadata_snap
3381 * release_metadata_snap
3383 static int pool_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
3386 struct pool_c
*pt
= ti
->private;
3387 struct pool
*pool
= pt
->pool
;
3389 if (get_pool_mode(pool
) >= PM_READ_ONLY
) {
3390 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3391 dm_device_name(pool
->pool_md
));
3395 if (!strcasecmp(argv
[0], "create_thin"))
3396 r
= process_create_thin_mesg(argc
, argv
, pool
);
3398 else if (!strcasecmp(argv
[0], "create_snap"))
3399 r
= process_create_snap_mesg(argc
, argv
, pool
);
3401 else if (!strcasecmp(argv
[0], "delete"))
3402 r
= process_delete_mesg(argc
, argv
, pool
);
3404 else if (!strcasecmp(argv
[0], "set_transaction_id"))
3405 r
= process_set_transaction_id_mesg(argc
, argv
, pool
);
3407 else if (!strcasecmp(argv
[0], "reserve_metadata_snap"))
3408 r
= process_reserve_metadata_snap_mesg(argc
, argv
, pool
);
3410 else if (!strcasecmp(argv
[0], "release_metadata_snap"))
3411 r
= process_release_metadata_snap_mesg(argc
, argv
, pool
);
3414 DMWARN("Unrecognised thin pool target message received: %s", argv
[0]);
3417 (void) commit(pool
);
3422 static void emit_flags(struct pool_features
*pf
, char *result
,
3423 unsigned sz
, unsigned maxlen
)
3425 unsigned count
= !pf
->zero_new_blocks
+ !pf
->discard_enabled
+
3426 !pf
->discard_passdown
+ (pf
->mode
== PM_READ_ONLY
) +
3427 pf
->error_if_no_space
;
3428 DMEMIT("%u ", count
);
3430 if (!pf
->zero_new_blocks
)
3431 DMEMIT("skip_block_zeroing ");
3433 if (!pf
->discard_enabled
)
3434 DMEMIT("ignore_discard ");
3436 if (!pf
->discard_passdown
)
3437 DMEMIT("no_discard_passdown ");
3439 if (pf
->mode
== PM_READ_ONLY
)
3440 DMEMIT("read_only ");
3442 if (pf
->error_if_no_space
)
3443 DMEMIT("error_if_no_space ");
3448 * <transaction id> <used metadata sectors>/<total metadata sectors>
3449 * <used data sectors>/<total data sectors> <held metadata root>
3451 static void pool_status(struct dm_target
*ti
, status_type_t type
,
3452 unsigned status_flags
, char *result
, unsigned maxlen
)
3456 uint64_t transaction_id
;
3457 dm_block_t nr_free_blocks_data
;
3458 dm_block_t nr_free_blocks_metadata
;
3459 dm_block_t nr_blocks_data
;
3460 dm_block_t nr_blocks_metadata
;
3461 dm_block_t held_root
;
3462 char buf
[BDEVNAME_SIZE
];
3463 char buf2
[BDEVNAME_SIZE
];
3464 struct pool_c
*pt
= ti
->private;
3465 struct pool
*pool
= pt
->pool
;
3468 case STATUSTYPE_INFO
:
3469 if (get_pool_mode(pool
) == PM_FAIL
) {
3474 /* Commit to ensure statistics aren't out-of-date */
3475 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
))
3476 (void) commit(pool
);
3478 r
= dm_pool_get_metadata_transaction_id(pool
->pmd
, &transaction_id
);
3480 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3481 dm_device_name(pool
->pool_md
), r
);
3485 r
= dm_pool_get_free_metadata_block_count(pool
->pmd
, &nr_free_blocks_metadata
);
3487 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3488 dm_device_name(pool
->pool_md
), r
);
3492 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &nr_blocks_metadata
);
3494 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3495 dm_device_name(pool
->pool_md
), r
);
3499 r
= dm_pool_get_free_block_count(pool
->pmd
, &nr_free_blocks_data
);
3501 DMERR("%s: dm_pool_get_free_block_count returned %d",
3502 dm_device_name(pool
->pool_md
), r
);
3506 r
= dm_pool_get_data_dev_size(pool
->pmd
, &nr_blocks_data
);
3508 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3509 dm_device_name(pool
->pool_md
), r
);
3513 r
= dm_pool_get_metadata_snap(pool
->pmd
, &held_root
);
3515 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3516 dm_device_name(pool
->pool_md
), r
);
3520 DMEMIT("%llu %llu/%llu %llu/%llu ",
3521 (unsigned long long)transaction_id
,
3522 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
3523 (unsigned long long)nr_blocks_metadata
,
3524 (unsigned long long)(nr_blocks_data
- nr_free_blocks_data
),
3525 (unsigned long long)nr_blocks_data
);
3528 DMEMIT("%llu ", held_root
);
3532 if (pool
->pf
.mode
== PM_OUT_OF_DATA_SPACE
)
3533 DMEMIT("out_of_data_space ");
3534 else if (pool
->pf
.mode
== PM_READ_ONLY
)
3539 if (!pool
->pf
.discard_enabled
)
3540 DMEMIT("ignore_discard ");
3541 else if (pool
->pf
.discard_passdown
)
3542 DMEMIT("discard_passdown ");
3544 DMEMIT("no_discard_passdown ");
3546 if (pool
->pf
.error_if_no_space
)
3547 DMEMIT("error_if_no_space ");
3549 DMEMIT("queue_if_no_space ");
3553 case STATUSTYPE_TABLE
:
3554 DMEMIT("%s %s %lu %llu ",
3555 format_dev_t(buf
, pt
->metadata_dev
->bdev
->bd_dev
),
3556 format_dev_t(buf2
, pt
->data_dev
->bdev
->bd_dev
),
3557 (unsigned long)pool
->sectors_per_block
,
3558 (unsigned long long)pt
->low_water_blocks
);
3559 emit_flags(&pt
->requested_pf
, result
, sz
, maxlen
);
3568 static int pool_iterate_devices(struct dm_target
*ti
,
3569 iterate_devices_callout_fn fn
, void *data
)
3571 struct pool_c
*pt
= ti
->private;
3573 return fn(ti
, pt
->data_dev
, 0, ti
->len
, data
);
3576 static int pool_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
3577 struct bio_vec
*biovec
, int max_size
)
3579 struct pool_c
*pt
= ti
->private;
3580 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
3582 if (!q
->merge_bvec_fn
)
3585 bvm
->bi_bdev
= pt
->data_dev
->bdev
;
3587 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
3590 static void set_discard_limits(struct pool_c
*pt
, struct queue_limits
*limits
)
3592 struct pool
*pool
= pt
->pool
;
3593 struct queue_limits
*data_limits
;
3595 limits
->max_discard_sectors
= pool
->sectors_per_block
;
3598 * discard_granularity is just a hint, and not enforced.
3600 if (pt
->adjusted_pf
.discard_passdown
) {
3601 data_limits
= &bdev_get_queue(pt
->data_dev
->bdev
)->limits
;
3602 limits
->discard_granularity
= max(data_limits
->discard_granularity
,
3603 pool
->sectors_per_block
<< SECTOR_SHIFT
);
3605 limits
->discard_granularity
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
3608 static void pool_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3610 struct pool_c
*pt
= ti
->private;
3611 struct pool
*pool
= pt
->pool
;
3612 sector_t io_opt_sectors
= limits
->io_opt
>> SECTOR_SHIFT
;
3615 * If max_sectors is smaller than pool->sectors_per_block adjust it
3616 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3617 * This is especially beneficial when the pool's data device is a RAID
3618 * device that has a full stripe width that matches pool->sectors_per_block
3619 * -- because even though partial RAID stripe-sized IOs will be issued to a
3620 * single RAID stripe; when aggregated they will end on a full RAID stripe
3621 * boundary.. which avoids additional partial RAID stripe writes cascading
3623 if (limits
->max_sectors
< pool
->sectors_per_block
) {
3624 while (!is_factor(pool
->sectors_per_block
, limits
->max_sectors
)) {
3625 if ((limits
->max_sectors
& (limits
->max_sectors
- 1)) == 0)
3626 limits
->max_sectors
--;
3627 limits
->max_sectors
= rounddown_pow_of_two(limits
->max_sectors
);
3632 * If the system-determined stacked limits are compatible with the
3633 * pool's blocksize (io_opt is a factor) do not override them.
3635 if (io_opt_sectors
< pool
->sectors_per_block
||
3636 !is_factor(io_opt_sectors
, pool
->sectors_per_block
)) {
3637 if (is_factor(pool
->sectors_per_block
, limits
->max_sectors
))
3638 blk_limits_io_min(limits
, limits
->max_sectors
<< SECTOR_SHIFT
);
3640 blk_limits_io_min(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
3641 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
3645 * pt->adjusted_pf is a staging area for the actual features to use.
3646 * They get transferred to the live pool in bind_control_target()
3647 * called from pool_preresume().
3649 if (!pt
->adjusted_pf
.discard_enabled
) {
3651 * Must explicitly disallow stacking discard limits otherwise the
3652 * block layer will stack them if pool's data device has support.
3653 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3654 * user to see that, so make sure to set all discard limits to 0.
3656 limits
->discard_granularity
= 0;
3660 disable_passdown_if_not_supported(pt
);
3662 set_discard_limits(pt
, limits
);
3665 static struct target_type pool_target
= {
3666 .name
= "thin-pool",
3667 .features
= DM_TARGET_SINGLETON
| DM_TARGET_ALWAYS_WRITEABLE
|
3668 DM_TARGET_IMMUTABLE
,
3669 .version
= {1, 14, 0},
3670 .module
= THIS_MODULE
,
3674 .presuspend
= pool_presuspend
,
3675 .presuspend_undo
= pool_presuspend_undo
,
3676 .postsuspend
= pool_postsuspend
,
3677 .preresume
= pool_preresume
,
3678 .resume
= pool_resume
,
3679 .message
= pool_message
,
3680 .status
= pool_status
,
3681 .merge
= pool_merge
,
3682 .iterate_devices
= pool_iterate_devices
,
3683 .io_hints
= pool_io_hints
,
3686 /*----------------------------------------------------------------
3687 * Thin target methods
3688 *--------------------------------------------------------------*/
3689 static void thin_get(struct thin_c
*tc
)
3691 atomic_inc(&tc
->refcount
);
3694 static void thin_put(struct thin_c
*tc
)
3696 if (atomic_dec_and_test(&tc
->refcount
))
3697 complete(&tc
->can_destroy
);
3700 static void thin_dtr(struct dm_target
*ti
)
3702 struct thin_c
*tc
= ti
->private;
3703 unsigned long flags
;
3705 spin_lock_irqsave(&tc
->pool
->lock
, flags
);
3706 list_del_rcu(&tc
->list
);
3707 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
3711 wait_for_completion(&tc
->can_destroy
);
3713 mutex_lock(&dm_thin_pool_table
.mutex
);
3715 __pool_dec(tc
->pool
);
3716 dm_pool_close_thin_device(tc
->td
);
3717 dm_put_device(ti
, tc
->pool_dev
);
3719 dm_put_device(ti
, tc
->origin_dev
);
3722 mutex_unlock(&dm_thin_pool_table
.mutex
);
3726 * Thin target parameters:
3728 * <pool_dev> <dev_id> [origin_dev]
3730 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3731 * dev_id: the internal device identifier
3732 * origin_dev: a device external to the pool that should act as the origin
3734 * If the pool device has discards disabled, they get disabled for the thin
3737 static int thin_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
3741 struct dm_dev
*pool_dev
, *origin_dev
;
3742 struct mapped_device
*pool_md
;
3743 unsigned long flags
;
3745 mutex_lock(&dm_thin_pool_table
.mutex
);
3747 if (argc
!= 2 && argc
!= 3) {
3748 ti
->error
= "Invalid argument count";
3753 tc
= ti
->private = kzalloc(sizeof(*tc
), GFP_KERNEL
);
3755 ti
->error
= "Out of memory";
3759 tc
->thin_md
= dm_table_get_md(ti
->table
);
3760 spin_lock_init(&tc
->lock
);
3761 INIT_LIST_HEAD(&tc
->deferred_cells
);
3762 bio_list_init(&tc
->deferred_bio_list
);
3763 bio_list_init(&tc
->retry_on_resume_list
);
3764 tc
->sort_bio_list
= RB_ROOT
;
3767 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &origin_dev
);
3769 ti
->error
= "Error opening origin device";
3770 goto bad_origin_dev
;
3772 tc
->origin_dev
= origin_dev
;
3775 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &pool_dev
);
3777 ti
->error
= "Error opening pool device";
3780 tc
->pool_dev
= pool_dev
;
3782 if (read_dev_id(argv
[1], (unsigned long long *)&tc
->dev_id
, 0)) {
3783 ti
->error
= "Invalid device id";
3788 pool_md
= dm_get_md(tc
->pool_dev
->bdev
->bd_dev
);
3790 ti
->error
= "Couldn't get pool mapped device";
3795 tc
->pool
= __pool_table_lookup(pool_md
);
3797 ti
->error
= "Couldn't find pool object";
3799 goto bad_pool_lookup
;
3801 __pool_inc(tc
->pool
);
3803 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3804 ti
->error
= "Couldn't open thin device, Pool is in fail mode";
3809 r
= dm_pool_open_thin_device(tc
->pool
->pmd
, tc
->dev_id
, &tc
->td
);
3811 ti
->error
= "Couldn't open thin internal device";
3815 r
= dm_set_target_max_io_len(ti
, tc
->pool
->sectors_per_block
);
3819 ti
->num_flush_bios
= 1;
3820 ti
->flush_supported
= true;
3821 ti
->per_bio_data_size
= sizeof(struct dm_thin_endio_hook
);
3823 /* In case the pool supports discards, pass them on. */
3824 ti
->discard_zeroes_data_unsupported
= true;
3825 if (tc
->pool
->pf
.discard_enabled
) {
3826 ti
->discards_supported
= true;
3827 ti
->num_discard_bios
= 1;
3828 /* Discard bios must be split on a block boundary */
3829 ti
->split_discard_bios
= true;
3832 mutex_unlock(&dm_thin_pool_table
.mutex
);
3834 spin_lock_irqsave(&tc
->pool
->lock
, flags
);
3835 if (tc
->pool
->suspended
) {
3836 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
3837 mutex_lock(&dm_thin_pool_table
.mutex
); /* reacquire for __pool_dec */
3838 ti
->error
= "Unable to activate thin device while pool is suspended";
3842 atomic_set(&tc
->refcount
, 1);
3843 init_completion(&tc
->can_destroy
);
3844 list_add_tail_rcu(&tc
->list
, &tc
->pool
->active_thins
);
3845 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
3847 * This synchronize_rcu() call is needed here otherwise we risk a
3848 * wake_worker() call finding no bios to process (because the newly
3849 * added tc isn't yet visible). So this reduces latency since we
3850 * aren't then dependent on the periodic commit to wake_worker().
3859 dm_pool_close_thin_device(tc
->td
);
3861 __pool_dec(tc
->pool
);
3865 dm_put_device(ti
, tc
->pool_dev
);
3868 dm_put_device(ti
, tc
->origin_dev
);
3872 mutex_unlock(&dm_thin_pool_table
.mutex
);
3877 static int thin_map(struct dm_target
*ti
, struct bio
*bio
)
3879 bio
->bi_iter
.bi_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
3881 return thin_bio_map(ti
, bio
);
3884 static int thin_endio(struct dm_target
*ti
, struct bio
*bio
, int err
)
3886 unsigned long flags
;
3887 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
3888 struct list_head work
;
3889 struct dm_thin_new_mapping
*m
, *tmp
;
3890 struct pool
*pool
= h
->tc
->pool
;
3892 if (h
->shared_read_entry
) {
3893 INIT_LIST_HEAD(&work
);
3894 dm_deferred_entry_dec(h
->shared_read_entry
, &work
);
3896 spin_lock_irqsave(&pool
->lock
, flags
);
3897 list_for_each_entry_safe(m
, tmp
, &work
, list
) {
3899 __complete_mapping_preparation(m
);
3901 spin_unlock_irqrestore(&pool
->lock
, flags
);
3904 if (h
->all_io_entry
) {
3905 INIT_LIST_HEAD(&work
);
3906 dm_deferred_entry_dec(h
->all_io_entry
, &work
);
3907 if (!list_empty(&work
)) {
3908 spin_lock_irqsave(&pool
->lock
, flags
);
3909 list_for_each_entry_safe(m
, tmp
, &work
, list
)
3910 list_add_tail(&m
->list
, &pool
->prepared_discards
);
3911 spin_unlock_irqrestore(&pool
->lock
, flags
);
3919 static void thin_presuspend(struct dm_target
*ti
)
3921 struct thin_c
*tc
= ti
->private;
3923 if (dm_noflush_suspending(ti
))
3924 noflush_work(tc
, do_noflush_start
);
3927 static void thin_postsuspend(struct dm_target
*ti
)
3929 struct thin_c
*tc
= ti
->private;
3932 * The dm_noflush_suspending flag has been cleared by now, so
3933 * unfortunately we must always run this.
3935 noflush_work(tc
, do_noflush_stop
);
3938 static int thin_preresume(struct dm_target
*ti
)
3940 struct thin_c
*tc
= ti
->private;
3943 tc
->origin_size
= get_dev_size(tc
->origin_dev
->bdev
);
3949 * <nr mapped sectors> <highest mapped sector>
3951 static void thin_status(struct dm_target
*ti
, status_type_t type
,
3952 unsigned status_flags
, char *result
, unsigned maxlen
)
3956 dm_block_t mapped
, highest
;
3957 char buf
[BDEVNAME_SIZE
];
3958 struct thin_c
*tc
= ti
->private;
3960 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3969 case STATUSTYPE_INFO
:
3970 r
= dm_thin_get_mapped_count(tc
->td
, &mapped
);
3972 DMERR("dm_thin_get_mapped_count returned %d", r
);
3976 r
= dm_thin_get_highest_mapped_block(tc
->td
, &highest
);
3978 DMERR("dm_thin_get_highest_mapped_block returned %d", r
);
3982 DMEMIT("%llu ", mapped
* tc
->pool
->sectors_per_block
);
3984 DMEMIT("%llu", ((highest
+ 1) *
3985 tc
->pool
->sectors_per_block
) - 1);
3990 case STATUSTYPE_TABLE
:
3992 format_dev_t(buf
, tc
->pool_dev
->bdev
->bd_dev
),
3993 (unsigned long) tc
->dev_id
);
3995 DMEMIT(" %s", format_dev_t(buf
, tc
->origin_dev
->bdev
->bd_dev
));
4006 static int thin_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
4007 struct bio_vec
*biovec
, int max_size
)
4009 struct thin_c
*tc
= ti
->private;
4010 struct request_queue
*q
= bdev_get_queue(tc
->pool_dev
->bdev
);
4012 if (!q
->merge_bvec_fn
)
4015 bvm
->bi_bdev
= tc
->pool_dev
->bdev
;
4016 bvm
->bi_sector
= dm_target_offset(ti
, bvm
->bi_sector
);
4018 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
4021 static int thin_iterate_devices(struct dm_target
*ti
,
4022 iterate_devices_callout_fn fn
, void *data
)
4025 struct thin_c
*tc
= ti
->private;
4026 struct pool
*pool
= tc
->pool
;
4029 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4030 * we follow a more convoluted path through to the pool's target.
4033 return 0; /* nothing is bound */
4035 blocks
= pool
->ti
->len
;
4036 (void) sector_div(blocks
, pool
->sectors_per_block
);
4038 return fn(ti
, tc
->pool_dev
, 0, pool
->sectors_per_block
* blocks
, data
);
4043 static struct target_type thin_target
= {
4045 .version
= {1, 14, 0},
4046 .module
= THIS_MODULE
,
4050 .end_io
= thin_endio
,
4051 .preresume
= thin_preresume
,
4052 .presuspend
= thin_presuspend
,
4053 .postsuspend
= thin_postsuspend
,
4054 .status
= thin_status
,
4055 .merge
= thin_merge
,
4056 .iterate_devices
= thin_iterate_devices
,
4059 /*----------------------------------------------------------------*/
4061 static int __init
dm_thin_init(void)
4067 r
= dm_register_target(&thin_target
);
4071 r
= dm_register_target(&pool_target
);
4073 goto bad_pool_target
;
4077 _new_mapping_cache
= KMEM_CACHE(dm_thin_new_mapping
, 0);
4078 if (!_new_mapping_cache
)
4079 goto bad_new_mapping_cache
;
4083 bad_new_mapping_cache
:
4084 dm_unregister_target(&pool_target
);
4086 dm_unregister_target(&thin_target
);
4091 static void dm_thin_exit(void)
4093 dm_unregister_target(&thin_target
);
4094 dm_unregister_target(&pool_target
);
4096 kmem_cache_destroy(_new_mapping_cache
);
4099 module_init(dm_thin_init
);
4100 module_exit(dm_thin_exit
);
4102 module_param_named(no_space_timeout
, no_space_timeout_secs
, uint
, S_IRUGO
| S_IWUSR
);
4103 MODULE_PARM_DESC(no_space_timeout
, "Out of data space queue IO timeout in seconds");
4105 MODULE_DESCRIPTION(DM_NAME
" thin provisioning target");
4106 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4107 MODULE_LICENSE("GPL");