Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


Besides the above constants, a few additional values are defined inenum bh_state_bits.Theyare
ignored because they are either of little importance or are simply no longer used. They are retained
in the kernel sources for historical reasons and will disappear sooner or later.

The kernel defines theset_buffer_fooandget_buffer_foofunctions to set and read the buffer state
bits forBH_Foo.

Thebuffer_headstructure also includes further elements whose meanings are given below:

❑ b_countimplements the usual access counter to prevent the kernel from freeing buffer heads
that are still in active use.
❑ b_pageholds a pointer to apageinstance with which the buffer head is associated when used in
conjunction with the page cache. If the buffer is independent,b_pagecontains a null pointer.
❑ As discussed above, several buffers are used to split the contents of a page into smaller units. All
buffer heads belonging to these units are kept on a singly linked, circular list usingb_this_page
(the entry for the last buffer points to the entry for the first buffer to create a circular structure).
❑ b_blocknrholds the number of the block on the underlying block device, andb_sizespecifies
the size of the block.b_bdevis a pointer to theblock_deviceinstance of the block device. This
information uniquely identifies the source of the data.
❑ The pointer to the data in memory is held inb_data(the end position can be calculated from
b_size; there is therefore no need for an explicit pointer to this position, although a pointer was
used above for the sake of simplicity).
❑ b_end_iopoints to a routine that is automatically invoked by the kernel when an I/O operation
involving the buffer is completed (it is required by the BIO routines described in Chapter 6). This
enables the kernel to postpone further buffer handling until a desired input or output operation
has, in fact, been completed.
❑ b_privateis a pointer reserved for private use byb_end_io. It is used primarily by journaling
filesystems. It is usually set toNULLif it is not needed.

16.5.2 Operations


The kernel must provide a set of operations so that the rest of the code can easily and efficiently exploit
the functionality of buffers. This section describes the mechanisms for creating and managing new buffer
heads.

Caution: These mechanisms make no contribution tothe actual caching of data in memory, discussed in
later sections.

Before buffers can be used, the kernel must first create an instance of thebuffer_headstructure on which
the remaining functions act. As the new generation of new buffer heads is a frequently recurring task,
it should be performed as quickly as possible. This is a classical situation for the use of a slab cache as
described in Chapter 3.

Caution: When a slab cache is used, memory is allocated only for the buffer head.
The actual data are ignored when the buffer head is created and must be stored
elsewhere.
Free download pdf