Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/filemap.c |
| 3 | * |
| 4 | * Copyright (C) 1994-1999 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This file handles the generic file mmap semantics used by |
| 9 | * most "normal" filesystems (but you don't /have/ to use this: |
| 10 | * the NFS filesystem used to do this differently, for example) |
| 11 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/compiler.h> |
| 15 | #include <linux/fs.h> |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/aio.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kernel_stat.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/swap.h> |
| 22 | #include <linux/mman.h> |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <linux/file.h> |
| 25 | #include <linux/uio.h> |
| 26 | #include <linux/hash.h> |
| 27 | #include <linux/writeback.h> |
| 28 | #include <linux/pagevec.h> |
| 29 | #include <linux/blkdev.h> |
| 30 | #include <linux/security.h> |
| 31 | #include <linux/syscalls.h> |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 32 | #include <linux/cpuset.h> |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 33 | #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */ |
Nick Piggin | 0f8053a | 2006-03-22 00:08:33 -0800 | [diff] [blame] | 34 | #include "internal.h" |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * FIXME: remove all knowledge of the buffer layer from the core VM |
| 38 | */ |
| 39 | #include <linux/buffer_head.h> /* for generic_osync_inode */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/mman.h> |
| 42 | |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 43 | static ssize_t |
| 44 | generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 45 | loff_t offset, unsigned long nr_segs); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Shared mappings implemented 30.11.1994. It's not fully working yet, |
| 49 | * though. |
| 50 | * |
| 51 | * Shared mappings now work. 15.8.1995 Bruno. |
| 52 | * |
| 53 | * finished 'unifying' the page and buffer cache and SMP-threaded the |
| 54 | * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> |
| 55 | * |
| 56 | * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> |
| 57 | */ |
| 58 | |
| 59 | /* |
| 60 | * Lock ordering: |
| 61 | * |
| 62 | * ->i_mmap_lock (vmtruncate) |
| 63 | * ->private_lock (__free_pte->__set_page_dirty_buffers) |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 64 | * ->swap_lock (exclusive_swap_page, others) |
| 65 | * ->mapping->tree_lock |
Nick Piggin | 7a40507 | 2007-10-16 23:26:02 -0700 | [diff] [blame] | 66 | * ->zone.lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 68 | * ->i_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | * ->i_mmap_lock (truncate->unmap_mapping_range) |
| 70 | * |
| 71 | * ->mmap_sem |
| 72 | * ->i_mmap_lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 73 | * ->page_table_lock or pte_lock (various, mainly in memory.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) |
| 75 | * |
| 76 | * ->mmap_sem |
| 77 | * ->lock_page (access_process_vm) |
| 78 | * |
Nick Piggin | 82591e6 | 2006-10-19 23:29:10 -0700 | [diff] [blame] | 79 | * ->i_mutex (generic_file_buffered_write) |
| 80 | * ->mmap_sem (fault_in_pages_readable->do_page_fault) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 82 | * ->i_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * ->i_alloc_sem (various) |
| 84 | * |
| 85 | * ->inode_lock |
| 86 | * ->sb_lock (fs/fs-writeback.c) |
| 87 | * ->mapping->tree_lock (__sync_single_inode) |
| 88 | * |
| 89 | * ->i_mmap_lock |
| 90 | * ->anon_vma.lock (vma_adjust) |
| 91 | * |
| 92 | * ->anon_vma.lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 93 | * ->page_table_lock or pte_lock (anon_vma_prepare and various) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 95 | * ->page_table_lock or pte_lock |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 96 | * ->swap_lock (try_to_unmap_one) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * ->private_lock (try_to_unmap_one) |
| 98 | * ->tree_lock (try_to_unmap_one) |
| 99 | * ->zone.lru_lock (follow_page->mark_page_accessed) |
Nick Piggin | 053837f | 2006-01-18 17:42:27 -0800 | [diff] [blame] | 100 | * ->zone.lru_lock (check_pte_range->isolate_lru_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | * ->private_lock (page_remove_rmap->set_page_dirty) |
| 102 | * ->tree_lock (page_remove_rmap->set_page_dirty) |
| 103 | * ->inode_lock (page_remove_rmap->set_page_dirty) |
| 104 | * ->inode_lock (zap_pte_range->set_page_dirty) |
| 105 | * ->private_lock (zap_pte_range->__set_page_dirty_buffers) |
| 106 | * |
| 107 | * ->task->proc_lock |
| 108 | * ->dcache_lock (proc_pid_lookup) |
| 109 | */ |
| 110 | |
| 111 | /* |
| 112 | * Remove a page from the page cache and free it. Caller has to make |
| 113 | * sure the page is locked and that nobody else uses it - or that usage |
| 114 | * is safe. The caller must hold a write_lock on the mapping's tree_lock. |
| 115 | */ |
| 116 | void __remove_from_page_cache(struct page *page) |
| 117 | { |
| 118 | struct address_space *mapping = page->mapping; |
| 119 | |
| 120 | radix_tree_delete(&mapping->page_tree, page->index); |
| 121 | page->mapping = NULL; |
| 122 | mapping->nrpages--; |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 123 | __dec_zone_page_state(page, NR_FILE_PAGES); |
Nick Piggin | 4542681 | 2007-07-15 23:38:12 -0700 | [diff] [blame] | 124 | BUG_ON(page_mapped(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void remove_from_page_cache(struct page *page) |
| 128 | { |
| 129 | struct address_space *mapping = page->mapping; |
| 130 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 131 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | write_lock_irq(&mapping->tree_lock); |
| 134 | __remove_from_page_cache(page); |
| 135 | write_unlock_irq(&mapping->tree_lock); |
| 136 | } |
| 137 | |
| 138 | static int sync_page(void *word) |
| 139 | { |
| 140 | struct address_space *mapping; |
| 141 | struct page *page; |
| 142 | |
Andi Kleen | 07808b7 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 143 | page = container_of((unsigned long *)word, struct page, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
William Lee Irwin III | dd1d5af | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 146 | * page_mapping() is being called without PG_locked held. |
| 147 | * Some knowledge of the state and use of the page is used to |
| 148 | * reduce the requirements down to a memory barrier. |
| 149 | * The danger here is of a stale page_mapping() return value |
| 150 | * indicating a struct address_space different from the one it's |
| 151 | * associated with when it is associated with one. |
| 152 | * After smp_mb(), it's either the correct page_mapping() for |
| 153 | * the page, or an old page_mapping() and the page's own |
| 154 | * page_mapping() has gone NULL. |
| 155 | * The ->sync_page() address_space operation must tolerate |
| 156 | * page_mapping() going NULL. By an amazing coincidence, |
| 157 | * this comes about because none of the users of the page |
| 158 | * in the ->sync_page() methods make essential use of the |
| 159 | * page_mapping(), merely passing the page down to the backing |
| 160 | * device's unplug functions when it's non-NULL, which in turn |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 161 | * ignore it for all cases but swap, where only page_private(page) is |
William Lee Irwin III | dd1d5af | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 162 | * of interest. When page_mapping() does go NULL, the entire |
| 163 | * call stack gracefully ignores the page and returns. |
| 164 | * -- wli |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | */ |
| 166 | smp_mb(); |
| 167 | mapping = page_mapping(page); |
| 168 | if (mapping && mapping->a_ops && mapping->a_ops->sync_page) |
| 169 | mapping->a_ops->sync_page(page); |
| 170 | io_schedule(); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 175 | * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 176 | * @mapping: address space structure to write |
| 177 | * @start: offset in bytes where the range starts |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 178 | * @end: offset in bytes where the range ends (inclusive) |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 179 | * @sync_mode: enable synchronous operation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 181 | * Start writeback against all of a mapping's dirty pages that lie |
| 182 | * within the byte offsets <start, end> inclusive. |
| 183 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 185 | * opposed to a regular memory cleansing writeback. The difference between |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * these two operations is that if a dirty page/buffer is encountered, it must |
| 187 | * be waited upon, and not just skipped over. |
| 188 | */ |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 189 | int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
| 190 | loff_t end, int sync_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | int ret; |
| 193 | struct writeback_control wbc = { |
| 194 | .sync_mode = sync_mode, |
| 195 | .nr_to_write = mapping->nrpages * 2, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 196 | .range_start = start, |
| 197 | .range_end = end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | if (!mapping_cap_writeback_dirty(mapping)) |
| 201 | return 0; |
| 202 | |
| 203 | ret = do_writepages(mapping, &wbc); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | static inline int __filemap_fdatawrite(struct address_space *mapping, |
| 208 | int sync_mode) |
| 209 | { |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 210 | return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | int filemap_fdatawrite(struct address_space *mapping) |
| 214 | { |
| 215 | return __filemap_fdatawrite(mapping, WB_SYNC_ALL); |
| 216 | } |
| 217 | EXPORT_SYMBOL(filemap_fdatawrite); |
| 218 | |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 219 | static int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
| 220 | loff_t end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL); |
| 223 | } |
| 224 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 225 | /** |
| 226 | * filemap_flush - mostly a non-blocking flush |
| 227 | * @mapping: target address_space |
| 228 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | * This is a mostly non-blocking flush. Not suitable for data-integrity |
| 230 | * purposes - I/O may not be started against all dirty pages. |
| 231 | */ |
| 232 | int filemap_flush(struct address_space *mapping) |
| 233 | { |
| 234 | return __filemap_fdatawrite(mapping, WB_SYNC_NONE); |
| 235 | } |
| 236 | EXPORT_SYMBOL(filemap_flush); |
| 237 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 238 | /** |
| 239 | * wait_on_page_writeback_range - wait for writeback to complete |
| 240 | * @mapping: target address_space |
| 241 | * @start: beginning page index |
| 242 | * @end: ending page index |
| 243 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | * Wait for writeback to complete against pages indexed by start->end |
| 245 | * inclusive |
| 246 | */ |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 247 | int wait_on_page_writeback_range(struct address_space *mapping, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | pgoff_t start, pgoff_t end) |
| 249 | { |
| 250 | struct pagevec pvec; |
| 251 | int nr_pages; |
| 252 | int ret = 0; |
| 253 | pgoff_t index; |
| 254 | |
| 255 | if (end < start) |
| 256 | return 0; |
| 257 | |
| 258 | pagevec_init(&pvec, 0); |
| 259 | index = start; |
| 260 | while ((index <= end) && |
| 261 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 262 | PAGECACHE_TAG_WRITEBACK, |
| 263 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) { |
| 264 | unsigned i; |
| 265 | |
| 266 | for (i = 0; i < nr_pages; i++) { |
| 267 | struct page *page = pvec.pages[i]; |
| 268 | |
| 269 | /* until radix tree lookup accepts end_index */ |
| 270 | if (page->index > end) |
| 271 | continue; |
| 272 | |
| 273 | wait_on_page_writeback(page); |
| 274 | if (PageError(page)) |
| 275 | ret = -EIO; |
| 276 | } |
| 277 | pagevec_release(&pvec); |
| 278 | cond_resched(); |
| 279 | } |
| 280 | |
| 281 | /* Check for outstanding write errors */ |
| 282 | if (test_and_clear_bit(AS_ENOSPC, &mapping->flags)) |
| 283 | ret = -ENOSPC; |
| 284 | if (test_and_clear_bit(AS_EIO, &mapping->flags)) |
| 285 | ret = -EIO; |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 290 | /** |
| 291 | * sync_page_range - write and wait on all pages in the passed range |
| 292 | * @inode: target inode |
| 293 | * @mapping: target address_space |
| 294 | * @pos: beginning offset in pages to write |
| 295 | * @count: number of bytes to write |
| 296 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * Write and wait upon all the pages in the passed range. This is a "data |
| 298 | * integrity" operation. It waits upon in-flight writeout before starting and |
| 299 | * waiting upon new writeout. If there was an IO error, return it. |
| 300 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 301 | * We need to re-take i_mutex during the generic_osync_inode list walk because |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * it is otherwise livelockable. |
| 303 | */ |
| 304 | int sync_page_range(struct inode *inode, struct address_space *mapping, |
OGAWA Hirofumi | 268fc16 | 2006-01-08 01:02:12 -0800 | [diff] [blame] | 305 | loff_t pos, loff_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
| 307 | pgoff_t start = pos >> PAGE_CACHE_SHIFT; |
| 308 | pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT; |
| 309 | int ret; |
| 310 | |
| 311 | if (!mapping_cap_writeback_dirty(mapping) || !count) |
| 312 | return 0; |
| 313 | ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1); |
| 314 | if (ret == 0) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 315 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | ret = generic_osync_inode(inode, mapping, OSYNC_METADATA); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 317 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | if (ret == 0) |
| 320 | ret = wait_on_page_writeback_range(mapping, start, end); |
| 321 | return ret; |
| 322 | } |
| 323 | EXPORT_SYMBOL(sync_page_range); |
| 324 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 325 | /** |
| 326 | * sync_page_range_nolock |
| 327 | * @inode: target inode |
| 328 | * @mapping: target address_space |
| 329 | * @pos: beginning offset in pages to write |
| 330 | * @count: number of bytes to write |
| 331 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 332 | * Note: Holding i_mutex across sync_page_range_nolock() is not a good idea |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * as it forces O_SYNC writers to different parts of the same file |
| 334 | * to be serialised right until io completion. |
| 335 | */ |
OGAWA Hirofumi | 268fc16 | 2006-01-08 01:02:12 -0800 | [diff] [blame] | 336 | int sync_page_range_nolock(struct inode *inode, struct address_space *mapping, |
| 337 | loff_t pos, loff_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | pgoff_t start = pos >> PAGE_CACHE_SHIFT; |
| 340 | pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT; |
| 341 | int ret; |
| 342 | |
| 343 | if (!mapping_cap_writeback_dirty(mapping) || !count) |
| 344 | return 0; |
| 345 | ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1); |
| 346 | if (ret == 0) |
| 347 | ret = generic_osync_inode(inode, mapping, OSYNC_METADATA); |
| 348 | if (ret == 0) |
| 349 | ret = wait_on_page_writeback_range(mapping, start, end); |
| 350 | return ret; |
| 351 | } |
OGAWA Hirofumi | 268fc16 | 2006-01-08 01:02:12 -0800 | [diff] [blame] | 352 | EXPORT_SYMBOL(sync_page_range_nolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 355 | * filemap_fdatawait - wait for all under-writeback pages to complete |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | * @mapping: address space structure to wait for |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 357 | * |
| 358 | * Walk the list of under-writeback pages of the given address space |
| 359 | * and wait for all of them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | */ |
| 361 | int filemap_fdatawait(struct address_space *mapping) |
| 362 | { |
| 363 | loff_t i_size = i_size_read(mapping->host); |
| 364 | |
| 365 | if (i_size == 0) |
| 366 | return 0; |
| 367 | |
| 368 | return wait_on_page_writeback_range(mapping, 0, |
| 369 | (i_size - 1) >> PAGE_CACHE_SHIFT); |
| 370 | } |
| 371 | EXPORT_SYMBOL(filemap_fdatawait); |
| 372 | |
| 373 | int filemap_write_and_wait(struct address_space *mapping) |
| 374 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 375 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | if (mapping->nrpages) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 378 | err = filemap_fdatawrite(mapping); |
| 379 | /* |
| 380 | * Even if the above returned error, the pages may be |
| 381 | * written partially (e.g. -ENOSPC), so we wait for it. |
| 382 | * But the -EIO is special case, it may indicate the worst |
| 383 | * thing (e.g. bug) happened, so we avoid waiting for it. |
| 384 | */ |
| 385 | if (err != -EIO) { |
| 386 | int err2 = filemap_fdatawait(mapping); |
| 387 | if (!err) |
| 388 | err = err2; |
| 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 391 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 393 | EXPORT_SYMBOL(filemap_write_and_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 395 | /** |
| 396 | * filemap_write_and_wait_range - write out & wait on a file range |
| 397 | * @mapping: the address_space for the pages |
| 398 | * @lstart: offset in bytes where the range starts |
| 399 | * @lend: offset in bytes where the range ends (inclusive) |
| 400 | * |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 401 | * Write out and wait upon file offsets lstart->lend, inclusive. |
| 402 | * |
| 403 | * Note that `lend' is inclusive (describes the last byte to be written) so |
| 404 | * that this function can be used to write to the very end-of-file (end = -1). |
| 405 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | int filemap_write_and_wait_range(struct address_space *mapping, |
| 407 | loff_t lstart, loff_t lend) |
| 408 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 409 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | if (mapping->nrpages) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 412 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 413 | WB_SYNC_ALL); |
| 414 | /* See comment of filemap_write_and_wait() */ |
| 415 | if (err != -EIO) { |
| 416 | int err2 = wait_on_page_writeback_range(mapping, |
| 417 | lstart >> PAGE_CACHE_SHIFT, |
| 418 | lend >> PAGE_CACHE_SHIFT); |
| 419 | if (!err) |
| 420 | err = err2; |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 423 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 426 | /** |
| 427 | * add_to_page_cache - add newly allocated pagecache pages |
| 428 | * @page: page to add |
| 429 | * @mapping: the page's address_space |
| 430 | * @offset: page index |
| 431 | * @gfp_mask: page allocation mode |
| 432 | * |
| 433 | * This function is used to add newly allocated pagecache pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | * the page is new, so we can just run SetPageLocked() against it. |
| 435 | * The other page state flags were set by rmqueue(). |
| 436 | * |
| 437 | * This function does not add the page to the LRU. The caller must do that. |
| 438 | */ |
| 439 | int add_to_page_cache(struct page *page, struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 440 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM); |
| 443 | |
| 444 | if (error == 0) { |
| 445 | write_lock_irq(&mapping->tree_lock); |
| 446 | error = radix_tree_insert(&mapping->page_tree, offset, page); |
| 447 | if (!error) { |
| 448 | page_cache_get(page); |
| 449 | SetPageLocked(page); |
| 450 | page->mapping = mapping; |
| 451 | page->index = offset; |
| 452 | mapping->nrpages++; |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 453 | __inc_zone_page_state(page, NR_FILE_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | write_unlock_irq(&mapping->tree_lock); |
| 456 | radix_tree_preload_end(); |
| 457 | } |
| 458 | return error; |
| 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | EXPORT_SYMBOL(add_to_page_cache); |
| 461 | |
| 462 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 463 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
| 465 | int ret = add_to_page_cache(page, mapping, offset, gfp_mask); |
| 466 | if (ret == 0) |
| 467 | lru_cache_add(page); |
| 468 | return ret; |
| 469 | } |
| 470 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 471 | #ifdef CONFIG_NUMA |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 472 | struct page *__page_cache_alloc(gfp_t gfp) |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 473 | { |
| 474 | if (cpuset_do_page_mem_spread()) { |
| 475 | int n = cpuset_mem_spread_node(); |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 476 | return alloc_pages_node(n, gfp, 0); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 477 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 478 | return alloc_pages(gfp, 0); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 479 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 480 | EXPORT_SYMBOL(__page_cache_alloc); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 481 | #endif |
| 482 | |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 483 | static int __sleep_on_page_lock(void *word) |
| 484 | { |
| 485 | io_schedule(); |
| 486 | return 0; |
| 487 | } |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /* |
| 490 | * In order to wait for pages to become available there must be |
| 491 | * waitqueues associated with pages. By using a hash table of |
| 492 | * waitqueues where the bucket discipline is to maintain all |
| 493 | * waiters on the same queue and wake all when any of the pages |
| 494 | * become available, and for the woken contexts to check to be |
| 495 | * sure the appropriate page became available, this saves space |
| 496 | * at a cost of "thundering herd" phenomena during rare hash |
| 497 | * collisions. |
| 498 | */ |
| 499 | static wait_queue_head_t *page_waitqueue(struct page *page) |
| 500 | { |
| 501 | const struct zone *zone = page_zone(page); |
| 502 | |
| 503 | return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)]; |
| 504 | } |
| 505 | |
| 506 | static inline void wake_up_page(struct page *page, int bit) |
| 507 | { |
| 508 | __wake_up_bit(page_waitqueue(page), &page->flags, bit); |
| 509 | } |
| 510 | |
| 511 | void fastcall wait_on_page_bit(struct page *page, int bit_nr) |
| 512 | { |
| 513 | DEFINE_WAIT_BIT(wait, &page->flags, bit_nr); |
| 514 | |
| 515 | if (test_bit(bit_nr, &page->flags)) |
| 516 | __wait_on_bit(page_waitqueue(page), &wait, sync_page, |
| 517 | TASK_UNINTERRUPTIBLE); |
| 518 | } |
| 519 | EXPORT_SYMBOL(wait_on_page_bit); |
| 520 | |
| 521 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 522 | * unlock_page - unlock a locked page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | * @page: the page |
| 524 | * |
| 525 | * Unlocks the page and wakes up sleepers in ___wait_on_page_locked(). |
| 526 | * Also wakes sleepers in wait_on_page_writeback() because the wakeup |
| 527 | * mechananism between PageLocked pages and PageWriteback pages is shared. |
| 528 | * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. |
| 529 | * |
| 530 | * The first mb is necessary to safely close the critical section opened by the |
| 531 | * TestSetPageLocked(), the second mb is necessary to enforce ordering between |
| 532 | * the clear_bit and the read of the waitqueue (to avoid SMP races with a |
| 533 | * parallel wait_on_page_locked()). |
| 534 | */ |
| 535 | void fastcall unlock_page(struct page *page) |
| 536 | { |
| 537 | smp_mb__before_clear_bit(); |
| 538 | if (!TestClearPageLocked(page)) |
| 539 | BUG(); |
| 540 | smp_mb__after_clear_bit(); |
| 541 | wake_up_page(page, PG_locked); |
| 542 | } |
| 543 | EXPORT_SYMBOL(unlock_page); |
| 544 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 545 | /** |
| 546 | * end_page_writeback - end writeback against a page |
| 547 | * @page: the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | */ |
| 549 | void end_page_writeback(struct page *page) |
| 550 | { |
| 551 | if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) { |
| 552 | if (!test_clear_page_writeback(page)) |
| 553 | BUG(); |
| 554 | } |
| 555 | smp_mb__after_clear_bit(); |
| 556 | wake_up_page(page, PG_writeback); |
| 557 | } |
| 558 | EXPORT_SYMBOL(end_page_writeback); |
| 559 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 560 | /** |
| 561 | * __lock_page - get a lock on the page, assuming we need to sleep to get it |
| 562 | * @page: the page to lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 564 | * Ugly. Running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | * random driver's requestfn sets TASK_RUNNING, we could busywait. However |
| 566 | * chances are that on the second loop, the block layer's plug list is empty, |
| 567 | * so sync_page() will then return in state TASK_UNINTERRUPTIBLE. |
| 568 | */ |
| 569 | void fastcall __lock_page(struct page *page) |
| 570 | { |
| 571 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); |
| 572 | |
| 573 | __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page, |
| 574 | TASK_UNINTERRUPTIBLE); |
| 575 | } |
| 576 | EXPORT_SYMBOL(__lock_page); |
| 577 | |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 578 | /* |
| 579 | * Variant of lock_page that does not require the caller to hold a reference |
| 580 | * on the page's mapping. |
| 581 | */ |
| 582 | void fastcall __lock_page_nosync(struct page *page) |
| 583 | { |
| 584 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); |
| 585 | __wait_on_bit_lock(page_waitqueue(page), &wait, __sleep_on_page_lock, |
| 586 | TASK_UNINTERRUPTIBLE); |
| 587 | } |
| 588 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 589 | /** |
| 590 | * find_get_page - find and get a page reference |
| 591 | * @mapping: the address_space to search |
| 592 | * @offset: the page index |
| 593 | * |
Nick Piggin | da6052f | 2006-09-25 23:31:35 -0700 | [diff] [blame] | 594 | * Is there a pagecache struct page at the given (mapping, offset) tuple? |
| 595 | * If yes, increment its refcount and return it; if no, return NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | */ |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 597 | struct page * find_get_page(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | { |
| 599 | struct page *page; |
| 600 | |
| 601 | read_lock_irq(&mapping->tree_lock); |
| 602 | page = radix_tree_lookup(&mapping->page_tree, offset); |
| 603 | if (page) |
| 604 | page_cache_get(page); |
| 605 | read_unlock_irq(&mapping->tree_lock); |
| 606 | return page; |
| 607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | EXPORT_SYMBOL(find_get_page); |
| 609 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 610 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | * find_lock_page - locate, pin and lock a pagecache page |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 612 | * @mapping: the address_space to search |
| 613 | * @offset: the page index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * |
| 615 | * Locates the desired pagecache page, locks it, increments its reference |
| 616 | * count and returns its address. |
| 617 | * |
| 618 | * Returns zero if the page was not present. find_lock_page() may sleep. |
| 619 | */ |
| 620 | struct page *find_lock_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 621 | pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
| 623 | struct page *page; |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | repeat: |
Nick Piggin | 45726cb | 2007-10-16 01:24:41 -0700 | [diff] [blame] | 626 | read_lock_irq(&mapping->tree_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | page = radix_tree_lookup(&mapping->page_tree, offset); |
| 628 | if (page) { |
| 629 | page_cache_get(page); |
| 630 | if (TestSetPageLocked(page)) { |
| 631 | read_unlock_irq(&mapping->tree_lock); |
Nikita Danilov | bbfbb7c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 632 | __lock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | /* Has the page been truncated while we slept? */ |
Nick Piggin | 45726cb | 2007-10-16 01:24:41 -0700 | [diff] [blame] | 635 | if (unlikely(page->mapping != mapping)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | unlock_page(page); |
| 637 | page_cache_release(page); |
| 638 | goto repeat; |
| 639 | } |
Nick Piggin | 45726cb | 2007-10-16 01:24:41 -0700 | [diff] [blame] | 640 | VM_BUG_ON(page->index != offset); |
| 641 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | read_unlock_irq(&mapping->tree_lock); |
Nick Piggin | 45726cb | 2007-10-16 01:24:41 -0700 | [diff] [blame] | 645 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return page; |
| 647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | EXPORT_SYMBOL(find_lock_page); |
| 649 | |
| 650 | /** |
| 651 | * find_or_create_page - locate or add a pagecache page |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 652 | * @mapping: the page's address_space |
| 653 | * @index: the page's index into the mapping |
| 654 | * @gfp_mask: page allocation mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | * |
| 656 | * Locates a page in the pagecache. If the page is not present, a new page |
| 657 | * is allocated using @gfp_mask and is added to the pagecache and to the VM's |
| 658 | * LRU list. The returned page is locked and has its reference count |
| 659 | * incremented. |
| 660 | * |
| 661 | * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic |
| 662 | * allocation! |
| 663 | * |
| 664 | * find_or_create_page() returns the desired page's address, or zero on |
| 665 | * memory exhaustion. |
| 666 | */ |
| 667 | struct page *find_or_create_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 668 | pgoff_t index, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 670 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | int err; |
| 672 | repeat: |
| 673 | page = find_lock_page(mapping, index); |
| 674 | if (!page) { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 675 | page = __page_cache_alloc(gfp_mask); |
| 676 | if (!page) |
| 677 | return NULL; |
| 678 | err = add_to_page_cache_lru(page, mapping, index, gfp_mask); |
| 679 | if (unlikely(err)) { |
| 680 | page_cache_release(page); |
| 681 | page = NULL; |
| 682 | if (err == -EEXIST) |
| 683 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return page; |
| 687 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | EXPORT_SYMBOL(find_or_create_page); |
| 689 | |
| 690 | /** |
| 691 | * find_get_pages - gang pagecache lookup |
| 692 | * @mapping: The address_space to search |
| 693 | * @start: The starting page index |
| 694 | * @nr_pages: The maximum number of pages |
| 695 | * @pages: Where the resulting pages are placed |
| 696 | * |
| 697 | * find_get_pages() will search for and return a group of up to |
| 698 | * @nr_pages pages in the mapping. The pages are placed at @pages. |
| 699 | * find_get_pages() takes a reference against the returned pages. |
| 700 | * |
| 701 | * The search returns a group of mapping-contiguous pages with ascending |
| 702 | * indexes. There may be holes in the indices due to not-present pages. |
| 703 | * |
| 704 | * find_get_pages() returns the number of pages which were found. |
| 705 | */ |
| 706 | unsigned find_get_pages(struct address_space *mapping, pgoff_t start, |
| 707 | unsigned int nr_pages, struct page **pages) |
| 708 | { |
| 709 | unsigned int i; |
| 710 | unsigned int ret; |
| 711 | |
| 712 | read_lock_irq(&mapping->tree_lock); |
| 713 | ret = radix_tree_gang_lookup(&mapping->page_tree, |
| 714 | (void **)pages, start, nr_pages); |
| 715 | for (i = 0; i < ret; i++) |
| 716 | page_cache_get(pages[i]); |
| 717 | read_unlock_irq(&mapping->tree_lock); |
| 718 | return ret; |
| 719 | } |
| 720 | |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 721 | /** |
| 722 | * find_get_pages_contig - gang contiguous pagecache lookup |
| 723 | * @mapping: The address_space to search |
| 724 | * @index: The starting page index |
| 725 | * @nr_pages: The maximum number of pages |
| 726 | * @pages: Where the resulting pages are placed |
| 727 | * |
| 728 | * find_get_pages_contig() works exactly like find_get_pages(), except |
| 729 | * that the returned number of pages are guaranteed to be contiguous. |
| 730 | * |
| 731 | * find_get_pages_contig() returns the number of pages which were found. |
| 732 | */ |
| 733 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, |
| 734 | unsigned int nr_pages, struct page **pages) |
| 735 | { |
| 736 | unsigned int i; |
| 737 | unsigned int ret; |
| 738 | |
| 739 | read_lock_irq(&mapping->tree_lock); |
| 740 | ret = radix_tree_gang_lookup(&mapping->page_tree, |
| 741 | (void **)pages, index, nr_pages); |
| 742 | for (i = 0; i < ret; i++) { |
| 743 | if (pages[i]->mapping == NULL || pages[i]->index != index) |
| 744 | break; |
| 745 | |
| 746 | page_cache_get(pages[i]); |
| 747 | index++; |
| 748 | } |
| 749 | read_unlock_irq(&mapping->tree_lock); |
| 750 | return i; |
| 751 | } |
David Howells | ef71c15 | 2007-05-09 02:33:44 -0700 | [diff] [blame] | 752 | EXPORT_SYMBOL(find_get_pages_contig); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 753 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 754 | /** |
| 755 | * find_get_pages_tag - find and return pages that match @tag |
| 756 | * @mapping: the address_space to search |
| 757 | * @index: the starting page index |
| 758 | * @tag: the tag index |
| 759 | * @nr_pages: the maximum number of pages |
| 760 | * @pages: where the resulting pages are placed |
| 761 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | * Like find_get_pages, except we only return pages which are tagged with |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 763 | * @tag. We update @index to index the next page for the traversal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | */ |
| 765 | unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, |
| 766 | int tag, unsigned int nr_pages, struct page **pages) |
| 767 | { |
| 768 | unsigned int i; |
| 769 | unsigned int ret; |
| 770 | |
| 771 | read_lock_irq(&mapping->tree_lock); |
| 772 | ret = radix_tree_gang_lookup_tag(&mapping->page_tree, |
| 773 | (void **)pages, *index, nr_pages, tag); |
| 774 | for (i = 0; i < ret; i++) |
| 775 | page_cache_get(pages[i]); |
| 776 | if (ret) |
| 777 | *index = pages[ret - 1]->index + 1; |
| 778 | read_unlock_irq(&mapping->tree_lock); |
| 779 | return ret; |
| 780 | } |
David Howells | ef71c15 | 2007-05-09 02:33:44 -0700 | [diff] [blame] | 781 | EXPORT_SYMBOL(find_get_pages_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 783 | /** |
| 784 | * grab_cache_page_nowait - returns locked page at given index in given cache |
| 785 | * @mapping: target address_space |
| 786 | * @index: the page index |
| 787 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 788 | * Same as grab_cache_page(), but do not wait if the page is unavailable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | * This is intended for speculative data generators, where the data can |
| 790 | * be regenerated if the page couldn't be grabbed. This routine should |
| 791 | * be safe to call while holding the lock for another page. |
| 792 | * |
| 793 | * Clear __GFP_FS when allocating the page to avoid recursion into the fs |
| 794 | * and deadlock against the caller's locked page. |
| 795 | */ |
| 796 | struct page * |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 797 | grab_cache_page_nowait(struct address_space *mapping, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | { |
| 799 | struct page *page = find_get_page(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | if (page) { |
| 802 | if (!TestSetPageLocked(page)) |
| 803 | return page; |
| 804 | page_cache_release(page); |
| 805 | return NULL; |
| 806 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 807 | page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS); |
| 808 | if (page && add_to_page_cache_lru(page, mapping, index, GFP_KERNEL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | page_cache_release(page); |
| 810 | page = NULL; |
| 811 | } |
| 812 | return page; |
| 813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | EXPORT_SYMBOL(grab_cache_page_nowait); |
| 815 | |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 816 | /* |
| 817 | * CD/DVDs are error prone. When a medium error occurs, the driver may fail |
| 818 | * a _large_ part of the i/o request. Imagine the worst scenario: |
| 819 | * |
| 820 | * ---R__________________________________________B__________ |
| 821 | * ^ reading here ^ bad block(assume 4k) |
| 822 | * |
| 823 | * read(R) => miss => readahead(R...B) => media error => frustrating retries |
| 824 | * => failing the whole request => read(R) => read(R+1) => |
| 825 | * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) => |
| 826 | * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) => |
| 827 | * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ...... |
| 828 | * |
| 829 | * It is going insane. Fix it by quickly scaling down the readahead size. |
| 830 | */ |
| 831 | static void shrink_readahead_size_eio(struct file *filp, |
| 832 | struct file_ra_state *ra) |
| 833 | { |
| 834 | if (!ra->ra_pages) |
| 835 | return; |
| 836 | |
| 837 | ra->ra_pages /= 4; |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 840 | /** |
| 841 | * do_generic_mapping_read - generic file read routine |
| 842 | * @mapping: address_space to be read |
Stephen Hemminger | f0b85c0 | 2007-08-10 13:01:16 -0700 | [diff] [blame] | 843 | * @_ra: file's readahead state |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 844 | * @filp: the file to read |
| 845 | * @ppos: current file position |
| 846 | * @desc: read_descriptor |
| 847 | * @actor: read method |
| 848 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | * This is a generic file read routine, and uses the |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 850 | * mapping->a_ops->readpage() function for the actual low-level stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | * |
| 852 | * This is really ugly. But the goto's actually try to clarify some |
| 853 | * of the logic when it comes to error handling etc. |
| 854 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 855 | * Note the struct file* is only passed for the use of readpage. |
| 856 | * It may be NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | */ |
| 858 | void do_generic_mapping_read(struct address_space *mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 859 | struct file_ra_state *ra, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | struct file *filp, |
| 861 | loff_t *ppos, |
| 862 | read_descriptor_t *desc, |
| 863 | read_actor_t actor) |
| 864 | { |
| 865 | struct inode *inode = mapping->host; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 866 | pgoff_t index; |
| 867 | pgoff_t last_index; |
| 868 | pgoff_t prev_index; |
| 869 | unsigned long offset; /* offset into pagecache page */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 870 | unsigned int prev_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | index = *ppos >> PAGE_CACHE_SHIFT; |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 874 | prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT; |
| 875 | prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT; |
| 877 | offset = *ppos & ~PAGE_CACHE_MASK; |
| 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | for (;;) { |
| 880 | struct page *page; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 881 | pgoff_t end_index; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 882 | loff_t isize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | unsigned long nr, ret; |
| 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | find_page: |
| 887 | page = find_get_page(mapping, index); |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 888 | if (!page) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 889 | page_cache_sync_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 890 | ra, filp, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 891 | index, last_index - index); |
| 892 | page = find_get_page(mapping, index); |
| 893 | if (unlikely(page == NULL)) |
| 894 | goto no_cached_page; |
| 895 | } |
| 896 | if (PageReadahead(page)) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 897 | page_cache_async_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 898 | ra, filp, page, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 899 | index, last_index - index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | if (!PageUptodate(page)) |
| 902 | goto page_not_up_to_date; |
| 903 | page_ok: |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 904 | /* |
| 905 | * i_size must be checked after we know the page is Uptodate. |
| 906 | * |
| 907 | * Checking i_size after the check allows us to calculate |
| 908 | * the correct value for "nr", which means the zero-filled |
| 909 | * part of the page is not copied back to userspace (unless |
| 910 | * another truncate extends the file - this is desired though). |
| 911 | */ |
| 912 | |
| 913 | isize = i_size_read(inode); |
| 914 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 915 | if (unlikely(!isize || index > end_index)) { |
| 916 | page_cache_release(page); |
| 917 | goto out; |
| 918 | } |
| 919 | |
| 920 | /* nr is the maximum number of bytes to copy from this page */ |
| 921 | nr = PAGE_CACHE_SIZE; |
| 922 | if (index == end_index) { |
| 923 | nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 924 | if (nr <= offset) { |
| 925 | page_cache_release(page); |
| 926 | goto out; |
| 927 | } |
| 928 | } |
| 929 | nr = nr - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
| 931 | /* If users can be writing to this page using arbitrary |
| 932 | * virtual addresses, take care about potential aliasing |
| 933 | * before reading the page on the kernel side. |
| 934 | */ |
| 935 | if (mapping_writably_mapped(mapping)) |
| 936 | flush_dcache_page(page); |
| 937 | |
| 938 | /* |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 939 | * When a sequential read accesses a page several times, |
| 940 | * only mark it as accessed the first time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 942 | if (prev_index != index || offset != prev_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | mark_page_accessed(page); |
| 944 | prev_index = index; |
| 945 | |
| 946 | /* |
| 947 | * Ok, we have the page, and it's up-to-date, so |
| 948 | * now we can copy it to user space... |
| 949 | * |
| 950 | * The actor routine returns how many bytes were actually used.. |
| 951 | * NOTE! This may not be the same as how much of a user buffer |
| 952 | * we filled up (we may be padding etc), so we can only update |
| 953 | * "pos" here (the actor routine has to update the user buffer |
| 954 | * pointers and the remaining count). |
| 955 | */ |
| 956 | ret = actor(desc, page, offset, nr); |
| 957 | offset += ret; |
| 958 | index += offset >> PAGE_CACHE_SHIFT; |
| 959 | offset &= ~PAGE_CACHE_MASK; |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 960 | prev_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | page_cache_release(page); |
| 963 | if (ret == nr && desc->count) |
| 964 | continue; |
| 965 | goto out; |
| 966 | |
| 967 | page_not_up_to_date: |
| 968 | /* Get exclusive access to the page ... */ |
| 969 | lock_page(page); |
| 970 | |
Nick Piggin | da6052f | 2006-09-25 23:31:35 -0700 | [diff] [blame] | 971 | /* Did it get truncated before we got the lock? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | if (!page->mapping) { |
| 973 | unlock_page(page); |
| 974 | page_cache_release(page); |
| 975 | continue; |
| 976 | } |
| 977 | |
| 978 | /* Did somebody else fill it already? */ |
| 979 | if (PageUptodate(page)) { |
| 980 | unlock_page(page); |
| 981 | goto page_ok; |
| 982 | } |
| 983 | |
| 984 | readpage: |
| 985 | /* Start the actual read. The read will unlock the page. */ |
| 986 | error = mapping->a_ops->readpage(filp, page); |
| 987 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 988 | if (unlikely(error)) { |
| 989 | if (error == AOP_TRUNCATED_PAGE) { |
| 990 | page_cache_release(page); |
| 991 | goto find_page; |
| 992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | goto readpage_error; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 994 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | if (!PageUptodate(page)) { |
| 997 | lock_page(page); |
| 998 | if (!PageUptodate(page)) { |
| 999 | if (page->mapping == NULL) { |
| 1000 | /* |
| 1001 | * invalidate_inode_pages got it |
| 1002 | */ |
| 1003 | unlock_page(page); |
| 1004 | page_cache_release(page); |
| 1005 | goto find_page; |
| 1006 | } |
| 1007 | unlock_page(page); |
| 1008 | error = -EIO; |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1009 | shrink_readahead_size_eio(filp, ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | goto readpage_error; |
| 1011 | } |
| 1012 | unlock_page(page); |
| 1013 | } |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | goto page_ok; |
| 1016 | |
| 1017 | readpage_error: |
| 1018 | /* UHHUH! A synchronous read error occurred. Report it */ |
| 1019 | desc->error = error; |
| 1020 | page_cache_release(page); |
| 1021 | goto out; |
| 1022 | |
| 1023 | no_cached_page: |
| 1024 | /* |
| 1025 | * Ok, it wasn't cached, so we need to create a new |
| 1026 | * page.. |
| 1027 | */ |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1028 | page = page_cache_alloc_cold(mapping); |
| 1029 | if (!page) { |
| 1030 | desc->error = -ENOMEM; |
| 1031 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1033 | error = add_to_page_cache_lru(page, mapping, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | index, GFP_KERNEL); |
| 1035 | if (error) { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1036 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | if (error == -EEXIST) |
| 1038 | goto find_page; |
| 1039 | desc->error = error; |
| 1040 | goto out; |
| 1041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | goto readpage; |
| 1043 | } |
| 1044 | |
| 1045 | out: |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1046 | ra->prev_pos = prev_index; |
| 1047 | ra->prev_pos <<= PAGE_CACHE_SHIFT; |
| 1048 | ra->prev_pos |= prev_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
Fengguang Wu | f4e6b49 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 1050 | *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | if (filp) |
| 1052 | file_accessed(filp); |
| 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | EXPORT_SYMBOL(do_generic_mapping_read); |
| 1055 | |
| 1056 | int file_read_actor(read_descriptor_t *desc, struct page *page, |
| 1057 | unsigned long offset, unsigned long size) |
| 1058 | { |
| 1059 | char *kaddr; |
| 1060 | unsigned long left, count = desc->count; |
| 1061 | |
| 1062 | if (size > count) |
| 1063 | size = count; |
| 1064 | |
| 1065 | /* |
| 1066 | * Faults on the destination of a read are common, so do it before |
| 1067 | * taking the kmap. |
| 1068 | */ |
| 1069 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 1070 | kaddr = kmap_atomic(page, KM_USER0); |
| 1071 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 1072 | kaddr + offset, size); |
| 1073 | kunmap_atomic(kaddr, KM_USER0); |
| 1074 | if (left == 0) |
| 1075 | goto success; |
| 1076 | } |
| 1077 | |
| 1078 | /* Do it the slow way */ |
| 1079 | kaddr = kmap(page); |
| 1080 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 1081 | kunmap(page); |
| 1082 | |
| 1083 | if (left) { |
| 1084 | size -= left; |
| 1085 | desc->error = -EFAULT; |
| 1086 | } |
| 1087 | success: |
| 1088 | desc->count = count - size; |
| 1089 | desc->written += size; |
| 1090 | desc->arg.buf += size; |
| 1091 | return size; |
| 1092 | } |
| 1093 | |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 1094 | /* |
| 1095 | * Performs necessary checks before doing a write |
| 1096 | * @iov: io vector request |
| 1097 | * @nr_segs: number of segments in the iovec |
| 1098 | * @count: number of bytes to write |
| 1099 | * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE |
| 1100 | * |
| 1101 | * Adjust number of segments and amount of bytes to write (nr_segs should be |
| 1102 | * properly initialized first). Returns appropriate error code that caller |
| 1103 | * should return or zero in case that write should be allowed. |
| 1104 | */ |
| 1105 | int generic_segment_checks(const struct iovec *iov, |
| 1106 | unsigned long *nr_segs, size_t *count, int access_flags) |
| 1107 | { |
| 1108 | unsigned long seg; |
| 1109 | size_t cnt = 0; |
| 1110 | for (seg = 0; seg < *nr_segs; seg++) { |
| 1111 | const struct iovec *iv = &iov[seg]; |
| 1112 | |
| 1113 | /* |
| 1114 | * If any segment has a negative length, or the cumulative |
| 1115 | * length ever wraps negative then return -EINVAL. |
| 1116 | */ |
| 1117 | cnt += iv->iov_len; |
| 1118 | if (unlikely((ssize_t)(cnt|iv->iov_len) < 0)) |
| 1119 | return -EINVAL; |
| 1120 | if (access_ok(access_flags, iv->iov_base, iv->iov_len)) |
| 1121 | continue; |
| 1122 | if (seg == 0) |
| 1123 | return -EFAULT; |
| 1124 | *nr_segs = seg; |
| 1125 | cnt -= iv->iov_len; /* This segment is no good */ |
| 1126 | break; |
| 1127 | } |
| 1128 | *count = cnt; |
| 1129 | return 0; |
| 1130 | } |
| 1131 | EXPORT_SYMBOL(generic_segment_checks); |
| 1132 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1133 | /** |
Henrik Kretzschmar | b2abacf | 2006-10-04 02:15:22 -0700 | [diff] [blame] | 1134 | * generic_file_aio_read - generic filesystem read routine |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1135 | * @iocb: kernel I/O control block |
| 1136 | * @iov: io vector request |
| 1137 | * @nr_segs: number of segments in the iovec |
Henrik Kretzschmar | b2abacf | 2006-10-04 02:15:22 -0700 | [diff] [blame] | 1138 | * @pos: current file position |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1139 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | * This is the "read()" routine for all filesystems |
| 1141 | * that can use the page cache directly. |
| 1142 | */ |
| 1143 | ssize_t |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1144 | generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 1145 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | { |
| 1147 | struct file *filp = iocb->ki_filp; |
| 1148 | ssize_t retval; |
| 1149 | unsigned long seg; |
| 1150 | size_t count; |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1151 | loff_t *ppos = &iocb->ki_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
| 1153 | count = 0; |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 1154 | retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); |
| 1155 | if (retval) |
| 1156 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 1159 | if (filp->f_flags & O_DIRECT) { |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1160 | loff_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | struct address_space *mapping; |
| 1162 | struct inode *inode; |
| 1163 | |
| 1164 | mapping = filp->f_mapping; |
| 1165 | inode = mapping->host; |
| 1166 | retval = 0; |
| 1167 | if (!count) |
| 1168 | goto out; /* skip atime */ |
| 1169 | size = i_size_read(inode); |
| 1170 | if (pos < size) { |
| 1171 | retval = generic_file_direct_IO(READ, iocb, |
| 1172 | iov, pos, nr_segs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | if (retval > 0) |
| 1174 | *ppos = pos + retval; |
| 1175 | } |
Steven Whitehouse | 0e0bcae | 2006-09-27 14:45:07 -0400 | [diff] [blame] | 1176 | if (likely(retval != 0)) { |
Steven Whitehouse | 3f1a9aa | 2006-09-27 14:52:48 -0400 | [diff] [blame] | 1177 | file_accessed(filp); |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 1178 | goto out; |
Steven Whitehouse | 0e0bcae | 2006-09-27 14:45:07 -0400 | [diff] [blame] | 1179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | retval = 0; |
| 1183 | if (count) { |
| 1184 | for (seg = 0; seg < nr_segs; seg++) { |
| 1185 | read_descriptor_t desc; |
| 1186 | |
| 1187 | desc.written = 0; |
| 1188 | desc.arg.buf = iov[seg].iov_base; |
| 1189 | desc.count = iov[seg].iov_len; |
| 1190 | if (desc.count == 0) |
| 1191 | continue; |
| 1192 | desc.error = 0; |
| 1193 | do_generic_file_read(filp,ppos,&desc,file_read_actor); |
| 1194 | retval += desc.written; |
Tejun Heo | 39e88ca | 2005-10-30 15:02:40 -0800 | [diff] [blame] | 1195 | if (desc.error) { |
| 1196 | retval = retval ?: desc.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | break; |
| 1198 | } |
akpm@linux-foundation.org | c44939e | 2007-07-15 23:38:25 -0700 | [diff] [blame] | 1199 | if (desc.count > 0) |
| 1200 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | out: |
| 1204 | return retval; |
| 1205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | EXPORT_SYMBOL(generic_file_aio_read); |
| 1207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | static ssize_t |
| 1209 | do_readahead(struct address_space *mapping, struct file *filp, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1210 | pgoff_t index, unsigned long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | { |
| 1212 | if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | force_page_cache_readahead(mapping, filp, index, |
| 1216 | max_sane_readahead(nr)); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count) |
| 1221 | { |
| 1222 | ssize_t ret; |
| 1223 | struct file *file; |
| 1224 | |
| 1225 | ret = -EBADF; |
| 1226 | file = fget(fd); |
| 1227 | if (file) { |
| 1228 | if (file->f_mode & FMODE_READ) { |
| 1229 | struct address_space *mapping = file->f_mapping; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1230 | pgoff_t start = offset >> PAGE_CACHE_SHIFT; |
| 1231 | pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | unsigned long len = end - start + 1; |
| 1233 | ret = do_readahead(mapping, file, start, len); |
| 1234 | } |
| 1235 | fput(file); |
| 1236 | } |
| 1237 | return ret; |
| 1238 | } |
| 1239 | |
| 1240 | #ifdef CONFIG_MMU |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1241 | /** |
| 1242 | * page_cache_read - adds requested page to the page cache if not already there |
| 1243 | * @file: file to read |
| 1244 | * @offset: page index |
| 1245 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | * This adds the requested page to the page cache if it isn't already there, |
| 1247 | * and schedules an I/O to read in its contents from disk. |
| 1248 | */ |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1249 | static int fastcall page_cache_read(struct file * file, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | { |
| 1251 | struct address_space *mapping = file->f_mapping; |
| 1252 | struct page *page; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1253 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1255 | do { |
| 1256 | page = page_cache_alloc_cold(mapping); |
| 1257 | if (!page) |
| 1258 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1260 | ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL); |
| 1261 | if (ret == 0) |
| 1262 | ret = mapping->a_ops->readpage(file, page); |
| 1263 | else if (ret == -EEXIST) |
| 1264 | ret = 0; /* losing race to add is OK */ |
| 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1268 | } while (ret == AOP_TRUNCATED_PAGE); |
| 1269 | |
| 1270 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | #define MMAP_LOTSAMISS (100) |
| 1274 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1275 | /** |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1276 | * filemap_fault - read in file data for page fault handling |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1277 | * @vma: vma in which the fault was taken |
| 1278 | * @vmf: struct vm_fault containing details of the fault |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1279 | * |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1280 | * filemap_fault() is invoked via the vma operations vector for a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | * mapped memory region to read in file data during a page fault. |
| 1282 | * |
| 1283 | * The goto's are kind of ugly, but this streamlines the normal case of having |
| 1284 | * it in the page cache, and handles the special cases reasonably without |
| 1285 | * having a lot of duplicated code. |
| 1286 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1287 | int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
| 1289 | int error; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1290 | struct file *file = vma->vm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | struct address_space *mapping = file->f_mapping; |
| 1292 | struct file_ra_state *ra = &file->f_ra; |
| 1293 | struct inode *inode = mapping->host; |
| 1294 | struct page *page; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1295 | unsigned long size; |
| 1296 | int did_readaround = 0; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1297 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1300 | if (vmf->pgoff >= size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | goto outside_data_content; |
| 1302 | |
| 1303 | /* If we don't want any read-ahead, don't bother */ |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1304 | if (VM_RandomReadHint(vma)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | goto no_cached_page; |
| 1306 | |
| 1307 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | * Do we have something in the page cache already? |
| 1309 | */ |
| 1310 | retry_find: |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1311 | page = find_lock_page(mapping, vmf->pgoff); |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 1312 | /* |
| 1313 | * For sequential accesses, we use the generic readahead logic. |
| 1314 | */ |
| 1315 | if (VM_SequentialReadHint(vma)) { |
| 1316 | if (!page) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 1317 | page_cache_sync_readahead(mapping, ra, file, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 1318 | vmf->pgoff, 1); |
| 1319 | page = find_lock_page(mapping, vmf->pgoff); |
| 1320 | if (!page) |
| 1321 | goto no_cached_page; |
| 1322 | } |
| 1323 | if (PageReadahead(page)) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 1324 | page_cache_async_readahead(mapping, ra, file, page, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 1325 | vmf->pgoff, 1); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | if (!page) { |
| 1330 | unsigned long ra_pages; |
| 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | ra->mmap_miss++; |
| 1333 | |
| 1334 | /* |
| 1335 | * Do we miss much more than hit in this file? If so, |
| 1336 | * stop bothering with read-ahead. It will only hurt. |
| 1337 | */ |
Fengguang Wu | 0bb7ba6 | 2007-10-16 01:24:32 -0700 | [diff] [blame] | 1338 | if (ra->mmap_miss > MMAP_LOTSAMISS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | goto no_cached_page; |
| 1340 | |
| 1341 | /* |
| 1342 | * To keep the pgmajfault counter straight, we need to |
| 1343 | * check did_readaround, as this is an inner loop. |
| 1344 | */ |
| 1345 | if (!did_readaround) { |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1346 | ret = VM_FAULT_MAJOR; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 1347 | count_vm_event(PGMAJFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
| 1349 | did_readaround = 1; |
| 1350 | ra_pages = max_sane_readahead(file->f_ra.ra_pages); |
| 1351 | if (ra_pages) { |
| 1352 | pgoff_t start = 0; |
| 1353 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1354 | if (vmf->pgoff > ra_pages / 2) |
| 1355 | start = vmf->pgoff - ra_pages / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | do_page_cache_readahead(mapping, file, start, ra_pages); |
| 1357 | } |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1358 | page = find_lock_page(mapping, vmf->pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | if (!page) |
| 1360 | goto no_cached_page; |
| 1361 | } |
| 1362 | |
| 1363 | if (!did_readaround) |
Fengguang Wu | 0bb7ba6 | 2007-10-16 01:24:32 -0700 | [diff] [blame] | 1364 | ra->mmap_miss--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1367 | * We have a locked page in the page cache, now we need to check |
| 1368 | * that it's up-to-date. If not, it is going to be due to an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | */ |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1370 | if (unlikely(!PageUptodate(page))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | goto page_not_uptodate; |
| 1372 | |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1373 | /* Must recheck i_size under page lock */ |
| 1374 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1375 | if (unlikely(vmf->pgoff >= size)) { |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1376 | unlock_page(page); |
Yan Zheng | 745ad48 | 2007-10-08 10:08:37 -0700 | [diff] [blame] | 1377 | page_cache_release(page); |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1378 | goto outside_data_content; |
| 1379 | } |
| 1380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | /* |
| 1382 | * Found the page and have a reference on it. |
| 1383 | */ |
| 1384 | mark_page_accessed(page); |
Fengguang Wu | f4e6b49 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 1385 | ra->prev_pos = (loff_t)page->index << PAGE_CACHE_SHIFT; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1386 | vmf->page = page; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1387 | return ret | VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
| 1389 | outside_data_content: |
| 1390 | /* |
| 1391 | * An external ptracer can access pages that normally aren't |
| 1392 | * accessible.. |
| 1393 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1394 | if (vma->vm_mm == current->mm) |
| 1395 | return VM_FAULT_SIGBUS; |
| 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | /* Fall through to the non-read-ahead case */ |
| 1398 | no_cached_page: |
| 1399 | /* |
| 1400 | * We're only likely to ever get here if MADV_RANDOM is in |
| 1401 | * effect. |
| 1402 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1403 | error = page_cache_read(file, vmf->pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | /* |
| 1406 | * The page we want has now been added to the page cache. |
| 1407 | * In the unlikely event that someone removed it in the |
| 1408 | * meantime, we'll just come back here and read it again. |
| 1409 | */ |
| 1410 | if (error >= 0) |
| 1411 | goto retry_find; |
| 1412 | |
| 1413 | /* |
| 1414 | * An error return from page_cache_read can result if the |
| 1415 | * system is low on memory, or a problem occurs while trying |
| 1416 | * to schedule I/O. |
| 1417 | */ |
| 1418 | if (error == -ENOMEM) |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1419 | return VM_FAULT_OOM; |
| 1420 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | page_not_uptodate: |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1423 | /* IO error path */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | if (!did_readaround) { |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1425 | ret = VM_FAULT_MAJOR; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 1426 | count_vm_event(PGMAJFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 1430 | * Umm, take care of errors if the page isn't up-to-date. |
| 1431 | * Try to re-read it _once_. We do this synchronously, |
| 1432 | * because there really aren't any performance issues here |
| 1433 | * and we need to check for errors. |
| 1434 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | ClearPageError(page); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1436 | error = mapping->a_ops->readpage(file, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | page_cache_release(page); |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1438 | |
| 1439 | if (!error || error == AOP_TRUNCATED_PAGE) |
| 1440 | goto retry_find; |
| 1441 | |
| 1442 | /* Things didn't work out. Return zero to tell the mm layer so. */ |
| 1443 | shrink_readahead_size_eio(file, ra); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1444 | return VM_FAULT_SIGBUS; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1445 | } |
| 1446 | EXPORT_SYMBOL(filemap_fault); |
| 1447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | struct vm_operations_struct generic_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1449 | .fault = filemap_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | }; |
| 1451 | |
| 1452 | /* This is used for a general mmap of a disk file */ |
| 1453 | |
| 1454 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 1455 | { |
| 1456 | struct address_space *mapping = file->f_mapping; |
| 1457 | |
| 1458 | if (!mapping->a_ops->readpage) |
| 1459 | return -ENOEXEC; |
| 1460 | file_accessed(file); |
| 1461 | vma->vm_ops = &generic_file_vm_ops; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1462 | vma->vm_flags |= VM_CAN_NONLINEAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | |
| 1466 | /* |
| 1467 | * This is for filesystems which do not implement ->writepage. |
| 1468 | */ |
| 1469 | int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) |
| 1470 | { |
| 1471 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 1472 | return -EINVAL; |
| 1473 | return generic_file_mmap(file, vma); |
| 1474 | } |
| 1475 | #else |
| 1476 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 1477 | { |
| 1478 | return -ENOSYS; |
| 1479 | } |
| 1480 | int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma) |
| 1481 | { |
| 1482 | return -ENOSYS; |
| 1483 | } |
| 1484 | #endif /* CONFIG_MMU */ |
| 1485 | |
| 1486 | EXPORT_SYMBOL(generic_file_mmap); |
| 1487 | EXPORT_SYMBOL(generic_file_readonly_mmap); |
| 1488 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 1489 | static struct page *__read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1490 | pgoff_t index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | int (*filler)(void *,struct page*), |
| 1492 | void *data) |
| 1493 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1494 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | int err; |
| 1496 | repeat: |
| 1497 | page = find_get_page(mapping, index); |
| 1498 | if (!page) { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1499 | page = page_cache_alloc_cold(mapping); |
| 1500 | if (!page) |
| 1501 | return ERR_PTR(-ENOMEM); |
| 1502 | err = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL); |
| 1503 | if (unlikely(err)) { |
| 1504 | page_cache_release(page); |
| 1505 | if (err == -EEXIST) |
| 1506 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | /* Presumably ENOMEM for radix tree node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return ERR_PTR(err); |
| 1509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | err = filler(data, page); |
| 1511 | if (err < 0) { |
| 1512 | page_cache_release(page); |
| 1513 | page = ERR_PTR(err); |
| 1514 | } |
| 1515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | return page; |
| 1517 | } |
| 1518 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 1519 | /* |
| 1520 | * Same as read_cache_page, but don't wait for page to become unlocked |
| 1521 | * after submitting it to the filler. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | */ |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 1523 | struct page *read_cache_page_async(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1524 | pgoff_t index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | int (*filler)(void *,struct page*), |
| 1526 | void *data) |
| 1527 | { |
| 1528 | struct page *page; |
| 1529 | int err; |
| 1530 | |
| 1531 | retry: |
| 1532 | page = __read_cache_page(mapping, index, filler, data); |
| 1533 | if (IS_ERR(page)) |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 1534 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (PageUptodate(page)) |
| 1536 | goto out; |
| 1537 | |
| 1538 | lock_page(page); |
| 1539 | if (!page->mapping) { |
| 1540 | unlock_page(page); |
| 1541 | page_cache_release(page); |
| 1542 | goto retry; |
| 1543 | } |
| 1544 | if (PageUptodate(page)) { |
| 1545 | unlock_page(page); |
| 1546 | goto out; |
| 1547 | } |
| 1548 | err = filler(data, page); |
| 1549 | if (err < 0) { |
| 1550 | page_cache_release(page); |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 1551 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | } |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 1553 | out: |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 1554 | mark_page_accessed(page); |
| 1555 | return page; |
| 1556 | } |
| 1557 | EXPORT_SYMBOL(read_cache_page_async); |
| 1558 | |
| 1559 | /** |
| 1560 | * read_cache_page - read into page cache, fill it if needed |
| 1561 | * @mapping: the page's address_space |
| 1562 | * @index: the page index |
| 1563 | * @filler: function to perform the read |
| 1564 | * @data: destination for read data |
| 1565 | * |
| 1566 | * Read into the page cache. If a page already exists, and PageUptodate() is |
| 1567 | * not set, try to fill the page then wait for it to become unlocked. |
| 1568 | * |
| 1569 | * If the page does not get brought uptodate, return -EIO. |
| 1570 | */ |
| 1571 | struct page *read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1572 | pgoff_t index, |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 1573 | int (*filler)(void *,struct page*), |
| 1574 | void *data) |
| 1575 | { |
| 1576 | struct page *page; |
| 1577 | |
| 1578 | page = read_cache_page_async(mapping, index, filler, data); |
| 1579 | if (IS_ERR(page)) |
| 1580 | goto out; |
| 1581 | wait_on_page_locked(page); |
| 1582 | if (!PageUptodate(page)) { |
| 1583 | page_cache_release(page); |
| 1584 | page = ERR_PTR(-EIO); |
| 1585 | } |
| 1586 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | return page; |
| 1588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | EXPORT_SYMBOL(read_cache_page); |
| 1590 | |
| 1591 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | * The logic we want is |
| 1593 | * |
| 1594 | * if suid or (sgid and xgrp) |
| 1595 | * remove privs |
| 1596 | */ |
Jens Axboe | 01de85e | 2006-10-17 19:50:36 +0200 | [diff] [blame] | 1597 | int should_remove_suid(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | { |
| 1599 | mode_t mode = dentry->d_inode->i_mode; |
| 1600 | int kill = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
| 1602 | /* suid always must be killed */ |
| 1603 | if (unlikely(mode & S_ISUID)) |
| 1604 | kill = ATTR_KILL_SUID; |
| 1605 | |
| 1606 | /* |
| 1607 | * sgid without any exec bits is just a mandatory locking mark; leave |
| 1608 | * it alone. If some exec bits are set, it's a real sgid; kill it. |
| 1609 | */ |
| 1610 | if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) |
| 1611 | kill |= ATTR_KILL_SGID; |
| 1612 | |
Jens Axboe | 01de85e | 2006-10-17 19:50:36 +0200 | [diff] [blame] | 1613 | if (unlikely(kill && !capable(CAP_FSETID))) |
| 1614 | return kill; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
Jens Axboe | 01de85e | 2006-10-17 19:50:36 +0200 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
Mark Fasheh | d23a147 | 2006-10-17 17:05:18 -0700 | [diff] [blame] | 1618 | EXPORT_SYMBOL(should_remove_suid); |
Jens Axboe | 01de85e | 2006-10-17 19:50:36 +0200 | [diff] [blame] | 1619 | |
| 1620 | int __remove_suid(struct dentry *dentry, int kill) |
| 1621 | { |
| 1622 | struct iattr newattrs; |
| 1623 | |
| 1624 | newattrs.ia_valid = ATTR_FORCE | kill; |
| 1625 | return notify_change(dentry, &newattrs); |
| 1626 | } |
| 1627 | |
| 1628 | int remove_suid(struct dentry *dentry) |
| 1629 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 1630 | int killsuid = should_remove_suid(dentry); |
| 1631 | int killpriv = security_inode_need_killpriv(dentry); |
| 1632 | int error = 0; |
Jens Axboe | 01de85e | 2006-10-17 19:50:36 +0200 | [diff] [blame] | 1633 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 1634 | if (killpriv < 0) |
| 1635 | return killpriv; |
| 1636 | if (killpriv) |
| 1637 | error = security_inode_killpriv(dentry); |
| 1638 | if (!error && killsuid) |
| 1639 | error = __remove_suid(dentry, killsuid); |
Jens Axboe | 01de85e | 2006-10-17 19:50:36 +0200 | [diff] [blame] | 1640 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 1641 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | } |
| 1643 | EXPORT_SYMBOL(remove_suid); |
| 1644 | |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1645 | static size_t __iovec_copy_from_user_inatomic(char *vaddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | const struct iovec *iov, size_t base, size_t bytes) |
| 1647 | { |
| 1648 | size_t copied = 0, left = 0; |
| 1649 | |
| 1650 | while (bytes) { |
| 1651 | char __user *buf = iov->iov_base + base; |
| 1652 | int copy = min(bytes, iov->iov_len - base); |
| 1653 | |
| 1654 | base = 0; |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 1655 | left = __copy_from_user_inatomic_nocache(vaddr, buf, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | copied += copy; |
| 1657 | bytes -= copy; |
| 1658 | vaddr += copy; |
| 1659 | iov++; |
| 1660 | |
NeilBrown | 01408c4 | 2006-06-25 05:47:58 -0700 | [diff] [blame] | 1661 | if (unlikely(left)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | } |
| 1664 | return copied - left; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1668 | * Copy as much as we can into the page and return the number of bytes which |
| 1669 | * were sucessfully copied. If a fault is encountered then return the number of |
| 1670 | * bytes which were copied. |
| 1671 | */ |
| 1672 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
| 1673 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 1674 | { |
| 1675 | char *kaddr; |
| 1676 | size_t copied; |
| 1677 | |
| 1678 | BUG_ON(!in_atomic()); |
| 1679 | kaddr = kmap_atomic(page, KM_USER0); |
| 1680 | if (likely(i->nr_segs == 1)) { |
| 1681 | int left; |
| 1682 | char __user *buf = i->iov->iov_base + i->iov_offset; |
| 1683 | left = __copy_from_user_inatomic_nocache(kaddr + offset, |
| 1684 | buf, bytes); |
| 1685 | copied = bytes - left; |
| 1686 | } else { |
| 1687 | copied = __iovec_copy_from_user_inatomic(kaddr + offset, |
| 1688 | i->iov, i->iov_offset, bytes); |
| 1689 | } |
| 1690 | kunmap_atomic(kaddr, KM_USER0); |
| 1691 | |
| 1692 | return copied; |
| 1693 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 1694 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1695 | |
| 1696 | /* |
| 1697 | * This has the same sideeffects and return value as |
| 1698 | * iov_iter_copy_from_user_atomic(). |
| 1699 | * The difference is that it attempts to resolve faults. |
| 1700 | * Page must not be locked. |
| 1701 | */ |
| 1702 | size_t iov_iter_copy_from_user(struct page *page, |
| 1703 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 1704 | { |
| 1705 | char *kaddr; |
| 1706 | size_t copied; |
| 1707 | |
| 1708 | kaddr = kmap(page); |
| 1709 | if (likely(i->nr_segs == 1)) { |
| 1710 | int left; |
| 1711 | char __user *buf = i->iov->iov_base + i->iov_offset; |
| 1712 | left = __copy_from_user_nocache(kaddr + offset, buf, bytes); |
| 1713 | copied = bytes - left; |
| 1714 | } else { |
| 1715 | copied = __iovec_copy_from_user_inatomic(kaddr + offset, |
| 1716 | i->iov, i->iov_offset, bytes); |
| 1717 | } |
| 1718 | kunmap(page); |
| 1719 | return copied; |
| 1720 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 1721 | EXPORT_SYMBOL(iov_iter_copy_from_user); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1722 | |
| 1723 | static void __iov_iter_advance_iov(struct iov_iter *i, size_t bytes) |
| 1724 | { |
| 1725 | if (likely(i->nr_segs == 1)) { |
| 1726 | i->iov_offset += bytes; |
| 1727 | } else { |
| 1728 | const struct iovec *iov = i->iov; |
| 1729 | size_t base = i->iov_offset; |
| 1730 | |
| 1731 | while (bytes) { |
| 1732 | int copy = min(bytes, iov->iov_len - base); |
| 1733 | |
| 1734 | bytes -= copy; |
| 1735 | base += copy; |
| 1736 | if (iov->iov_len == base) { |
| 1737 | iov++; |
| 1738 | base = 0; |
| 1739 | } |
| 1740 | } |
| 1741 | i->iov = iov; |
| 1742 | i->iov_offset = base; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | void iov_iter_advance(struct iov_iter *i, size_t bytes) |
| 1747 | { |
| 1748 | BUG_ON(i->count < bytes); |
| 1749 | |
| 1750 | __iov_iter_advance_iov(i, bytes); |
| 1751 | i->count -= bytes; |
| 1752 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 1753 | EXPORT_SYMBOL(iov_iter_advance); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1754 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1755 | /* |
| 1756 | * Fault in the first iovec of the given iov_iter, to a maximum length |
| 1757 | * of bytes. Returns 0 on success, or non-zero if the memory could not be |
| 1758 | * accessed (ie. because it is an invalid address). |
| 1759 | * |
| 1760 | * writev-intensive code may want this to prefault several iovecs -- that |
| 1761 | * would be possible (callers must not rely on the fact that _only_ the |
| 1762 | * first iovec will be faulted with the current implementation). |
| 1763 | */ |
| 1764 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1765 | { |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1766 | char __user *buf = i->iov->iov_base + i->iov_offset; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1767 | bytes = min(bytes, i->iov->iov_len - i->iov_offset); |
| 1768 | return fault_in_pages_readable(buf, bytes); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1769 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 1770 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1771 | |
| 1772 | /* |
| 1773 | * Return the count of just the current iov_iter segment. |
| 1774 | */ |
| 1775 | size_t iov_iter_single_seg_count(struct iov_iter *i) |
| 1776 | { |
| 1777 | const struct iovec *iov = i->iov; |
| 1778 | if (i->nr_segs == 1) |
| 1779 | return i->count; |
| 1780 | else |
| 1781 | return min(i->count, iov->iov_len - i->iov_offset); |
| 1782 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 1783 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 1784 | |
| 1785 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | * Performs necessary checks before doing a write |
| 1787 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1788 | * Can adjust writing position or amount of bytes to write. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | * Returns appropriate error code that caller should return or |
| 1790 | * zero in case that write should be allowed. |
| 1791 | */ |
| 1792 | inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk) |
| 1793 | { |
| 1794 | struct inode *inode = file->f_mapping->host; |
| 1795 | unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 1796 | |
| 1797 | if (unlikely(*pos < 0)) |
| 1798 | return -EINVAL; |
| 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | if (!isblk) { |
| 1801 | /* FIXME: this is for backwards compatibility with 2.4 */ |
| 1802 | if (file->f_flags & O_APPEND) |
| 1803 | *pos = i_size_read(inode); |
| 1804 | |
| 1805 | if (limit != RLIM_INFINITY) { |
| 1806 | if (*pos >= limit) { |
| 1807 | send_sig(SIGXFSZ, current, 0); |
| 1808 | return -EFBIG; |
| 1809 | } |
| 1810 | if (*count > limit - (typeof(limit))*pos) { |
| 1811 | *count = limit - (typeof(limit))*pos; |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * LFS rule |
| 1818 | */ |
| 1819 | if (unlikely(*pos + *count > MAX_NON_LFS && |
| 1820 | !(file->f_flags & O_LARGEFILE))) { |
| 1821 | if (*pos >= MAX_NON_LFS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | return -EFBIG; |
| 1823 | } |
| 1824 | if (*count > MAX_NON_LFS - (unsigned long)*pos) { |
| 1825 | *count = MAX_NON_LFS - (unsigned long)*pos; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * Are we about to exceed the fs block limit ? |
| 1831 | * |
| 1832 | * If we have written data it becomes a short write. If we have |
| 1833 | * exceeded without writing data we send a signal and return EFBIG. |
| 1834 | * Linus frestrict idea will clean these up nicely.. |
| 1835 | */ |
| 1836 | if (likely(!isblk)) { |
| 1837 | if (unlikely(*pos >= inode->i_sb->s_maxbytes)) { |
| 1838 | if (*count || *pos > inode->i_sb->s_maxbytes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | return -EFBIG; |
| 1840 | } |
| 1841 | /* zero-length writes at ->s_maxbytes are OK */ |
| 1842 | } |
| 1843 | |
| 1844 | if (unlikely(*pos + *count > inode->i_sb->s_maxbytes)) |
| 1845 | *count = inode->i_sb->s_maxbytes - *pos; |
| 1846 | } else { |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 1847 | #ifdef CONFIG_BLOCK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | loff_t isize; |
| 1849 | if (bdev_read_only(I_BDEV(inode))) |
| 1850 | return -EPERM; |
| 1851 | isize = i_size_read(inode); |
| 1852 | if (*pos >= isize) { |
| 1853 | if (*count || *pos > isize) |
| 1854 | return -ENOSPC; |
| 1855 | } |
| 1856 | |
| 1857 | if (*pos + *count > isize) |
| 1858 | *count = isize - *pos; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 1859 | #else |
| 1860 | return -EPERM; |
| 1861 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | } |
| 1863 | return 0; |
| 1864 | } |
| 1865 | EXPORT_SYMBOL(generic_write_checks); |
| 1866 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1867 | int pagecache_write_begin(struct file *file, struct address_space *mapping, |
| 1868 | loff_t pos, unsigned len, unsigned flags, |
| 1869 | struct page **pagep, void **fsdata) |
| 1870 | { |
| 1871 | const struct address_space_operations *aops = mapping->a_ops; |
| 1872 | |
| 1873 | if (aops->write_begin) { |
| 1874 | return aops->write_begin(file, mapping, pos, len, flags, |
| 1875 | pagep, fsdata); |
| 1876 | } else { |
| 1877 | int ret; |
| 1878 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 1879 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1880 | struct inode *inode = mapping->host; |
| 1881 | struct page *page; |
| 1882 | again: |
| 1883 | page = __grab_cache_page(mapping, index); |
| 1884 | *pagep = page; |
| 1885 | if (!page) |
| 1886 | return -ENOMEM; |
| 1887 | |
| 1888 | if (flags & AOP_FLAG_UNINTERRUPTIBLE && !PageUptodate(page)) { |
| 1889 | /* |
| 1890 | * There is no way to resolve a short write situation |
| 1891 | * for a !Uptodate page (except by double copying in |
| 1892 | * the caller done by generic_perform_write_2copy). |
| 1893 | * |
| 1894 | * Instead, we have to bring it uptodate here. |
| 1895 | */ |
| 1896 | ret = aops->readpage(file, page); |
| 1897 | page_cache_release(page); |
| 1898 | if (ret) { |
| 1899 | if (ret == AOP_TRUNCATED_PAGE) |
| 1900 | goto again; |
| 1901 | return ret; |
| 1902 | } |
| 1903 | goto again; |
| 1904 | } |
| 1905 | |
| 1906 | ret = aops->prepare_write(file, page, offset, offset+len); |
| 1907 | if (ret) { |
Nick Piggin | 5514476 | 2007-10-16 01:25:26 -0700 | [diff] [blame] | 1908 | unlock_page(page); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1909 | page_cache_release(page); |
| 1910 | if (pos + len > inode->i_size) |
| 1911 | vmtruncate(inode, inode->i_size); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1912 | } |
| 1913 | return ret; |
| 1914 | } |
| 1915 | } |
| 1916 | EXPORT_SYMBOL(pagecache_write_begin); |
| 1917 | |
| 1918 | int pagecache_write_end(struct file *file, struct address_space *mapping, |
| 1919 | loff_t pos, unsigned len, unsigned copied, |
| 1920 | struct page *page, void *fsdata) |
| 1921 | { |
| 1922 | const struct address_space_operations *aops = mapping->a_ops; |
| 1923 | int ret; |
| 1924 | |
| 1925 | if (aops->write_end) { |
| 1926 | mark_page_accessed(page); |
| 1927 | ret = aops->write_end(file, mapping, pos, len, copied, |
| 1928 | page, fsdata); |
| 1929 | } else { |
| 1930 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1931 | struct inode *inode = mapping->host; |
| 1932 | |
| 1933 | flush_dcache_page(page); |
| 1934 | ret = aops->commit_write(file, page, offset, offset+len); |
| 1935 | unlock_page(page); |
| 1936 | mark_page_accessed(page); |
| 1937 | page_cache_release(page); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1938 | |
| 1939 | if (ret < 0) { |
| 1940 | if (pos + len > inode->i_size) |
| 1941 | vmtruncate(inode, inode->i_size); |
| 1942 | } else if (ret > 0) |
| 1943 | ret = min_t(size_t, copied, ret); |
| 1944 | else |
| 1945 | ret = copied; |
| 1946 | } |
| 1947 | |
| 1948 | return ret; |
| 1949 | } |
| 1950 | EXPORT_SYMBOL(pagecache_write_end); |
| 1951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | ssize_t |
| 1953 | generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov, |
| 1954 | unsigned long *nr_segs, loff_t pos, loff_t *ppos, |
| 1955 | size_t count, size_t ocount) |
| 1956 | { |
| 1957 | struct file *file = iocb->ki_filp; |
| 1958 | struct address_space *mapping = file->f_mapping; |
| 1959 | struct inode *inode = mapping->host; |
| 1960 | ssize_t written; |
| 1961 | |
| 1962 | if (count != ocount) |
| 1963 | *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count); |
| 1964 | |
| 1965 | written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs); |
| 1966 | if (written > 0) { |
| 1967 | loff_t end = pos + written; |
| 1968 | if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { |
| 1969 | i_size_write(inode, end); |
| 1970 | mark_inode_dirty(inode); |
| 1971 | } |
| 1972 | *ppos = end; |
| 1973 | } |
| 1974 | |
| 1975 | /* |
| 1976 | * Sync the fs metadata but not the minor inode changes and |
| 1977 | * of course not the data as we did direct DMA for the IO. |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1978 | * i_mutex is held, which protects generic_osync_inode() from |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1979 | * livelocking. AIO O_DIRECT ops attempt to sync metadata here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | */ |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1981 | if ((written >= 0 || written == -EIOCBQUEUED) && |
| 1982 | ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
Hifumi Hisashi | 1e8a81c | 2005-06-25 14:54:32 -0700 | [diff] [blame] | 1983 | int err = generic_osync_inode(inode, mapping, OSYNC_METADATA); |
| 1984 | if (err < 0) |
| 1985 | written = err; |
| 1986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | return written; |
| 1988 | } |
| 1989 | EXPORT_SYMBOL(generic_file_direct_write); |
| 1990 | |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1991 | /* |
| 1992 | * Find or create a page at the given pagecache position. Return the locked |
| 1993 | * page. This function is specifically for buffered writes. |
| 1994 | */ |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 1995 | struct page *__grab_cache_page(struct address_space *mapping, pgoff_t index) |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1996 | { |
| 1997 | int status; |
| 1998 | struct page *page; |
| 1999 | repeat: |
| 2000 | page = find_lock_page(mapping, index); |
| 2001 | if (likely(page)) |
| 2002 | return page; |
| 2003 | |
| 2004 | page = page_cache_alloc(mapping); |
| 2005 | if (!page) |
| 2006 | return NULL; |
| 2007 | status = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL); |
| 2008 | if (unlikely(status)) { |
| 2009 | page_cache_release(page); |
| 2010 | if (status == -EEXIST) |
| 2011 | goto repeat; |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | return page; |
| 2015 | } |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2016 | EXPORT_SYMBOL(__grab_cache_page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2017 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2018 | static ssize_t generic_perform_write_2copy(struct file *file, |
| 2019 | struct iov_iter *i, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | { |
Andrew Morton | ae37461 | 2007-10-16 01:24:55 -0700 | [diff] [blame] | 2021 | struct address_space *mapping = file->f_mapping; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 2022 | const struct address_space_operations *a_ops = mapping->a_ops; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2023 | struct inode *inode = mapping->host; |
| 2024 | long status = 0; |
| 2025 | ssize_t written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
| 2027 | do { |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2028 | struct page *src_page; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2029 | struct page *page; |
Andrew Morton | ae37461 | 2007-10-16 01:24:55 -0700 | [diff] [blame] | 2030 | pgoff_t index; /* Pagecache index for current page */ |
| 2031 | unsigned long offset; /* Offset into pagecache page */ |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2032 | unsigned long bytes; /* Bytes to write to page */ |
Andrew Morton | ae37461 | 2007-10-16 01:24:55 -0700 | [diff] [blame] | 2033 | size_t copied; /* Bytes copied from user */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | |
Andrew Morton | ae37461 | 2007-10-16 01:24:55 -0700 | [diff] [blame] | 2035 | offset = (pos & (PAGE_CACHE_SIZE - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | index = pos >> PAGE_CACHE_SHIFT; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2037 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2038 | iov_iter_count(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2040 | /* |
| 2041 | * a non-NULL src_page indicates that we're doing the |
| 2042 | * copy via get_user_pages and kmap. |
| 2043 | */ |
| 2044 | src_page = NULL; |
Andrew Morton | ae37461 | 2007-10-16 01:24:55 -0700 | [diff] [blame] | 2045 | |
Nick Piggin | 41cb8ac | 2007-10-16 01:24:53 -0700 | [diff] [blame] | 2046 | /* |
| 2047 | * Bring in the user page that we will copy from _first_. |
| 2048 | * Otherwise there's a nasty deadlock on copying from the |
| 2049 | * same page as we're writing to, without it being marked |
| 2050 | * up-to-date. |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2051 | * |
| 2052 | * Not only is this an optimisation, but it is also required |
| 2053 | * to check that the address is actually valid, when atomic |
| 2054 | * usercopies are used, below. |
Nick Piggin | 41cb8ac | 2007-10-16 01:24:53 -0700 | [diff] [blame] | 2055 | */ |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2056 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2057 | status = -EFAULT; |
| 2058 | break; |
| 2059 | } |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2060 | |
| 2061 | page = __grab_cache_page(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | if (!page) { |
| 2063 | status = -ENOMEM; |
| 2064 | break; |
| 2065 | } |
| 2066 | |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2067 | /* |
| 2068 | * non-uptodate pages cannot cope with short copies, and we |
| 2069 | * cannot take a pagefault with the destination page locked. |
| 2070 | * So pin the source page to copy it. |
| 2071 | */ |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2072 | if (!PageUptodate(page) && !segment_eq(get_fs(), KERNEL_DS)) { |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2073 | unlock_page(page); |
| 2074 | |
| 2075 | src_page = alloc_page(GFP_KERNEL); |
| 2076 | if (!src_page) { |
| 2077 | page_cache_release(page); |
| 2078 | status = -ENOMEM; |
| 2079 | break; |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * Cannot get_user_pages with a page locked for the |
| 2084 | * same reason as we can't take a page fault with a |
| 2085 | * page locked (as explained below). |
| 2086 | */ |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2087 | copied = iov_iter_copy_from_user(src_page, i, |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2088 | offset, bytes); |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2089 | if (unlikely(copied == 0)) { |
| 2090 | status = -EFAULT; |
| 2091 | page_cache_release(page); |
| 2092 | page_cache_release(src_page); |
| 2093 | break; |
| 2094 | } |
| 2095 | bytes = copied; |
| 2096 | |
| 2097 | lock_page(page); |
| 2098 | /* |
| 2099 | * Can't handle the page going uptodate here, because |
| 2100 | * that means we would use non-atomic usercopies, which |
| 2101 | * zero out the tail of the page, which can cause |
| 2102 | * zeroes to become transiently visible. We could just |
| 2103 | * use a non-zeroing copy, but the APIs aren't too |
| 2104 | * consistent. |
| 2105 | */ |
| 2106 | if (unlikely(!page->mapping || PageUptodate(page))) { |
| 2107 | unlock_page(page); |
| 2108 | page_cache_release(page); |
| 2109 | page_cache_release(src_page); |
| 2110 | continue; |
| 2111 | } |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | status = a_ops->prepare_write(file, page, offset, offset+bytes); |
Nick Piggin | 64649a5 | 2007-10-16 01:24:56 -0700 | [diff] [blame] | 2115 | if (unlikely(status)) |
| 2116 | goto fs_write_aop_error; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2117 | |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2118 | if (!src_page) { |
| 2119 | /* |
| 2120 | * Must not enter the pagefault handler here, because |
| 2121 | * we hold the page lock, so we might recursively |
| 2122 | * deadlock on the same lock, or get an ABBA deadlock |
| 2123 | * against a different lock, or against the mmap_sem |
| 2124 | * (which nests outside the page lock). So increment |
| 2125 | * preempt count, and use _atomic usercopies. |
| 2126 | * |
| 2127 | * The page is uptodate so we are OK to encounter a |
| 2128 | * short copy: if unmodified parts of the page are |
| 2129 | * marked dirty and written out to disk, it doesn't |
| 2130 | * really matter. |
| 2131 | */ |
| 2132 | pagefault_disable(); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2133 | copied = iov_iter_copy_from_user_atomic(page, i, |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2134 | offset, bytes); |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2135 | pagefault_enable(); |
| 2136 | } else { |
| 2137 | void *src, *dst; |
| 2138 | src = kmap_atomic(src_page, KM_USER0); |
| 2139 | dst = kmap_atomic(page, KM_USER1); |
| 2140 | memcpy(dst + offset, src + offset, bytes); |
| 2141 | kunmap_atomic(dst, KM_USER1); |
| 2142 | kunmap_atomic(src, KM_USER0); |
| 2143 | copied = bytes; |
| 2144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | flush_dcache_page(page); |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 2146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | status = a_ops->commit_write(file, page, offset, offset+bytes); |
Nick Piggin | 5514476 | 2007-10-16 01:25:26 -0700 | [diff] [blame] | 2148 | if (unlikely(status < 0)) |
Nick Piggin | 64649a5 | 2007-10-16 01:24:56 -0700 | [diff] [blame] | 2149 | goto fs_write_aop_error; |
Nick Piggin | 64649a5 | 2007-10-16 01:24:56 -0700 | [diff] [blame] | 2150 | if (unlikely(status > 0)) /* filesystem did partial write */ |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2151 | copied = min_t(size_t, copied, status); |
| 2152 | |
| 2153 | unlock_page(page); |
| 2154 | mark_page_accessed(page); |
| 2155 | page_cache_release(page); |
| 2156 | if (src_page) |
| 2157 | page_cache_release(src_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2159 | iov_iter_advance(i, copied); |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 2160 | pos += copied; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2161 | written += copied; |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 2162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | balance_dirty_pages_ratelimited(mapping); |
| 2164 | cond_resched(); |
Nick Piggin | 64649a5 | 2007-10-16 01:24:56 -0700 | [diff] [blame] | 2165 | continue; |
| 2166 | |
| 2167 | fs_write_aop_error: |
Nick Piggin | 5514476 | 2007-10-16 01:25:26 -0700 | [diff] [blame] | 2168 | unlock_page(page); |
Nick Piggin | 64649a5 | 2007-10-16 01:24:56 -0700 | [diff] [blame] | 2169 | page_cache_release(page); |
Nick Piggin | 08291429 | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2170 | if (src_page) |
| 2171 | page_cache_release(src_page); |
Nick Piggin | 64649a5 | 2007-10-16 01:24:56 -0700 | [diff] [blame] | 2172 | |
| 2173 | /* |
| 2174 | * prepare_write() may have instantiated a few blocks |
| 2175 | * outside i_size. Trim these off again. Don't need |
| 2176 | * i_size_read because we hold i_mutex. |
| 2177 | */ |
| 2178 | if (pos + bytes > inode->i_size) |
| 2179 | vmtruncate(inode, inode->i_size); |
Nick Piggin | 5514476 | 2007-10-16 01:25:26 -0700 | [diff] [blame] | 2180 | break; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2181 | } while (iov_iter_count(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2183 | return written ? written : status; |
| 2184 | } |
| 2185 | |
| 2186 | static ssize_t generic_perform_write(struct file *file, |
| 2187 | struct iov_iter *i, loff_t pos) |
| 2188 | { |
| 2189 | struct address_space *mapping = file->f_mapping; |
| 2190 | const struct address_space_operations *a_ops = mapping->a_ops; |
| 2191 | long status = 0; |
| 2192 | ssize_t written = 0; |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2193 | unsigned int flags = 0; |
| 2194 | |
| 2195 | /* |
| 2196 | * Copies from kernel address space cannot fail (NFSD is a big user). |
| 2197 | */ |
| 2198 | if (segment_eq(get_fs(), KERNEL_DS)) |
| 2199 | flags |= AOP_FLAG_UNINTERRUPTIBLE; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2200 | |
| 2201 | do { |
| 2202 | struct page *page; |
| 2203 | pgoff_t index; /* Pagecache index for current page */ |
| 2204 | unsigned long offset; /* Offset into pagecache page */ |
| 2205 | unsigned long bytes; /* Bytes to write to page */ |
| 2206 | size_t copied; /* Bytes copied from user */ |
| 2207 | void *fsdata; |
| 2208 | |
| 2209 | offset = (pos & (PAGE_CACHE_SIZE - 1)); |
| 2210 | index = pos >> PAGE_CACHE_SHIFT; |
| 2211 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, |
| 2212 | iov_iter_count(i)); |
| 2213 | |
| 2214 | again: |
| 2215 | |
| 2216 | /* |
| 2217 | * Bring in the user page that we will copy from _first_. |
| 2218 | * Otherwise there's a nasty deadlock on copying from the |
| 2219 | * same page as we're writing to, without it being marked |
| 2220 | * up-to-date. |
| 2221 | * |
| 2222 | * Not only is this an optimisation, but it is also required |
| 2223 | * to check that the address is actually valid, when atomic |
| 2224 | * usercopies are used, below. |
| 2225 | */ |
| 2226 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
| 2227 | status = -EFAULT; |
| 2228 | break; |
| 2229 | } |
| 2230 | |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2231 | status = a_ops->write_begin(file, mapping, pos, bytes, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2232 | &page, &fsdata); |
| 2233 | if (unlikely(status)) |
| 2234 | break; |
| 2235 | |
| 2236 | pagefault_disable(); |
| 2237 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
| 2238 | pagefault_enable(); |
| 2239 | flush_dcache_page(page); |
| 2240 | |
| 2241 | status = a_ops->write_end(file, mapping, pos, bytes, copied, |
| 2242 | page, fsdata); |
| 2243 | if (unlikely(status < 0)) |
| 2244 | break; |
| 2245 | copied = status; |
| 2246 | |
| 2247 | cond_resched(); |
| 2248 | |
| 2249 | if (unlikely(copied == 0)) { |
| 2250 | /* |
| 2251 | * If we were unable to copy any data at all, we must |
| 2252 | * fall back to a single segment length write. |
| 2253 | * |
| 2254 | * If we didn't fallback here, we could livelock |
| 2255 | * because not all segments in the iov can be copied at |
| 2256 | * once without a pagefault. |
| 2257 | */ |
| 2258 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, |
| 2259 | iov_iter_single_seg_count(i)); |
| 2260 | goto again; |
| 2261 | } |
| 2262 | iov_iter_advance(i, copied); |
| 2263 | pos += copied; |
| 2264 | written += copied; |
| 2265 | |
| 2266 | balance_dirty_pages_ratelimited(mapping); |
| 2267 | |
| 2268 | } while (iov_iter_count(i)); |
| 2269 | |
| 2270 | return written ? written : status; |
| 2271 | } |
| 2272 | |
| 2273 | ssize_t |
| 2274 | generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov, |
| 2275 | unsigned long nr_segs, loff_t pos, loff_t *ppos, |
| 2276 | size_t count, ssize_t written) |
| 2277 | { |
| 2278 | struct file *file = iocb->ki_filp; |
| 2279 | struct address_space *mapping = file->f_mapping; |
| 2280 | const struct address_space_operations *a_ops = mapping->a_ops; |
| 2281 | struct inode *inode = mapping->host; |
| 2282 | ssize_t status; |
| 2283 | struct iov_iter i; |
| 2284 | |
| 2285 | iov_iter_init(&i, iov, nr_segs, count, written); |
| 2286 | if (a_ops->write_begin) |
| 2287 | status = generic_perform_write(file, &i, pos); |
| 2288 | else |
| 2289 | status = generic_perform_write_2copy(file, &i, pos); |
| 2290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | if (likely(status >= 0)) { |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2292 | written += status; |
| 2293 | *ppos = pos + status; |
| 2294 | |
| 2295 | /* |
| 2296 | * For now, when the user asks for O_SYNC, we'll actually give |
| 2297 | * O_DSYNC |
| 2298 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2300 | if (!a_ops->writepage || !is_sync_kiocb(iocb)) |
| 2301 | status = generic_osync_inode(inode, mapping, |
| 2302 | OSYNC_METADATA|OSYNC_DATA); |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | * If we get here for O_DIRECT writes then we must have fallen through |
| 2308 | * to buffered writes (block instantiation inside i_size). So we sync |
| 2309 | * the file data here, to try to honour O_DIRECT expectations. |
| 2310 | */ |
| 2311 | if (unlikely(file->f_flags & O_DIRECT) && written) |
| 2312 | status = filemap_write_and_wait(mapping); |
| 2313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | return written ? written : status; |
| 2315 | } |
| 2316 | EXPORT_SYMBOL(generic_file_buffered_write); |
| 2317 | |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 2318 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov, |
| 2320 | unsigned long nr_segs, loff_t *ppos) |
| 2321 | { |
| 2322 | struct file *file = iocb->ki_filp; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2323 | struct address_space * mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | size_t ocount; /* original count */ |
| 2325 | size_t count; /* after file limit checks */ |
| 2326 | struct inode *inode = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | loff_t pos; |
| 2328 | ssize_t written; |
| 2329 | ssize_t err; |
| 2330 | |
| 2331 | ocount = 0; |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 2332 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
| 2333 | if (err) |
| 2334 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
| 2336 | count = ocount; |
| 2337 | pos = *ppos; |
| 2338 | |
| 2339 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 2340 | |
| 2341 | /* We can write back this queue in page reclaim */ |
| 2342 | current->backing_dev_info = mapping->backing_dev_info; |
| 2343 | written = 0; |
| 2344 | |
| 2345 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 2346 | if (err) |
| 2347 | goto out; |
| 2348 | |
| 2349 | if (count == 0) |
| 2350 | goto out; |
| 2351 | |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 2352 | err = remove_suid(file->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | if (err) |
| 2354 | goto out; |
| 2355 | |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 2356 | file_update_time(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | |
| 2358 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 2359 | if (unlikely(file->f_flags & O_DIRECT)) { |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2360 | loff_t endbyte; |
| 2361 | ssize_t written_buffered; |
| 2362 | |
| 2363 | written = generic_file_direct_write(iocb, iov, &nr_segs, pos, |
| 2364 | ppos, count, ocount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | if (written < 0 || written == count) |
| 2366 | goto out; |
| 2367 | /* |
| 2368 | * direct-io write to a hole: fall through to buffered I/O |
| 2369 | * for completing the rest of the request. |
| 2370 | */ |
| 2371 | pos += written; |
| 2372 | count -= written; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2373 | written_buffered = generic_file_buffered_write(iocb, iov, |
| 2374 | nr_segs, pos, ppos, count, |
| 2375 | written); |
| 2376 | /* |
| 2377 | * If generic_file_buffered_write() retuned a synchronous error |
| 2378 | * then we want to return the number of bytes which were |
| 2379 | * direct-written, or the error code if that was zero. Note |
| 2380 | * that this differs from normal direct-io semantics, which |
| 2381 | * will return -EFOO even if some bytes were written. |
| 2382 | */ |
| 2383 | if (written_buffered < 0) { |
| 2384 | err = written_buffered; |
| 2385 | goto out; |
| 2386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2388 | /* |
| 2389 | * We need to ensure that the page cache pages are written to |
| 2390 | * disk and invalidated to preserve the expected O_DIRECT |
| 2391 | * semantics. |
| 2392 | */ |
| 2393 | endbyte = pos + written_buffered - written - 1; |
Mark Fasheh | ef51c97 | 2007-05-08 00:27:10 -0700 | [diff] [blame] | 2394 | err = do_sync_mapping_range(file->f_mapping, pos, endbyte, |
| 2395 | SYNC_FILE_RANGE_WAIT_BEFORE| |
| 2396 | SYNC_FILE_RANGE_WRITE| |
| 2397 | SYNC_FILE_RANGE_WAIT_AFTER); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2398 | if (err == 0) { |
| 2399 | written = written_buffered; |
| 2400 | invalidate_mapping_pages(mapping, |
| 2401 | pos >> PAGE_CACHE_SHIFT, |
| 2402 | endbyte >> PAGE_CACHE_SHIFT); |
| 2403 | } else { |
| 2404 | /* |
| 2405 | * We don't know how much we wrote, so just return |
| 2406 | * the number of bytes which were direct-written |
| 2407 | */ |
| 2408 | } |
| 2409 | } else { |
| 2410 | written = generic_file_buffered_write(iocb, iov, nr_segs, |
| 2411 | pos, ppos, count, written); |
| 2412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | out: |
| 2414 | current->backing_dev_info = NULL; |
| 2415 | return written ? written : err; |
| 2416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2418 | ssize_t generic_file_aio_write_nolock(struct kiocb *iocb, |
| 2419 | const struct iovec *iov, unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | { |
| 2421 | struct file *file = iocb->ki_filp; |
| 2422 | struct address_space *mapping = file->f_mapping; |
| 2423 | struct inode *inode = mapping->host; |
| 2424 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2426 | BUG_ON(iocb->ki_pos != pos); |
| 2427 | |
| 2428 | ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, |
| 2429 | &iocb->ki_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | |
| 2431 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2432 | ssize_t err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | |
| 2434 | err = sync_page_range_nolock(inode, mapping, pos, ret); |
| 2435 | if (err < 0) |
| 2436 | ret = err; |
| 2437 | } |
| 2438 | return ret; |
| 2439 | } |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2440 | EXPORT_SYMBOL(generic_file_aio_write_nolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2442 | ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 2443 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | { |
| 2445 | struct file *file = iocb->ki_filp; |
| 2446 | struct address_space *mapping = file->f_mapping; |
| 2447 | struct inode *inode = mapping->host; |
| 2448 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
| 2450 | BUG_ON(iocb->ki_pos != pos); |
| 2451 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2452 | mutex_lock(&inode->i_mutex); |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2453 | ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, |
| 2454 | &iocb->ki_pos); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2455 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | |
| 2457 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2458 | ssize_t err; |
| 2459 | |
| 2460 | err = sync_page_range(inode, mapping, pos, ret); |
| 2461 | if (err < 0) |
| 2462 | ret = err; |
| 2463 | } |
| 2464 | return ret; |
| 2465 | } |
| 2466 | EXPORT_SYMBOL(generic_file_aio_write); |
| 2467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2469 | * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | * went wrong during pagecache shootdown. |
| 2471 | */ |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 2472 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 2474 | loff_t offset, unsigned long nr_segs) |
| 2475 | { |
| 2476 | struct file *file = iocb->ki_filp; |
| 2477 | struct address_space *mapping = file->f_mapping; |
| 2478 | ssize_t retval; |
Zach Brown | 65b8291 | 2007-03-16 13:38:11 -0800 | [diff] [blame] | 2479 | size_t write_len; |
| 2480 | pgoff_t end = 0; /* silence gcc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | |
| 2482 | /* |
| 2483 | * If it's a write, unmap all mmappings of the file up-front. This |
| 2484 | * will cause any pte dirty bits to be propagated into the pageframes |
| 2485 | * for the subsequent filemap_write_and_wait(). |
| 2486 | */ |
| 2487 | if (rw == WRITE) { |
| 2488 | write_len = iov_length(iov, nr_segs); |
Zach Brown | 65b8291 | 2007-03-16 13:38:11 -0800 | [diff] [blame] | 2489 | end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | if (mapping_mapped(mapping)) |
| 2491 | unmap_mapping_range(mapping, offset, write_len, 0); |
| 2492 | } |
| 2493 | |
| 2494 | retval = filemap_write_and_wait(mapping); |
Zach Brown | 65b8291 | 2007-03-16 13:38:11 -0800 | [diff] [blame] | 2495 | if (retval) |
| 2496 | goto out; |
| 2497 | |
| 2498 | /* |
| 2499 | * After a write we want buffered reads to be sure to go to disk to get |
| 2500 | * the new data. We invalidate clean cached page from the region we're |
| 2501 | * about to write. We do this *before* the write so that we can return |
| 2502 | * -EIO without clobbering -EIOCBQUEUED from ->direct_IO(). |
| 2503 | */ |
| 2504 | if (rw == WRITE && mapping->nrpages) { |
| 2505 | retval = invalidate_inode_pages2_range(mapping, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | offset >> PAGE_CACHE_SHIFT, end); |
Zach Brown | 65b8291 | 2007-03-16 13:38:11 -0800 | [diff] [blame] | 2507 | if (retval) |
| 2508 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | } |
Zach Brown | 65b8291 | 2007-03-16 13:38:11 -0800 | [diff] [blame] | 2510 | |
| 2511 | retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs); |
| 2512 | if (retval) |
| 2513 | goto out; |
| 2514 | |
| 2515 | /* |
| 2516 | * Finally, try again to invalidate clean pages which might have been |
| 2517 | * faulted in by get_user_pages() if the source of the write was an |
| 2518 | * mmap()ed region of the file we're writing. That's a pretty crazy |
| 2519 | * thing to do, so we don't support it 100%. If this invalidation |
| 2520 | * fails and we have -EIOCBQUEUED we ignore the failure. |
| 2521 | */ |
| 2522 | if (rw == WRITE && mapping->nrpages) { |
| 2523 | int err = invalidate_inode_pages2_range(mapping, |
| 2524 | offset >> PAGE_CACHE_SHIFT, end); |
| 2525 | if (err && retval >= 0) |
| 2526 | retval = err; |
| 2527 | } |
| 2528 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | return retval; |
| 2530 | } |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 2531 | |
| 2532 | /** |
| 2533 | * try_to_release_page() - release old fs-specific metadata on a page |
| 2534 | * |
| 2535 | * @page: the page which the kernel is trying to free |
| 2536 | * @gfp_mask: memory allocation flags (and I/O mode) |
| 2537 | * |
| 2538 | * The address_space is to try to release any data against the page |
| 2539 | * (presumably at page->private). If the release was successful, return `1'. |
| 2540 | * Otherwise return zero. |
| 2541 | * |
| 2542 | * The @gfp_mask argument specifies whether I/O may be performed to release |
| 2543 | * this page (__GFP_IO), and whether the call may block (__GFP_WAIT). |
| 2544 | * |
| 2545 | * NOTE: @gfp_mask may go away, and this function may become non-blocking. |
| 2546 | */ |
| 2547 | int try_to_release_page(struct page *page, gfp_t gfp_mask) |
| 2548 | { |
| 2549 | struct address_space * const mapping = page->mapping; |
| 2550 | |
| 2551 | BUG_ON(!PageLocked(page)); |
| 2552 | if (PageWriteback(page)) |
| 2553 | return 0; |
| 2554 | |
| 2555 | if (mapping && mapping->a_ops->releasepage) |
| 2556 | return mapping->a_ops->releasepage(page, gfp_mask); |
| 2557 | return try_to_free_buffers(page); |
| 2558 | } |
| 2559 | |
| 2560 | EXPORT_SYMBOL(try_to_release_page); |