Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


16.5.1 Data Structures


Fortunately, the data structures for both types of cache — the independent buffer cache and the elements
used to support the page cache — are identical, and this greatly simplifies implementation. The principal
elements of the buffer cache are the buffer heads, whose basic characteristics are discussed above. The
buffer head definition in the kernel sources is as follows:

<buffer_head.h>
struct buffer_head {
unsigned long b_state; /* buffer state bitmap (see above) */
struct buffer_head *b_this_page;/* circular list of page’s buffers */
struct page *b_page; /* the page this bh is mapped to */

sector_t b_blocknr; /* start block number */
size_t b_size; /* size of mapping */
char *b_data; /* pointer to data within the page */

struct block_device *b_bdev;
bh_end_io_t *b_end_io; /* I/O completion */
void *b_private; /* reserved for b_end_io */
...
atomic_t b_count; /* users using this buffer_head */
};

Buffers, like pages, can have many states. The current state of a buffer head is held in theb_stateele-
ment that accepts the following selection of values (the full list of values is available as anenumcalled
bh_state_bitsininclude/linux/buffer_heads.h):

❑ The state isBH_Uptodateif the current data in the buffer match the data in the backing store.
❑ Buffers are labeled asBH_Dirtyif their data have been modified and no longer match the data in
the backing store.
❑ BH_Lockindicates that the buffer is locked for further access. Buffers are explicitly locked dur-
ing I/O operations to prevent several threads from handling the buffers concurrently and thus
interfering with each other.
❑ BH_Mappedmeans that there is a mapping of the buffer contents on a secondary storage device, as
is the case with all buffers that originate from filesystems or from direct accesses to block devices.
❑ BH_Newmarks newly created buffers as new.

b_stateis interpreted as a bitmap. Every possible constant stands for a position in
the bitmap. As a result, several values (BK_LockandBH_Mapped, e.g.) can be active at
the same time — as also at many other points in the kernel.

BH_UptodateandBH_Dirtycan also be activeat the same time, and this is often the
case. WhereasBH_Uptodateis set after a buffer has been filled with data from the
block device, the kernel usesBH_Dirtyto indicate that the data in memory have
been modified but not yet been written back. This may appear to be confusing but
must be remembered when considering the information below.
Free download pdf