Chapter 12. Loading Block Drivers

As outlined in Section 1.3, in Chapter 1, Unix device drivers are not limited to char drivers. This chapter introduces the second main class of device drivers—block drivers. A block-oriented device is one that transfers data only in blocks (for example, the floppy disk or the hard drive), where the hardware block is usually called a ``sector.'' The word ``block,'' on the other hand, will be used to denote a software concept: the driver often uses 1KB blocks even if the sector size is 512 bytes.

In this chapter, we’ll build a full-featured block driver called sbull, short for ``Simple Block Utility for Loading Localities.'' This driver is similar to scull in that it uses the computer’s memory as the hardware device. In other words, it’s a RAM-disk driver. sbull can be executed on any Linux computer (although I have been able to test it only on a limited set of platforms).

If you need to write a driver for a real block device, the information in Chapter 8, and Chapter 9, should be used to supplement this chapter.

Registering the Driver

Like a char driver, a block driver in the kernel is identified by a major number. The functions used to register and unregister the driver are:

int register_blkdev(unsigned int major, const char *name, 
                    struct file_operations *fops);
int unregister_blkdev(unsigned int major, const char *name);

The meaning of the arguments is the same as for char drivers, and dynamic assignment of the major number can be performed ...

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.