Sectors Blocks and Buffers
Each data transfer operation for a block device acts on a group of adjacent bytes called a sector. In most disk devices, the size of a sector is 512 bytes, although there are devices that use larger sectors (1,024 and 2,048 bytes). Notice that the sector should be considered the basic unit of data transfer; it is never possible to transfer less than a sector, although most disk devices are capable of transferring several adjacent sectors at once.
The kernel stores the sector size of each hardware block device in a table named hardsect_size. Each element in the table is indexed by the major number and the minor number of the corresponding block device file. Thus, hardsect_size represents the sector size of /dev/hda2, which is the second primary partition of the first IDE disk (see Table 13-2). If hardsect_size[maj] is null, all block devices sharing the major number maj have a standard sector size of 512 bytes.
Block device drivers transfer a large number of adjacent bytes called a block in a single operation. A block should not be confused with a sector. The sector is the basic unit of data transfer for the hardware device, while the block is simply a group of adjacent bytes involved in an I/O operation requested by a device driver.
In Linux, the block size must be a power of 2 and cannot be larger than a page frame. Moreover, it must be a multiple of the sector size, since each block must include an integral number of sectors. Therefore, on PC architecture, the permitted block sizes are 512, 1,024, 2,048, and 4,096 bytes. The same block device driver may operate with several block sizes, since it has to handle a set of device files sharing the same major number, while each block device file has its own predefined block size. For instance, a block device driver could handle a hard disk with two partitions containing an Ext2 filesystem and a swap area (see Chapter 16 and Chapter 17). In this case, the device driver uses two different block sizes: 1,024 bytes for the Ext2 partition and 4,096 bytes for the swap partition.
The kernel stores the block size in a table named blksize_size; each element in the table is indexed by the major number and the minor number of the corresponding block device file. If blksize_size[maj] is null, all block devices sharing the major number maj have a standard block size of 1,024 bytes. (You should not confuse blk_size with the blksize_size array, which stores the block size of the block devices rather than the size of the block device themselves.)
Each block requires its own buffer, which is a RAM memory area used by the kernel to store the block's content. When a device driver reads a block from disk, it fills the corresponding buffer with the values obtained from the hardware device; similarly, when a device driver writes a block on disk, it updates the corresponding group of adjacent bytes on the hardware device with the actual values of the associated buffer. The size of a buffer always matches the size of the corresponding block.
Continue reading here: Buffer Heads
Was this article helpful?