The ioctl Method
Like char devices, block devices can be acted on by using the ioctl system call. The only relevant difference between the two implementations is that block drivers share a number of common ioctl commands that most drivers are expected to support.
The commands that block drivers usually handle are
the following, declared in <linux/fs.h>:
-
BLKGETSIZE Retrieve the size of the current device, expressed as the number of sectors. The value of
argpassed by the system call is a pointer to alongvalue and should be used to copy the size to a user-space variable. This ioctl command is used, for instance, by mkfs to know the size of the filesystem being created.-
BLKFLSBUF Literally, ``flush buffers.'' The implementation of this command is the same for every device and is shown later with the sample code for the whole ioctl method.
-
BLKRAGET Used to get the current read-ahead value for the device. The current value should be written to user space as a
longitem using the pointer passed to ioctl inarg.-
BLKRASET Set the read-ahead value. The user process passes the new value in
arg.-
BLKRRPART Reread the partition table. This command is meaningful only for partitionable devices, introduced later in Section 12.7.
-
BLKROSET,BLKROGET These commands are used to change and check the read-only flag for the device. They are implemented by the macro
RO_IOCTLS(kdev_t dev, unsigned long where)because the code is device-independent. The macro is defined inblk.h.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access