[2/2] dm-thin: Allow specifying an offset
Commit Message
This allows exposing only part of a thin volume without having to layer
dm-linear. One use-case is a hypervisor replacing a partition table.
Signed-off-by: Demi Marie Obenour <demi@invisiblethingslab.com>
---
drivers/md/dm-thin.c | 32 ++++++++++++++++++++++++++------
1 file changed, 26 insertions(+), 6 deletions(-)
Comments
On Tue, Feb 07, 2023 at 03:03:57PM +0000, Joe Thornber wrote:
> Nack. I'm not building a linear target into every other target. Layering
> targets is simple.
It also introduces a performance penalty, which is measurable on some
workloads. Even dm-linear is not free. The crypt target also has this
feature, so there is precedent.
@@ -357,6 +357,7 @@ struct thin_c {
*/
refcount_t refcount;
struct completion can_destroy;
+ u64 offset;
};
/*----------------------------------------------------------------*/
@@ -1180,9 +1181,9 @@ static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)
discard_parent = bio_alloc(NULL, 1, 0, GFP_NOIO);
discard_parent->bi_end_io = passdown_endio;
discard_parent->bi_private = m;
- if (m->maybe_shared)
- passdown_double_checking_shared_status(m, discard_parent);
- else {
+ if (m->maybe_shared)
+ passdown_double_checking_shared_status(m, discard_parent);
+ else {
struct discard_op op;
begin_discard(&op, tc, discard_parent);
@@ -4149,7 +4150,7 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
mutex_lock(&dm_thin_pool_table.mutex);
- if (argc != 2 && argc != 3) {
+ if (argc < 2 || argc > 4) {
ti->error = "Invalid argument count";
r = -EINVAL;
goto out_unlock;
@@ -4168,7 +4169,8 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
bio_list_init(&tc->retry_on_resume_list);
tc->sort_bio_list = RB_ROOT;
- if (argc == 3) {
+ /* Use "/" to indicate "no origin device" while providing an offset */
+ if (argc >= 3 && strcmp(argv[2], "/")) {
if (!strcmp(argv[0], argv[2])) {
ti->error = "Error setting origin device";
r = -EINVAL;
@@ -4196,6 +4198,23 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
goto bad_common;
}
+ tc->offset = 0;
+ if (argc > 3) {
+ sector_t sector_offset;
+
+ if (kstrtoull(argv[3], 10, &tc->offset)) {
+ ti->error = "Invalid offset";
+ r = -EINVAL;
+ goto bad_common;
+ }
+
+ if (check_add_overflow(tc->offset, ti->len, §or_offset)) {
+ ti->error = "Offset + len overflows sector_t";
+ r = -EINVAL;
+ goto bad_common;
+ }
+ }
+
pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
if (!pool_md) {
ti->error = "Couldn't get pool mapped device";
@@ -4285,8 +4304,9 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
static int thin_map(struct dm_target *ti, struct bio *bio)
{
- bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
+ struct thin_c *tc = ti->private;
+ bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector) + tc->offset;
return thin_bio_map(ti, bio);
}