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
28 #define NO_SPACE_TIMEOUT_SECS 60
30 static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
32 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
33 "A percentage of time allocated for copy on write");
36 * The block size of the device holding pool data must be
37 * between 64KB and 1GB.
39 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
40 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
43 * Device id is restricted to 24 bits.
45 #define MAX_DEV_ID ((1 << 24) - 1)
48 * How do we handle breaking sharing of data blocks?
49 * =================================================
51 * We use a standard copy-on-write btree to store the mappings for the
52 * devices (note I'm talking about copy-on-write of the metadata here, not
53 * the data). When you take an internal snapshot you clone the root node
54 * of the origin btree. After this there is no concept of an origin or a
55 * snapshot. They are just two device trees that happen to point to the
58 * When we get a write in we decide if it's to a shared data block using
59 * some timestamp magic. If it is, we have to break sharing.
61 * Let's say we write to a shared block in what was the origin. The
64 * i) plug io further to this physical block. (see bio_prison code).
66 * ii) quiesce any read io to that shared data block. Obviously
67 * including all devices that share this block. (see dm_deferred_set code)
69 * iii) copy the data block to a newly allocate block. This step can be
70 * missed out if the io covers the block. (schedule_copy).
72 * iv) insert the new mapping into the origin's btree
73 * (process_prepared_mapping). This act of inserting breaks some
74 * sharing of btree nodes between the two devices. Breaking sharing only
75 * effects the btree of that specific device. Btrees for the other
76 * devices that share the block never change. The btree for the origin
77 * device as it was after the last commit is untouched, ie. we're using
78 * persistent data structures in the functional programming sense.
80 * v) unplug io to this physical block, including the io that triggered
81 * the breaking of sharing.
83 * Steps (ii) and (iii) occur in parallel.
85 * The metadata _doesn't_ need to be committed before the io continues. We
86 * get away with this because the io is always written to a _new_ block.
87 * If there's a crash, then:
89 * - The origin mapping will point to the old origin block (the shared
90 * one). This will contain the data as it was before the io that triggered
91 * the breaking of sharing came in.
93 * - The snap mapping still points to the old block. As it would after
96 * The downside of this scheme is the timestamp magic isn't perfect, and
97 * will continue to think that data block in the snapshot device is shared
98 * even after the write to the origin has broken sharing. I suspect data
99 * blocks will typically be shared by many different devices, so we're
100 * breaking sharing n + 1 times, rather than n, where n is the number of
101 * devices that reference this data block. At the moment I think the
102 * benefits far, far outweigh the disadvantages.
105 /*----------------------------------------------------------------*/
110 static void build_data_key(struct dm_thin_device *td,
111 dm_block_t b, struct dm_cell_key *key)
114 key->dev = dm_thin_dev_id(td);
118 static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
119 struct dm_cell_key *key)
122 key->dev = dm_thin_dev_id(td);
126 /*----------------------------------------------------------------*/
129 * A pool device ties together a metadata device and a data device. It
130 * also provides the interface for creating and destroying internal
133 struct dm_thin_new_mapping;
136 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
139 PM_WRITE, /* metadata may be changed */
140 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
141 PM_READ_ONLY, /* metadata may not be changed */
142 PM_FAIL, /* all I/O fails */
145 struct pool_features {
148 bool zero_new_blocks:1;
149 bool discard_enabled:1;
150 bool discard_passdown:1;
151 bool error_if_no_space:1;
155 typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
156 typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
159 struct list_head list;
160 struct dm_target *ti; /* Only set if a pool target is bound */
162 struct mapped_device *pool_md;
163 struct block_device *md_dev;
164 struct dm_pool_metadata *pmd;
166 dm_block_t low_water_blocks;
167 uint32_t sectors_per_block;
168 int sectors_per_block_shift;
170 struct pool_features pf;
171 bool low_water_triggered:1; /* A dm event has been sent */
173 struct dm_bio_prison *prison;
174 struct dm_kcopyd_client *copier;
176 struct workqueue_struct *wq;
177 struct work_struct worker;
178 struct delayed_work waker;
179 struct delayed_work no_space_timeout;
181 unsigned long last_commit_jiffies;
185 struct bio_list deferred_bios;
186 struct bio_list deferred_flush_bios;
187 struct list_head prepared_mappings;
188 struct list_head prepared_discards;
190 struct bio_list retry_on_resume_list;
192 struct dm_deferred_set *shared_read_ds;
193 struct dm_deferred_set *all_io_ds;
195 struct dm_thin_new_mapping *next_mapping;
196 mempool_t *mapping_pool;
198 process_bio_fn process_bio;
199 process_bio_fn process_discard;
201 process_mapping_fn process_prepared_mapping;
202 process_mapping_fn process_prepared_discard;
205 static enum pool_mode get_pool_mode(struct pool *pool);
206 static void metadata_operation_failed(struct pool *pool, const char *op, int r);
209 * Target context for a pool.
212 struct dm_target *ti;
214 struct dm_dev *data_dev;
215 struct dm_dev *metadata_dev;
216 struct dm_target_callbacks callbacks;
218 dm_block_t low_water_blocks;
219 struct pool_features requested_pf; /* Features requested during table load */
220 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
224 * Target context for a thin.
227 struct dm_dev *pool_dev;
228 struct dm_dev *origin_dev;
232 struct dm_thin_device *td;
236 /*----------------------------------------------------------------*/
239 * wake_worker() is used when new work is queued and when pool_resume is
240 * ready to continue deferred IO processing.
242 static void wake_worker(struct pool *pool)
244 queue_work(pool->wq, &pool->worker);
247 /*----------------------------------------------------------------*/
249 static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
250 struct dm_bio_prison_cell **cell_result)
253 struct dm_bio_prison_cell *cell_prealloc;
256 * Allocate a cell from the prison's mempool.
257 * This might block but it can't fail.
259 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
261 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
264 * We reused an old cell; we can get rid of
267 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
272 static void cell_release(struct pool *pool,
273 struct dm_bio_prison_cell *cell,
274 struct bio_list *bios)
276 dm_cell_release(pool->prison, cell, bios);
277 dm_bio_prison_free_cell(pool->prison, cell);
280 static void cell_release_no_holder(struct pool *pool,
281 struct dm_bio_prison_cell *cell,
282 struct bio_list *bios)
284 dm_cell_release_no_holder(pool->prison, cell, bios);
285 dm_bio_prison_free_cell(pool->prison, cell);
288 static void cell_defer_no_holder_no_free(struct thin_c *tc,
289 struct dm_bio_prison_cell *cell)
291 struct pool *pool = tc->pool;
294 spin_lock_irqsave(&pool->lock, flags);
295 dm_cell_release_no_holder(pool->prison, cell, &pool->deferred_bios);
296 spin_unlock_irqrestore(&pool->lock, flags);
301 static void cell_error(struct pool *pool,
302 struct dm_bio_prison_cell *cell)
304 dm_cell_error(pool->prison, cell);
305 dm_bio_prison_free_cell(pool->prison, cell);
308 /*----------------------------------------------------------------*/
311 * A global list of pools that uses a struct mapped_device as a key.
313 static struct dm_thin_pool_table {
315 struct list_head pools;
316 } dm_thin_pool_table;
318 static void pool_table_init(void)
320 mutex_init(&dm_thin_pool_table.mutex);
321 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
324 static void __pool_table_insert(struct pool *pool)
326 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
327 list_add(&pool->list, &dm_thin_pool_table.pools);
330 static void __pool_table_remove(struct pool *pool)
332 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
333 list_del(&pool->list);
336 static struct pool *__pool_table_lookup(struct mapped_device *md)
338 struct pool *pool = NULL, *tmp;
340 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
342 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
343 if (tmp->pool_md == md) {
352 static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
354 struct pool *pool = NULL, *tmp;
356 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
358 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
359 if (tmp->md_dev == md_dev) {
368 /*----------------------------------------------------------------*/
370 struct dm_thin_endio_hook {
372 struct dm_deferred_entry *shared_read_entry;
373 struct dm_deferred_entry *all_io_entry;
374 struct dm_thin_new_mapping *overwrite_mapping;
377 static void requeue_bio_list(struct thin_c *tc, struct bio_list *master)
380 struct bio_list bios;
383 bio_list_init(&bios);
385 spin_lock_irqsave(&tc->pool->lock, flags);
386 bio_list_merge(&bios, master);
387 bio_list_init(master);
388 spin_unlock_irqrestore(&tc->pool->lock, flags);
390 while ((bio = bio_list_pop(&bios))) {
391 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
394 bio_endio(bio, DM_ENDIO_REQUEUE);
396 bio_list_add(master, bio);
400 static void requeue_io(struct thin_c *tc)
402 struct pool *pool = tc->pool;
404 requeue_bio_list(tc, &pool->deferred_bios);
405 requeue_bio_list(tc, &pool->retry_on_resume_list);
408 static void error_retry_list(struct pool *pool)
412 struct bio_list bios;
414 bio_list_init(&bios);
416 spin_lock_irqsave(&pool->lock, flags);
417 bio_list_merge(&bios, &pool->retry_on_resume_list);
418 bio_list_init(&pool->retry_on_resume_list);
419 spin_unlock_irqrestore(&pool->lock, flags);
421 while ((bio = bio_list_pop(&bios)))
426 * This section of code contains the logic for processing a thin device's IO.
427 * Much of the code depends on pool object resources (lists, workqueues, etc)
428 * but most is exclusively called from the thin target rather than the thin-pool
432 static bool block_size_is_power_of_two(struct pool *pool)
434 return pool->sectors_per_block_shift >= 0;
437 static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
439 struct pool *pool = tc->pool;
440 sector_t block_nr = bio->bi_iter.bi_sector;
442 if (block_size_is_power_of_two(pool))
443 block_nr >>= pool->sectors_per_block_shift;
445 (void) sector_div(block_nr, pool->sectors_per_block);
450 static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
452 struct pool *pool = tc->pool;
453 sector_t bi_sector = bio->bi_iter.bi_sector;
455 bio->bi_bdev = tc->pool_dev->bdev;
456 if (block_size_is_power_of_two(pool))
457 bio->bi_iter.bi_sector =
458 (block << pool->sectors_per_block_shift) |
459 (bi_sector & (pool->sectors_per_block - 1));
461 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
462 sector_div(bi_sector, pool->sectors_per_block);
465 static void remap_to_origin(struct thin_c *tc, struct bio *bio)
467 bio->bi_bdev = tc->origin_dev->bdev;
470 static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
472 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
473 dm_thin_changed_this_transaction(tc->td);
476 static void inc_all_io_entry(struct pool *pool, struct bio *bio)
478 struct dm_thin_endio_hook *h;
480 if (bio->bi_rw & REQ_DISCARD)
483 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
484 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
487 static void issue(struct thin_c *tc, struct bio *bio)
489 struct pool *pool = tc->pool;
492 if (!bio_triggers_commit(tc, bio)) {
493 generic_make_request(bio);
498 * Complete bio with an error if earlier I/O caused changes to
499 * the metadata that can't be committed e.g, due to I/O errors
500 * on the metadata device.
502 if (dm_thin_aborted_changes(tc->td)) {
508 * Batch together any bios that trigger commits and then issue a
509 * single commit for them in process_deferred_bios().
511 spin_lock_irqsave(&pool->lock, flags);
512 bio_list_add(&pool->deferred_flush_bios, bio);
513 spin_unlock_irqrestore(&pool->lock, flags);
516 static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
518 remap_to_origin(tc, bio);
522 static void remap_and_issue(struct thin_c *tc, struct bio *bio,
525 remap(tc, bio, block);
529 /*----------------------------------------------------------------*/
532 * Bio endio functions.
534 struct dm_thin_new_mapping {
535 struct list_head list;
540 bool definitely_not_shared:1;
544 dm_block_t virt_block;
545 dm_block_t data_block;
546 struct dm_bio_prison_cell *cell, *cell2;
549 * If the bio covers the whole area of a block then we can avoid
550 * zeroing or copying. Instead this bio is hooked. The bio will
551 * still be in the cell, so care has to be taken to avoid issuing
555 bio_end_io_t *saved_bi_end_io;
558 static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
560 struct pool *pool = m->tc->pool;
562 if (m->quiesced && m->prepared) {
563 list_add_tail(&m->list, &pool->prepared_mappings);
568 static void copy_complete(int read_err, unsigned long write_err, void *context)
571 struct dm_thin_new_mapping *m = context;
572 struct pool *pool = m->tc->pool;
574 m->err = read_err || write_err ? -EIO : 0;
576 spin_lock_irqsave(&pool->lock, flags);
578 __maybe_add_mapping(m);
579 spin_unlock_irqrestore(&pool->lock, flags);
582 static void overwrite_endio(struct bio *bio, int err)
585 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
586 struct dm_thin_new_mapping *m = h->overwrite_mapping;
587 struct pool *pool = m->tc->pool;
591 spin_lock_irqsave(&pool->lock, flags);
593 __maybe_add_mapping(m);
594 spin_unlock_irqrestore(&pool->lock, flags);
597 /*----------------------------------------------------------------*/
604 * Prepared mapping jobs.
608 * This sends the bios in the cell back to the deferred_bios list.
610 static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
612 struct pool *pool = tc->pool;
615 spin_lock_irqsave(&pool->lock, flags);
616 cell_release(pool, cell, &pool->deferred_bios);
617 spin_unlock_irqrestore(&tc->pool->lock, flags);
623 * Same as cell_defer above, except it omits the original holder of the cell.
625 static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
627 struct pool *pool = tc->pool;
630 spin_lock_irqsave(&pool->lock, flags);
631 cell_release_no_holder(pool, cell, &pool->deferred_bios);
632 spin_unlock_irqrestore(&pool->lock, flags);
637 static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
640 m->bio->bi_end_io = m->saved_bi_end_io;
641 atomic_inc(&m->bio->bi_remaining);
643 cell_error(m->tc->pool, m->cell);
645 mempool_free(m, m->tc->pool->mapping_pool);
648 static void process_prepared_mapping(struct dm_thin_new_mapping *m)
650 struct thin_c *tc = m->tc;
651 struct pool *pool = tc->pool;
657 bio->bi_end_io = m->saved_bi_end_io;
658 atomic_inc(&bio->bi_remaining);
662 cell_error(pool, m->cell);
667 * Commit the prepared block into the mapping btree.
668 * Any I/O for this block arriving after this point will get
669 * remapped to it directly.
671 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
673 metadata_operation_failed(pool, "dm_thin_insert_block", r);
674 cell_error(pool, m->cell);
679 * Release any bios held while the block was being provisioned.
680 * If we are processing a write bio that completely covers the block,
681 * we already processed it so can ignore it now when processing
682 * the bios in the cell.
685 cell_defer_no_holder(tc, m->cell);
688 cell_defer(tc, m->cell);
692 mempool_free(m, pool->mapping_pool);
695 static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
697 struct thin_c *tc = m->tc;
699 bio_io_error(m->bio);
700 cell_defer_no_holder(tc, m->cell);
701 cell_defer_no_holder(tc, m->cell2);
702 mempool_free(m, tc->pool->mapping_pool);
705 static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
707 struct thin_c *tc = m->tc;
709 inc_all_io_entry(tc->pool, m->bio);
710 cell_defer_no_holder(tc, m->cell);
711 cell_defer_no_holder(tc, m->cell2);
714 if (m->definitely_not_shared)
715 remap_and_issue(tc, m->bio, m->data_block);
718 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
719 bio_endio(m->bio, 0);
721 remap_and_issue(tc, m->bio, m->data_block);
724 bio_endio(m->bio, 0);
726 mempool_free(m, tc->pool->mapping_pool);
729 static void process_prepared_discard(struct dm_thin_new_mapping *m)
732 struct thin_c *tc = m->tc;
734 r = dm_thin_remove_block(tc->td, m->virt_block);
736 DMERR_LIMIT("dm_thin_remove_block() failed");
738 process_prepared_discard_passdown(m);
741 static void process_prepared(struct pool *pool, struct list_head *head,
742 process_mapping_fn *fn)
745 struct list_head maps;
746 struct dm_thin_new_mapping *m, *tmp;
748 INIT_LIST_HEAD(&maps);
749 spin_lock_irqsave(&pool->lock, flags);
750 list_splice_init(head, &maps);
751 spin_unlock_irqrestore(&pool->lock, flags);
753 list_for_each_entry_safe(m, tmp, &maps, list)
760 static int io_overlaps_block(struct pool *pool, struct bio *bio)
762 return bio->bi_iter.bi_size ==
763 (pool->sectors_per_block << SECTOR_SHIFT);
766 static int io_overwrites_block(struct pool *pool, struct bio *bio)
768 return (bio_data_dir(bio) == WRITE) &&
769 io_overlaps_block(pool, bio);
772 static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
775 *save = bio->bi_end_io;
779 static int ensure_next_mapping(struct pool *pool)
781 if (pool->next_mapping)
784 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
786 return pool->next_mapping ? 0 : -ENOMEM;
789 static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
791 struct dm_thin_new_mapping *m = pool->next_mapping;
793 BUG_ON(!pool->next_mapping);
795 memset(m, 0, sizeof(struct dm_thin_new_mapping));
796 INIT_LIST_HEAD(&m->list);
799 pool->next_mapping = NULL;
804 static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
805 struct dm_dev *origin, dm_block_t data_origin,
806 dm_block_t data_dest,
807 struct dm_bio_prison_cell *cell, struct bio *bio)
810 struct pool *pool = tc->pool;
811 struct dm_thin_new_mapping *m = get_next_mapping(pool);
814 m->virt_block = virt_block;
815 m->data_block = data_dest;
818 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
822 * IO to pool_dev remaps to the pool target's data_dev.
824 * If the whole block of data is being overwritten, we can issue the
825 * bio immediately. Otherwise we use kcopyd to clone the data first.
827 if (io_overwrites_block(pool, bio)) {
828 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
830 h->overwrite_mapping = m;
832 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
833 inc_all_io_entry(pool, bio);
834 remap_and_issue(tc, bio, data_dest);
836 struct dm_io_region from, to;
838 from.bdev = origin->bdev;
839 from.sector = data_origin * pool->sectors_per_block;
840 from.count = pool->sectors_per_block;
842 to.bdev = tc->pool_dev->bdev;
843 to.sector = data_dest * pool->sectors_per_block;
844 to.count = pool->sectors_per_block;
846 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
847 0, copy_complete, m);
849 mempool_free(m, pool->mapping_pool);
850 DMERR_LIMIT("dm_kcopyd_copy() failed");
851 cell_error(pool, cell);
856 static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
857 dm_block_t data_origin, dm_block_t data_dest,
858 struct dm_bio_prison_cell *cell, struct bio *bio)
860 schedule_copy(tc, virt_block, tc->pool_dev,
861 data_origin, data_dest, cell, bio);
864 static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
865 dm_block_t data_dest,
866 struct dm_bio_prison_cell *cell, struct bio *bio)
868 schedule_copy(tc, virt_block, tc->origin_dev,
869 virt_block, data_dest, cell, bio);
872 static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
873 dm_block_t data_block, struct dm_bio_prison_cell *cell,
876 struct pool *pool = tc->pool;
877 struct dm_thin_new_mapping *m = get_next_mapping(pool);
882 m->virt_block = virt_block;
883 m->data_block = data_block;
887 * If the whole block of data is being overwritten or we are not
888 * zeroing pre-existing data, we can issue the bio immediately.
889 * Otherwise we use kcopyd to zero the data first.
891 if (!pool->pf.zero_new_blocks)
892 process_prepared_mapping(m);
894 else if (io_overwrites_block(pool, bio)) {
895 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
897 h->overwrite_mapping = m;
899 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
900 inc_all_io_entry(pool, bio);
901 remap_and_issue(tc, bio, data_block);
904 struct dm_io_region to;
906 to.bdev = tc->pool_dev->bdev;
907 to.sector = data_block * pool->sectors_per_block;
908 to.count = pool->sectors_per_block;
910 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
912 mempool_free(m, pool->mapping_pool);
913 DMERR_LIMIT("dm_kcopyd_zero() failed");
914 cell_error(pool, cell);
920 * A non-zero return indicates read_only or fail_io mode.
921 * Many callers don't care about the return value.
923 static int commit(struct pool *pool)
927 if (get_pool_mode(pool) >= PM_READ_ONLY)
930 r = dm_pool_commit_metadata(pool->pmd);
932 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
937 static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
941 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
942 DMWARN("%s: reached low water mark for data device: sending event.",
943 dm_device_name(pool->pool_md));
944 spin_lock_irqsave(&pool->lock, flags);
945 pool->low_water_triggered = true;
946 spin_unlock_irqrestore(&pool->lock, flags);
947 dm_table_event(pool->ti->table);
951 static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
953 static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
956 dm_block_t free_blocks;
957 struct pool *pool = tc->pool;
959 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
962 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
964 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
968 check_low_water_mark(pool, free_blocks);
972 * Try to commit to see if that will free up some
979 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
981 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
986 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
991 r = dm_pool_alloc_data_block(pool->pmd, result);
993 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
1001 * If we have run out of space, queue bios until the device is
1002 * resumed, presumably after having been reloaded with more space.
1004 static void retry_on_resume(struct bio *bio)
1006 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1007 struct thin_c *tc = h->tc;
1008 struct pool *pool = tc->pool;
1009 unsigned long flags;
1011 spin_lock_irqsave(&pool->lock, flags);
1012 bio_list_add(&pool->retry_on_resume_list, bio);
1013 spin_unlock_irqrestore(&pool->lock, flags);
1016 static bool should_error_unserviceable_bio(struct pool *pool)
1018 enum pool_mode m = get_pool_mode(pool);
1022 /* Shouldn't get here */
1023 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1026 case PM_OUT_OF_DATA_SPACE:
1027 return pool->pf.error_if_no_space;
1033 /* Shouldn't get here */
1034 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1039 static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1041 if (should_error_unserviceable_bio(pool))
1044 retry_on_resume(bio);
1047 static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
1050 struct bio_list bios;
1052 if (should_error_unserviceable_bio(pool)) {
1053 cell_error(pool, cell);
1057 bio_list_init(&bios);
1058 cell_release(pool, cell, &bios);
1060 if (should_error_unserviceable_bio(pool))
1061 while ((bio = bio_list_pop(&bios)))
1064 while ((bio = bio_list_pop(&bios)))
1065 retry_on_resume(bio);
1068 static void process_discard(struct thin_c *tc, struct bio *bio)
1071 unsigned long flags;
1072 struct pool *pool = tc->pool;
1073 struct dm_bio_prison_cell *cell, *cell2;
1074 struct dm_cell_key key, key2;
1075 dm_block_t block = get_bio_block(tc, bio);
1076 struct dm_thin_lookup_result lookup_result;
1077 struct dm_thin_new_mapping *m;
1079 build_virtual_key(tc->td, block, &key);
1080 if (bio_detain(tc->pool, &key, bio, &cell))
1083 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1087 * Check nobody is fiddling with this pool block. This can
1088 * happen if someone's in the process of breaking sharing
1091 build_data_key(tc->td, lookup_result.block, &key2);
1092 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
1093 cell_defer_no_holder(tc, cell);
1097 if (io_overlaps_block(pool, bio)) {
1099 * IO may still be going to the destination block. We must
1100 * quiesce before we can do the removal.
1102 m = get_next_mapping(pool);
1104 m->pass_discard = pool->pf.discard_passdown;
1105 m->definitely_not_shared = !lookup_result.shared;
1106 m->virt_block = block;
1107 m->data_block = lookup_result.block;
1112 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
1113 spin_lock_irqsave(&pool->lock, flags);
1114 list_add_tail(&m->list, &pool->prepared_discards);
1115 spin_unlock_irqrestore(&pool->lock, flags);
1119 inc_all_io_entry(pool, bio);
1120 cell_defer_no_holder(tc, cell);
1121 cell_defer_no_holder(tc, cell2);
1124 * The DM core makes sure that the discard doesn't span
1125 * a block boundary. So we submit the discard of a
1126 * partial block appropriately.
1128 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1129 remap_and_issue(tc, bio, lookup_result.block);
1137 * It isn't provisioned, just forget it.
1139 cell_defer_no_holder(tc, cell);
1144 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1146 cell_defer_no_holder(tc, cell);
1152 static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
1153 struct dm_cell_key *key,
1154 struct dm_thin_lookup_result *lookup_result,
1155 struct dm_bio_prison_cell *cell)
1158 dm_block_t data_block;
1159 struct pool *pool = tc->pool;
1161 r = alloc_data_block(tc, &data_block);
1164 schedule_internal_copy(tc, block, lookup_result->block,
1165 data_block, cell, bio);
1169 retry_bios_on_resume(pool, cell);
1173 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1175 cell_error(pool, cell);
1180 static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1182 struct dm_thin_lookup_result *lookup_result)
1184 struct dm_bio_prison_cell *cell;
1185 struct pool *pool = tc->pool;
1186 struct dm_cell_key key;
1189 * If cell is already occupied, then sharing is already in the process
1190 * of being broken so we have nothing further to do here.
1192 build_data_key(tc->td, lookup_result->block, &key);
1193 if (bio_detain(pool, &key, bio, &cell))
1196 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size)
1197 break_sharing(tc, bio, block, &key, lookup_result, cell);
1199 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1201 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
1202 inc_all_io_entry(pool, bio);
1203 cell_defer_no_holder(tc, cell);
1205 remap_and_issue(tc, bio, lookup_result->block);
1209 static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
1210 struct dm_bio_prison_cell *cell)
1213 dm_block_t data_block;
1214 struct pool *pool = tc->pool;
1217 * Remap empty bios (flushes) immediately, without provisioning.
1219 if (!bio->bi_iter.bi_size) {
1220 inc_all_io_entry(pool, bio);
1221 cell_defer_no_holder(tc, cell);
1223 remap_and_issue(tc, bio, 0);
1228 * Fill read bios with zeroes and complete them immediately.
1230 if (bio_data_dir(bio) == READ) {
1232 cell_defer_no_holder(tc, cell);
1237 r = alloc_data_block(tc, &data_block);
1241 schedule_external_copy(tc, block, data_block, cell, bio);
1243 schedule_zero(tc, block, data_block, cell, bio);
1247 retry_bios_on_resume(pool, cell);
1251 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1253 cell_error(pool, cell);
1258 static void process_bio(struct thin_c *tc, struct bio *bio)
1261 struct pool *pool = tc->pool;
1262 dm_block_t block = get_bio_block(tc, bio);
1263 struct dm_bio_prison_cell *cell;
1264 struct dm_cell_key key;
1265 struct dm_thin_lookup_result lookup_result;
1268 * If cell is already occupied, then the block is already
1269 * being provisioned so we have nothing further to do here.
1271 build_virtual_key(tc->td, block, &key);
1272 if (bio_detain(pool, &key, bio, &cell))
1275 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1278 if (lookup_result.shared) {
1279 process_shared_bio(tc, bio, block, &lookup_result);
1280 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
1282 inc_all_io_entry(pool, bio);
1283 cell_defer_no_holder(tc, cell);
1285 remap_and_issue(tc, bio, lookup_result.block);
1290 if (bio_data_dir(bio) == READ && tc->origin_dev) {
1291 inc_all_io_entry(pool, bio);
1292 cell_defer_no_holder(tc, cell);
1294 remap_to_origin_and_issue(tc, bio);
1296 provision_block(tc, bio, block, cell);
1300 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1302 cell_defer_no_holder(tc, cell);
1308 static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1311 int rw = bio_data_dir(bio);
1312 dm_block_t block = get_bio_block(tc, bio);
1313 struct dm_thin_lookup_result lookup_result;
1315 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1318 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size)
1319 handle_unserviceable_bio(tc->pool, bio);
1321 inc_all_io_entry(tc->pool, bio);
1322 remap_and_issue(tc, bio, lookup_result.block);
1328 handle_unserviceable_bio(tc->pool, bio);
1332 if (tc->origin_dev) {
1333 inc_all_io_entry(tc->pool, bio);
1334 remap_to_origin_and_issue(tc, bio);
1343 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1350 static void process_bio_success(struct thin_c *tc, struct bio *bio)
1355 static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1361 * FIXME: should we also commit due to size of transaction, measured in
1364 static int need_commit_due_to_time(struct pool *pool)
1366 return jiffies < pool->last_commit_jiffies ||
1367 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1370 static void process_deferred_bios(struct pool *pool)
1372 unsigned long flags;
1374 struct bio_list bios;
1376 bio_list_init(&bios);
1378 spin_lock_irqsave(&pool->lock, flags);
1379 bio_list_merge(&bios, &pool->deferred_bios);
1380 bio_list_init(&pool->deferred_bios);
1381 spin_unlock_irqrestore(&pool->lock, flags);
1383 while ((bio = bio_list_pop(&bios))) {
1384 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1385 struct thin_c *tc = h->tc;
1387 if (tc->requeue_mode) {
1388 bio_endio(bio, DM_ENDIO_REQUEUE);
1393 * If we've got no free new_mapping structs, and processing
1394 * this bio might require one, we pause until there are some
1395 * prepared mappings to process.
1397 if (ensure_next_mapping(pool)) {
1398 spin_lock_irqsave(&pool->lock, flags);
1399 bio_list_add(&pool->deferred_bios, bio);
1400 bio_list_merge(&pool->deferred_bios, &bios);
1401 spin_unlock_irqrestore(&pool->lock, flags);
1405 if (bio->bi_rw & REQ_DISCARD)
1406 pool->process_discard(tc, bio);
1408 pool->process_bio(tc, bio);
1412 * If there are any deferred flush bios, we must commit
1413 * the metadata before issuing them.
1415 bio_list_init(&bios);
1416 spin_lock_irqsave(&pool->lock, flags);
1417 bio_list_merge(&bios, &pool->deferred_flush_bios);
1418 bio_list_init(&pool->deferred_flush_bios);
1419 spin_unlock_irqrestore(&pool->lock, flags);
1421 if (bio_list_empty(&bios) &&
1422 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
1426 while ((bio = bio_list_pop(&bios)))
1430 pool->last_commit_jiffies = jiffies;
1432 while ((bio = bio_list_pop(&bios)))
1433 generic_make_request(bio);
1436 static void do_worker(struct work_struct *ws)
1438 struct pool *pool = container_of(ws, struct pool, worker);
1440 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1441 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
1442 process_deferred_bios(pool);
1446 * We want to commit periodically so that not too much
1447 * unwritten data builds up.
1449 static void do_waker(struct work_struct *ws)
1451 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1453 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1457 * We're holding onto IO to allow userland time to react. After the
1458 * timeout either the pool will have been resized (and thus back in
1459 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
1461 static void do_no_space_timeout(struct work_struct *ws)
1463 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
1466 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
1467 set_pool_mode(pool, PM_READ_ONLY);
1470 /*----------------------------------------------------------------*/
1472 struct noflush_work {
1473 struct work_struct worker;
1477 wait_queue_head_t wait;
1480 static void complete_noflush_work(struct noflush_work *w)
1482 atomic_set(&w->complete, 1);
1486 static void do_noflush_start(struct work_struct *ws)
1488 struct noflush_work *w = container_of(ws, struct noflush_work, worker);
1489 w->tc->requeue_mode = true;
1491 complete_noflush_work(w);
1494 static void do_noflush_stop(struct work_struct *ws)
1496 struct noflush_work *w = container_of(ws, struct noflush_work, worker);
1497 w->tc->requeue_mode = false;
1498 complete_noflush_work(w);
1501 static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
1503 struct noflush_work w;
1505 INIT_WORK(&w.worker, fn);
1507 atomic_set(&w.complete, 0);
1508 init_waitqueue_head(&w.wait);
1510 queue_work(tc->pool->wq, &w.worker);
1512 wait_event(w.wait, atomic_read(&w.complete));
1515 /*----------------------------------------------------------------*/
1517 static enum pool_mode get_pool_mode(struct pool *pool)
1519 return pool->pf.mode;
1522 static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
1524 dm_table_event(pool->ti->table);
1525 DMINFO("%s: switching pool to %s mode",
1526 dm_device_name(pool->pool_md), new_mode);
1529 static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
1531 struct pool_c *pt = pool->ti->private;
1532 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
1533 enum pool_mode old_mode = get_pool_mode(pool);
1534 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
1537 * Never allow the pool to transition to PM_WRITE mode if user
1538 * intervention is required to verify metadata and data consistency.
1540 if (new_mode == PM_WRITE && needs_check) {
1541 DMERR("%s: unable to switch pool to write mode until repaired.",
1542 dm_device_name(pool->pool_md));
1543 if (old_mode != new_mode)
1544 new_mode = old_mode;
1546 new_mode = PM_READ_ONLY;
1549 * If we were in PM_FAIL mode, rollback of metadata failed. We're
1550 * not going to recover without a thin_repair. So we never let the
1551 * pool move out of the old mode.
1553 if (old_mode == PM_FAIL)
1554 new_mode = old_mode;
1558 if (old_mode != new_mode)
1559 notify_of_pool_mode_change(pool, "failure");
1560 dm_pool_metadata_read_only(pool->pmd);
1561 pool->process_bio = process_bio_fail;
1562 pool->process_discard = process_bio_fail;
1563 pool->process_prepared_mapping = process_prepared_mapping_fail;
1564 pool->process_prepared_discard = process_prepared_discard_fail;
1566 error_retry_list(pool);
1570 if (old_mode != new_mode)
1571 notify_of_pool_mode_change(pool, "read-only");
1572 dm_pool_metadata_read_only(pool->pmd);
1573 pool->process_bio = process_bio_read_only;
1574 pool->process_discard = process_bio_success;
1575 pool->process_prepared_mapping = process_prepared_mapping_fail;
1576 pool->process_prepared_discard = process_prepared_discard_passdown;
1578 error_retry_list(pool);
1581 case PM_OUT_OF_DATA_SPACE:
1583 * Ideally we'd never hit this state; the low water mark
1584 * would trigger userland to extend the pool before we
1585 * completely run out of data space. However, many small
1586 * IOs to unprovisioned space can consume data space at an
1587 * alarming rate. Adjust your low water mark if you're
1588 * frequently seeing this mode.
1590 if (old_mode != new_mode)
1591 notify_of_pool_mode_change(pool, "out-of-data-space");
1592 pool->process_bio = process_bio_read_only;
1593 pool->process_discard = process_discard;
1594 pool->process_prepared_mapping = process_prepared_mapping;
1595 pool->process_prepared_discard = process_prepared_discard_passdown;
1597 if (!pool->pf.error_if_no_space && no_space_timeout)
1598 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
1602 if (old_mode != new_mode)
1603 notify_of_pool_mode_change(pool, "write");
1604 dm_pool_metadata_read_write(pool->pmd);
1605 pool->process_bio = process_bio;
1606 pool->process_discard = process_discard;
1607 pool->process_prepared_mapping = process_prepared_mapping;
1608 pool->process_prepared_discard = process_prepared_discard;
1612 pool->pf.mode = new_mode;
1614 * The pool mode may have changed, sync it so bind_control_target()
1615 * doesn't cause an unexpected mode transition on resume.
1617 pt->adjusted_pf.mode = new_mode;
1620 static void abort_transaction(struct pool *pool)
1622 const char *dev_name = dm_device_name(pool->pool_md);
1624 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
1625 if (dm_pool_abort_metadata(pool->pmd)) {
1626 DMERR("%s: failed to abort metadata transaction", dev_name);
1627 set_pool_mode(pool, PM_FAIL);
1630 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
1631 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
1632 set_pool_mode(pool, PM_FAIL);
1636 static void metadata_operation_failed(struct pool *pool, const char *op, int r)
1638 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1639 dm_device_name(pool->pool_md), op, r);
1641 abort_transaction(pool);
1642 set_pool_mode(pool, PM_READ_ONLY);
1645 /*----------------------------------------------------------------*/
1648 * Mapping functions.
1652 * Called only while mapping a thin bio to hand it over to the workqueue.
1654 static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1656 unsigned long flags;
1657 struct pool *pool = tc->pool;
1659 spin_lock_irqsave(&pool->lock, flags);
1660 bio_list_add(&pool->deferred_bios, bio);
1661 spin_unlock_irqrestore(&pool->lock, flags);
1666 static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
1668 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1671 h->shared_read_entry = NULL;
1672 h->all_io_entry = NULL;
1673 h->overwrite_mapping = NULL;
1677 * Non-blocking function called from the thin target's map function.
1679 static int thin_bio_map(struct dm_target *ti, struct bio *bio)
1682 struct thin_c *tc = ti->private;
1683 dm_block_t block = get_bio_block(tc, bio);
1684 struct dm_thin_device *td = tc->td;
1685 struct dm_thin_lookup_result result;
1686 struct dm_bio_prison_cell cell1, cell2;
1687 struct dm_bio_prison_cell *cell_result;
1688 struct dm_cell_key key;
1690 thin_hook_bio(tc, bio);
1692 if (tc->requeue_mode) {
1693 bio_endio(bio, DM_ENDIO_REQUEUE);
1694 return DM_MAPIO_SUBMITTED;
1697 if (get_pool_mode(tc->pool) == PM_FAIL) {
1699 return DM_MAPIO_SUBMITTED;
1702 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
1703 thin_defer_bio(tc, bio);
1704 return DM_MAPIO_SUBMITTED;
1708 * We must hold the virtual cell before doing the lookup, otherwise
1709 * there's a race with discard.
1711 build_virtual_key(tc->td, block, &key);
1712 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result))
1713 return DM_MAPIO_SUBMITTED;
1715 r = dm_thin_find_block(td, block, 0, &result);
1718 * Note that we defer readahead too.
1722 if (unlikely(result.shared)) {
1724 * We have a race condition here between the
1725 * result.shared value returned by the lookup and
1726 * snapshot creation, which may cause new
1729 * To avoid this always quiesce the origin before
1730 * taking the snap. You want to do this anyway to
1731 * ensure a consistent application view
1734 * More distant ancestors are irrelevant. The
1735 * shared flag will be set in their case.
1737 thin_defer_bio(tc, bio);
1738 cell_defer_no_holder_no_free(tc, &cell1);
1739 return DM_MAPIO_SUBMITTED;
1742 build_data_key(tc->td, result.block, &key);
1743 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) {
1744 cell_defer_no_holder_no_free(tc, &cell1);
1745 return DM_MAPIO_SUBMITTED;
1748 inc_all_io_entry(tc->pool, bio);
1749 cell_defer_no_holder_no_free(tc, &cell2);
1750 cell_defer_no_holder_no_free(tc, &cell1);
1752 remap(tc, bio, result.block);
1753 return DM_MAPIO_REMAPPED;
1756 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1758 * This block isn't provisioned, and we have no way
1761 handle_unserviceable_bio(tc->pool, bio);
1762 cell_defer_no_holder_no_free(tc, &cell1);
1763 return DM_MAPIO_SUBMITTED;
1769 * In future, the failed dm_thin_find_block above could
1770 * provide the hint to load the metadata into cache.
1772 thin_defer_bio(tc, bio);
1773 cell_defer_no_holder_no_free(tc, &cell1);
1774 return DM_MAPIO_SUBMITTED;
1778 * Must always call bio_io_error on failure.
1779 * dm_thin_find_block can fail with -EINVAL if the
1780 * pool is switched to fail-io mode.
1783 cell_defer_no_holder_no_free(tc, &cell1);
1784 return DM_MAPIO_SUBMITTED;
1788 static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1791 unsigned long flags;
1792 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
1794 spin_lock_irqsave(&pt->pool->lock, flags);
1795 r = !bio_list_empty(&pt->pool->retry_on_resume_list);
1796 spin_unlock_irqrestore(&pt->pool->lock, flags);
1799 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1800 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1806 static void __requeue_bios(struct pool *pool)
1808 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1809 bio_list_init(&pool->retry_on_resume_list);
1812 /*----------------------------------------------------------------
1813 * Binding of control targets to a pool object
1814 *--------------------------------------------------------------*/
1815 static bool data_dev_supports_discard(struct pool_c *pt)
1817 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1819 return q && blk_queue_discard(q);
1822 static bool is_factor(sector_t block_size, uint32_t n)
1824 return !sector_div(block_size, n);
1828 * If discard_passdown was enabled verify that the data device
1829 * supports discards. Disable discard_passdown if not.
1831 static void disable_passdown_if_not_supported(struct pool_c *pt)
1833 struct pool *pool = pt->pool;
1834 struct block_device *data_bdev = pt->data_dev->bdev;
1835 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
1836 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
1837 const char *reason = NULL;
1838 char buf[BDEVNAME_SIZE];
1840 if (!pt->adjusted_pf.discard_passdown)
1843 if (!data_dev_supports_discard(pt))
1844 reason = "discard unsupported";
1846 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1847 reason = "max discard sectors smaller than a block";
1849 else if (data_limits->discard_granularity > block_size)
1850 reason = "discard granularity larger than a block";
1852 else if (!is_factor(block_size, data_limits->discard_granularity))
1853 reason = "discard granularity not a factor of block size";
1856 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1857 pt->adjusted_pf.discard_passdown = false;
1861 static int bind_control_target(struct pool *pool, struct dm_target *ti)
1863 struct pool_c *pt = ti->private;
1866 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
1868 enum pool_mode old_mode = get_pool_mode(pool);
1869 enum pool_mode new_mode = pt->adjusted_pf.mode;
1872 * Don't change the pool's mode until set_pool_mode() below.
1873 * Otherwise the pool's process_* function pointers may
1874 * not match the desired pool mode.
1876 pt->adjusted_pf.mode = old_mode;
1879 pool->pf = pt->adjusted_pf;
1880 pool->low_water_blocks = pt->low_water_blocks;
1882 set_pool_mode(pool, new_mode);
1887 static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1893 /*----------------------------------------------------------------
1895 *--------------------------------------------------------------*/
1896 /* Initialize pool features. */
1897 static void pool_features_init(struct pool_features *pf)
1899 pf->mode = PM_WRITE;
1900 pf->zero_new_blocks = true;
1901 pf->discard_enabled = true;
1902 pf->discard_passdown = true;
1903 pf->error_if_no_space = false;
1906 static void __pool_destroy(struct pool *pool)
1908 __pool_table_remove(pool);
1910 if (dm_pool_metadata_close(pool->pmd) < 0)
1911 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1913 dm_bio_prison_destroy(pool->prison);
1914 dm_kcopyd_client_destroy(pool->copier);
1917 destroy_workqueue(pool->wq);
1919 if (pool->next_mapping)
1920 mempool_free(pool->next_mapping, pool->mapping_pool);
1921 mempool_destroy(pool->mapping_pool);
1922 dm_deferred_set_destroy(pool->shared_read_ds);
1923 dm_deferred_set_destroy(pool->all_io_ds);
1927 static struct kmem_cache *_new_mapping_cache;
1929 static struct pool *pool_create(struct mapped_device *pool_md,
1930 struct block_device *metadata_dev,
1931 unsigned long block_size,
1932 int read_only, char **error)
1937 struct dm_pool_metadata *pmd;
1938 bool format_device = read_only ? false : true;
1940 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
1942 *error = "Error creating metadata object";
1943 return (struct pool *)pmd;
1946 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1948 *error = "Error allocating memory for pool";
1949 err_p = ERR_PTR(-ENOMEM);
1954 pool->sectors_per_block = block_size;
1955 if (block_size & (block_size - 1))
1956 pool->sectors_per_block_shift = -1;
1958 pool->sectors_per_block_shift = __ffs(block_size);
1959 pool->low_water_blocks = 0;
1960 pool_features_init(&pool->pf);
1961 pool->prison = dm_bio_prison_create(PRISON_CELLS);
1962 if (!pool->prison) {
1963 *error = "Error creating pool's bio prison";
1964 err_p = ERR_PTR(-ENOMEM);
1968 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1969 if (IS_ERR(pool->copier)) {
1970 r = PTR_ERR(pool->copier);
1971 *error = "Error creating pool's kcopyd client";
1973 goto bad_kcopyd_client;
1977 * Create singlethreaded workqueue that will service all devices
1978 * that use this metadata.
1980 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1982 *error = "Error creating pool's workqueue";
1983 err_p = ERR_PTR(-ENOMEM);
1987 INIT_WORK(&pool->worker, do_worker);
1988 INIT_DELAYED_WORK(&pool->waker, do_waker);
1989 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
1990 spin_lock_init(&pool->lock);
1991 bio_list_init(&pool->deferred_bios);
1992 bio_list_init(&pool->deferred_flush_bios);
1993 INIT_LIST_HEAD(&pool->prepared_mappings);
1994 INIT_LIST_HEAD(&pool->prepared_discards);
1995 pool->low_water_triggered = false;
1996 bio_list_init(&pool->retry_on_resume_list);
1998 pool->shared_read_ds = dm_deferred_set_create();
1999 if (!pool->shared_read_ds) {
2000 *error = "Error creating pool's shared read deferred set";
2001 err_p = ERR_PTR(-ENOMEM);
2002 goto bad_shared_read_ds;
2005 pool->all_io_ds = dm_deferred_set_create();
2006 if (!pool->all_io_ds) {
2007 *error = "Error creating pool's all io deferred set";
2008 err_p = ERR_PTR(-ENOMEM);
2012 pool->next_mapping = NULL;
2013 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2014 _new_mapping_cache);
2015 if (!pool->mapping_pool) {
2016 *error = "Error creating pool's mapping mempool";
2017 err_p = ERR_PTR(-ENOMEM);
2018 goto bad_mapping_pool;
2021 pool->ref_count = 1;
2022 pool->last_commit_jiffies = jiffies;
2023 pool->pool_md = pool_md;
2024 pool->md_dev = metadata_dev;
2025 __pool_table_insert(pool);
2030 dm_deferred_set_destroy(pool->all_io_ds);
2032 dm_deferred_set_destroy(pool->shared_read_ds);
2034 destroy_workqueue(pool->wq);
2036 dm_kcopyd_client_destroy(pool->copier);
2038 dm_bio_prison_destroy(pool->prison);
2042 if (dm_pool_metadata_close(pmd))
2043 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2048 static void __pool_inc(struct pool *pool)
2050 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2054 static void __pool_dec(struct pool *pool)
2056 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2057 BUG_ON(!pool->ref_count);
2058 if (!--pool->ref_count)
2059 __pool_destroy(pool);
2062 static struct pool *__pool_find(struct mapped_device *pool_md,
2063 struct block_device *metadata_dev,
2064 unsigned long block_size, int read_only,
2065 char **error, int *created)
2067 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2070 if (pool->pool_md != pool_md) {
2071 *error = "metadata device already in use by a pool";
2072 return ERR_PTR(-EBUSY);
2077 pool = __pool_table_lookup(pool_md);
2079 if (pool->md_dev != metadata_dev) {
2080 *error = "different pool cannot replace a pool";
2081 return ERR_PTR(-EINVAL);
2086 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
2094 /*----------------------------------------------------------------
2095 * Pool target methods
2096 *--------------------------------------------------------------*/
2097 static void pool_dtr(struct dm_target *ti)
2099 struct pool_c *pt = ti->private;
2101 mutex_lock(&dm_thin_pool_table.mutex);
2103 unbind_control_target(pt->pool, ti);
2104 __pool_dec(pt->pool);
2105 dm_put_device(ti, pt->metadata_dev);
2106 dm_put_device(ti, pt->data_dev);
2109 mutex_unlock(&dm_thin_pool_table.mutex);
2112 static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2113 struct dm_target *ti)
2117 const char *arg_name;
2119 static struct dm_arg _args[] = {
2120 {0, 4, "Invalid number of pool feature arguments"},
2124 * No feature arguments supplied.
2129 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2133 while (argc && !r) {
2134 arg_name = dm_shift_arg(as);
2137 if (!strcasecmp(arg_name, "skip_block_zeroing"))
2138 pf->zero_new_blocks = false;
2140 else if (!strcasecmp(arg_name, "ignore_discard"))
2141 pf->discard_enabled = false;
2143 else if (!strcasecmp(arg_name, "no_discard_passdown"))
2144 pf->discard_passdown = false;
2146 else if (!strcasecmp(arg_name, "read_only"))
2147 pf->mode = PM_READ_ONLY;
2149 else if (!strcasecmp(arg_name, "error_if_no_space"))
2150 pf->error_if_no_space = true;
2153 ti->error = "Unrecognised pool feature requested";
2162 static void metadata_low_callback(void *context)
2164 struct pool *pool = context;
2166 DMWARN("%s: reached low water mark for metadata device: sending event.",
2167 dm_device_name(pool->pool_md));
2169 dm_table_event(pool->ti->table);
2172 static sector_t get_dev_size(struct block_device *bdev)
2174 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2177 static void warn_if_metadata_device_too_big(struct block_device *bdev)
2179 sector_t metadata_dev_size = get_dev_size(bdev);
2180 char buffer[BDEVNAME_SIZE];
2182 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
2183 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2184 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
2187 static sector_t get_metadata_dev_size(struct block_device *bdev)
2189 sector_t metadata_dev_size = get_dev_size(bdev);
2191 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2192 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
2194 return metadata_dev_size;
2197 static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2199 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2201 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
2203 return metadata_dev_size;
2207 * When a metadata threshold is crossed a dm event is triggered, and
2208 * userland should respond by growing the metadata device. We could let
2209 * userland set the threshold, like we do with the data threshold, but I'm
2210 * not sure they know enough to do this well.
2212 static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2215 * 4M is ample for all ops with the possible exception of thin
2216 * device deletion which is harmless if it fails (just retry the
2217 * delete after you've grown the device).
2219 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2220 return min((dm_block_t)1024ULL /* 4M */, quarter);
2224 * thin-pool <metadata dev> <data dev>
2225 * <data block size (sectors)>
2226 * <low water mark (blocks)>
2227 * [<#feature args> [<arg>]*]
2229 * Optional feature arguments are:
2230 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
2231 * ignore_discard: disable discard
2232 * no_discard_passdown: don't pass discards down to the data device
2233 * read_only: Don't allow any changes to be made to the pool metadata.
2234 * error_if_no_space: error IOs, instead of queueing, if no space.
2236 static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2238 int r, pool_created = 0;
2241 struct pool_features pf;
2242 struct dm_arg_set as;
2243 struct dm_dev *data_dev;
2244 unsigned long block_size;
2245 dm_block_t low_water_blocks;
2246 struct dm_dev *metadata_dev;
2247 fmode_t metadata_mode;
2250 * FIXME Remove validation from scope of lock.
2252 mutex_lock(&dm_thin_pool_table.mutex);
2255 ti->error = "Invalid argument count";
2264 * Set default pool features.
2266 pool_features_init(&pf);
2268 dm_consume_args(&as, 4);
2269 r = parse_pool_features(&as, &pf, ti);
2273 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2274 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
2276 ti->error = "Error opening metadata block device";
2279 warn_if_metadata_device_too_big(metadata_dev->bdev);
2281 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2283 ti->error = "Error getting data device";
2287 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2288 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2289 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2290 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
2291 ti->error = "Invalid block size";
2296 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2297 ti->error = "Invalid low water mark";
2302 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2308 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
2309 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
2316 * 'pool_created' reflects whether this is the first table load.
2317 * Top level discard support is not allowed to be changed after
2318 * initial load. This would require a pool reload to trigger thin
2321 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2322 ti->error = "Discard support cannot be disabled once enabled";
2324 goto out_flags_changed;
2329 pt->metadata_dev = metadata_dev;
2330 pt->data_dev = data_dev;
2331 pt->low_water_blocks = low_water_blocks;
2332 pt->adjusted_pf = pt->requested_pf = pf;
2333 ti->num_flush_bios = 1;
2336 * Only need to enable discards if the pool should pass
2337 * them down to the data device. The thin device's discard
2338 * processing will cause mappings to be removed from the btree.
2340 ti->discard_zeroes_data_unsupported = true;
2341 if (pf.discard_enabled && pf.discard_passdown) {
2342 ti->num_discard_bios = 1;
2345 * Setting 'discards_supported' circumvents the normal
2346 * stacking of discard limits (this keeps the pool and
2347 * thin devices' discard limits consistent).
2349 ti->discards_supported = true;
2353 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2354 calc_metadata_threshold(pt),
2355 metadata_low_callback,
2360 pt->callbacks.congested_fn = pool_is_congested;
2361 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2363 mutex_unlock(&dm_thin_pool_table.mutex);
2372 dm_put_device(ti, data_dev);
2374 dm_put_device(ti, metadata_dev);
2376 mutex_unlock(&dm_thin_pool_table.mutex);
2381 static int pool_map(struct dm_target *ti, struct bio *bio)
2384 struct pool_c *pt = ti->private;
2385 struct pool *pool = pt->pool;
2386 unsigned long flags;
2389 * As this is a singleton target, ti->begin is always zero.
2391 spin_lock_irqsave(&pool->lock, flags);
2392 bio->bi_bdev = pt->data_dev->bdev;
2393 r = DM_MAPIO_REMAPPED;
2394 spin_unlock_irqrestore(&pool->lock, flags);
2399 static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
2402 struct pool_c *pt = ti->private;
2403 struct pool *pool = pt->pool;
2404 sector_t data_size = ti->len;
2405 dm_block_t sb_data_size;
2407 *need_commit = false;
2409 (void) sector_div(data_size, pool->sectors_per_block);
2411 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2413 DMERR("%s: failed to retrieve data device size",
2414 dm_device_name(pool->pool_md));
2418 if (data_size < sb_data_size) {
2419 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2420 dm_device_name(pool->pool_md),
2421 (unsigned long long)data_size, sb_data_size);
2424 } else if (data_size > sb_data_size) {
2425 if (dm_pool_metadata_needs_check(pool->pmd)) {
2426 DMERR("%s: unable to grow the data device until repaired.",
2427 dm_device_name(pool->pool_md));
2432 DMINFO("%s: growing the data device from %llu to %llu blocks",
2433 dm_device_name(pool->pool_md),
2434 sb_data_size, (unsigned long long)data_size);
2435 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2437 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
2441 *need_commit = true;
2447 static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
2450 struct pool_c *pt = ti->private;
2451 struct pool *pool = pt->pool;
2452 dm_block_t metadata_dev_size, sb_metadata_dev_size;
2454 *need_commit = false;
2456 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
2458 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
2460 DMERR("%s: failed to retrieve metadata device size",
2461 dm_device_name(pool->pool_md));
2465 if (metadata_dev_size < sb_metadata_dev_size) {
2466 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2467 dm_device_name(pool->pool_md),
2468 metadata_dev_size, sb_metadata_dev_size);
2471 } else if (metadata_dev_size > sb_metadata_dev_size) {
2472 if (dm_pool_metadata_needs_check(pool->pmd)) {
2473 DMERR("%s: unable to grow the metadata device until repaired.",
2474 dm_device_name(pool->pool_md));
2478 warn_if_metadata_device_too_big(pool->md_dev);
2479 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2480 dm_device_name(pool->pool_md),
2481 sb_metadata_dev_size, metadata_dev_size);
2482 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
2484 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
2488 *need_commit = true;
2495 * Retrieves the number of blocks of the data device from
2496 * the superblock and compares it to the actual device size,
2497 * thus resizing the data device in case it has grown.
2499 * This both copes with opening preallocated data devices in the ctr
2500 * being followed by a resume
2502 * calling the resume method individually after userspace has
2503 * grown the data device in reaction to a table event.
2505 static int pool_preresume(struct dm_target *ti)
2508 bool need_commit1, need_commit2;
2509 struct pool_c *pt = ti->private;
2510 struct pool *pool = pt->pool;
2513 * Take control of the pool object.
2515 r = bind_control_target(pool, ti);
2519 r = maybe_resize_data_dev(ti, &need_commit1);
2523 r = maybe_resize_metadata_dev(ti, &need_commit2);
2527 if (need_commit1 || need_commit2)
2528 (void) commit(pool);
2533 static void pool_resume(struct dm_target *ti)
2535 struct pool_c *pt = ti->private;
2536 struct pool *pool = pt->pool;
2537 unsigned long flags;
2539 spin_lock_irqsave(&pool->lock, flags);
2540 pool->low_water_triggered = false;
2541 __requeue_bios(pool);
2542 spin_unlock_irqrestore(&pool->lock, flags);
2544 do_waker(&pool->waker.work);
2547 static void pool_postsuspend(struct dm_target *ti)
2549 struct pool_c *pt = ti->private;
2550 struct pool *pool = pt->pool;
2552 cancel_delayed_work(&pool->waker);
2553 cancel_delayed_work(&pool->no_space_timeout);
2554 flush_workqueue(pool->wq);
2555 (void) commit(pool);
2558 static int check_arg_count(unsigned argc, unsigned args_required)
2560 if (argc != args_required) {
2561 DMWARN("Message received with %u arguments instead of %u.",
2562 argc, args_required);
2569 static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2571 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2572 *dev_id <= MAX_DEV_ID)
2576 DMWARN("Message received with invalid device id: %s", arg);
2581 static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2586 r = check_arg_count(argc, 2);
2590 r = read_dev_id(argv[1], &dev_id, 1);
2594 r = dm_pool_create_thin(pool->pmd, dev_id);
2596 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2604 static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2607 dm_thin_id origin_dev_id;
2610 r = check_arg_count(argc, 3);
2614 r = read_dev_id(argv[1], &dev_id, 1);
2618 r = read_dev_id(argv[2], &origin_dev_id, 1);
2622 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2624 DMWARN("Creation of new snapshot %s of device %s failed.",
2632 static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2637 r = check_arg_count(argc, 2);
2641 r = read_dev_id(argv[1], &dev_id, 1);
2645 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2647 DMWARN("Deletion of thin device %s failed.", argv[1]);
2652 static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2654 dm_thin_id old_id, new_id;
2657 r = check_arg_count(argc, 3);
2661 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2662 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2666 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2667 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2671 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2673 DMWARN("Failed to change transaction id from %s to %s.",
2681 static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2685 r = check_arg_count(argc, 1);
2689 (void) commit(pool);
2691 r = dm_pool_reserve_metadata_snap(pool->pmd);
2693 DMWARN("reserve_metadata_snap message failed.");
2698 static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2702 r = check_arg_count(argc, 1);
2706 r = dm_pool_release_metadata_snap(pool->pmd);
2708 DMWARN("release_metadata_snap message failed.");
2714 * Messages supported:
2715 * create_thin <dev_id>
2716 * create_snap <dev_id> <origin_id>
2718 * trim <dev_id> <new_size_in_sectors>
2719 * set_transaction_id <current_trans_id> <new_trans_id>
2720 * reserve_metadata_snap
2721 * release_metadata_snap
2723 static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2726 struct pool_c *pt = ti->private;
2727 struct pool *pool = pt->pool;
2729 if (!strcasecmp(argv[0], "create_thin"))
2730 r = process_create_thin_mesg(argc, argv, pool);
2732 else if (!strcasecmp(argv[0], "create_snap"))
2733 r = process_create_snap_mesg(argc, argv, pool);
2735 else if (!strcasecmp(argv[0], "delete"))
2736 r = process_delete_mesg(argc, argv, pool);
2738 else if (!strcasecmp(argv[0], "set_transaction_id"))
2739 r = process_set_transaction_id_mesg(argc, argv, pool);
2741 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2742 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2744 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2745 r = process_release_metadata_snap_mesg(argc, argv, pool);
2748 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2751 (void) commit(pool);
2756 static void emit_flags(struct pool_features *pf, char *result,
2757 unsigned sz, unsigned maxlen)
2759 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
2760 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
2761 pf->error_if_no_space;
2762 DMEMIT("%u ", count);
2764 if (!pf->zero_new_blocks)
2765 DMEMIT("skip_block_zeroing ");
2767 if (!pf->discard_enabled)
2768 DMEMIT("ignore_discard ");
2770 if (!pf->discard_passdown)
2771 DMEMIT("no_discard_passdown ");
2773 if (pf->mode == PM_READ_ONLY)
2774 DMEMIT("read_only ");
2776 if (pf->error_if_no_space)
2777 DMEMIT("error_if_no_space ");
2782 * <transaction id> <used metadata sectors>/<total metadata sectors>
2783 * <used data sectors>/<total data sectors> <held metadata root>
2785 static void pool_status(struct dm_target *ti, status_type_t type,
2786 unsigned status_flags, char *result, unsigned maxlen)
2790 uint64_t transaction_id;
2791 dm_block_t nr_free_blocks_data;
2792 dm_block_t nr_free_blocks_metadata;
2793 dm_block_t nr_blocks_data;
2794 dm_block_t nr_blocks_metadata;
2795 dm_block_t held_root;
2796 char buf[BDEVNAME_SIZE];
2797 char buf2[BDEVNAME_SIZE];
2798 struct pool_c *pt = ti->private;
2799 struct pool *pool = pt->pool;
2802 case STATUSTYPE_INFO:
2803 if (get_pool_mode(pool) == PM_FAIL) {
2808 /* Commit to ensure statistics aren't out-of-date */
2809 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
2810 (void) commit(pool);
2812 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
2814 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
2815 dm_device_name(pool->pool_md), r);
2819 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
2821 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
2822 dm_device_name(pool->pool_md), r);
2826 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
2828 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
2829 dm_device_name(pool->pool_md), r);
2833 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
2835 DMERR("%s: dm_pool_get_free_block_count returned %d",
2836 dm_device_name(pool->pool_md), r);
2840 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
2842 DMERR("%s: dm_pool_get_data_dev_size returned %d",
2843 dm_device_name(pool->pool_md), r);
2847 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
2849 DMERR("%s: dm_pool_get_metadata_snap returned %d",
2850 dm_device_name(pool->pool_md), r);
2854 DMEMIT("%llu %llu/%llu %llu/%llu ",
2855 (unsigned long long)transaction_id,
2856 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2857 (unsigned long long)nr_blocks_metadata,
2858 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2859 (unsigned long long)nr_blocks_data);
2862 DMEMIT("%llu ", held_root);
2866 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
2867 DMEMIT("out_of_data_space ");
2868 else if (pool->pf.mode == PM_READ_ONLY)
2873 if (!pool->pf.discard_enabled)
2874 DMEMIT("ignore_discard ");
2875 else if (pool->pf.discard_passdown)
2876 DMEMIT("discard_passdown ");
2878 DMEMIT("no_discard_passdown ");
2880 if (pool->pf.error_if_no_space)
2881 DMEMIT("error_if_no_space ");
2883 DMEMIT("queue_if_no_space ");
2887 case STATUSTYPE_TABLE:
2888 DMEMIT("%s %s %lu %llu ",
2889 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2890 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2891 (unsigned long)pool->sectors_per_block,
2892 (unsigned long long)pt->low_water_blocks);
2893 emit_flags(&pt->requested_pf, result, sz, maxlen);
2902 static int pool_iterate_devices(struct dm_target *ti,
2903 iterate_devices_callout_fn fn, void *data)
2905 struct pool_c *pt = ti->private;
2907 return fn(ti, pt->data_dev, 0, ti->len, data);
2910 static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2911 struct bio_vec *biovec, int max_size)
2913 struct pool_c *pt = ti->private;
2914 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2916 if (!q->merge_bvec_fn)
2919 bvm->bi_bdev = pt->data_dev->bdev;
2921 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2924 static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
2926 struct pool *pool = pt->pool;
2927 struct queue_limits *data_limits;
2929 limits->max_discard_sectors = pool->sectors_per_block;
2932 * discard_granularity is just a hint, and not enforced.
2934 if (pt->adjusted_pf.discard_passdown) {
2935 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
2936 limits->discard_granularity = max(data_limits->discard_granularity,
2937 pool->sectors_per_block << SECTOR_SHIFT);
2939 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
2942 static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2944 struct pool_c *pt = ti->private;
2945 struct pool *pool = pt->pool;
2946 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
2949 * If the system-determined stacked limits are compatible with the
2950 * pool's blocksize (io_opt is a factor) do not override them.
2952 if (io_opt_sectors < pool->sectors_per_block ||
2953 do_div(io_opt_sectors, pool->sectors_per_block)) {
2954 blk_limits_io_min(limits, 0);
2955 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
2959 * pt->adjusted_pf is a staging area for the actual features to use.
2960 * They get transferred to the live pool in bind_control_target()
2961 * called from pool_preresume().
2963 if (!pt->adjusted_pf.discard_enabled) {
2965 * Must explicitly disallow stacking discard limits otherwise the
2966 * block layer will stack them if pool's data device has support.
2967 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
2968 * user to see that, so make sure to set all discard limits to 0.
2970 limits->discard_granularity = 0;
2974 disable_passdown_if_not_supported(pt);
2976 set_discard_limits(pt, limits);
2979 static struct target_type pool_target = {
2980 .name = "thin-pool",
2981 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2982 DM_TARGET_IMMUTABLE,
2983 .version = {1, 11, 0},
2984 .module = THIS_MODULE,
2988 .postsuspend = pool_postsuspend,
2989 .preresume = pool_preresume,
2990 .resume = pool_resume,
2991 .message = pool_message,
2992 .status = pool_status,
2993 .merge = pool_merge,
2994 .iterate_devices = pool_iterate_devices,
2995 .io_hints = pool_io_hints,
2998 /*----------------------------------------------------------------
2999 * Thin target methods
3000 *--------------------------------------------------------------*/
3001 static void thin_dtr(struct dm_target *ti)
3003 struct thin_c *tc = ti->private;
3005 mutex_lock(&dm_thin_pool_table.mutex);
3007 __pool_dec(tc->pool);
3008 dm_pool_close_thin_device(tc->td);
3009 dm_put_device(ti, tc->pool_dev);
3011 dm_put_device(ti, tc->origin_dev);
3014 mutex_unlock(&dm_thin_pool_table.mutex);
3018 * Thin target parameters:
3020 * <pool_dev> <dev_id> [origin_dev]
3022 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3023 * dev_id: the internal device identifier
3024 * origin_dev: a device external to the pool that should act as the origin
3026 * If the pool device has discards disabled, they get disabled for the thin
3029 static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3033 struct dm_dev *pool_dev, *origin_dev;
3034 struct mapped_device *pool_md;
3036 mutex_lock(&dm_thin_pool_table.mutex);
3038 if (argc != 2 && argc != 3) {
3039 ti->error = "Invalid argument count";
3044 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3046 ti->error = "Out of memory";
3052 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3054 ti->error = "Error opening origin device";
3055 goto bad_origin_dev;
3057 tc->origin_dev = origin_dev;
3060 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3062 ti->error = "Error opening pool device";
3065 tc->pool_dev = pool_dev;
3067 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3068 ti->error = "Invalid device id";
3073 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3075 ti->error = "Couldn't get pool mapped device";
3080 tc->pool = __pool_table_lookup(pool_md);
3082 ti->error = "Couldn't find pool object";
3084 goto bad_pool_lookup;
3086 __pool_inc(tc->pool);
3088 if (get_pool_mode(tc->pool) == PM_FAIL) {
3089 ti->error = "Couldn't open thin device, Pool is in fail mode";
3094 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3096 ti->error = "Couldn't open thin internal device";
3100 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3102 goto bad_target_max_io_len;
3104 ti->num_flush_bios = 1;
3105 ti->flush_supported = true;
3106 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
3108 /* In case the pool supports discards, pass them on. */
3109 ti->discard_zeroes_data_unsupported = true;
3110 if (tc->pool->pf.discard_enabled) {
3111 ti->discards_supported = true;
3112 ti->num_discard_bios = 1;
3113 /* Discard bios must be split on a block boundary */
3114 ti->split_discard_bios = true;
3119 mutex_unlock(&dm_thin_pool_table.mutex);
3123 bad_target_max_io_len:
3124 dm_pool_close_thin_device(tc->td);
3126 __pool_dec(tc->pool);
3130 dm_put_device(ti, tc->pool_dev);
3133 dm_put_device(ti, tc->origin_dev);
3137 mutex_unlock(&dm_thin_pool_table.mutex);
3142 static int thin_map(struct dm_target *ti, struct bio *bio)
3144 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
3146 return thin_bio_map(ti, bio);
3149 static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
3151 unsigned long flags;
3152 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
3153 struct list_head work;
3154 struct dm_thin_new_mapping *m, *tmp;
3155 struct pool *pool = h->tc->pool;
3157 if (h->shared_read_entry) {
3158 INIT_LIST_HEAD(&work);
3159 dm_deferred_entry_dec(h->shared_read_entry, &work);
3161 spin_lock_irqsave(&pool->lock, flags);
3162 list_for_each_entry_safe(m, tmp, &work, list) {
3165 __maybe_add_mapping(m);
3167 spin_unlock_irqrestore(&pool->lock, flags);
3170 if (h->all_io_entry) {
3171 INIT_LIST_HEAD(&work);
3172 dm_deferred_entry_dec(h->all_io_entry, &work);
3173 if (!list_empty(&work)) {
3174 spin_lock_irqsave(&pool->lock, flags);
3175 list_for_each_entry_safe(m, tmp, &work, list)
3176 list_add_tail(&m->list, &pool->prepared_discards);
3177 spin_unlock_irqrestore(&pool->lock, flags);
3185 static void thin_presuspend(struct dm_target *ti)
3187 struct thin_c *tc = ti->private;
3189 if (dm_noflush_suspending(ti))
3190 noflush_work(tc, do_noflush_start);
3193 static void thin_postsuspend(struct dm_target *ti)
3195 struct thin_c *tc = ti->private;
3198 * The dm_noflush_suspending flag has been cleared by now, so
3199 * unfortunately we must always run this.
3201 noflush_work(tc, do_noflush_stop);
3205 * <nr mapped sectors> <highest mapped sector>
3207 static void thin_status(struct dm_target *ti, status_type_t type,
3208 unsigned status_flags, char *result, unsigned maxlen)
3212 dm_block_t mapped, highest;
3213 char buf[BDEVNAME_SIZE];
3214 struct thin_c *tc = ti->private;
3216 if (get_pool_mode(tc->pool) == PM_FAIL) {
3225 case STATUSTYPE_INFO:
3226 r = dm_thin_get_mapped_count(tc->td, &mapped);
3228 DMERR("dm_thin_get_mapped_count returned %d", r);
3232 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
3234 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3238 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3240 DMEMIT("%llu", ((highest + 1) *
3241 tc->pool->sectors_per_block) - 1);
3246 case STATUSTYPE_TABLE:
3248 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3249 (unsigned long) tc->dev_id);
3251 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
3262 static int thin_iterate_devices(struct dm_target *ti,
3263 iterate_devices_callout_fn fn, void *data)
3266 struct thin_c *tc = ti->private;
3267 struct pool *pool = tc->pool;
3270 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3271 * we follow a more convoluted path through to the pool's target.
3274 return 0; /* nothing is bound */
3276 blocks = pool->ti->len;
3277 (void) sector_div(blocks, pool->sectors_per_block);
3279 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
3284 static struct target_type thin_target = {
3286 .version = {1, 11, 0},
3287 .module = THIS_MODULE,
3291 .end_io = thin_endio,
3292 .presuspend = thin_presuspend,
3293 .postsuspend = thin_postsuspend,
3294 .status = thin_status,
3295 .iterate_devices = thin_iterate_devices,
3298 /*----------------------------------------------------------------*/
3300 static int __init dm_thin_init(void)
3306 r = dm_register_target(&thin_target);
3310 r = dm_register_target(&pool_target);
3312 goto bad_pool_target;
3316 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
3317 if (!_new_mapping_cache)
3318 goto bad_new_mapping_cache;
3322 bad_new_mapping_cache:
3323 dm_unregister_target(&pool_target);
3325 dm_unregister_target(&thin_target);
3330 static void dm_thin_exit(void)
3332 dm_unregister_target(&thin_target);
3333 dm_unregister_target(&pool_target);
3335 kmem_cache_destroy(_new_mapping_cache);
3338 module_init(dm_thin_init);
3339 module_exit(dm_thin_exit);
3341 module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
3342 MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
3344 MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
3345 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3346 MODULE_LICENSE("GPL");