<div dir="ltr">Nack.  I'm not building a linear target into every other target.  Layering targets is simple.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 7, 2023 at 7:56 AM Demi Marie Obenour <<a href="mailto:demi@invisiblethingslab.com">demi@invisiblethingslab.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This allows exposing only part of a thin volume without having to layer<br>
dm-linear.  One use-case is a hypervisor replacing a partition table.<br>
<br>
Signed-off-by: Demi Marie Obenour <<a href="mailto:demi@invisiblethingslab.com" target="_blank">demi@invisiblethingslab.com</a>><br>
---<br>
 drivers/md/dm-thin.c | 32 ++++++++++++++++++++++++++------<br>
 1 file changed, 26 insertions(+), 6 deletions(-)<br>
<br>
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c<br>
index d85fdbd782ae5426003c99a4b4bf53818cc85efa..87f14933375b050a950a5f58e98c13b4d28f6af0 100644<br>
--- a/drivers/md/dm-thin.c<br>
+++ b/drivers/md/dm-thin.c<br>
@@ -357,6 +357,7 @@ struct thin_c {<br>
         */<br>
        refcount_t refcount;<br>
        struct completion can_destroy;<br>
+       u64 offset;<br>
 };<br>
<br>
 /*----------------------------------------------------------------*/<br>
@@ -1180,9 +1181,9 @@ static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)<br>
        discard_parent = bio_alloc(NULL, 1, 0, GFP_NOIO);<br>
        discard_parent->bi_end_io = passdown_endio;<br>
        discard_parent->bi_private = m;<br>
-       if (m->maybe_shared)<br>
-               passdown_double_checking_shared_status(m, discard_parent);<br>
-       else {<br>
+       if (m->maybe_shared)<br>
+               passdown_double_checking_shared_status(m, discard_parent);<br>
+       else {<br>
                struct discard_op op;<br>
<br>
                begin_discard(&op, tc, discard_parent);<br>
@@ -4149,7 +4150,7 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)<br>
<br>
        mutex_lock(&dm_thin_pool_table.mutex);<br>
<br>
-       if (argc != 2 && argc != 3) {<br>
+       if (argc < 2 || argc > 4) {<br>
                ti->error = "Invalid argument count";<br>
                r = -EINVAL;<br>
                goto out_unlock;<br>
@@ -4168,7 +4169,8 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)<br>
        bio_list_init(&tc->retry_on_resume_list);<br>
        tc->sort_bio_list = RB_ROOT;<br>
<br>
-       if (argc == 3) {<br>
+       /* Use "/" to indicate "no origin device" while providing an offset */<br>
+       if (argc >= 3 && strcmp(argv[2], "/")) {<br>
                if (!strcmp(argv[0], argv[2])) {<br>
                        ti->error = "Error setting origin device";<br>
                        r = -EINVAL;<br>
@@ -4196,6 +4198,23 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)<br>
                goto bad_common;<br>
        }<br>
<br>
+       tc->offset = 0;<br>
+       if (argc > 3) {<br>
+               sector_t sector_offset;<br>
+<br>
+               if (kstrtoull(argv[3], 10, &tc->offset)) {<br>
+                       ti->error = "Invalid offset";<br>
+                       r = -EINVAL;<br>
+                       goto bad_common;<br>
+               }<br>
+<br>
+               if (check_add_overflow(tc->offset, ti->len, &sector_offset)) {<br>
+                       ti->error = "Offset + len overflows sector_t";<br>
+                       r = -EINVAL;<br>
+                       goto bad_common;<br>
+               }<br>
+       }<br>
+<br>
        pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);<br>
        if (!pool_md) {<br>
                ti->error = "Couldn't get pool mapped device";<br>
@@ -4285,8 +4304,9 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)<br>
<br>
 static int thin_map(struct dm_target *ti, struct bio *bio)<br>
 {<br>
-       bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);<br>
+       struct thin_c *tc = ti->private;<br>
<br>
+       bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector) + tc->offset;<br>
        return thin_bio_map(ti, bio);<br>
 }<br>
<br>
-- <br>
Sincerely,<br>
Demi Marie Obenour (she/her/hers)<br>
Invisible Things Lab<br>
<br>
--<br>
dm-devel mailing list<br>
<a href="mailto:dm-devel@redhat.com" target="_blank">dm-devel@redhat.com</a><br>
<a href="https://listman.redhat.com/mailman/listinfo/dm-devel" rel="noreferrer" target="_blank">https://listman.redhat.com/mailman/listinfo/dm-devel</a><br>
<br>
</blockquote></div>