Mmfilemapc

struct page * find_get_page(struct address_space *mapping, pgoff_t offset) {

struct page *page;

page = radix_tree_lookup(&mapping->page_tree, offset); if (page)

page_cache_get(page); return page;

Life is easy for the page cache because all the hard work is done by the radix tree implementation: radix_tree_lookup finds the desired page at a given offset, and page_cache_get increments the page's reference count if one was found.

However, pages will very often belong to a file. Unfortunately, positions in a file are specified as byte offsets, not as offsets within the page cache. How can a file offset be converted into a page cache offset?

Currently, the granularity of the page cache is a single page; that is, the leaf elements of the page cache radix tree are single pages. Future kernels might, however, increase the granularity, so assuming a page size granularity is not valid. Instead, the macro page_cache_shift is provided. The object size for a page cache element can be computed by 2PAGE_CACHE_SHIFT.

Converting between byte offsets in a file and page cache offsets is then a simple matter of dividing the index by page_cache_shift:

ppos is a byte offset into a file, and index contains the corresponding page cache offset. Two auxiliary functions are provided for convenience: <pagemap.h>

struct page * find_or_create_page(struct address_space *mapping, pgoff_t index, gfp_t gfp_mask); struct page * find_lock_page(struct address_space *mapping, pgoff_t index);

find_or_create_page does what the name promises — it looks up a page in the page cache and allocates a fresh one if it is not there. The page is inserted into the cache and the LRU list by calling add_to_page_cache_lru.

find_lock_page works like find_get_page, but locks the page.

Caution: If the page is already locked from some other part of the kernel, the function can sleep until the page is unlocked.

It is also possible to search for more than one page. Here are the prototypes of the responsible auxiliary functions:

Continue reading here: Info

Was this article helpful?

0 0