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