Question

I am trying to write a function in Linux kernel space that walks over a page cache, and searches for a page that contains a specific block.

I don't know how to get the pages in the page-cache one-by-one.

I saw that find_get_page is a function that can help me, but I don't know how to get the first page offset and how to continue. As I said, I am trying to do something like that:

for(every page in struct address_space *mapping)
{
    for(every struct buffer_head in current_page->buffers)
    {
        check if(my_sector == current_buffer_head->b_blocknr)
            ...
    }
}

Can anyone help to find how to walk over all the page-cache?

I believe that there is a code in Linux kernel that does something like this (for example: when there is a write to a page and the page is searched in the cache), but I didn't find it...

Thanks!

Was it helpful?

Solution

The address_space structure holds all the pages in radix_tree (mapping->page_tree in your case). So all you need is to iterate over that tree. Linux kernel has radix tree API (see here) including the for_each iterators. For eaxmple:

396 /**
397  * radix_tree_for_each_chunk_slot - iterate over slots in one chunk
398  *
399  * @slot:       the void** variable, at the beginning points to chunk first slot
400  * @iter:       the struct radix_tree_iter pointer
401  * @flags:      RADIX_TREE_ITER_*, should be constant
402  *
403  * This macro is designed to be nested inside radix_tree_for_each_chunk().
404  * @slot points to the radix tree slot, @iter->index contains its index.
405  */
406 #define radix_tree_for_each_chunk_slot(slot, iter, flags)               \
407         for (; slot ; slot = radix_tree_next_slot(slot, iter, flags))
408 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top