Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #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> | |
14 | ||
15 | /* | |
16 | * Bits in mapping->flags. The lower __GFP_BITS_SHIFT bits are the page | |
17 | * allocation mode flags. | |
18 | */ | |
19 | #define AS_EIO (__GFP_BITS_SHIFT + 0) /* IO error on async write */ | |
20 | #define AS_ENOSPC (__GFP_BITS_SHIFT + 1) /* ENOSPC on async write */ | |
21 | ||
dd0fc66f | 22 | static inline gfp_t mapping_gfp_mask(struct address_space * mapping) |
1da177e4 | 23 | { |
260b2367 | 24 | return (__force gfp_t)mapping->flags & __GFP_BITS_MASK; |
1da177e4 LT |
25 | } |
26 | ||
27 | /* | |
28 | * This is non-atomic. Only to be used before the mapping is activated. | |
29 | * Probably needs a barrier... | |
30 | */ | |
260b2367 | 31 | static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask) |
1da177e4 | 32 | { |
260b2367 AV |
33 | m->flags = (m->flags & ~(__force unsigned long)__GFP_BITS_MASK) | |
34 | (__force unsigned long)mask; | |
1da177e4 LT |
35 | } |
36 | ||
37 | /* | |
38 | * The page cache can done in larger chunks than | |
39 | * one page, because it allows for more efficient | |
40 | * throughput (it can then be mapped into user | |
41 | * space in smaller chunks for same flexibility). | |
42 | * | |
43 | * Or rather, it _will_ be done in larger chunks. | |
44 | */ | |
45 | #define PAGE_CACHE_SHIFT PAGE_SHIFT | |
46 | #define PAGE_CACHE_SIZE PAGE_SIZE | |
47 | #define PAGE_CACHE_MASK PAGE_MASK | |
48 | #define PAGE_CACHE_ALIGN(addr) (((addr)+PAGE_CACHE_SIZE-1)&PAGE_CACHE_MASK) | |
49 | ||
50 | #define page_cache_get(page) get_page(page) | |
51 | #define page_cache_release(page) put_page(page) | |
52 | void release_pages(struct page **pages, int nr, int cold); | |
53 | ||
44110fe3 PJ |
54 | #ifdef CONFIG_NUMA |
55 | extern struct page *page_cache_alloc(struct address_space *x); | |
56 | extern struct page *page_cache_alloc_cold(struct address_space *x); | |
57 | #else | |
1da177e4 LT |
58 | static inline struct page *page_cache_alloc(struct address_space *x) |
59 | { | |
2d6c666e | 60 | return alloc_pages(mapping_gfp_mask(x), 0); |
1da177e4 LT |
61 | } |
62 | ||
63 | static inline struct page *page_cache_alloc_cold(struct address_space *x) | |
64 | { | |
2d6c666e | 65 | return alloc_pages(mapping_gfp_mask(x)|__GFP_COLD, 0); |
1da177e4 | 66 | } |
44110fe3 | 67 | #endif |
1da177e4 LT |
68 | |
69 | typedef int filler_t(void *, struct page *); | |
70 | ||
71 | extern struct page * find_get_page(struct address_space *mapping, | |
72 | unsigned long index); | |
73 | extern struct page * find_lock_page(struct address_space *mapping, | |
74 | unsigned long index); | |
93fac704 NP |
75 | extern __deprecated_for_modules struct page * find_trylock_page( |
76 | struct address_space *mapping, unsigned long index); | |
1da177e4 | 77 | extern struct page * find_or_create_page(struct address_space *mapping, |
6daa0e28 | 78 | unsigned long index, gfp_t gfp_mask); |
1da177e4 LT |
79 | unsigned find_get_pages(struct address_space *mapping, pgoff_t start, |
80 | unsigned int nr_pages, struct page **pages); | |
81 | unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, | |
82 | int tag, unsigned int nr_pages, struct page **pages); | |
83 | ||
84 | /* | |
85 | * Returns locked page at given index in given cache, creating it if needed. | |
86 | */ | |
87 | static inline struct page *grab_cache_page(struct address_space *mapping, unsigned long index) | |
88 | { | |
89 | return find_or_create_page(mapping, index, mapping_gfp_mask(mapping)); | |
90 | } | |
91 | ||
92 | extern struct page * grab_cache_page_nowait(struct address_space *mapping, | |
93 | unsigned long index); | |
94 | extern struct page * read_cache_page(struct address_space *mapping, | |
95 | unsigned long index, filler_t *filler, | |
96 | void *data); | |
97 | extern int read_cache_pages(struct address_space *mapping, | |
98 | struct list_head *pages, filler_t *filler, void *data); | |
99 | ||
100 | int add_to_page_cache(struct page *page, struct address_space *mapping, | |
6daa0e28 | 101 | unsigned long index, gfp_t gfp_mask); |
1da177e4 | 102 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, |
6daa0e28 | 103 | unsigned long index, gfp_t gfp_mask); |
1da177e4 LT |
104 | extern void remove_from_page_cache(struct page *page); |
105 | extern void __remove_from_page_cache(struct page *page); | |
106 | ||
107 | extern atomic_t nr_pagecache; | |
108 | ||
109 | #ifdef CONFIG_SMP | |
110 | ||
111 | #define PAGECACHE_ACCT_THRESHOLD max(16, NR_CPUS * 2) | |
112 | DECLARE_PER_CPU(long, nr_pagecache_local); | |
113 | ||
114 | /* | |
115 | * pagecache_acct implements approximate accounting for pagecache. | |
116 | * vm_enough_memory() do not need high accuracy. Writers will keep | |
117 | * an offset in their per-cpu arena and will spill that into the | |
118 | * global count whenever the absolute value of the local count | |
119 | * exceeds the counter's threshold. | |
120 | * | |
121 | * MUST be protected from preemption. | |
122 | * current protection is mapping->page_lock. | |
123 | */ | |
124 | static inline void pagecache_acct(int count) | |
125 | { | |
126 | long *local; | |
127 | ||
128 | local = &__get_cpu_var(nr_pagecache_local); | |
129 | *local += count; | |
130 | if (*local > PAGECACHE_ACCT_THRESHOLD || *local < -PAGECACHE_ACCT_THRESHOLD) { | |
131 | atomic_add(*local, &nr_pagecache); | |
132 | *local = 0; | |
133 | } | |
134 | } | |
135 | ||
136 | #else | |
137 | ||
138 | static inline void pagecache_acct(int count) | |
139 | { | |
140 | atomic_add(count, &nr_pagecache); | |
141 | } | |
142 | #endif | |
143 | ||
144 | static inline unsigned long get_page_cache_size(void) | |
145 | { | |
146 | int ret = atomic_read(&nr_pagecache); | |
147 | if (unlikely(ret < 0)) | |
148 | ret = 0; | |
149 | return ret; | |
150 | } | |
151 | ||
152 | /* | |
153 | * Return byte-offset into filesystem object for page. | |
154 | */ | |
155 | static inline loff_t page_offset(struct page *page) | |
156 | { | |
157 | return ((loff_t)page->index) << PAGE_CACHE_SHIFT; | |
158 | } | |
159 | ||
160 | static inline pgoff_t linear_page_index(struct vm_area_struct *vma, | |
161 | unsigned long address) | |
162 | { | |
163 | pgoff_t pgoff = (address - vma->vm_start) >> PAGE_SHIFT; | |
164 | pgoff += vma->vm_pgoff; | |
165 | return pgoff >> (PAGE_CACHE_SHIFT - PAGE_SHIFT); | |
166 | } | |
167 | ||
168 | extern void FASTCALL(__lock_page(struct page *page)); | |
169 | extern void FASTCALL(unlock_page(struct page *page)); | |
170 | ||
171 | static inline void lock_page(struct page *page) | |
172 | { | |
173 | might_sleep(); | |
174 | if (TestSetPageLocked(page)) | |
175 | __lock_page(page); | |
176 | } | |
177 | ||
178 | /* | |
179 | * This is exported only for wait_on_page_locked/wait_on_page_writeback. | |
180 | * Never use this directly! | |
181 | */ | |
182 | extern void FASTCALL(wait_on_page_bit(struct page *page, int bit_nr)); | |
183 | ||
184 | /* | |
185 | * Wait for a page to be unlocked. | |
186 | * | |
187 | * This must be called with the caller "holding" the page, | |
188 | * ie with increased "page->count" so that the page won't | |
189 | * go away during the wait.. | |
190 | */ | |
191 | static inline void wait_on_page_locked(struct page *page) | |
192 | { | |
193 | if (PageLocked(page)) | |
194 | wait_on_page_bit(page, PG_locked); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Wait for a page to complete writeback | |
199 | */ | |
200 | static inline void wait_on_page_writeback(struct page *page) | |
201 | { | |
202 | if (PageWriteback(page)) | |
203 | wait_on_page_bit(page, PG_writeback); | |
204 | } | |
205 | ||
206 | extern void end_page_writeback(struct page *page); | |
207 | ||
208 | /* | |
209 | * Fault a userspace page into pagetables. Return non-zero on a fault. | |
210 | * | |
211 | * This assumes that two userspace pages are always sufficient. That's | |
212 | * not true if PAGE_CACHE_SIZE > PAGE_SIZE. | |
213 | */ | |
214 | static inline int fault_in_pages_writeable(char __user *uaddr, int size) | |
215 | { | |
216 | int ret; | |
217 | ||
218 | /* | |
219 | * Writing zeroes into userspace here is OK, because we know that if | |
220 | * the zero gets there, we'll be overwriting it. | |
221 | */ | |
222 | ret = __put_user(0, uaddr); | |
223 | if (ret == 0) { | |
224 | char __user *end = uaddr + size - 1; | |
225 | ||
226 | /* | |
227 | * If the page was already mapped, this will get a cache miss | |
228 | * for sure, so try to avoid doing it. | |
229 | */ | |
230 | if (((unsigned long)uaddr & PAGE_MASK) != | |
231 | ((unsigned long)end & PAGE_MASK)) | |
232 | ret = __put_user(0, end); | |
233 | } | |
234 | return ret; | |
235 | } | |
236 | ||
237 | static inline void fault_in_pages_readable(const char __user *uaddr, int size) | |
238 | { | |
239 | volatile char c; | |
240 | int ret; | |
241 | ||
242 | ret = __get_user(c, uaddr); | |
243 | if (ret == 0) { | |
244 | const char __user *end = uaddr + size - 1; | |
245 | ||
246 | if (((unsigned long)uaddr & PAGE_MASK) != | |
247 | ((unsigned long)end & PAGE_MASK)) | |
248 | __get_user(c, end); | |
249 | } | |
250 | } | |
251 | ||
252 | #endif /* _LINUX_PAGEMAP_H */ |