2 * Copyright (C) 2011-2012 Red Hat UK.
4 * This file is released under the GPL.
7 #include "dm-thin-metadata.h"
8 #include "dm-bio-prison.h"
11 #include <linux/device-mapper.h>
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/list.h>
15 #include <linux/rculist.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/rbtree.h>
21 #define DM_MSG_PREFIX "thin"
26 #define ENDIO_HOOK_POOL_SIZE 1024
27 #define MAPPING_POOL_SIZE 1024
28 #define COMMIT_PERIOD HZ
29 #define NO_SPACE_TIMEOUT_SECS 60
31 static unsigned no_space_timeout_secs
= NO_SPACE_TIMEOUT_SECS
;
33 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle
,
34 "A percentage of time allocated for copy on write");
37 * The block size of the device holding pool data must be
38 * between 64KB and 1GB.
40 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
41 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
44 * Device id is restricted to 24 bits.
46 #define MAX_DEV_ID ((1 << 24) - 1)
49 * How do we handle breaking sharing of data blocks?
50 * =================================================
52 * We use a standard copy-on-write btree to store the mappings for the
53 * devices (note I'm talking about copy-on-write of the metadata here, not
54 * the data). When you take an internal snapshot you clone the root node
55 * of the origin btree. After this there is no concept of an origin or a
56 * snapshot. They are just two device trees that happen to point to the
59 * When we get a write in we decide if it's to a shared data block using
60 * some timestamp magic. If it is, we have to break sharing.
62 * Let's say we write to a shared block in what was the origin. The
65 * i) plug io further to this physical block. (see bio_prison code).
67 * ii) quiesce any read io to that shared data block. Obviously
68 * including all devices that share this block. (see dm_deferred_set code)
70 * iii) copy the data block to a newly allocate block. This step can be
71 * missed out if the io covers the block. (schedule_copy).
73 * iv) insert the new mapping into the origin's btree
74 * (process_prepared_mapping). This act of inserting breaks some
75 * sharing of btree nodes between the two devices. Breaking sharing only
76 * effects the btree of that specific device. Btrees for the other
77 * devices that share the block never change. The btree for the origin
78 * device as it was after the last commit is untouched, ie. we're using
79 * persistent data structures in the functional programming sense.
81 * v) unplug io to this physical block, including the io that triggered
82 * the breaking of sharing.
84 * Steps (ii) and (iii) occur in parallel.
86 * The metadata _doesn't_ need to be committed before the io continues. We
87 * get away with this because the io is always written to a _new_ block.
88 * If there's a crash, then:
90 * - The origin mapping will point to the old origin block (the shared
91 * one). This will contain the data as it was before the io that triggered
92 * the breaking of sharing came in.
94 * - The snap mapping still points to the old block. As it would after
97 * The downside of this scheme is the timestamp magic isn't perfect, and
98 * will continue to think that data block in the snapshot device is shared
99 * even after the write to the origin has broken sharing. I suspect data
100 * blocks will typically be shared by many different devices, so we're
101 * breaking sharing n + 1 times, rather than n, where n is the number of
102 * devices that reference this data block. At the moment I think the
103 * benefits far, far outweigh the disadvantages.
106 /*----------------------------------------------------------------*/
111 static void build_data_key(struct dm_thin_device
*td
,
112 dm_block_t b
, struct dm_cell_key
*key
)
115 key
->dev
= dm_thin_dev_id(td
);
119 static void build_virtual_key(struct dm_thin_device
*td
, dm_block_t b
,
120 struct dm_cell_key
*key
)
123 key
->dev
= dm_thin_dev_id(td
);
127 /*----------------------------------------------------------------*/
129 #define THROTTLE_THRESHOLD (1 * HZ)
132 struct rw_semaphore lock
;
133 unsigned long threshold
;
134 bool throttle_applied
;
137 static void throttle_init(struct throttle
*t
)
139 init_rwsem(&t
->lock
);
140 t
->throttle_applied
= false;
143 static void throttle_work_start(struct throttle
*t
)
145 t
->threshold
= jiffies
+ THROTTLE_THRESHOLD
;
148 static void throttle_work_update(struct throttle
*t
)
150 if (!t
->throttle_applied
&& jiffies
> t
->threshold
) {
151 down_write(&t
->lock
);
152 t
->throttle_applied
= true;
156 static void throttle_work_complete(struct throttle
*t
)
158 if (t
->throttle_applied
) {
159 t
->throttle_applied
= false;
164 static void throttle_lock(struct throttle
*t
)
169 static void throttle_unlock(struct throttle
*t
)
174 /*----------------------------------------------------------------*/
177 * A pool device ties together a metadata device and a data device. It
178 * also provides the interface for creating and destroying internal
181 struct dm_thin_new_mapping
;
184 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
187 PM_WRITE
, /* metadata may be changed */
188 PM_OUT_OF_DATA_SPACE
, /* metadata may be changed, though data may not be allocated */
189 PM_READ_ONLY
, /* metadata may not be changed */
190 PM_FAIL
, /* all I/O fails */
193 struct pool_features
{
196 bool zero_new_blocks
:1;
197 bool discard_enabled
:1;
198 bool discard_passdown
:1;
199 bool error_if_no_space
:1;
203 typedef void (*process_bio_fn
)(struct thin_c
*tc
, struct bio
*bio
);
204 typedef void (*process_mapping_fn
)(struct dm_thin_new_mapping
*m
);
207 struct list_head list
;
208 struct dm_target
*ti
; /* Only set if a pool target is bound */
210 struct mapped_device
*pool_md
;
211 struct block_device
*md_dev
;
212 struct dm_pool_metadata
*pmd
;
214 dm_block_t low_water_blocks
;
215 uint32_t sectors_per_block
;
216 int sectors_per_block_shift
;
218 struct pool_features pf
;
219 bool low_water_triggered
:1; /* A dm event has been sent */
221 struct dm_bio_prison
*prison
;
222 struct dm_kcopyd_client
*copier
;
224 struct workqueue_struct
*wq
;
225 struct throttle throttle
;
226 struct work_struct worker
;
227 struct delayed_work waker
;
228 struct delayed_work no_space_timeout
;
230 unsigned long last_commit_jiffies
;
234 struct bio_list deferred_flush_bios
;
235 struct list_head prepared_mappings
;
236 struct list_head prepared_discards
;
237 struct list_head active_thins
;
239 struct dm_deferred_set
*shared_read_ds
;
240 struct dm_deferred_set
*all_io_ds
;
242 struct dm_thin_new_mapping
*next_mapping
;
243 mempool_t
*mapping_pool
;
245 process_bio_fn process_bio
;
246 process_bio_fn process_discard
;
248 process_mapping_fn process_prepared_mapping
;
249 process_mapping_fn process_prepared_discard
;
252 static enum pool_mode
get_pool_mode(struct pool
*pool
);
253 static void metadata_operation_failed(struct pool
*pool
, const char *op
, int r
);
256 * Target context for a pool.
259 struct dm_target
*ti
;
261 struct dm_dev
*data_dev
;
262 struct dm_dev
*metadata_dev
;
263 struct dm_target_callbacks callbacks
;
265 dm_block_t low_water_blocks
;
266 struct pool_features requested_pf
; /* Features requested during table load */
267 struct pool_features adjusted_pf
; /* Features used after adjusting for constituent devices */
271 * Target context for a thin.
274 struct list_head list
;
275 struct dm_dev
*pool_dev
;
276 struct dm_dev
*origin_dev
;
277 sector_t origin_size
;
281 struct dm_thin_device
*td
;
284 struct bio_list deferred_bio_list
;
285 struct bio_list retry_on_resume_list
;
286 struct rb_root sort_bio_list
; /* sorted list of deferred bios */
289 * Ensures the thin is not destroyed until the worker has finished
290 * iterating the active_thins list.
293 struct completion can_destroy
;
296 /*----------------------------------------------------------------*/
299 * wake_worker() is used when new work is queued and when pool_resume is
300 * ready to continue deferred IO processing.
302 static void wake_worker(struct pool
*pool
)
304 queue_work(pool
->wq
, &pool
->worker
);
307 /*----------------------------------------------------------------*/
309 static int bio_detain(struct pool
*pool
, struct dm_cell_key
*key
, struct bio
*bio
,
310 struct dm_bio_prison_cell
**cell_result
)
313 struct dm_bio_prison_cell
*cell_prealloc
;
316 * Allocate a cell from the prison's mempool.
317 * This might block but it can't fail.
319 cell_prealloc
= dm_bio_prison_alloc_cell(pool
->prison
, GFP_NOIO
);
321 r
= dm_bio_detain(pool
->prison
, key
, bio
, cell_prealloc
, cell_result
);
324 * We reused an old cell; we can get rid of
327 dm_bio_prison_free_cell(pool
->prison
, cell_prealloc
);
332 static void cell_release(struct pool
*pool
,
333 struct dm_bio_prison_cell
*cell
,
334 struct bio_list
*bios
)
336 dm_cell_release(pool
->prison
, cell
, bios
);
337 dm_bio_prison_free_cell(pool
->prison
, cell
);
340 static void cell_release_no_holder(struct pool
*pool
,
341 struct dm_bio_prison_cell
*cell
,
342 struct bio_list
*bios
)
344 dm_cell_release_no_holder(pool
->prison
, cell
, bios
);
345 dm_bio_prison_free_cell(pool
->prison
, cell
);
348 static void cell_defer_no_holder_no_free(struct thin_c
*tc
,
349 struct dm_bio_prison_cell
*cell
)
351 struct pool
*pool
= tc
->pool
;
354 spin_lock_irqsave(&tc
->lock
, flags
);
355 dm_cell_release_no_holder(pool
->prison
, cell
, &tc
->deferred_bio_list
);
356 spin_unlock_irqrestore(&tc
->lock
, flags
);
361 static void cell_error_with_code(struct pool
*pool
,
362 struct dm_bio_prison_cell
*cell
, int error_code
)
364 dm_cell_error(pool
->prison
, cell
, error_code
);
365 dm_bio_prison_free_cell(pool
->prison
, cell
);
368 static void cell_error(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
370 cell_error_with_code(pool
, cell
, -EIO
);
373 /*----------------------------------------------------------------*/
376 * A global list of pools that uses a struct mapped_device as a key.
378 static struct dm_thin_pool_table
{
380 struct list_head pools
;
381 } dm_thin_pool_table
;
383 static void pool_table_init(void)
385 mutex_init(&dm_thin_pool_table
.mutex
);
386 INIT_LIST_HEAD(&dm_thin_pool_table
.pools
);
389 static void __pool_table_insert(struct pool
*pool
)
391 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
392 list_add(&pool
->list
, &dm_thin_pool_table
.pools
);
395 static void __pool_table_remove(struct pool
*pool
)
397 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
398 list_del(&pool
->list
);
401 static struct pool
*__pool_table_lookup(struct mapped_device
*md
)
403 struct pool
*pool
= NULL
, *tmp
;
405 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
407 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
408 if (tmp
->pool_md
== md
) {
417 static struct pool
*__pool_table_lookup_metadata_dev(struct block_device
*md_dev
)
419 struct pool
*pool
= NULL
, *tmp
;
421 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
423 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
424 if (tmp
->md_dev
== md_dev
) {
433 /*----------------------------------------------------------------*/
435 struct dm_thin_endio_hook
{
437 struct dm_deferred_entry
*shared_read_entry
;
438 struct dm_deferred_entry
*all_io_entry
;
439 struct dm_thin_new_mapping
*overwrite_mapping
;
440 struct rb_node rb_node
;
443 static void requeue_bio_list(struct thin_c
*tc
, struct bio_list
*master
)
446 struct bio_list bios
;
449 bio_list_init(&bios
);
451 spin_lock_irqsave(&tc
->lock
, flags
);
452 bio_list_merge(&bios
, master
);
453 bio_list_init(master
);
454 spin_unlock_irqrestore(&tc
->lock
, flags
);
456 while ((bio
= bio_list_pop(&bios
)))
457 bio_endio(bio
, DM_ENDIO_REQUEUE
);
460 static void requeue_io(struct thin_c
*tc
)
462 requeue_bio_list(tc
, &tc
->deferred_bio_list
);
463 requeue_bio_list(tc
, &tc
->retry_on_resume_list
);
466 static void error_thin_retry_list(struct thin_c
*tc
)
470 struct bio_list bios
;
472 bio_list_init(&bios
);
474 spin_lock_irqsave(&tc
->lock
, flags
);
475 bio_list_merge(&bios
, &tc
->retry_on_resume_list
);
476 bio_list_init(&tc
->retry_on_resume_list
);
477 spin_unlock_irqrestore(&tc
->lock
, flags
);
479 while ((bio
= bio_list_pop(&bios
)))
483 static void error_retry_list(struct pool
*pool
)
488 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
)
489 error_thin_retry_list(tc
);
494 * This section of code contains the logic for processing a thin device's IO.
495 * Much of the code depends on pool object resources (lists, workqueues, etc)
496 * but most is exclusively called from the thin target rather than the thin-pool
500 static bool block_size_is_power_of_two(struct pool
*pool
)
502 return pool
->sectors_per_block_shift
>= 0;
505 static dm_block_t
get_bio_block(struct thin_c
*tc
, struct bio
*bio
)
507 struct pool
*pool
= tc
->pool
;
508 sector_t block_nr
= bio
->bi_iter
.bi_sector
;
510 if (block_size_is_power_of_two(pool
))
511 block_nr
>>= pool
->sectors_per_block_shift
;
513 (void) sector_div(block_nr
, pool
->sectors_per_block
);
518 static void remap(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
)
520 struct pool
*pool
= tc
->pool
;
521 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
523 bio
->bi_bdev
= tc
->pool_dev
->bdev
;
524 if (block_size_is_power_of_two(pool
))
525 bio
->bi_iter
.bi_sector
=
526 (block
<< pool
->sectors_per_block_shift
) |
527 (bi_sector
& (pool
->sectors_per_block
- 1));
529 bio
->bi_iter
.bi_sector
= (block
* pool
->sectors_per_block
) +
530 sector_div(bi_sector
, pool
->sectors_per_block
);
533 static void remap_to_origin(struct thin_c
*tc
, struct bio
*bio
)
535 bio
->bi_bdev
= tc
->origin_dev
->bdev
;
538 static int bio_triggers_commit(struct thin_c
*tc
, struct bio
*bio
)
540 return (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) &&
541 dm_thin_changed_this_transaction(tc
->td
);
544 static void inc_all_io_entry(struct pool
*pool
, struct bio
*bio
)
546 struct dm_thin_endio_hook
*h
;
548 if (bio
->bi_rw
& REQ_DISCARD
)
551 h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
552 h
->all_io_entry
= dm_deferred_entry_inc(pool
->all_io_ds
);
555 static void issue(struct thin_c
*tc
, struct bio
*bio
)
557 struct pool
*pool
= tc
->pool
;
560 if (!bio_triggers_commit(tc
, bio
)) {
561 generic_make_request(bio
);
566 * Complete bio with an error if earlier I/O caused changes to
567 * the metadata that can't be committed e.g, due to I/O errors
568 * on the metadata device.
570 if (dm_thin_aborted_changes(tc
->td
)) {
576 * Batch together any bios that trigger commits and then issue a
577 * single commit for them in process_deferred_bios().
579 spin_lock_irqsave(&pool
->lock
, flags
);
580 bio_list_add(&pool
->deferred_flush_bios
, bio
);
581 spin_unlock_irqrestore(&pool
->lock
, flags
);
584 static void remap_to_origin_and_issue(struct thin_c
*tc
, struct bio
*bio
)
586 remap_to_origin(tc
, bio
);
590 static void remap_and_issue(struct thin_c
*tc
, struct bio
*bio
,
593 remap(tc
, bio
, block
);
597 /*----------------------------------------------------------------*/
600 * Bio endio functions.
602 struct dm_thin_new_mapping
{
603 struct list_head list
;
606 bool definitely_not_shared
:1;
609 * Track quiescing, copying and zeroing preparation actions. When this
610 * counter hits zero the block is prepared and can be inserted into the
613 atomic_t prepare_actions
;
617 dm_block_t virt_block
;
618 dm_block_t data_block
;
619 struct dm_bio_prison_cell
*cell
, *cell2
;
622 * If the bio covers the whole area of a block then we can avoid
623 * zeroing or copying. Instead this bio is hooked. The bio will
624 * still be in the cell, so care has to be taken to avoid issuing
628 bio_end_io_t
*saved_bi_end_io
;
631 static void __complete_mapping_preparation(struct dm_thin_new_mapping
*m
)
633 struct pool
*pool
= m
->tc
->pool
;
635 if (atomic_dec_and_test(&m
->prepare_actions
)) {
636 list_add_tail(&m
->list
, &pool
->prepared_mappings
);
641 static void complete_mapping_preparation(struct dm_thin_new_mapping
*m
)
644 struct pool
*pool
= m
->tc
->pool
;
646 spin_lock_irqsave(&pool
->lock
, flags
);
647 __complete_mapping_preparation(m
);
648 spin_unlock_irqrestore(&pool
->lock
, flags
);
651 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
653 struct dm_thin_new_mapping
*m
= context
;
655 m
->err
= read_err
|| write_err
? -EIO
: 0;
656 complete_mapping_preparation(m
);
659 static void overwrite_endio(struct bio
*bio
, int err
)
661 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
662 struct dm_thin_new_mapping
*m
= h
->overwrite_mapping
;
665 complete_mapping_preparation(m
);
668 /*----------------------------------------------------------------*/
675 * Prepared mapping jobs.
679 * This sends the bios in the cell back to the deferred_bios list.
681 static void cell_defer(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
683 struct pool
*pool
= tc
->pool
;
686 spin_lock_irqsave(&tc
->lock
, flags
);
687 cell_release(pool
, cell
, &tc
->deferred_bio_list
);
688 spin_unlock_irqrestore(&tc
->lock
, flags
);
694 * Same as cell_defer above, except it omits the original holder of the cell.
696 static void cell_defer_no_holder(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
698 struct pool
*pool
= tc
->pool
;
701 spin_lock_irqsave(&tc
->lock
, flags
);
702 cell_release_no_holder(pool
, cell
, &tc
->deferred_bio_list
);
703 spin_unlock_irqrestore(&tc
->lock
, flags
);
708 static void process_prepared_mapping_fail(struct dm_thin_new_mapping
*m
)
711 m
->bio
->bi_end_io
= m
->saved_bi_end_io
;
712 atomic_inc(&m
->bio
->bi_remaining
);
714 cell_error(m
->tc
->pool
, m
->cell
);
716 mempool_free(m
, m
->tc
->pool
->mapping_pool
);
719 static void process_prepared_mapping(struct dm_thin_new_mapping
*m
)
721 struct thin_c
*tc
= m
->tc
;
722 struct pool
*pool
= tc
->pool
;
728 bio
->bi_end_io
= m
->saved_bi_end_io
;
729 atomic_inc(&bio
->bi_remaining
);
733 cell_error(pool
, m
->cell
);
738 * Commit the prepared block into the mapping btree.
739 * Any I/O for this block arriving after this point will get
740 * remapped to it directly.
742 r
= dm_thin_insert_block(tc
->td
, m
->virt_block
, m
->data_block
);
744 metadata_operation_failed(pool
, "dm_thin_insert_block", r
);
745 cell_error(pool
, m
->cell
);
750 * Release any bios held while the block was being provisioned.
751 * If we are processing a write bio that completely covers the block,
752 * we already processed it so can ignore it now when processing
753 * the bios in the cell.
756 cell_defer_no_holder(tc
, m
->cell
);
759 cell_defer(tc
, m
->cell
);
763 mempool_free(m
, pool
->mapping_pool
);
766 static void process_prepared_discard_fail(struct dm_thin_new_mapping
*m
)
768 struct thin_c
*tc
= m
->tc
;
770 bio_io_error(m
->bio
);
771 cell_defer_no_holder(tc
, m
->cell
);
772 cell_defer_no_holder(tc
, m
->cell2
);
773 mempool_free(m
, tc
->pool
->mapping_pool
);
776 static void process_prepared_discard_passdown(struct dm_thin_new_mapping
*m
)
778 struct thin_c
*tc
= m
->tc
;
780 inc_all_io_entry(tc
->pool
, m
->bio
);
781 cell_defer_no_holder(tc
, m
->cell
);
782 cell_defer_no_holder(tc
, m
->cell2
);
785 if (m
->definitely_not_shared
)
786 remap_and_issue(tc
, m
->bio
, m
->data_block
);
789 if (dm_pool_block_is_used(tc
->pool
->pmd
, m
->data_block
, &used
) || used
)
790 bio_endio(m
->bio
, 0);
792 remap_and_issue(tc
, m
->bio
, m
->data_block
);
795 bio_endio(m
->bio
, 0);
797 mempool_free(m
, tc
->pool
->mapping_pool
);
800 static void process_prepared_discard(struct dm_thin_new_mapping
*m
)
803 struct thin_c
*tc
= m
->tc
;
805 r
= dm_thin_remove_block(tc
->td
, m
->virt_block
);
807 DMERR_LIMIT("dm_thin_remove_block() failed");
809 process_prepared_discard_passdown(m
);
812 static void process_prepared(struct pool
*pool
, struct list_head
*head
,
813 process_mapping_fn
*fn
)
816 struct list_head maps
;
817 struct dm_thin_new_mapping
*m
, *tmp
;
819 INIT_LIST_HEAD(&maps
);
820 spin_lock_irqsave(&pool
->lock
, flags
);
821 list_splice_init(head
, &maps
);
822 spin_unlock_irqrestore(&pool
->lock
, flags
);
824 list_for_each_entry_safe(m
, tmp
, &maps
, list
)
831 static int io_overlaps_block(struct pool
*pool
, struct bio
*bio
)
833 return bio
->bi_iter
.bi_size
==
834 (pool
->sectors_per_block
<< SECTOR_SHIFT
);
837 static int io_overwrites_block(struct pool
*pool
, struct bio
*bio
)
839 return (bio_data_dir(bio
) == WRITE
) &&
840 io_overlaps_block(pool
, bio
);
843 static void save_and_set_endio(struct bio
*bio
, bio_end_io_t
**save
,
846 *save
= bio
->bi_end_io
;
850 static int ensure_next_mapping(struct pool
*pool
)
852 if (pool
->next_mapping
)
855 pool
->next_mapping
= mempool_alloc(pool
->mapping_pool
, GFP_ATOMIC
);
857 return pool
->next_mapping
? 0 : -ENOMEM
;
860 static struct dm_thin_new_mapping
*get_next_mapping(struct pool
*pool
)
862 struct dm_thin_new_mapping
*m
= pool
->next_mapping
;
864 BUG_ON(!pool
->next_mapping
);
866 memset(m
, 0, sizeof(struct dm_thin_new_mapping
));
867 INIT_LIST_HEAD(&m
->list
);
870 pool
->next_mapping
= NULL
;
875 static void ll_zero(struct thin_c
*tc
, struct dm_thin_new_mapping
*m
,
876 sector_t begin
, sector_t end
)
879 struct dm_io_region to
;
881 to
.bdev
= tc
->pool_dev
->bdev
;
883 to
.count
= end
- begin
;
885 r
= dm_kcopyd_zero(tc
->pool
->copier
, 1, &to
, 0, copy_complete
, m
);
887 DMERR_LIMIT("dm_kcopyd_zero() failed");
888 copy_complete(1, 1, m
);
893 * A partial copy also needs to zero the uncopied region.
895 static void schedule_copy(struct thin_c
*tc
, dm_block_t virt_block
,
896 struct dm_dev
*origin
, dm_block_t data_origin
,
897 dm_block_t data_dest
,
898 struct dm_bio_prison_cell
*cell
, struct bio
*bio
,
902 struct pool
*pool
= tc
->pool
;
903 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
906 m
->virt_block
= virt_block
;
907 m
->data_block
= data_dest
;
911 * quiesce action + copy action + an extra reference held for the
912 * duration of this function (we may need to inc later for a
915 atomic_set(&m
->prepare_actions
, 3);
917 if (!dm_deferred_set_add_work(pool
->shared_read_ds
, &m
->list
))
918 complete_mapping_preparation(m
); /* already quiesced */
921 * IO to pool_dev remaps to the pool target's data_dev.
923 * If the whole block of data is being overwritten, we can issue the
924 * bio immediately. Otherwise we use kcopyd to clone the data first.
926 if (io_overwrites_block(pool
, bio
)) {
927 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
929 h
->overwrite_mapping
= m
;
931 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
932 inc_all_io_entry(pool
, bio
);
933 remap_and_issue(tc
, bio
, data_dest
);
935 struct dm_io_region from
, to
;
937 from
.bdev
= origin
->bdev
;
938 from
.sector
= data_origin
* pool
->sectors_per_block
;
941 to
.bdev
= tc
->pool_dev
->bdev
;
942 to
.sector
= data_dest
* pool
->sectors_per_block
;
945 r
= dm_kcopyd_copy(pool
->copier
, &from
, 1, &to
,
946 0, copy_complete
, m
);
948 DMERR_LIMIT("dm_kcopyd_copy() failed");
949 copy_complete(1, 1, m
);
952 * We allow the zero to be issued, to simplify the
953 * error path. Otherwise we'd need to start
954 * worrying about decrementing the prepare_actions
960 * Do we need to zero a tail region?
962 if (len
< pool
->sectors_per_block
&& pool
->pf
.zero_new_blocks
) {
963 atomic_inc(&m
->prepare_actions
);
965 data_dest
* pool
->sectors_per_block
+ len
,
966 (data_dest
+ 1) * pool
->sectors_per_block
);
970 complete_mapping_preparation(m
); /* drop our ref */
973 static void schedule_internal_copy(struct thin_c
*tc
, dm_block_t virt_block
,
974 dm_block_t data_origin
, dm_block_t data_dest
,
975 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
977 schedule_copy(tc
, virt_block
, tc
->pool_dev
,
978 data_origin
, data_dest
, cell
, bio
,
979 tc
->pool
->sectors_per_block
);
982 static void schedule_zero(struct thin_c
*tc
, dm_block_t virt_block
,
983 dm_block_t data_block
, struct dm_bio_prison_cell
*cell
,
986 struct pool
*pool
= tc
->pool
;
987 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
989 atomic_set(&m
->prepare_actions
, 1); /* no need to quiesce */
991 m
->virt_block
= virt_block
;
992 m
->data_block
= data_block
;
996 * If the whole block of data is being overwritten or we are not
997 * zeroing pre-existing data, we can issue the bio immediately.
998 * Otherwise we use kcopyd to zero the data first.
1000 if (!pool
->pf
.zero_new_blocks
)
1001 process_prepared_mapping(m
);
1003 else if (io_overwrites_block(pool
, bio
)) {
1004 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1006 h
->overwrite_mapping
= m
;
1008 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
1009 inc_all_io_entry(pool
, bio
);
1010 remap_and_issue(tc
, bio
, data_block
);
1014 data_block
* pool
->sectors_per_block
,
1015 (data_block
+ 1) * pool
->sectors_per_block
);
1018 static void schedule_external_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1019 dm_block_t data_dest
,
1020 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
1022 struct pool
*pool
= tc
->pool
;
1023 sector_t virt_block_begin
= virt_block
* pool
->sectors_per_block
;
1024 sector_t virt_block_end
= (virt_block
+ 1) * pool
->sectors_per_block
;
1026 if (virt_block_end
<= tc
->origin_size
)
1027 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
1028 virt_block
, data_dest
, cell
, bio
,
1029 pool
->sectors_per_block
);
1031 else if (virt_block_begin
< tc
->origin_size
)
1032 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
1033 virt_block
, data_dest
, cell
, bio
,
1034 tc
->origin_size
- virt_block_begin
);
1037 schedule_zero(tc
, virt_block
, data_dest
, cell
, bio
);
1041 * A non-zero return indicates read_only or fail_io mode.
1042 * Many callers don't care about the return value.
1044 static int commit(struct pool
*pool
)
1048 if (get_pool_mode(pool
) >= PM_READ_ONLY
)
1051 r
= dm_pool_commit_metadata(pool
->pmd
);
1053 metadata_operation_failed(pool
, "dm_pool_commit_metadata", r
);
1058 static void check_low_water_mark(struct pool
*pool
, dm_block_t free_blocks
)
1060 unsigned long flags
;
1062 if (free_blocks
<= pool
->low_water_blocks
&& !pool
->low_water_triggered
) {
1063 DMWARN("%s: reached low water mark for data device: sending event.",
1064 dm_device_name(pool
->pool_md
));
1065 spin_lock_irqsave(&pool
->lock
, flags
);
1066 pool
->low_water_triggered
= true;
1067 spin_unlock_irqrestore(&pool
->lock
, flags
);
1068 dm_table_event(pool
->ti
->table
);
1072 static void set_pool_mode(struct pool
*pool
, enum pool_mode new_mode
);
1074 static int alloc_data_block(struct thin_c
*tc
, dm_block_t
*result
)
1077 dm_block_t free_blocks
;
1078 struct pool
*pool
= tc
->pool
;
1080 if (WARN_ON(get_pool_mode(pool
) != PM_WRITE
))
1083 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
1085 metadata_operation_failed(pool
, "dm_pool_get_free_block_count", r
);
1089 check_low_water_mark(pool
, free_blocks
);
1093 * Try to commit to see if that will free up some
1100 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
1102 metadata_operation_failed(pool
, "dm_pool_get_free_block_count", r
);
1107 set_pool_mode(pool
, PM_OUT_OF_DATA_SPACE
);
1112 r
= dm_pool_alloc_data_block(pool
->pmd
, result
);
1114 metadata_operation_failed(pool
, "dm_pool_alloc_data_block", r
);
1122 * If we have run out of space, queue bios until the device is
1123 * resumed, presumably after having been reloaded with more space.
1125 static void retry_on_resume(struct bio
*bio
)
1127 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1128 struct thin_c
*tc
= h
->tc
;
1129 unsigned long flags
;
1131 spin_lock_irqsave(&tc
->lock
, flags
);
1132 bio_list_add(&tc
->retry_on_resume_list
, bio
);
1133 spin_unlock_irqrestore(&tc
->lock
, flags
);
1136 static int should_error_unserviceable_bio(struct pool
*pool
)
1138 enum pool_mode m
= get_pool_mode(pool
);
1142 /* Shouldn't get here */
1143 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1146 case PM_OUT_OF_DATA_SPACE
:
1147 return pool
->pf
.error_if_no_space
? -ENOSPC
: 0;
1153 /* Shouldn't get here */
1154 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1159 static void handle_unserviceable_bio(struct pool
*pool
, struct bio
*bio
)
1161 int error
= should_error_unserviceable_bio(pool
);
1164 bio_endio(bio
, error
);
1166 retry_on_resume(bio
);
1169 static void retry_bios_on_resume(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
1172 struct bio_list bios
;
1175 error
= should_error_unserviceable_bio(pool
);
1177 cell_error_with_code(pool
, cell
, error
);
1181 bio_list_init(&bios
);
1182 cell_release(pool
, cell
, &bios
);
1184 error
= should_error_unserviceable_bio(pool
);
1186 while ((bio
= bio_list_pop(&bios
)))
1187 bio_endio(bio
, error
);
1189 while ((bio
= bio_list_pop(&bios
)))
1190 retry_on_resume(bio
);
1193 static void process_discard(struct thin_c
*tc
, struct bio
*bio
)
1196 unsigned long flags
;
1197 struct pool
*pool
= tc
->pool
;
1198 struct dm_bio_prison_cell
*cell
, *cell2
;
1199 struct dm_cell_key key
, key2
;
1200 dm_block_t block
= get_bio_block(tc
, bio
);
1201 struct dm_thin_lookup_result lookup_result
;
1202 struct dm_thin_new_mapping
*m
;
1204 build_virtual_key(tc
->td
, block
, &key
);
1205 if (bio_detain(tc
->pool
, &key
, bio
, &cell
))
1208 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1212 * Check nobody is fiddling with this pool block. This can
1213 * happen if someone's in the process of breaking sharing
1216 build_data_key(tc
->td
, lookup_result
.block
, &key2
);
1217 if (bio_detain(tc
->pool
, &key2
, bio
, &cell2
)) {
1218 cell_defer_no_holder(tc
, cell
);
1222 if (io_overlaps_block(pool
, bio
)) {
1224 * IO may still be going to the destination block. We must
1225 * quiesce before we can do the removal.
1227 m
= get_next_mapping(pool
);
1229 m
->pass_discard
= pool
->pf
.discard_passdown
;
1230 m
->definitely_not_shared
= !lookup_result
.shared
;
1231 m
->virt_block
= block
;
1232 m
->data_block
= lookup_result
.block
;
1237 if (!dm_deferred_set_add_work(pool
->all_io_ds
, &m
->list
)) {
1238 spin_lock_irqsave(&pool
->lock
, flags
);
1239 list_add_tail(&m
->list
, &pool
->prepared_discards
);
1240 spin_unlock_irqrestore(&pool
->lock
, flags
);
1244 inc_all_io_entry(pool
, bio
);
1245 cell_defer_no_holder(tc
, cell
);
1246 cell_defer_no_holder(tc
, cell2
);
1249 * The DM core makes sure that the discard doesn't span
1250 * a block boundary. So we submit the discard of a
1251 * partial block appropriately.
1253 if ((!lookup_result
.shared
) && pool
->pf
.discard_passdown
)
1254 remap_and_issue(tc
, bio
, lookup_result
.block
);
1262 * It isn't provisioned, just forget it.
1264 cell_defer_no_holder(tc
, cell
);
1269 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1271 cell_defer_no_holder(tc
, cell
);
1277 static void break_sharing(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1278 struct dm_cell_key
*key
,
1279 struct dm_thin_lookup_result
*lookup_result
,
1280 struct dm_bio_prison_cell
*cell
)
1283 dm_block_t data_block
;
1284 struct pool
*pool
= tc
->pool
;
1286 r
= alloc_data_block(tc
, &data_block
);
1289 schedule_internal_copy(tc
, block
, lookup_result
->block
,
1290 data_block
, cell
, bio
);
1294 retry_bios_on_resume(pool
, cell
);
1298 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1300 cell_error(pool
, cell
);
1305 static void process_shared_bio(struct thin_c
*tc
, struct bio
*bio
,
1307 struct dm_thin_lookup_result
*lookup_result
)
1309 struct dm_bio_prison_cell
*cell
;
1310 struct pool
*pool
= tc
->pool
;
1311 struct dm_cell_key key
;
1314 * If cell is already occupied, then sharing is already in the process
1315 * of being broken so we have nothing further to do here.
1317 build_data_key(tc
->td
, lookup_result
->block
, &key
);
1318 if (bio_detain(pool
, &key
, bio
, &cell
))
1321 if (bio_data_dir(bio
) == WRITE
&& bio
->bi_iter
.bi_size
)
1322 break_sharing(tc
, bio
, block
, &key
, lookup_result
, cell
);
1324 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1326 h
->shared_read_entry
= dm_deferred_entry_inc(pool
->shared_read_ds
);
1327 inc_all_io_entry(pool
, bio
);
1328 cell_defer_no_holder(tc
, cell
);
1330 remap_and_issue(tc
, bio
, lookup_result
->block
);
1334 static void provision_block(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1335 struct dm_bio_prison_cell
*cell
)
1338 dm_block_t data_block
;
1339 struct pool
*pool
= tc
->pool
;
1342 * Remap empty bios (flushes) immediately, without provisioning.
1344 if (!bio
->bi_iter
.bi_size
) {
1345 inc_all_io_entry(pool
, bio
);
1346 cell_defer_no_holder(tc
, cell
);
1348 remap_and_issue(tc
, bio
, 0);
1353 * Fill read bios with zeroes and complete them immediately.
1355 if (bio_data_dir(bio
) == READ
) {
1357 cell_defer_no_holder(tc
, cell
);
1362 r
= alloc_data_block(tc
, &data_block
);
1366 schedule_external_copy(tc
, block
, data_block
, cell
, bio
);
1368 schedule_zero(tc
, block
, data_block
, cell
, bio
);
1372 retry_bios_on_resume(pool
, cell
);
1376 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1378 cell_error(pool
, cell
);
1383 static void process_bio(struct thin_c
*tc
, struct bio
*bio
)
1386 struct pool
*pool
= tc
->pool
;
1387 dm_block_t block
= get_bio_block(tc
, bio
);
1388 struct dm_bio_prison_cell
*cell
;
1389 struct dm_cell_key key
;
1390 struct dm_thin_lookup_result lookup_result
;
1393 * If cell is already occupied, then the block is already
1394 * being provisioned so we have nothing further to do here.
1396 build_virtual_key(tc
->td
, block
, &key
);
1397 if (bio_detain(pool
, &key
, bio
, &cell
))
1400 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1403 if (lookup_result
.shared
) {
1404 process_shared_bio(tc
, bio
, block
, &lookup_result
);
1405 cell_defer_no_holder(tc
, cell
); /* FIXME: pass this cell into process_shared? */
1407 inc_all_io_entry(pool
, bio
);
1408 cell_defer_no_holder(tc
, cell
);
1410 remap_and_issue(tc
, bio
, lookup_result
.block
);
1415 if (bio_data_dir(bio
) == READ
&& tc
->origin_dev
) {
1416 inc_all_io_entry(pool
, bio
);
1417 cell_defer_no_holder(tc
, cell
);
1419 if (bio_end_sector(bio
) <= tc
->origin_size
)
1420 remap_to_origin_and_issue(tc
, bio
);
1422 else if (bio
->bi_iter
.bi_sector
< tc
->origin_size
) {
1424 bio
->bi_iter
.bi_size
= (tc
->origin_size
- bio
->bi_iter
.bi_sector
) << SECTOR_SHIFT
;
1425 remap_to_origin_and_issue(tc
, bio
);
1432 provision_block(tc
, bio
, block
, cell
);
1436 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1438 cell_defer_no_holder(tc
, cell
);
1444 static void process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
)
1447 int rw
= bio_data_dir(bio
);
1448 dm_block_t block
= get_bio_block(tc
, bio
);
1449 struct dm_thin_lookup_result lookup_result
;
1451 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1454 if (lookup_result
.shared
&& (rw
== WRITE
) && bio
->bi_iter
.bi_size
)
1455 handle_unserviceable_bio(tc
->pool
, bio
);
1457 inc_all_io_entry(tc
->pool
, bio
);
1458 remap_and_issue(tc
, bio
, lookup_result
.block
);
1464 handle_unserviceable_bio(tc
->pool
, bio
);
1468 if (tc
->origin_dev
) {
1469 inc_all_io_entry(tc
->pool
, bio
);
1470 remap_to_origin_and_issue(tc
, bio
);
1479 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1486 static void process_bio_success(struct thin_c
*tc
, struct bio
*bio
)
1491 static void process_bio_fail(struct thin_c
*tc
, struct bio
*bio
)
1497 * FIXME: should we also commit due to size of transaction, measured in
1500 static int need_commit_due_to_time(struct pool
*pool
)
1502 return jiffies
< pool
->last_commit_jiffies
||
1503 jiffies
> pool
->last_commit_jiffies
+ COMMIT_PERIOD
;
1506 #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1507 #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1509 static void __thin_bio_rb_add(struct thin_c
*tc
, struct bio
*bio
)
1511 struct rb_node
**rbp
, *parent
;
1512 struct dm_thin_endio_hook
*pbd
;
1513 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
1515 rbp
= &tc
->sort_bio_list
.rb_node
;
1519 pbd
= thin_pbd(parent
);
1521 if (bi_sector
< thin_bio(pbd
)->bi_iter
.bi_sector
)
1522 rbp
= &(*rbp
)->rb_left
;
1524 rbp
= &(*rbp
)->rb_right
;
1527 pbd
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1528 rb_link_node(&pbd
->rb_node
, parent
, rbp
);
1529 rb_insert_color(&pbd
->rb_node
, &tc
->sort_bio_list
);
1532 static void __extract_sorted_bios(struct thin_c
*tc
)
1534 struct rb_node
*node
;
1535 struct dm_thin_endio_hook
*pbd
;
1538 for (node
= rb_first(&tc
->sort_bio_list
); node
; node
= rb_next(node
)) {
1539 pbd
= thin_pbd(node
);
1540 bio
= thin_bio(pbd
);
1542 bio_list_add(&tc
->deferred_bio_list
, bio
);
1543 rb_erase(&pbd
->rb_node
, &tc
->sort_bio_list
);
1546 WARN_ON(!RB_EMPTY_ROOT(&tc
->sort_bio_list
));
1549 static void __sort_thin_deferred_bios(struct thin_c
*tc
)
1552 struct bio_list bios
;
1554 bio_list_init(&bios
);
1555 bio_list_merge(&bios
, &tc
->deferred_bio_list
);
1556 bio_list_init(&tc
->deferred_bio_list
);
1558 /* Sort deferred_bio_list using rb-tree */
1559 while ((bio
= bio_list_pop(&bios
)))
1560 __thin_bio_rb_add(tc
, bio
);
1563 * Transfer the sorted bios in sort_bio_list back to
1564 * deferred_bio_list to allow lockless submission of
1567 __extract_sorted_bios(tc
);
1570 static void process_thin_deferred_bios(struct thin_c
*tc
)
1572 struct pool
*pool
= tc
->pool
;
1573 unsigned long flags
;
1575 struct bio_list bios
;
1576 struct blk_plug plug
;
1579 if (tc
->requeue_mode
) {
1580 requeue_bio_list(tc
, &tc
->deferred_bio_list
);
1584 bio_list_init(&bios
);
1586 spin_lock_irqsave(&tc
->lock
, flags
);
1588 if (bio_list_empty(&tc
->deferred_bio_list
)) {
1589 spin_unlock_irqrestore(&tc
->lock
, flags
);
1593 __sort_thin_deferred_bios(tc
);
1595 bio_list_merge(&bios
, &tc
->deferred_bio_list
);
1596 bio_list_init(&tc
->deferred_bio_list
);
1598 spin_unlock_irqrestore(&tc
->lock
, flags
);
1600 blk_start_plug(&plug
);
1601 while ((bio
= bio_list_pop(&bios
))) {
1603 * If we've got no free new_mapping structs, and processing
1604 * this bio might require one, we pause until there are some
1605 * prepared mappings to process.
1607 if (ensure_next_mapping(pool
)) {
1608 spin_lock_irqsave(&tc
->lock
, flags
);
1609 bio_list_add(&tc
->deferred_bio_list
, bio
);
1610 bio_list_merge(&tc
->deferred_bio_list
, &bios
);
1611 spin_unlock_irqrestore(&tc
->lock
, flags
);
1615 if (bio
->bi_rw
& REQ_DISCARD
)
1616 pool
->process_discard(tc
, bio
);
1618 pool
->process_bio(tc
, bio
);
1620 if ((count
++ & 127) == 0) {
1621 throttle_work_update(&pool
->throttle
);
1622 dm_pool_issue_prefetches(pool
->pmd
);
1625 blk_finish_plug(&plug
);
1628 static void thin_get(struct thin_c
*tc
);
1629 static void thin_put(struct thin_c
*tc
);
1632 * We can't hold rcu_read_lock() around code that can block. So we
1633 * find a thin with the rcu lock held; bump a refcount; then drop
1636 static struct thin_c
*get_first_thin(struct pool
*pool
)
1638 struct thin_c
*tc
= NULL
;
1641 if (!list_empty(&pool
->active_thins
)) {
1642 tc
= list_entry_rcu(pool
->active_thins
.next
, struct thin_c
, list
);
1650 static struct thin_c
*get_next_thin(struct pool
*pool
, struct thin_c
*tc
)
1652 struct thin_c
*old_tc
= tc
;
1655 list_for_each_entry_continue_rcu(tc
, &pool
->active_thins
, list
) {
1667 static void process_deferred_bios(struct pool
*pool
)
1669 unsigned long flags
;
1671 struct bio_list bios
;
1674 tc
= get_first_thin(pool
);
1676 process_thin_deferred_bios(tc
);
1677 tc
= get_next_thin(pool
, tc
);
1681 * If there are any deferred flush bios, we must commit
1682 * the metadata before issuing them.
1684 bio_list_init(&bios
);
1685 spin_lock_irqsave(&pool
->lock
, flags
);
1686 bio_list_merge(&bios
, &pool
->deferred_flush_bios
);
1687 bio_list_init(&pool
->deferred_flush_bios
);
1688 spin_unlock_irqrestore(&pool
->lock
, flags
);
1690 if (bio_list_empty(&bios
) &&
1691 !(dm_pool_changed_this_transaction(pool
->pmd
) && need_commit_due_to_time(pool
)))
1695 while ((bio
= bio_list_pop(&bios
)))
1699 pool
->last_commit_jiffies
= jiffies
;
1701 while ((bio
= bio_list_pop(&bios
)))
1702 generic_make_request(bio
);
1705 static void do_worker(struct work_struct
*ws
)
1707 struct pool
*pool
= container_of(ws
, struct pool
, worker
);
1709 throttle_work_start(&pool
->throttle
);
1710 dm_pool_issue_prefetches(pool
->pmd
);
1711 throttle_work_update(&pool
->throttle
);
1712 process_prepared(pool
, &pool
->prepared_mappings
, &pool
->process_prepared_mapping
);
1713 throttle_work_update(&pool
->throttle
);
1714 process_prepared(pool
, &pool
->prepared_discards
, &pool
->process_prepared_discard
);
1715 throttle_work_update(&pool
->throttle
);
1716 process_deferred_bios(pool
);
1717 throttle_work_complete(&pool
->throttle
);
1721 * We want to commit periodically so that not too much
1722 * unwritten data builds up.
1724 static void do_waker(struct work_struct
*ws
)
1726 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
, waker
);
1728 queue_delayed_work(pool
->wq
, &pool
->waker
, COMMIT_PERIOD
);
1732 * We're holding onto IO to allow userland time to react. After the
1733 * timeout either the pool will have been resized (and thus back in
1734 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
1736 static void do_no_space_timeout(struct work_struct
*ws
)
1738 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
,
1741 if (get_pool_mode(pool
) == PM_OUT_OF_DATA_SPACE
&& !pool
->pf
.error_if_no_space
)
1742 set_pool_mode(pool
, PM_READ_ONLY
);
1745 /*----------------------------------------------------------------*/
1748 struct work_struct worker
;
1749 struct completion complete
;
1752 static struct pool_work
*to_pool_work(struct work_struct
*ws
)
1754 return container_of(ws
, struct pool_work
, worker
);
1757 static void pool_work_complete(struct pool_work
*pw
)
1759 complete(&pw
->complete
);
1762 static void pool_work_wait(struct pool_work
*pw
, struct pool
*pool
,
1763 void (*fn
)(struct work_struct
*))
1765 INIT_WORK_ONSTACK(&pw
->worker
, fn
);
1766 init_completion(&pw
->complete
);
1767 queue_work(pool
->wq
, &pw
->worker
);
1768 wait_for_completion(&pw
->complete
);
1771 /*----------------------------------------------------------------*/
1773 struct noflush_work
{
1774 struct pool_work pw
;
1778 static struct noflush_work
*to_noflush(struct work_struct
*ws
)
1780 return container_of(to_pool_work(ws
), struct noflush_work
, pw
);
1783 static void do_noflush_start(struct work_struct
*ws
)
1785 struct noflush_work
*w
= to_noflush(ws
);
1786 w
->tc
->requeue_mode
= true;
1788 pool_work_complete(&w
->pw
);
1791 static void do_noflush_stop(struct work_struct
*ws
)
1793 struct noflush_work
*w
= to_noflush(ws
);
1794 w
->tc
->requeue_mode
= false;
1795 pool_work_complete(&w
->pw
);
1798 static void noflush_work(struct thin_c
*tc
, void (*fn
)(struct work_struct
*))
1800 struct noflush_work w
;
1803 pool_work_wait(&w
.pw
, tc
->pool
, fn
);
1806 /*----------------------------------------------------------------*/
1808 static enum pool_mode
get_pool_mode(struct pool
*pool
)
1810 return pool
->pf
.mode
;
1813 static void notify_of_pool_mode_change(struct pool
*pool
, const char *new_mode
)
1815 dm_table_event(pool
->ti
->table
);
1816 DMINFO("%s: switching pool to %s mode",
1817 dm_device_name(pool
->pool_md
), new_mode
);
1820 static void set_pool_mode(struct pool
*pool
, enum pool_mode new_mode
)
1822 struct pool_c
*pt
= pool
->ti
->private;
1823 bool needs_check
= dm_pool_metadata_needs_check(pool
->pmd
);
1824 enum pool_mode old_mode
= get_pool_mode(pool
);
1825 unsigned long no_space_timeout
= ACCESS_ONCE(no_space_timeout_secs
) * HZ
;
1828 * Never allow the pool to transition to PM_WRITE mode if user
1829 * intervention is required to verify metadata and data consistency.
1831 if (new_mode
== PM_WRITE
&& needs_check
) {
1832 DMERR("%s: unable to switch pool to write mode until repaired.",
1833 dm_device_name(pool
->pool_md
));
1834 if (old_mode
!= new_mode
)
1835 new_mode
= old_mode
;
1837 new_mode
= PM_READ_ONLY
;
1840 * If we were in PM_FAIL mode, rollback of metadata failed. We're
1841 * not going to recover without a thin_repair. So we never let the
1842 * pool move out of the old mode.
1844 if (old_mode
== PM_FAIL
)
1845 new_mode
= old_mode
;
1849 if (old_mode
!= new_mode
)
1850 notify_of_pool_mode_change(pool
, "failure");
1851 dm_pool_metadata_read_only(pool
->pmd
);
1852 pool
->process_bio
= process_bio_fail
;
1853 pool
->process_discard
= process_bio_fail
;
1854 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1855 pool
->process_prepared_discard
= process_prepared_discard_fail
;
1857 error_retry_list(pool
);
1861 if (old_mode
!= new_mode
)
1862 notify_of_pool_mode_change(pool
, "read-only");
1863 dm_pool_metadata_read_only(pool
->pmd
);
1864 pool
->process_bio
= process_bio_read_only
;
1865 pool
->process_discard
= process_bio_success
;
1866 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1867 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1869 error_retry_list(pool
);
1872 case PM_OUT_OF_DATA_SPACE
:
1874 * Ideally we'd never hit this state; the low water mark
1875 * would trigger userland to extend the pool before we
1876 * completely run out of data space. However, many small
1877 * IOs to unprovisioned space can consume data space at an
1878 * alarming rate. Adjust your low water mark if you're
1879 * frequently seeing this mode.
1881 if (old_mode
!= new_mode
)
1882 notify_of_pool_mode_change(pool
, "out-of-data-space");
1883 pool
->process_bio
= process_bio_read_only
;
1884 pool
->process_discard
= process_discard
;
1885 pool
->process_prepared_mapping
= process_prepared_mapping
;
1886 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1888 if (!pool
->pf
.error_if_no_space
&& no_space_timeout
)
1889 queue_delayed_work(pool
->wq
, &pool
->no_space_timeout
, no_space_timeout
);
1893 if (old_mode
!= new_mode
)
1894 notify_of_pool_mode_change(pool
, "write");
1895 dm_pool_metadata_read_write(pool
->pmd
);
1896 pool
->process_bio
= process_bio
;
1897 pool
->process_discard
= process_discard
;
1898 pool
->process_prepared_mapping
= process_prepared_mapping
;
1899 pool
->process_prepared_discard
= process_prepared_discard
;
1903 pool
->pf
.mode
= new_mode
;
1905 * The pool mode may have changed, sync it so bind_control_target()
1906 * doesn't cause an unexpected mode transition on resume.
1908 pt
->adjusted_pf
.mode
= new_mode
;
1911 static void abort_transaction(struct pool
*pool
)
1913 const char *dev_name
= dm_device_name(pool
->pool_md
);
1915 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name
);
1916 if (dm_pool_abort_metadata(pool
->pmd
)) {
1917 DMERR("%s: failed to abort metadata transaction", dev_name
);
1918 set_pool_mode(pool
, PM_FAIL
);
1921 if (dm_pool_metadata_set_needs_check(pool
->pmd
)) {
1922 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name
);
1923 set_pool_mode(pool
, PM_FAIL
);
1927 static void metadata_operation_failed(struct pool
*pool
, const char *op
, int r
)
1929 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1930 dm_device_name(pool
->pool_md
), op
, r
);
1932 abort_transaction(pool
);
1933 set_pool_mode(pool
, PM_READ_ONLY
);
1936 /*----------------------------------------------------------------*/
1939 * Mapping functions.
1943 * Called only while mapping a thin bio to hand it over to the workqueue.
1945 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
)
1947 unsigned long flags
;
1948 struct pool
*pool
= tc
->pool
;
1950 spin_lock_irqsave(&tc
->lock
, flags
);
1951 bio_list_add(&tc
->deferred_bio_list
, bio
);
1952 spin_unlock_irqrestore(&tc
->lock
, flags
);
1957 static void thin_defer_bio_with_throttle(struct thin_c
*tc
, struct bio
*bio
)
1959 struct pool
*pool
= tc
->pool
;
1961 throttle_lock(&pool
->throttle
);
1962 thin_defer_bio(tc
, bio
);
1963 throttle_unlock(&pool
->throttle
);
1966 static void thin_hook_bio(struct thin_c
*tc
, struct bio
*bio
)
1968 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1971 h
->shared_read_entry
= NULL
;
1972 h
->all_io_entry
= NULL
;
1973 h
->overwrite_mapping
= NULL
;
1977 * Non-blocking function called from the thin target's map function.
1979 static int thin_bio_map(struct dm_target
*ti
, struct bio
*bio
)
1982 struct thin_c
*tc
= ti
->private;
1983 dm_block_t block
= get_bio_block(tc
, bio
);
1984 struct dm_thin_device
*td
= tc
->td
;
1985 struct dm_thin_lookup_result result
;
1986 struct dm_bio_prison_cell cell1
, cell2
;
1987 struct dm_bio_prison_cell
*cell_result
;
1988 struct dm_cell_key key
;
1990 thin_hook_bio(tc
, bio
);
1992 if (tc
->requeue_mode
) {
1993 bio_endio(bio
, DM_ENDIO_REQUEUE
);
1994 return DM_MAPIO_SUBMITTED
;
1997 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
1999 return DM_MAPIO_SUBMITTED
;
2002 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)) {
2003 thin_defer_bio_with_throttle(tc
, bio
);
2004 return DM_MAPIO_SUBMITTED
;
2008 * We must hold the virtual cell before doing the lookup, otherwise
2009 * there's a race with discard.
2011 build_virtual_key(tc
->td
, block
, &key
);
2012 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell1
, &cell_result
))
2013 return DM_MAPIO_SUBMITTED
;
2015 r
= dm_thin_find_block(td
, block
, 0, &result
);
2018 * Note that we defer readahead too.
2022 if (unlikely(result
.shared
)) {
2024 * We have a race condition here between the
2025 * result.shared value returned by the lookup and
2026 * snapshot creation, which may cause new
2029 * To avoid this always quiesce the origin before
2030 * taking the snap. You want to do this anyway to
2031 * ensure a consistent application view
2034 * More distant ancestors are irrelevant. The
2035 * shared flag will be set in their case.
2037 thin_defer_bio(tc
, bio
);
2038 cell_defer_no_holder_no_free(tc
, &cell1
);
2039 return DM_MAPIO_SUBMITTED
;
2042 build_data_key(tc
->td
, result
.block
, &key
);
2043 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell2
, &cell_result
)) {
2044 cell_defer_no_holder_no_free(tc
, &cell1
);
2045 return DM_MAPIO_SUBMITTED
;
2048 inc_all_io_entry(tc
->pool
, bio
);
2049 cell_defer_no_holder_no_free(tc
, &cell2
);
2050 cell_defer_no_holder_no_free(tc
, &cell1
);
2052 remap(tc
, bio
, result
.block
);
2053 return DM_MAPIO_REMAPPED
;
2056 if (get_pool_mode(tc
->pool
) == PM_READ_ONLY
) {
2058 * This block isn't provisioned, and we have no way
2061 handle_unserviceable_bio(tc
->pool
, bio
);
2062 cell_defer_no_holder_no_free(tc
, &cell1
);
2063 return DM_MAPIO_SUBMITTED
;
2068 thin_defer_bio(tc
, bio
);
2069 cell_defer_no_holder_no_free(tc
, &cell1
);
2070 return DM_MAPIO_SUBMITTED
;
2074 * Must always call bio_io_error on failure.
2075 * dm_thin_find_block can fail with -EINVAL if the
2076 * pool is switched to fail-io mode.
2079 cell_defer_no_holder_no_free(tc
, &cell1
);
2080 return DM_MAPIO_SUBMITTED
;
2084 static int pool_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
2086 struct pool_c
*pt
= container_of(cb
, struct pool_c
, callbacks
);
2087 struct request_queue
*q
;
2089 if (get_pool_mode(pt
->pool
) == PM_OUT_OF_DATA_SPACE
)
2092 q
= bdev_get_queue(pt
->data_dev
->bdev
);
2093 return bdi_congested(&q
->backing_dev_info
, bdi_bits
);
2096 static void requeue_bios(struct pool
*pool
)
2098 unsigned long flags
;
2102 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
) {
2103 spin_lock_irqsave(&tc
->lock
, flags
);
2104 bio_list_merge(&tc
->deferred_bio_list
, &tc
->retry_on_resume_list
);
2105 bio_list_init(&tc
->retry_on_resume_list
);
2106 spin_unlock_irqrestore(&tc
->lock
, flags
);
2111 /*----------------------------------------------------------------
2112 * Binding of control targets to a pool object
2113 *--------------------------------------------------------------*/
2114 static bool data_dev_supports_discard(struct pool_c
*pt
)
2116 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
2118 return q
&& blk_queue_discard(q
);
2121 static bool is_factor(sector_t block_size
, uint32_t n
)
2123 return !sector_div(block_size
, n
);
2127 * If discard_passdown was enabled verify that the data device
2128 * supports discards. Disable discard_passdown if not.
2130 static void disable_passdown_if_not_supported(struct pool_c
*pt
)
2132 struct pool
*pool
= pt
->pool
;
2133 struct block_device
*data_bdev
= pt
->data_dev
->bdev
;
2134 struct queue_limits
*data_limits
= &bdev_get_queue(data_bdev
)->limits
;
2135 sector_t block_size
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
2136 const char *reason
= NULL
;
2137 char buf
[BDEVNAME_SIZE
];
2139 if (!pt
->adjusted_pf
.discard_passdown
)
2142 if (!data_dev_supports_discard(pt
))
2143 reason
= "discard unsupported";
2145 else if (data_limits
->max_discard_sectors
< pool
->sectors_per_block
)
2146 reason
= "max discard sectors smaller than a block";
2148 else if (data_limits
->discard_granularity
> block_size
)
2149 reason
= "discard granularity larger than a block";
2151 else if (!is_factor(block_size
, data_limits
->discard_granularity
))
2152 reason
= "discard granularity not a factor of block size";
2155 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev
, buf
), reason
);
2156 pt
->adjusted_pf
.discard_passdown
= false;
2160 static int bind_control_target(struct pool
*pool
, struct dm_target
*ti
)
2162 struct pool_c
*pt
= ti
->private;
2165 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
2167 enum pool_mode old_mode
= get_pool_mode(pool
);
2168 enum pool_mode new_mode
= pt
->adjusted_pf
.mode
;
2171 * Don't change the pool's mode until set_pool_mode() below.
2172 * Otherwise the pool's process_* function pointers may
2173 * not match the desired pool mode.
2175 pt
->adjusted_pf
.mode
= old_mode
;
2178 pool
->pf
= pt
->adjusted_pf
;
2179 pool
->low_water_blocks
= pt
->low_water_blocks
;
2181 set_pool_mode(pool
, new_mode
);
2186 static void unbind_control_target(struct pool
*pool
, struct dm_target
*ti
)
2192 /*----------------------------------------------------------------
2194 *--------------------------------------------------------------*/
2195 /* Initialize pool features. */
2196 static void pool_features_init(struct pool_features
*pf
)
2198 pf
->mode
= PM_WRITE
;
2199 pf
->zero_new_blocks
= true;
2200 pf
->discard_enabled
= true;
2201 pf
->discard_passdown
= true;
2202 pf
->error_if_no_space
= false;
2205 static void __pool_destroy(struct pool
*pool
)
2207 __pool_table_remove(pool
);
2209 if (dm_pool_metadata_close(pool
->pmd
) < 0)
2210 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
2212 dm_bio_prison_destroy(pool
->prison
);
2213 dm_kcopyd_client_destroy(pool
->copier
);
2216 destroy_workqueue(pool
->wq
);
2218 if (pool
->next_mapping
)
2219 mempool_free(pool
->next_mapping
, pool
->mapping_pool
);
2220 mempool_destroy(pool
->mapping_pool
);
2221 dm_deferred_set_destroy(pool
->shared_read_ds
);
2222 dm_deferred_set_destroy(pool
->all_io_ds
);
2226 static struct kmem_cache
*_new_mapping_cache
;
2228 static struct pool
*pool_create(struct mapped_device
*pool_md
,
2229 struct block_device
*metadata_dev
,
2230 unsigned long block_size
,
2231 int read_only
, char **error
)
2236 struct dm_pool_metadata
*pmd
;
2237 bool format_device
= read_only
? false : true;
2239 pmd
= dm_pool_metadata_open(metadata_dev
, block_size
, format_device
);
2241 *error
= "Error creating metadata object";
2242 return (struct pool
*)pmd
;
2245 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
2247 *error
= "Error allocating memory for pool";
2248 err_p
= ERR_PTR(-ENOMEM
);
2253 pool
->sectors_per_block
= block_size
;
2254 if (block_size
& (block_size
- 1))
2255 pool
->sectors_per_block_shift
= -1;
2257 pool
->sectors_per_block_shift
= __ffs(block_size
);
2258 pool
->low_water_blocks
= 0;
2259 pool_features_init(&pool
->pf
);
2260 pool
->prison
= dm_bio_prison_create();
2261 if (!pool
->prison
) {
2262 *error
= "Error creating pool's bio prison";
2263 err_p
= ERR_PTR(-ENOMEM
);
2267 pool
->copier
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2268 if (IS_ERR(pool
->copier
)) {
2269 r
= PTR_ERR(pool
->copier
);
2270 *error
= "Error creating pool's kcopyd client";
2272 goto bad_kcopyd_client
;
2276 * Create singlethreaded workqueue that will service all devices
2277 * that use this metadata.
2279 pool
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
2281 *error
= "Error creating pool's workqueue";
2282 err_p
= ERR_PTR(-ENOMEM
);
2286 throttle_init(&pool
->throttle
);
2287 INIT_WORK(&pool
->worker
, do_worker
);
2288 INIT_DELAYED_WORK(&pool
->waker
, do_waker
);
2289 INIT_DELAYED_WORK(&pool
->no_space_timeout
, do_no_space_timeout
);
2290 spin_lock_init(&pool
->lock
);
2291 bio_list_init(&pool
->deferred_flush_bios
);
2292 INIT_LIST_HEAD(&pool
->prepared_mappings
);
2293 INIT_LIST_HEAD(&pool
->prepared_discards
);
2294 INIT_LIST_HEAD(&pool
->active_thins
);
2295 pool
->low_water_triggered
= false;
2297 pool
->shared_read_ds
= dm_deferred_set_create();
2298 if (!pool
->shared_read_ds
) {
2299 *error
= "Error creating pool's shared read deferred set";
2300 err_p
= ERR_PTR(-ENOMEM
);
2301 goto bad_shared_read_ds
;
2304 pool
->all_io_ds
= dm_deferred_set_create();
2305 if (!pool
->all_io_ds
) {
2306 *error
= "Error creating pool's all io deferred set";
2307 err_p
= ERR_PTR(-ENOMEM
);
2311 pool
->next_mapping
= NULL
;
2312 pool
->mapping_pool
= mempool_create_slab_pool(MAPPING_POOL_SIZE
,
2313 _new_mapping_cache
);
2314 if (!pool
->mapping_pool
) {
2315 *error
= "Error creating pool's mapping mempool";
2316 err_p
= ERR_PTR(-ENOMEM
);
2317 goto bad_mapping_pool
;
2320 pool
->ref_count
= 1;
2321 pool
->last_commit_jiffies
= jiffies
;
2322 pool
->pool_md
= pool_md
;
2323 pool
->md_dev
= metadata_dev
;
2324 __pool_table_insert(pool
);
2329 dm_deferred_set_destroy(pool
->all_io_ds
);
2331 dm_deferred_set_destroy(pool
->shared_read_ds
);
2333 destroy_workqueue(pool
->wq
);
2335 dm_kcopyd_client_destroy(pool
->copier
);
2337 dm_bio_prison_destroy(pool
->prison
);
2341 if (dm_pool_metadata_close(pmd
))
2342 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
2347 static void __pool_inc(struct pool
*pool
)
2349 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2353 static void __pool_dec(struct pool
*pool
)
2355 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2356 BUG_ON(!pool
->ref_count
);
2357 if (!--pool
->ref_count
)
2358 __pool_destroy(pool
);
2361 static struct pool
*__pool_find(struct mapped_device
*pool_md
,
2362 struct block_device
*metadata_dev
,
2363 unsigned long block_size
, int read_only
,
2364 char **error
, int *created
)
2366 struct pool
*pool
= __pool_table_lookup_metadata_dev(metadata_dev
);
2369 if (pool
->pool_md
!= pool_md
) {
2370 *error
= "metadata device already in use by a pool";
2371 return ERR_PTR(-EBUSY
);
2376 pool
= __pool_table_lookup(pool_md
);
2378 if (pool
->md_dev
!= metadata_dev
) {
2379 *error
= "different pool cannot replace a pool";
2380 return ERR_PTR(-EINVAL
);
2385 pool
= pool_create(pool_md
, metadata_dev
, block_size
, read_only
, error
);
2393 /*----------------------------------------------------------------
2394 * Pool target methods
2395 *--------------------------------------------------------------*/
2396 static void pool_dtr(struct dm_target
*ti
)
2398 struct pool_c
*pt
= ti
->private;
2400 mutex_lock(&dm_thin_pool_table
.mutex
);
2402 unbind_control_target(pt
->pool
, ti
);
2403 __pool_dec(pt
->pool
);
2404 dm_put_device(ti
, pt
->metadata_dev
);
2405 dm_put_device(ti
, pt
->data_dev
);
2408 mutex_unlock(&dm_thin_pool_table
.mutex
);
2411 static int parse_pool_features(struct dm_arg_set
*as
, struct pool_features
*pf
,
2412 struct dm_target
*ti
)
2416 const char *arg_name
;
2418 static struct dm_arg _args
[] = {
2419 {0, 4, "Invalid number of pool feature arguments"},
2423 * No feature arguments supplied.
2428 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
2432 while (argc
&& !r
) {
2433 arg_name
= dm_shift_arg(as
);
2436 if (!strcasecmp(arg_name
, "skip_block_zeroing"))
2437 pf
->zero_new_blocks
= false;
2439 else if (!strcasecmp(arg_name
, "ignore_discard"))
2440 pf
->discard_enabled
= false;
2442 else if (!strcasecmp(arg_name
, "no_discard_passdown"))
2443 pf
->discard_passdown
= false;
2445 else if (!strcasecmp(arg_name
, "read_only"))
2446 pf
->mode
= PM_READ_ONLY
;
2448 else if (!strcasecmp(arg_name
, "error_if_no_space"))
2449 pf
->error_if_no_space
= true;
2452 ti
->error
= "Unrecognised pool feature requested";
2461 static void metadata_low_callback(void *context
)
2463 struct pool
*pool
= context
;
2465 DMWARN("%s: reached low water mark for metadata device: sending event.",
2466 dm_device_name(pool
->pool_md
));
2468 dm_table_event(pool
->ti
->table
);
2471 static sector_t
get_dev_size(struct block_device
*bdev
)
2473 return i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
;
2476 static void warn_if_metadata_device_too_big(struct block_device
*bdev
)
2478 sector_t metadata_dev_size
= get_dev_size(bdev
);
2479 char buffer
[BDEVNAME_SIZE
];
2481 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS_WARNING
)
2482 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2483 bdevname(bdev
, buffer
), THIN_METADATA_MAX_SECTORS
);
2486 static sector_t
get_metadata_dev_size(struct block_device
*bdev
)
2488 sector_t metadata_dev_size
= get_dev_size(bdev
);
2490 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS
)
2491 metadata_dev_size
= THIN_METADATA_MAX_SECTORS
;
2493 return metadata_dev_size
;
2496 static dm_block_t
get_metadata_dev_size_in_blocks(struct block_device
*bdev
)
2498 sector_t metadata_dev_size
= get_metadata_dev_size(bdev
);
2500 sector_div(metadata_dev_size
, THIN_METADATA_BLOCK_SIZE
);
2502 return metadata_dev_size
;
2506 * When a metadata threshold is crossed a dm event is triggered, and
2507 * userland should respond by growing the metadata device. We could let
2508 * userland set the threshold, like we do with the data threshold, but I'm
2509 * not sure they know enough to do this well.
2511 static dm_block_t
calc_metadata_threshold(struct pool_c
*pt
)
2514 * 4M is ample for all ops with the possible exception of thin
2515 * device deletion which is harmless if it fails (just retry the
2516 * delete after you've grown the device).
2518 dm_block_t quarter
= get_metadata_dev_size_in_blocks(pt
->metadata_dev
->bdev
) / 4;
2519 return min((dm_block_t
)1024ULL /* 4M */, quarter
);
2523 * thin-pool <metadata dev> <data dev>
2524 * <data block size (sectors)>
2525 * <low water mark (blocks)>
2526 * [<#feature args> [<arg>]*]
2528 * Optional feature arguments are:
2529 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
2530 * ignore_discard: disable discard
2531 * no_discard_passdown: don't pass discards down to the data device
2532 * read_only: Don't allow any changes to be made to the pool metadata.
2533 * error_if_no_space: error IOs, instead of queueing, if no space.
2535 static int pool_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2537 int r
, pool_created
= 0;
2540 struct pool_features pf
;
2541 struct dm_arg_set as
;
2542 struct dm_dev
*data_dev
;
2543 unsigned long block_size
;
2544 dm_block_t low_water_blocks
;
2545 struct dm_dev
*metadata_dev
;
2546 fmode_t metadata_mode
;
2549 * FIXME Remove validation from scope of lock.
2551 mutex_lock(&dm_thin_pool_table
.mutex
);
2554 ti
->error
= "Invalid argument count";
2563 * Set default pool features.
2565 pool_features_init(&pf
);
2567 dm_consume_args(&as
, 4);
2568 r
= parse_pool_features(&as
, &pf
, ti
);
2572 metadata_mode
= FMODE_READ
| ((pf
.mode
== PM_READ_ONLY
) ? 0 : FMODE_WRITE
);
2573 r
= dm_get_device(ti
, argv
[0], metadata_mode
, &metadata_dev
);
2575 ti
->error
= "Error opening metadata block device";
2578 warn_if_metadata_device_too_big(metadata_dev
->bdev
);
2580 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &data_dev
);
2582 ti
->error
= "Error getting data device";
2586 if (kstrtoul(argv
[2], 10, &block_size
) || !block_size
||
2587 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
2588 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
2589 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
2590 ti
->error
= "Invalid block size";
2595 if (kstrtoull(argv
[3], 10, (unsigned long long *)&low_water_blocks
)) {
2596 ti
->error
= "Invalid low water mark";
2601 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
2607 pool
= __pool_find(dm_table_get_md(ti
->table
), metadata_dev
->bdev
,
2608 block_size
, pf
.mode
== PM_READ_ONLY
, &ti
->error
, &pool_created
);
2615 * 'pool_created' reflects whether this is the first table load.
2616 * Top level discard support is not allowed to be changed after
2617 * initial load. This would require a pool reload to trigger thin
2620 if (!pool_created
&& pf
.discard_enabled
!= pool
->pf
.discard_enabled
) {
2621 ti
->error
= "Discard support cannot be disabled once enabled";
2623 goto out_flags_changed
;
2628 pt
->metadata_dev
= metadata_dev
;
2629 pt
->data_dev
= data_dev
;
2630 pt
->low_water_blocks
= low_water_blocks
;
2631 pt
->adjusted_pf
= pt
->requested_pf
= pf
;
2632 ti
->num_flush_bios
= 1;
2635 * Only need to enable discards if the pool should pass
2636 * them down to the data device. The thin device's discard
2637 * processing will cause mappings to be removed from the btree.
2639 ti
->discard_zeroes_data_unsupported
= true;
2640 if (pf
.discard_enabled
&& pf
.discard_passdown
) {
2641 ti
->num_discard_bios
= 1;
2644 * Setting 'discards_supported' circumvents the normal
2645 * stacking of discard limits (this keeps the pool and
2646 * thin devices' discard limits consistent).
2648 ti
->discards_supported
= true;
2652 r
= dm_pool_register_metadata_threshold(pt
->pool
->pmd
,
2653 calc_metadata_threshold(pt
),
2654 metadata_low_callback
,
2659 pt
->callbacks
.congested_fn
= pool_is_congested
;
2660 dm_table_add_target_callbacks(ti
->table
, &pt
->callbacks
);
2662 mutex_unlock(&dm_thin_pool_table
.mutex
);
2671 dm_put_device(ti
, data_dev
);
2673 dm_put_device(ti
, metadata_dev
);
2675 mutex_unlock(&dm_thin_pool_table
.mutex
);
2680 static int pool_map(struct dm_target
*ti
, struct bio
*bio
)
2683 struct pool_c
*pt
= ti
->private;
2684 struct pool
*pool
= pt
->pool
;
2685 unsigned long flags
;
2688 * As this is a singleton target, ti->begin is always zero.
2690 spin_lock_irqsave(&pool
->lock
, flags
);
2691 bio
->bi_bdev
= pt
->data_dev
->bdev
;
2692 r
= DM_MAPIO_REMAPPED
;
2693 spin_unlock_irqrestore(&pool
->lock
, flags
);
2698 static int maybe_resize_data_dev(struct dm_target
*ti
, bool *need_commit
)
2701 struct pool_c
*pt
= ti
->private;
2702 struct pool
*pool
= pt
->pool
;
2703 sector_t data_size
= ti
->len
;
2704 dm_block_t sb_data_size
;
2706 *need_commit
= false;
2708 (void) sector_div(data_size
, pool
->sectors_per_block
);
2710 r
= dm_pool_get_data_dev_size(pool
->pmd
, &sb_data_size
);
2712 DMERR("%s: failed to retrieve data device size",
2713 dm_device_name(pool
->pool_md
));
2717 if (data_size
< sb_data_size
) {
2718 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2719 dm_device_name(pool
->pool_md
),
2720 (unsigned long long)data_size
, sb_data_size
);
2723 } else if (data_size
> sb_data_size
) {
2724 if (dm_pool_metadata_needs_check(pool
->pmd
)) {
2725 DMERR("%s: unable to grow the data device until repaired.",
2726 dm_device_name(pool
->pool_md
));
2731 DMINFO("%s: growing the data device from %llu to %llu blocks",
2732 dm_device_name(pool
->pool_md
),
2733 sb_data_size
, (unsigned long long)data_size
);
2734 r
= dm_pool_resize_data_dev(pool
->pmd
, data_size
);
2736 metadata_operation_failed(pool
, "dm_pool_resize_data_dev", r
);
2740 *need_commit
= true;
2746 static int maybe_resize_metadata_dev(struct dm_target
*ti
, bool *need_commit
)
2749 struct pool_c
*pt
= ti
->private;
2750 struct pool
*pool
= pt
->pool
;
2751 dm_block_t metadata_dev_size
, sb_metadata_dev_size
;
2753 *need_commit
= false;
2755 metadata_dev_size
= get_metadata_dev_size_in_blocks(pool
->md_dev
);
2757 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &sb_metadata_dev_size
);
2759 DMERR("%s: failed to retrieve metadata device size",
2760 dm_device_name(pool
->pool_md
));
2764 if (metadata_dev_size
< sb_metadata_dev_size
) {
2765 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2766 dm_device_name(pool
->pool_md
),
2767 metadata_dev_size
, sb_metadata_dev_size
);
2770 } else if (metadata_dev_size
> sb_metadata_dev_size
) {
2771 if (dm_pool_metadata_needs_check(pool
->pmd
)) {
2772 DMERR("%s: unable to grow the metadata device until repaired.",
2773 dm_device_name(pool
->pool_md
));
2777 warn_if_metadata_device_too_big(pool
->md_dev
);
2778 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2779 dm_device_name(pool
->pool_md
),
2780 sb_metadata_dev_size
, metadata_dev_size
);
2781 r
= dm_pool_resize_metadata_dev(pool
->pmd
, metadata_dev_size
);
2783 metadata_operation_failed(pool
, "dm_pool_resize_metadata_dev", r
);
2787 *need_commit
= true;
2794 * Retrieves the number of blocks of the data device from
2795 * the superblock and compares it to the actual device size,
2796 * thus resizing the data device in case it has grown.
2798 * This both copes with opening preallocated data devices in the ctr
2799 * being followed by a resume
2801 * calling the resume method individually after userspace has
2802 * grown the data device in reaction to a table event.
2804 static int pool_preresume(struct dm_target
*ti
)
2807 bool need_commit1
, need_commit2
;
2808 struct pool_c
*pt
= ti
->private;
2809 struct pool
*pool
= pt
->pool
;
2812 * Take control of the pool object.
2814 r
= bind_control_target(pool
, ti
);
2818 r
= maybe_resize_data_dev(ti
, &need_commit1
);
2822 r
= maybe_resize_metadata_dev(ti
, &need_commit2
);
2826 if (need_commit1
|| need_commit2
)
2827 (void) commit(pool
);
2832 static void pool_resume(struct dm_target
*ti
)
2834 struct pool_c
*pt
= ti
->private;
2835 struct pool
*pool
= pt
->pool
;
2836 unsigned long flags
;
2838 spin_lock_irqsave(&pool
->lock
, flags
);
2839 pool
->low_water_triggered
= false;
2840 spin_unlock_irqrestore(&pool
->lock
, flags
);
2843 do_waker(&pool
->waker
.work
);
2846 static void pool_postsuspend(struct dm_target
*ti
)
2848 struct pool_c
*pt
= ti
->private;
2849 struct pool
*pool
= pt
->pool
;
2851 cancel_delayed_work(&pool
->waker
);
2852 cancel_delayed_work(&pool
->no_space_timeout
);
2853 flush_workqueue(pool
->wq
);
2854 (void) commit(pool
);
2857 static int check_arg_count(unsigned argc
, unsigned args_required
)
2859 if (argc
!= args_required
) {
2860 DMWARN("Message received with %u arguments instead of %u.",
2861 argc
, args_required
);
2868 static int read_dev_id(char *arg
, dm_thin_id
*dev_id
, int warning
)
2870 if (!kstrtoull(arg
, 10, (unsigned long long *)dev_id
) &&
2871 *dev_id
<= MAX_DEV_ID
)
2875 DMWARN("Message received with invalid device id: %s", arg
);
2880 static int process_create_thin_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2885 r
= check_arg_count(argc
, 2);
2889 r
= read_dev_id(argv
[1], &dev_id
, 1);
2893 r
= dm_pool_create_thin(pool
->pmd
, dev_id
);
2895 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2903 static int process_create_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2906 dm_thin_id origin_dev_id
;
2909 r
= check_arg_count(argc
, 3);
2913 r
= read_dev_id(argv
[1], &dev_id
, 1);
2917 r
= read_dev_id(argv
[2], &origin_dev_id
, 1);
2921 r
= dm_pool_create_snap(pool
->pmd
, dev_id
, origin_dev_id
);
2923 DMWARN("Creation of new snapshot %s of device %s failed.",
2931 static int process_delete_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2936 r
= check_arg_count(argc
, 2);
2940 r
= read_dev_id(argv
[1], &dev_id
, 1);
2944 r
= dm_pool_delete_thin_device(pool
->pmd
, dev_id
);
2946 DMWARN("Deletion of thin device %s failed.", argv
[1]);
2951 static int process_set_transaction_id_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2953 dm_thin_id old_id
, new_id
;
2956 r
= check_arg_count(argc
, 3);
2960 if (kstrtoull(argv
[1], 10, (unsigned long long *)&old_id
)) {
2961 DMWARN("set_transaction_id message: Unrecognised id %s.", argv
[1]);
2965 if (kstrtoull(argv
[2], 10, (unsigned long long *)&new_id
)) {
2966 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv
[2]);
2970 r
= dm_pool_set_metadata_transaction_id(pool
->pmd
, old_id
, new_id
);
2972 DMWARN("Failed to change transaction id from %s to %s.",
2980 static int process_reserve_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2984 r
= check_arg_count(argc
, 1);
2988 (void) commit(pool
);
2990 r
= dm_pool_reserve_metadata_snap(pool
->pmd
);
2992 DMWARN("reserve_metadata_snap message failed.");
2997 static int process_release_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
3001 r
= check_arg_count(argc
, 1);
3005 r
= dm_pool_release_metadata_snap(pool
->pmd
);
3007 DMWARN("release_metadata_snap message failed.");
3013 * Messages supported:
3014 * create_thin <dev_id>
3015 * create_snap <dev_id> <origin_id>
3017 * trim <dev_id> <new_size_in_sectors>
3018 * set_transaction_id <current_trans_id> <new_trans_id>
3019 * reserve_metadata_snap
3020 * release_metadata_snap
3022 static int pool_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
3025 struct pool_c
*pt
= ti
->private;
3026 struct pool
*pool
= pt
->pool
;
3028 if (!strcasecmp(argv
[0], "create_thin"))
3029 r
= process_create_thin_mesg(argc
, argv
, pool
);
3031 else if (!strcasecmp(argv
[0], "create_snap"))
3032 r
= process_create_snap_mesg(argc
, argv
, pool
);
3034 else if (!strcasecmp(argv
[0], "delete"))
3035 r
= process_delete_mesg(argc
, argv
, pool
);
3037 else if (!strcasecmp(argv
[0], "set_transaction_id"))
3038 r
= process_set_transaction_id_mesg(argc
, argv
, pool
);
3040 else if (!strcasecmp(argv
[0], "reserve_metadata_snap"))
3041 r
= process_reserve_metadata_snap_mesg(argc
, argv
, pool
);
3043 else if (!strcasecmp(argv
[0], "release_metadata_snap"))
3044 r
= process_release_metadata_snap_mesg(argc
, argv
, pool
);
3047 DMWARN("Unrecognised thin pool target message received: %s", argv
[0]);
3050 (void) commit(pool
);
3055 static void emit_flags(struct pool_features
*pf
, char *result
,
3056 unsigned sz
, unsigned maxlen
)
3058 unsigned count
= !pf
->zero_new_blocks
+ !pf
->discard_enabled
+
3059 !pf
->discard_passdown
+ (pf
->mode
== PM_READ_ONLY
) +
3060 pf
->error_if_no_space
;
3061 DMEMIT("%u ", count
);
3063 if (!pf
->zero_new_blocks
)
3064 DMEMIT("skip_block_zeroing ");
3066 if (!pf
->discard_enabled
)
3067 DMEMIT("ignore_discard ");
3069 if (!pf
->discard_passdown
)
3070 DMEMIT("no_discard_passdown ");
3072 if (pf
->mode
== PM_READ_ONLY
)
3073 DMEMIT("read_only ");
3075 if (pf
->error_if_no_space
)
3076 DMEMIT("error_if_no_space ");
3081 * <transaction id> <used metadata sectors>/<total metadata sectors>
3082 * <used data sectors>/<total data sectors> <held metadata root>
3084 static void pool_status(struct dm_target
*ti
, status_type_t type
,
3085 unsigned status_flags
, char *result
, unsigned maxlen
)
3089 uint64_t transaction_id
;
3090 dm_block_t nr_free_blocks_data
;
3091 dm_block_t nr_free_blocks_metadata
;
3092 dm_block_t nr_blocks_data
;
3093 dm_block_t nr_blocks_metadata
;
3094 dm_block_t held_root
;
3095 char buf
[BDEVNAME_SIZE
];
3096 char buf2
[BDEVNAME_SIZE
];
3097 struct pool_c
*pt
= ti
->private;
3098 struct pool
*pool
= pt
->pool
;
3101 case STATUSTYPE_INFO
:
3102 if (get_pool_mode(pool
) == PM_FAIL
) {
3107 /* Commit to ensure statistics aren't out-of-date */
3108 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
))
3109 (void) commit(pool
);
3111 r
= dm_pool_get_metadata_transaction_id(pool
->pmd
, &transaction_id
);
3113 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3114 dm_device_name(pool
->pool_md
), r
);
3118 r
= dm_pool_get_free_metadata_block_count(pool
->pmd
, &nr_free_blocks_metadata
);
3120 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3121 dm_device_name(pool
->pool_md
), r
);
3125 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &nr_blocks_metadata
);
3127 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3128 dm_device_name(pool
->pool_md
), r
);
3132 r
= dm_pool_get_free_block_count(pool
->pmd
, &nr_free_blocks_data
);
3134 DMERR("%s: dm_pool_get_free_block_count returned %d",
3135 dm_device_name(pool
->pool_md
), r
);
3139 r
= dm_pool_get_data_dev_size(pool
->pmd
, &nr_blocks_data
);
3141 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3142 dm_device_name(pool
->pool_md
), r
);
3146 r
= dm_pool_get_metadata_snap(pool
->pmd
, &held_root
);
3148 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3149 dm_device_name(pool
->pool_md
), r
);
3153 DMEMIT("%llu %llu/%llu %llu/%llu ",
3154 (unsigned long long)transaction_id
,
3155 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
3156 (unsigned long long)nr_blocks_metadata
,
3157 (unsigned long long)(nr_blocks_data
- nr_free_blocks_data
),
3158 (unsigned long long)nr_blocks_data
);
3161 DMEMIT("%llu ", held_root
);
3165 if (pool
->pf
.mode
== PM_OUT_OF_DATA_SPACE
)
3166 DMEMIT("out_of_data_space ");
3167 else if (pool
->pf
.mode
== PM_READ_ONLY
)
3172 if (!pool
->pf
.discard_enabled
)
3173 DMEMIT("ignore_discard ");
3174 else if (pool
->pf
.discard_passdown
)
3175 DMEMIT("discard_passdown ");
3177 DMEMIT("no_discard_passdown ");
3179 if (pool
->pf
.error_if_no_space
)
3180 DMEMIT("error_if_no_space ");
3182 DMEMIT("queue_if_no_space ");
3186 case STATUSTYPE_TABLE
:
3187 DMEMIT("%s %s %lu %llu ",
3188 format_dev_t(buf
, pt
->metadata_dev
->bdev
->bd_dev
),
3189 format_dev_t(buf2
, pt
->data_dev
->bdev
->bd_dev
),
3190 (unsigned long)pool
->sectors_per_block
,
3191 (unsigned long long)pt
->low_water_blocks
);
3192 emit_flags(&pt
->requested_pf
, result
, sz
, maxlen
);
3201 static int pool_iterate_devices(struct dm_target
*ti
,
3202 iterate_devices_callout_fn fn
, void *data
)
3204 struct pool_c
*pt
= ti
->private;
3206 return fn(ti
, pt
->data_dev
, 0, ti
->len
, data
);
3209 static int pool_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
3210 struct bio_vec
*biovec
, int max_size
)
3212 struct pool_c
*pt
= ti
->private;
3213 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
3215 if (!q
->merge_bvec_fn
)
3218 bvm
->bi_bdev
= pt
->data_dev
->bdev
;
3220 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
3223 static void set_discard_limits(struct pool_c
*pt
, struct queue_limits
*limits
)
3225 struct pool
*pool
= pt
->pool
;
3226 struct queue_limits
*data_limits
;
3228 limits
->max_discard_sectors
= pool
->sectors_per_block
;
3231 * discard_granularity is just a hint, and not enforced.
3233 if (pt
->adjusted_pf
.discard_passdown
) {
3234 data_limits
= &bdev_get_queue(pt
->data_dev
->bdev
)->limits
;
3235 limits
->discard_granularity
= max(data_limits
->discard_granularity
,
3236 pool
->sectors_per_block
<< SECTOR_SHIFT
);
3238 limits
->discard_granularity
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
3241 static void pool_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3243 struct pool_c
*pt
= ti
->private;
3244 struct pool
*pool
= pt
->pool
;
3245 uint64_t io_opt_sectors
= limits
->io_opt
>> SECTOR_SHIFT
;
3248 * If the system-determined stacked limits are compatible with the
3249 * pool's blocksize (io_opt is a factor) do not override them.
3251 if (io_opt_sectors
< pool
->sectors_per_block
||
3252 do_div(io_opt_sectors
, pool
->sectors_per_block
)) {
3253 blk_limits_io_min(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
3254 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
3258 * pt->adjusted_pf is a staging area for the actual features to use.
3259 * They get transferred to the live pool in bind_control_target()
3260 * called from pool_preresume().
3262 if (!pt
->adjusted_pf
.discard_enabled
) {
3264 * Must explicitly disallow stacking discard limits otherwise the
3265 * block layer will stack them if pool's data device has support.
3266 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3267 * user to see that, so make sure to set all discard limits to 0.
3269 limits
->discard_granularity
= 0;
3273 disable_passdown_if_not_supported(pt
);
3275 set_discard_limits(pt
, limits
);
3278 static struct target_type pool_target
= {
3279 .name
= "thin-pool",
3280 .features
= DM_TARGET_SINGLETON
| DM_TARGET_ALWAYS_WRITEABLE
|
3281 DM_TARGET_IMMUTABLE
,
3282 .version
= {1, 13, 0},
3283 .module
= THIS_MODULE
,
3287 .postsuspend
= pool_postsuspend
,
3288 .preresume
= pool_preresume
,
3289 .resume
= pool_resume
,
3290 .message
= pool_message
,
3291 .status
= pool_status
,
3292 .merge
= pool_merge
,
3293 .iterate_devices
= pool_iterate_devices
,
3294 .io_hints
= pool_io_hints
,
3297 /*----------------------------------------------------------------
3298 * Thin target methods
3299 *--------------------------------------------------------------*/
3300 static void thin_get(struct thin_c
*tc
)
3302 atomic_inc(&tc
->refcount
);
3305 static void thin_put(struct thin_c
*tc
)
3307 if (atomic_dec_and_test(&tc
->refcount
))
3308 complete(&tc
->can_destroy
);
3311 static void thin_dtr(struct dm_target
*ti
)
3313 struct thin_c
*tc
= ti
->private;
3314 unsigned long flags
;
3317 wait_for_completion(&tc
->can_destroy
);
3319 spin_lock_irqsave(&tc
->pool
->lock
, flags
);
3320 list_del_rcu(&tc
->list
);
3321 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
3324 mutex_lock(&dm_thin_pool_table
.mutex
);
3326 __pool_dec(tc
->pool
);
3327 dm_pool_close_thin_device(tc
->td
);
3328 dm_put_device(ti
, tc
->pool_dev
);
3330 dm_put_device(ti
, tc
->origin_dev
);
3333 mutex_unlock(&dm_thin_pool_table
.mutex
);
3337 * Thin target parameters:
3339 * <pool_dev> <dev_id> [origin_dev]
3341 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3342 * dev_id: the internal device identifier
3343 * origin_dev: a device external to the pool that should act as the origin
3345 * If the pool device has discards disabled, they get disabled for the thin
3348 static int thin_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
3352 struct dm_dev
*pool_dev
, *origin_dev
;
3353 struct mapped_device
*pool_md
;
3354 unsigned long flags
;
3356 mutex_lock(&dm_thin_pool_table
.mutex
);
3358 if (argc
!= 2 && argc
!= 3) {
3359 ti
->error
= "Invalid argument count";
3364 tc
= ti
->private = kzalloc(sizeof(*tc
), GFP_KERNEL
);
3366 ti
->error
= "Out of memory";
3370 spin_lock_init(&tc
->lock
);
3371 bio_list_init(&tc
->deferred_bio_list
);
3372 bio_list_init(&tc
->retry_on_resume_list
);
3373 tc
->sort_bio_list
= RB_ROOT
;
3376 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &origin_dev
);
3378 ti
->error
= "Error opening origin device";
3379 goto bad_origin_dev
;
3381 tc
->origin_dev
= origin_dev
;
3384 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &pool_dev
);
3386 ti
->error
= "Error opening pool device";
3389 tc
->pool_dev
= pool_dev
;
3391 if (read_dev_id(argv
[1], (unsigned long long *)&tc
->dev_id
, 0)) {
3392 ti
->error
= "Invalid device id";
3397 pool_md
= dm_get_md(tc
->pool_dev
->bdev
->bd_dev
);
3399 ti
->error
= "Couldn't get pool mapped device";
3404 tc
->pool
= __pool_table_lookup(pool_md
);
3406 ti
->error
= "Couldn't find pool object";
3408 goto bad_pool_lookup
;
3410 __pool_inc(tc
->pool
);
3412 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3413 ti
->error
= "Couldn't open thin device, Pool is in fail mode";
3418 r
= dm_pool_open_thin_device(tc
->pool
->pmd
, tc
->dev_id
, &tc
->td
);
3420 ti
->error
= "Couldn't open thin internal device";
3424 r
= dm_set_target_max_io_len(ti
, tc
->pool
->sectors_per_block
);
3426 goto bad_target_max_io_len
;
3428 ti
->num_flush_bios
= 1;
3429 ti
->flush_supported
= true;
3430 ti
->per_bio_data_size
= sizeof(struct dm_thin_endio_hook
);
3432 /* In case the pool supports discards, pass them on. */
3433 ti
->discard_zeroes_data_unsupported
= true;
3434 if (tc
->pool
->pf
.discard_enabled
) {
3435 ti
->discards_supported
= true;
3436 ti
->num_discard_bios
= 1;
3437 /* Discard bios must be split on a block boundary */
3438 ti
->split_discard_bios
= true;
3443 mutex_unlock(&dm_thin_pool_table
.mutex
);
3445 atomic_set(&tc
->refcount
, 1);
3446 init_completion(&tc
->can_destroy
);
3448 spin_lock_irqsave(&tc
->pool
->lock
, flags
);
3449 list_add_tail_rcu(&tc
->list
, &tc
->pool
->active_thins
);
3450 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
3452 * This synchronize_rcu() call is needed here otherwise we risk a
3453 * wake_worker() call finding no bios to process (because the newly
3454 * added tc isn't yet visible). So this reduces latency since we
3455 * aren't then dependent on the periodic commit to wake_worker().
3461 bad_target_max_io_len
:
3462 dm_pool_close_thin_device(tc
->td
);
3464 __pool_dec(tc
->pool
);
3468 dm_put_device(ti
, tc
->pool_dev
);
3471 dm_put_device(ti
, tc
->origin_dev
);
3475 mutex_unlock(&dm_thin_pool_table
.mutex
);
3480 static int thin_map(struct dm_target
*ti
, struct bio
*bio
)
3482 bio
->bi_iter
.bi_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
3484 return thin_bio_map(ti
, bio
);
3487 static int thin_endio(struct dm_target
*ti
, struct bio
*bio
, int err
)
3489 unsigned long flags
;
3490 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
3491 struct list_head work
;
3492 struct dm_thin_new_mapping
*m
, *tmp
;
3493 struct pool
*pool
= h
->tc
->pool
;
3495 if (h
->shared_read_entry
) {
3496 INIT_LIST_HEAD(&work
);
3497 dm_deferred_entry_dec(h
->shared_read_entry
, &work
);
3499 spin_lock_irqsave(&pool
->lock
, flags
);
3500 list_for_each_entry_safe(m
, tmp
, &work
, list
) {
3502 __complete_mapping_preparation(m
);
3504 spin_unlock_irqrestore(&pool
->lock
, flags
);
3507 if (h
->all_io_entry
) {
3508 INIT_LIST_HEAD(&work
);
3509 dm_deferred_entry_dec(h
->all_io_entry
, &work
);
3510 if (!list_empty(&work
)) {
3511 spin_lock_irqsave(&pool
->lock
, flags
);
3512 list_for_each_entry_safe(m
, tmp
, &work
, list
)
3513 list_add_tail(&m
->list
, &pool
->prepared_discards
);
3514 spin_unlock_irqrestore(&pool
->lock
, flags
);
3522 static void thin_presuspend(struct dm_target
*ti
)
3524 struct thin_c
*tc
= ti
->private;
3526 if (dm_noflush_suspending(ti
))
3527 noflush_work(tc
, do_noflush_start
);
3530 static void thin_postsuspend(struct dm_target
*ti
)
3532 struct thin_c
*tc
= ti
->private;
3535 * The dm_noflush_suspending flag has been cleared by now, so
3536 * unfortunately we must always run this.
3538 noflush_work(tc
, do_noflush_stop
);
3541 static int thin_preresume(struct dm_target
*ti
)
3543 struct thin_c
*tc
= ti
->private;
3546 tc
->origin_size
= get_dev_size(tc
->origin_dev
->bdev
);
3552 * <nr mapped sectors> <highest mapped sector>
3554 static void thin_status(struct dm_target
*ti
, status_type_t type
,
3555 unsigned status_flags
, char *result
, unsigned maxlen
)
3559 dm_block_t mapped
, highest
;
3560 char buf
[BDEVNAME_SIZE
];
3561 struct thin_c
*tc
= ti
->private;
3563 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3572 case STATUSTYPE_INFO
:
3573 r
= dm_thin_get_mapped_count(tc
->td
, &mapped
);
3575 DMERR("dm_thin_get_mapped_count returned %d", r
);
3579 r
= dm_thin_get_highest_mapped_block(tc
->td
, &highest
);
3581 DMERR("dm_thin_get_highest_mapped_block returned %d", r
);
3585 DMEMIT("%llu ", mapped
* tc
->pool
->sectors_per_block
);
3587 DMEMIT("%llu", ((highest
+ 1) *
3588 tc
->pool
->sectors_per_block
) - 1);
3593 case STATUSTYPE_TABLE
:
3595 format_dev_t(buf
, tc
->pool_dev
->bdev
->bd_dev
),
3596 (unsigned long) tc
->dev_id
);
3598 DMEMIT(" %s", format_dev_t(buf
, tc
->origin_dev
->bdev
->bd_dev
));
3609 static int thin_iterate_devices(struct dm_target
*ti
,
3610 iterate_devices_callout_fn fn
, void *data
)
3613 struct thin_c
*tc
= ti
->private;
3614 struct pool
*pool
= tc
->pool
;
3617 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3618 * we follow a more convoluted path through to the pool's target.
3621 return 0; /* nothing is bound */
3623 blocks
= pool
->ti
->len
;
3624 (void) sector_div(blocks
, pool
->sectors_per_block
);
3626 return fn(ti
, tc
->pool_dev
, 0, pool
->sectors_per_block
* blocks
, data
);
3631 static struct target_type thin_target
= {
3633 .version
= {1, 13, 0},
3634 .module
= THIS_MODULE
,
3638 .end_io
= thin_endio
,
3639 .preresume
= thin_preresume
,
3640 .presuspend
= thin_presuspend
,
3641 .postsuspend
= thin_postsuspend
,
3642 .status
= thin_status
,
3643 .iterate_devices
= thin_iterate_devices
,
3646 /*----------------------------------------------------------------*/
3648 static int __init
dm_thin_init(void)
3654 r
= dm_register_target(&thin_target
);
3658 r
= dm_register_target(&pool_target
);
3660 goto bad_pool_target
;
3664 _new_mapping_cache
= KMEM_CACHE(dm_thin_new_mapping
, 0);
3665 if (!_new_mapping_cache
)
3666 goto bad_new_mapping_cache
;
3670 bad_new_mapping_cache
:
3671 dm_unregister_target(&pool_target
);
3673 dm_unregister_target(&thin_target
);
3678 static void dm_thin_exit(void)
3680 dm_unregister_target(&thin_target
);
3681 dm_unregister_target(&pool_target
);
3683 kmem_cache_destroy(_new_mapping_cache
);
3686 module_init(dm_thin_init
);
3687 module_exit(dm_thin_exit
);
3689 module_param_named(no_space_timeout
, no_space_timeout_secs
, uint
, S_IRUGO
| S_IWUSR
);
3690 MODULE_PARM_DESC(no_space_timeout
, "Out of data space queue IO timeout in seconds");
3692 MODULE_DESCRIPTION(DM_NAME
" thin provisioning target");
3693 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3694 MODULE_LICENSE("GPL");