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 arg passed by the system call is a pointer to a long value 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 long item using the pointer passed to ioctl in arg.

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 in blk.h.

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.