Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Guillaume Chazarain | 3e9f45b | 2007-05-08 00:23:25 -0700 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 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 Chazarain | 3e9f45b | 2007-05-08 00:23:25 -0700 | [diff] [blame] | 23 | static 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 Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 33 | static inline gfp_t mapping_gfp_mask(struct address_space * mapping) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Al Viro | 260b236 | 2005-10-21 03:22:44 -0400 | [diff] [blame] | 35 | return (__force gfp_t)mapping->flags & __GFP_BITS_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
| 39 | * This is non-atomic. Only to be used before the mapping is activated. |
| 40 | * Probably needs a barrier... |
| 41 | */ |
Al Viro | 260b236 | 2005-10-21 03:22:44 -0400 | [diff] [blame] | 42 | static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Al Viro | 260b236 | 2005-10-21 03:22:44 -0400 | [diff] [blame] | 44 | m->flags = (m->flags & ~(__force unsigned long)__GFP_BITS_MASK) | |
| 45 | (__force unsigned long)mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 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) |
| 63 | void release_pages(struct page **pages, int nr, int cold); |
| 64 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 65 | #ifdef CONFIG_NUMA |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 66 | extern struct page *__page_cache_alloc(gfp_t gfp); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 67 | #else |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 68 | static inline struct page *__page_cache_alloc(gfp_t gfp) |
| 69 | { |
| 70 | return alloc_pages(gfp, 0); |
| 71 | } |
| 72 | #endif |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static inline struct page *page_cache_alloc(struct address_space *x) |
| 75 | { |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 76 | return __page_cache_alloc(mapping_gfp_mask(x)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline struct page *page_cache_alloc_cold(struct address_space *x) |
| 80 | { |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 81 | return __page_cache_alloc(mapping_gfp_mask(x)|__GFP_COLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | typedef int filler_t(void *, struct page *); |
| 85 | |
| 86 | extern struct page * find_get_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 87 | pgoff_t index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | extern struct page * find_lock_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 89 | pgoff_t index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | extern struct page * find_or_create_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 91 | pgoff_t index, gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | unsigned find_get_pages(struct address_space *mapping, pgoff_t start, |
| 93 | unsigned int nr_pages, struct page **pages); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 94 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start, |
| 95 | unsigned int nr_pages, struct page **pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, |
| 97 | int tag, unsigned int nr_pages, struct page **pages); |
| 98 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 99 | struct page *__grab_cache_page(struct address_space *mapping, pgoff_t index); |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Returns locked page at given index in given cache, creating it if needed. |
| 103 | */ |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 104 | static inline struct page *grab_cache_page(struct address_space *mapping, |
| 105 | pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | return find_or_create_page(mapping, index, mapping_gfp_mask(mapping)); |
| 108 | } |
| 109 | |
| 110 | extern struct page * grab_cache_page_nowait(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 111 | pgoff_t index); |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 112 | extern struct page * read_cache_page_async(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 113 | pgoff_t index, filler_t *filler, |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 114 | void *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | extern struct page * read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 116 | pgoff_t index, filler_t *filler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | void *data); |
| 118 | extern int read_cache_pages(struct address_space *mapping, |
| 119 | struct list_head *pages, filler_t *filler, void *data); |
| 120 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 121 | static inline struct page *read_mapping_page_async( |
| 122 | struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 123 | pgoff_t index, void *data) |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 124 | { |
| 125 | filler_t *filler = (filler_t *)mapping->a_ops->readpage; |
| 126 | return read_cache_page_async(mapping, index, filler, data); |
| 127 | } |
| 128 | |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 129 | static inline struct page *read_mapping_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 130 | pgoff_t index, void *data) |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 131 | { |
| 132 | filler_t *filler = (filler_t *)mapping->a_ops->readpage; |
| 133 | return read_cache_page(mapping, index, filler, data); |
| 134 | } |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int add_to_page_cache(struct page *page, struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 137 | pgoff_t index, gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 139 | pgoff_t index, gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | extern void remove_from_page_cache(struct page *page); |
| 141 | extern void __remove_from_page_cache(struct page *page); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | /* |
| 144 | * Return byte-offset into filesystem object for page. |
| 145 | */ |
| 146 | static inline loff_t page_offset(struct page *page) |
| 147 | { |
| 148 | return ((loff_t)page->index) << PAGE_CACHE_SHIFT; |
| 149 | } |
| 150 | |
| 151 | static inline pgoff_t linear_page_index(struct vm_area_struct *vma, |
| 152 | unsigned long address) |
| 153 | { |
| 154 | pgoff_t pgoff = (address - vma->vm_start) >> PAGE_SHIFT; |
| 155 | pgoff += vma->vm_pgoff; |
| 156 | return pgoff >> (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
| 157 | } |
| 158 | |
| 159 | extern void FASTCALL(__lock_page(struct page *page)); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 160 | extern int FASTCALL(__lock_page_killable(struct page *page)); |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 161 | extern void FASTCALL(__lock_page_nosync(struct page *page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | extern void FASTCALL(unlock_page(struct page *page)); |
| 163 | |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 164 | /* |
| 165 | * lock_page may only be called if we have the page's inode pinned. |
| 166 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static inline void lock_page(struct page *page) |
| 168 | { |
| 169 | might_sleep(); |
| 170 | if (TestSetPageLocked(page)) |
| 171 | __lock_page(page); |
| 172 | } |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 173 | |
| 174 | /* |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 175 | * lock_page_killable is like lock_page but can be interrupted by fatal |
| 176 | * signals. It returns 0 if it locked the page and -EINTR if it was |
| 177 | * killed while waiting. |
| 178 | */ |
| 179 | static inline int lock_page_killable(struct page *page) |
| 180 | { |
| 181 | might_sleep(); |
| 182 | if (TestSetPageLocked(page)) |
| 183 | return __lock_page_killable(page); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 188 | * lock_page_nosync should only be used if we can't pin the page's inode. |
| 189 | * Doesn't play quite so well with block device plugging. |
| 190 | */ |
| 191 | static inline void lock_page_nosync(struct page *page) |
| 192 | { |
| 193 | might_sleep(); |
| 194 | if (TestSetPageLocked(page)) |
| 195 | __lock_page_nosync(page); |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * This is exported only for wait_on_page_locked/wait_on_page_writeback. |
| 200 | * Never use this directly! |
| 201 | */ |
| 202 | extern void FASTCALL(wait_on_page_bit(struct page *page, int bit_nr)); |
| 203 | |
| 204 | /* |
| 205 | * Wait for a page to be unlocked. |
| 206 | * |
| 207 | * This must be called with the caller "holding" the page, |
| 208 | * ie with increased "page->count" so that the page won't |
| 209 | * go away during the wait.. |
| 210 | */ |
| 211 | static inline void wait_on_page_locked(struct page *page) |
| 212 | { |
| 213 | if (PageLocked(page)) |
| 214 | wait_on_page_bit(page, PG_locked); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Wait for a page to complete writeback |
| 219 | */ |
| 220 | static inline void wait_on_page_writeback(struct page *page) |
| 221 | { |
| 222 | if (PageWriteback(page)) |
| 223 | wait_on_page_bit(page, PG_writeback); |
| 224 | } |
| 225 | |
| 226 | extern void end_page_writeback(struct page *page); |
| 227 | |
| 228 | /* |
| 229 | * Fault a userspace page into pagetables. Return non-zero on a fault. |
| 230 | * |
| 231 | * This assumes that two userspace pages are always sufficient. That's |
| 232 | * not true if PAGE_CACHE_SIZE > PAGE_SIZE. |
| 233 | */ |
| 234 | static inline int fault_in_pages_writeable(char __user *uaddr, int size) |
| 235 | { |
| 236 | int ret; |
| 237 | |
Nick Piggin | 0829142 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 238 | if (unlikely(size == 0)) |
| 239 | return 0; |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | /* |
| 242 | * Writing zeroes into userspace here is OK, because we know that if |
| 243 | * the zero gets there, we'll be overwriting it. |
| 244 | */ |
| 245 | ret = __put_user(0, uaddr); |
| 246 | if (ret == 0) { |
| 247 | char __user *end = uaddr + size - 1; |
| 248 | |
| 249 | /* |
| 250 | * If the page was already mapped, this will get a cache miss |
| 251 | * for sure, so try to avoid doing it. |
| 252 | */ |
| 253 | if (((unsigned long)uaddr & PAGE_MASK) != |
| 254 | ((unsigned long)end & PAGE_MASK)) |
| 255 | ret = __put_user(0, end); |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
Nick Piggin | 0829142 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 260 | static inline int fault_in_pages_readable(const char __user *uaddr, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | volatile char c; |
| 263 | int ret; |
| 264 | |
Nick Piggin | 0829142 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 265 | if (unlikely(size == 0)) |
| 266 | return 0; |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | ret = __get_user(c, uaddr); |
| 269 | if (ret == 0) { |
| 270 | const char __user *end = uaddr + size - 1; |
| 271 | |
| 272 | if (((unsigned long)uaddr & PAGE_MASK) != |
| 273 | ((unsigned long)end & PAGE_MASK)) |
Nick Piggin | 0829142 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 274 | ret = __get_user(c, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
Nick Piggin | 0829142 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 276 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | #endif /* _LINUX_PAGEMAP_H */ |