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