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