Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


{
struct bio *bio = NULL;
unsigned page_idx;
sector_t last_block_in_bio = 0;
struct buffer_head map_bh;
struct pagevec lru_pvec;

clear_buffer_mapped(&map_bh);
for (page_idx = 0; page_idx < nr_pages; page_idx++) {
struct page *page = list_entry(pages->prev, struct page, lru);

Each loop pass first adds the page to the address space-specific cache before abiorequest is created to
read the desired data for the block layer:

fs/mpage.c
list_del(&page->lru);
if (!add_to_page_cache_lru(page, mapping,
page->index, GFP_KERNEL)) {
bio = do_mpage_readpage(bio, page,
nr_pages - page_idx,
&last_block_in_bio, &map_bh,
&first_logical_block,
get_block);
} else {
page_cache_release(page);
}
}
The pages are installed both in the page cache and in the kernel’s LRU list usingadd_to_page_cache_lru.

Whendo_mpage_readpagebuilds thebiorequest, the BIO data of the preceding pages are also included
so that a combined request can be constructed. If several successive pages are to be read from the block
device, this can be done in a single request rather than submitting an individual request for each page.
Notice that thebuffer_headpassed todo_mpage_readpageis usually not required. However, if an
unusual situation is encountered (e.g., a page that contains buffers), then it falls back to using the old-
fashioned, blockwise read routines.

If, at the end of the loop, a BIO request is left unprocessed bydo_mpage_readpage, it is now submitted:

fs/mpage.c
if (bio)
mpage_bio_submit(READ, bio);
return 0;
}

16.4.5 Page Cache Readahead


Predicting the future is generally accepted to be a rather hard problem, but from time to time, the kernel
cannot resist making a try nevertheless. Actually, there are situations where it is not too hard to say what
will happen next, namely, when a process is reading data from a file.

Usually pages are read sequentially — this is also an assumption made by most filesystems. Recall from
Chapter 9 that the extended filesystem family makes great effort to allocate adjacent blocks for a file such
that the head of a block device only needs to move as little as possible when data are read and written.
Free download pdf