Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read/Write block directly to disk in linux kernel

As the title has said, I'm looking for how I can read/write blocks directly to disk in linux kernel space (bypassing the file system) and directly interact with block IO layer.

After reading through some kernel codes, I realize bio is the structure I should be using to achieve such goal in block IO layer. But I don't quite understand the structures of bio and haven't figure out how exactly I can do that.

Any helps? Thank you

like image 363
James Avatar asked Oct 19 '25 09:10

James


2 Answers

If you're only doing something simple, you don't really need to mess with BIO. What you can do instead is to simply open the block device (/dev/whatever) as if it was a file. The kernel will do the right thing and will give you the "thin" wrapper for read/write operations.

In regard to opening the file from the kernel space, there are few questions here, already answered, like this one:

How to read/write files within a Linux kernel module?

If you want to do anything more fancy, you will have to study the sources of the FS drivers (in the fs/ subdirectory) to hunt for examples.

like image 98
oakad Avatar answered Oct 22 '25 00:10

oakad


In case anyone is interested in doing this with Node.js, we released a native add on yesterday with helpers for opening and working with block devices: https://github.com/ronomon/direct-io

like image 45
Joran Greef Avatar answered Oct 22 '25 00:10

Joran Greef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!