1 From: Peter Zijlstra <a.p.zijlstra@chello.nl>
2 Subject: mm: methods for teaching filesystems about PG_swapcache pages
4 References: FATE#303834
6 In order to teach filesystems to handle swap cache pages, three new page
7 functions are introduced:
9 pgoff_t page_file_index(struct page *);
10 loff_t page_file_offset(struct page *);
11 struct address_space *page_file_mapping(struct page *);
13 page_file_index() - gives the offset of this page in the file in
14 PAGE_CACHE_SIZE blocks. Like page->index is for mapped pages, this function
15 also gives the correct index for PG_swapcache pages.
17 page_file_offset() - uses page_file_index(), so that it will give the expected
18 result, even for PG_swapcache pages.
20 page_file_mapping() - gives the mapping backing the actual page; that is for
21 swap cache pages it will give swap_file->f_mapping.
23 Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
24 Acked-by: Neil Brown <neilb@suse.de>
25 Acked-by: Suresh Jayaraman <sjayaraman@suse.de>
28 include/linux/mm.h | 25 +++++++++++++++++++++++++
29 include/linux/pagemap.h | 5 +++++
30 mm/swapfile.c | 19 +++++++++++++++++++
31 3 files changed, 49 insertions(+)
33 --- a/include/linux/mm.h
34 +++ b/include/linux/mm.h
35 @@ -646,6 +646,17 @@ static inline struct address_space *page
39 +extern struct address_space *__page_file_mapping(struct page *);
42 +struct address_space *page_file_mapping(struct page *page)
44 + if (unlikely(PageSwapCache(page)))
45 + return __page_file_mapping(page);
47 + return page->mapping;
50 static inline int PageAnon(struct page *page)
52 return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
53 @@ -662,6 +673,20 @@ static inline pgoff_t page_index(struct
57 +extern pgoff_t __page_file_index(struct page *page);
60 + * Return the file index of the page. Regular pagecache pages use ->index
61 + * whereas swapcache pages use swp_offset(->private)
63 +static inline pgoff_t page_file_index(struct page *page)
65 + if (unlikely(PageSwapCache(page)))
66 + return __page_file_index(page);
72 * The atomic page->_mapcount, like _count, starts from -1:
73 * so that transitions both from it and to it can be tracked,
74 --- a/include/linux/pagemap.h
75 +++ b/include/linux/pagemap.h
76 @@ -259,6 +259,11 @@ static inline loff_t page_offset(struct
77 return ((loff_t)page->index) << PAGE_CACHE_SHIFT;
80 +static inline loff_t page_file_offset(struct page *page)
82 + return ((loff_t)page_file_index(page)) << PAGE_CACHE_SHIFT;
85 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
86 unsigned long address)
90 @@ -1856,6 +1856,25 @@ struct swap_info_struct *page_swap_info(
94 + * out-of-line __page_file_ methods to avoid include hell.
97 +struct address_space *__page_file_mapping(struct page *page)
99 + VM_BUG_ON(!PageSwapCache(page));
100 + return page_swap_info(page)->swap_file->f_mapping;
102 +EXPORT_SYMBOL_GPL(__page_file_mapping);
104 +pgoff_t __page_file_index(struct page *page)
106 + swp_entry_t swap = { .val = page_private(page) };
107 + VM_BUG_ON(!PageSwapCache(page));
108 + return swp_offset(swap);
110 +EXPORT_SYMBOL_GPL(__page_file_index);
113 * swap_lock prevents swap_map being freed. Don't grab an extra
114 * reference on the swaphandle, it doesn't matter if it becomes unused.