blob: 862fc07dc6c02b32e88665236ae73f5ce43a1f07 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_PAGEMAP_H
2#define _LINUX_PAGEMAP_H
3
4/*
5 * Copyright 1995 Linus Torvalds
6 */
7#include <linux/mm.h>
8#include <linux/fs.h>
9#include <linux/list.h>
10#include <linux/highmem.h>
11#include <linux/compiler.h>
12#include <asm/uaccess.h>
13#include <linux/gfp.h>
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -070014#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * Bits in mapping->flags. The lower __GFP_BITS_SHIFT bits are the page
18 * allocation mode flags.
19 */
20#define AS_EIO (__GFP_BITS_SHIFT + 0) /* IO error on async write */
21#define AS_ENOSPC (__GFP_BITS_SHIFT + 1) /* ENOSPC on async write */
22
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -070023static inline void mapping_set_error(struct address_space *mapping, int error)
24{
25 if (error) {
26 if (error == -ENOSPC)
27 set_bit(AS_ENOSPC, &mapping->flags);
28 else
29 set_bit(AS_EIO, &mapping->flags);
30 }
31}
32
Al Virodd0fc662005-10-07 07:46:04 +010033static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Al Viro260b2362005-10-21 03:22:44 -040035 return (__force gfp_t)mapping->flags & __GFP_BITS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38/*
39 * This is non-atomic. Only to be used before the mapping is activated.
40 * Probably needs a barrier...
41 */
Al Viro260b2362005-10-21 03:22:44 -040042static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Al Viro260b2362005-10-21 03:22:44 -040044 m->flags = (m->flags & ~(__force unsigned long)__GFP_BITS_MASK) |
45 (__force unsigned long)mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48/*
49 * The page cache can done in larger chunks than
50 * one page, because it allows for more efficient
51 * throughput (it can then be mapped into user
52 * space in smaller chunks for same flexibility).
53 *
54 * Or rather, it _will_ be done in larger chunks.
55 */
56#define PAGE_CACHE_SHIFT PAGE_SHIFT
57#define PAGE_CACHE_SIZE PAGE_SIZE
58#define PAGE_CACHE_MASK PAGE_MASK
59#define PAGE_CACHE_ALIGN(addr) (((addr)+PAGE_CACHE_SIZE-1)&PAGE_CACHE_MASK)
60
61#define page_cache_get(page) get_page(page)
62#define page_cache_release(page) put_page(page)
63void release_pages(struct page **pages, int nr, int cold);
64
Paul Jackson44110fe2006-03-24 03:16:04 -080065#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -070066extern struct page *__page_cache_alloc(gfp_t gfp);
Paul Jackson44110fe2006-03-24 03:16:04 -080067#else
Nick Piggin2ae88142006-10-28 10:38:23 -070068static inline struct page *__page_cache_alloc(gfp_t gfp)
69{
70 return alloc_pages(gfp, 0);
71}
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static inline struct page *page_cache_alloc(struct address_space *x)
75{
Nick Piggin2ae88142006-10-28 10:38:23 -070076 return __page_cache_alloc(mapping_gfp_mask(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static inline struct page *page_cache_alloc_cold(struct address_space *x)
80{
Nick Piggin2ae88142006-10-28 10:38:23 -070081 return __page_cache_alloc(mapping_gfp_mask(x)|__GFP_COLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84typedef int filler_t(void *, struct page *);
85
86extern struct page * find_get_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -070087 pgoff_t index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088extern struct page * find_lock_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -070089 pgoff_t index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090extern struct page * find_or_create_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -070091 pgoff_t index, gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
93 unsigned int nr_pages, struct page **pages);
Jens Axboeebf43502006-04-27 08:46:01 +020094unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
95 unsigned int nr_pages, struct page **pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
97 int tag, unsigned int nr_pages, struct page **pages);
98
99/*
100 * Returns locked page at given index in given cache, creating it if needed.
101 */
Fengguang Wu57f6b962007-10-16 01:24:37 -0700102static inline struct page *grab_cache_page(struct address_space *mapping,
103 pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
106}
107
108extern struct page * grab_cache_page_nowait(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700109 pgoff_t index);
Nick Piggin6fe69002007-05-06 14:49:04 -0700110extern struct page * read_cache_page_async(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700111 pgoff_t index, filler_t *filler,
Nick Piggin6fe69002007-05-06 14:49:04 -0700112 void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113extern struct page * read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700114 pgoff_t index, filler_t *filler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 void *data);
116extern int read_cache_pages(struct address_space *mapping,
117 struct list_head *pages, filler_t *filler, void *data);
118
Nick Piggin6fe69002007-05-06 14:49:04 -0700119static inline struct page *read_mapping_page_async(
120 struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700121 pgoff_t index, void *data)
Nick Piggin6fe69002007-05-06 14:49:04 -0700122{
123 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
124 return read_cache_page_async(mapping, index, filler, data);
125}
126
Pekka Enberg090d2b12006-06-23 02:05:08 -0700127static inline struct page *read_mapping_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700128 pgoff_t index, void *data)
Pekka Enberg090d2b12006-06-23 02:05:08 -0700129{
130 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
131 return read_cache_page(mapping, index, filler, data);
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134int add_to_page_cache(struct page *page, struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700135 pgoff_t index, gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700137 pgoff_t index, gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138extern void remove_from_page_cache(struct page *page);
139extern void __remove_from_page_cache(struct page *page);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * Return byte-offset into filesystem object for page.
143 */
144static inline loff_t page_offset(struct page *page)
145{
146 return ((loff_t)page->index) << PAGE_CACHE_SHIFT;
147}
148
149static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
150 unsigned long address)
151{
152 pgoff_t pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
153 pgoff += vma->vm_pgoff;
154 return pgoff >> (PAGE_CACHE_SHIFT - PAGE_SHIFT);
155}
156
157extern void FASTCALL(__lock_page(struct page *page));
Nick Piggindb376482006-09-25 23:31:24 -0700158extern void FASTCALL(__lock_page_nosync(struct page *page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159extern void FASTCALL(unlock_page(struct page *page));
160
Nick Piggindb376482006-09-25 23:31:24 -0700161/*
162 * lock_page may only be called if we have the page's inode pinned.
163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static inline void lock_page(struct page *page)
165{
166 might_sleep();
167 if (TestSetPageLocked(page))
168 __lock_page(page);
169}
Nick Piggindb376482006-09-25 23:31:24 -0700170
171/*
172 * lock_page_nosync should only be used if we can't pin the page's inode.
173 * Doesn't play quite so well with block device plugging.
174 */
175static inline void lock_page_nosync(struct page *page)
176{
177 might_sleep();
178 if (TestSetPageLocked(page))
179 __lock_page_nosync(page);
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
183 * This is exported only for wait_on_page_locked/wait_on_page_writeback.
184 * Never use this directly!
185 */
186extern void FASTCALL(wait_on_page_bit(struct page *page, int bit_nr));
187
188/*
189 * Wait for a page to be unlocked.
190 *
191 * This must be called with the caller "holding" the page,
192 * ie with increased "page->count" so that the page won't
193 * go away during the wait..
194 */
195static inline void wait_on_page_locked(struct page *page)
196{
197 if (PageLocked(page))
198 wait_on_page_bit(page, PG_locked);
199}
200
201/*
202 * Wait for a page to complete writeback
203 */
204static inline void wait_on_page_writeback(struct page *page)
205{
206 if (PageWriteback(page))
207 wait_on_page_bit(page, PG_writeback);
208}
209
210extern void end_page_writeback(struct page *page);
211
212/*
213 * Fault a userspace page into pagetables. Return non-zero on a fault.
214 *
215 * This assumes that two userspace pages are always sufficient. That's
216 * not true if PAGE_CACHE_SIZE > PAGE_SIZE.
217 */
218static inline int fault_in_pages_writeable(char __user *uaddr, int size)
219{
220 int ret;
221
222 /*
223 * Writing zeroes into userspace here is OK, because we know that if
224 * the zero gets there, we'll be overwriting it.
225 */
226 ret = __put_user(0, uaddr);
227 if (ret == 0) {
228 char __user *end = uaddr + size - 1;
229
230 /*
231 * If the page was already mapped, this will get a cache miss
232 * for sure, so try to avoid doing it.
233 */
234 if (((unsigned long)uaddr & PAGE_MASK) !=
235 ((unsigned long)end & PAGE_MASK))
236 ret = __put_user(0, end);
237 }
238 return ret;
239}
240
241static inline void fault_in_pages_readable(const char __user *uaddr, int size)
242{
243 volatile char c;
244 int ret;
245
246 ret = __get_user(c, uaddr);
247 if (ret == 0) {
248 const char __user *end = uaddr + size - 1;
249
250 if (((unsigned long)uaddr & PAGE_MASK) !=
251 ((unsigned long)end & PAGE_MASK))
252 __get_user(c, end);
253 }
254}
255
256#endif /* _LINUX_PAGEMAP_H */