create device mapper target
Asked Answered
K

1

6

I am trying to implement device mapper target by referring to the already existing ones dm-linear, dm-snapshot, dm-cache etc. In my implementation, I need to perform a read/modify/write operation on a certain sector range. Since the device mapper directly talks to the block layer, I am not sure what data structures/functions to use to read the sectors in the memory, modify the buffer and write it back to another sector range. At the application level, we have syscalls and below we have vfs_read/vfs_write. Is there anything similar for device mapper layer? I have been stuck here for very long. Any help will be appreciated.

Kohinoor answered 13/6, 2014 at 21:53 Comment(0)
C
11

NOTE: My answer is related to kernel version < 3.14, because since 3.14 API is slightly changed.

In kernel you read/write certain sectors with struct bio. This struct is used for all block level I/O. Comprehensive documentation can be found in kernel and on lwn. These are the several most significant members of this structure:

  • bio->bi_sector - first sector of block I/O request
  • bio->bi_size - size of I/O request
  • bio->bi_bdev - device to read/write
  • bio->bi_end_io - callback that kernel will call on the end of request

What you do in device mapper target is map incoming bio. When you creating your device mapper target you supply at least 2 callbacks: ctr, and map. For example, the simplest device-mapper target dm-zero declares it's callbacks as following:

static struct target_type zero_target = {
         .name   = "zero",
         .version = {1, 1, 0},
         .module = THIS_MODULE,
         .ctr    = zero_ctr,
         .map    = zero_map,
};

map is a key callback - it's a heart of every device-mapper target. map receive incoming bio and it can do anything with it. For example, dm-linear just shift sector of every incoming bio by predefined offset. See the code:

static sector_t linear_map_sector(struct dm_target *ti, sector_t bi_sector)
{
        struct linear_c *lc = ti->private;

        return lc->start + dm_target_offset(ti, bi_sector);
}

static void linear_map_bio(struct dm_target *ti, struct bio *bio)
{
        struct linear_c *lc = ti->private;

        bio->bi_bdev = lc->dev->bdev;
        if (bio_sectors(bio))
                bio->bi_sector = linear_map_sector(ti, bio->bi_sector);
}

static int linear_map(struct dm_target *ti, struct bio *bio)
{
        linear_map_bio(ti, bio);

        return DM_MAPIO_REMAPPED;
}

Because map receives pointer to bio it can change value under that pointer and that's it.

That's how you map I/O requests. If you want to create your own requests then you must allocate bio, fill it's sector, device, size, end callback and add buffers to read into/write from. Basically, it's just a few steps:

  • Call to bio_alloc to allocate bio.
  • Set bio->bi_bdev, bio->bi_sector, bio->bi_size, bio->bi_end_io
  • Add pages via bio_add_page.
  • Call submit_bio.
  • Handle results and errors in bio->bi_end_io callback

Example can be found in dm-crypt target in crypt_alloc_buffer function.

Contempt answered 16/6, 2014 at 8:18 Comment(3)
Thanks @avd, This is really helpful. Do you think I can do the same rmw operation using dm-bufio.c. I want to read a range of sectors, modify some of those sectors and the write the data into a new range. it is similar to what we do in circular buffers.Kohinoor
It seems <linux/dm-bufio.h> does not exist while compilingKohinoor
dm-bufio is internal device-mapper header. It is not export via include/linux directory. The only way I can see to use it is create your own dm target in kernel tree - under drivers/md/dm-ours-target.c and compile it inside kernel. There you can include it like #include "dm-bufio.h".Contempt

© 2022 - 2024 — McMap. All rights reserved.