aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-thin.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/md/dm-thin.c')
-rw-r--r--drivers/md/dm-thin.c105
1 files changed, 69 insertions, 36 deletions
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index ce59824fb414..93e3e542cff9 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -19,7 +19,7 @@
19/* 19/*
20 * Tunable constants 20 * Tunable constants
21 */ 21 */
22#define ENDIO_HOOK_POOL_SIZE 10240 22#define ENDIO_HOOK_POOL_SIZE 1024
23#define DEFERRED_SET_SIZE 64 23#define DEFERRED_SET_SIZE 64
24#define MAPPING_POOL_SIZE 1024 24#define MAPPING_POOL_SIZE 1024
25#define PRISON_CELLS 1024 25#define PRISON_CELLS 1024
@@ -510,10 +510,9 @@ struct pool {
510 struct block_device *md_dev; 510 struct block_device *md_dev;
511 struct dm_pool_metadata *pmd; 511 struct dm_pool_metadata *pmd;
512 512
513 uint32_t sectors_per_block;
514 unsigned block_shift;
515 dm_block_t offset_mask;
516 dm_block_t low_water_blocks; 513 dm_block_t low_water_blocks;
514 uint32_t sectors_per_block;
515 int sectors_per_block_shift;
517 516
518 struct pool_features pf; 517 struct pool_features pf;
519 unsigned low_water_triggered:1; /* A dm event has been sent */ 518 unsigned low_water_triggered:1; /* A dm event has been sent */
@@ -526,8 +525,8 @@ struct pool {
526 struct work_struct worker; 525 struct work_struct worker;
527 struct delayed_work waker; 526 struct delayed_work waker;
528 527
529 unsigned ref_count;
530 unsigned long last_commit_jiffies; 528 unsigned long last_commit_jiffies;
529 unsigned ref_count;
531 530
532 spinlock_t lock; 531 spinlock_t lock;
533 struct bio_list deferred_bios; 532 struct bio_list deferred_bios;
@@ -679,16 +678,28 @@ static void requeue_io(struct thin_c *tc)
679 678
680static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio) 679static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
681{ 680{
682 return bio->bi_sector >> tc->pool->block_shift; 681 sector_t block_nr = bio->bi_sector;
682
683 if (tc->pool->sectors_per_block_shift < 0)
684 (void) sector_div(block_nr, tc->pool->sectors_per_block);
685 else
686 block_nr >>= tc->pool->sectors_per_block_shift;
687
688 return block_nr;
683} 689}
684 690
685static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block) 691static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
686{ 692{
687 struct pool *pool = tc->pool; 693 struct pool *pool = tc->pool;
694 sector_t bi_sector = bio->bi_sector;
688 695
689 bio->bi_bdev = tc->pool_dev->bdev; 696 bio->bi_bdev = tc->pool_dev->bdev;
690 bio->bi_sector = (block << pool->block_shift) + 697 if (tc->pool->sectors_per_block_shift < 0)
691 (bio->bi_sector & pool->offset_mask); 698 bio->bi_sector = (block * pool->sectors_per_block) +
699 sector_div(bi_sector, pool->sectors_per_block);
700 else
701 bio->bi_sector = (block << pool->sectors_per_block_shift) |
702 (bi_sector & (pool->sectors_per_block - 1));
692} 703}
693 704
694static void remap_to_origin(struct thin_c *tc, struct bio *bio) 705static void remap_to_origin(struct thin_c *tc, struct bio *bio)
@@ -933,9 +944,7 @@ static void process_prepared(struct pool *pool, struct list_head *head,
933 */ 944 */
934static int io_overlaps_block(struct pool *pool, struct bio *bio) 945static int io_overlaps_block(struct pool *pool, struct bio *bio)
935{ 946{
936 return !(bio->bi_sector & pool->offset_mask) && 947 return bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT);
937 (bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT));
938
939} 948}
940 949
941static int io_overwrites_block(struct pool *pool, struct bio *bio) 950static int io_overwrites_block(struct pool *pool, struct bio *bio)
@@ -1218,7 +1227,7 @@ static void process_discard(struct thin_c *tc, struct bio *bio)
1218 */ 1227 */
1219 m = get_next_mapping(pool); 1228 m = get_next_mapping(pool);
1220 m->tc = tc; 1229 m->tc = tc;
1221 m->pass_discard = (!lookup_result.shared) & pool->pf.discard_passdown; 1230 m->pass_discard = (!lookup_result.shared) && pool->pf.discard_passdown;
1222 m->virt_block = block; 1231 m->virt_block = block;
1223 m->data_block = lookup_result.block; 1232 m->data_block = lookup_result.block;
1224 m->cell = cell; 1233 m->cell = cell;
@@ -1234,18 +1243,16 @@ static void process_discard(struct thin_c *tc, struct bio *bio)
1234 } 1243 }
1235 } else { 1244 } else {
1236 /* 1245 /*
1237 * This path is hit if people are ignoring 1246 * The DM core makes sure that the discard doesn't span
1238 * limits->discard_granularity. It ignores any 1247 * a block boundary. So we submit the discard of a
1239 * part of the discard that is in a subsequent 1248 * partial block appropriately.
1240 * block.
1241 */ 1249 */
1242 sector_t offset = bio->bi_sector - (block << pool->block_shift);
1243 unsigned remaining = (pool->sectors_per_block - offset) << 9;
1244 bio->bi_size = min(bio->bi_size, remaining);
1245
1246 cell_release_singleton(cell, bio); 1250 cell_release_singleton(cell, bio);
1247 cell_release_singleton(cell2, bio); 1251 cell_release_singleton(cell2, bio);
1248 remap_and_issue(tc, bio, lookup_result.block); 1252 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1253 remap_and_issue(tc, bio, lookup_result.block);
1254 else
1255 bio_endio(bio, 0);
1249 } 1256 }
1250 break; 1257 break;
1251 1258
@@ -1719,8 +1726,10 @@ static struct pool *pool_create(struct mapped_device *pool_md,
1719 1726
1720 pool->pmd = pmd; 1727 pool->pmd = pmd;
1721 pool->sectors_per_block = block_size; 1728 pool->sectors_per_block = block_size;
1722 pool->block_shift = ffs(block_size) - 1; 1729 if (block_size & (block_size - 1))
1723 pool->offset_mask = block_size - 1; 1730 pool->sectors_per_block_shift = -1;
1731 else
1732 pool->sectors_per_block_shift = __ffs(block_size);
1724 pool->low_water_blocks = 0; 1733 pool->low_water_blocks = 0;
1725 pool_features_init(&pool->pf); 1734 pool_features_init(&pool->pf);
1726 pool->prison = prison_create(PRISON_CELLS); 1735 pool->prison = prison_create(PRISON_CELLS);
@@ -1825,15 +1834,19 @@ static struct pool *__pool_find(struct mapped_device *pool_md,
1825 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev); 1834 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1826 1835
1827 if (pool) { 1836 if (pool) {
1828 if (pool->pool_md != pool_md) 1837 if (pool->pool_md != pool_md) {
1838 *error = "metadata device already in use by a pool";
1829 return ERR_PTR(-EBUSY); 1839 return ERR_PTR(-EBUSY);
1840 }
1830 __pool_inc(pool); 1841 __pool_inc(pool);
1831 1842
1832 } else { 1843 } else {
1833 pool = __pool_table_lookup(pool_md); 1844 pool = __pool_table_lookup(pool_md);
1834 if (pool) { 1845 if (pool) {
1835 if (pool->md_dev != metadata_dev) 1846 if (pool->md_dev != metadata_dev) {
1847 *error = "different pool cannot replace a pool";
1836 return ERR_PTR(-EINVAL); 1848 return ERR_PTR(-EINVAL);
1849 }
1837 __pool_inc(pool); 1850 __pool_inc(pool);
1838 1851
1839 } else { 1852 } else {
@@ -1964,7 +1977,7 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1964 if (kstrtoul(argv[2], 10, &block_size) || !block_size || 1977 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
1965 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || 1978 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1966 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || 1979 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1967 !is_power_of_2(block_size)) { 1980 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
1968 ti->error = "Invalid block size"; 1981 ti->error = "Invalid block size";
1969 r = -EINVAL; 1982 r = -EINVAL;
1970 goto out; 1983 goto out;
@@ -2011,6 +2024,15 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2011 goto out_flags_changed; 2024 goto out_flags_changed;
2012 } 2025 }
2013 2026
2027 /*
2028 * The block layer requires discard_granularity to be a power of 2.
2029 */
2030 if (pf.discard_enabled && !is_power_of_2(block_size)) {
2031 ti->error = "Discard support must be disabled when the block size is not a power of 2";
2032 r = -EINVAL;
2033 goto out_flags_changed;
2034 }
2035
2014 pt->pool = pool; 2036 pt->pool = pool;
2015 pt->ti = ti; 2037 pt->ti = ti;
2016 pt->metadata_dev = metadata_dev; 2038 pt->metadata_dev = metadata_dev;
@@ -2090,7 +2112,8 @@ static int pool_preresume(struct dm_target *ti)
2090 int r; 2112 int r;
2091 struct pool_c *pt = ti->private; 2113 struct pool_c *pt = ti->private;
2092 struct pool *pool = pt->pool; 2114 struct pool *pool = pt->pool;
2093 dm_block_t data_size, sb_data_size; 2115 sector_t data_size = ti->len;
2116 dm_block_t sb_data_size;
2094 2117
2095 /* 2118 /*
2096 * Take control of the pool object. 2119 * Take control of the pool object.
@@ -2099,7 +2122,8 @@ static int pool_preresume(struct dm_target *ti)
2099 if (r) 2122 if (r)
2100 return r; 2123 return r;
2101 2124
2102 data_size = ti->len >> pool->block_shift; 2125 (void) sector_div(data_size, pool->sectors_per_block);
2126
2103 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size); 2127 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2104 if (r) { 2128 if (r) {
2105 DMERR("failed to retrieve data device size"); 2129 DMERR("failed to retrieve data device size");
@@ -2108,7 +2132,7 @@ static int pool_preresume(struct dm_target *ti)
2108 2132
2109 if (data_size < sb_data_size) { 2133 if (data_size < sb_data_size) {
2110 DMERR("pool target too small, is %llu blocks (expected %llu)", 2134 DMERR("pool target too small, is %llu blocks (expected %llu)",
2111 data_size, sb_data_size); 2135 (unsigned long long)data_size, sb_data_size);
2112 return -EINVAL; 2136 return -EINVAL;
2113 2137
2114 } else if (data_size > sb_data_size) { 2138 } else if (data_size > sb_data_size) {
@@ -2489,7 +2513,8 @@ static void set_discard_limits(struct pool *pool, struct queue_limits *limits)
2489 2513
2490 /* 2514 /*
2491 * This is just a hint, and not enforced. We have to cope with 2515 * This is just a hint, and not enforced. We have to cope with
2492 * bios that overlap 2 blocks. 2516 * bios that cover a block partially. A discard that spans a block
2517 * boundary is not sent to this target.
2493 */ 2518 */
2494 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT; 2519 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
2495 limits->discard_zeroes_data = pool->pf.zero_new_blocks; 2520 limits->discard_zeroes_data = pool->pf.zero_new_blocks;
@@ -2621,13 +2646,19 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2621 goto bad_thin_open; 2646 goto bad_thin_open;
2622 } 2647 }
2623 2648
2624 ti->split_io = tc->pool->sectors_per_block; 2649 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
2650 if (r)
2651 goto bad_thin_open;
2652
2625 ti->num_flush_requests = 1; 2653 ti->num_flush_requests = 1;
2626 2654
2627 /* In case the pool supports discards, pass them on. */ 2655 /* In case the pool supports discards, pass them on. */
2628 if (tc->pool->pf.discard_enabled) { 2656 if (tc->pool->pf.discard_enabled) {
2629 ti->discards_supported = 1; 2657 ti->discards_supported = 1;
2630 ti->num_discard_requests = 1; 2658 ti->num_discard_requests = 1;
2659 ti->discard_zeroes_data_unsupported = 1;
2660 /* Discard requests must be split on a block boundary */
2661 ti->split_discard_requests = 1;
2631 } 2662 }
2632 2663
2633 dm_put(pool_md); 2664 dm_put(pool_md);
@@ -2753,19 +2784,21 @@ static int thin_status(struct dm_target *ti, status_type_t type,
2753static int thin_iterate_devices(struct dm_target *ti, 2784static int thin_iterate_devices(struct dm_target *ti,
2754 iterate_devices_callout_fn fn, void *data) 2785 iterate_devices_callout_fn fn, void *data)
2755{ 2786{
2756 dm_block_t blocks; 2787 sector_t blocks;
2757 struct thin_c *tc = ti->private; 2788 struct thin_c *tc = ti->private;
2789 struct pool *pool = tc->pool;
2758 2790
2759 /* 2791 /*
2760 * We can't call dm_pool_get_data_dev_size() since that blocks. So 2792 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2761 * we follow a more convoluted path through to the pool's target. 2793 * we follow a more convoluted path through to the pool's target.
2762 */ 2794 */
2763 if (!tc->pool->ti) 2795 if (!pool->ti)
2764 return 0; /* nothing is bound */ 2796 return 0; /* nothing is bound */
2765 2797
2766 blocks = tc->pool->ti->len >> tc->pool->block_shift; 2798 blocks = pool->ti->len;
2799 (void) sector_div(blocks, pool->sectors_per_block);
2767 if (blocks) 2800 if (blocks)
2768 return fn(ti, tc->pool_dev, 0, tc->pool->sectors_per_block * blocks, data); 2801 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
2769 2802
2770 return 0; 2803 return 0;
2771} 2804}
@@ -2782,7 +2815,7 @@ static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
2782 2815
2783static struct target_type thin_target = { 2816static struct target_type thin_target = {
2784 .name = "thin", 2817 .name = "thin",
2785 .version = {1, 1, 0}, 2818 .version = {1, 2, 0},
2786 .module = THIS_MODULE, 2819 .module = THIS_MODULE,
2787 .ctr = thin_ctr, 2820 .ctr = thin_ctr,
2788 .dtr = thin_dtr, 2821 .dtr = thin_dtr,