Read / Write a device block driver from a custom application

I am trying to implement a "simple file system" for my own personal experience. For this, I created a block device driver with which I will perform read / write operations in a block of blocks. Now my question is how should I perform an open, read, write and close operation on a block device from a custom application.

What I am really looking for is a function with which I can open the / dev / sbd block device and if successful it will return struct block_device

. And for read / write functions, I can issue a block device request struct request

with parameters like "buffer, sectore_number, numbe_of_sectors".

So far I only got the functions block_read()

and block_write()

. But they seem to be BSD specific. And I am using Debain.

Does anyone have any idea about this? Thank.

+3


source to share


1 answer


I was doing something similar by recording an application-level filesystem that works with files or devices. What you are writing is not actually a device driver, as device drivers are directly handled / used by the kernel. The user application does not have direct access to it. Regardless, I want to point you to the function calls open (2), read (2), write (2), close (2) (section of man page 2 for all of these). To use them, you need a header file unistd.h. You can set the read / write size as a multiple of your block size when calling read and write. But in the end, you are still going through the core.

EDIT: Upon further examination and comments, the device driver is indeed in the kernel. There is usually no direct connection between the driver and the application, as there are multiple layers of code in the kernel to abstract the device so it looks just like everything else to the application.



There are two ways to get around this. One is to set one or more system calls in the system call tree to expose the device driver read / write routines for the application. Another idea I have used is to use the ioctl (I / O Control) system call to accomplish this, but this call is for controlling the actual device. For example, the hard drive uses read and write commands to transfer data, but to talk to the hard drive to get information about it, such as what the last LBA is or get its ID, you have to use IOCTL for that.

Hope it helps.

+2


source







All Articles