views:

108

answers:

1

How to access block level storage via the kernel (w/o using scsi libraries)?

My intent is to implement a block level storage protocol over network for learning purpose, almost the same way SCSI works. Requests will be generated by initiator and sent to target (both userspace program) which makes call to kernel module and returns the data using TCP protocol to initiator.

So far, I have managed to build a simple "Hello" module and run it (I am new at kernel programming), but unable to proceed with block access.

After searching a lot, I found struct buffer_head * bread(int dev,int block) in linux/fs.h, but the compiler throws error.

 error: implicit declaration of function ‘bread’

Please help, also feel free to advice on starting with kernel programming.
Thank you!


bread as used in old kernels.

Looking into struct request *blk_get_request(struct request_queue *, int, gfp_t); in linux/blkdev.h


Accessing the block device has to be accomplished via kernel.

A: 

Not a kernel developer, but a few comments:

  • The implicit declaration error means that the definition you've found somehow isn't in scope when you call the function. Maybe it's hidden in an #ifdef or maybe you forgot to include linux/fs.h somehow.

  • As far as advice on linux kernel programming, you might want to check out kernelnewbies.org.

  • There have been various books written on kernel programming, but be aware that the details in the kernel change very rapidly. Most of the concepts in the older books will still be valid, but at least some of the details in some areas will have changed.

  • Finally, you might have to brave the linux kernel mailing list. It's rather intimidating, I'm sorry to say, so try to have your questions well thought out before you post them.

  • A block level storage protocol is itself a fair bit of work. Perhaps you want to get the protocol in place in user space first, with the target doing direct access to, eg, /dev/sdc before diving into the kernel.

As I read your question more closely, it appears your main interest is in the storage protocol aspect of this project. If so, why do you need to modify the kernel. If you have a locally attached disk, say /dev/sdX on the target, then you can do something like this from user space:

fd = open("/dev/sdX", O_RDWR);
pwrite(fd, buf, len, offset);
pread(fd, buf, len, offset);

So, unless you're specifically interested in playing around inside the kernel, I don't think you need to do any kernel module to do a basic storage protocol between user processes.

Dale Hagglund
`bread` was actually used in older kernels (around 0.1), I have updated the question. (I have checked out both kernelnewbies.org and related sites and also reading Linux Device Drivers, but the problem is that the content is either very simple like writing a Hello world module or outdated). Can you please elaborate on the last point?
N 1.1
I actually just want to access block storage via kernel. Request will be generated in a user space program, which will delegate it to kernel and kernel will do all the work. How can I access say `/dev/sdc` in that manner?
N 1.1
You have two parts to your problem: (1) the user-space initiator and target programs and the protocol between them, and (b) how to access the storage device at the target. Depending on which area you want to learn more about, you could, for example, do (1) but just do regular i/o to `/dev/sdX` from user space via `open`, `read`, `write`, etc. Ie, it's possible to do block device i/o in user space. If your primary interest is kernel stuff, then start with (2), but set the initiator/target programs aside for a while. Does that help?
Dale Hagglund
No, I need to access device only via kernel.
N 1.1
Why do you need to access the device only via the kernel? What does that buy you? Is there something special about the device? Are you concerned about the performance? Of course, if this is a personal interest project (which it sounds like so far), then the answer "because I want to" is fine as well. Anyway, if you insist on getting at the device through the kernel, fine, but I'm afraid I don't know the linux kernel at a sufficient level of detail to be helpful.
Dale Hagglund