Search code examples
linuxlinux-kernellinux-device-driver

How do I persist data to storage from a block driver in the Linux kernel?


I am trying to implement a block device driver in the Linux kernel (version 6.2). This is a university project, and I need to implement a redundancy scheme which will be added to another tool for use. The redundancy scheme is based on creating parity blocks based on actual data blocks (with the help of the XOR operation), and persisting everything in storage so that data corruption can be repaired. The scheme should work on 4096-byte blocks.

My question is this: How do I actually persist a block of memory to the storage device from my code? I found resources online, and some example code (like from the LDD3 book), but as far as I understood the implementations there are for a ram-disk, i.e. the memory that is intercepted in requests is not persisted to storage.

I started writing some code, and I have some baseline done for the block driver, but I am confused about the fact of how to persist data. As far as I understood, I should intercept read/write requests (from the request queue) which contain bios, do the work I need to do, and persist the blocks on the device. I found that calling submit_bio() does the trick, however doesn't this just send the request to the request_queue, which I then intercept again and again, thus creating a bad recursion?

I am probably missing something, as I am by no means an expert on the Linux kernel, so any help is greatly appreciated.


Solution

  • So I found out I was looking at the problem all wrong. For what I need, I do not need to create a whole new block device driver, but rather just a new device-mapper. The device-mapper will intercept bio requests and there I can do what I want regarding my redundancy implementation, and it just uses the block device implementation already in the Linux kernel source code.

    Big thanks to everyone who has taken the time to answer my question!