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 PRISON_CELLS 1024
29 #define COMMIT_PERIOD HZ
31 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle
,
32 "A percentage of time allocated for copy on write");
35 * The block size of the device holding pool data must be
36 * between 64KB and 1GB.
38 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
39 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
42 * Device id is restricted to 24 bits.
44 #define MAX_DEV_ID ((1 << 24) - 1)
47 * How do we handle breaking sharing of data blocks?
48 * =================================================
50 * We use a standard copy-on-write btree to store the mappings for the
51 * devices (note I'm talking about copy-on-write of the metadata here, not
52 * the data). When you take an internal snapshot you clone the root node
53 * of the origin btree. After this there is no concept of an origin or a
54 * snapshot. They are just two device trees that happen to point to the
57 * When we get a write in we decide if it's to a shared data block using
58 * some timestamp magic. If it is, we have to break sharing.
60 * Let's say we write to a shared block in what was the origin. The
63 * i) plug io further to this physical block. (see bio_prison code).
65 * ii) quiesce any read io to that shared data block. Obviously
66 * including all devices that share this block. (see dm_deferred_set code)
68 * iii) copy the data block to a newly allocate block. This step can be
69 * missed out if the io covers the block. (schedule_copy).
71 * iv) insert the new mapping into the origin's btree
72 * (process_prepared_mapping). This act of inserting breaks some
73 * sharing of btree nodes between the two devices. Breaking sharing only
74 * effects the btree of that specific device. Btrees for the other
75 * devices that share the block never change. The btree for the origin
76 * device as it was after the last commit is untouched, ie. we're using
77 * persistent data structures in the functional programming sense.
79 * v) unplug io to this physical block, including the io that triggered
80 * the breaking of sharing.
82 * Steps (ii) and (iii) occur in parallel.
84 * The metadata _doesn't_ need to be committed before the io continues. We
85 * get away with this because the io is always written to a _new_ block.
86 * If there's a crash, then:
88 * - The origin mapping will point to the old origin block (the shared
89 * one). This will contain the data as it was before the io that triggered
90 * the breaking of sharing came in.
92 * - The snap mapping still points to the old block. As it would after
95 * The downside of this scheme is the timestamp magic isn't perfect, and
96 * will continue to think that data block in the snapshot device is shared
97 * even after the write to the origin has broken sharing. I suspect data
98 * blocks will typically be shared by many different devices, so we're
99 * breaking sharing n + 1 times, rather than n, where n is the number of
100 * devices that reference this data block. At the moment I think the
101 * benefits far, far outweigh the disadvantages.
104 /*----------------------------------------------------------------*/
109 static void build_data_key(struct dm_thin_device
*td
,
110 dm_block_t b
, struct dm_cell_key
*key
)
113 key
->dev
= dm_thin_dev_id(td
);
117 static void build_virtual_key(struct dm_thin_device
*td
, dm_block_t b
,
118 struct dm_cell_key
*key
)
121 key
->dev
= dm_thin_dev_id(td
);
125 /*----------------------------------------------------------------*/
128 * A pool device ties together a metadata device and a data device. It
129 * also provides the interface for creating and destroying internal
132 struct dm_thin_new_mapping
;
135 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
138 PM_WRITE
, /* metadata may be changed */
139 PM_OUT_OF_DATA_SPACE
, /* metadata may be changed, though data may not be allocated */
140 PM_READ_ONLY
, /* metadata may not be changed */
141 PM_FAIL
, /* all I/O fails */
144 struct pool_features
{
147 bool zero_new_blocks
:1;
148 bool discard_enabled
:1;
149 bool discard_passdown
:1;
150 bool error_if_no_space
:1;
154 typedef void (*process_bio_fn
)(struct thin_c
*tc
, struct bio
*bio
);
155 typedef void (*process_mapping_fn
)(struct dm_thin_new_mapping
*m
);
158 struct list_head list
;
159 struct dm_target
*ti
; /* Only set if a pool target is bound */
161 struct mapped_device
*pool_md
;
162 struct block_device
*md_dev
;
163 struct dm_pool_metadata
*pmd
;
165 dm_block_t low_water_blocks
;
166 uint32_t sectors_per_block
;
167 int sectors_per_block_shift
;
169 struct pool_features pf
;
170 bool low_water_triggered
:1; /* A dm event has been sent */
172 struct dm_bio_prison
*prison
;
173 struct dm_kcopyd_client
*copier
;
175 struct workqueue_struct
*wq
;
176 struct work_struct worker
;
177 struct delayed_work waker
;
179 unsigned long last_commit_jiffies
;
183 struct bio_list deferred_flush_bios
;
184 struct list_head prepared_mappings
;
185 struct list_head prepared_discards
;
186 struct list_head active_thins
;
188 struct dm_deferred_set
*shared_read_ds
;
189 struct dm_deferred_set
*all_io_ds
;
191 struct dm_thin_new_mapping
*next_mapping
;
192 mempool_t
*mapping_pool
;
194 process_bio_fn process_bio
;
195 process_bio_fn process_discard
;
197 process_mapping_fn process_prepared_mapping
;
198 process_mapping_fn process_prepared_discard
;
201 static enum pool_mode
get_pool_mode(struct pool
*pool
);
202 static void metadata_operation_failed(struct pool
*pool
, const char *op
, int r
);
205 * Target context for a pool.
208 struct dm_target
*ti
;
210 struct dm_dev
*data_dev
;
211 struct dm_dev
*metadata_dev
;
212 struct dm_target_callbacks callbacks
;
214 dm_block_t low_water_blocks
;
215 struct pool_features requested_pf
; /* Features requested during table load */
216 struct pool_features adjusted_pf
; /* Features used after adjusting for constituent devices */
220 * Target context for a thin.
223 struct list_head list
;
224 struct dm_dev
*pool_dev
;
225 struct dm_dev
*origin_dev
;
229 struct dm_thin_device
*td
;
232 struct bio_list deferred_bio_list
;
233 struct bio_list retry_on_resume_list
;
234 struct rb_root sort_bio_list
; /* sorted list of deferred bios */
237 /*----------------------------------------------------------------*/
240 * wake_worker() is used when new work is queued and when pool_resume is
241 * ready to continue deferred IO processing.
243 static void wake_worker(struct pool
*pool
)
245 queue_work(pool
->wq
, &pool
->worker
);
248 /*----------------------------------------------------------------*/
250 static int bio_detain(struct pool
*pool
, struct dm_cell_key
*key
, struct bio
*bio
,
251 struct dm_bio_prison_cell
**cell_result
)
254 struct dm_bio_prison_cell
*cell_prealloc
;
257 * Allocate a cell from the prison's mempool.
258 * This might block but it can't fail.
260 cell_prealloc
= dm_bio_prison_alloc_cell(pool
->prison
, GFP_NOIO
);
262 r
= dm_bio_detain(pool
->prison
, key
, bio
, cell_prealloc
, cell_result
);
265 * We reused an old cell; we can get rid of
268 dm_bio_prison_free_cell(pool
->prison
, cell_prealloc
);
273 static void cell_release(struct pool
*pool
,
274 struct dm_bio_prison_cell
*cell
,
275 struct bio_list
*bios
)
277 dm_cell_release(pool
->prison
, cell
, bios
);
278 dm_bio_prison_free_cell(pool
->prison
, cell
);
281 static void cell_release_no_holder(struct pool
*pool
,
282 struct dm_bio_prison_cell
*cell
,
283 struct bio_list
*bios
)
285 dm_cell_release_no_holder(pool
->prison
, cell
, bios
);
286 dm_bio_prison_free_cell(pool
->prison
, cell
);
289 static void cell_defer_no_holder_no_free(struct thin_c
*tc
,
290 struct dm_bio_prison_cell
*cell
)
292 struct pool
*pool
= tc
->pool
;
295 spin_lock_irqsave(&tc
->lock
, flags
);
296 dm_cell_release_no_holder(pool
->prison
, cell
, &tc
->deferred_bio_list
);
297 spin_unlock_irqrestore(&tc
->lock
, flags
);
302 static void cell_error(struct pool
*pool
,
303 struct dm_bio_prison_cell
*cell
)
305 dm_cell_error(pool
->prison
, cell
);
306 dm_bio_prison_free_cell(pool
->prison
, cell
);
309 /*----------------------------------------------------------------*/
312 * A global list of pools that uses a struct mapped_device as a key.
314 static struct dm_thin_pool_table
{
316 struct list_head pools
;
317 } dm_thin_pool_table
;
319 static void pool_table_init(void)
321 mutex_init(&dm_thin_pool_table
.mutex
);
322 INIT_LIST_HEAD(&dm_thin_pool_table
.pools
);
325 static void __pool_table_insert(struct pool
*pool
)
327 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
328 list_add(&pool
->list
, &dm_thin_pool_table
.pools
);
331 static void __pool_table_remove(struct pool
*pool
)
333 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
334 list_del(&pool
->list
);
337 static struct pool
*__pool_table_lookup(struct mapped_device
*md
)
339 struct pool
*pool
= NULL
, *tmp
;
341 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
343 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
344 if (tmp
->pool_md
== md
) {
353 static struct pool
*__pool_table_lookup_metadata_dev(struct block_device
*md_dev
)
355 struct pool
*pool
= NULL
, *tmp
;
357 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
359 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
360 if (tmp
->md_dev
== md_dev
) {
369 /*----------------------------------------------------------------*/
371 struct dm_thin_endio_hook
{
373 struct dm_deferred_entry
*shared_read_entry
;
374 struct dm_deferred_entry
*all_io_entry
;
375 struct dm_thin_new_mapping
*overwrite_mapping
;
376 struct rb_node rb_node
;
379 static void requeue_bio_list(struct thin_c
*tc
, struct bio_list
*master
)
382 struct bio_list bios
;
385 bio_list_init(&bios
);
387 spin_lock_irqsave(&tc
->lock
, flags
);
388 bio_list_merge(&bios
, master
);
389 bio_list_init(master
);
390 spin_unlock_irqrestore(&tc
->lock
, flags
);
392 while ((bio
= bio_list_pop(&bios
)))
393 bio_endio(bio
, DM_ENDIO_REQUEUE
);
396 static void requeue_io(struct thin_c
*tc
)
398 requeue_bio_list(tc
, &tc
->deferred_bio_list
);
399 requeue_bio_list(tc
, &tc
->retry_on_resume_list
);
402 static void error_thin_retry_list(struct thin_c
*tc
)
406 struct bio_list bios
;
408 bio_list_init(&bios
);
410 spin_lock_irqsave(&tc
->lock
, flags
);
411 bio_list_merge(&bios
, &tc
->retry_on_resume_list
);
412 bio_list_init(&tc
->retry_on_resume_list
);
413 spin_unlock_irqrestore(&tc
->lock
, flags
);
415 while ((bio
= bio_list_pop(&bios
)))
419 static void error_retry_list(struct pool
*pool
)
424 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
)
425 error_thin_retry_list(tc
);
430 * This section of code contains the logic for processing a thin device's IO.
431 * Much of the code depends on pool object resources (lists, workqueues, etc)
432 * but most is exclusively called from the thin target rather than the thin-pool
436 static bool block_size_is_power_of_two(struct pool
*pool
)
438 return pool
->sectors_per_block_shift
>= 0;
441 static dm_block_t
get_bio_block(struct thin_c
*tc
, struct bio
*bio
)
443 struct pool
*pool
= tc
->pool
;
444 sector_t block_nr
= bio
->bi_iter
.bi_sector
;
446 if (block_size_is_power_of_two(pool
))
447 block_nr
>>= pool
->sectors_per_block_shift
;
449 (void) sector_div(block_nr
, pool
->sectors_per_block
);
454 static void remap(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
)
456 struct pool
*pool
= tc
->pool
;
457 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
459 bio
->bi_bdev
= tc
->pool_dev
->bdev
;
460 if (block_size_is_power_of_two(pool
))
461 bio
->bi_iter
.bi_sector
=
462 (block
<< pool
->sectors_per_block_shift
) |
463 (bi_sector
& (pool
->sectors_per_block
- 1));
465 bio
->bi_iter
.bi_sector
= (block
* pool
->sectors_per_block
) +
466 sector_div(bi_sector
, pool
->sectors_per_block
);
469 static void remap_to_origin(struct thin_c
*tc
, struct bio
*bio
)
471 bio
->bi_bdev
= tc
->origin_dev
->bdev
;
474 static int bio_triggers_commit(struct thin_c
*tc
, struct bio
*bio
)
476 return (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) &&
477 dm_thin_changed_this_transaction(tc
->td
);
480 static void inc_all_io_entry(struct pool
*pool
, struct bio
*bio
)
482 struct dm_thin_endio_hook
*h
;
484 if (bio
->bi_rw
& REQ_DISCARD
)
487 h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
488 h
->all_io_entry
= dm_deferred_entry_inc(pool
->all_io_ds
);
491 static void issue(struct thin_c
*tc
, struct bio
*bio
)
493 struct pool
*pool
= tc
->pool
;
496 if (!bio_triggers_commit(tc
, bio
)) {
497 generic_make_request(bio
);
502 * Complete bio with an error if earlier I/O caused changes to
503 * the metadata that can't be committed e.g, due to I/O errors
504 * on the metadata device.
506 if (dm_thin_aborted_changes(tc
->td
)) {
512 * Batch together any bios that trigger commits and then issue a
513 * single commit for them in process_deferred_bios().
515 spin_lock_irqsave(&pool
->lock
, flags
);
516 bio_list_add(&pool
->deferred_flush_bios
, bio
);
517 spin_unlock_irqrestore(&pool
->lock
, flags
);
520 static void remap_to_origin_and_issue(struct thin_c
*tc
, struct bio
*bio
)
522 remap_to_origin(tc
, bio
);
526 static void remap_and_issue(struct thin_c
*tc
, struct bio
*bio
,
529 remap(tc
, bio
, block
);
533 /*----------------------------------------------------------------*/
536 * Bio endio functions.
538 struct dm_thin_new_mapping
{
539 struct list_head list
;
544 bool definitely_not_shared
:1;
548 dm_block_t virt_block
;
549 dm_block_t data_block
;
550 struct dm_bio_prison_cell
*cell
, *cell2
;
553 * If the bio covers the whole area of a block then we can avoid
554 * zeroing or copying. Instead this bio is hooked. The bio will
555 * still be in the cell, so care has to be taken to avoid issuing
559 bio_end_io_t
*saved_bi_end_io
;
562 static void __maybe_add_mapping(struct dm_thin_new_mapping
*m
)
564 struct pool
*pool
= m
->tc
->pool
;
566 if (m
->quiesced
&& m
->prepared
) {
567 list_add_tail(&m
->list
, &pool
->prepared_mappings
);
572 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
575 struct dm_thin_new_mapping
*m
= context
;
576 struct pool
*pool
= m
->tc
->pool
;
578 m
->err
= read_err
|| write_err
? -EIO
: 0;
580 spin_lock_irqsave(&pool
->lock
, flags
);
582 __maybe_add_mapping(m
);
583 spin_unlock_irqrestore(&pool
->lock
, flags
);
586 static void overwrite_endio(struct bio
*bio
, int err
)
589 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
590 struct dm_thin_new_mapping
*m
= h
->overwrite_mapping
;
591 struct pool
*pool
= m
->tc
->pool
;
595 spin_lock_irqsave(&pool
->lock
, flags
);
597 __maybe_add_mapping(m
);
598 spin_unlock_irqrestore(&pool
->lock
, flags
);
601 /*----------------------------------------------------------------*/
608 * Prepared mapping jobs.
612 * This sends the bios in the cell back to the deferred_bios list.
614 static void cell_defer(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
616 struct pool
*pool
= tc
->pool
;
619 spin_lock_irqsave(&tc
->lock
, flags
);
620 cell_release(pool
, cell
, &tc
->deferred_bio_list
);
621 spin_unlock_irqrestore(&tc
->lock
, flags
);
627 * Same as cell_defer above, except it omits the original holder of the cell.
629 static void cell_defer_no_holder(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
631 struct pool
*pool
= tc
->pool
;
634 spin_lock_irqsave(&tc
->lock
, flags
);
635 cell_release_no_holder(pool
, cell
, &tc
->deferred_bio_list
);
636 spin_unlock_irqrestore(&tc
->lock
, flags
);
641 static void process_prepared_mapping_fail(struct dm_thin_new_mapping
*m
)
644 m
->bio
->bi_end_io
= m
->saved_bi_end_io
;
645 atomic_inc(&m
->bio
->bi_remaining
);
647 cell_error(m
->tc
->pool
, m
->cell
);
649 mempool_free(m
, m
->tc
->pool
->mapping_pool
);
652 static void process_prepared_mapping(struct dm_thin_new_mapping
*m
)
654 struct thin_c
*tc
= m
->tc
;
655 struct pool
*pool
= tc
->pool
;
661 bio
->bi_end_io
= m
->saved_bi_end_io
;
662 atomic_inc(&bio
->bi_remaining
);
666 cell_error(pool
, m
->cell
);
671 * Commit the prepared block into the mapping btree.
672 * Any I/O for this block arriving after this point will get
673 * remapped to it directly.
675 r
= dm_thin_insert_block(tc
->td
, m
->virt_block
, m
->data_block
);
677 metadata_operation_failed(pool
, "dm_thin_insert_block", r
);
678 cell_error(pool
, m
->cell
);
683 * Release any bios held while the block was being provisioned.
684 * If we are processing a write bio that completely covers the block,
685 * we already processed it so can ignore it now when processing
686 * the bios in the cell.
689 cell_defer_no_holder(tc
, m
->cell
);
692 cell_defer(tc
, m
->cell
);
696 mempool_free(m
, pool
->mapping_pool
);
699 static void process_prepared_discard_fail(struct dm_thin_new_mapping
*m
)
701 struct thin_c
*tc
= m
->tc
;
703 bio_io_error(m
->bio
);
704 cell_defer_no_holder(tc
, m
->cell
);
705 cell_defer_no_holder(tc
, m
->cell2
);
706 mempool_free(m
, tc
->pool
->mapping_pool
);
709 static void process_prepared_discard_passdown(struct dm_thin_new_mapping
*m
)
711 struct thin_c
*tc
= m
->tc
;
713 inc_all_io_entry(tc
->pool
, m
->bio
);
714 cell_defer_no_holder(tc
, m
->cell
);
715 cell_defer_no_holder(tc
, m
->cell2
);
718 if (m
->definitely_not_shared
)
719 remap_and_issue(tc
, m
->bio
, m
->data_block
);
722 if (dm_pool_block_is_used(tc
->pool
->pmd
, m
->data_block
, &used
) || used
)
723 bio_endio(m
->bio
, 0);
725 remap_and_issue(tc
, m
->bio
, m
->data_block
);
728 bio_endio(m
->bio
, 0);
730 mempool_free(m
, tc
->pool
->mapping_pool
);
733 static void process_prepared_discard(struct dm_thin_new_mapping
*m
)
736 struct thin_c
*tc
= m
->tc
;
738 r
= dm_thin_remove_block(tc
->td
, m
->virt_block
);
740 DMERR_LIMIT("dm_thin_remove_block() failed");
742 process_prepared_discard_passdown(m
);
745 static void process_prepared(struct pool
*pool
, struct list_head
*head
,
746 process_mapping_fn
*fn
)
749 struct list_head maps
;
750 struct dm_thin_new_mapping
*m
, *tmp
;
752 INIT_LIST_HEAD(&maps
);
753 spin_lock_irqsave(&pool
->lock
, flags
);
754 list_splice_init(head
, &maps
);
755 spin_unlock_irqrestore(&pool
->lock
, flags
);
757 list_for_each_entry_safe(m
, tmp
, &maps
, list
)
764 static int io_overlaps_block(struct pool
*pool
, struct bio
*bio
)
766 return bio
->bi_iter
.bi_size
==
767 (pool
->sectors_per_block
<< SECTOR_SHIFT
);
770 static int io_overwrites_block(struct pool
*pool
, struct bio
*bio
)
772 return (bio_data_dir(bio
) == WRITE
) &&
773 io_overlaps_block(pool
, bio
);
776 static void save_and_set_endio(struct bio
*bio
, bio_end_io_t
**save
,
779 *save
= bio
->bi_end_io
;
783 static int ensure_next_mapping(struct pool
*pool
)
785 if (pool
->next_mapping
)
788 pool
->next_mapping
= mempool_alloc(pool
->mapping_pool
, GFP_ATOMIC
);
790 return pool
->next_mapping
? 0 : -ENOMEM
;
793 static struct dm_thin_new_mapping
*get_next_mapping(struct pool
*pool
)
795 struct dm_thin_new_mapping
*m
= pool
->next_mapping
;
797 BUG_ON(!pool
->next_mapping
);
799 memset(m
, 0, sizeof(struct dm_thin_new_mapping
));
800 INIT_LIST_HEAD(&m
->list
);
803 pool
->next_mapping
= NULL
;
808 static void schedule_copy(struct thin_c
*tc
, dm_block_t virt_block
,
809 struct dm_dev
*origin
, dm_block_t data_origin
,
810 dm_block_t data_dest
,
811 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
814 struct pool
*pool
= tc
->pool
;
815 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
818 m
->virt_block
= virt_block
;
819 m
->data_block
= data_dest
;
822 if (!dm_deferred_set_add_work(pool
->shared_read_ds
, &m
->list
))
826 * IO to pool_dev remaps to the pool target's data_dev.
828 * If the whole block of data is being overwritten, we can issue the
829 * bio immediately. Otherwise we use kcopyd to clone the data first.
831 if (io_overwrites_block(pool
, bio
)) {
832 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
834 h
->overwrite_mapping
= m
;
836 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
837 inc_all_io_entry(pool
, bio
);
838 remap_and_issue(tc
, bio
, data_dest
);
840 struct dm_io_region from
, to
;
842 from
.bdev
= origin
->bdev
;
843 from
.sector
= data_origin
* pool
->sectors_per_block
;
844 from
.count
= pool
->sectors_per_block
;
846 to
.bdev
= tc
->pool_dev
->bdev
;
847 to
.sector
= data_dest
* pool
->sectors_per_block
;
848 to
.count
= pool
->sectors_per_block
;
850 r
= dm_kcopyd_copy(pool
->copier
, &from
, 1, &to
,
851 0, copy_complete
, m
);
853 mempool_free(m
, pool
->mapping_pool
);
854 DMERR_LIMIT("dm_kcopyd_copy() failed");
855 cell_error(pool
, cell
);
860 static void schedule_internal_copy(struct thin_c
*tc
, dm_block_t virt_block
,
861 dm_block_t data_origin
, dm_block_t data_dest
,
862 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
864 schedule_copy(tc
, virt_block
, tc
->pool_dev
,
865 data_origin
, data_dest
, cell
, bio
);
868 static void schedule_external_copy(struct thin_c
*tc
, dm_block_t virt_block
,
869 dm_block_t data_dest
,
870 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
872 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
873 virt_block
, data_dest
, cell
, bio
);
876 static void schedule_zero(struct thin_c
*tc
, dm_block_t virt_block
,
877 dm_block_t data_block
, struct dm_bio_prison_cell
*cell
,
880 struct pool
*pool
= tc
->pool
;
881 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
886 m
->virt_block
= virt_block
;
887 m
->data_block
= data_block
;
891 * If the whole block of data is being overwritten or we are not
892 * zeroing pre-existing data, we can issue the bio immediately.
893 * Otherwise we use kcopyd to zero the data first.
895 if (!pool
->pf
.zero_new_blocks
)
896 process_prepared_mapping(m
);
898 else if (io_overwrites_block(pool
, bio
)) {
899 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
901 h
->overwrite_mapping
= m
;
903 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
904 inc_all_io_entry(pool
, bio
);
905 remap_and_issue(tc
, bio
, data_block
);
908 struct dm_io_region to
;
910 to
.bdev
= tc
->pool_dev
->bdev
;
911 to
.sector
= data_block
* pool
->sectors_per_block
;
912 to
.count
= pool
->sectors_per_block
;
914 r
= dm_kcopyd_zero(pool
->copier
, 1, &to
, 0, copy_complete
, m
);
916 mempool_free(m
, pool
->mapping_pool
);
917 DMERR_LIMIT("dm_kcopyd_zero() failed");
918 cell_error(pool
, cell
);
924 * A non-zero return indicates read_only or fail_io mode.
925 * Many callers don't care about the return value.
927 static int commit(struct pool
*pool
)
931 if (get_pool_mode(pool
) != PM_WRITE
)
934 r
= dm_pool_commit_metadata(pool
->pmd
);
936 metadata_operation_failed(pool
, "dm_pool_commit_metadata", r
);
941 static void check_low_water_mark(struct pool
*pool
, dm_block_t free_blocks
)
945 if (free_blocks
<= pool
->low_water_blocks
&& !pool
->low_water_triggered
) {
946 DMWARN("%s: reached low water mark for data device: sending event.",
947 dm_device_name(pool
->pool_md
));
948 spin_lock_irqsave(&pool
->lock
, flags
);
949 pool
->low_water_triggered
= true;
950 spin_unlock_irqrestore(&pool
->lock
, flags
);
951 dm_table_event(pool
->ti
->table
);
955 static void set_pool_mode(struct pool
*pool
, enum pool_mode new_mode
);
957 static int alloc_data_block(struct thin_c
*tc
, dm_block_t
*result
)
960 dm_block_t free_blocks
;
961 struct pool
*pool
= tc
->pool
;
963 if (WARN_ON(get_pool_mode(pool
) != PM_WRITE
))
966 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
968 metadata_operation_failed(pool
, "dm_pool_get_free_block_count", r
);
972 check_low_water_mark(pool
, free_blocks
);
976 * Try to commit to see if that will free up some
983 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
985 metadata_operation_failed(pool
, "dm_pool_get_free_block_count", r
);
990 set_pool_mode(pool
, PM_OUT_OF_DATA_SPACE
);
995 r
= dm_pool_alloc_data_block(pool
->pmd
, result
);
997 metadata_operation_failed(pool
, "dm_pool_alloc_data_block", r
);
1005 * If we have run out of space, queue bios until the device is
1006 * resumed, presumably after having been reloaded with more space.
1008 static void retry_on_resume(struct bio
*bio
)
1010 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1011 struct thin_c
*tc
= h
->tc
;
1012 unsigned long flags
;
1014 spin_lock_irqsave(&tc
->lock
, flags
);
1015 bio_list_add(&tc
->retry_on_resume_list
, bio
);
1016 spin_unlock_irqrestore(&tc
->lock
, flags
);
1019 static bool should_error_unserviceable_bio(struct pool
*pool
)
1021 enum pool_mode m
= get_pool_mode(pool
);
1025 /* Shouldn't get here */
1026 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1029 case PM_OUT_OF_DATA_SPACE
:
1030 return pool
->pf
.error_if_no_space
;
1036 /* Shouldn't get here */
1037 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1042 static void handle_unserviceable_bio(struct pool
*pool
, struct bio
*bio
)
1044 if (should_error_unserviceable_bio(pool
))
1047 retry_on_resume(bio
);
1050 static void retry_bios_on_resume(struct pool
*pool
, struct dm_bio_prison_cell
*cell
)
1053 struct bio_list bios
;
1055 if (should_error_unserviceable_bio(pool
)) {
1056 cell_error(pool
, cell
);
1060 bio_list_init(&bios
);
1061 cell_release(pool
, cell
, &bios
);
1063 if (should_error_unserviceable_bio(pool
))
1064 while ((bio
= bio_list_pop(&bios
)))
1067 while ((bio
= bio_list_pop(&bios
)))
1068 retry_on_resume(bio
);
1071 static void process_discard(struct thin_c
*tc
, struct bio
*bio
)
1074 unsigned long flags
;
1075 struct pool
*pool
= tc
->pool
;
1076 struct dm_bio_prison_cell
*cell
, *cell2
;
1077 struct dm_cell_key key
, key2
;
1078 dm_block_t block
= get_bio_block(tc
, bio
);
1079 struct dm_thin_lookup_result lookup_result
;
1080 struct dm_thin_new_mapping
*m
;
1082 build_virtual_key(tc
->td
, block
, &key
);
1083 if (bio_detain(tc
->pool
, &key
, bio
, &cell
))
1086 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1090 * Check nobody is fiddling with this pool block. This can
1091 * happen if someone's in the process of breaking sharing
1094 build_data_key(tc
->td
, lookup_result
.block
, &key2
);
1095 if (bio_detain(tc
->pool
, &key2
, bio
, &cell2
)) {
1096 cell_defer_no_holder(tc
, cell
);
1100 if (io_overlaps_block(pool
, bio
)) {
1102 * IO may still be going to the destination block. We must
1103 * quiesce before we can do the removal.
1105 m
= get_next_mapping(pool
);
1107 m
->pass_discard
= pool
->pf
.discard_passdown
;
1108 m
->definitely_not_shared
= !lookup_result
.shared
;
1109 m
->virt_block
= block
;
1110 m
->data_block
= lookup_result
.block
;
1115 if (!dm_deferred_set_add_work(pool
->all_io_ds
, &m
->list
)) {
1116 spin_lock_irqsave(&pool
->lock
, flags
);
1117 list_add_tail(&m
->list
, &pool
->prepared_discards
);
1118 spin_unlock_irqrestore(&pool
->lock
, flags
);
1122 inc_all_io_entry(pool
, bio
);
1123 cell_defer_no_holder(tc
, cell
);
1124 cell_defer_no_holder(tc
, cell2
);
1127 * The DM core makes sure that the discard doesn't span
1128 * a block boundary. So we submit the discard of a
1129 * partial block appropriately.
1131 if ((!lookup_result
.shared
) && pool
->pf
.discard_passdown
)
1132 remap_and_issue(tc
, bio
, lookup_result
.block
);
1140 * It isn't provisioned, just forget it.
1142 cell_defer_no_holder(tc
, cell
);
1147 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1149 cell_defer_no_holder(tc
, cell
);
1155 static void break_sharing(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1156 struct dm_cell_key
*key
,
1157 struct dm_thin_lookup_result
*lookup_result
,
1158 struct dm_bio_prison_cell
*cell
)
1161 dm_block_t data_block
;
1162 struct pool
*pool
= tc
->pool
;
1164 r
= alloc_data_block(tc
, &data_block
);
1167 schedule_internal_copy(tc
, block
, lookup_result
->block
,
1168 data_block
, cell
, bio
);
1172 retry_bios_on_resume(pool
, cell
);
1176 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1178 cell_error(pool
, cell
);
1183 static void process_shared_bio(struct thin_c
*tc
, struct bio
*bio
,
1185 struct dm_thin_lookup_result
*lookup_result
)
1187 struct dm_bio_prison_cell
*cell
;
1188 struct pool
*pool
= tc
->pool
;
1189 struct dm_cell_key key
;
1192 * If cell is already occupied, then sharing is already in the process
1193 * of being broken so we have nothing further to do here.
1195 build_data_key(tc
->td
, lookup_result
->block
, &key
);
1196 if (bio_detain(pool
, &key
, bio
, &cell
))
1199 if (bio_data_dir(bio
) == WRITE
&& bio
->bi_iter
.bi_size
)
1200 break_sharing(tc
, bio
, block
, &key
, lookup_result
, cell
);
1202 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1204 h
->shared_read_entry
= dm_deferred_entry_inc(pool
->shared_read_ds
);
1205 inc_all_io_entry(pool
, bio
);
1206 cell_defer_no_holder(tc
, cell
);
1208 remap_and_issue(tc
, bio
, lookup_result
->block
);
1212 static void provision_block(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1213 struct dm_bio_prison_cell
*cell
)
1216 dm_block_t data_block
;
1217 struct pool
*pool
= tc
->pool
;
1220 * Remap empty bios (flushes) immediately, without provisioning.
1222 if (!bio
->bi_iter
.bi_size
) {
1223 inc_all_io_entry(pool
, bio
);
1224 cell_defer_no_holder(tc
, cell
);
1226 remap_and_issue(tc
, bio
, 0);
1231 * Fill read bios with zeroes and complete them immediately.
1233 if (bio_data_dir(bio
) == READ
) {
1235 cell_defer_no_holder(tc
, cell
);
1240 r
= alloc_data_block(tc
, &data_block
);
1244 schedule_external_copy(tc
, block
, data_block
, cell
, bio
);
1246 schedule_zero(tc
, block
, data_block
, cell
, bio
);
1250 retry_bios_on_resume(pool
, cell
);
1254 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1256 cell_error(pool
, cell
);
1261 static void process_bio(struct thin_c
*tc
, struct bio
*bio
)
1264 struct pool
*pool
= tc
->pool
;
1265 dm_block_t block
= get_bio_block(tc
, bio
);
1266 struct dm_bio_prison_cell
*cell
;
1267 struct dm_cell_key key
;
1268 struct dm_thin_lookup_result lookup_result
;
1271 * If cell is already occupied, then the block is already
1272 * being provisioned so we have nothing further to do here.
1274 build_virtual_key(tc
->td
, block
, &key
);
1275 if (bio_detain(pool
, &key
, bio
, &cell
))
1278 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1281 if (lookup_result
.shared
) {
1282 process_shared_bio(tc
, bio
, block
, &lookup_result
);
1283 cell_defer_no_holder(tc
, cell
); /* FIXME: pass this cell into process_shared? */
1285 inc_all_io_entry(pool
, bio
);
1286 cell_defer_no_holder(tc
, cell
);
1288 remap_and_issue(tc
, bio
, lookup_result
.block
);
1293 if (bio_data_dir(bio
) == READ
&& tc
->origin_dev
) {
1294 inc_all_io_entry(pool
, bio
);
1295 cell_defer_no_holder(tc
, cell
);
1297 remap_to_origin_and_issue(tc
, bio
);
1299 provision_block(tc
, bio
, block
, cell
);
1303 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1305 cell_defer_no_holder(tc
, cell
);
1311 static void process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
)
1314 int rw
= bio_data_dir(bio
);
1315 dm_block_t block
= get_bio_block(tc
, bio
);
1316 struct dm_thin_lookup_result lookup_result
;
1318 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1321 if (lookup_result
.shared
&& (rw
== WRITE
) && bio
->bi_iter
.bi_size
)
1322 handle_unserviceable_bio(tc
->pool
, bio
);
1324 inc_all_io_entry(tc
->pool
, bio
);
1325 remap_and_issue(tc
, bio
, lookup_result
.block
);
1331 handle_unserviceable_bio(tc
->pool
, bio
);
1335 if (tc
->origin_dev
) {
1336 inc_all_io_entry(tc
->pool
, bio
);
1337 remap_to_origin_and_issue(tc
, bio
);
1346 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1353 static void process_bio_success(struct thin_c
*tc
, struct bio
*bio
)
1358 static void process_bio_fail(struct thin_c
*tc
, struct bio
*bio
)
1364 * FIXME: should we also commit due to size of transaction, measured in
1367 static int need_commit_due_to_time(struct pool
*pool
)
1369 return jiffies
< pool
->last_commit_jiffies
||
1370 jiffies
> pool
->last_commit_jiffies
+ COMMIT_PERIOD
;
1373 #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1374 #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1376 static void __thin_bio_rb_add(struct thin_c
*tc
, struct bio
*bio
)
1378 struct rb_node
**rbp
, *parent
;
1379 struct dm_thin_endio_hook
*pbd
;
1380 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
1382 rbp
= &tc
->sort_bio_list
.rb_node
;
1386 pbd
= thin_pbd(parent
);
1388 if (bi_sector
< thin_bio(pbd
)->bi_iter
.bi_sector
)
1389 rbp
= &(*rbp
)->rb_left
;
1391 rbp
= &(*rbp
)->rb_right
;
1394 pbd
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1395 rb_link_node(&pbd
->rb_node
, parent
, rbp
);
1396 rb_insert_color(&pbd
->rb_node
, &tc
->sort_bio_list
);
1399 static void __extract_sorted_bios(struct thin_c
*tc
)
1401 struct rb_node
*node
;
1402 struct dm_thin_endio_hook
*pbd
;
1405 for (node
= rb_first(&tc
->sort_bio_list
); node
; node
= rb_next(node
)) {
1406 pbd
= thin_pbd(node
);
1407 bio
= thin_bio(pbd
);
1409 bio_list_add(&tc
->deferred_bio_list
, bio
);
1410 rb_erase(&pbd
->rb_node
, &tc
->sort_bio_list
);
1413 WARN_ON(!RB_EMPTY_ROOT(&tc
->sort_bio_list
));
1416 static void __sort_thin_deferred_bios(struct thin_c
*tc
)
1419 struct bio_list bios
;
1421 bio_list_init(&bios
);
1422 bio_list_merge(&bios
, &tc
->deferred_bio_list
);
1423 bio_list_init(&tc
->deferred_bio_list
);
1425 /* Sort deferred_bio_list using rb-tree */
1426 while ((bio
= bio_list_pop(&bios
)))
1427 __thin_bio_rb_add(tc
, bio
);
1430 * Transfer the sorted bios in sort_bio_list back to
1431 * deferred_bio_list to allow lockless submission of
1434 __extract_sorted_bios(tc
);
1437 static void process_thin_deferred_bios(struct thin_c
*tc
)
1439 struct pool
*pool
= tc
->pool
;
1440 unsigned long flags
;
1442 struct bio_list bios
;
1443 struct blk_plug plug
;
1445 if (tc
->requeue_mode
) {
1446 requeue_bio_list(tc
, &tc
->deferred_bio_list
);
1450 bio_list_init(&bios
);
1452 spin_lock_irqsave(&tc
->lock
, flags
);
1454 if (bio_list_empty(&tc
->deferred_bio_list
)) {
1455 spin_unlock_irqrestore(&tc
->lock
, flags
);
1459 __sort_thin_deferred_bios(tc
);
1461 bio_list_merge(&bios
, &tc
->deferred_bio_list
);
1462 bio_list_init(&tc
->deferred_bio_list
);
1464 spin_unlock_irqrestore(&tc
->lock
, flags
);
1466 blk_start_plug(&plug
);
1467 while ((bio
= bio_list_pop(&bios
))) {
1469 * If we've got no free new_mapping structs, and processing
1470 * this bio might require one, we pause until there are some
1471 * prepared mappings to process.
1473 if (ensure_next_mapping(pool
)) {
1474 spin_lock_irqsave(&tc
->lock
, flags
);
1475 bio_list_add(&tc
->deferred_bio_list
, bio
);
1476 bio_list_merge(&tc
->deferred_bio_list
, &bios
);
1477 spin_unlock_irqrestore(&tc
->lock
, flags
);
1481 if (bio
->bi_rw
& REQ_DISCARD
)
1482 pool
->process_discard(tc
, bio
);
1484 pool
->process_bio(tc
, bio
);
1486 blk_finish_plug(&plug
);
1489 static void process_deferred_bios(struct pool
*pool
)
1491 unsigned long flags
;
1493 struct bio_list bios
;
1497 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
)
1498 process_thin_deferred_bios(tc
);
1502 * If there are any deferred flush bios, we must commit
1503 * the metadata before issuing them.
1505 bio_list_init(&bios
);
1506 spin_lock_irqsave(&pool
->lock
, flags
);
1507 bio_list_merge(&bios
, &pool
->deferred_flush_bios
);
1508 bio_list_init(&pool
->deferred_flush_bios
);
1509 spin_unlock_irqrestore(&pool
->lock
, flags
);
1511 if (bio_list_empty(&bios
) &&
1512 !(dm_pool_changed_this_transaction(pool
->pmd
) && need_commit_due_to_time(pool
)))
1516 while ((bio
= bio_list_pop(&bios
)))
1520 pool
->last_commit_jiffies
= jiffies
;
1522 while ((bio
= bio_list_pop(&bios
)))
1523 generic_make_request(bio
);
1526 static void do_worker(struct work_struct
*ws
)
1528 struct pool
*pool
= container_of(ws
, struct pool
, worker
);
1530 process_prepared(pool
, &pool
->prepared_mappings
, &pool
->process_prepared_mapping
);
1531 process_prepared(pool
, &pool
->prepared_discards
, &pool
->process_prepared_discard
);
1532 process_deferred_bios(pool
);
1536 * We want to commit periodically so that not too much
1537 * unwritten data builds up.
1539 static void do_waker(struct work_struct
*ws
)
1541 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
, waker
);
1543 queue_delayed_work(pool
->wq
, &pool
->waker
, COMMIT_PERIOD
);
1546 /*----------------------------------------------------------------*/
1548 struct noflush_work
{
1549 struct work_struct worker
;
1553 wait_queue_head_t wait
;
1556 static void complete_noflush_work(struct noflush_work
*w
)
1558 atomic_set(&w
->complete
, 1);
1562 static void do_noflush_start(struct work_struct
*ws
)
1564 struct noflush_work
*w
= container_of(ws
, struct noflush_work
, worker
);
1565 w
->tc
->requeue_mode
= true;
1567 complete_noflush_work(w
);
1570 static void do_noflush_stop(struct work_struct
*ws
)
1572 struct noflush_work
*w
= container_of(ws
, struct noflush_work
, worker
);
1573 w
->tc
->requeue_mode
= false;
1574 complete_noflush_work(w
);
1577 static void noflush_work(struct thin_c
*tc
, void (*fn
)(struct work_struct
*))
1579 struct noflush_work w
;
1581 INIT_WORK(&w
.worker
, fn
);
1583 atomic_set(&w
.complete
, 0);
1584 init_waitqueue_head(&w
.wait
);
1586 queue_work(tc
->pool
->wq
, &w
.worker
);
1588 wait_event(w
.wait
, atomic_read(&w
.complete
));
1591 /*----------------------------------------------------------------*/
1593 static enum pool_mode
get_pool_mode(struct pool
*pool
)
1595 return pool
->pf
.mode
;
1598 static void notify_of_pool_mode_change(struct pool
*pool
, const char *new_mode
)
1600 dm_table_event(pool
->ti
->table
);
1601 DMINFO("%s: switching pool to %s mode",
1602 dm_device_name(pool
->pool_md
), new_mode
);
1605 static void set_pool_mode(struct pool
*pool
, enum pool_mode new_mode
)
1607 struct pool_c
*pt
= pool
->ti
->private;
1608 bool needs_check
= dm_pool_metadata_needs_check(pool
->pmd
);
1609 enum pool_mode old_mode
= get_pool_mode(pool
);
1612 * Never allow the pool to transition to PM_WRITE mode if user
1613 * intervention is required to verify metadata and data consistency.
1615 if (new_mode
== PM_WRITE
&& needs_check
) {
1616 DMERR("%s: unable to switch pool to write mode until repaired.",
1617 dm_device_name(pool
->pool_md
));
1618 if (old_mode
!= new_mode
)
1619 new_mode
= old_mode
;
1621 new_mode
= PM_READ_ONLY
;
1624 * If we were in PM_FAIL mode, rollback of metadata failed. We're
1625 * not going to recover without a thin_repair. So we never let the
1626 * pool move out of the old mode.
1628 if (old_mode
== PM_FAIL
)
1629 new_mode
= old_mode
;
1633 if (old_mode
!= new_mode
)
1634 notify_of_pool_mode_change(pool
, "failure");
1635 dm_pool_metadata_read_only(pool
->pmd
);
1636 pool
->process_bio
= process_bio_fail
;
1637 pool
->process_discard
= process_bio_fail
;
1638 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1639 pool
->process_prepared_discard
= process_prepared_discard_fail
;
1641 error_retry_list(pool
);
1645 if (old_mode
!= new_mode
)
1646 notify_of_pool_mode_change(pool
, "read-only");
1647 dm_pool_metadata_read_only(pool
->pmd
);
1648 pool
->process_bio
= process_bio_read_only
;
1649 pool
->process_discard
= process_bio_success
;
1650 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1651 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1653 error_retry_list(pool
);
1656 case PM_OUT_OF_DATA_SPACE
:
1658 * Ideally we'd never hit this state; the low water mark
1659 * would trigger userland to extend the pool before we
1660 * completely run out of data space. However, many small
1661 * IOs to unprovisioned space can consume data space at an
1662 * alarming rate. Adjust your low water mark if you're
1663 * frequently seeing this mode.
1665 if (old_mode
!= new_mode
)
1666 notify_of_pool_mode_change(pool
, "out-of-data-space");
1667 pool
->process_bio
= process_bio_read_only
;
1668 pool
->process_discard
= process_discard
;
1669 pool
->process_prepared_mapping
= process_prepared_mapping
;
1670 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1674 if (old_mode
!= new_mode
)
1675 notify_of_pool_mode_change(pool
, "write");
1676 dm_pool_metadata_read_write(pool
->pmd
);
1677 pool
->process_bio
= process_bio
;
1678 pool
->process_discard
= process_discard
;
1679 pool
->process_prepared_mapping
= process_prepared_mapping
;
1680 pool
->process_prepared_discard
= process_prepared_discard
;
1684 pool
->pf
.mode
= new_mode
;
1686 * The pool mode may have changed, sync it so bind_control_target()
1687 * doesn't cause an unexpected mode transition on resume.
1689 pt
->adjusted_pf
.mode
= new_mode
;
1692 static void abort_transaction(struct pool
*pool
)
1694 const char *dev_name
= dm_device_name(pool
->pool_md
);
1696 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name
);
1697 if (dm_pool_abort_metadata(pool
->pmd
)) {
1698 DMERR("%s: failed to abort metadata transaction", dev_name
);
1699 set_pool_mode(pool
, PM_FAIL
);
1702 if (dm_pool_metadata_set_needs_check(pool
->pmd
)) {
1703 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name
);
1704 set_pool_mode(pool
, PM_FAIL
);
1708 static void metadata_operation_failed(struct pool
*pool
, const char *op
, int r
)
1710 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1711 dm_device_name(pool
->pool_md
), op
, r
);
1713 abort_transaction(pool
);
1714 set_pool_mode(pool
, PM_READ_ONLY
);
1717 /*----------------------------------------------------------------*/
1720 * Mapping functions.
1724 * Called only while mapping a thin bio to hand it over to the workqueue.
1726 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
)
1728 unsigned long flags
;
1729 struct pool
*pool
= tc
->pool
;
1731 spin_lock_irqsave(&tc
->lock
, flags
);
1732 bio_list_add(&tc
->deferred_bio_list
, bio
);
1733 spin_unlock_irqrestore(&tc
->lock
, flags
);
1738 static void thin_hook_bio(struct thin_c
*tc
, struct bio
*bio
)
1740 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1743 h
->shared_read_entry
= NULL
;
1744 h
->all_io_entry
= NULL
;
1745 h
->overwrite_mapping
= NULL
;
1749 * Non-blocking function called from the thin target's map function.
1751 static int thin_bio_map(struct dm_target
*ti
, struct bio
*bio
)
1754 struct thin_c
*tc
= ti
->private;
1755 dm_block_t block
= get_bio_block(tc
, bio
);
1756 struct dm_thin_device
*td
= tc
->td
;
1757 struct dm_thin_lookup_result result
;
1758 struct dm_bio_prison_cell cell1
, cell2
;
1759 struct dm_bio_prison_cell
*cell_result
;
1760 struct dm_cell_key key
;
1762 thin_hook_bio(tc
, bio
);
1764 if (tc
->requeue_mode
) {
1765 bio_endio(bio
, DM_ENDIO_REQUEUE
);
1766 return DM_MAPIO_SUBMITTED
;
1769 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
1771 return DM_MAPIO_SUBMITTED
;
1774 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)) {
1775 thin_defer_bio(tc
, bio
);
1776 return DM_MAPIO_SUBMITTED
;
1779 r
= dm_thin_find_block(td
, block
, 0, &result
);
1782 * Note that we defer readahead too.
1786 if (unlikely(result
.shared
)) {
1788 * We have a race condition here between the
1789 * result.shared value returned by the lookup and
1790 * snapshot creation, which may cause new
1793 * To avoid this always quiesce the origin before
1794 * taking the snap. You want to do this anyway to
1795 * ensure a consistent application view
1798 * More distant ancestors are irrelevant. The
1799 * shared flag will be set in their case.
1801 thin_defer_bio(tc
, bio
);
1802 return DM_MAPIO_SUBMITTED
;
1805 build_virtual_key(tc
->td
, block
, &key
);
1806 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell1
, &cell_result
))
1807 return DM_MAPIO_SUBMITTED
;
1809 build_data_key(tc
->td
, result
.block
, &key
);
1810 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell2
, &cell_result
)) {
1811 cell_defer_no_holder_no_free(tc
, &cell1
);
1812 return DM_MAPIO_SUBMITTED
;
1815 inc_all_io_entry(tc
->pool
, bio
);
1816 cell_defer_no_holder_no_free(tc
, &cell2
);
1817 cell_defer_no_holder_no_free(tc
, &cell1
);
1819 remap(tc
, bio
, result
.block
);
1820 return DM_MAPIO_REMAPPED
;
1823 if (get_pool_mode(tc
->pool
) == PM_READ_ONLY
) {
1825 * This block isn't provisioned, and we have no way
1828 handle_unserviceable_bio(tc
->pool
, bio
);
1829 return DM_MAPIO_SUBMITTED
;
1835 * In future, the failed dm_thin_find_block above could
1836 * provide the hint to load the metadata into cache.
1838 thin_defer_bio(tc
, bio
);
1839 return DM_MAPIO_SUBMITTED
;
1843 * Must always call bio_io_error on failure.
1844 * dm_thin_find_block can fail with -EINVAL if the
1845 * pool is switched to fail-io mode.
1848 return DM_MAPIO_SUBMITTED
;
1852 static int pool_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
1854 struct pool_c
*pt
= container_of(cb
, struct pool_c
, callbacks
);
1855 struct request_queue
*q
;
1857 if (get_pool_mode(pt
->pool
) == PM_OUT_OF_DATA_SPACE
)
1860 q
= bdev_get_queue(pt
->data_dev
->bdev
);
1861 return bdi_congested(&q
->backing_dev_info
, bdi_bits
);
1864 static void requeue_bios(struct pool
*pool
)
1866 unsigned long flags
;
1870 list_for_each_entry_rcu(tc
, &pool
->active_thins
, list
) {
1871 spin_lock_irqsave(&tc
->lock
, flags
);
1872 bio_list_merge(&tc
->deferred_bio_list
, &tc
->retry_on_resume_list
);
1873 bio_list_init(&tc
->retry_on_resume_list
);
1874 spin_unlock_irqrestore(&tc
->lock
, flags
);
1879 /*----------------------------------------------------------------
1880 * Binding of control targets to a pool object
1881 *--------------------------------------------------------------*/
1882 static bool data_dev_supports_discard(struct pool_c
*pt
)
1884 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1886 return q
&& blk_queue_discard(q
);
1889 static bool is_factor(sector_t block_size
, uint32_t n
)
1891 return !sector_div(block_size
, n
);
1895 * If discard_passdown was enabled verify that the data device
1896 * supports discards. Disable discard_passdown if not.
1898 static void disable_passdown_if_not_supported(struct pool_c
*pt
)
1900 struct pool
*pool
= pt
->pool
;
1901 struct block_device
*data_bdev
= pt
->data_dev
->bdev
;
1902 struct queue_limits
*data_limits
= &bdev_get_queue(data_bdev
)->limits
;
1903 sector_t block_size
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
1904 const char *reason
= NULL
;
1905 char buf
[BDEVNAME_SIZE
];
1907 if (!pt
->adjusted_pf
.discard_passdown
)
1910 if (!data_dev_supports_discard(pt
))
1911 reason
= "discard unsupported";
1913 else if (data_limits
->max_discard_sectors
< pool
->sectors_per_block
)
1914 reason
= "max discard sectors smaller than a block";
1916 else if (data_limits
->discard_granularity
> block_size
)
1917 reason
= "discard granularity larger than a block";
1919 else if (!is_factor(block_size
, data_limits
->discard_granularity
))
1920 reason
= "discard granularity not a factor of block size";
1923 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev
, buf
), reason
);
1924 pt
->adjusted_pf
.discard_passdown
= false;
1928 static int bind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1930 struct pool_c
*pt
= ti
->private;
1933 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
1935 enum pool_mode old_mode
= get_pool_mode(pool
);
1936 enum pool_mode new_mode
= pt
->adjusted_pf
.mode
;
1939 * Don't change the pool's mode until set_pool_mode() below.
1940 * Otherwise the pool's process_* function pointers may
1941 * not match the desired pool mode.
1943 pt
->adjusted_pf
.mode
= old_mode
;
1946 pool
->pf
= pt
->adjusted_pf
;
1947 pool
->low_water_blocks
= pt
->low_water_blocks
;
1949 set_pool_mode(pool
, new_mode
);
1954 static void unbind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1960 /*----------------------------------------------------------------
1962 *--------------------------------------------------------------*/
1963 /* Initialize pool features. */
1964 static void pool_features_init(struct pool_features
*pf
)
1966 pf
->mode
= PM_WRITE
;
1967 pf
->zero_new_blocks
= true;
1968 pf
->discard_enabled
= true;
1969 pf
->discard_passdown
= true;
1970 pf
->error_if_no_space
= false;
1973 static void __pool_destroy(struct pool
*pool
)
1975 __pool_table_remove(pool
);
1977 if (dm_pool_metadata_close(pool
->pmd
) < 0)
1978 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
1980 dm_bio_prison_destroy(pool
->prison
);
1981 dm_kcopyd_client_destroy(pool
->copier
);
1984 destroy_workqueue(pool
->wq
);
1986 if (pool
->next_mapping
)
1987 mempool_free(pool
->next_mapping
, pool
->mapping_pool
);
1988 mempool_destroy(pool
->mapping_pool
);
1989 dm_deferred_set_destroy(pool
->shared_read_ds
);
1990 dm_deferred_set_destroy(pool
->all_io_ds
);
1994 static struct kmem_cache
*_new_mapping_cache
;
1996 static struct pool
*pool_create(struct mapped_device
*pool_md
,
1997 struct block_device
*metadata_dev
,
1998 unsigned long block_size
,
1999 int read_only
, char **error
)
2004 struct dm_pool_metadata
*pmd
;
2005 bool format_device
= read_only
? false : true;
2007 pmd
= dm_pool_metadata_open(metadata_dev
, block_size
, format_device
);
2009 *error
= "Error creating metadata object";
2010 return (struct pool
*)pmd
;
2013 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
2015 *error
= "Error allocating memory for pool";
2016 err_p
= ERR_PTR(-ENOMEM
);
2021 pool
->sectors_per_block
= block_size
;
2022 if (block_size
& (block_size
- 1))
2023 pool
->sectors_per_block_shift
= -1;
2025 pool
->sectors_per_block_shift
= __ffs(block_size
);
2026 pool
->low_water_blocks
= 0;
2027 pool_features_init(&pool
->pf
);
2028 pool
->prison
= dm_bio_prison_create(PRISON_CELLS
);
2029 if (!pool
->prison
) {
2030 *error
= "Error creating pool's bio prison";
2031 err_p
= ERR_PTR(-ENOMEM
);
2035 pool
->copier
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2036 if (IS_ERR(pool
->copier
)) {
2037 r
= PTR_ERR(pool
->copier
);
2038 *error
= "Error creating pool's kcopyd client";
2040 goto bad_kcopyd_client
;
2044 * Create singlethreaded workqueue that will service all devices
2045 * that use this metadata.
2047 pool
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
2049 *error
= "Error creating pool's workqueue";
2050 err_p
= ERR_PTR(-ENOMEM
);
2054 INIT_WORK(&pool
->worker
, do_worker
);
2055 INIT_DELAYED_WORK(&pool
->waker
, do_waker
);
2056 spin_lock_init(&pool
->lock
);
2057 bio_list_init(&pool
->deferred_flush_bios
);
2058 INIT_LIST_HEAD(&pool
->prepared_mappings
);
2059 INIT_LIST_HEAD(&pool
->prepared_discards
);
2060 INIT_LIST_HEAD(&pool
->active_thins
);
2061 pool
->low_water_triggered
= false;
2063 pool
->shared_read_ds
= dm_deferred_set_create();
2064 if (!pool
->shared_read_ds
) {
2065 *error
= "Error creating pool's shared read deferred set";
2066 err_p
= ERR_PTR(-ENOMEM
);
2067 goto bad_shared_read_ds
;
2070 pool
->all_io_ds
= dm_deferred_set_create();
2071 if (!pool
->all_io_ds
) {
2072 *error
= "Error creating pool's all io deferred set";
2073 err_p
= ERR_PTR(-ENOMEM
);
2077 pool
->next_mapping
= NULL
;
2078 pool
->mapping_pool
= mempool_create_slab_pool(MAPPING_POOL_SIZE
,
2079 _new_mapping_cache
);
2080 if (!pool
->mapping_pool
) {
2081 *error
= "Error creating pool's mapping mempool";
2082 err_p
= ERR_PTR(-ENOMEM
);
2083 goto bad_mapping_pool
;
2086 pool
->ref_count
= 1;
2087 pool
->last_commit_jiffies
= jiffies
;
2088 pool
->pool_md
= pool_md
;
2089 pool
->md_dev
= metadata_dev
;
2090 __pool_table_insert(pool
);
2095 dm_deferred_set_destroy(pool
->all_io_ds
);
2097 dm_deferred_set_destroy(pool
->shared_read_ds
);
2099 destroy_workqueue(pool
->wq
);
2101 dm_kcopyd_client_destroy(pool
->copier
);
2103 dm_bio_prison_destroy(pool
->prison
);
2107 if (dm_pool_metadata_close(pmd
))
2108 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
2113 static void __pool_inc(struct pool
*pool
)
2115 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2119 static void __pool_dec(struct pool
*pool
)
2121 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2122 BUG_ON(!pool
->ref_count
);
2123 if (!--pool
->ref_count
)
2124 __pool_destroy(pool
);
2127 static struct pool
*__pool_find(struct mapped_device
*pool_md
,
2128 struct block_device
*metadata_dev
,
2129 unsigned long block_size
, int read_only
,
2130 char **error
, int *created
)
2132 struct pool
*pool
= __pool_table_lookup_metadata_dev(metadata_dev
);
2135 if (pool
->pool_md
!= pool_md
) {
2136 *error
= "metadata device already in use by a pool";
2137 return ERR_PTR(-EBUSY
);
2142 pool
= __pool_table_lookup(pool_md
);
2144 if (pool
->md_dev
!= metadata_dev
) {
2145 *error
= "different pool cannot replace a pool";
2146 return ERR_PTR(-EINVAL
);
2151 pool
= pool_create(pool_md
, metadata_dev
, block_size
, read_only
, error
);
2159 /*----------------------------------------------------------------
2160 * Pool target methods
2161 *--------------------------------------------------------------*/
2162 static void pool_dtr(struct dm_target
*ti
)
2164 struct pool_c
*pt
= ti
->private;
2166 mutex_lock(&dm_thin_pool_table
.mutex
);
2168 unbind_control_target(pt
->pool
, ti
);
2169 __pool_dec(pt
->pool
);
2170 dm_put_device(ti
, pt
->metadata_dev
);
2171 dm_put_device(ti
, pt
->data_dev
);
2174 mutex_unlock(&dm_thin_pool_table
.mutex
);
2177 static int parse_pool_features(struct dm_arg_set
*as
, struct pool_features
*pf
,
2178 struct dm_target
*ti
)
2182 const char *arg_name
;
2184 static struct dm_arg _args
[] = {
2185 {0, 4, "Invalid number of pool feature arguments"},
2189 * No feature arguments supplied.
2194 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
2198 while (argc
&& !r
) {
2199 arg_name
= dm_shift_arg(as
);
2202 if (!strcasecmp(arg_name
, "skip_block_zeroing"))
2203 pf
->zero_new_blocks
= false;
2205 else if (!strcasecmp(arg_name
, "ignore_discard"))
2206 pf
->discard_enabled
= false;
2208 else if (!strcasecmp(arg_name
, "no_discard_passdown"))
2209 pf
->discard_passdown
= false;
2211 else if (!strcasecmp(arg_name
, "read_only"))
2212 pf
->mode
= PM_READ_ONLY
;
2214 else if (!strcasecmp(arg_name
, "error_if_no_space"))
2215 pf
->error_if_no_space
= true;
2218 ti
->error
= "Unrecognised pool feature requested";
2227 static void metadata_low_callback(void *context
)
2229 struct pool
*pool
= context
;
2231 DMWARN("%s: reached low water mark for metadata device: sending event.",
2232 dm_device_name(pool
->pool_md
));
2234 dm_table_event(pool
->ti
->table
);
2237 static sector_t
get_dev_size(struct block_device
*bdev
)
2239 return i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
;
2242 static void warn_if_metadata_device_too_big(struct block_device
*bdev
)
2244 sector_t metadata_dev_size
= get_dev_size(bdev
);
2245 char buffer
[BDEVNAME_SIZE
];
2247 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS_WARNING
)
2248 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2249 bdevname(bdev
, buffer
), THIN_METADATA_MAX_SECTORS
);
2252 static sector_t
get_metadata_dev_size(struct block_device
*bdev
)
2254 sector_t metadata_dev_size
= get_dev_size(bdev
);
2256 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS
)
2257 metadata_dev_size
= THIN_METADATA_MAX_SECTORS
;
2259 return metadata_dev_size
;
2262 static dm_block_t
get_metadata_dev_size_in_blocks(struct block_device
*bdev
)
2264 sector_t metadata_dev_size
= get_metadata_dev_size(bdev
);
2266 sector_div(metadata_dev_size
, THIN_METADATA_BLOCK_SIZE
);
2268 return metadata_dev_size
;
2272 * When a metadata threshold is crossed a dm event is triggered, and
2273 * userland should respond by growing the metadata device. We could let
2274 * userland set the threshold, like we do with the data threshold, but I'm
2275 * not sure they know enough to do this well.
2277 static dm_block_t
calc_metadata_threshold(struct pool_c
*pt
)
2280 * 4M is ample for all ops with the possible exception of thin
2281 * device deletion which is harmless if it fails (just retry the
2282 * delete after you've grown the device).
2284 dm_block_t quarter
= get_metadata_dev_size_in_blocks(pt
->metadata_dev
->bdev
) / 4;
2285 return min((dm_block_t
)1024ULL /* 4M */, quarter
);
2289 * thin-pool <metadata dev> <data dev>
2290 * <data block size (sectors)>
2291 * <low water mark (blocks)>
2292 * [<#feature args> [<arg>]*]
2294 * Optional feature arguments are:
2295 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
2296 * ignore_discard: disable discard
2297 * no_discard_passdown: don't pass discards down to the data device
2298 * read_only: Don't allow any changes to be made to the pool metadata.
2299 * error_if_no_space: error IOs, instead of queueing, if no space.
2301 static int pool_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2303 int r
, pool_created
= 0;
2306 struct pool_features pf
;
2307 struct dm_arg_set as
;
2308 struct dm_dev
*data_dev
;
2309 unsigned long block_size
;
2310 dm_block_t low_water_blocks
;
2311 struct dm_dev
*metadata_dev
;
2312 fmode_t metadata_mode
;
2315 * FIXME Remove validation from scope of lock.
2317 mutex_lock(&dm_thin_pool_table
.mutex
);
2320 ti
->error
= "Invalid argument count";
2329 * Set default pool features.
2331 pool_features_init(&pf
);
2333 dm_consume_args(&as
, 4);
2334 r
= parse_pool_features(&as
, &pf
, ti
);
2338 metadata_mode
= FMODE_READ
| ((pf
.mode
== PM_READ_ONLY
) ? 0 : FMODE_WRITE
);
2339 r
= dm_get_device(ti
, argv
[0], metadata_mode
, &metadata_dev
);
2341 ti
->error
= "Error opening metadata block device";
2344 warn_if_metadata_device_too_big(metadata_dev
->bdev
);
2346 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &data_dev
);
2348 ti
->error
= "Error getting data device";
2352 if (kstrtoul(argv
[2], 10, &block_size
) || !block_size
||
2353 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
2354 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
2355 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
2356 ti
->error
= "Invalid block size";
2361 if (kstrtoull(argv
[3], 10, (unsigned long long *)&low_water_blocks
)) {
2362 ti
->error
= "Invalid low water mark";
2367 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
2373 pool
= __pool_find(dm_table_get_md(ti
->table
), metadata_dev
->bdev
,
2374 block_size
, pf
.mode
== PM_READ_ONLY
, &ti
->error
, &pool_created
);
2381 * 'pool_created' reflects whether this is the first table load.
2382 * Top level discard support is not allowed to be changed after
2383 * initial load. This would require a pool reload to trigger thin
2386 if (!pool_created
&& pf
.discard_enabled
!= pool
->pf
.discard_enabled
) {
2387 ti
->error
= "Discard support cannot be disabled once enabled";
2389 goto out_flags_changed
;
2394 pt
->metadata_dev
= metadata_dev
;
2395 pt
->data_dev
= data_dev
;
2396 pt
->low_water_blocks
= low_water_blocks
;
2397 pt
->adjusted_pf
= pt
->requested_pf
= pf
;
2398 ti
->num_flush_bios
= 1;
2401 * Only need to enable discards if the pool should pass
2402 * them down to the data device. The thin device's discard
2403 * processing will cause mappings to be removed from the btree.
2405 ti
->discard_zeroes_data_unsupported
= true;
2406 if (pf
.discard_enabled
&& pf
.discard_passdown
) {
2407 ti
->num_discard_bios
= 1;
2410 * Setting 'discards_supported' circumvents the normal
2411 * stacking of discard limits (this keeps the pool and
2412 * thin devices' discard limits consistent).
2414 ti
->discards_supported
= true;
2418 r
= dm_pool_register_metadata_threshold(pt
->pool
->pmd
,
2419 calc_metadata_threshold(pt
),
2420 metadata_low_callback
,
2425 pt
->callbacks
.congested_fn
= pool_is_congested
;
2426 dm_table_add_target_callbacks(ti
->table
, &pt
->callbacks
);
2428 mutex_unlock(&dm_thin_pool_table
.mutex
);
2437 dm_put_device(ti
, data_dev
);
2439 dm_put_device(ti
, metadata_dev
);
2441 mutex_unlock(&dm_thin_pool_table
.mutex
);
2446 static int pool_map(struct dm_target
*ti
, struct bio
*bio
)
2449 struct pool_c
*pt
= ti
->private;
2450 struct pool
*pool
= pt
->pool
;
2451 unsigned long flags
;
2454 * As this is a singleton target, ti->begin is always zero.
2456 spin_lock_irqsave(&pool
->lock
, flags
);
2457 bio
->bi_bdev
= pt
->data_dev
->bdev
;
2458 r
= DM_MAPIO_REMAPPED
;
2459 spin_unlock_irqrestore(&pool
->lock
, flags
);
2464 static int maybe_resize_data_dev(struct dm_target
*ti
, bool *need_commit
)
2467 struct pool_c
*pt
= ti
->private;
2468 struct pool
*pool
= pt
->pool
;
2469 sector_t data_size
= ti
->len
;
2470 dm_block_t sb_data_size
;
2472 *need_commit
= false;
2474 (void) sector_div(data_size
, pool
->sectors_per_block
);
2476 r
= dm_pool_get_data_dev_size(pool
->pmd
, &sb_data_size
);
2478 DMERR("%s: failed to retrieve data device size",
2479 dm_device_name(pool
->pool_md
));
2483 if (data_size
< sb_data_size
) {
2484 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2485 dm_device_name(pool
->pool_md
),
2486 (unsigned long long)data_size
, sb_data_size
);
2489 } else if (data_size
> sb_data_size
) {
2490 if (dm_pool_metadata_needs_check(pool
->pmd
)) {
2491 DMERR("%s: unable to grow the data device until repaired.",
2492 dm_device_name(pool
->pool_md
));
2497 DMINFO("%s: growing the data device from %llu to %llu blocks",
2498 dm_device_name(pool
->pool_md
),
2499 sb_data_size
, (unsigned long long)data_size
);
2500 r
= dm_pool_resize_data_dev(pool
->pmd
, data_size
);
2502 metadata_operation_failed(pool
, "dm_pool_resize_data_dev", r
);
2506 *need_commit
= true;
2512 static int maybe_resize_metadata_dev(struct dm_target
*ti
, bool *need_commit
)
2515 struct pool_c
*pt
= ti
->private;
2516 struct pool
*pool
= pt
->pool
;
2517 dm_block_t metadata_dev_size
, sb_metadata_dev_size
;
2519 *need_commit
= false;
2521 metadata_dev_size
= get_metadata_dev_size_in_blocks(pool
->md_dev
);
2523 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &sb_metadata_dev_size
);
2525 DMERR("%s: failed to retrieve metadata device size",
2526 dm_device_name(pool
->pool_md
));
2530 if (metadata_dev_size
< sb_metadata_dev_size
) {
2531 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2532 dm_device_name(pool
->pool_md
),
2533 metadata_dev_size
, sb_metadata_dev_size
);
2536 } else if (metadata_dev_size
> sb_metadata_dev_size
) {
2537 if (dm_pool_metadata_needs_check(pool
->pmd
)) {
2538 DMERR("%s: unable to grow the metadata device until repaired.",
2539 dm_device_name(pool
->pool_md
));
2543 warn_if_metadata_device_too_big(pool
->md_dev
);
2544 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2545 dm_device_name(pool
->pool_md
),
2546 sb_metadata_dev_size
, metadata_dev_size
);
2547 r
= dm_pool_resize_metadata_dev(pool
->pmd
, metadata_dev_size
);
2549 metadata_operation_failed(pool
, "dm_pool_resize_metadata_dev", r
);
2553 *need_commit
= true;
2560 * Retrieves the number of blocks of the data device from
2561 * the superblock and compares it to the actual device size,
2562 * thus resizing the data device in case it has grown.
2564 * This both copes with opening preallocated data devices in the ctr
2565 * being followed by a resume
2567 * calling the resume method individually after userspace has
2568 * grown the data device in reaction to a table event.
2570 static int pool_preresume(struct dm_target
*ti
)
2573 bool need_commit1
, need_commit2
;
2574 struct pool_c
*pt
= ti
->private;
2575 struct pool
*pool
= pt
->pool
;
2578 * Take control of the pool object.
2580 r
= bind_control_target(pool
, ti
);
2584 r
= maybe_resize_data_dev(ti
, &need_commit1
);
2588 r
= maybe_resize_metadata_dev(ti
, &need_commit2
);
2592 if (need_commit1
|| need_commit2
)
2593 (void) commit(pool
);
2598 static void pool_resume(struct dm_target
*ti
)
2600 struct pool_c
*pt
= ti
->private;
2601 struct pool
*pool
= pt
->pool
;
2602 unsigned long flags
;
2604 spin_lock_irqsave(&pool
->lock
, flags
);
2605 pool
->low_water_triggered
= false;
2606 spin_unlock_irqrestore(&pool
->lock
, flags
);
2609 do_waker(&pool
->waker
.work
);
2612 static void pool_postsuspend(struct dm_target
*ti
)
2614 struct pool_c
*pt
= ti
->private;
2615 struct pool
*pool
= pt
->pool
;
2617 cancel_delayed_work(&pool
->waker
);
2618 flush_workqueue(pool
->wq
);
2619 (void) commit(pool
);
2622 static int check_arg_count(unsigned argc
, unsigned args_required
)
2624 if (argc
!= args_required
) {
2625 DMWARN("Message received with %u arguments instead of %u.",
2626 argc
, args_required
);
2633 static int read_dev_id(char *arg
, dm_thin_id
*dev_id
, int warning
)
2635 if (!kstrtoull(arg
, 10, (unsigned long long *)dev_id
) &&
2636 *dev_id
<= MAX_DEV_ID
)
2640 DMWARN("Message received with invalid device id: %s", arg
);
2645 static int process_create_thin_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2650 r
= check_arg_count(argc
, 2);
2654 r
= read_dev_id(argv
[1], &dev_id
, 1);
2658 r
= dm_pool_create_thin(pool
->pmd
, dev_id
);
2660 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2668 static int process_create_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2671 dm_thin_id origin_dev_id
;
2674 r
= check_arg_count(argc
, 3);
2678 r
= read_dev_id(argv
[1], &dev_id
, 1);
2682 r
= read_dev_id(argv
[2], &origin_dev_id
, 1);
2686 r
= dm_pool_create_snap(pool
->pmd
, dev_id
, origin_dev_id
);
2688 DMWARN("Creation of new snapshot %s of device %s failed.",
2696 static int process_delete_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2701 r
= check_arg_count(argc
, 2);
2705 r
= read_dev_id(argv
[1], &dev_id
, 1);
2709 r
= dm_pool_delete_thin_device(pool
->pmd
, dev_id
);
2711 DMWARN("Deletion of thin device %s failed.", argv
[1]);
2716 static int process_set_transaction_id_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2718 dm_thin_id old_id
, new_id
;
2721 r
= check_arg_count(argc
, 3);
2725 if (kstrtoull(argv
[1], 10, (unsigned long long *)&old_id
)) {
2726 DMWARN("set_transaction_id message: Unrecognised id %s.", argv
[1]);
2730 if (kstrtoull(argv
[2], 10, (unsigned long long *)&new_id
)) {
2731 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv
[2]);
2735 r
= dm_pool_set_metadata_transaction_id(pool
->pmd
, old_id
, new_id
);
2737 DMWARN("Failed to change transaction id from %s to %s.",
2745 static int process_reserve_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2749 r
= check_arg_count(argc
, 1);
2753 (void) commit(pool
);
2755 r
= dm_pool_reserve_metadata_snap(pool
->pmd
);
2757 DMWARN("reserve_metadata_snap message failed.");
2762 static int process_release_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2766 r
= check_arg_count(argc
, 1);
2770 r
= dm_pool_release_metadata_snap(pool
->pmd
);
2772 DMWARN("release_metadata_snap message failed.");
2778 * Messages supported:
2779 * create_thin <dev_id>
2780 * create_snap <dev_id> <origin_id>
2782 * trim <dev_id> <new_size_in_sectors>
2783 * set_transaction_id <current_trans_id> <new_trans_id>
2784 * reserve_metadata_snap
2785 * release_metadata_snap
2787 static int pool_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
2790 struct pool_c
*pt
= ti
->private;
2791 struct pool
*pool
= pt
->pool
;
2793 if (!strcasecmp(argv
[0], "create_thin"))
2794 r
= process_create_thin_mesg(argc
, argv
, pool
);
2796 else if (!strcasecmp(argv
[0], "create_snap"))
2797 r
= process_create_snap_mesg(argc
, argv
, pool
);
2799 else if (!strcasecmp(argv
[0], "delete"))
2800 r
= process_delete_mesg(argc
, argv
, pool
);
2802 else if (!strcasecmp(argv
[0], "set_transaction_id"))
2803 r
= process_set_transaction_id_mesg(argc
, argv
, pool
);
2805 else if (!strcasecmp(argv
[0], "reserve_metadata_snap"))
2806 r
= process_reserve_metadata_snap_mesg(argc
, argv
, pool
);
2808 else if (!strcasecmp(argv
[0], "release_metadata_snap"))
2809 r
= process_release_metadata_snap_mesg(argc
, argv
, pool
);
2812 DMWARN("Unrecognised thin pool target message received: %s", argv
[0]);
2815 (void) commit(pool
);
2820 static void emit_flags(struct pool_features
*pf
, char *result
,
2821 unsigned sz
, unsigned maxlen
)
2823 unsigned count
= !pf
->zero_new_blocks
+ !pf
->discard_enabled
+
2824 !pf
->discard_passdown
+ (pf
->mode
== PM_READ_ONLY
) +
2825 pf
->error_if_no_space
;
2826 DMEMIT("%u ", count
);
2828 if (!pf
->zero_new_blocks
)
2829 DMEMIT("skip_block_zeroing ");
2831 if (!pf
->discard_enabled
)
2832 DMEMIT("ignore_discard ");
2834 if (!pf
->discard_passdown
)
2835 DMEMIT("no_discard_passdown ");
2837 if (pf
->mode
== PM_READ_ONLY
)
2838 DMEMIT("read_only ");
2840 if (pf
->error_if_no_space
)
2841 DMEMIT("error_if_no_space ");
2846 * <transaction id> <used metadata sectors>/<total metadata sectors>
2847 * <used data sectors>/<total data sectors> <held metadata root>
2849 static void pool_status(struct dm_target
*ti
, status_type_t type
,
2850 unsigned status_flags
, char *result
, unsigned maxlen
)
2854 uint64_t transaction_id
;
2855 dm_block_t nr_free_blocks_data
;
2856 dm_block_t nr_free_blocks_metadata
;
2857 dm_block_t nr_blocks_data
;
2858 dm_block_t nr_blocks_metadata
;
2859 dm_block_t held_root
;
2860 char buf
[BDEVNAME_SIZE
];
2861 char buf2
[BDEVNAME_SIZE
];
2862 struct pool_c
*pt
= ti
->private;
2863 struct pool
*pool
= pt
->pool
;
2866 case STATUSTYPE_INFO
:
2867 if (get_pool_mode(pool
) == PM_FAIL
) {
2872 /* Commit to ensure statistics aren't out-of-date */
2873 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
))
2874 (void) commit(pool
);
2876 r
= dm_pool_get_metadata_transaction_id(pool
->pmd
, &transaction_id
);
2878 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
2879 dm_device_name(pool
->pool_md
), r
);
2883 r
= dm_pool_get_free_metadata_block_count(pool
->pmd
, &nr_free_blocks_metadata
);
2885 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
2886 dm_device_name(pool
->pool_md
), r
);
2890 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &nr_blocks_metadata
);
2892 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
2893 dm_device_name(pool
->pool_md
), r
);
2897 r
= dm_pool_get_free_block_count(pool
->pmd
, &nr_free_blocks_data
);
2899 DMERR("%s: dm_pool_get_free_block_count returned %d",
2900 dm_device_name(pool
->pool_md
), r
);
2904 r
= dm_pool_get_data_dev_size(pool
->pmd
, &nr_blocks_data
);
2906 DMERR("%s: dm_pool_get_data_dev_size returned %d",
2907 dm_device_name(pool
->pool_md
), r
);
2911 r
= dm_pool_get_metadata_snap(pool
->pmd
, &held_root
);
2913 DMERR("%s: dm_pool_get_metadata_snap returned %d",
2914 dm_device_name(pool
->pool_md
), r
);
2918 DMEMIT("%llu %llu/%llu %llu/%llu ",
2919 (unsigned long long)transaction_id
,
2920 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
2921 (unsigned long long)nr_blocks_metadata
,
2922 (unsigned long long)(nr_blocks_data
- nr_free_blocks_data
),
2923 (unsigned long long)nr_blocks_data
);
2926 DMEMIT("%llu ", held_root
);
2930 if (pool
->pf
.mode
== PM_OUT_OF_DATA_SPACE
)
2931 DMEMIT("out_of_data_space ");
2932 else if (pool
->pf
.mode
== PM_READ_ONLY
)
2937 if (!pool
->pf
.discard_enabled
)
2938 DMEMIT("ignore_discard ");
2939 else if (pool
->pf
.discard_passdown
)
2940 DMEMIT("discard_passdown ");
2942 DMEMIT("no_discard_passdown ");
2944 if (pool
->pf
.error_if_no_space
)
2945 DMEMIT("error_if_no_space ");
2947 DMEMIT("queue_if_no_space ");
2951 case STATUSTYPE_TABLE
:
2952 DMEMIT("%s %s %lu %llu ",
2953 format_dev_t(buf
, pt
->metadata_dev
->bdev
->bd_dev
),
2954 format_dev_t(buf2
, pt
->data_dev
->bdev
->bd_dev
),
2955 (unsigned long)pool
->sectors_per_block
,
2956 (unsigned long long)pt
->low_water_blocks
);
2957 emit_flags(&pt
->requested_pf
, result
, sz
, maxlen
);
2966 static int pool_iterate_devices(struct dm_target
*ti
,
2967 iterate_devices_callout_fn fn
, void *data
)
2969 struct pool_c
*pt
= ti
->private;
2971 return fn(ti
, pt
->data_dev
, 0, ti
->len
, data
);
2974 static int pool_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
2975 struct bio_vec
*biovec
, int max_size
)
2977 struct pool_c
*pt
= ti
->private;
2978 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
2980 if (!q
->merge_bvec_fn
)
2983 bvm
->bi_bdev
= pt
->data_dev
->bdev
;
2985 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
2988 static void set_discard_limits(struct pool_c
*pt
, struct queue_limits
*limits
)
2990 struct pool
*pool
= pt
->pool
;
2991 struct queue_limits
*data_limits
;
2993 limits
->max_discard_sectors
= pool
->sectors_per_block
;
2996 * discard_granularity is just a hint, and not enforced.
2998 if (pt
->adjusted_pf
.discard_passdown
) {
2999 data_limits
= &bdev_get_queue(pt
->data_dev
->bdev
)->limits
;
3000 limits
->discard_granularity
= data_limits
->discard_granularity
;
3002 limits
->discard_granularity
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
3005 static void pool_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3007 struct pool_c
*pt
= ti
->private;
3008 struct pool
*pool
= pt
->pool
;
3009 uint64_t io_opt_sectors
= limits
->io_opt
>> SECTOR_SHIFT
;
3012 * If the system-determined stacked limits are compatible with the
3013 * pool's blocksize (io_opt is a factor) do not override them.
3015 if (io_opt_sectors
< pool
->sectors_per_block
||
3016 do_div(io_opt_sectors
, pool
->sectors_per_block
)) {
3017 blk_limits_io_min(limits
, 0);
3018 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
3022 * pt->adjusted_pf is a staging area for the actual features to use.
3023 * They get transferred to the live pool in bind_control_target()
3024 * called from pool_preresume().
3026 if (!pt
->adjusted_pf
.discard_enabled
) {
3028 * Must explicitly disallow stacking discard limits otherwise the
3029 * block layer will stack them if pool's data device has support.
3030 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3031 * user to see that, so make sure to set all discard limits to 0.
3033 limits
->discard_granularity
= 0;
3037 disable_passdown_if_not_supported(pt
);
3039 set_discard_limits(pt
, limits
);
3042 static struct target_type pool_target
= {
3043 .name
= "thin-pool",
3044 .features
= DM_TARGET_SINGLETON
| DM_TARGET_ALWAYS_WRITEABLE
|
3045 DM_TARGET_IMMUTABLE
,
3046 .version
= {1, 12, 0},
3047 .module
= THIS_MODULE
,
3051 .postsuspend
= pool_postsuspend
,
3052 .preresume
= pool_preresume
,
3053 .resume
= pool_resume
,
3054 .message
= pool_message
,
3055 .status
= pool_status
,
3056 .merge
= pool_merge
,
3057 .iterate_devices
= pool_iterate_devices
,
3058 .io_hints
= pool_io_hints
,
3061 /*----------------------------------------------------------------
3062 * Thin target methods
3063 *--------------------------------------------------------------*/
3064 static void thin_dtr(struct dm_target
*ti
)
3066 struct thin_c
*tc
= ti
->private;
3067 unsigned long flags
;
3069 spin_lock_irqsave(&tc
->pool
->lock
, flags
);
3070 list_del_rcu(&tc
->list
);
3071 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
3074 mutex_lock(&dm_thin_pool_table
.mutex
);
3076 __pool_dec(tc
->pool
);
3077 dm_pool_close_thin_device(tc
->td
);
3078 dm_put_device(ti
, tc
->pool_dev
);
3080 dm_put_device(ti
, tc
->origin_dev
);
3083 mutex_unlock(&dm_thin_pool_table
.mutex
);
3087 * Thin target parameters:
3089 * <pool_dev> <dev_id> [origin_dev]
3091 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3092 * dev_id: the internal device identifier
3093 * origin_dev: a device external to the pool that should act as the origin
3095 * If the pool device has discards disabled, they get disabled for the thin
3098 static int thin_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
3102 struct dm_dev
*pool_dev
, *origin_dev
;
3103 struct mapped_device
*pool_md
;
3105 mutex_lock(&dm_thin_pool_table
.mutex
);
3107 if (argc
!= 2 && argc
!= 3) {
3108 ti
->error
= "Invalid argument count";
3113 tc
= ti
->private = kzalloc(sizeof(*tc
), GFP_KERNEL
);
3115 ti
->error
= "Out of memory";
3119 spin_lock_init(&tc
->lock
);
3120 bio_list_init(&tc
->deferred_bio_list
);
3121 bio_list_init(&tc
->retry_on_resume_list
);
3122 tc
->sort_bio_list
= RB_ROOT
;
3125 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &origin_dev
);
3127 ti
->error
= "Error opening origin device";
3128 goto bad_origin_dev
;
3130 tc
->origin_dev
= origin_dev
;
3133 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &pool_dev
);
3135 ti
->error
= "Error opening pool device";
3138 tc
->pool_dev
= pool_dev
;
3140 if (read_dev_id(argv
[1], (unsigned long long *)&tc
->dev_id
, 0)) {
3141 ti
->error
= "Invalid device id";
3146 pool_md
= dm_get_md(tc
->pool_dev
->bdev
->bd_dev
);
3148 ti
->error
= "Couldn't get pool mapped device";
3153 tc
->pool
= __pool_table_lookup(pool_md
);
3155 ti
->error
= "Couldn't find pool object";
3157 goto bad_pool_lookup
;
3159 __pool_inc(tc
->pool
);
3161 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3162 ti
->error
= "Couldn't open thin device, Pool is in fail mode";
3167 r
= dm_pool_open_thin_device(tc
->pool
->pmd
, tc
->dev_id
, &tc
->td
);
3169 ti
->error
= "Couldn't open thin internal device";
3173 r
= dm_set_target_max_io_len(ti
, tc
->pool
->sectors_per_block
);
3175 goto bad_target_max_io_len
;
3177 ti
->num_flush_bios
= 1;
3178 ti
->flush_supported
= true;
3179 ti
->per_bio_data_size
= sizeof(struct dm_thin_endio_hook
);
3181 /* In case the pool supports discards, pass them on. */
3182 ti
->discard_zeroes_data_unsupported
= true;
3183 if (tc
->pool
->pf
.discard_enabled
) {
3184 ti
->discards_supported
= true;
3185 ti
->num_discard_bios
= 1;
3186 /* Discard bios must be split on a block boundary */
3187 ti
->split_discard_bios
= true;
3192 mutex_unlock(&dm_thin_pool_table
.mutex
);
3194 spin_lock(&tc
->pool
->lock
);
3195 list_add_tail_rcu(&tc
->list
, &tc
->pool
->active_thins
);
3196 spin_unlock(&tc
->pool
->lock
);
3198 * This synchronize_rcu() call is needed here otherwise we risk a
3199 * wake_worker() call finding no bios to process (because the newly
3200 * added tc isn't yet visible). So this reduces latency since we
3201 * aren't then dependent on the periodic commit to wake_worker().
3207 bad_target_max_io_len
:
3208 dm_pool_close_thin_device(tc
->td
);
3210 __pool_dec(tc
->pool
);
3214 dm_put_device(ti
, tc
->pool_dev
);
3217 dm_put_device(ti
, tc
->origin_dev
);
3221 mutex_unlock(&dm_thin_pool_table
.mutex
);
3226 static int thin_map(struct dm_target
*ti
, struct bio
*bio
)
3228 bio
->bi_iter
.bi_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
3230 return thin_bio_map(ti
, bio
);
3233 static int thin_endio(struct dm_target
*ti
, struct bio
*bio
, int err
)
3235 unsigned long flags
;
3236 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
3237 struct list_head work
;
3238 struct dm_thin_new_mapping
*m
, *tmp
;
3239 struct pool
*pool
= h
->tc
->pool
;
3241 if (h
->shared_read_entry
) {
3242 INIT_LIST_HEAD(&work
);
3243 dm_deferred_entry_dec(h
->shared_read_entry
, &work
);
3245 spin_lock_irqsave(&pool
->lock
, flags
);
3246 list_for_each_entry_safe(m
, tmp
, &work
, list
) {
3249 __maybe_add_mapping(m
);
3251 spin_unlock_irqrestore(&pool
->lock
, flags
);
3254 if (h
->all_io_entry
) {
3255 INIT_LIST_HEAD(&work
);
3256 dm_deferred_entry_dec(h
->all_io_entry
, &work
);
3257 if (!list_empty(&work
)) {
3258 spin_lock_irqsave(&pool
->lock
, flags
);
3259 list_for_each_entry_safe(m
, tmp
, &work
, list
)
3260 list_add_tail(&m
->list
, &pool
->prepared_discards
);
3261 spin_unlock_irqrestore(&pool
->lock
, flags
);
3269 static void thin_presuspend(struct dm_target
*ti
)
3271 struct thin_c
*tc
= ti
->private;
3273 if (dm_noflush_suspending(ti
))
3274 noflush_work(tc
, do_noflush_start
);
3277 static void thin_postsuspend(struct dm_target
*ti
)
3279 struct thin_c
*tc
= ti
->private;
3282 * The dm_noflush_suspending flag has been cleared by now, so
3283 * unfortunately we must always run this.
3285 noflush_work(tc
, do_noflush_stop
);
3289 * <nr mapped sectors> <highest mapped sector>
3291 static void thin_status(struct dm_target
*ti
, status_type_t type
,
3292 unsigned status_flags
, char *result
, unsigned maxlen
)
3296 dm_block_t mapped
, highest
;
3297 char buf
[BDEVNAME_SIZE
];
3298 struct thin_c
*tc
= ti
->private;
3300 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3309 case STATUSTYPE_INFO
:
3310 r
= dm_thin_get_mapped_count(tc
->td
, &mapped
);
3312 DMERR("dm_thin_get_mapped_count returned %d", r
);
3316 r
= dm_thin_get_highest_mapped_block(tc
->td
, &highest
);
3318 DMERR("dm_thin_get_highest_mapped_block returned %d", r
);
3322 DMEMIT("%llu ", mapped
* tc
->pool
->sectors_per_block
);
3324 DMEMIT("%llu", ((highest
+ 1) *
3325 tc
->pool
->sectors_per_block
) - 1);
3330 case STATUSTYPE_TABLE
:
3332 format_dev_t(buf
, tc
->pool_dev
->bdev
->bd_dev
),
3333 (unsigned long) tc
->dev_id
);
3335 DMEMIT(" %s", format_dev_t(buf
, tc
->origin_dev
->bdev
->bd_dev
));
3346 static int thin_iterate_devices(struct dm_target
*ti
,
3347 iterate_devices_callout_fn fn
, void *data
)
3350 struct thin_c
*tc
= ti
->private;
3351 struct pool
*pool
= tc
->pool
;
3354 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3355 * we follow a more convoluted path through to the pool's target.
3358 return 0; /* nothing is bound */
3360 blocks
= pool
->ti
->len
;
3361 (void) sector_div(blocks
, pool
->sectors_per_block
);
3363 return fn(ti
, tc
->pool_dev
, 0, pool
->sectors_per_block
* blocks
, data
);
3368 static struct target_type thin_target
= {
3370 .version
= {1, 12, 0},
3371 .module
= THIS_MODULE
,
3375 .end_io
= thin_endio
,
3376 .presuspend
= thin_presuspend
,
3377 .postsuspend
= thin_postsuspend
,
3378 .status
= thin_status
,
3379 .iterate_devices
= thin_iterate_devices
,
3382 /*----------------------------------------------------------------*/
3384 static int __init
dm_thin_init(void)
3390 r
= dm_register_target(&thin_target
);
3394 r
= dm_register_target(&pool_target
);
3396 goto bad_pool_target
;
3400 _new_mapping_cache
= KMEM_CACHE(dm_thin_new_mapping
, 0);
3401 if (!_new_mapping_cache
)
3402 goto bad_new_mapping_cache
;
3406 bad_new_mapping_cache
:
3407 dm_unregister_target(&pool_target
);
3409 dm_unregister_target(&thin_target
);
3414 static void dm_thin_exit(void)
3416 dm_unregister_target(&thin_target
);
3417 dm_unregister_target(&pool_target
);
3419 kmem_cache_destroy(_new_mapping_cache
);
3422 module_init(dm_thin_init
);
3423 module_exit(dm_thin_exit
);
3425 MODULE_DESCRIPTION(DM_NAME
" thin provisioning target");
3426 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3427 MODULE_LICENSE("GPL");