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