Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


If the buffer data are not up-to-date, the rest of the work is delegated to__bread_slow—inotherwords,
to the slow path, as the name indicates. Essentially, this submits a request to the block layer to physically
read the data, and waits for the operation to complete. The buffer — which is now guaranteed to be filled
and current — is then returned.

Use in the Filesystem


When is it necessary to read individual blocks? There are not too many points in the kernel where this
must be done, but these are nevertheless of great importance. Filesystems in particular make use of the
routines described above when reading superblocks or management blocks.

The kernel defines two functions to simplify the work of filesystems with individual blocks:

<buffer_head.h>
static inline struct buffer_head *
sb_bread(struct super_block *sb, sector_t block)
{
return __bread(sb->s_bdev, block, sb->s_blocksize);
}

static inline struct buffer_head *
sb_getblk(struct super_block *sb, sector_t block)
{
return __getblk(sb->s_bdev, block, sb->s_blocksize);
}

As the code shows, the routines are used to read specific filesystem blocks found using a superblock, a
block number, and a block size.

16.6 Summary


Reading data from external storage devices like hard disks is much slower than reading data from RAM,
so Linux uses caching mechanisms to keep data in RAM once they have been read in, and accesses them
from there. Page frames are the natural units on which the page cache operates, and I have discussed in
this chapter how the kernel keeps track of which portions of a block device are cached in RAM. You have
been introduced to the concept of address spaces which allow for linking cached data with their source,
and how address spaces are manipulated and queried. Following that, I have examined the algorithms
employed by Linux to handle the technical details of bringing content into the page cache.

Traditionally,Unixcaches used smaller units than complete pages, and this technique survived until
today in the form of the buffer cache. While the main caching load is handled by the page cache, there
are still some users of the buffer cache, and you have therefore also been introduced to the corresponding
mechanisms.

Using RAM to cache data read from a disk is one aspect of the interaction between RAM and disks, but
there’s also another side to the story: The kernel must also take care of synchronizing modified data in
RAM back to the persistent storage on disk; the next chapter will introduce you to the corresponding
mechanisms.
Free download pdf