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/log2.h>
15 #include <linux/list.h>
16 #include <linux/rculist.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/rbtree.h>
22 #define DM_MSG_PREFIX "thin"
27 #define ENDIO_HOOK_POOL_SIZE 1024
28 #define MAPPING_POOL_SIZE 1024
29 #define COMMIT_PERIOD HZ
30 #define NO_SPACE_TIMEOUT_SECS 60
32 static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
34 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
35 "A percentage of time allocated for copy on write");
38 * The block size of the device holding pool data must be
39 * between 64KB and 1GB.
41 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
42 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
45 * Device id is restricted to 24 bits.
47 #define MAX_DEV_ID ((1 << 24) - 1)
50 * How do we handle breaking sharing of data blocks?
51 * =================================================
53 * We use a standard copy-on-write btree to store the mappings for the
54 * devices (note I'm talking about copy-on-write of the metadata here, not
55 * the data). When you take an internal snapshot you clone the root node
56 * of the origin btree. After this there is no concept of an origin or a
57 * snapshot. They are just two device trees that happen to point to the
60 * When we get a write in we decide if it's to a shared data block using
61 * some timestamp magic. If it is, we have to break sharing.
63 * Let's say we write to a shared block in what was the origin. The
66 * i) plug io further to this physical block. (see bio_prison code).
68 * ii) quiesce any read io to that shared data block. Obviously
69 * including all devices that share this block. (see dm_deferred_set code)
71 * iii) copy the data block to a newly allocate block. This step can be
72 * missed out if the io covers the block. (schedule_copy).
74 * iv) insert the new mapping into the origin's btree
75 * (process_prepared_mapping). This act of inserting breaks some
76 * sharing of btree nodes between the two devices. Breaking sharing only
77 * effects the btree of that specific device. Btrees for the other
78 * devices that share the block never change. The btree for the origin
79 * device as it was after the last commit is untouched, ie. we're using
80 * persistent data structures in the functional programming sense.
82 * v) unplug io to this physical block, including the io that triggered
83 * the breaking of sharing.
85 * Steps (ii) and (iii) occur in parallel.
87 * The metadata _doesn't_ need to be committed before the io continues. We
88 * get away with this because the io is always written to a _new_ block.
89 * If there's a crash, then:
91 * - The origin mapping will point to the old origin block (the shared
92 * one). This will contain the data as it was before the io that triggered
93 * the breaking of sharing came in.
95 * - The snap mapping still points to the old block. As it would after
98 * The downside of this scheme is the timestamp magic isn't perfect, and
99 * will continue to think that data block in the snapshot device is shared
100 * even after the write to the origin has broken sharing. I suspect data
101 * blocks will typically be shared by many different devices, so we're
102 * breaking sharing n + 1 times, rather than n, where n is the number of
103 * devices that reference this data block. At the moment I think the
104 * benefits far, far outweigh the disadvantages.
107 /*----------------------------------------------------------------*/
112 static void build_data_key(struct dm_thin_device *td,
113 dm_block_t b, struct dm_cell_key *key)
116 key->dev = dm_thin_dev_id(td);
120 static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
121 struct dm_cell_key *key)
124 key->dev = dm_thin_dev_id(td);
128 /*----------------------------------------------------------------*/
130 #define THROTTLE_THRESHOLD (1 * HZ)
133 struct rw_semaphore lock;
134 unsigned long threshold;
135 bool throttle_applied;
138 static void throttle_init(struct throttle *t)
140 init_rwsem(&t->lock);
141 t->throttle_applied = false;
144 static void throttle_work_start(struct throttle *t)
146 t->threshold = jiffies + THROTTLE_THRESHOLD;
149 static void throttle_work_update(struct throttle *t)
151 if (!t->throttle_applied && jiffies > t->threshold) {
152 down_write(&t->lock);
153 t->throttle_applied = true;
157 static void throttle_work_complete(struct throttle *t)
159 if (t->throttle_applied) {
160 t->throttle_applied = false;
165 static void throttle_lock(struct throttle *t)
170 static void throttle_unlock(struct throttle *t)
175 /*----------------------------------------------------------------*/
178 * A pool device ties together a metadata device and a data device. It
179 * also provides the interface for creating and destroying internal
182 struct dm_thin_new_mapping;
185 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
188 PM_WRITE, /* metadata may be changed */
189 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
190 PM_READ_ONLY, /* metadata may not be changed */
191 PM_FAIL, /* all I/O fails */
194 struct pool_features {
197 bool zero_new_blocks:1;
198 bool discard_enabled:1;
199 bool discard_passdown:1;
200 bool error_if_no_space:1;
204 typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
205 typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
206 typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
209 struct list_head list;
210 struct dm_target *ti; /* Only set if a pool target is bound */
212 struct mapped_device *pool_md;
213 struct block_device *md_dev;
214 struct dm_pool_metadata *pmd;
216 dm_block_t low_water_blocks;
217 uint32_t sectors_per_block;
218 int sectors_per_block_shift;
220 struct pool_features pf;
221 bool low_water_triggered:1; /* A dm event has been sent */
223 struct dm_bio_prison *prison;
224 struct dm_kcopyd_client *copier;
226 struct workqueue_struct *wq;
227 struct throttle throttle;
228 struct work_struct worker;
229 struct delayed_work waker;
230 struct delayed_work no_space_timeout;
232 unsigned long last_commit_jiffies;
236 struct bio_list deferred_flush_bios;
237 struct list_head prepared_mappings;
238 struct list_head prepared_discards;
239 struct list_head active_thins;
241 struct dm_deferred_set *shared_read_ds;
242 struct dm_deferred_set *all_io_ds;
244 struct dm_thin_new_mapping *next_mapping;
245 mempool_t *mapping_pool;
247 process_bio_fn process_bio;
248 process_bio_fn process_discard;
250 process_cell_fn process_cell;
251 process_cell_fn process_discard_cell;
253 process_mapping_fn process_prepared_mapping;
254 process_mapping_fn process_prepared_discard;
257 static enum pool_mode get_pool_mode(struct pool *pool);
258 static void metadata_operation_failed(struct pool *pool, const char *op, int r);
261 * Target context for a pool.
264 struct dm_target *ti;
266 struct dm_dev *data_dev;
267 struct dm_dev *metadata_dev;
268 struct dm_target_callbacks callbacks;
270 dm_block_t low_water_blocks;
271 struct pool_features requested_pf; /* Features requested during table load */
272 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
276 * Target context for a thin.
279 struct list_head list;
280 struct dm_dev *pool_dev;
281 struct dm_dev *origin_dev;
282 sector_t origin_size;
286 struct dm_thin_device *td;
289 struct list_head deferred_cells;
290 struct bio_list deferred_bio_list;
291 struct bio_list retry_on_resume_list;
292 struct rb_root sort_bio_list; /* sorted list of deferred bios */
295 * Ensures the thin is not destroyed until the worker has finished
296 * iterating the active_thins list.
299 struct completion can_destroy;
302 /*----------------------------------------------------------------*/
305 * wake_worker() is used when new work is queued and when pool_resume is
306 * ready to continue deferred IO processing.
308 static void wake_worker(struct pool *pool)
310 queue_work(pool->wq, &pool->worker);
313 /*----------------------------------------------------------------*/
315 static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
316 struct dm_bio_prison_cell **cell_result)
319 struct dm_bio_prison_cell *cell_prealloc;
322 * Allocate a cell from the prison's mempool.
323 * This might block but it can't fail.
325 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
327 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
330 * We reused an old cell; we can get rid of
333 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
338 static void cell_release(struct pool *pool,
339 struct dm_bio_prison_cell *cell,
340 struct bio_list *bios)
342 dm_cell_release(pool->prison, cell, bios);
343 dm_bio_prison_free_cell(pool->prison, cell);
346 static void cell_visit_release(struct pool *pool,
347 void (*fn)(void *, struct dm_bio_prison_cell *),
349 struct dm_bio_prison_cell *cell)
351 dm_cell_visit_release(pool->prison, fn, context, cell);
352 dm_bio_prison_free_cell(pool->prison, cell);
355 static void cell_release_no_holder(struct pool *pool,
356 struct dm_bio_prison_cell *cell,
357 struct bio_list *bios)
359 dm_cell_release_no_holder(pool->prison, cell, bios);
360 dm_bio_prison_free_cell(pool->prison, cell);
363 static void cell_error_with_code(struct pool *pool,
364 struct dm_bio_prison_cell *cell, int error_code)
366 dm_cell_error(pool->prison, cell, error_code);
367 dm_bio_prison_free_cell(pool->prison, cell);
370 static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
372 cell_error_with_code(pool, cell, -EIO);
375 static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
377 cell_error_with_code(pool, cell, 0);
380 static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
382 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
385 /*----------------------------------------------------------------*/
388 * A global list of pools that uses a struct mapped_device as a key.
390 static struct dm_thin_pool_table {
392 struct list_head pools;
393 } dm_thin_pool_table;
395 static void pool_table_init(void)
397 mutex_init(&dm_thin_pool_table.mutex);
398 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
401 static void __pool_table_insert(struct pool *pool)
403 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
404 list_add(&pool->list, &dm_thin_pool_table.pools);
407 static void __pool_table_remove(struct pool *pool)
409 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
410 list_del(&pool->list);
413 static struct pool *__pool_table_lookup(struct mapped_device *md)
415 struct pool *pool = NULL, *tmp;
417 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
419 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
420 if (tmp->pool_md == md) {
429 static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
431 struct pool *pool = NULL, *tmp;
433 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
435 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
436 if (tmp->md_dev == md_dev) {
445 /*----------------------------------------------------------------*/
447 struct dm_thin_endio_hook {
449 struct dm_deferred_entry *shared_read_entry;
450 struct dm_deferred_entry *all_io_entry;
451 struct dm_thin_new_mapping *overwrite_mapping;
452 struct rb_node rb_node;
455 static void requeue_bio_list(struct thin_c *tc, struct bio_list *master)
458 struct bio_list bios;
461 bio_list_init(&bios);
463 spin_lock_irqsave(&tc->lock, flags);
464 bio_list_merge(&bios, master);
465 bio_list_init(master);
466 spin_unlock_irqrestore(&tc->lock, flags);
468 while ((bio = bio_list_pop(&bios)))
469 bio_endio(bio, DM_ENDIO_REQUEUE);
472 static void requeue_deferred_cells(struct thin_c *tc)
474 struct pool *pool = tc->pool;
476 struct list_head cells;
477 struct dm_bio_prison_cell *cell, *tmp;
479 INIT_LIST_HEAD(&cells);
481 spin_lock_irqsave(&tc->lock, flags);
482 list_splice_init(&tc->deferred_cells, &cells);
483 spin_unlock_irqrestore(&tc->lock, flags);
485 list_for_each_entry_safe(cell, tmp, &cells, user_list)
486 cell_requeue(pool, cell);
489 static void requeue_io(struct thin_c *tc)
491 requeue_bio_list(tc, &tc->deferred_bio_list);
492 requeue_bio_list(tc, &tc->retry_on_resume_list);
493 requeue_deferred_cells(tc);
496 static void error_thin_retry_list(struct thin_c *tc)
500 struct bio_list bios;
502 bio_list_init(&bios);
504 spin_lock_irqsave(&tc->lock, flags);
505 bio_list_merge(&bios, &tc->retry_on_resume_list);
506 bio_list_init(&tc->retry_on_resume_list);
507 spin_unlock_irqrestore(&tc->lock, flags);
509 while ((bio = bio_list_pop(&bios)))
513 static void error_retry_list(struct pool *pool)
518 list_for_each_entry_rcu(tc, &pool->active_thins, list)
519 error_thin_retry_list(tc);
524 * This section of code contains the logic for processing a thin device's IO.
525 * Much of the code depends on pool object resources (lists, workqueues, etc)
526 * but most is exclusively called from the thin target rather than the thin-pool
530 static bool block_size_is_power_of_two(struct pool *pool)
532 return pool->sectors_per_block_shift >= 0;
535 static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
537 struct pool *pool = tc->pool;
538 sector_t block_nr = bio->bi_iter.bi_sector;
540 if (block_size_is_power_of_two(pool))
541 block_nr >>= pool->sectors_per_block_shift;
543 (void) sector_div(block_nr, pool->sectors_per_block);
548 static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
550 struct pool *pool = tc->pool;
551 sector_t bi_sector = bio->bi_iter.bi_sector;
553 bio->bi_bdev = tc->pool_dev->bdev;
554 if (block_size_is_power_of_two(pool))
555 bio->bi_iter.bi_sector =
556 (block << pool->sectors_per_block_shift) |
557 (bi_sector & (pool->sectors_per_block - 1));
559 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
560 sector_div(bi_sector, pool->sectors_per_block);
563 static void remap_to_origin(struct thin_c *tc, struct bio *bio)
565 bio->bi_bdev = tc->origin_dev->bdev;
568 static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
570 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
571 dm_thin_changed_this_transaction(tc->td);
574 static void inc_all_io_entry(struct pool *pool, struct bio *bio)
576 struct dm_thin_endio_hook *h;
578 if (bio->bi_rw & REQ_DISCARD)
581 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
582 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
585 static void issue(struct thin_c *tc, struct bio *bio)
587 struct pool *pool = tc->pool;
590 if (!bio_triggers_commit(tc, bio)) {
591 generic_make_request(bio);
596 * Complete bio with an error if earlier I/O caused changes to
597 * the metadata that can't be committed e.g, due to I/O errors
598 * on the metadata device.
600 if (dm_thin_aborted_changes(tc->td)) {
606 * Batch together any bios that trigger commits and then issue a
607 * single commit for them in process_deferred_bios().
609 spin_lock_irqsave(&pool->lock, flags);
610 bio_list_add(&pool->deferred_flush_bios, bio);
611 spin_unlock_irqrestore(&pool->lock, flags);
614 static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
616 remap_to_origin(tc, bio);
620 static void remap_and_issue(struct thin_c *tc, struct bio *bio,
623 remap(tc, bio, block);
627 /*----------------------------------------------------------------*/
630 * Bio endio functions.
632 struct dm_thin_new_mapping {
633 struct list_head list;
636 bool definitely_not_shared:1;
639 * Track quiescing, copying and zeroing preparation actions. When this
640 * counter hits zero the block is prepared and can be inserted into the
643 atomic_t prepare_actions;
647 dm_block_t virt_block;
648 dm_block_t data_block;
649 struct dm_bio_prison_cell *cell, *cell2;
652 * If the bio covers the whole area of a block then we can avoid
653 * zeroing or copying. Instead this bio is hooked. The bio will
654 * still be in the cell, so care has to be taken to avoid issuing
658 bio_end_io_t *saved_bi_end_io;
661 static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
663 struct pool *pool = m->tc->pool;
665 if (atomic_dec_and_test(&m->prepare_actions)) {
666 list_add_tail(&m->list, &pool->prepared_mappings);
671 static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
674 struct pool *pool = m->tc->pool;
676 spin_lock_irqsave(&pool->lock, flags);
677 __complete_mapping_preparation(m);
678 spin_unlock_irqrestore(&pool->lock, flags);
681 static void copy_complete(int read_err, unsigned long write_err, void *context)
683 struct dm_thin_new_mapping *m = context;
685 m->err = read_err || write_err ? -EIO : 0;
686 complete_mapping_preparation(m);
689 static void overwrite_endio(struct bio *bio, int err)
691 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
692 struct dm_thin_new_mapping *m = h->overwrite_mapping;
695 complete_mapping_preparation(m);
698 /*----------------------------------------------------------------*/
705 * Prepared mapping jobs.
709 * This sends the bios in the cell, except the original holder, back
710 * to the deferred_bios list.
712 static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
714 struct pool *pool = tc->pool;
717 spin_lock_irqsave(&tc->lock, flags);
718 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
719 spin_unlock_irqrestore(&tc->lock, flags);
724 static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
728 struct bio_list defer_bios;
729 struct bio_list issue_bios;
732 static void __inc_remap_and_issue_cell(void *context,
733 struct dm_bio_prison_cell *cell)
735 struct remap_info *info = context;
738 while ((bio = bio_list_pop(&cell->bios))) {
739 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
740 bio_list_add(&info->defer_bios, bio);
742 inc_all_io_entry(info->tc->pool, bio);
745 * We can't issue the bios with the bio prison lock
746 * held, so we add them to a list to issue on
747 * return from this function.
749 bio_list_add(&info->issue_bios, bio);
754 static void inc_remap_and_issue_cell(struct thin_c *tc,
755 struct dm_bio_prison_cell *cell,
759 struct remap_info info;
762 bio_list_init(&info.defer_bios);
763 bio_list_init(&info.issue_bios);
766 * We have to be careful to inc any bios we're about to issue
767 * before the cell is released, and avoid a race with new bios
768 * being added to the cell.
770 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
773 while ((bio = bio_list_pop(&info.defer_bios)))
774 thin_defer_bio(tc, bio);
776 while ((bio = bio_list_pop(&info.issue_bios)))
777 remap_and_issue(info.tc, bio, block);
780 static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
783 m->bio->bi_end_io = m->saved_bi_end_io;
784 atomic_inc(&m->bio->bi_remaining);
786 cell_error(m->tc->pool, m->cell);
788 mempool_free(m, m->tc->pool->mapping_pool);
791 static void process_prepared_mapping(struct dm_thin_new_mapping *m)
793 struct thin_c *tc = m->tc;
794 struct pool *pool = tc->pool;
800 bio->bi_end_io = m->saved_bi_end_io;
801 atomic_inc(&bio->bi_remaining);
805 cell_error(pool, m->cell);
810 * Commit the prepared block into the mapping btree.
811 * Any I/O for this block arriving after this point will get
812 * remapped to it directly.
814 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
816 metadata_operation_failed(pool, "dm_thin_insert_block", r);
817 cell_error(pool, m->cell);
822 * Release any bios held while the block was being provisioned.
823 * If we are processing a write bio that completely covers the block,
824 * we already processed it so can ignore it now when processing
825 * the bios in the cell.
828 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
831 inc_all_io_entry(tc->pool, m->cell->holder);
832 remap_and_issue(tc, m->cell->holder, m->data_block);
833 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
838 mempool_free(m, pool->mapping_pool);
841 static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
843 struct thin_c *tc = m->tc;
845 bio_io_error(m->bio);
846 cell_defer_no_holder(tc, m->cell);
847 cell_defer_no_holder(tc, m->cell2);
848 mempool_free(m, tc->pool->mapping_pool);
851 static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
853 struct thin_c *tc = m->tc;
855 inc_all_io_entry(tc->pool, m->bio);
856 cell_defer_no_holder(tc, m->cell);
857 cell_defer_no_holder(tc, m->cell2);
860 if (m->definitely_not_shared)
861 remap_and_issue(tc, m->bio, m->data_block);
864 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
865 bio_endio(m->bio, 0);
867 remap_and_issue(tc, m->bio, m->data_block);
870 bio_endio(m->bio, 0);
872 mempool_free(m, tc->pool->mapping_pool);
875 static void process_prepared_discard(struct dm_thin_new_mapping *m)
878 struct thin_c *tc = m->tc;
880 r = dm_thin_remove_block(tc->td, m->virt_block);
882 DMERR_LIMIT("dm_thin_remove_block() failed");
884 process_prepared_discard_passdown(m);
887 static void process_prepared(struct pool *pool, struct list_head *head,
888 process_mapping_fn *fn)
891 struct list_head maps;
892 struct dm_thin_new_mapping *m, *tmp;
894 INIT_LIST_HEAD(&maps);
895 spin_lock_irqsave(&pool->lock, flags);
896 list_splice_init(head, &maps);
897 spin_unlock_irqrestore(&pool->lock, flags);
899 list_for_each_entry_safe(m, tmp, &maps, list)
906 static int io_overlaps_block(struct pool *pool, struct bio *bio)
908 return bio->bi_iter.bi_size ==
909 (pool->sectors_per_block << SECTOR_SHIFT);
912 static int io_overwrites_block(struct pool *pool, struct bio *bio)
914 return (bio_data_dir(bio) == WRITE) &&
915 io_overlaps_block(pool, bio);
918 static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
921 *save = bio->bi_end_io;
925 static int ensure_next_mapping(struct pool *pool)
927 if (pool->next_mapping)
930 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
932 return pool->next_mapping ? 0 : -ENOMEM;
935 static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
937 struct dm_thin_new_mapping *m = pool->next_mapping;
939 BUG_ON(!pool->next_mapping);
941 memset(m, 0, sizeof(struct dm_thin_new_mapping));
942 INIT_LIST_HEAD(&m->list);
945 pool->next_mapping = NULL;
950 static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
951 sector_t begin, sector_t end)
954 struct dm_io_region to;
956 to.bdev = tc->pool_dev->bdev;
958 to.count = end - begin;
960 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
962 DMERR_LIMIT("dm_kcopyd_zero() failed");
963 copy_complete(1, 1, m);
967 static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
968 dm_block_t data_block,
969 struct dm_thin_new_mapping *m)
971 struct pool *pool = tc->pool;
972 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
974 h->overwrite_mapping = m;
976 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
977 inc_all_io_entry(pool, bio);
978 remap_and_issue(tc, bio, data_block);
982 * A partial copy also needs to zero the uncopied region.
984 static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
985 struct dm_dev *origin, dm_block_t data_origin,
986 dm_block_t data_dest,
987 struct dm_bio_prison_cell *cell, struct bio *bio,
991 struct pool *pool = tc->pool;
992 struct dm_thin_new_mapping *m = get_next_mapping(pool);
995 m->virt_block = virt_block;
996 m->data_block = data_dest;
1000 * quiesce action + copy action + an extra reference held for the
1001 * duration of this function (we may need to inc later for a
1004 atomic_set(&m->prepare_actions, 3);
1006 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
1007 complete_mapping_preparation(m); /* already quiesced */
1010 * IO to pool_dev remaps to the pool target's data_dev.
1012 * If the whole block of data is being overwritten, we can issue the
1013 * bio immediately. Otherwise we use kcopyd to clone the data first.
1015 if (io_overwrites_block(pool, bio))
1016 remap_and_issue_overwrite(tc, bio, data_dest, m);
1018 struct dm_io_region from, to;
1020 from.bdev = origin->bdev;
1021 from.sector = data_origin * pool->sectors_per_block;
1024 to.bdev = tc->pool_dev->bdev;
1025 to.sector = data_dest * pool->sectors_per_block;
1028 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1029 0, copy_complete, m);
1031 DMERR_LIMIT("dm_kcopyd_copy() failed");
1032 copy_complete(1, 1, m);
1035 * We allow the zero to be issued, to simplify the
1036 * error path. Otherwise we'd need to start
1037 * worrying about decrementing the prepare_actions
1043 * Do we need to zero a tail region?
1045 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1046 atomic_inc(&m->prepare_actions);
1048 data_dest * pool->sectors_per_block + len,
1049 (data_dest + 1) * pool->sectors_per_block);
1053 complete_mapping_preparation(m); /* drop our ref */
1056 static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1057 dm_block_t data_origin, dm_block_t data_dest,
1058 struct dm_bio_prison_cell *cell, struct bio *bio)
1060 schedule_copy(tc, virt_block, tc->pool_dev,
1061 data_origin, data_dest, cell, bio,
1062 tc->pool->sectors_per_block);
1065 static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
1066 dm_block_t data_block, struct dm_bio_prison_cell *cell,
1069 struct pool *pool = tc->pool;
1070 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1072 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
1074 m->virt_block = virt_block;
1075 m->data_block = data_block;
1079 * If the whole block of data is being overwritten or we are not
1080 * zeroing pre-existing data, we can issue the bio immediately.
1081 * Otherwise we use kcopyd to zero the data first.
1083 if (!pool->pf.zero_new_blocks)
1084 process_prepared_mapping(m);
1086 else if (io_overwrites_block(pool, bio))
1087 remap_and_issue_overwrite(tc, bio, data_block, m);
1091 data_block * pool->sectors_per_block,
1092 (data_block + 1) * pool->sectors_per_block);
1095 static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1096 dm_block_t data_dest,
1097 struct dm_bio_prison_cell *cell, struct bio *bio)
1099 struct pool *pool = tc->pool;
1100 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1101 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1103 if (virt_block_end <= tc->origin_size)
1104 schedule_copy(tc, virt_block, tc->origin_dev,
1105 virt_block, data_dest, cell, bio,
1106 pool->sectors_per_block);
1108 else if (virt_block_begin < tc->origin_size)
1109 schedule_copy(tc, virt_block, tc->origin_dev,
1110 virt_block, data_dest, cell, bio,
1111 tc->origin_size - virt_block_begin);
1114 schedule_zero(tc, virt_block, data_dest, cell, bio);
1118 * A non-zero return indicates read_only or fail_io mode.
1119 * Many callers don't care about the return value.
1121 static int commit(struct pool *pool)
1125 if (get_pool_mode(pool) >= PM_READ_ONLY)
1128 r = dm_pool_commit_metadata(pool->pmd);
1130 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
1135 static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1137 unsigned long flags;
1139 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1140 DMWARN("%s: reached low water mark for data device: sending event.",
1141 dm_device_name(pool->pool_md));
1142 spin_lock_irqsave(&pool->lock, flags);
1143 pool->low_water_triggered = true;
1144 spin_unlock_irqrestore(&pool->lock, flags);
1145 dm_table_event(pool->ti->table);
1149 static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1151 static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1154 dm_block_t free_blocks;
1155 struct pool *pool = tc->pool;
1157 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
1160 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1162 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
1166 check_low_water_mark(pool, free_blocks);
1170 * Try to commit to see if that will free up some
1177 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1179 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
1184 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
1189 r = dm_pool_alloc_data_block(pool->pmd, result);
1191 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
1199 * If we have run out of space, queue bios until the device is
1200 * resumed, presumably after having been reloaded with more space.
1202 static void retry_on_resume(struct bio *bio)
1204 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1205 struct thin_c *tc = h->tc;
1206 unsigned long flags;
1208 spin_lock_irqsave(&tc->lock, flags);
1209 bio_list_add(&tc->retry_on_resume_list, bio);
1210 spin_unlock_irqrestore(&tc->lock, flags);
1213 static int should_error_unserviceable_bio(struct pool *pool)
1215 enum pool_mode m = get_pool_mode(pool);
1219 /* Shouldn't get here */
1220 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1223 case PM_OUT_OF_DATA_SPACE:
1224 return pool->pf.error_if_no_space ? -ENOSPC : 0;
1230 /* Shouldn't get here */
1231 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1236 static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1238 int error = should_error_unserviceable_bio(pool);
1241 bio_endio(bio, error);
1243 retry_on_resume(bio);
1246 static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
1249 struct bio_list bios;
1252 error = should_error_unserviceable_bio(pool);
1254 cell_error_with_code(pool, cell, error);
1258 bio_list_init(&bios);
1259 cell_release(pool, cell, &bios);
1261 error = should_error_unserviceable_bio(pool);
1263 while ((bio = bio_list_pop(&bios)))
1264 bio_endio(bio, error);
1266 while ((bio = bio_list_pop(&bios)))
1267 retry_on_resume(bio);
1270 static void process_discard_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1273 struct bio *bio = cell->holder;
1274 struct pool *pool = tc->pool;
1275 struct dm_bio_prison_cell *cell2;
1276 struct dm_cell_key key2;
1277 dm_block_t block = get_bio_block(tc, bio);
1278 struct dm_thin_lookup_result lookup_result;
1279 struct dm_thin_new_mapping *m;
1281 if (tc->requeue_mode) {
1282 cell_requeue(pool, cell);
1286 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1290 * Check nobody is fiddling with this pool block. This can
1291 * happen if someone's in the process of breaking sharing
1294 build_data_key(tc->td, lookup_result.block, &key2);
1295 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
1296 cell_defer_no_holder(tc, cell);
1300 if (io_overlaps_block(pool, bio)) {
1302 * IO may still be going to the destination block. We must
1303 * quiesce before we can do the removal.
1305 m = get_next_mapping(pool);
1307 m->pass_discard = pool->pf.discard_passdown;
1308 m->definitely_not_shared = !lookup_result.shared;
1309 m->virt_block = block;
1310 m->data_block = lookup_result.block;
1315 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1316 pool->process_prepared_discard(m);
1319 inc_all_io_entry(pool, bio);
1320 cell_defer_no_holder(tc, cell);
1321 cell_defer_no_holder(tc, cell2);
1324 * The DM core makes sure that the discard doesn't span
1325 * a block boundary. So we submit the discard of a
1326 * partial block appropriately.
1328 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1329 remap_and_issue(tc, bio, lookup_result.block);
1337 * It isn't provisioned, just forget it.
1339 cell_defer_no_holder(tc, cell);
1344 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1346 cell_defer_no_holder(tc, cell);
1352 static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1354 struct dm_bio_prison_cell *cell;
1355 struct dm_cell_key key;
1356 dm_block_t block = get_bio_block(tc, bio);
1358 build_virtual_key(tc->td, block, &key);
1359 if (bio_detain(tc->pool, &key, bio, &cell))
1362 process_discard_cell(tc, cell);
1365 static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
1366 struct dm_cell_key *key,
1367 struct dm_thin_lookup_result *lookup_result,
1368 struct dm_bio_prison_cell *cell)
1371 dm_block_t data_block;
1372 struct pool *pool = tc->pool;
1374 r = alloc_data_block(tc, &data_block);
1377 schedule_internal_copy(tc, block, lookup_result->block,
1378 data_block, cell, bio);
1382 retry_bios_on_resume(pool, cell);
1386 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1388 cell_error(pool, cell);
1393 static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1395 struct dm_thin_lookup_result *lookup_result)
1397 struct dm_bio_prison_cell *cell;
1398 struct pool *pool = tc->pool;
1399 struct dm_cell_key key;
1402 * If cell is already occupied, then sharing is already in the process
1403 * of being broken so we have nothing further to do here.
1405 build_data_key(tc->td, lookup_result->block, &key);
1406 if (bio_detain(pool, &key, bio, &cell))
1409 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size)
1410 break_sharing(tc, bio, block, &key, lookup_result, cell);
1412 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1414 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
1415 inc_all_io_entry(pool, bio);
1416 cell_defer_no_holder(tc, cell);
1418 remap_and_issue(tc, bio, lookup_result->block);
1422 static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
1423 struct dm_bio_prison_cell *cell)
1426 dm_block_t data_block;
1427 struct pool *pool = tc->pool;
1430 * Remap empty bios (flushes) immediately, without provisioning.
1432 if (!bio->bi_iter.bi_size) {
1433 inc_all_io_entry(pool, bio);
1434 cell_defer_no_holder(tc, cell);
1436 remap_and_issue(tc, bio, 0);
1441 * Fill read bios with zeroes and complete them immediately.
1443 if (bio_data_dir(bio) == READ) {
1445 cell_defer_no_holder(tc, cell);
1450 r = alloc_data_block(tc, &data_block);
1454 schedule_external_copy(tc, block, data_block, cell, bio);
1456 schedule_zero(tc, block, data_block, cell, bio);
1460 retry_bios_on_resume(pool, cell);
1464 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1466 cell_error(pool, cell);
1471 static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1474 struct pool *pool = tc->pool;
1475 struct bio *bio = cell->holder;
1476 dm_block_t block = get_bio_block(tc, bio);
1477 struct dm_thin_lookup_result lookup_result;
1479 if (tc->requeue_mode) {
1480 cell_requeue(pool, cell);
1484 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1487 if (lookup_result.shared) {
1488 process_shared_bio(tc, bio, block, &lookup_result);
1489 // FIXME: we can't remap because we're waiting on a commit
1490 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
1492 inc_all_io_entry(pool, bio);
1493 remap_and_issue(tc, bio, lookup_result.block);
1494 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
1499 if (bio_data_dir(bio) == READ && tc->origin_dev) {
1500 inc_all_io_entry(pool, bio);
1501 cell_defer_no_holder(tc, cell);
1503 if (bio_end_sector(bio) <= tc->origin_size)
1504 remap_to_origin_and_issue(tc, bio);
1506 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1508 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1509 remap_to_origin_and_issue(tc, bio);
1516 provision_block(tc, bio, block, cell);
1520 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1522 cell_defer_no_holder(tc, cell);
1528 static void process_bio(struct thin_c *tc, struct bio *bio)
1530 struct pool *pool = tc->pool;
1531 dm_block_t block = get_bio_block(tc, bio);
1532 struct dm_bio_prison_cell *cell;
1533 struct dm_cell_key key;
1536 * If cell is already occupied, then the block is already
1537 * being provisioned so we have nothing further to do here.
1539 build_virtual_key(tc->td, block, &key);
1540 if (bio_detain(pool, &key, bio, &cell))
1543 process_cell(tc, cell);
1546 static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1547 struct dm_bio_prison_cell *cell)
1550 int rw = bio_data_dir(bio);
1551 dm_block_t block = get_bio_block(tc, bio);
1552 struct dm_thin_lookup_result lookup_result;
1554 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1557 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
1558 handle_unserviceable_bio(tc->pool, bio);
1560 cell_defer_no_holder(tc, cell);
1562 inc_all_io_entry(tc->pool, bio);
1563 remap_and_issue(tc, bio, lookup_result.block);
1565 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
1571 cell_defer_no_holder(tc, cell);
1573 handle_unserviceable_bio(tc->pool, bio);
1577 if (tc->origin_dev) {
1578 inc_all_io_entry(tc->pool, bio);
1579 remap_to_origin_and_issue(tc, bio);
1588 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1591 cell_defer_no_holder(tc, cell);
1597 static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1599 __process_bio_read_only(tc, bio, NULL);
1602 static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1604 __process_bio_read_only(tc, cell->holder, cell);
1607 static void process_bio_success(struct thin_c *tc, struct bio *bio)
1612 static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1617 static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1619 cell_success(tc->pool, cell);
1622 static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1624 cell_error(tc->pool, cell);
1628 * FIXME: should we also commit due to size of transaction, measured in
1631 static int need_commit_due_to_time(struct pool *pool)
1633 return jiffies < pool->last_commit_jiffies ||
1634 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1637 #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1638 #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1640 static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1642 struct rb_node **rbp, *parent;
1643 struct dm_thin_endio_hook *pbd;
1644 sector_t bi_sector = bio->bi_iter.bi_sector;
1646 rbp = &tc->sort_bio_list.rb_node;
1650 pbd = thin_pbd(parent);
1652 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1653 rbp = &(*rbp)->rb_left;
1655 rbp = &(*rbp)->rb_right;
1658 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1659 rb_link_node(&pbd->rb_node, parent, rbp);
1660 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1663 static void __extract_sorted_bios(struct thin_c *tc)
1665 struct rb_node *node;
1666 struct dm_thin_endio_hook *pbd;
1669 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1670 pbd = thin_pbd(node);
1671 bio = thin_bio(pbd);
1673 bio_list_add(&tc->deferred_bio_list, bio);
1674 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1677 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1680 static void __sort_thin_deferred_bios(struct thin_c *tc)
1683 struct bio_list bios;
1685 bio_list_init(&bios);
1686 bio_list_merge(&bios, &tc->deferred_bio_list);
1687 bio_list_init(&tc->deferred_bio_list);
1689 /* Sort deferred_bio_list using rb-tree */
1690 while ((bio = bio_list_pop(&bios)))
1691 __thin_bio_rb_add(tc, bio);
1694 * Transfer the sorted bios in sort_bio_list back to
1695 * deferred_bio_list to allow lockless submission of
1698 __extract_sorted_bios(tc);
1701 static void process_thin_deferred_bios(struct thin_c *tc)
1703 struct pool *pool = tc->pool;
1704 unsigned long flags;
1706 struct bio_list bios;
1707 struct blk_plug plug;
1710 if (tc->requeue_mode) {
1711 requeue_bio_list(tc, &tc->deferred_bio_list);
1715 bio_list_init(&bios);
1717 spin_lock_irqsave(&tc->lock, flags);
1719 if (bio_list_empty(&tc->deferred_bio_list)) {
1720 spin_unlock_irqrestore(&tc->lock, flags);
1724 __sort_thin_deferred_bios(tc);
1726 bio_list_merge(&bios, &tc->deferred_bio_list);
1727 bio_list_init(&tc->deferred_bio_list);
1729 spin_unlock_irqrestore(&tc->lock, flags);
1731 blk_start_plug(&plug);
1732 while ((bio = bio_list_pop(&bios))) {
1734 * If we've got no free new_mapping structs, and processing
1735 * this bio might require one, we pause until there are some
1736 * prepared mappings to process.
1738 if (ensure_next_mapping(pool)) {
1739 spin_lock_irqsave(&tc->lock, flags);
1740 bio_list_add(&tc->deferred_bio_list, bio);
1741 bio_list_merge(&tc->deferred_bio_list, &bios);
1742 spin_unlock_irqrestore(&tc->lock, flags);
1746 if (bio->bi_rw & REQ_DISCARD)
1747 pool->process_discard(tc, bio);
1749 pool->process_bio(tc, bio);
1751 if ((count++ & 127) == 0) {
1752 throttle_work_update(&pool->throttle);
1753 dm_pool_issue_prefetches(pool->pmd);
1756 blk_finish_plug(&plug);
1759 static void process_thin_deferred_cells(struct thin_c *tc)
1761 struct pool *pool = tc->pool;
1762 unsigned long flags;
1763 struct list_head cells;
1764 struct dm_bio_prison_cell *cell, *tmp;
1766 INIT_LIST_HEAD(&cells);
1768 spin_lock_irqsave(&tc->lock, flags);
1769 list_splice_init(&tc->deferred_cells, &cells);
1770 spin_unlock_irqrestore(&tc->lock, flags);
1772 if (list_empty(&cells))
1775 list_for_each_entry_safe(cell, tmp, &cells, user_list) {
1776 BUG_ON(!cell->holder);
1779 * If we've got no free new_mapping structs, and processing
1780 * this bio might require one, we pause until there are some
1781 * prepared mappings to process.
1783 if (ensure_next_mapping(pool)) {
1784 spin_lock_irqsave(&tc->lock, flags);
1785 list_add(&cell->user_list, &tc->deferred_cells);
1786 list_splice(&cells, &tc->deferred_cells);
1787 spin_unlock_irqrestore(&tc->lock, flags);
1791 if (cell->holder->bi_rw & REQ_DISCARD)
1792 pool->process_discard_cell(tc, cell);
1794 pool->process_cell(tc, cell);
1798 static void thin_get(struct thin_c *tc);
1799 static void thin_put(struct thin_c *tc);
1802 * We can't hold rcu_read_lock() around code that can block. So we
1803 * find a thin with the rcu lock held; bump a refcount; then drop
1806 static struct thin_c *get_first_thin(struct pool *pool)
1808 struct thin_c *tc = NULL;
1811 if (!list_empty(&pool->active_thins)) {
1812 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
1820 static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
1822 struct thin_c *old_tc = tc;
1825 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
1837 static void process_deferred_bios(struct pool *pool)
1839 unsigned long flags;
1841 struct bio_list bios;
1844 tc = get_first_thin(pool);
1846 process_thin_deferred_cells(tc);
1847 process_thin_deferred_bios(tc);
1848 tc = get_next_thin(pool, tc);
1852 * If there are any deferred flush bios, we must commit
1853 * the metadata before issuing them.
1855 bio_list_init(&bios);
1856 spin_lock_irqsave(&pool->lock, flags);
1857 bio_list_merge(&bios, &pool->deferred_flush_bios);
1858 bio_list_init(&pool->deferred_flush_bios);
1859 spin_unlock_irqrestore(&pool->lock, flags);
1861 if (bio_list_empty(&bios) &&
1862 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
1866 while ((bio = bio_list_pop(&bios)))
1870 pool->last_commit_jiffies = jiffies;
1872 while ((bio = bio_list_pop(&bios)))
1873 generic_make_request(bio);
1876 static void do_worker(struct work_struct *ws)
1878 struct pool *pool = container_of(ws, struct pool, worker);
1880 throttle_work_start(&pool->throttle);
1881 dm_pool_issue_prefetches(pool->pmd);
1882 throttle_work_update(&pool->throttle);
1883 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1884 throttle_work_update(&pool->throttle);
1885 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
1886 throttle_work_update(&pool->throttle);
1887 process_deferred_bios(pool);
1888 throttle_work_complete(&pool->throttle);
1892 * We want to commit periodically so that not too much
1893 * unwritten data builds up.
1895 static void do_waker(struct work_struct *ws)
1897 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1899 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1903 * We're holding onto IO to allow userland time to react. After the
1904 * timeout either the pool will have been resized (and thus back in
1905 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
1907 static void do_no_space_timeout(struct work_struct *ws)
1909 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
1912 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
1913 set_pool_mode(pool, PM_READ_ONLY);
1916 /*----------------------------------------------------------------*/
1919 struct work_struct worker;
1920 struct completion complete;
1923 static struct pool_work *to_pool_work(struct work_struct *ws)
1925 return container_of(ws, struct pool_work, worker);
1928 static void pool_work_complete(struct pool_work *pw)
1930 complete(&pw->complete);
1933 static void pool_work_wait(struct pool_work *pw, struct pool *pool,
1934 void (*fn)(struct work_struct *))
1936 INIT_WORK_ONSTACK(&pw->worker, fn);
1937 init_completion(&pw->complete);
1938 queue_work(pool->wq, &pw->worker);
1939 wait_for_completion(&pw->complete);
1942 /*----------------------------------------------------------------*/
1944 struct noflush_work {
1945 struct pool_work pw;
1949 static struct noflush_work *to_noflush(struct work_struct *ws)
1951 return container_of(to_pool_work(ws), struct noflush_work, pw);
1954 static void do_noflush_start(struct work_struct *ws)
1956 struct noflush_work *w = to_noflush(ws);
1957 w->tc->requeue_mode = true;
1959 pool_work_complete(&w->pw);
1962 static void do_noflush_stop(struct work_struct *ws)
1964 struct noflush_work *w = to_noflush(ws);
1965 w->tc->requeue_mode = false;
1966 pool_work_complete(&w->pw);
1969 static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
1971 struct noflush_work w;
1974 pool_work_wait(&w.pw, tc->pool, fn);
1977 /*----------------------------------------------------------------*/
1979 static enum pool_mode get_pool_mode(struct pool *pool)
1981 return pool->pf.mode;
1984 static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
1986 dm_table_event(pool->ti->table);
1987 DMINFO("%s: switching pool to %s mode",
1988 dm_device_name(pool->pool_md), new_mode);
1991 static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
1993 struct pool_c *pt = pool->ti->private;
1994 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
1995 enum pool_mode old_mode = get_pool_mode(pool);
1996 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
1999 * Never allow the pool to transition to PM_WRITE mode if user
2000 * intervention is required to verify metadata and data consistency.
2002 if (new_mode == PM_WRITE && needs_check) {
2003 DMERR("%s: unable to switch pool to write mode until repaired.",
2004 dm_device_name(pool->pool_md));
2005 if (old_mode != new_mode)
2006 new_mode = old_mode;
2008 new_mode = PM_READ_ONLY;
2011 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2012 * not going to recover without a thin_repair. So we never let the
2013 * pool move out of the old mode.
2015 if (old_mode == PM_FAIL)
2016 new_mode = old_mode;
2020 if (old_mode != new_mode)
2021 notify_of_pool_mode_change(pool, "failure");
2022 dm_pool_metadata_read_only(pool->pmd);
2023 pool->process_bio = process_bio_fail;
2024 pool->process_discard = process_bio_fail;
2025 pool->process_cell = process_cell_fail;
2026 pool->process_discard_cell = process_cell_fail;
2027 pool->process_prepared_mapping = process_prepared_mapping_fail;
2028 pool->process_prepared_discard = process_prepared_discard_fail;
2030 error_retry_list(pool);
2034 if (old_mode != new_mode)
2035 notify_of_pool_mode_change(pool, "read-only");
2036 dm_pool_metadata_read_only(pool->pmd);
2037 pool->process_bio = process_bio_read_only;
2038 pool->process_discard = process_bio_success;
2039 pool->process_cell = process_cell_read_only;
2040 pool->process_discard_cell = process_cell_success;
2041 pool->process_prepared_mapping = process_prepared_mapping_fail;
2042 pool->process_prepared_discard = process_prepared_discard_passdown;
2044 error_retry_list(pool);
2047 case PM_OUT_OF_DATA_SPACE:
2049 * Ideally we'd never hit this state; the low water mark
2050 * would trigger userland to extend the pool before we
2051 * completely run out of data space. However, many small
2052 * IOs to unprovisioned space can consume data space at an
2053 * alarming rate. Adjust your low water mark if you're
2054 * frequently seeing this mode.
2056 if (old_mode != new_mode)
2057 notify_of_pool_mode_change(pool, "out-of-data-space");
2058 pool->process_bio = process_bio_read_only;
2059 pool->process_discard = process_discard_bio;
2060 pool->process_cell = process_cell_read_only;
2061 pool->process_discard_cell = process_discard_cell;
2062 pool->process_prepared_mapping = process_prepared_mapping;
2063 pool->process_prepared_discard = process_prepared_discard_passdown;
2065 if (!pool->pf.error_if_no_space && no_space_timeout)
2066 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
2070 if (old_mode != new_mode)
2071 notify_of_pool_mode_change(pool, "write");
2072 dm_pool_metadata_read_write(pool->pmd);
2073 pool->process_bio = process_bio;
2074 pool->process_discard = process_discard_bio;
2075 pool->process_cell = process_cell;
2076 pool->process_discard_cell = process_discard_cell;
2077 pool->process_prepared_mapping = process_prepared_mapping;
2078 pool->process_prepared_discard = process_prepared_discard;
2082 pool->pf.mode = new_mode;
2084 * The pool mode may have changed, sync it so bind_control_target()
2085 * doesn't cause an unexpected mode transition on resume.
2087 pt->adjusted_pf.mode = new_mode;
2090 static void abort_transaction(struct pool *pool)
2092 const char *dev_name = dm_device_name(pool->pool_md);
2094 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2095 if (dm_pool_abort_metadata(pool->pmd)) {
2096 DMERR("%s: failed to abort metadata transaction", dev_name);
2097 set_pool_mode(pool, PM_FAIL);
2100 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2101 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2102 set_pool_mode(pool, PM_FAIL);
2106 static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2108 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2109 dm_device_name(pool->pool_md), op, r);
2111 abort_transaction(pool);
2112 set_pool_mode(pool, PM_READ_ONLY);
2115 /*----------------------------------------------------------------*/
2118 * Mapping functions.
2122 * Called only while mapping a thin bio to hand it over to the workqueue.
2124 static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2126 unsigned long flags;
2127 struct pool *pool = tc->pool;
2129 spin_lock_irqsave(&tc->lock, flags);
2130 bio_list_add(&tc->deferred_bio_list, bio);
2131 spin_unlock_irqrestore(&tc->lock, flags);
2136 static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2138 struct pool *pool = tc->pool;
2140 throttle_lock(&pool->throttle);
2141 thin_defer_bio(tc, bio);
2142 throttle_unlock(&pool->throttle);
2145 static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2147 unsigned long flags;
2148 struct pool *pool = tc->pool;
2150 throttle_lock(&pool->throttle);
2151 spin_lock_irqsave(&tc->lock, flags);
2152 list_add_tail(&cell->user_list, &tc->deferred_cells);
2153 spin_unlock_irqrestore(&tc->lock, flags);
2154 throttle_unlock(&pool->throttle);
2159 static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
2161 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2164 h->shared_read_entry = NULL;
2165 h->all_io_entry = NULL;
2166 h->overwrite_mapping = NULL;
2170 * Non-blocking function called from the thin target's map function.
2172 static int thin_bio_map(struct dm_target *ti, struct bio *bio)
2175 struct thin_c *tc = ti->private;
2176 dm_block_t block = get_bio_block(tc, bio);
2177 struct dm_thin_device *td = tc->td;
2178 struct dm_thin_lookup_result result;
2179 struct dm_bio_prison_cell *virt_cell, *data_cell;
2180 struct dm_cell_key key;
2182 thin_hook_bio(tc, bio);
2184 if (tc->requeue_mode) {
2185 bio_endio(bio, DM_ENDIO_REQUEUE);
2186 return DM_MAPIO_SUBMITTED;
2189 if (get_pool_mode(tc->pool) == PM_FAIL) {
2191 return DM_MAPIO_SUBMITTED;
2194 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
2195 thin_defer_bio_with_throttle(tc, bio);
2196 return DM_MAPIO_SUBMITTED;
2200 * We must hold the virtual cell before doing the lookup, otherwise
2201 * there's a race with discard.
2203 build_virtual_key(tc->td, block, &key);
2204 if (bio_detain(tc->pool, &key, bio, &virt_cell))
2205 return DM_MAPIO_SUBMITTED;
2207 r = dm_thin_find_block(td, block, 0, &result);
2210 * Note that we defer readahead too.
2214 if (unlikely(result.shared)) {
2216 * We have a race condition here between the
2217 * result.shared value returned by the lookup and
2218 * snapshot creation, which may cause new
2221 * To avoid this always quiesce the origin before
2222 * taking the snap. You want to do this anyway to
2223 * ensure a consistent application view
2226 * More distant ancestors are irrelevant. The
2227 * shared flag will be set in their case.
2229 thin_defer_cell(tc, virt_cell);
2230 return DM_MAPIO_SUBMITTED;
2233 build_data_key(tc->td, result.block, &key);
2234 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2235 cell_defer_no_holder(tc, virt_cell);
2236 return DM_MAPIO_SUBMITTED;
2239 inc_all_io_entry(tc->pool, bio);
2240 cell_defer_no_holder(tc, data_cell);
2241 cell_defer_no_holder(tc, virt_cell);
2243 remap(tc, bio, result.block);
2244 return DM_MAPIO_REMAPPED;
2247 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
2249 * This block isn't provisioned, and we have no way
2252 handle_unserviceable_bio(tc->pool, bio);
2253 cell_defer_no_holder(tc, virt_cell);
2254 return DM_MAPIO_SUBMITTED;
2259 thin_defer_cell(tc, virt_cell);
2260 return DM_MAPIO_SUBMITTED;
2264 * Must always call bio_io_error on failure.
2265 * dm_thin_find_block can fail with -EINVAL if the
2266 * pool is switched to fail-io mode.
2269 cell_defer_no_holder(tc, virt_cell);
2270 return DM_MAPIO_SUBMITTED;
2274 static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2276 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
2277 struct request_queue *q;
2279 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2282 q = bdev_get_queue(pt->data_dev->bdev);
2283 return bdi_congested(&q->backing_dev_info, bdi_bits);
2286 static void requeue_bios(struct pool *pool)
2288 unsigned long flags;
2292 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2293 spin_lock_irqsave(&tc->lock, flags);
2294 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2295 bio_list_init(&tc->retry_on_resume_list);
2296 spin_unlock_irqrestore(&tc->lock, flags);
2301 /*----------------------------------------------------------------
2302 * Binding of control targets to a pool object
2303 *--------------------------------------------------------------*/
2304 static bool data_dev_supports_discard(struct pool_c *pt)
2306 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2308 return q && blk_queue_discard(q);
2311 static bool is_factor(sector_t block_size, uint32_t n)
2313 return !sector_div(block_size, n);
2317 * If discard_passdown was enabled verify that the data device
2318 * supports discards. Disable discard_passdown if not.
2320 static void disable_passdown_if_not_supported(struct pool_c *pt)
2322 struct pool *pool = pt->pool;
2323 struct block_device *data_bdev = pt->data_dev->bdev;
2324 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2325 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
2326 const char *reason = NULL;
2327 char buf[BDEVNAME_SIZE];
2329 if (!pt->adjusted_pf.discard_passdown)
2332 if (!data_dev_supports_discard(pt))
2333 reason = "discard unsupported";
2335 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2336 reason = "max discard sectors smaller than a block";
2338 else if (data_limits->discard_granularity > block_size)
2339 reason = "discard granularity larger than a block";
2341 else if (!is_factor(block_size, data_limits->discard_granularity))
2342 reason = "discard granularity not a factor of block size";
2345 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2346 pt->adjusted_pf.discard_passdown = false;
2350 static int bind_control_target(struct pool *pool, struct dm_target *ti)
2352 struct pool_c *pt = ti->private;
2355 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
2357 enum pool_mode old_mode = get_pool_mode(pool);
2358 enum pool_mode new_mode = pt->adjusted_pf.mode;
2361 * Don't change the pool's mode until set_pool_mode() below.
2362 * Otherwise the pool's process_* function pointers may
2363 * not match the desired pool mode.
2365 pt->adjusted_pf.mode = old_mode;
2368 pool->pf = pt->adjusted_pf;
2369 pool->low_water_blocks = pt->low_water_blocks;
2371 set_pool_mode(pool, new_mode);
2376 static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2382 /*----------------------------------------------------------------
2384 *--------------------------------------------------------------*/
2385 /* Initialize pool features. */
2386 static void pool_features_init(struct pool_features *pf)
2388 pf->mode = PM_WRITE;
2389 pf->zero_new_blocks = true;
2390 pf->discard_enabled = true;
2391 pf->discard_passdown = true;
2392 pf->error_if_no_space = false;
2395 static void __pool_destroy(struct pool *pool)
2397 __pool_table_remove(pool);
2399 if (dm_pool_metadata_close(pool->pmd) < 0)
2400 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2402 dm_bio_prison_destroy(pool->prison);
2403 dm_kcopyd_client_destroy(pool->copier);
2406 destroy_workqueue(pool->wq);
2408 if (pool->next_mapping)
2409 mempool_free(pool->next_mapping, pool->mapping_pool);
2410 mempool_destroy(pool->mapping_pool);
2411 dm_deferred_set_destroy(pool->shared_read_ds);
2412 dm_deferred_set_destroy(pool->all_io_ds);
2416 static struct kmem_cache *_new_mapping_cache;
2418 static struct pool *pool_create(struct mapped_device *pool_md,
2419 struct block_device *metadata_dev,
2420 unsigned long block_size,
2421 int read_only, char **error)
2426 struct dm_pool_metadata *pmd;
2427 bool format_device = read_only ? false : true;
2429 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
2431 *error = "Error creating metadata object";
2432 return (struct pool *)pmd;
2435 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2437 *error = "Error allocating memory for pool";
2438 err_p = ERR_PTR(-ENOMEM);
2443 pool->sectors_per_block = block_size;
2444 if (block_size & (block_size - 1))
2445 pool->sectors_per_block_shift = -1;
2447 pool->sectors_per_block_shift = __ffs(block_size);
2448 pool->low_water_blocks = 0;
2449 pool_features_init(&pool->pf);
2450 pool->prison = dm_bio_prison_create();
2451 if (!pool->prison) {
2452 *error = "Error creating pool's bio prison";
2453 err_p = ERR_PTR(-ENOMEM);
2457 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2458 if (IS_ERR(pool->copier)) {
2459 r = PTR_ERR(pool->copier);
2460 *error = "Error creating pool's kcopyd client";
2462 goto bad_kcopyd_client;
2466 * Create singlethreaded workqueue that will service all devices
2467 * that use this metadata.
2469 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2471 *error = "Error creating pool's workqueue";
2472 err_p = ERR_PTR(-ENOMEM);
2476 throttle_init(&pool->throttle);
2477 INIT_WORK(&pool->worker, do_worker);
2478 INIT_DELAYED_WORK(&pool->waker, do_waker);
2479 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
2480 spin_lock_init(&pool->lock);
2481 bio_list_init(&pool->deferred_flush_bios);
2482 INIT_LIST_HEAD(&pool->prepared_mappings);
2483 INIT_LIST_HEAD(&pool->prepared_discards);
2484 INIT_LIST_HEAD(&pool->active_thins);
2485 pool->low_water_triggered = false;
2487 pool->shared_read_ds = dm_deferred_set_create();
2488 if (!pool->shared_read_ds) {
2489 *error = "Error creating pool's shared read deferred set";
2490 err_p = ERR_PTR(-ENOMEM);
2491 goto bad_shared_read_ds;
2494 pool->all_io_ds = dm_deferred_set_create();
2495 if (!pool->all_io_ds) {
2496 *error = "Error creating pool's all io deferred set";
2497 err_p = ERR_PTR(-ENOMEM);
2501 pool->next_mapping = NULL;
2502 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2503 _new_mapping_cache);
2504 if (!pool->mapping_pool) {
2505 *error = "Error creating pool's mapping mempool";
2506 err_p = ERR_PTR(-ENOMEM);
2507 goto bad_mapping_pool;
2510 pool->ref_count = 1;
2511 pool->last_commit_jiffies = jiffies;
2512 pool->pool_md = pool_md;
2513 pool->md_dev = metadata_dev;
2514 __pool_table_insert(pool);
2519 dm_deferred_set_destroy(pool->all_io_ds);
2521 dm_deferred_set_destroy(pool->shared_read_ds);
2523 destroy_workqueue(pool->wq);
2525 dm_kcopyd_client_destroy(pool->copier);
2527 dm_bio_prison_destroy(pool->prison);
2531 if (dm_pool_metadata_close(pmd))
2532 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2537 static void __pool_inc(struct pool *pool)
2539 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2543 static void __pool_dec(struct pool *pool)
2545 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2546 BUG_ON(!pool->ref_count);
2547 if (!--pool->ref_count)
2548 __pool_destroy(pool);
2551 static struct pool *__pool_find(struct mapped_device *pool_md,
2552 struct block_device *metadata_dev,
2553 unsigned long block_size, int read_only,
2554 char **error, int *created)
2556 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2559 if (pool->pool_md != pool_md) {
2560 *error = "metadata device already in use by a pool";
2561 return ERR_PTR(-EBUSY);
2566 pool = __pool_table_lookup(pool_md);
2568 if (pool->md_dev != metadata_dev) {
2569 *error = "different pool cannot replace a pool";
2570 return ERR_PTR(-EINVAL);
2575 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
2583 /*----------------------------------------------------------------
2584 * Pool target methods
2585 *--------------------------------------------------------------*/
2586 static void pool_dtr(struct dm_target *ti)
2588 struct pool_c *pt = ti->private;
2590 mutex_lock(&dm_thin_pool_table.mutex);
2592 unbind_control_target(pt->pool, ti);
2593 __pool_dec(pt->pool);
2594 dm_put_device(ti, pt->metadata_dev);
2595 dm_put_device(ti, pt->data_dev);
2598 mutex_unlock(&dm_thin_pool_table.mutex);
2601 static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2602 struct dm_target *ti)
2606 const char *arg_name;
2608 static struct dm_arg _args[] = {
2609 {0, 4, "Invalid number of pool feature arguments"},
2613 * No feature arguments supplied.
2618 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2622 while (argc && !r) {
2623 arg_name = dm_shift_arg(as);
2626 if (!strcasecmp(arg_name, "skip_block_zeroing"))
2627 pf->zero_new_blocks = false;
2629 else if (!strcasecmp(arg_name, "ignore_discard"))
2630 pf->discard_enabled = false;
2632 else if (!strcasecmp(arg_name, "no_discard_passdown"))
2633 pf->discard_passdown = false;
2635 else if (!strcasecmp(arg_name, "read_only"))
2636 pf->mode = PM_READ_ONLY;
2638 else if (!strcasecmp(arg_name, "error_if_no_space"))
2639 pf->error_if_no_space = true;
2642 ti->error = "Unrecognised pool feature requested";
2651 static void metadata_low_callback(void *context)
2653 struct pool *pool = context;
2655 DMWARN("%s: reached low water mark for metadata device: sending event.",
2656 dm_device_name(pool->pool_md));
2658 dm_table_event(pool->ti->table);
2661 static sector_t get_dev_size(struct block_device *bdev)
2663 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2666 static void warn_if_metadata_device_too_big(struct block_device *bdev)
2668 sector_t metadata_dev_size = get_dev_size(bdev);
2669 char buffer[BDEVNAME_SIZE];
2671 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
2672 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2673 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
2676 static sector_t get_metadata_dev_size(struct block_device *bdev)
2678 sector_t metadata_dev_size = get_dev_size(bdev);
2680 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2681 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
2683 return metadata_dev_size;
2686 static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2688 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2690 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
2692 return metadata_dev_size;
2696 * When a metadata threshold is crossed a dm event is triggered, and
2697 * userland should respond by growing the metadata device. We could let
2698 * userland set the threshold, like we do with the data threshold, but I'm
2699 * not sure they know enough to do this well.
2701 static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2704 * 4M is ample for all ops with the possible exception of thin
2705 * device deletion which is harmless if it fails (just retry the
2706 * delete after you've grown the device).
2708 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2709 return min((dm_block_t)1024ULL /* 4M */, quarter);
2713 * thin-pool <metadata dev> <data dev>
2714 * <data block size (sectors)>
2715 * <low water mark (blocks)>
2716 * [<#feature args> [<arg>]*]
2718 * Optional feature arguments are:
2719 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
2720 * ignore_discard: disable discard
2721 * no_discard_passdown: don't pass discards down to the data device
2722 * read_only: Don't allow any changes to be made to the pool metadata.
2723 * error_if_no_space: error IOs, instead of queueing, if no space.
2725 static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2727 int r, pool_created = 0;
2730 struct pool_features pf;
2731 struct dm_arg_set as;
2732 struct dm_dev *data_dev;
2733 unsigned long block_size;
2734 dm_block_t low_water_blocks;
2735 struct dm_dev *metadata_dev;
2736 fmode_t metadata_mode;
2739 * FIXME Remove validation from scope of lock.
2741 mutex_lock(&dm_thin_pool_table.mutex);
2744 ti->error = "Invalid argument count";
2753 * Set default pool features.
2755 pool_features_init(&pf);
2757 dm_consume_args(&as, 4);
2758 r = parse_pool_features(&as, &pf, ti);
2762 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2763 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
2765 ti->error = "Error opening metadata block device";
2768 warn_if_metadata_device_too_big(metadata_dev->bdev);
2770 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2772 ti->error = "Error getting data device";
2776 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2777 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2778 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2779 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
2780 ti->error = "Invalid block size";
2785 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2786 ti->error = "Invalid low water mark";
2791 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2797 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
2798 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
2805 * 'pool_created' reflects whether this is the first table load.
2806 * Top level discard support is not allowed to be changed after
2807 * initial load. This would require a pool reload to trigger thin
2810 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2811 ti->error = "Discard support cannot be disabled once enabled";
2813 goto out_flags_changed;
2818 pt->metadata_dev = metadata_dev;
2819 pt->data_dev = data_dev;
2820 pt->low_water_blocks = low_water_blocks;
2821 pt->adjusted_pf = pt->requested_pf = pf;
2822 ti->num_flush_bios = 1;
2825 * Only need to enable discards if the pool should pass
2826 * them down to the data device. The thin device's discard
2827 * processing will cause mappings to be removed from the btree.
2829 ti->discard_zeroes_data_unsupported = true;
2830 if (pf.discard_enabled && pf.discard_passdown) {
2831 ti->num_discard_bios = 1;
2834 * Setting 'discards_supported' circumvents the normal
2835 * stacking of discard limits (this keeps the pool and
2836 * thin devices' discard limits consistent).
2838 ti->discards_supported = true;
2842 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2843 calc_metadata_threshold(pt),
2844 metadata_low_callback,
2849 pt->callbacks.congested_fn = pool_is_congested;
2850 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2852 mutex_unlock(&dm_thin_pool_table.mutex);
2861 dm_put_device(ti, data_dev);
2863 dm_put_device(ti, metadata_dev);
2865 mutex_unlock(&dm_thin_pool_table.mutex);
2870 static int pool_map(struct dm_target *ti, struct bio *bio)
2873 struct pool_c *pt = ti->private;
2874 struct pool *pool = pt->pool;
2875 unsigned long flags;
2878 * As this is a singleton target, ti->begin is always zero.
2880 spin_lock_irqsave(&pool->lock, flags);
2881 bio->bi_bdev = pt->data_dev->bdev;
2882 r = DM_MAPIO_REMAPPED;
2883 spin_unlock_irqrestore(&pool->lock, flags);
2888 static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
2891 struct pool_c *pt = ti->private;
2892 struct pool *pool = pt->pool;
2893 sector_t data_size = ti->len;
2894 dm_block_t sb_data_size;
2896 *need_commit = false;
2898 (void) sector_div(data_size, pool->sectors_per_block);
2900 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2902 DMERR("%s: failed to retrieve data device size",
2903 dm_device_name(pool->pool_md));
2907 if (data_size < sb_data_size) {
2908 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2909 dm_device_name(pool->pool_md),
2910 (unsigned long long)data_size, sb_data_size);
2913 } else if (data_size > sb_data_size) {
2914 if (dm_pool_metadata_needs_check(pool->pmd)) {
2915 DMERR("%s: unable to grow the data device until repaired.",
2916 dm_device_name(pool->pool_md));
2921 DMINFO("%s: growing the data device from %llu to %llu blocks",
2922 dm_device_name(pool->pool_md),
2923 sb_data_size, (unsigned long long)data_size);
2924 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2926 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
2930 *need_commit = true;
2936 static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
2939 struct pool_c *pt = ti->private;
2940 struct pool *pool = pt->pool;
2941 dm_block_t metadata_dev_size, sb_metadata_dev_size;
2943 *need_commit = false;
2945 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
2947 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
2949 DMERR("%s: failed to retrieve metadata device size",
2950 dm_device_name(pool->pool_md));
2954 if (metadata_dev_size < sb_metadata_dev_size) {
2955 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2956 dm_device_name(pool->pool_md),
2957 metadata_dev_size, sb_metadata_dev_size);
2960 } else if (metadata_dev_size > sb_metadata_dev_size) {
2961 if (dm_pool_metadata_needs_check(pool->pmd)) {
2962 DMERR("%s: unable to grow the metadata device until repaired.",
2963 dm_device_name(pool->pool_md));
2967 warn_if_metadata_device_too_big(pool->md_dev);
2968 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2969 dm_device_name(pool->pool_md),
2970 sb_metadata_dev_size, metadata_dev_size);
2971 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
2973 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
2977 *need_commit = true;
2984 * Retrieves the number of blocks of the data device from
2985 * the superblock and compares it to the actual device size,
2986 * thus resizing the data device in case it has grown.
2988 * This both copes with opening preallocated data devices in the ctr
2989 * being followed by a resume
2991 * calling the resume method individually after userspace has
2992 * grown the data device in reaction to a table event.
2994 static int pool_preresume(struct dm_target *ti)
2997 bool need_commit1, need_commit2;
2998 struct pool_c *pt = ti->private;
2999 struct pool *pool = pt->pool;
3002 * Take control of the pool object.
3004 r = bind_control_target(pool, ti);
3008 r = maybe_resize_data_dev(ti, &need_commit1);
3012 r = maybe_resize_metadata_dev(ti, &need_commit2);
3016 if (need_commit1 || need_commit2)
3017 (void) commit(pool);
3022 static void pool_resume(struct dm_target *ti)
3024 struct pool_c *pt = ti->private;
3025 struct pool *pool = pt->pool;
3026 unsigned long flags;
3028 spin_lock_irqsave(&pool->lock, flags);
3029 pool->low_water_triggered = false;
3030 spin_unlock_irqrestore(&pool->lock, flags);
3033 do_waker(&pool->waker.work);
3036 static void pool_postsuspend(struct dm_target *ti)
3038 struct pool_c *pt = ti->private;
3039 struct pool *pool = pt->pool;
3041 cancel_delayed_work(&pool->waker);
3042 cancel_delayed_work(&pool->no_space_timeout);
3043 flush_workqueue(pool->wq);
3044 (void) commit(pool);
3047 static int check_arg_count(unsigned argc, unsigned args_required)
3049 if (argc != args_required) {
3050 DMWARN("Message received with %u arguments instead of %u.",
3051 argc, args_required);
3058 static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3060 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3061 *dev_id <= MAX_DEV_ID)
3065 DMWARN("Message received with invalid device id: %s", arg);
3070 static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3075 r = check_arg_count(argc, 2);
3079 r = read_dev_id(argv[1], &dev_id, 1);
3083 r = dm_pool_create_thin(pool->pmd, dev_id);
3085 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3093 static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3096 dm_thin_id origin_dev_id;
3099 r = check_arg_count(argc, 3);
3103 r = read_dev_id(argv[1], &dev_id, 1);
3107 r = read_dev_id(argv[2], &origin_dev_id, 1);
3111 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3113 DMWARN("Creation of new snapshot %s of device %s failed.",
3121 static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3126 r = check_arg_count(argc, 2);
3130 r = read_dev_id(argv[1], &dev_id, 1);
3134 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3136 DMWARN("Deletion of thin device %s failed.", argv[1]);
3141 static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3143 dm_thin_id old_id, new_id;
3146 r = check_arg_count(argc, 3);
3150 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3151 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3155 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3156 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3160 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3162 DMWARN("Failed to change transaction id from %s to %s.",
3170 static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3174 r = check_arg_count(argc, 1);
3178 (void) commit(pool);
3180 r = dm_pool_reserve_metadata_snap(pool->pmd);
3182 DMWARN("reserve_metadata_snap message failed.");
3187 static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3191 r = check_arg_count(argc, 1);
3195 r = dm_pool_release_metadata_snap(pool->pmd);
3197 DMWARN("release_metadata_snap message failed.");
3203 * Messages supported:
3204 * create_thin <dev_id>
3205 * create_snap <dev_id> <origin_id>
3207 * trim <dev_id> <new_size_in_sectors>
3208 * set_transaction_id <current_trans_id> <new_trans_id>
3209 * reserve_metadata_snap
3210 * release_metadata_snap
3212 static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3215 struct pool_c *pt = ti->private;
3216 struct pool *pool = pt->pool;
3218 if (!strcasecmp(argv[0], "create_thin"))
3219 r = process_create_thin_mesg(argc, argv, pool);
3221 else if (!strcasecmp(argv[0], "create_snap"))
3222 r = process_create_snap_mesg(argc, argv, pool);
3224 else if (!strcasecmp(argv[0], "delete"))
3225 r = process_delete_mesg(argc, argv, pool);
3227 else if (!strcasecmp(argv[0], "set_transaction_id"))
3228 r = process_set_transaction_id_mesg(argc, argv, pool);
3230 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3231 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3233 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3234 r = process_release_metadata_snap_mesg(argc, argv, pool);
3237 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3240 (void) commit(pool);
3245 static void emit_flags(struct pool_features *pf, char *result,
3246 unsigned sz, unsigned maxlen)
3248 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
3249 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3250 pf->error_if_no_space;
3251 DMEMIT("%u ", count);
3253 if (!pf->zero_new_blocks)
3254 DMEMIT("skip_block_zeroing ");
3256 if (!pf->discard_enabled)
3257 DMEMIT("ignore_discard ");
3259 if (!pf->discard_passdown)
3260 DMEMIT("no_discard_passdown ");
3262 if (pf->mode == PM_READ_ONLY)
3263 DMEMIT("read_only ");
3265 if (pf->error_if_no_space)
3266 DMEMIT("error_if_no_space ");
3271 * <transaction id> <used metadata sectors>/<total metadata sectors>
3272 * <used data sectors>/<total data sectors> <held metadata root>
3274 static void pool_status(struct dm_target *ti, status_type_t type,
3275 unsigned status_flags, char *result, unsigned maxlen)
3279 uint64_t transaction_id;
3280 dm_block_t nr_free_blocks_data;
3281 dm_block_t nr_free_blocks_metadata;
3282 dm_block_t nr_blocks_data;
3283 dm_block_t nr_blocks_metadata;
3284 dm_block_t held_root;
3285 char buf[BDEVNAME_SIZE];
3286 char buf2[BDEVNAME_SIZE];
3287 struct pool_c *pt = ti->private;
3288 struct pool *pool = pt->pool;
3291 case STATUSTYPE_INFO:
3292 if (get_pool_mode(pool) == PM_FAIL) {
3297 /* Commit to ensure statistics aren't out-of-date */
3298 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3299 (void) commit(pool);
3301 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3303 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3304 dm_device_name(pool->pool_md), r);
3308 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3310 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3311 dm_device_name(pool->pool_md), r);
3315 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
3317 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3318 dm_device_name(pool->pool_md), r);
3322 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3324 DMERR("%s: dm_pool_get_free_block_count returned %d",
3325 dm_device_name(pool->pool_md), r);
3329 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
3331 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3332 dm_device_name(pool->pool_md), r);
3336 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
3338 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3339 dm_device_name(pool->pool_md), r);
3343 DMEMIT("%llu %llu/%llu %llu/%llu ",
3344 (unsigned long long)transaction_id,
3345 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3346 (unsigned long long)nr_blocks_metadata,
3347 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3348 (unsigned long long)nr_blocks_data);
3351 DMEMIT("%llu ", held_root);
3355 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3356 DMEMIT("out_of_data_space ");
3357 else if (pool->pf.mode == PM_READ_ONLY)
3362 if (!pool->pf.discard_enabled)
3363 DMEMIT("ignore_discard ");
3364 else if (pool->pf.discard_passdown)
3365 DMEMIT("discard_passdown ");
3367 DMEMIT("no_discard_passdown ");
3369 if (pool->pf.error_if_no_space)
3370 DMEMIT("error_if_no_space ");
3372 DMEMIT("queue_if_no_space ");
3376 case STATUSTYPE_TABLE:
3377 DMEMIT("%s %s %lu %llu ",
3378 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3379 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3380 (unsigned long)pool->sectors_per_block,
3381 (unsigned long long)pt->low_water_blocks);
3382 emit_flags(&pt->requested_pf, result, sz, maxlen);
3391 static int pool_iterate_devices(struct dm_target *ti,
3392 iterate_devices_callout_fn fn, void *data)
3394 struct pool_c *pt = ti->private;
3396 return fn(ti, pt->data_dev, 0, ti->len, data);
3399 static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3400 struct bio_vec *biovec, int max_size)
3402 struct pool_c *pt = ti->private;
3403 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3405 if (!q->merge_bvec_fn)
3408 bvm->bi_bdev = pt->data_dev->bdev;
3410 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3413 static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
3415 struct pool *pool = pt->pool;
3416 struct queue_limits *data_limits;
3418 limits->max_discard_sectors = pool->sectors_per_block;
3421 * discard_granularity is just a hint, and not enforced.
3423 if (pt->adjusted_pf.discard_passdown) {
3424 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
3425 limits->discard_granularity = max(data_limits->discard_granularity,
3426 pool->sectors_per_block << SECTOR_SHIFT);
3428 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
3431 static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3433 struct pool_c *pt = ti->private;
3434 struct pool *pool = pt->pool;
3435 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3438 * Adjust max_sectors_kb to highest possible power-of-2
3439 * factor of pool->sectors_per_block.
3441 if (limits->max_hw_sectors & (limits->max_hw_sectors - 1))
3442 limits->max_sectors = rounddown_pow_of_two(limits->max_hw_sectors);
3444 limits->max_sectors = limits->max_hw_sectors;
3446 if (limits->max_sectors < pool->sectors_per_block) {
3447 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3448 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3449 limits->max_sectors--;
3450 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3452 } else if (block_size_is_power_of_two(pool)) {
3453 /* max_sectors_kb is >= power-of-2 thinp blocksize */
3454 while (!is_factor(limits->max_sectors, pool->sectors_per_block)) {
3455 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3456 limits->max_sectors--;
3457 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3462 * If the system-determined stacked limits are compatible with the
3463 * pool's blocksize (io_opt is a factor) do not override them.
3465 if (io_opt_sectors < pool->sectors_per_block ||
3466 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3467 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3468 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3470 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
3471 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3475 * pt->adjusted_pf is a staging area for the actual features to use.
3476 * They get transferred to the live pool in bind_control_target()
3477 * called from pool_preresume().
3479 if (!pt->adjusted_pf.discard_enabled) {
3481 * Must explicitly disallow stacking discard limits otherwise the
3482 * block layer will stack them if pool's data device has support.
3483 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3484 * user to see that, so make sure to set all discard limits to 0.
3486 limits->discard_granularity = 0;
3490 disable_passdown_if_not_supported(pt);
3492 set_discard_limits(pt, limits);
3495 static struct target_type pool_target = {
3496 .name = "thin-pool",
3497 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3498 DM_TARGET_IMMUTABLE,
3499 .version = {1, 14, 0},
3500 .module = THIS_MODULE,
3504 .postsuspend = pool_postsuspend,
3505 .preresume = pool_preresume,
3506 .resume = pool_resume,
3507 .message = pool_message,
3508 .status = pool_status,
3509 .merge = pool_merge,
3510 .iterate_devices = pool_iterate_devices,
3511 .io_hints = pool_io_hints,
3514 /*----------------------------------------------------------------
3515 * Thin target methods
3516 *--------------------------------------------------------------*/
3517 static void thin_get(struct thin_c *tc)
3519 atomic_inc(&tc->refcount);
3522 static void thin_put(struct thin_c *tc)
3524 if (atomic_dec_and_test(&tc->refcount))
3525 complete(&tc->can_destroy);
3528 static void thin_dtr(struct dm_target *ti)
3530 struct thin_c *tc = ti->private;
3531 unsigned long flags;
3534 wait_for_completion(&tc->can_destroy);
3536 spin_lock_irqsave(&tc->pool->lock, flags);
3537 list_del_rcu(&tc->list);
3538 spin_unlock_irqrestore(&tc->pool->lock, flags);
3541 mutex_lock(&dm_thin_pool_table.mutex);
3543 __pool_dec(tc->pool);
3544 dm_pool_close_thin_device(tc->td);
3545 dm_put_device(ti, tc->pool_dev);
3547 dm_put_device(ti, tc->origin_dev);
3550 mutex_unlock(&dm_thin_pool_table.mutex);
3554 * Thin target parameters:
3556 * <pool_dev> <dev_id> [origin_dev]
3558 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3559 * dev_id: the internal device identifier
3560 * origin_dev: a device external to the pool that should act as the origin
3562 * If the pool device has discards disabled, they get disabled for the thin
3565 static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3569 struct dm_dev *pool_dev, *origin_dev;
3570 struct mapped_device *pool_md;
3571 unsigned long flags;
3573 mutex_lock(&dm_thin_pool_table.mutex);
3575 if (argc != 2 && argc != 3) {
3576 ti->error = "Invalid argument count";
3581 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3583 ti->error = "Out of memory";
3587 spin_lock_init(&tc->lock);
3588 INIT_LIST_HEAD(&tc->deferred_cells);
3589 bio_list_init(&tc->deferred_bio_list);
3590 bio_list_init(&tc->retry_on_resume_list);
3591 tc->sort_bio_list = RB_ROOT;
3594 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3596 ti->error = "Error opening origin device";
3597 goto bad_origin_dev;
3599 tc->origin_dev = origin_dev;
3602 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3604 ti->error = "Error opening pool device";
3607 tc->pool_dev = pool_dev;
3609 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3610 ti->error = "Invalid device id";
3615 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3617 ti->error = "Couldn't get pool mapped device";
3622 tc->pool = __pool_table_lookup(pool_md);
3624 ti->error = "Couldn't find pool object";
3626 goto bad_pool_lookup;
3628 __pool_inc(tc->pool);
3630 if (get_pool_mode(tc->pool) == PM_FAIL) {
3631 ti->error = "Couldn't open thin device, Pool is in fail mode";
3636 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3638 ti->error = "Couldn't open thin internal device";
3642 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3644 goto bad_target_max_io_len;
3646 ti->num_flush_bios = 1;
3647 ti->flush_supported = true;
3648 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
3650 /* In case the pool supports discards, pass them on. */
3651 ti->discard_zeroes_data_unsupported = true;
3652 if (tc->pool->pf.discard_enabled) {
3653 ti->discards_supported = true;
3654 ti->num_discard_bios = 1;
3655 /* Discard bios must be split on a block boundary */
3656 ti->split_discard_bios = true;
3661 mutex_unlock(&dm_thin_pool_table.mutex);
3663 atomic_set(&tc->refcount, 1);
3664 init_completion(&tc->can_destroy);
3666 spin_lock_irqsave(&tc->pool->lock, flags);
3667 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
3668 spin_unlock_irqrestore(&tc->pool->lock, flags);
3670 * This synchronize_rcu() call is needed here otherwise we risk a
3671 * wake_worker() call finding no bios to process (because the newly
3672 * added tc isn't yet visible). So this reduces latency since we
3673 * aren't then dependent on the periodic commit to wake_worker().
3679 bad_target_max_io_len:
3680 dm_pool_close_thin_device(tc->td);
3682 __pool_dec(tc->pool);
3686 dm_put_device(ti, tc->pool_dev);
3689 dm_put_device(ti, tc->origin_dev);
3693 mutex_unlock(&dm_thin_pool_table.mutex);
3698 static int thin_map(struct dm_target *ti, struct bio *bio)
3700 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
3702 return thin_bio_map(ti, bio);
3705 static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
3707 unsigned long flags;
3708 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
3709 struct list_head work;
3710 struct dm_thin_new_mapping *m, *tmp;
3711 struct pool *pool = h->tc->pool;
3713 if (h->shared_read_entry) {
3714 INIT_LIST_HEAD(&work);
3715 dm_deferred_entry_dec(h->shared_read_entry, &work);
3717 spin_lock_irqsave(&pool->lock, flags);
3718 list_for_each_entry_safe(m, tmp, &work, list) {
3720 __complete_mapping_preparation(m);
3722 spin_unlock_irqrestore(&pool->lock, flags);
3725 if (h->all_io_entry) {
3726 INIT_LIST_HEAD(&work);
3727 dm_deferred_entry_dec(h->all_io_entry, &work);
3728 if (!list_empty(&work)) {
3729 spin_lock_irqsave(&pool->lock, flags);
3730 list_for_each_entry_safe(m, tmp, &work, list)
3731 list_add_tail(&m->list, &pool->prepared_discards);
3732 spin_unlock_irqrestore(&pool->lock, flags);
3740 static void thin_presuspend(struct dm_target *ti)
3742 struct thin_c *tc = ti->private;
3744 if (dm_noflush_suspending(ti))
3745 noflush_work(tc, do_noflush_start);
3748 static void thin_postsuspend(struct dm_target *ti)
3750 struct thin_c *tc = ti->private;
3753 * The dm_noflush_suspending flag has been cleared by now, so
3754 * unfortunately we must always run this.
3756 noflush_work(tc, do_noflush_stop);
3759 static int thin_preresume(struct dm_target *ti)
3761 struct thin_c *tc = ti->private;
3764 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
3770 * <nr mapped sectors> <highest mapped sector>
3772 static void thin_status(struct dm_target *ti, status_type_t type,
3773 unsigned status_flags, char *result, unsigned maxlen)
3777 dm_block_t mapped, highest;
3778 char buf[BDEVNAME_SIZE];
3779 struct thin_c *tc = ti->private;
3781 if (get_pool_mode(tc->pool) == PM_FAIL) {
3790 case STATUSTYPE_INFO:
3791 r = dm_thin_get_mapped_count(tc->td, &mapped);
3793 DMERR("dm_thin_get_mapped_count returned %d", r);
3797 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
3799 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3803 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3805 DMEMIT("%llu", ((highest + 1) *
3806 tc->pool->sectors_per_block) - 1);
3811 case STATUSTYPE_TABLE:
3813 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3814 (unsigned long) tc->dev_id);
3816 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
3827 static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3828 struct bio_vec *biovec, int max_size)
3830 struct thin_c *tc = ti->private;
3831 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
3833 if (!q->merge_bvec_fn)
3836 bvm->bi_bdev = tc->pool_dev->bdev;
3837 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
3839 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3842 static int thin_iterate_devices(struct dm_target *ti,
3843 iterate_devices_callout_fn fn, void *data)
3846 struct thin_c *tc = ti->private;
3847 struct pool *pool = tc->pool;
3850 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3851 * we follow a more convoluted path through to the pool's target.
3854 return 0; /* nothing is bound */
3856 blocks = pool->ti->len;
3857 (void) sector_div(blocks, pool->sectors_per_block);
3859 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
3864 static struct target_type thin_target = {
3866 .version = {1, 14, 0},
3867 .module = THIS_MODULE,
3871 .end_io = thin_endio,
3872 .preresume = thin_preresume,
3873 .presuspend = thin_presuspend,
3874 .postsuspend = thin_postsuspend,
3875 .status = thin_status,
3876 .merge = thin_merge,
3877 .iterate_devices = thin_iterate_devices,
3880 /*----------------------------------------------------------------*/
3882 static int __init dm_thin_init(void)
3888 r = dm_register_target(&thin_target);
3892 r = dm_register_target(&pool_target);
3894 goto bad_pool_target;
3898 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
3899 if (!_new_mapping_cache)
3900 goto bad_new_mapping_cache;
3904 bad_new_mapping_cache:
3905 dm_unregister_target(&pool_target);
3907 dm_unregister_target(&thin_target);
3912 static void dm_thin_exit(void)
3914 dm_unregister_target(&thin_target);
3915 dm_unregister_target(&pool_target);
3917 kmem_cache_destroy(_new_mapping_cache);
3920 module_init(dm_thin_init);
3921 module_exit(dm_thin_exit);
3923 module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
3924 MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
3926 MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
3927 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3928 MODULE_LICENSE("GPL");