2 * Copyright (C) 2011-2012 Red Hat UK.
4 * This file is released under the GPL.
7 #include "dm-thin-metadata.h"
8 #include "dm-bio-prison.h"
11 #include <linux/device-mapper.h>
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/list.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #define DM_MSG_PREFIX "thin"
24 #define ENDIO_HOOK_POOL_SIZE 1024
25 #define MAPPING_POOL_SIZE 1024
26 #define PRISON_CELLS 1024
27 #define COMMIT_PERIOD HZ
30 * The block size of the device holding pool data must be
31 * between 64KB and 1GB.
33 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
34 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
37 * Device id is restricted to 24 bits.
39 #define MAX_DEV_ID ((1 << 24) - 1)
42 * How do we handle breaking sharing of data blocks?
43 * =================================================
45 * We use a standard copy-on-write btree to store the mappings for the
46 * devices (note I'm talking about copy-on-write of the metadata here, not
47 * the data). When you take an internal snapshot you clone the root node
48 * of the origin btree. After this there is no concept of an origin or a
49 * snapshot. They are just two device trees that happen to point to the
52 * When we get a write in we decide if it's to a shared data block using
53 * some timestamp magic. If it is, we have to break sharing.
55 * Let's say we write to a shared block in what was the origin. The
58 * i) plug io further to this physical block. (see bio_prison code).
60 * ii) quiesce any read io to that shared data block. Obviously
61 * including all devices that share this block. (see dm_deferred_set code)
63 * iii) copy the data block to a newly allocate block. This step can be
64 * missed out if the io covers the block. (schedule_copy).
66 * iv) insert the new mapping into the origin's btree
67 * (process_prepared_mapping). This act of inserting breaks some
68 * sharing of btree nodes between the two devices. Breaking sharing only
69 * effects the btree of that specific device. Btrees for the other
70 * devices that share the block never change. The btree for the origin
71 * device as it was after the last commit is untouched, ie. we're using
72 * persistent data structures in the functional programming sense.
74 * v) unplug io to this physical block, including the io that triggered
75 * the breaking of sharing.
77 * Steps (ii) and (iii) occur in parallel.
79 * The metadata _doesn't_ need to be committed before the io continues. We
80 * get away with this because the io is always written to a _new_ block.
81 * If there's a crash, then:
83 * - The origin mapping will point to the old origin block (the shared
84 * one). This will contain the data as it was before the io that triggered
85 * the breaking of sharing came in.
87 * - The snap mapping still points to the old block. As it would after
90 * The downside of this scheme is the timestamp magic isn't perfect, and
91 * will continue to think that data block in the snapshot device is shared
92 * even after the write to the origin has broken sharing. I suspect data
93 * blocks will typically be shared by many different devices, so we're
94 * breaking sharing n + 1 times, rather than n, where n is the number of
95 * devices that reference this data block. At the moment I think the
96 * benefits far, far outweigh the disadvantages.
99 /*----------------------------------------------------------------*/
104 static void build_data_key(struct dm_thin_device
*td
,
105 dm_block_t b
, struct dm_cell_key
*key
)
108 key
->dev
= dm_thin_dev_id(td
);
112 static void build_virtual_key(struct dm_thin_device
*td
, dm_block_t b
,
113 struct dm_cell_key
*key
)
116 key
->dev
= dm_thin_dev_id(td
);
120 /*----------------------------------------------------------------*/
123 * A pool device ties together a metadata device and a data device. It
124 * also provides the interface for creating and destroying internal
127 struct dm_thin_new_mapping
;
130 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
133 PM_WRITE
, /* metadata may be changed */
134 PM_READ_ONLY
, /* metadata may not be changed */
135 PM_FAIL
, /* all I/O fails */
138 struct pool_features
{
141 bool zero_new_blocks
:1;
142 bool discard_enabled
:1;
143 bool discard_passdown
:1;
147 typedef void (*process_bio_fn
)(struct thin_c
*tc
, struct bio
*bio
);
148 typedef void (*process_mapping_fn
)(struct dm_thin_new_mapping
*m
);
151 struct list_head list
;
152 struct dm_target
*ti
; /* Only set if a pool target is bound */
154 struct mapped_device
*pool_md
;
155 struct block_device
*md_dev
;
156 struct dm_pool_metadata
*pmd
;
158 dm_block_t low_water_blocks
;
159 uint32_t sectors_per_block
;
160 int sectors_per_block_shift
;
162 struct pool_features pf
;
163 unsigned low_water_triggered
:1; /* A dm event has been sent */
164 unsigned no_free_space
:1; /* A -ENOSPC warning has been issued */
166 struct dm_bio_prison
*prison
;
167 struct dm_kcopyd_client
*copier
;
169 struct workqueue_struct
*wq
;
170 struct work_struct worker
;
171 struct delayed_work waker
;
173 unsigned long last_commit_jiffies
;
177 struct bio_list deferred_bios
;
178 struct bio_list deferred_flush_bios
;
179 struct list_head prepared_mappings
;
180 struct list_head prepared_discards
;
182 struct bio_list retry_on_resume_list
;
184 struct dm_deferred_set
*shared_read_ds
;
185 struct dm_deferred_set
*all_io_ds
;
187 struct dm_thin_new_mapping
*next_mapping
;
188 mempool_t
*mapping_pool
;
190 process_bio_fn process_bio
;
191 process_bio_fn process_discard
;
193 process_mapping_fn process_prepared_mapping
;
194 process_mapping_fn process_prepared_discard
;
197 static enum pool_mode
get_pool_mode(struct pool
*pool
);
198 static void set_pool_mode(struct pool
*pool
, enum pool_mode mode
);
201 * Target context for a pool.
204 struct dm_target
*ti
;
206 struct dm_dev
*data_dev
;
207 struct dm_dev
*metadata_dev
;
208 struct dm_target_callbacks callbacks
;
210 dm_block_t low_water_blocks
;
211 struct pool_features requested_pf
; /* Features requested during table load */
212 struct pool_features adjusted_pf
; /* Features used after adjusting for constituent devices */
216 * Target context for a thin.
219 struct dm_dev
*pool_dev
;
220 struct dm_dev
*origin_dev
;
224 struct dm_thin_device
*td
;
227 /*----------------------------------------------------------------*/
230 * A global list of pools that uses a struct mapped_device as a key.
232 static struct dm_thin_pool_table
{
234 struct list_head pools
;
235 } dm_thin_pool_table
;
237 static void pool_table_init(void)
239 mutex_init(&dm_thin_pool_table
.mutex
);
240 INIT_LIST_HEAD(&dm_thin_pool_table
.pools
);
243 static void __pool_table_insert(struct pool
*pool
)
245 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
246 list_add(&pool
->list
, &dm_thin_pool_table
.pools
);
249 static void __pool_table_remove(struct pool
*pool
)
251 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
252 list_del(&pool
->list
);
255 static struct pool
*__pool_table_lookup(struct mapped_device
*md
)
257 struct pool
*pool
= NULL
, *tmp
;
259 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
261 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
262 if (tmp
->pool_md
== md
) {
271 static struct pool
*__pool_table_lookup_metadata_dev(struct block_device
*md_dev
)
273 struct pool
*pool
= NULL
, *tmp
;
275 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
277 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
278 if (tmp
->md_dev
== md_dev
) {
287 /*----------------------------------------------------------------*/
289 struct dm_thin_endio_hook
{
291 struct dm_deferred_entry
*shared_read_entry
;
292 struct dm_deferred_entry
*all_io_entry
;
293 struct dm_thin_new_mapping
*overwrite_mapping
;
296 static void __requeue_bio_list(struct thin_c
*tc
, struct bio_list
*master
)
299 struct bio_list bios
;
301 bio_list_init(&bios
);
302 bio_list_merge(&bios
, master
);
303 bio_list_init(master
);
305 while ((bio
= bio_list_pop(&bios
))) {
306 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
309 bio_endio(bio
, DM_ENDIO_REQUEUE
);
311 bio_list_add(master
, bio
);
315 static void requeue_io(struct thin_c
*tc
)
317 struct pool
*pool
= tc
->pool
;
320 spin_lock_irqsave(&pool
->lock
, flags
);
321 __requeue_bio_list(tc
, &pool
->deferred_bios
);
322 __requeue_bio_list(tc
, &pool
->retry_on_resume_list
);
323 spin_unlock_irqrestore(&pool
->lock
, flags
);
327 * This section of code contains the logic for processing a thin device's IO.
328 * Much of the code depends on pool object resources (lists, workqueues, etc)
329 * but most is exclusively called from the thin target rather than the thin-pool
333 static dm_block_t
get_bio_block(struct thin_c
*tc
, struct bio
*bio
)
335 sector_t block_nr
= bio
->bi_sector
;
337 if (tc
->pool
->sectors_per_block_shift
< 0)
338 (void) sector_div(block_nr
, tc
->pool
->sectors_per_block
);
340 block_nr
>>= tc
->pool
->sectors_per_block_shift
;
345 static void remap(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
)
347 struct pool
*pool
= tc
->pool
;
348 sector_t bi_sector
= bio
->bi_sector
;
350 bio
->bi_bdev
= tc
->pool_dev
->bdev
;
351 if (tc
->pool
->sectors_per_block_shift
< 0)
352 bio
->bi_sector
= (block
* pool
->sectors_per_block
) +
353 sector_div(bi_sector
, pool
->sectors_per_block
);
355 bio
->bi_sector
= (block
<< pool
->sectors_per_block_shift
) |
356 (bi_sector
& (pool
->sectors_per_block
- 1));
359 static void remap_to_origin(struct thin_c
*tc
, struct bio
*bio
)
361 bio
->bi_bdev
= tc
->origin_dev
->bdev
;
364 static int bio_triggers_commit(struct thin_c
*tc
, struct bio
*bio
)
366 return (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) &&
367 dm_thin_changed_this_transaction(tc
->td
);
370 static void inc_all_io_entry(struct pool
*pool
, struct bio
*bio
)
372 struct dm_thin_endio_hook
*h
;
374 if (bio
->bi_rw
& REQ_DISCARD
)
377 h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
378 h
->all_io_entry
= dm_deferred_entry_inc(pool
->all_io_ds
);
381 static void issue(struct thin_c
*tc
, struct bio
*bio
)
383 struct pool
*pool
= tc
->pool
;
386 if (!bio_triggers_commit(tc
, bio
)) {
387 generic_make_request(bio
);
392 * Complete bio with an error if earlier I/O caused changes to
393 * the metadata that can't be committed e.g, due to I/O errors
394 * on the metadata device.
396 if (dm_thin_aborted_changes(tc
->td
)) {
402 * Batch together any bios that trigger commits and then issue a
403 * single commit for them in process_deferred_bios().
405 spin_lock_irqsave(&pool
->lock
, flags
);
406 bio_list_add(&pool
->deferred_flush_bios
, bio
);
407 spin_unlock_irqrestore(&pool
->lock
, flags
);
410 static void remap_to_origin_and_issue(struct thin_c
*tc
, struct bio
*bio
)
412 remap_to_origin(tc
, bio
);
416 static void remap_and_issue(struct thin_c
*tc
, struct bio
*bio
,
419 remap(tc
, bio
, block
);
424 * wake_worker() is used when new work is queued and when pool_resume is
425 * ready to continue deferred IO processing.
427 static void wake_worker(struct pool
*pool
)
429 queue_work(pool
->wq
, &pool
->worker
);
432 /*----------------------------------------------------------------*/
435 * Bio endio functions.
437 struct dm_thin_new_mapping
{
438 struct list_head list
;
442 unsigned pass_discard
:1;
445 dm_block_t virt_block
;
446 dm_block_t data_block
;
447 struct dm_bio_prison_cell
*cell
, *cell2
;
451 * If the bio covers the whole area of a block then we can avoid
452 * zeroing or copying. Instead this bio is hooked. The bio will
453 * still be in the cell, so care has to be taken to avoid issuing
457 bio_end_io_t
*saved_bi_end_io
;
460 static void __maybe_add_mapping(struct dm_thin_new_mapping
*m
)
462 struct pool
*pool
= m
->tc
->pool
;
464 if (m
->quiesced
&& m
->prepared
) {
465 list_add(&m
->list
, &pool
->prepared_mappings
);
470 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
473 struct dm_thin_new_mapping
*m
= context
;
474 struct pool
*pool
= m
->tc
->pool
;
476 m
->err
= read_err
|| write_err
? -EIO
: 0;
478 spin_lock_irqsave(&pool
->lock
, flags
);
480 __maybe_add_mapping(m
);
481 spin_unlock_irqrestore(&pool
->lock
, flags
);
484 static void overwrite_endio(struct bio
*bio
, int err
)
487 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
488 struct dm_thin_new_mapping
*m
= h
->overwrite_mapping
;
489 struct pool
*pool
= m
->tc
->pool
;
493 spin_lock_irqsave(&pool
->lock
, flags
);
495 __maybe_add_mapping(m
);
496 spin_unlock_irqrestore(&pool
->lock
, flags
);
499 /*----------------------------------------------------------------*/
506 * Prepared mapping jobs.
510 * This sends the bios in the cell back to the deferred_bios list.
512 static void cell_defer(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
514 struct pool
*pool
= tc
->pool
;
517 spin_lock_irqsave(&pool
->lock
, flags
);
518 dm_cell_release(cell
, &pool
->deferred_bios
);
519 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
525 * Same as cell_defer except it omits the original holder of the cell.
527 static void cell_defer_no_holder(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
529 struct pool
*pool
= tc
->pool
;
532 spin_lock_irqsave(&pool
->lock
, flags
);
533 dm_cell_release_no_holder(cell
, &pool
->deferred_bios
);
534 spin_unlock_irqrestore(&pool
->lock
, flags
);
539 static void process_prepared_mapping_fail(struct dm_thin_new_mapping
*m
)
542 m
->bio
->bi_end_io
= m
->saved_bi_end_io
;
543 dm_cell_error(m
->cell
);
545 mempool_free(m
, m
->tc
->pool
->mapping_pool
);
547 static void process_prepared_mapping(struct dm_thin_new_mapping
*m
)
549 struct thin_c
*tc
= m
->tc
;
555 bio
->bi_end_io
= m
->saved_bi_end_io
;
558 dm_cell_error(m
->cell
);
563 * Commit the prepared block into the mapping btree.
564 * Any I/O for this block arriving after this point will get
565 * remapped to it directly.
567 r
= dm_thin_insert_block(tc
->td
, m
->virt_block
, m
->data_block
);
569 DMERR_LIMIT("dm_thin_insert_block() failed");
570 dm_cell_error(m
->cell
);
575 * Release any bios held while the block was being provisioned.
576 * If we are processing a write bio that completely covers the block,
577 * we already processed it so can ignore it now when processing
578 * the bios in the cell.
581 cell_defer_no_holder(tc
, m
->cell
);
584 cell_defer(tc
, m
->cell
);
588 mempool_free(m
, tc
->pool
->mapping_pool
);
591 static void process_prepared_discard_fail(struct dm_thin_new_mapping
*m
)
593 struct thin_c
*tc
= m
->tc
;
595 bio_io_error(m
->bio
);
596 cell_defer_no_holder(tc
, m
->cell
);
597 cell_defer_no_holder(tc
, m
->cell2
);
598 mempool_free(m
, tc
->pool
->mapping_pool
);
601 static void process_prepared_discard_passdown(struct dm_thin_new_mapping
*m
)
603 struct thin_c
*tc
= m
->tc
;
605 inc_all_io_entry(tc
->pool
, m
->bio
);
606 cell_defer_no_holder(tc
, m
->cell
);
607 cell_defer_no_holder(tc
, m
->cell2
);
610 remap_and_issue(tc
, m
->bio
, m
->data_block
);
612 bio_endio(m
->bio
, 0);
614 mempool_free(m
, tc
->pool
->mapping_pool
);
617 static void process_prepared_discard(struct dm_thin_new_mapping
*m
)
620 struct thin_c
*tc
= m
->tc
;
622 r
= dm_thin_remove_block(tc
->td
, m
->virt_block
);
624 DMERR_LIMIT("dm_thin_remove_block() failed");
626 process_prepared_discard_passdown(m
);
629 static void process_prepared(struct pool
*pool
, struct list_head
*head
,
630 process_mapping_fn
*fn
)
633 struct list_head maps
;
634 struct dm_thin_new_mapping
*m
, *tmp
;
636 INIT_LIST_HEAD(&maps
);
637 spin_lock_irqsave(&pool
->lock
, flags
);
638 list_splice_init(head
, &maps
);
639 spin_unlock_irqrestore(&pool
->lock
, flags
);
641 list_for_each_entry_safe(m
, tmp
, &maps
, list
)
648 static int io_overlaps_block(struct pool
*pool
, struct bio
*bio
)
650 return bio
->bi_size
== (pool
->sectors_per_block
<< SECTOR_SHIFT
);
653 static int io_overwrites_block(struct pool
*pool
, struct bio
*bio
)
655 return (bio_data_dir(bio
) == WRITE
) &&
656 io_overlaps_block(pool
, bio
);
659 static void save_and_set_endio(struct bio
*bio
, bio_end_io_t
**save
,
662 *save
= bio
->bi_end_io
;
666 static int ensure_next_mapping(struct pool
*pool
)
668 if (pool
->next_mapping
)
671 pool
->next_mapping
= mempool_alloc(pool
->mapping_pool
, GFP_ATOMIC
);
673 return pool
->next_mapping
? 0 : -ENOMEM
;
676 static struct dm_thin_new_mapping
*get_next_mapping(struct pool
*pool
)
678 struct dm_thin_new_mapping
*r
= pool
->next_mapping
;
680 BUG_ON(!pool
->next_mapping
);
682 pool
->next_mapping
= NULL
;
687 static void schedule_copy(struct thin_c
*tc
, dm_block_t virt_block
,
688 struct dm_dev
*origin
, dm_block_t data_origin
,
689 dm_block_t data_dest
,
690 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
693 struct pool
*pool
= tc
->pool
;
694 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
696 INIT_LIST_HEAD(&m
->list
);
700 m
->virt_block
= virt_block
;
701 m
->data_block
= data_dest
;
706 if (!dm_deferred_set_add_work(pool
->shared_read_ds
, &m
->list
))
710 * IO to pool_dev remaps to the pool target's data_dev.
712 * If the whole block of data is being overwritten, we can issue the
713 * bio immediately. Otherwise we use kcopyd to clone the data first.
715 if (io_overwrites_block(pool
, bio
)) {
716 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
718 h
->overwrite_mapping
= m
;
720 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
721 inc_all_io_entry(pool
, bio
);
722 remap_and_issue(tc
, bio
, data_dest
);
724 struct dm_io_region from
, to
;
726 from
.bdev
= origin
->bdev
;
727 from
.sector
= data_origin
* pool
->sectors_per_block
;
728 from
.count
= pool
->sectors_per_block
;
730 to
.bdev
= tc
->pool_dev
->bdev
;
731 to
.sector
= data_dest
* pool
->sectors_per_block
;
732 to
.count
= pool
->sectors_per_block
;
734 r
= dm_kcopyd_copy(pool
->copier
, &from
, 1, &to
,
735 0, copy_complete
, m
);
737 mempool_free(m
, pool
->mapping_pool
);
738 DMERR_LIMIT("dm_kcopyd_copy() failed");
744 static void schedule_internal_copy(struct thin_c
*tc
, dm_block_t virt_block
,
745 dm_block_t data_origin
, dm_block_t data_dest
,
746 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
748 schedule_copy(tc
, virt_block
, tc
->pool_dev
,
749 data_origin
, data_dest
, cell
, bio
);
752 static void schedule_external_copy(struct thin_c
*tc
, dm_block_t virt_block
,
753 dm_block_t data_dest
,
754 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
756 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
757 virt_block
, data_dest
, cell
, bio
);
760 static void schedule_zero(struct thin_c
*tc
, dm_block_t virt_block
,
761 dm_block_t data_block
, struct dm_bio_prison_cell
*cell
,
764 struct pool
*pool
= tc
->pool
;
765 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
767 INIT_LIST_HEAD(&m
->list
);
771 m
->virt_block
= virt_block
;
772 m
->data_block
= data_block
;
778 * If the whole block of data is being overwritten or we are not
779 * zeroing pre-existing data, we can issue the bio immediately.
780 * Otherwise we use kcopyd to zero the data first.
782 if (!pool
->pf
.zero_new_blocks
)
783 process_prepared_mapping(m
);
785 else if (io_overwrites_block(pool
, bio
)) {
786 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
788 h
->overwrite_mapping
= m
;
790 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
791 inc_all_io_entry(pool
, bio
);
792 remap_and_issue(tc
, bio
, data_block
);
795 struct dm_io_region to
;
797 to
.bdev
= tc
->pool_dev
->bdev
;
798 to
.sector
= data_block
* pool
->sectors_per_block
;
799 to
.count
= pool
->sectors_per_block
;
801 r
= dm_kcopyd_zero(pool
->copier
, 1, &to
, 0, copy_complete
, m
);
803 mempool_free(m
, pool
->mapping_pool
);
804 DMERR_LIMIT("dm_kcopyd_zero() failed");
810 static int commit(struct pool
*pool
)
814 r
= dm_pool_commit_metadata(pool
->pmd
);
816 DMERR_LIMIT("commit failed: error = %d", r
);
822 * A non-zero return indicates read_only or fail_io mode.
823 * Many callers don't care about the return value.
825 static int commit_or_fallback(struct pool
*pool
)
829 if (get_pool_mode(pool
) != PM_WRITE
)
834 set_pool_mode(pool
, PM_READ_ONLY
);
839 static int alloc_data_block(struct thin_c
*tc
, dm_block_t
*result
)
842 dm_block_t free_blocks
;
844 struct pool
*pool
= tc
->pool
;
846 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
850 if (free_blocks
<= pool
->low_water_blocks
&& !pool
->low_water_triggered
) {
851 DMWARN("%s: reached low water mark, sending event.",
852 dm_device_name(pool
->pool_md
));
853 spin_lock_irqsave(&pool
->lock
, flags
);
854 pool
->low_water_triggered
= 1;
855 spin_unlock_irqrestore(&pool
->lock
, flags
);
856 dm_table_event(pool
->ti
->table
);
860 if (pool
->no_free_space
)
864 * Try to commit to see if that will free up some
867 (void) commit_or_fallback(pool
);
869 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
874 * If we still have no space we set a flag to avoid
875 * doing all this checking and return -ENOSPC.
878 DMWARN("%s: no free space available.",
879 dm_device_name(pool
->pool_md
));
880 spin_lock_irqsave(&pool
->lock
, flags
);
881 pool
->no_free_space
= 1;
882 spin_unlock_irqrestore(&pool
->lock
, flags
);
888 r
= dm_pool_alloc_data_block(pool
->pmd
, result
);
896 * If we have run out of space, queue bios until the device is
897 * resumed, presumably after having been reloaded with more space.
899 static void retry_on_resume(struct bio
*bio
)
901 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
902 struct thin_c
*tc
= h
->tc
;
903 struct pool
*pool
= tc
->pool
;
906 spin_lock_irqsave(&pool
->lock
, flags
);
907 bio_list_add(&pool
->retry_on_resume_list
, bio
);
908 spin_unlock_irqrestore(&pool
->lock
, flags
);
911 static void no_space(struct dm_bio_prison_cell
*cell
)
914 struct bio_list bios
;
916 bio_list_init(&bios
);
917 dm_cell_release(cell
, &bios
);
919 while ((bio
= bio_list_pop(&bios
)))
920 retry_on_resume(bio
);
923 static void process_discard(struct thin_c
*tc
, struct bio
*bio
)
927 struct pool
*pool
= tc
->pool
;
928 struct dm_bio_prison_cell
*cell
, *cell2
;
929 struct dm_cell_key key
, key2
;
930 dm_block_t block
= get_bio_block(tc
, bio
);
931 struct dm_thin_lookup_result lookup_result
;
932 struct dm_thin_new_mapping
*m
;
934 build_virtual_key(tc
->td
, block
, &key
);
935 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell
))
938 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
942 * Check nobody is fiddling with this pool block. This can
943 * happen if someone's in the process of breaking sharing
946 build_data_key(tc
->td
, lookup_result
.block
, &key2
);
947 if (dm_bio_detain(tc
->pool
->prison
, &key2
, bio
, &cell2
)) {
948 cell_defer_no_holder(tc
, cell
);
952 if (io_overlaps_block(pool
, bio
)) {
954 * IO may still be going to the destination block. We must
955 * quiesce before we can do the removal.
957 m
= get_next_mapping(pool
);
959 m
->pass_discard
= (!lookup_result
.shared
) && pool
->pf
.discard_passdown
;
960 m
->virt_block
= block
;
961 m
->data_block
= lookup_result
.block
;
967 if (!dm_deferred_set_add_work(pool
->all_io_ds
, &m
->list
)) {
968 spin_lock_irqsave(&pool
->lock
, flags
);
969 list_add(&m
->list
, &pool
->prepared_discards
);
970 spin_unlock_irqrestore(&pool
->lock
, flags
);
974 inc_all_io_entry(pool
, bio
);
975 cell_defer_no_holder(tc
, cell
);
976 cell_defer_no_holder(tc
, cell2
);
979 * The DM core makes sure that the discard doesn't span
980 * a block boundary. So we submit the discard of a
981 * partial block appropriately.
983 if ((!lookup_result
.shared
) && pool
->pf
.discard_passdown
)
984 remap_and_issue(tc
, bio
, lookup_result
.block
);
992 * It isn't provisioned, just forget it.
994 cell_defer_no_holder(tc
, cell
);
999 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1001 cell_defer_no_holder(tc
, cell
);
1007 static void break_sharing(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1008 struct dm_cell_key
*key
,
1009 struct dm_thin_lookup_result
*lookup_result
,
1010 struct dm_bio_prison_cell
*cell
)
1013 dm_block_t data_block
;
1015 r
= alloc_data_block(tc
, &data_block
);
1018 schedule_internal_copy(tc
, block
, lookup_result
->block
,
1019 data_block
, cell
, bio
);
1027 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1029 dm_cell_error(cell
);
1034 static void process_shared_bio(struct thin_c
*tc
, struct bio
*bio
,
1036 struct dm_thin_lookup_result
*lookup_result
)
1038 struct dm_bio_prison_cell
*cell
;
1039 struct pool
*pool
= tc
->pool
;
1040 struct dm_cell_key key
;
1043 * If cell is already occupied, then sharing is already in the process
1044 * of being broken so we have nothing further to do here.
1046 build_data_key(tc
->td
, lookup_result
->block
, &key
);
1047 if (dm_bio_detain(pool
->prison
, &key
, bio
, &cell
))
1050 if (bio_data_dir(bio
) == WRITE
&& bio
->bi_size
)
1051 break_sharing(tc
, bio
, block
, &key
, lookup_result
, cell
);
1053 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1055 h
->shared_read_entry
= dm_deferred_entry_inc(pool
->shared_read_ds
);
1056 inc_all_io_entry(pool
, bio
);
1057 cell_defer_no_holder(tc
, cell
);
1059 remap_and_issue(tc
, bio
, lookup_result
->block
);
1063 static void provision_block(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1064 struct dm_bio_prison_cell
*cell
)
1067 dm_block_t data_block
;
1070 * Remap empty bios (flushes) immediately, without provisioning.
1072 if (!bio
->bi_size
) {
1073 inc_all_io_entry(tc
->pool
, bio
);
1074 cell_defer_no_holder(tc
, cell
);
1076 remap_and_issue(tc
, bio
, 0);
1081 * Fill read bios with zeroes and complete them immediately.
1083 if (bio_data_dir(bio
) == READ
) {
1085 cell_defer_no_holder(tc
, cell
);
1090 r
= alloc_data_block(tc
, &data_block
);
1094 schedule_external_copy(tc
, block
, data_block
, cell
, bio
);
1096 schedule_zero(tc
, block
, data_block
, cell
, bio
);
1104 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1106 set_pool_mode(tc
->pool
, PM_READ_ONLY
);
1107 dm_cell_error(cell
);
1112 static void process_bio(struct thin_c
*tc
, struct bio
*bio
)
1115 dm_block_t block
= get_bio_block(tc
, bio
);
1116 struct dm_bio_prison_cell
*cell
;
1117 struct dm_cell_key key
;
1118 struct dm_thin_lookup_result lookup_result
;
1121 * If cell is already occupied, then the block is already
1122 * being provisioned so we have nothing further to do here.
1124 build_virtual_key(tc
->td
, block
, &key
);
1125 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell
))
1128 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1131 if (lookup_result
.shared
) {
1132 process_shared_bio(tc
, bio
, block
, &lookup_result
);
1133 cell_defer_no_holder(tc
, cell
);
1135 inc_all_io_entry(tc
->pool
, bio
);
1136 cell_defer_no_holder(tc
, cell
);
1138 remap_and_issue(tc
, bio
, lookup_result
.block
);
1143 if (bio_data_dir(bio
) == READ
&& tc
->origin_dev
) {
1144 inc_all_io_entry(tc
->pool
, bio
);
1145 cell_defer_no_holder(tc
, cell
);
1147 remap_to_origin_and_issue(tc
, bio
);
1149 provision_block(tc
, bio
, block
, cell
);
1153 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1155 cell_defer_no_holder(tc
, cell
);
1161 static void process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
)
1164 int rw
= bio_data_dir(bio
);
1165 dm_block_t block
= get_bio_block(tc
, bio
);
1166 struct dm_thin_lookup_result lookup_result
;
1168 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1171 if (lookup_result
.shared
&& (rw
== WRITE
) && bio
->bi_size
)
1174 inc_all_io_entry(tc
->pool
, bio
);
1175 remap_and_issue(tc
, bio
, lookup_result
.block
);
1185 if (tc
->origin_dev
) {
1186 inc_all_io_entry(tc
->pool
, bio
);
1187 remap_to_origin_and_issue(tc
, bio
);
1196 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1203 static void process_bio_fail(struct thin_c
*tc
, struct bio
*bio
)
1208 static int need_commit_due_to_time(struct pool
*pool
)
1210 return jiffies
< pool
->last_commit_jiffies
||
1211 jiffies
> pool
->last_commit_jiffies
+ COMMIT_PERIOD
;
1214 static void process_deferred_bios(struct pool
*pool
)
1216 unsigned long flags
;
1218 struct bio_list bios
;
1220 bio_list_init(&bios
);
1222 spin_lock_irqsave(&pool
->lock
, flags
);
1223 bio_list_merge(&bios
, &pool
->deferred_bios
);
1224 bio_list_init(&pool
->deferred_bios
);
1225 spin_unlock_irqrestore(&pool
->lock
, flags
);
1227 while ((bio
= bio_list_pop(&bios
))) {
1228 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1229 struct thin_c
*tc
= h
->tc
;
1232 * If we've got no free new_mapping structs, and processing
1233 * this bio might require one, we pause until there are some
1234 * prepared mappings to process.
1236 if (ensure_next_mapping(pool
)) {
1237 spin_lock_irqsave(&pool
->lock
, flags
);
1238 bio_list_merge(&pool
->deferred_bios
, &bios
);
1239 spin_unlock_irqrestore(&pool
->lock
, flags
);
1244 if (bio
->bi_rw
& REQ_DISCARD
)
1245 pool
->process_discard(tc
, bio
);
1247 pool
->process_bio(tc
, bio
);
1251 * If there are any deferred flush bios, we must commit
1252 * the metadata before issuing them.
1254 bio_list_init(&bios
);
1255 spin_lock_irqsave(&pool
->lock
, flags
);
1256 bio_list_merge(&bios
, &pool
->deferred_flush_bios
);
1257 bio_list_init(&pool
->deferred_flush_bios
);
1258 spin_unlock_irqrestore(&pool
->lock
, flags
);
1260 if (bio_list_empty(&bios
) && !need_commit_due_to_time(pool
))
1263 if (commit_or_fallback(pool
)) {
1264 while ((bio
= bio_list_pop(&bios
)))
1268 pool
->last_commit_jiffies
= jiffies
;
1270 while ((bio
= bio_list_pop(&bios
)))
1271 generic_make_request(bio
);
1274 static void do_worker(struct work_struct
*ws
)
1276 struct pool
*pool
= container_of(ws
, struct pool
, worker
);
1278 process_prepared(pool
, &pool
->prepared_mappings
, &pool
->process_prepared_mapping
);
1279 process_prepared(pool
, &pool
->prepared_discards
, &pool
->process_prepared_discard
);
1280 process_deferred_bios(pool
);
1284 * We want to commit periodically so that not too much
1285 * unwritten data builds up.
1287 static void do_waker(struct work_struct
*ws
)
1289 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
, waker
);
1291 queue_delayed_work(pool
->wq
, &pool
->waker
, COMMIT_PERIOD
);
1294 /*----------------------------------------------------------------*/
1296 static enum pool_mode
get_pool_mode(struct pool
*pool
)
1298 return pool
->pf
.mode
;
1301 static void set_pool_mode(struct pool
*pool
, enum pool_mode mode
)
1305 pool
->pf
.mode
= mode
;
1309 DMERR("switching pool to failure mode");
1310 pool
->process_bio
= process_bio_fail
;
1311 pool
->process_discard
= process_bio_fail
;
1312 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1313 pool
->process_prepared_discard
= process_prepared_discard_fail
;
1317 DMERR("switching pool to read-only mode");
1318 r
= dm_pool_abort_metadata(pool
->pmd
);
1320 DMERR("aborting transaction failed");
1321 set_pool_mode(pool
, PM_FAIL
);
1323 dm_pool_metadata_read_only(pool
->pmd
);
1324 pool
->process_bio
= process_bio_read_only
;
1325 pool
->process_discard
= process_discard
;
1326 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1327 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1332 pool
->process_bio
= process_bio
;
1333 pool
->process_discard
= process_discard
;
1334 pool
->process_prepared_mapping
= process_prepared_mapping
;
1335 pool
->process_prepared_discard
= process_prepared_discard
;
1340 /*----------------------------------------------------------------*/
1343 * Mapping functions.
1347 * Called only while mapping a thin bio to hand it over to the workqueue.
1349 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
)
1351 unsigned long flags
;
1352 struct pool
*pool
= tc
->pool
;
1354 spin_lock_irqsave(&pool
->lock
, flags
);
1355 bio_list_add(&pool
->deferred_bios
, bio
);
1356 spin_unlock_irqrestore(&pool
->lock
, flags
);
1361 static void thin_hook_bio(struct thin_c
*tc
, struct bio
*bio
)
1363 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
1366 h
->shared_read_entry
= NULL
;
1367 h
->all_io_entry
= NULL
;
1368 h
->overwrite_mapping
= NULL
;
1372 * Non-blocking function called from the thin target's map function.
1374 static int thin_bio_map(struct dm_target
*ti
, struct bio
*bio
)
1377 struct thin_c
*tc
= ti
->private;
1378 dm_block_t block
= get_bio_block(tc
, bio
);
1379 struct dm_thin_device
*td
= tc
->td
;
1380 struct dm_thin_lookup_result result
;
1381 struct dm_bio_prison_cell
*cell1
, *cell2
;
1382 struct dm_cell_key key
;
1384 thin_hook_bio(tc
, bio
);
1386 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
1388 return DM_MAPIO_SUBMITTED
;
1391 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)) {
1392 thin_defer_bio(tc
, bio
);
1393 return DM_MAPIO_SUBMITTED
;
1396 r
= dm_thin_find_block(td
, block
, 0, &result
);
1399 * Note that we defer readahead too.
1403 if (unlikely(result
.shared
)) {
1405 * We have a race condition here between the
1406 * result.shared value returned by the lookup and
1407 * snapshot creation, which may cause new
1410 * To avoid this always quiesce the origin before
1411 * taking the snap. You want to do this anyway to
1412 * ensure a consistent application view
1415 * More distant ancestors are irrelevant. The
1416 * shared flag will be set in their case.
1418 thin_defer_bio(tc
, bio
);
1419 return DM_MAPIO_SUBMITTED
;
1422 build_virtual_key(tc
->td
, block
, &key
);
1423 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell1
))
1424 return DM_MAPIO_SUBMITTED
;
1426 build_data_key(tc
->td
, result
.block
, &key
);
1427 if (dm_bio_detain(tc
->pool
->prison
, &key
, bio
, &cell2
)) {
1428 cell_defer_no_holder(tc
, cell1
);
1429 return DM_MAPIO_SUBMITTED
;
1432 inc_all_io_entry(tc
->pool
, bio
);
1433 cell_defer_no_holder(tc
, cell2
);
1434 cell_defer_no_holder(tc
, cell1
);
1436 remap(tc
, bio
, result
.block
);
1437 return DM_MAPIO_REMAPPED
;
1440 if (get_pool_mode(tc
->pool
) == PM_READ_ONLY
) {
1442 * This block isn't provisioned, and we have no way
1443 * of doing so. Just error it.
1446 return DM_MAPIO_SUBMITTED
;
1452 * In future, the failed dm_thin_find_block above could
1453 * provide the hint to load the metadata into cache.
1455 thin_defer_bio(tc
, bio
);
1456 return DM_MAPIO_SUBMITTED
;
1460 * Must always call bio_io_error on failure.
1461 * dm_thin_find_block can fail with -EINVAL if the
1462 * pool is switched to fail-io mode.
1465 return DM_MAPIO_SUBMITTED
;
1469 static int pool_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
1472 unsigned long flags
;
1473 struct pool_c
*pt
= container_of(cb
, struct pool_c
, callbacks
);
1475 spin_lock_irqsave(&pt
->pool
->lock
, flags
);
1476 r
= !bio_list_empty(&pt
->pool
->retry_on_resume_list
);
1477 spin_unlock_irqrestore(&pt
->pool
->lock
, flags
);
1480 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1481 r
= bdi_congested(&q
->backing_dev_info
, bdi_bits
);
1487 static void __requeue_bios(struct pool
*pool
)
1489 bio_list_merge(&pool
->deferred_bios
, &pool
->retry_on_resume_list
);
1490 bio_list_init(&pool
->retry_on_resume_list
);
1493 /*----------------------------------------------------------------
1494 * Binding of control targets to a pool object
1495 *--------------------------------------------------------------*/
1496 static bool data_dev_supports_discard(struct pool_c
*pt
)
1498 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1500 return q
&& blk_queue_discard(q
);
1504 * If discard_passdown was enabled verify that the data device
1505 * supports discards. Disable discard_passdown if not.
1507 static void disable_passdown_if_not_supported(struct pool_c
*pt
)
1509 struct pool
*pool
= pt
->pool
;
1510 struct block_device
*data_bdev
= pt
->data_dev
->bdev
;
1511 struct queue_limits
*data_limits
= &bdev_get_queue(data_bdev
)->limits
;
1512 sector_t block_size
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
1513 const char *reason
= NULL
;
1514 char buf
[BDEVNAME_SIZE
];
1516 if (!pt
->adjusted_pf
.discard_passdown
)
1519 if (!data_dev_supports_discard(pt
))
1520 reason
= "discard unsupported";
1522 else if (data_limits
->max_discard_sectors
< pool
->sectors_per_block
)
1523 reason
= "max discard sectors smaller than a block";
1525 else if (data_limits
->discard_granularity
> block_size
)
1526 reason
= "discard granularity larger than a block";
1528 else if (block_size
& (data_limits
->discard_granularity
- 1))
1529 reason
= "discard granularity not a factor of block size";
1532 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev
, buf
), reason
);
1533 pt
->adjusted_pf
.discard_passdown
= false;
1537 static int bind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1539 struct pool_c
*pt
= ti
->private;
1542 * We want to make sure that degraded pools are never upgraded.
1544 enum pool_mode old_mode
= pool
->pf
.mode
;
1545 enum pool_mode new_mode
= pt
->adjusted_pf
.mode
;
1547 if (old_mode
> new_mode
)
1548 new_mode
= old_mode
;
1551 pool
->low_water_blocks
= pt
->low_water_blocks
;
1552 pool
->pf
= pt
->adjusted_pf
;
1554 set_pool_mode(pool
, new_mode
);
1559 static void unbind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1565 /*----------------------------------------------------------------
1567 *--------------------------------------------------------------*/
1568 /* Initialize pool features. */
1569 static void pool_features_init(struct pool_features
*pf
)
1571 pf
->mode
= PM_WRITE
;
1572 pf
->zero_new_blocks
= true;
1573 pf
->discard_enabled
= true;
1574 pf
->discard_passdown
= true;
1577 static void __pool_destroy(struct pool
*pool
)
1579 __pool_table_remove(pool
);
1581 if (dm_pool_metadata_close(pool
->pmd
) < 0)
1582 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
1584 dm_bio_prison_destroy(pool
->prison
);
1585 dm_kcopyd_client_destroy(pool
->copier
);
1588 destroy_workqueue(pool
->wq
);
1590 if (pool
->next_mapping
)
1591 mempool_free(pool
->next_mapping
, pool
->mapping_pool
);
1592 mempool_destroy(pool
->mapping_pool
);
1593 dm_deferred_set_destroy(pool
->shared_read_ds
);
1594 dm_deferred_set_destroy(pool
->all_io_ds
);
1598 static struct kmem_cache
*_new_mapping_cache
;
1600 static struct pool
*pool_create(struct mapped_device
*pool_md
,
1601 struct block_device
*metadata_dev
,
1602 unsigned long block_size
,
1603 int read_only
, char **error
)
1608 struct dm_pool_metadata
*pmd
;
1609 bool format_device
= read_only
? false : true;
1611 pmd
= dm_pool_metadata_open(metadata_dev
, block_size
, format_device
);
1613 *error
= "Error creating metadata object";
1614 return (struct pool
*)pmd
;
1617 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
1619 *error
= "Error allocating memory for pool";
1620 err_p
= ERR_PTR(-ENOMEM
);
1625 pool
->sectors_per_block
= block_size
;
1626 if (block_size
& (block_size
- 1))
1627 pool
->sectors_per_block_shift
= -1;
1629 pool
->sectors_per_block_shift
= __ffs(block_size
);
1630 pool
->low_water_blocks
= 0;
1631 pool_features_init(&pool
->pf
);
1632 pool
->prison
= dm_bio_prison_create(PRISON_CELLS
);
1633 if (!pool
->prison
) {
1634 *error
= "Error creating pool's bio prison";
1635 err_p
= ERR_PTR(-ENOMEM
);
1639 pool
->copier
= dm_kcopyd_client_create();
1640 if (IS_ERR(pool
->copier
)) {
1641 r
= PTR_ERR(pool
->copier
);
1642 *error
= "Error creating pool's kcopyd client";
1644 goto bad_kcopyd_client
;
1648 * Create singlethreaded workqueue that will service all devices
1649 * that use this metadata.
1651 pool
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
1653 *error
= "Error creating pool's workqueue";
1654 err_p
= ERR_PTR(-ENOMEM
);
1658 INIT_WORK(&pool
->worker
, do_worker
);
1659 INIT_DELAYED_WORK(&pool
->waker
, do_waker
);
1660 spin_lock_init(&pool
->lock
);
1661 bio_list_init(&pool
->deferred_bios
);
1662 bio_list_init(&pool
->deferred_flush_bios
);
1663 INIT_LIST_HEAD(&pool
->prepared_mappings
);
1664 INIT_LIST_HEAD(&pool
->prepared_discards
);
1665 pool
->low_water_triggered
= 0;
1666 pool
->no_free_space
= 0;
1667 bio_list_init(&pool
->retry_on_resume_list
);
1669 pool
->shared_read_ds
= dm_deferred_set_create();
1670 if (!pool
->shared_read_ds
) {
1671 *error
= "Error creating pool's shared read deferred set";
1672 err_p
= ERR_PTR(-ENOMEM
);
1673 goto bad_shared_read_ds
;
1676 pool
->all_io_ds
= dm_deferred_set_create();
1677 if (!pool
->all_io_ds
) {
1678 *error
= "Error creating pool's all io deferred set";
1679 err_p
= ERR_PTR(-ENOMEM
);
1683 pool
->next_mapping
= NULL
;
1684 pool
->mapping_pool
= mempool_create_slab_pool(MAPPING_POOL_SIZE
,
1685 _new_mapping_cache
);
1686 if (!pool
->mapping_pool
) {
1687 *error
= "Error creating pool's mapping mempool";
1688 err_p
= ERR_PTR(-ENOMEM
);
1689 goto bad_mapping_pool
;
1692 pool
->ref_count
= 1;
1693 pool
->last_commit_jiffies
= jiffies
;
1694 pool
->pool_md
= pool_md
;
1695 pool
->md_dev
= metadata_dev
;
1696 __pool_table_insert(pool
);
1701 dm_deferred_set_destroy(pool
->all_io_ds
);
1703 dm_deferred_set_destroy(pool
->shared_read_ds
);
1705 destroy_workqueue(pool
->wq
);
1707 dm_kcopyd_client_destroy(pool
->copier
);
1709 dm_bio_prison_destroy(pool
->prison
);
1713 if (dm_pool_metadata_close(pmd
))
1714 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
1719 static void __pool_inc(struct pool
*pool
)
1721 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
1725 static void __pool_dec(struct pool
*pool
)
1727 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
1728 BUG_ON(!pool
->ref_count
);
1729 if (!--pool
->ref_count
)
1730 __pool_destroy(pool
);
1733 static struct pool
*__pool_find(struct mapped_device
*pool_md
,
1734 struct block_device
*metadata_dev
,
1735 unsigned long block_size
, int read_only
,
1736 char **error
, int *created
)
1738 struct pool
*pool
= __pool_table_lookup_metadata_dev(metadata_dev
);
1741 if (pool
->pool_md
!= pool_md
) {
1742 *error
= "metadata device already in use by a pool";
1743 return ERR_PTR(-EBUSY
);
1748 pool
= __pool_table_lookup(pool_md
);
1750 if (pool
->md_dev
!= metadata_dev
) {
1751 *error
= "different pool cannot replace a pool";
1752 return ERR_PTR(-EINVAL
);
1757 pool
= pool_create(pool_md
, metadata_dev
, block_size
, read_only
, error
);
1765 /*----------------------------------------------------------------
1766 * Pool target methods
1767 *--------------------------------------------------------------*/
1768 static void pool_dtr(struct dm_target
*ti
)
1770 struct pool_c
*pt
= ti
->private;
1772 mutex_lock(&dm_thin_pool_table
.mutex
);
1774 unbind_control_target(pt
->pool
, ti
);
1775 __pool_dec(pt
->pool
);
1776 dm_put_device(ti
, pt
->metadata_dev
);
1777 dm_put_device(ti
, pt
->data_dev
);
1780 mutex_unlock(&dm_thin_pool_table
.mutex
);
1783 static int parse_pool_features(struct dm_arg_set
*as
, struct pool_features
*pf
,
1784 struct dm_target
*ti
)
1788 const char *arg_name
;
1790 static struct dm_arg _args
[] = {
1791 {0, 3, "Invalid number of pool feature arguments"},
1795 * No feature arguments supplied.
1800 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
1804 while (argc
&& !r
) {
1805 arg_name
= dm_shift_arg(as
);
1808 if (!strcasecmp(arg_name
, "skip_block_zeroing"))
1809 pf
->zero_new_blocks
= false;
1811 else if (!strcasecmp(arg_name
, "ignore_discard"))
1812 pf
->discard_enabled
= false;
1814 else if (!strcasecmp(arg_name
, "no_discard_passdown"))
1815 pf
->discard_passdown
= false;
1817 else if (!strcasecmp(arg_name
, "read_only"))
1818 pf
->mode
= PM_READ_ONLY
;
1821 ti
->error
= "Unrecognised pool feature requested";
1831 * thin-pool <metadata dev> <data dev>
1832 * <data block size (sectors)>
1833 * <low water mark (blocks)>
1834 * [<#feature args> [<arg>]*]
1836 * Optional feature arguments are:
1837 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
1838 * ignore_discard: disable discard
1839 * no_discard_passdown: don't pass discards down to the data device
1841 static int pool_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1843 int r
, pool_created
= 0;
1846 struct pool_features pf
;
1847 struct dm_arg_set as
;
1848 struct dm_dev
*data_dev
;
1849 unsigned long block_size
;
1850 dm_block_t low_water_blocks
;
1851 struct dm_dev
*metadata_dev
;
1852 sector_t metadata_dev_size
;
1853 char b
[BDEVNAME_SIZE
];
1856 * FIXME Remove validation from scope of lock.
1858 mutex_lock(&dm_thin_pool_table
.mutex
);
1861 ti
->error
= "Invalid argument count";
1868 r
= dm_get_device(ti
, argv
[0], FMODE_READ
| FMODE_WRITE
, &metadata_dev
);
1870 ti
->error
= "Error opening metadata block device";
1874 metadata_dev_size
= i_size_read(metadata_dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
1875 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS_WARNING
)
1876 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1877 bdevname(metadata_dev
->bdev
, b
), THIN_METADATA_MAX_SECTORS
);
1879 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &data_dev
);
1881 ti
->error
= "Error getting data device";
1885 if (kstrtoul(argv
[2], 10, &block_size
) || !block_size
||
1886 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
1887 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
1888 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
1889 ti
->error
= "Invalid block size";
1894 if (kstrtoull(argv
[3], 10, (unsigned long long *)&low_water_blocks
)) {
1895 ti
->error
= "Invalid low water mark";
1901 * Set default pool features.
1903 pool_features_init(&pf
);
1905 dm_consume_args(&as
, 4);
1906 r
= parse_pool_features(&as
, &pf
, ti
);
1910 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
1916 pool
= __pool_find(dm_table_get_md(ti
->table
), metadata_dev
->bdev
,
1917 block_size
, pf
.mode
== PM_READ_ONLY
, &ti
->error
, &pool_created
);
1924 * 'pool_created' reflects whether this is the first table load.
1925 * Top level discard support is not allowed to be changed after
1926 * initial load. This would require a pool reload to trigger thin
1929 if (!pool_created
&& pf
.discard_enabled
!= pool
->pf
.discard_enabled
) {
1930 ti
->error
= "Discard support cannot be disabled once enabled";
1932 goto out_flags_changed
;
1937 pt
->metadata_dev
= metadata_dev
;
1938 pt
->data_dev
= data_dev
;
1939 pt
->low_water_blocks
= low_water_blocks
;
1940 pt
->adjusted_pf
= pt
->requested_pf
= pf
;
1941 ti
->num_flush_requests
= 1;
1944 * Only need to enable discards if the pool should pass
1945 * them down to the data device. The thin device's discard
1946 * processing will cause mappings to be removed from the btree.
1948 if (pf
.discard_enabled
&& pf
.discard_passdown
) {
1949 ti
->num_discard_requests
= 1;
1952 * Setting 'discards_supported' circumvents the normal
1953 * stacking of discard limits (this keeps the pool and
1954 * thin devices' discard limits consistent).
1956 ti
->discards_supported
= true;
1957 ti
->discard_zeroes_data_unsupported
= true;
1961 pt
->callbacks
.congested_fn
= pool_is_congested
;
1962 dm_table_add_target_callbacks(ti
->table
, &pt
->callbacks
);
1964 mutex_unlock(&dm_thin_pool_table
.mutex
);
1973 dm_put_device(ti
, data_dev
);
1975 dm_put_device(ti
, metadata_dev
);
1977 mutex_unlock(&dm_thin_pool_table
.mutex
);
1982 static int pool_map(struct dm_target
*ti
, struct bio
*bio
)
1985 struct pool_c
*pt
= ti
->private;
1986 struct pool
*pool
= pt
->pool
;
1987 unsigned long flags
;
1990 * As this is a singleton target, ti->begin is always zero.
1992 spin_lock_irqsave(&pool
->lock
, flags
);
1993 bio
->bi_bdev
= pt
->data_dev
->bdev
;
1994 r
= DM_MAPIO_REMAPPED
;
1995 spin_unlock_irqrestore(&pool
->lock
, flags
);
2001 * Retrieves the number of blocks of the data device from
2002 * the superblock and compares it to the actual device size,
2003 * thus resizing the data device in case it has grown.
2005 * This both copes with opening preallocated data devices in the ctr
2006 * being followed by a resume
2008 * calling the resume method individually after userspace has
2009 * grown the data device in reaction to a table event.
2011 static int pool_preresume(struct dm_target
*ti
)
2014 struct pool_c
*pt
= ti
->private;
2015 struct pool
*pool
= pt
->pool
;
2016 sector_t data_size
= ti
->len
;
2017 dm_block_t sb_data_size
;
2020 * Take control of the pool object.
2022 r
= bind_control_target(pool
, ti
);
2026 (void) sector_div(data_size
, pool
->sectors_per_block
);
2028 r
= dm_pool_get_data_dev_size(pool
->pmd
, &sb_data_size
);
2030 DMERR("failed to retrieve data device size");
2034 if (data_size
< sb_data_size
) {
2035 DMERR("pool target too small, is %llu blocks (expected %llu)",
2036 (unsigned long long)data_size
, sb_data_size
);
2039 } else if (data_size
> sb_data_size
) {
2040 r
= dm_pool_resize_data_dev(pool
->pmd
, data_size
);
2042 DMERR("failed to resize data device");
2043 /* FIXME Stricter than necessary: Rollback transaction instead here */
2044 set_pool_mode(pool
, PM_READ_ONLY
);
2048 (void) commit_or_fallback(pool
);
2054 static void pool_resume(struct dm_target
*ti
)
2056 struct pool_c
*pt
= ti
->private;
2057 struct pool
*pool
= pt
->pool
;
2058 unsigned long flags
;
2060 spin_lock_irqsave(&pool
->lock
, flags
);
2061 pool
->low_water_triggered
= 0;
2062 pool
->no_free_space
= 0;
2063 __requeue_bios(pool
);
2064 spin_unlock_irqrestore(&pool
->lock
, flags
);
2066 do_waker(&pool
->waker
.work
);
2069 static void pool_postsuspend(struct dm_target
*ti
)
2071 struct pool_c
*pt
= ti
->private;
2072 struct pool
*pool
= pt
->pool
;
2074 cancel_delayed_work(&pool
->waker
);
2075 flush_workqueue(pool
->wq
);
2076 (void) commit_or_fallback(pool
);
2079 static int check_arg_count(unsigned argc
, unsigned args_required
)
2081 if (argc
!= args_required
) {
2082 DMWARN("Message received with %u arguments instead of %u.",
2083 argc
, args_required
);
2090 static int read_dev_id(char *arg
, dm_thin_id
*dev_id
, int warning
)
2092 if (!kstrtoull(arg
, 10, (unsigned long long *)dev_id
) &&
2093 *dev_id
<= MAX_DEV_ID
)
2097 DMWARN("Message received with invalid device id: %s", arg
);
2102 static int process_create_thin_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2107 r
= check_arg_count(argc
, 2);
2111 r
= read_dev_id(argv
[1], &dev_id
, 1);
2115 r
= dm_pool_create_thin(pool
->pmd
, dev_id
);
2117 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2125 static int process_create_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2128 dm_thin_id origin_dev_id
;
2131 r
= check_arg_count(argc
, 3);
2135 r
= read_dev_id(argv
[1], &dev_id
, 1);
2139 r
= read_dev_id(argv
[2], &origin_dev_id
, 1);
2143 r
= dm_pool_create_snap(pool
->pmd
, dev_id
, origin_dev_id
);
2145 DMWARN("Creation of new snapshot %s of device %s failed.",
2153 static int process_delete_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2158 r
= check_arg_count(argc
, 2);
2162 r
= read_dev_id(argv
[1], &dev_id
, 1);
2166 r
= dm_pool_delete_thin_device(pool
->pmd
, dev_id
);
2168 DMWARN("Deletion of thin device %s failed.", argv
[1]);
2173 static int process_set_transaction_id_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2175 dm_thin_id old_id
, new_id
;
2178 r
= check_arg_count(argc
, 3);
2182 if (kstrtoull(argv
[1], 10, (unsigned long long *)&old_id
)) {
2183 DMWARN("set_transaction_id message: Unrecognised id %s.", argv
[1]);
2187 if (kstrtoull(argv
[2], 10, (unsigned long long *)&new_id
)) {
2188 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv
[2]);
2192 r
= dm_pool_set_metadata_transaction_id(pool
->pmd
, old_id
, new_id
);
2194 DMWARN("Failed to change transaction id from %s to %s.",
2202 static int process_reserve_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2206 r
= check_arg_count(argc
, 1);
2210 (void) commit_or_fallback(pool
);
2212 r
= dm_pool_reserve_metadata_snap(pool
->pmd
);
2214 DMWARN("reserve_metadata_snap message failed.");
2219 static int process_release_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2223 r
= check_arg_count(argc
, 1);
2227 r
= dm_pool_release_metadata_snap(pool
->pmd
);
2229 DMWARN("release_metadata_snap message failed.");
2235 * Messages supported:
2236 * create_thin <dev_id>
2237 * create_snap <dev_id> <origin_id>
2239 * trim <dev_id> <new_size_in_sectors>
2240 * set_transaction_id <current_trans_id> <new_trans_id>
2241 * reserve_metadata_snap
2242 * release_metadata_snap
2244 static int pool_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
2247 struct pool_c
*pt
= ti
->private;
2248 struct pool
*pool
= pt
->pool
;
2250 if (!strcasecmp(argv
[0], "create_thin"))
2251 r
= process_create_thin_mesg(argc
, argv
, pool
);
2253 else if (!strcasecmp(argv
[0], "create_snap"))
2254 r
= process_create_snap_mesg(argc
, argv
, pool
);
2256 else if (!strcasecmp(argv
[0], "delete"))
2257 r
= process_delete_mesg(argc
, argv
, pool
);
2259 else if (!strcasecmp(argv
[0], "set_transaction_id"))
2260 r
= process_set_transaction_id_mesg(argc
, argv
, pool
);
2262 else if (!strcasecmp(argv
[0], "reserve_metadata_snap"))
2263 r
= process_reserve_metadata_snap_mesg(argc
, argv
, pool
);
2265 else if (!strcasecmp(argv
[0], "release_metadata_snap"))
2266 r
= process_release_metadata_snap_mesg(argc
, argv
, pool
);
2269 DMWARN("Unrecognised thin pool target message received: %s", argv
[0]);
2272 (void) commit_or_fallback(pool
);
2277 static void emit_flags(struct pool_features
*pf
, char *result
,
2278 unsigned sz
, unsigned maxlen
)
2280 unsigned count
= !pf
->zero_new_blocks
+ !pf
->discard_enabled
+
2281 !pf
->discard_passdown
+ (pf
->mode
== PM_READ_ONLY
);
2282 DMEMIT("%u ", count
);
2284 if (!pf
->zero_new_blocks
)
2285 DMEMIT("skip_block_zeroing ");
2287 if (!pf
->discard_enabled
)
2288 DMEMIT("ignore_discard ");
2290 if (!pf
->discard_passdown
)
2291 DMEMIT("no_discard_passdown ");
2293 if (pf
->mode
== PM_READ_ONLY
)
2294 DMEMIT("read_only ");
2299 * <transaction id> <used metadata sectors>/<total metadata sectors>
2300 * <used data sectors>/<total data sectors> <held metadata root>
2302 static int pool_status(struct dm_target
*ti
, status_type_t type
,
2303 unsigned status_flags
, char *result
, unsigned maxlen
)
2307 uint64_t transaction_id
;
2308 dm_block_t nr_free_blocks_data
;
2309 dm_block_t nr_free_blocks_metadata
;
2310 dm_block_t nr_blocks_data
;
2311 dm_block_t nr_blocks_metadata
;
2312 dm_block_t held_root
;
2313 char buf
[BDEVNAME_SIZE
];
2314 char buf2
[BDEVNAME_SIZE
];
2315 struct pool_c
*pt
= ti
->private;
2316 struct pool
*pool
= pt
->pool
;
2319 case STATUSTYPE_INFO
:
2320 if (get_pool_mode(pool
) == PM_FAIL
) {
2325 /* Commit to ensure statistics aren't out-of-date */
2326 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
))
2327 (void) commit_or_fallback(pool
);
2329 r
= dm_pool_get_metadata_transaction_id(pool
->pmd
,
2334 r
= dm_pool_get_free_metadata_block_count(pool
->pmd
,
2335 &nr_free_blocks_metadata
);
2339 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &nr_blocks_metadata
);
2343 r
= dm_pool_get_free_block_count(pool
->pmd
,
2344 &nr_free_blocks_data
);
2348 r
= dm_pool_get_data_dev_size(pool
->pmd
, &nr_blocks_data
);
2352 r
= dm_pool_get_metadata_snap(pool
->pmd
, &held_root
);
2356 DMEMIT("%llu %llu/%llu %llu/%llu ",
2357 (unsigned long long)transaction_id
,
2358 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
2359 (unsigned long long)nr_blocks_metadata
,
2360 (unsigned long long)(nr_blocks_data
- nr_free_blocks_data
),
2361 (unsigned long long)nr_blocks_data
);
2364 DMEMIT("%llu ", held_root
);
2368 if (pool
->pf
.mode
== PM_READ_ONLY
)
2373 if (!pool
->pf
.discard_enabled
)
2374 DMEMIT("ignore_discard");
2375 else if (pool
->pf
.discard_passdown
)
2376 DMEMIT("discard_passdown");
2378 DMEMIT("no_discard_passdown");
2382 case STATUSTYPE_TABLE
:
2383 DMEMIT("%s %s %lu %llu ",
2384 format_dev_t(buf
, pt
->metadata_dev
->bdev
->bd_dev
),
2385 format_dev_t(buf2
, pt
->data_dev
->bdev
->bd_dev
),
2386 (unsigned long)pool
->sectors_per_block
,
2387 (unsigned long long)pt
->low_water_blocks
);
2388 emit_flags(&pt
->requested_pf
, result
, sz
, maxlen
);
2395 static int pool_iterate_devices(struct dm_target
*ti
,
2396 iterate_devices_callout_fn fn
, void *data
)
2398 struct pool_c
*pt
= ti
->private;
2400 return fn(ti
, pt
->data_dev
, 0, ti
->len
, data
);
2403 static int pool_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
2404 struct bio_vec
*biovec
, int max_size
)
2406 struct pool_c
*pt
= ti
->private;
2407 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
2409 if (!q
->merge_bvec_fn
)
2412 bvm
->bi_bdev
= pt
->data_dev
->bdev
;
2414 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
2417 static bool block_size_is_power_of_two(struct pool
*pool
)
2419 return pool
->sectors_per_block_shift
>= 0;
2422 static void set_discard_limits(struct pool_c
*pt
, struct queue_limits
*limits
)
2424 struct pool
*pool
= pt
->pool
;
2425 struct queue_limits
*data_limits
;
2427 limits
->max_discard_sectors
= pool
->sectors_per_block
;
2430 * discard_granularity is just a hint, and not enforced.
2432 if (pt
->adjusted_pf
.discard_passdown
) {
2433 data_limits
= &bdev_get_queue(pt
->data_dev
->bdev
)->limits
;
2434 limits
->discard_granularity
= data_limits
->discard_granularity
;
2435 } else if (block_size_is_power_of_two(pool
))
2436 limits
->discard_granularity
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
2439 * Use largest power of 2 that is a factor of sectors_per_block
2440 * but at least DATA_DEV_BLOCK_SIZE_MIN_SECTORS.
2442 limits
->discard_granularity
= max(1 << (ffs(pool
->sectors_per_block
) - 1),
2443 DATA_DEV_BLOCK_SIZE_MIN_SECTORS
) << SECTOR_SHIFT
;
2446 static void pool_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
2448 struct pool_c
*pt
= ti
->private;
2449 struct pool
*pool
= pt
->pool
;
2451 blk_limits_io_min(limits
, 0);
2452 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
2455 * pt->adjusted_pf is a staging area for the actual features to use.
2456 * They get transferred to the live pool in bind_control_target()
2457 * called from pool_preresume().
2459 if (!pt
->adjusted_pf
.discard_enabled
)
2462 disable_passdown_if_not_supported(pt
);
2464 set_discard_limits(pt
, limits
);
2467 static struct target_type pool_target
= {
2468 .name
= "thin-pool",
2469 .features
= DM_TARGET_SINGLETON
| DM_TARGET_ALWAYS_WRITEABLE
|
2470 DM_TARGET_IMMUTABLE
,
2471 .version
= {1, 6, 0},
2472 .module
= THIS_MODULE
,
2476 .postsuspend
= pool_postsuspend
,
2477 .preresume
= pool_preresume
,
2478 .resume
= pool_resume
,
2479 .message
= pool_message
,
2480 .status
= pool_status
,
2481 .merge
= pool_merge
,
2482 .iterate_devices
= pool_iterate_devices
,
2483 .io_hints
= pool_io_hints
,
2486 /*----------------------------------------------------------------
2487 * Thin target methods
2488 *--------------------------------------------------------------*/
2489 static void thin_dtr(struct dm_target
*ti
)
2491 struct thin_c
*tc
= ti
->private;
2493 mutex_lock(&dm_thin_pool_table
.mutex
);
2495 __pool_dec(tc
->pool
);
2496 dm_pool_close_thin_device(tc
->td
);
2497 dm_put_device(ti
, tc
->pool_dev
);
2499 dm_put_device(ti
, tc
->origin_dev
);
2502 mutex_unlock(&dm_thin_pool_table
.mutex
);
2506 * Thin target parameters:
2508 * <pool_dev> <dev_id> [origin_dev]
2510 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2511 * dev_id: the internal device identifier
2512 * origin_dev: a device external to the pool that should act as the origin
2514 * If the pool device has discards disabled, they get disabled for the thin
2517 static int thin_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2521 struct dm_dev
*pool_dev
, *origin_dev
;
2522 struct mapped_device
*pool_md
;
2524 mutex_lock(&dm_thin_pool_table
.mutex
);
2526 if (argc
!= 2 && argc
!= 3) {
2527 ti
->error
= "Invalid argument count";
2532 tc
= ti
->private = kzalloc(sizeof(*tc
), GFP_KERNEL
);
2534 ti
->error
= "Out of memory";
2540 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &origin_dev
);
2542 ti
->error
= "Error opening origin device";
2543 goto bad_origin_dev
;
2545 tc
->origin_dev
= origin_dev
;
2548 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &pool_dev
);
2550 ti
->error
= "Error opening pool device";
2553 tc
->pool_dev
= pool_dev
;
2555 if (read_dev_id(argv
[1], (unsigned long long *)&tc
->dev_id
, 0)) {
2556 ti
->error
= "Invalid device id";
2561 pool_md
= dm_get_md(tc
->pool_dev
->bdev
->bd_dev
);
2563 ti
->error
= "Couldn't get pool mapped device";
2568 tc
->pool
= __pool_table_lookup(pool_md
);
2570 ti
->error
= "Couldn't find pool object";
2572 goto bad_pool_lookup
;
2574 __pool_inc(tc
->pool
);
2576 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
2577 ti
->error
= "Couldn't open thin device, Pool is in fail mode";
2581 r
= dm_pool_open_thin_device(tc
->pool
->pmd
, tc
->dev_id
, &tc
->td
);
2583 ti
->error
= "Couldn't open thin internal device";
2587 r
= dm_set_target_max_io_len(ti
, tc
->pool
->sectors_per_block
);
2591 ti
->num_flush_requests
= 1;
2592 ti
->flush_supported
= true;
2593 ti
->per_bio_data_size
= sizeof(struct dm_thin_endio_hook
);
2595 /* In case the pool supports discards, pass them on. */
2596 if (tc
->pool
->pf
.discard_enabled
) {
2597 ti
->discards_supported
= true;
2598 ti
->num_discard_requests
= 1;
2599 ti
->discard_zeroes_data_unsupported
= true;
2600 /* Discard requests must be split on a block boundary */
2601 ti
->split_discard_requests
= true;
2606 mutex_unlock(&dm_thin_pool_table
.mutex
);
2611 __pool_dec(tc
->pool
);
2615 dm_put_device(ti
, tc
->pool_dev
);
2618 dm_put_device(ti
, tc
->origin_dev
);
2622 mutex_unlock(&dm_thin_pool_table
.mutex
);
2627 static int thin_map(struct dm_target
*ti
, struct bio
*bio
)
2629 bio
->bi_sector
= dm_target_offset(ti
, bio
->bi_sector
);
2631 return thin_bio_map(ti
, bio
);
2634 static int thin_endio(struct dm_target
*ti
, struct bio
*bio
, int err
)
2636 unsigned long flags
;
2637 struct dm_thin_endio_hook
*h
= dm_per_bio_data(bio
, sizeof(struct dm_thin_endio_hook
));
2638 struct list_head work
;
2639 struct dm_thin_new_mapping
*m
, *tmp
;
2640 struct pool
*pool
= h
->tc
->pool
;
2642 if (h
->shared_read_entry
) {
2643 INIT_LIST_HEAD(&work
);
2644 dm_deferred_entry_dec(h
->shared_read_entry
, &work
);
2646 spin_lock_irqsave(&pool
->lock
, flags
);
2647 list_for_each_entry_safe(m
, tmp
, &work
, list
) {
2650 __maybe_add_mapping(m
);
2652 spin_unlock_irqrestore(&pool
->lock
, flags
);
2655 if (h
->all_io_entry
) {
2656 INIT_LIST_HEAD(&work
);
2657 dm_deferred_entry_dec(h
->all_io_entry
, &work
);
2658 if (!list_empty(&work
)) {
2659 spin_lock_irqsave(&pool
->lock
, flags
);
2660 list_for_each_entry_safe(m
, tmp
, &work
, list
)
2661 list_add(&m
->list
, &pool
->prepared_discards
);
2662 spin_unlock_irqrestore(&pool
->lock
, flags
);
2670 static void thin_postsuspend(struct dm_target
*ti
)
2672 if (dm_noflush_suspending(ti
))
2673 requeue_io((struct thin_c
*)ti
->private);
2677 * <nr mapped sectors> <highest mapped sector>
2679 static int thin_status(struct dm_target
*ti
, status_type_t type
,
2680 unsigned status_flags
, char *result
, unsigned maxlen
)
2684 dm_block_t mapped
, highest
;
2685 char buf
[BDEVNAME_SIZE
];
2686 struct thin_c
*tc
= ti
->private;
2688 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
2697 case STATUSTYPE_INFO
:
2698 r
= dm_thin_get_mapped_count(tc
->td
, &mapped
);
2702 r
= dm_thin_get_highest_mapped_block(tc
->td
, &highest
);
2706 DMEMIT("%llu ", mapped
* tc
->pool
->sectors_per_block
);
2708 DMEMIT("%llu", ((highest
+ 1) *
2709 tc
->pool
->sectors_per_block
) - 1);
2714 case STATUSTYPE_TABLE
:
2716 format_dev_t(buf
, tc
->pool_dev
->bdev
->bd_dev
),
2717 (unsigned long) tc
->dev_id
);
2719 DMEMIT(" %s", format_dev_t(buf
, tc
->origin_dev
->bdev
->bd_dev
));
2727 static int thin_iterate_devices(struct dm_target
*ti
,
2728 iterate_devices_callout_fn fn
, void *data
)
2731 struct thin_c
*tc
= ti
->private;
2732 struct pool
*pool
= tc
->pool
;
2735 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2736 * we follow a more convoluted path through to the pool's target.
2739 return 0; /* nothing is bound */
2741 blocks
= pool
->ti
->len
;
2742 (void) sector_div(blocks
, pool
->sectors_per_block
);
2744 return fn(ti
, tc
->pool_dev
, 0, pool
->sectors_per_block
* blocks
, data
);
2749 static struct target_type thin_target
= {
2751 .version
= {1, 7, 0},
2752 .module
= THIS_MODULE
,
2756 .end_io
= thin_endio
,
2757 .postsuspend
= thin_postsuspend
,
2758 .status
= thin_status
,
2759 .iterate_devices
= thin_iterate_devices
,
2762 /*----------------------------------------------------------------*/
2764 static int __init
dm_thin_init(void)
2770 r
= dm_register_target(&thin_target
);
2774 r
= dm_register_target(&pool_target
);
2776 goto bad_pool_target
;
2780 _new_mapping_cache
= KMEM_CACHE(dm_thin_new_mapping
, 0);
2781 if (!_new_mapping_cache
)
2782 goto bad_new_mapping_cache
;
2786 bad_new_mapping_cache
:
2787 dm_unregister_target(&pool_target
);
2789 dm_unregister_target(&thin_target
);
2794 static void dm_thin_exit(void)
2796 dm_unregister_target(&thin_target
);
2797 dm_unregister_target(&pool_target
);
2799 kmem_cache_destroy(_new_mapping_cache
);
2802 module_init(dm_thin_init
);
2803 module_exit(dm_thin_exit
);
2805 MODULE_DESCRIPTION(DM_NAME
" thin provisioning target");
2806 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2807 MODULE_LICENSE("GPL");