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> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kernel_stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/swap.h> |
| 23 | #include <linux/mman.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/file.h> |
| 26 | #include <linux/uio.h> |
Josef Bacik | cfcbfb1 | 2019-05-13 17:21:04 -0700 | [diff] [blame] | 27 | #include <linux/error-injection.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/hash.h> |
| 29 | #include <linux/writeback.h> |
Linus Torvalds | 5325338 | 2007-10-18 14:47:32 -0700 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/pagevec.h> |
| 32 | #include <linux/blkdev.h> |
| 33 | #include <linux/security.h> |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 34 | #include <linux/cpuset.h> |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 35 | #include <linux/hugetlb.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 36 | #include <linux/memcontrol.h> |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 37 | #include <linux/cleancache.h> |
Mel Gorman | c7df8ad | 2017-11-15 17:37:41 -0800 | [diff] [blame] | 38 | #include <linux/shmem_fs.h> |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 39 | #include <linux/rmap.h> |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 40 | #include <linux/delayacct.h> |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 41 | #include <linux/psi.h> |
Nick Piggin | 0f8053a | 2006-03-22 00:08:33 -0800 | [diff] [blame] | 42 | #include "internal.h" |
| 43 | |
Robert Jarzmik | fe0bfaa | 2013-04-29 15:06:10 -0700 | [diff] [blame] | 44 | #define CREATE_TRACE_POINTS |
| 45 | #include <trace/events/filemap.h> |
| 46 | |
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 | * FIXME: remove all knowledge of the buffer layer from the core VM |
| 49 | */ |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 50 | #include <linux/buffer_head.h> /* for try_to_free_buffers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/mman.h> |
| 53 | |
| 54 | /* |
| 55 | * Shared mappings implemented 30.11.1994. It's not fully working yet, |
| 56 | * though. |
| 57 | * |
| 58 | * Shared mappings now work. 15.8.1995 Bruno. |
| 59 | * |
| 60 | * finished 'unifying' the page and buffer cache and SMP-threaded the |
| 61 | * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> |
| 62 | * |
| 63 | * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> |
| 64 | */ |
| 65 | |
| 66 | /* |
| 67 | * Lock ordering: |
| 68 | * |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 69 | * ->i_mmap_rwsem (truncate_pagecache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * ->private_lock (__free_pte->__set_page_dirty_buffers) |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 71 | * ->swap_lock (exclusive_swap_page, others) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 72 | * ->i_pages lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 74 | * ->i_mutex |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 75 | * ->i_mmap_rwsem (truncate->unmap_mapping_range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | * |
| 77 | * ->mmap_sem |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 78 | * ->i_mmap_rwsem |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 79 | * ->page_table_lock or pte_lock (various, mainly in memory.c) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 80 | * ->i_pages lock (arch-dependent flush_dcache_mmap_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
| 82 | * ->mmap_sem |
| 83 | * ->lock_page (access_process_vm) |
| 84 | * |
Al Viro | ccad236 | 2014-02-11 22:36:48 -0500 | [diff] [blame] | 85 | * ->i_mutex (generic_perform_write) |
Nick Piggin | 82591e6 | 2006-10-19 23:29:10 -0700 | [diff] [blame] | 86 | * ->mmap_sem (fault_in_pages_readable->do_page_fault) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 88 | * bdi->wb.list_lock |
Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 89 | * sb_lock (fs/fs-writeback.c) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 90 | * ->i_pages lock (__sync_single_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 92 | * ->i_mmap_rwsem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * ->anon_vma.lock (vma_adjust) |
| 94 | * |
| 95 | * ->anon_vma.lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 96 | * ->page_table_lock or pte_lock (anon_vma_prepare and various) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 98 | * ->page_table_lock or pte_lock |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 99 | * ->swap_lock (try_to_unmap_one) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * ->private_lock (try_to_unmap_one) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 101 | * ->i_pages lock (try_to_unmap_one) |
Andrey Ryabinin | f4b7e27 | 2019-03-05 15:49:39 -0800 | [diff] [blame] | 102 | * ->pgdat->lru_lock (follow_page->mark_page_accessed) |
| 103 | * ->pgdat->lru_lock (check_pte_range->isolate_lru_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * ->private_lock (page_remove_rmap->set_page_dirty) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 105 | * ->i_pages lock (page_remove_rmap->set_page_dirty) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 106 | * bdi.wb->list_lock (page_remove_rmap->set_page_dirty) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 107 | * ->inode->i_lock (page_remove_rmap->set_page_dirty) |
Johannes Weiner | 81f8c3a | 2016-03-15 14:57:04 -0700 | [diff] [blame] | 108 | * ->memcg->move_lock (page_remove_rmap->lock_page_memcg) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 109 | * bdi.wb->list_lock (zap_pte_range->set_page_dirty) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 110 | * ->inode->i_lock (zap_pte_range->set_page_dirty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * ->private_lock (zap_pte_range->__set_page_dirty_buffers) |
| 112 | * |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 113 | * ->i_mmap_rwsem |
Andi Kleen | 9a3c531 | 2012-03-21 16:34:09 -0700 | [diff] [blame] | 114 | * ->tasklist_lock (memory_failure, collect_procs_ao) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
| 116 | |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 117 | static void page_cache_delete(struct address_space *mapping, |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 118 | struct page *page, void *shadow) |
| 119 | { |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 120 | XA_STATE(xas, &mapping->i_pages, page->index); |
| 121 | unsigned int nr = 1; |
Kirill A. Shutemov | c70b647 | 2016-12-12 16:43:17 -0800 | [diff] [blame] | 122 | |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 123 | mapping_set_update(&xas, mapping); |
| 124 | |
| 125 | /* hugetlb pages are represented by a single entry in the xarray */ |
| 126 | if (!PageHuge(page)) { |
| 127 | xas_set_order(&xas, page->index, compound_order(page)); |
| 128 | nr = 1U << compound_order(page); |
| 129 | } |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 130 | |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 131 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 132 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 133 | VM_BUG_ON_PAGE(nr != 1 && shadow, page); |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 134 | |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 135 | xas_store(&xas, shadow); |
| 136 | xas_init_marks(&xas); |
Johannes Weiner | d3798ae | 2016-10-04 22:02:08 +0200 | [diff] [blame] | 137 | |
Jan Kara | 2300638 | 2017-11-15 17:37:26 -0800 | [diff] [blame] | 138 | page->mapping = NULL; |
| 139 | /* Leave page->index set: truncation lookup relies upon it */ |
| 140 | |
Johannes Weiner | d3798ae | 2016-10-04 22:02:08 +0200 | [diff] [blame] | 141 | if (shadow) { |
| 142 | mapping->nrexceptional += nr; |
| 143 | /* |
| 144 | * Make sure the nrexceptional update is committed before |
| 145 | * the nrpages update so that final truncate racing |
| 146 | * with reclaim does not see both counters 0 at the |
| 147 | * same time and miss a shadow entry. |
| 148 | */ |
| 149 | smp_wmb(); |
| 150 | } |
| 151 | mapping->nrpages -= nr; |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 154 | static void unaccount_page_cache_page(struct address_space *mapping, |
| 155 | struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 157 | int nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 159 | /* |
| 160 | * if we're uptodate, flush out into the cleancache, otherwise |
| 161 | * invalidate any existing cleancache entries. We can't leave |
| 162 | * stale data around in the cleancache once our page is gone |
| 163 | */ |
| 164 | if (PageUptodate(page) && PageMappedToDisk(page)) |
| 165 | cleancache_put_page(page); |
| 166 | else |
Dan Magenheimer | 3167760 | 2011-09-21 11:56:28 -0400 | [diff] [blame] | 167 | cleancache_invalidate_page(mapping, page); |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 168 | |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 169 | VM_BUG_ON_PAGE(PageTail(page), page); |
Hugh Dickins | 06b241f | 2016-03-09 14:08:07 -0800 | [diff] [blame] | 170 | VM_BUG_ON_PAGE(page_mapped(page), page); |
| 171 | if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) { |
| 172 | int mapcount; |
| 173 | |
| 174 | pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n", |
| 175 | current->comm, page_to_pfn(page)); |
| 176 | dump_page(page, "still mapped when deleted"); |
| 177 | dump_stack(); |
| 178 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
| 179 | |
| 180 | mapcount = page_mapcount(page); |
| 181 | if (mapping_exiting(mapping) && |
| 182 | page_count(page) >= mapcount + 2) { |
| 183 | /* |
| 184 | * All vmas have already been torn down, so it's |
| 185 | * a good bet that actually the page is unmapped, |
| 186 | * and we'd prefer not to leak it: if we're wrong, |
| 187 | * some other bad page check should catch it later. |
| 188 | */ |
| 189 | page_mapcount_reset(page); |
Joonsoo Kim | 6d061f9 | 2016-05-19 17:10:46 -0700 | [diff] [blame] | 190 | page_ref_sub(page, mapcount); |
Hugh Dickins | 06b241f | 2016-03-09 14:08:07 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Jan Kara | 76253fb | 2017-11-15 17:37:22 -0800 | [diff] [blame] | 194 | /* hugetlb pages do not participate in page cache accounting. */ |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 195 | if (PageHuge(page)) |
| 196 | return; |
Jan Kara | 76253fb | 2017-11-15 17:37:22 -0800 | [diff] [blame] | 197 | |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 198 | nr = hpage_nr_pages(page); |
| 199 | |
| 200 | __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr); |
| 201 | if (PageSwapBacked(page)) { |
| 202 | __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr); |
| 203 | if (PageTransHuge(page)) |
| 204 | __dec_node_page_state(page, NR_SHMEM_THPS); |
| 205 | } else { |
| 206 | VM_BUG_ON_PAGE(PageTransHuge(page), page); |
Jan Kara | 76253fb | 2017-11-15 17:37:22 -0800 | [diff] [blame] | 207 | } |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * At this point page must be either written or cleaned by |
| 211 | * truncate. Dirty page here signals a bug and loss of |
| 212 | * unwritten data. |
| 213 | * |
| 214 | * This fixes dirty accounting after removing the page entirely |
| 215 | * but leaves PageDirty set: it has no effect for truncated |
| 216 | * page and anyway will be cleared before returning page into |
| 217 | * buddy allocator. |
| 218 | */ |
| 219 | if (WARN_ON_ONCE(PageDirty(page))) |
| 220 | account_page_cleaned(page, mapping, inode_to_wb(mapping->host)); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Delete a page from the page cache and free it. Caller has to make |
| 225 | * sure the page is locked and that nobody else uses it - or that usage |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 226 | * is safe. The caller must hold the i_pages lock. |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 227 | */ |
| 228 | void __delete_from_page_cache(struct page *page, void *shadow) |
| 229 | { |
| 230 | struct address_space *mapping = page->mapping; |
| 231 | |
| 232 | trace_mm_filemap_delete_from_page_cache(page); |
| 233 | |
| 234 | unaccount_page_cache_page(mapping, page); |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 235 | page_cache_delete(mapping, page, shadow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jan Kara | 59c66c5 | 2017-11-15 17:37:18 -0800 | [diff] [blame] | 238 | static void page_cache_free_page(struct address_space *mapping, |
| 239 | struct page *page) |
| 240 | { |
| 241 | void (*freepage)(struct page *); |
| 242 | |
| 243 | freepage = mapping->a_ops->freepage; |
| 244 | if (freepage) |
| 245 | freepage(page); |
| 246 | |
| 247 | if (PageTransHuge(page) && !PageHuge(page)) { |
| 248 | page_ref_sub(page, HPAGE_PMD_NR); |
| 249 | VM_BUG_ON_PAGE(page_count(page) <= 0, page); |
| 250 | } else { |
| 251 | put_page(page); |
| 252 | } |
| 253 | } |
| 254 | |
Minchan Kim | 702cfbf | 2011-03-22 16:32:43 -0700 | [diff] [blame] | 255 | /** |
| 256 | * delete_from_page_cache - delete page from page cache |
| 257 | * @page: the page which the kernel is trying to remove from page cache |
| 258 | * |
| 259 | * This must be called only on pages that have been verified to be in the page |
| 260 | * cache and locked. It will never put the page into the free list, the caller |
| 261 | * has a reference on the page. |
| 262 | */ |
| 263 | void delete_from_page_cache(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 265 | struct address_space *mapping = page_mapping(page); |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 266 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 268 | BUG_ON(!PageLocked(page)); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 269 | xa_lock_irqsave(&mapping->i_pages, flags); |
Johannes Weiner | 62cccb8 | 2016-03-15 14:57:22 -0700 | [diff] [blame] | 270 | __delete_from_page_cache(page, NULL); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 271 | xa_unlock_irqrestore(&mapping->i_pages, flags); |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 272 | |
Jan Kara | 59c66c5 | 2017-11-15 17:37:18 -0800 | [diff] [blame] | 273 | page_cache_free_page(mapping, page); |
Minchan Kim | 97cecb5 | 2011-03-22 16:30:53 -0700 | [diff] [blame] | 274 | } |
| 275 | EXPORT_SYMBOL(delete_from_page_cache); |
| 276 | |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 277 | /* |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 278 | * page_cache_delete_batch - delete several pages from page cache |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 279 | * @mapping: the mapping to which pages belong |
| 280 | * @pvec: pagevec with pages to delete |
| 281 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 282 | * The function walks over mapping->i_pages and removes pages passed in @pvec |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 283 | * from the mapping. The function expects @pvec to be sorted by page index |
| 284 | * and is optimised for it to be dense. |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 285 | * It tolerates holes in @pvec (mapping entries at those indices are not |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 286 | * modified). The function expects only THP head pages to be present in the |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 287 | * @pvec. |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 288 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 289 | * The function expects the i_pages lock to be held. |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 290 | */ |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 291 | static void page_cache_delete_batch(struct address_space *mapping, |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 292 | struct pagevec *pvec) |
| 293 | { |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 294 | XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 295 | int total_pages = 0; |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 296 | int i = 0; |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 297 | struct page *page; |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 298 | |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 299 | mapping_set_update(&xas, mapping); |
| 300 | xas_for_each(&xas, page, ULONG_MAX) { |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 301 | if (i >= pagevec_count(pvec)) |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 302 | break; |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 303 | |
| 304 | /* A swap/dax/shadow entry got inserted? Skip it. */ |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 305 | if (xa_is_value(page)) |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 306 | continue; |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 307 | /* |
| 308 | * A page got inserted in our range? Skip it. We have our |
| 309 | * pages locked so they are protected from being removed. |
| 310 | * If we see a page whose index is higher than ours, it |
| 311 | * means our page has been removed, which shouldn't be |
| 312 | * possible because we're holding the PageLock. |
| 313 | */ |
| 314 | if (page != pvec->pages[i]) { |
| 315 | VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index, |
| 316 | page); |
| 317 | continue; |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 318 | } |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 319 | |
| 320 | WARN_ON_ONCE(!PageLocked(page)); |
| 321 | |
| 322 | if (page->index == xas.xa_index) |
| 323 | page->mapping = NULL; |
| 324 | /* Leave page->index set: truncation lookup relies on it */ |
| 325 | |
| 326 | /* |
| 327 | * Move to the next page in the vector if this is a regular |
| 328 | * page or the index is of the last sub-page of this compound |
| 329 | * page. |
| 330 | */ |
| 331 | if (page->index + (1UL << compound_order(page)) - 1 == |
| 332 | xas.xa_index) |
| 333 | i++; |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 334 | xas_store(&xas, NULL); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 335 | total_pages++; |
| 336 | } |
| 337 | mapping->nrpages -= total_pages; |
| 338 | } |
| 339 | |
| 340 | void delete_from_page_cache_batch(struct address_space *mapping, |
| 341 | struct pagevec *pvec) |
| 342 | { |
| 343 | int i; |
| 344 | unsigned long flags; |
| 345 | |
| 346 | if (!pagevec_count(pvec)) |
| 347 | return; |
| 348 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 349 | xa_lock_irqsave(&mapping->i_pages, flags); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 350 | for (i = 0; i < pagevec_count(pvec); i++) { |
| 351 | trace_mm_filemap_delete_from_page_cache(pvec->pages[i]); |
| 352 | |
| 353 | unaccount_page_cache_page(mapping, pvec->pages[i]); |
| 354 | } |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 355 | page_cache_delete_batch(mapping, pvec); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 356 | xa_unlock_irqrestore(&mapping->i_pages, flags); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 357 | |
| 358 | for (i = 0; i < pagevec_count(pvec); i++) |
| 359 | page_cache_free_page(mapping, pvec->pages[i]); |
| 360 | } |
| 361 | |
Miklos Szeredi | d72d9e2 | 2016-07-29 14:10:57 +0200 | [diff] [blame] | 362 | int filemap_check_errors(struct address_space *mapping) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 363 | { |
| 364 | int ret = 0; |
| 365 | /* Check for outstanding write errors */ |
Jens Axboe | 7fcbbaf | 2014-05-22 11:54:16 -0700 | [diff] [blame] | 366 | if (test_bit(AS_ENOSPC, &mapping->flags) && |
| 367 | test_and_clear_bit(AS_ENOSPC, &mapping->flags)) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 368 | ret = -ENOSPC; |
Jens Axboe | 7fcbbaf | 2014-05-22 11:54:16 -0700 | [diff] [blame] | 369 | if (test_bit(AS_EIO, &mapping->flags) && |
| 370 | test_and_clear_bit(AS_EIO, &mapping->flags)) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 371 | ret = -EIO; |
| 372 | return ret; |
| 373 | } |
Miklos Szeredi | d72d9e2 | 2016-07-29 14:10:57 +0200 | [diff] [blame] | 374 | EXPORT_SYMBOL(filemap_check_errors); |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 375 | |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 376 | static int filemap_check_and_keep_errors(struct address_space *mapping) |
| 377 | { |
| 378 | /* Check for outstanding write errors */ |
| 379 | if (test_bit(AS_EIO, &mapping->flags)) |
| 380 | return -EIO; |
| 381 | if (test_bit(AS_ENOSPC, &mapping->flags)) |
| 382 | return -ENOSPC; |
| 383 | return 0; |
| 384 | } |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 387 | * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 388 | * @mapping: address space structure to write |
| 389 | * @start: offset in bytes where the range starts |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 390 | * @end: offset in bytes where the range ends (inclusive) |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 391 | * @sync_mode: enable synchronous operation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 393 | * Start writeback against all of a mapping's dirty pages that lie |
| 394 | * within the byte offsets <start, end> inclusive. |
| 395 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | * 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] | 397 | * opposed to a regular memory cleansing writeback. The difference between |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | * these two operations is that if a dirty page/buffer is encountered, it must |
| 399 | * be waited upon, and not just skipped over. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 400 | * |
| 401 | * Return: %0 on success, negative error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | */ |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 403 | int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
| 404 | loff_t end, int sync_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
| 406 | int ret; |
| 407 | struct writeback_control wbc = { |
| 408 | .sync_mode = sync_mode, |
Nick Piggin | 05fe478 | 2009-01-06 14:39:08 -0800 | [diff] [blame] | 409 | .nr_to_write = LONG_MAX, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 410 | .range_start = start, |
| 411 | .range_end = end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | if (!mapping_cap_writeback_dirty(mapping)) |
| 415 | return 0; |
| 416 | |
Tejun Heo | b16b1de | 2015-06-02 08:39:48 -0600 | [diff] [blame] | 417 | wbc_attach_fdatawrite_inode(&wbc, mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | ret = do_writepages(mapping, &wbc); |
Tejun Heo | b16b1de | 2015-06-02 08:39:48 -0600 | [diff] [blame] | 419 | wbc_detach_inode(&wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | static inline int __filemap_fdatawrite(struct address_space *mapping, |
| 424 | int sync_mode) |
| 425 | { |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 426 | return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | int filemap_fdatawrite(struct address_space *mapping) |
| 430 | { |
| 431 | return __filemap_fdatawrite(mapping, WB_SYNC_ALL); |
| 432 | } |
| 433 | EXPORT_SYMBOL(filemap_fdatawrite); |
| 434 | |
Jan Kara | f4c0a0f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 435 | int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 436 | loff_t end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
| 438 | return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL); |
| 439 | } |
Jan Kara | f4c0a0f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 440 | EXPORT_SYMBOL(filemap_fdatawrite_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 442 | /** |
| 443 | * filemap_flush - mostly a non-blocking flush |
| 444 | * @mapping: target address_space |
| 445 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | * This is a mostly non-blocking flush. Not suitable for data-integrity |
| 447 | * purposes - I/O may not be started against all dirty pages. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 448 | * |
| 449 | * Return: %0 on success, negative error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | */ |
| 451 | int filemap_flush(struct address_space *mapping) |
| 452 | { |
| 453 | return __filemap_fdatawrite(mapping, WB_SYNC_NONE); |
| 454 | } |
| 455 | EXPORT_SYMBOL(filemap_flush); |
| 456 | |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 457 | /** |
| 458 | * filemap_range_has_page - check if a page exists in range. |
| 459 | * @mapping: address space within which to check |
| 460 | * @start_byte: offset in bytes where the range starts |
| 461 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 462 | * |
| 463 | * Find at least one page in the range supplied, usually used to check if |
| 464 | * direct writing in this range will trigger a writeback. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 465 | * |
| 466 | * Return: %true if at least one page exists in the specified range, |
| 467 | * %false otherwise. |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 468 | */ |
| 469 | bool filemap_range_has_page(struct address_space *mapping, |
| 470 | loff_t start_byte, loff_t end_byte) |
| 471 | { |
Jan Kara | f7b6804 | 2017-09-06 16:21:40 -0700 | [diff] [blame] | 472 | struct page *page; |
Matthew Wilcox | 8fa8e53 | 2018-01-16 06:26:49 -0500 | [diff] [blame] | 473 | XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); |
| 474 | pgoff_t max = end_byte >> PAGE_SHIFT; |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 475 | |
| 476 | if (end_byte < start_byte) |
| 477 | return false; |
| 478 | |
Matthew Wilcox | 8fa8e53 | 2018-01-16 06:26:49 -0500 | [diff] [blame] | 479 | rcu_read_lock(); |
| 480 | for (;;) { |
| 481 | page = xas_find(&xas, max); |
| 482 | if (xas_retry(&xas, page)) |
| 483 | continue; |
| 484 | /* Shadow entries don't count */ |
| 485 | if (xa_is_value(page)) |
| 486 | continue; |
| 487 | /* |
| 488 | * We don't need to try to pin this page; we're about to |
| 489 | * release the RCU lock anyway. It is enough to know that |
| 490 | * there was a page here recently. |
| 491 | */ |
| 492 | break; |
| 493 | } |
| 494 | rcu_read_unlock(); |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 495 | |
Matthew Wilcox | 8fa8e53 | 2018-01-16 06:26:49 -0500 | [diff] [blame] | 496 | return page != NULL; |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 497 | } |
| 498 | EXPORT_SYMBOL(filemap_range_has_page); |
| 499 | |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 500 | static void __filemap_fdatawait_range(struct address_space *mapping, |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 501 | loff_t start_byte, loff_t end_byte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 503 | pgoff_t index = start_byte >> PAGE_SHIFT; |
| 504 | pgoff_t end = end_byte >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | struct pagevec pvec; |
| 506 | int nr_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 508 | if (end_byte < start_byte) |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 509 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 511 | pagevec_init(&pvec); |
Jan Kara | 312e9d2 | 2017-11-15 17:35:05 -0800 | [diff] [blame] | 512 | while (index <= end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | unsigned i; |
| 514 | |
Jan Kara | 312e9d2 | 2017-11-15 17:35:05 -0800 | [diff] [blame] | 515 | nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 516 | end, PAGECACHE_TAG_WRITEBACK); |
Jan Kara | 312e9d2 | 2017-11-15 17:35:05 -0800 | [diff] [blame] | 517 | if (!nr_pages) |
| 518 | break; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | for (i = 0; i < nr_pages; i++) { |
| 521 | struct page *page = pvec.pages[i]; |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | wait_on_page_writeback(page); |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 524 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | pagevec_release(&pvec); |
| 527 | cond_resched(); |
| 528 | } |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
| 532 | * filemap_fdatawait_range - wait for writeback to complete |
| 533 | * @mapping: address space structure to wait for |
| 534 | * @start_byte: offset in bytes where the range starts |
| 535 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 536 | * |
| 537 | * Walk the list of under-writeback pages of the given address space |
| 538 | * in the given range and wait for all of them. Check error status of |
| 539 | * the address space and return it. |
| 540 | * |
| 541 | * Since the error status of the address space is cleared by this function, |
| 542 | * callers are responsible for checking the return value and handling and/or |
| 543 | * reporting the error. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 544 | * |
| 545 | * Return: error status of the address space. |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 546 | */ |
| 547 | int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte, |
| 548 | loff_t end_byte) |
| 549 | { |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 550 | __filemap_fdatawait_range(mapping, start_byte, end_byte); |
| 551 | return filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
Jan Kara | d3bccb6f | 2009-08-17 19:30:27 +0200 | [diff] [blame] | 553 | EXPORT_SYMBOL(filemap_fdatawait_range); |
| 554 | |
| 555 | /** |
Jeff Layton | a823e45 | 2017-07-28 07:24:43 -0400 | [diff] [blame] | 556 | * file_fdatawait_range - wait for writeback to complete |
| 557 | * @file: file pointing to address space structure to wait for |
| 558 | * @start_byte: offset in bytes where the range starts |
| 559 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 560 | * |
| 561 | * Walk the list of under-writeback pages of the address space that file |
| 562 | * refers to, in the given range and wait for all of them. Check error |
| 563 | * status of the address space vs. the file->f_wb_err cursor and return it. |
| 564 | * |
| 565 | * Since the error status of the file is advanced by this function, |
| 566 | * callers are responsible for checking the return value and handling and/or |
| 567 | * reporting the error. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 568 | * |
| 569 | * Return: error status of the address space vs. the file->f_wb_err cursor. |
Jeff Layton | a823e45 | 2017-07-28 07:24:43 -0400 | [diff] [blame] | 570 | */ |
| 571 | int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte) |
| 572 | { |
| 573 | struct address_space *mapping = file->f_mapping; |
| 574 | |
| 575 | __filemap_fdatawait_range(mapping, start_byte, end_byte); |
| 576 | return file_check_and_advance_wb_err(file); |
| 577 | } |
| 578 | EXPORT_SYMBOL(file_fdatawait_range); |
| 579 | |
| 580 | /** |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 581 | * filemap_fdatawait_keep_errors - wait for writeback without clearing errors |
| 582 | * @mapping: address space structure to wait for |
| 583 | * |
| 584 | * Walk the list of under-writeback pages of the given address space |
| 585 | * and wait for all of them. Unlike filemap_fdatawait(), this function |
| 586 | * does not clear error status of the address space. |
| 587 | * |
| 588 | * Use this function if callers don't handle errors themselves. Expected |
| 589 | * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2), |
| 590 | * fsfreeze(8) |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 591 | * |
| 592 | * Return: error status of the address space. |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 593 | */ |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 594 | int filemap_fdatawait_keep_errors(struct address_space *mapping) |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 595 | { |
Jeff Layton | ffb959b | 2017-07-31 10:29:38 -0400 | [diff] [blame] | 596 | __filemap_fdatawait_range(mapping, 0, LLONG_MAX); |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 597 | return filemap_check_and_keep_errors(mapping); |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 598 | } |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 599 | EXPORT_SYMBOL(filemap_fdatawait_keep_errors); |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 600 | |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 601 | static bool mapping_needs_writeback(struct address_space *mapping) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 603 | return (!dax_mapping(mapping) && mapping->nrpages) || |
| 604 | (dax_mapping(mapping) && mapping->nrexceptional); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | int filemap_write_and_wait(struct address_space *mapping) |
| 608 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 609 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 611 | if (mapping_needs_writeback(mapping)) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 612 | err = filemap_fdatawrite(mapping); |
| 613 | /* |
| 614 | * Even if the above returned error, the pages may be |
| 615 | * written partially (e.g. -ENOSPC), so we wait for it. |
| 616 | * But the -EIO is special case, it may indicate the worst |
| 617 | * thing (e.g. bug) happened, so we avoid waiting for it. |
| 618 | */ |
| 619 | if (err != -EIO) { |
| 620 | int err2 = filemap_fdatawait(mapping); |
| 621 | if (!err) |
| 622 | err = err2; |
Jeff Layton | cbeaf95 | 2017-07-06 07:02:23 -0400 | [diff] [blame] | 623 | } else { |
| 624 | /* Clear any previously stored errors */ |
| 625 | filemap_check_errors(mapping); |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 626 | } |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 627 | } else { |
| 628 | err = filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 630 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 632 | EXPORT_SYMBOL(filemap_write_and_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 634 | /** |
| 635 | * filemap_write_and_wait_range - write out & wait on a file range |
| 636 | * @mapping: the address_space for the pages |
| 637 | * @lstart: offset in bytes where the range starts |
| 638 | * @lend: offset in bytes where the range ends (inclusive) |
| 639 | * |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 640 | * Write out and wait upon file offsets lstart->lend, inclusive. |
| 641 | * |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 642 | * Note that @lend is inclusive (describes the last byte to be written) so |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 643 | * that this function can be used to write to the very end-of-file (end = -1). |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 644 | * |
| 645 | * Return: error status of the address space. |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 646 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | int filemap_write_and_wait_range(struct address_space *mapping, |
| 648 | loff_t lstart, loff_t lend) |
| 649 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 650 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 652 | if (mapping_needs_writeback(mapping)) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 653 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 654 | WB_SYNC_ALL); |
| 655 | /* See comment of filemap_write_and_wait() */ |
| 656 | if (err != -EIO) { |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 657 | int err2 = filemap_fdatawait_range(mapping, |
| 658 | lstart, lend); |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 659 | if (!err) |
| 660 | err = err2; |
Jeff Layton | cbeaf95 | 2017-07-06 07:02:23 -0400 | [diff] [blame] | 661 | } else { |
| 662 | /* Clear any previously stored errors */ |
| 663 | filemap_check_errors(mapping); |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 664 | } |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 665 | } else { |
| 666 | err = filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 668 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
Chris Mason | f699558 | 2009-04-15 13:22:37 -0400 | [diff] [blame] | 670 | EXPORT_SYMBOL(filemap_write_and_wait_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 672 | void __filemap_set_wb_err(struct address_space *mapping, int err) |
| 673 | { |
Jeff Layton | 3acdfd2 | 2017-07-24 06:22:15 -0400 | [diff] [blame] | 674 | errseq_t eseq = errseq_set(&mapping->wb_err, err); |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 675 | |
| 676 | trace_filemap_set_wb_err(mapping, eseq); |
| 677 | } |
| 678 | EXPORT_SYMBOL(__filemap_set_wb_err); |
| 679 | |
| 680 | /** |
| 681 | * file_check_and_advance_wb_err - report wb error (if any) that was previously |
| 682 | * and advance wb_err to current one |
| 683 | * @file: struct file on which the error is being reported |
| 684 | * |
| 685 | * When userland calls fsync (or something like nfsd does the equivalent), we |
| 686 | * want to report any writeback errors that occurred since the last fsync (or |
| 687 | * since the file was opened if there haven't been any). |
| 688 | * |
| 689 | * Grab the wb_err from the mapping. If it matches what we have in the file, |
| 690 | * then just quickly return 0. The file is all caught up. |
| 691 | * |
| 692 | * If it doesn't match, then take the mapping value, set the "seen" flag in |
| 693 | * it and try to swap it into place. If it works, or another task beat us |
| 694 | * to it with the new value, then update the f_wb_err and return the error |
| 695 | * portion. The error at this point must be reported via proper channels |
| 696 | * (a'la fsync, or NFS COMMIT operation, etc.). |
| 697 | * |
| 698 | * While we handle mapping->wb_err with atomic operations, the f_wb_err |
| 699 | * value is protected by the f_lock since we must ensure that it reflects |
| 700 | * the latest value swapped in for this file descriptor. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 701 | * |
| 702 | * Return: %0 on success, negative error code otherwise. |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 703 | */ |
| 704 | int file_check_and_advance_wb_err(struct file *file) |
| 705 | { |
| 706 | int err = 0; |
| 707 | errseq_t old = READ_ONCE(file->f_wb_err); |
| 708 | struct address_space *mapping = file->f_mapping; |
| 709 | |
| 710 | /* Locklessly handle the common case where nothing has changed */ |
| 711 | if (errseq_check(&mapping->wb_err, old)) { |
| 712 | /* Something changed, must use slow path */ |
| 713 | spin_lock(&file->f_lock); |
| 714 | old = file->f_wb_err; |
| 715 | err = errseq_check_and_advance(&mapping->wb_err, |
| 716 | &file->f_wb_err); |
| 717 | trace_file_check_and_advance_wb_err(file, old); |
| 718 | spin_unlock(&file->f_lock); |
| 719 | } |
Jeff Layton | f4e222c | 2017-10-03 16:15:25 -0700 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * We're mostly using this function as a drop in replacement for |
| 723 | * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect |
| 724 | * that the legacy code would have had on these flags. |
| 725 | */ |
| 726 | clear_bit(AS_EIO, &mapping->flags); |
| 727 | clear_bit(AS_ENOSPC, &mapping->flags); |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 728 | return err; |
| 729 | } |
| 730 | EXPORT_SYMBOL(file_check_and_advance_wb_err); |
| 731 | |
| 732 | /** |
| 733 | * file_write_and_wait_range - write out & wait on a file range |
| 734 | * @file: file pointing to address_space with pages |
| 735 | * @lstart: offset in bytes where the range starts |
| 736 | * @lend: offset in bytes where the range ends (inclusive) |
| 737 | * |
| 738 | * Write out and wait upon file offsets lstart->lend, inclusive. |
| 739 | * |
| 740 | * Note that @lend is inclusive (describes the last byte to be written) so |
| 741 | * that this function can be used to write to the very end-of-file (end = -1). |
| 742 | * |
| 743 | * After writing out and waiting on the data, we check and advance the |
| 744 | * f_wb_err cursor to the latest value, and return any errors detected there. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 745 | * |
| 746 | * Return: %0 on success, negative error code otherwise. |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 747 | */ |
| 748 | int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend) |
| 749 | { |
| 750 | int err = 0, err2; |
| 751 | struct address_space *mapping = file->f_mapping; |
| 752 | |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 753 | if (mapping_needs_writeback(mapping)) { |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 754 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 755 | WB_SYNC_ALL); |
| 756 | /* See comment of filemap_write_and_wait() */ |
| 757 | if (err != -EIO) |
| 758 | __filemap_fdatawait_range(mapping, lstart, lend); |
| 759 | } |
| 760 | err2 = file_check_and_advance_wb_err(file); |
| 761 | if (!err) |
| 762 | err = err2; |
| 763 | return err; |
| 764 | } |
| 765 | EXPORT_SYMBOL(file_write_and_wait_range); |
| 766 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 767 | /** |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 768 | * replace_page_cache_page - replace a pagecache page with a new one |
| 769 | * @old: page to be replaced |
| 770 | * @new: page to replace with |
| 771 | * @gfp_mask: allocation mode |
| 772 | * |
| 773 | * This function replaces a page in the pagecache with a new one. On |
| 774 | * success it acquires the pagecache reference for the new page and |
| 775 | * drops it for the old page. Both the old and new pages must be |
| 776 | * locked. This function does not add the new page to the LRU, the |
| 777 | * caller must do that. |
| 778 | * |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 779 | * The remove + add is atomic. This function cannot fail. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 780 | * |
| 781 | * Return: %0 |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 782 | */ |
| 783 | int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask) |
| 784 | { |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 785 | struct address_space *mapping = old->mapping; |
| 786 | void (*freepage)(struct page *) = mapping->a_ops->freepage; |
| 787 | pgoff_t offset = old->index; |
| 788 | XA_STATE(xas, &mapping->i_pages, offset); |
| 789 | unsigned long flags; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 790 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 791 | VM_BUG_ON_PAGE(!PageLocked(old), old); |
| 792 | VM_BUG_ON_PAGE(!PageLocked(new), new); |
| 793 | VM_BUG_ON_PAGE(new->mapping, new); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 794 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 795 | get_page(new); |
| 796 | new->mapping = mapping; |
| 797 | new->index = offset; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 798 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 799 | xas_lock_irqsave(&xas, flags); |
| 800 | xas_store(&xas, new); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 801 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 802 | old->mapping = NULL; |
| 803 | /* hugetlb pages do not participate in page cache accounting. */ |
| 804 | if (!PageHuge(old)) |
| 805 | __dec_node_page_state(new, NR_FILE_PAGES); |
| 806 | if (!PageHuge(new)) |
| 807 | __inc_node_page_state(new, NR_FILE_PAGES); |
| 808 | if (PageSwapBacked(old)) |
| 809 | __dec_node_page_state(new, NR_SHMEM); |
| 810 | if (PageSwapBacked(new)) |
| 811 | __inc_node_page_state(new, NR_SHMEM); |
| 812 | xas_unlock_irqrestore(&xas, flags); |
| 813 | mem_cgroup_migrate(old, new); |
| 814 | if (freepage) |
| 815 | freepage(old); |
| 816 | put_page(old); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 817 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 818 | return 0; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 819 | } |
| 820 | EXPORT_SYMBOL_GPL(replace_page_cache_page); |
| 821 | |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 822 | static int __add_to_page_cache_locked(struct page *page, |
| 823 | struct address_space *mapping, |
| 824 | pgoff_t offset, gfp_t gfp_mask, |
| 825 | void **shadowp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 827 | XA_STATE(xas, &mapping->i_pages, offset); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 828 | int huge = PageHuge(page); |
| 829 | struct mem_cgroup *memcg; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 830 | int error; |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 831 | void *old; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 832 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 833 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 834 | VM_BUG_ON_PAGE(PageSwapBacked(page), page); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 835 | mapping_set_update(&xas, mapping); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 836 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 837 | if (!huge) { |
| 838 | error = mem_cgroup_try_charge(page, current->mm, |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 839 | gfp_mask, &memcg, false); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 840 | if (error) |
| 841 | return error; |
| 842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 844 | get_page(page); |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 845 | page->mapping = mapping; |
| 846 | page->index = offset; |
| 847 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 848 | do { |
| 849 | xas_lock_irq(&xas); |
| 850 | old = xas_load(&xas); |
| 851 | if (old && !xa_is_value(old)) |
| 852 | xas_set_err(&xas, -EEXIST); |
| 853 | xas_store(&xas, page); |
| 854 | if (xas_error(&xas)) |
| 855 | goto unlock; |
Michal Hocko | 4165b9b4 | 2015-06-24 16:57:24 -0700 | [diff] [blame] | 856 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 857 | if (xa_is_value(old)) { |
| 858 | mapping->nrexceptional--; |
| 859 | if (shadowp) |
| 860 | *shadowp = old; |
| 861 | } |
| 862 | mapping->nrpages++; |
| 863 | |
| 864 | /* hugetlb pages do not participate in page cache accounting */ |
| 865 | if (!huge) |
| 866 | __inc_node_page_state(page, NR_FILE_PAGES); |
| 867 | unlock: |
| 868 | xas_unlock_irq(&xas); |
| 869 | } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK)); |
| 870 | |
| 871 | if (xas_error(&xas)) |
| 872 | goto error; |
| 873 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 874 | if (!huge) |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 875 | mem_cgroup_commit_charge(page, memcg, false, false); |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 876 | trace_mm_filemap_add_to_page_cache(page); |
| 877 | return 0; |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 878 | error: |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 879 | page->mapping = NULL; |
| 880 | /* Leave page->index set: truncation relies upon it */ |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 881 | if (!huge) |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 882 | mem_cgroup_cancel_charge(page, memcg, false); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 883 | put_page(page); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 884 | return xas_error(&xas); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Josef Bacik | cfcbfb1 | 2019-05-13 17:21:04 -0700 | [diff] [blame] | 886 | ALLOW_ERROR_INJECTION(__add_to_page_cache_locked, ERRNO); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 887 | |
| 888 | /** |
| 889 | * add_to_page_cache_locked - add a locked page to the pagecache |
| 890 | * @page: page to add |
| 891 | * @mapping: the page's address_space |
| 892 | * @offset: page index |
| 893 | * @gfp_mask: page allocation mode |
| 894 | * |
| 895 | * This function is used to add a page to the pagecache. It must be locked. |
| 896 | * This function does not add the page to the LRU. The caller must do that. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 897 | * |
| 898 | * Return: %0 on success, negative error code otherwise. |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 899 | */ |
| 900 | int add_to_page_cache_locked(struct page *page, struct address_space *mapping, |
| 901 | pgoff_t offset, gfp_t gfp_mask) |
| 902 | { |
| 903 | return __add_to_page_cache_locked(page, mapping, offset, |
| 904 | gfp_mask, NULL); |
| 905 | } |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 906 | EXPORT_SYMBOL(add_to_page_cache_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
| 908 | 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] | 909 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | { |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 911 | void *shadow = NULL; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 912 | int ret; |
| 913 | |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 914 | __SetPageLocked(page); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 915 | ret = __add_to_page_cache_locked(page, mapping, offset, |
| 916 | gfp_mask, &shadow); |
| 917 | if (unlikely(ret)) |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 918 | __ClearPageLocked(page); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 919 | else { |
| 920 | /* |
| 921 | * The page might have been evicted from cache only |
| 922 | * recently, in which case it should be activated like |
| 923 | * any other repeatedly accessed page. |
Rik van Riel | f0281a0 | 2016-05-20 16:56:25 -0700 | [diff] [blame] | 924 | * The exception is pages getting rewritten; evicting other |
| 925 | * data from the working set, only to cache data that will |
| 926 | * get overwritten with something else, is a waste of memory. |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 927 | */ |
Johannes Weiner | 1899ad1 | 2018-10-26 15:06:04 -0700 | [diff] [blame] | 928 | WARN_ON_ONCE(PageActive(page)); |
| 929 | if (!(gfp_mask & __GFP_WRITE) && shadow) |
| 930 | workingset_refault(page, shadow); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 931 | lru_cache_add(page); |
| 932 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return ret; |
| 934 | } |
Evgeniy Polyakov | 18bc0bb | 2009-02-09 17:02:42 +0300 | [diff] [blame] | 935 | EXPORT_SYMBOL_GPL(add_to_page_cache_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 937 | #ifdef CONFIG_NUMA |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 938 | struct page *__page_cache_alloc(gfp_t gfp) |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 939 | { |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 940 | int n; |
| 941 | struct page *page; |
| 942 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 943 | if (cpuset_do_page_mem_spread()) { |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 944 | unsigned int cpuset_mems_cookie; |
| 945 | do { |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 946 | cpuset_mems_cookie = read_mems_allowed_begin(); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 947 | n = cpuset_mem_spread_node(); |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 948 | page = __alloc_pages_node(n, gfp, 0); |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 949 | } while (!page && read_mems_allowed_retry(cpuset_mems_cookie)); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 950 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 951 | return page; |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 952 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 953 | return alloc_pages(gfp, 0); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 954 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 955 | EXPORT_SYMBOL(__page_cache_alloc); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 956 | #endif |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* |
| 959 | * In order to wait for pages to become available there must be |
| 960 | * waitqueues associated with pages. By using a hash table of |
| 961 | * waitqueues where the bucket discipline is to maintain all |
| 962 | * waiters on the same queue and wake all when any of the pages |
| 963 | * become available, and for the woken contexts to check to be |
| 964 | * sure the appropriate page became available, this saves space |
| 965 | * at a cost of "thundering herd" phenomena during rare hash |
| 966 | * collisions. |
| 967 | */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 968 | #define PAGE_WAIT_TABLE_BITS 8 |
| 969 | #define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS) |
| 970 | static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned; |
| 971 | |
| 972 | static wait_queue_head_t *page_waitqueue(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 974 | return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 976 | |
| 977 | void __init pagecache_init(void) |
| 978 | { |
| 979 | int i; |
| 980 | |
| 981 | for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++) |
| 982 | init_waitqueue_head(&page_wait_table[i]); |
| 983 | |
| 984 | page_writeback_init(); |
| 985 | } |
| 986 | |
Linus Torvalds | 3510ca2 | 2017-08-27 13:55:12 -0700 | [diff] [blame] | 987 | /* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 988 | struct wait_page_key { |
| 989 | struct page *page; |
| 990 | int bit_nr; |
| 991 | int page_match; |
| 992 | }; |
| 993 | |
| 994 | struct wait_page_queue { |
| 995 | struct page *page; |
| 996 | int bit_nr; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 997 | wait_queue_entry_t wait; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 998 | }; |
| 999 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1000 | static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg) |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1001 | { |
| 1002 | struct wait_page_key *key = arg; |
| 1003 | struct wait_page_queue *wait_page |
| 1004 | = container_of(wait, struct wait_page_queue, wait); |
| 1005 | |
| 1006 | if (wait_page->page != key->page) |
| 1007 | return 0; |
| 1008 | key->page_match = 1; |
| 1009 | |
| 1010 | if (wait_page->bit_nr != key->bit_nr) |
| 1011 | return 0; |
Linus Torvalds | 3510ca2 | 2017-08-27 13:55:12 -0700 | [diff] [blame] | 1012 | |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1013 | /* |
| 1014 | * Stop walking if it's locked. |
| 1015 | * Is this safe if put_and_wait_on_page_locked() is in use? |
| 1016 | * Yes: the waker must hold a reference to this page, and if PG_locked |
| 1017 | * has now already been set by another task, that task must also hold |
| 1018 | * a reference to the *same usage* of this page; so there is no need |
| 1019 | * to walk on to wake even the put_and_wait_on_page_locked() callers. |
| 1020 | */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1021 | if (test_bit(key->bit_nr, &key->page->flags)) |
Linus Torvalds | 3510ca2 | 2017-08-27 13:55:12 -0700 | [diff] [blame] | 1022 | return -1; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1023 | |
| 1024 | return autoremove_wake_function(wait, mode, sync, key); |
| 1025 | } |
| 1026 | |
Nicholas Piggin | 74d81bf | 2017-02-22 15:44:41 -0800 | [diff] [blame] | 1027 | static void wake_up_page_bit(struct page *page, int bit_nr) |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1028 | { |
| 1029 | wait_queue_head_t *q = page_waitqueue(page); |
| 1030 | struct wait_page_key key; |
| 1031 | unsigned long flags; |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 1032 | wait_queue_entry_t bookmark; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1033 | |
| 1034 | key.page = page; |
| 1035 | key.bit_nr = bit_nr; |
| 1036 | key.page_match = 0; |
| 1037 | |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 1038 | bookmark.flags = 0; |
| 1039 | bookmark.private = NULL; |
| 1040 | bookmark.func = NULL; |
| 1041 | INIT_LIST_HEAD(&bookmark.entry); |
| 1042 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1043 | spin_lock_irqsave(&q->lock, flags); |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 1044 | __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark); |
| 1045 | |
| 1046 | while (bookmark.flags & WQ_FLAG_BOOKMARK) { |
| 1047 | /* |
| 1048 | * Take a breather from holding the lock, |
| 1049 | * allow pages that finish wake up asynchronously |
| 1050 | * to acquire the lock and remove themselves |
| 1051 | * from wait queue |
| 1052 | */ |
| 1053 | spin_unlock_irqrestore(&q->lock, flags); |
| 1054 | cpu_relax(); |
| 1055 | spin_lock_irqsave(&q->lock, flags); |
| 1056 | __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark); |
| 1057 | } |
| 1058 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1059 | /* |
| 1060 | * It is possible for other pages to have collided on the waitqueue |
| 1061 | * hash, so in that case check for a page match. That prevents a long- |
| 1062 | * term waiter |
| 1063 | * |
| 1064 | * It is still possible to miss a case here, when we woke page waiters |
| 1065 | * and removed them from the waitqueue, but there are still other |
| 1066 | * page waiters. |
| 1067 | */ |
| 1068 | if (!waitqueue_active(q) || !key.page_match) { |
| 1069 | ClearPageWaiters(page); |
| 1070 | /* |
| 1071 | * It's possible to miss clearing Waiters here, when we woke |
| 1072 | * our page waiters, but the hashed waitqueue has waiters for |
| 1073 | * other pages on it. |
| 1074 | * |
| 1075 | * That's okay, it's a rare case. The next waker will clear it. |
| 1076 | */ |
| 1077 | } |
| 1078 | spin_unlock_irqrestore(&q->lock, flags); |
| 1079 | } |
Nicholas Piggin | 74d81bf | 2017-02-22 15:44:41 -0800 | [diff] [blame] | 1080 | |
| 1081 | static void wake_up_page(struct page *page, int bit) |
| 1082 | { |
| 1083 | if (!PageWaiters(page)) |
| 1084 | return; |
| 1085 | wake_up_page_bit(page, bit); |
| 1086 | } |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1087 | |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1088 | /* |
| 1089 | * A choice of three behaviors for wait_on_page_bit_common(): |
| 1090 | */ |
| 1091 | enum behavior { |
| 1092 | EXCLUSIVE, /* Hold ref to page and take the bit when woken, like |
| 1093 | * __lock_page() waiting on then setting PG_locked. |
| 1094 | */ |
| 1095 | SHARED, /* Hold ref to page and check the bit when woken, like |
| 1096 | * wait_on_page_writeback() waiting on PG_writeback. |
| 1097 | */ |
| 1098 | DROP, /* Drop ref to page before wait, no check when woken, |
| 1099 | * like put_and_wait_on_page_locked() on PG_locked. |
| 1100 | */ |
| 1101 | }; |
| 1102 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1103 | static inline int wait_on_page_bit_common(wait_queue_head_t *q, |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1104 | struct page *page, int bit_nr, int state, enum behavior behavior) |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1105 | { |
| 1106 | struct wait_page_queue wait_page; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1107 | wait_queue_entry_t *wait = &wait_page.wait; |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1108 | bool bit_is_set; |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1109 | bool thrashing = false; |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1110 | bool delayacct = false; |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1111 | unsigned long pflags; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1112 | int ret = 0; |
| 1113 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1114 | if (bit_nr == PG_locked && |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1115 | !PageUptodate(page) && PageWorkingset(page)) { |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1116 | if (!PageSwapBacked(page)) { |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1117 | delayacct_thrashing_start(); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1118 | delayacct = true; |
| 1119 | } |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1120 | psi_memstall_enter(&pflags); |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1121 | thrashing = true; |
| 1122 | } |
| 1123 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1124 | init_wait(wait); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1125 | wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1126 | wait->func = wake_page_function; |
| 1127 | wait_page.page = page; |
| 1128 | wait_page.bit_nr = bit_nr; |
| 1129 | |
| 1130 | for (;;) { |
| 1131 | spin_lock_irq(&q->lock); |
| 1132 | |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1133 | if (likely(list_empty(&wait->entry))) { |
Linus Torvalds | 3510ca2 | 2017-08-27 13:55:12 -0700 | [diff] [blame] | 1134 | __add_wait_queue_entry_tail(q, wait); |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1135 | SetPageWaiters(page); |
| 1136 | } |
| 1137 | |
| 1138 | set_current_state(state); |
| 1139 | |
| 1140 | spin_unlock_irq(&q->lock); |
| 1141 | |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1142 | bit_is_set = test_bit(bit_nr, &page->flags); |
| 1143 | if (behavior == DROP) |
| 1144 | put_page(page); |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1145 | |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1146 | if (likely(bit_is_set)) |
| 1147 | io_schedule(); |
| 1148 | |
| 1149 | if (behavior == EXCLUSIVE) { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1150 | if (!test_and_set_bit_lock(bit_nr, &page->flags)) |
| 1151 | break; |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1152 | } else if (behavior == SHARED) { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1153 | if (!test_bit(bit_nr, &page->flags)) |
| 1154 | break; |
| 1155 | } |
Linus Torvalds | a8b169a | 2017-08-27 16:25:09 -0700 | [diff] [blame] | 1156 | |
Davidlohr Bueso | fa45f11 | 2019-01-03 15:28:55 -0800 | [diff] [blame] | 1157 | if (signal_pending_state(state, current)) { |
Linus Torvalds | a8b169a | 2017-08-27 16:25:09 -0700 | [diff] [blame] | 1158 | ret = -EINTR; |
| 1159 | break; |
| 1160 | } |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1161 | |
| 1162 | if (behavior == DROP) { |
| 1163 | /* |
| 1164 | * We can no longer safely access page->flags: |
| 1165 | * even if CONFIG_MEMORY_HOTREMOVE is not enabled, |
| 1166 | * there is a risk of waiting forever on a page reused |
| 1167 | * for something that keeps it locked indefinitely. |
| 1168 | * But best check for -EINTR above before breaking. |
| 1169 | */ |
| 1170 | break; |
| 1171 | } |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | finish_wait(q, wait); |
| 1175 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1176 | if (thrashing) { |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1177 | if (delayacct) |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1178 | delayacct_thrashing_end(); |
| 1179 | psi_memstall_leave(&pflags); |
| 1180 | } |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1181 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1182 | /* |
| 1183 | * A signal could leave PageWaiters set. Clearing it here if |
| 1184 | * !waitqueue_active would be possible (by open-coding finish_wait), |
| 1185 | * but still fail to catch it in the case of wait hash collision. We |
| 1186 | * already can fail to clear wait hash collision cases, so don't |
| 1187 | * bother with signals either. |
| 1188 | */ |
| 1189 | |
| 1190 | return ret; |
| 1191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 1193 | void wait_on_page_bit(struct page *page, int bit_nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1195 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1196 | wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
| 1198 | EXPORT_SYMBOL(wait_on_page_bit); |
| 1199 | |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 1200 | int wait_on_page_bit_killable(struct page *page, int bit_nr) |
| 1201 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1202 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1203 | return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED); |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 1204 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 1205 | EXPORT_SYMBOL(wait_on_page_bit_killable); |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 1206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | /** |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1208 | * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked |
| 1209 | * @page: The page to wait for. |
| 1210 | * |
| 1211 | * The caller should hold a reference on @page. They expect the page to |
| 1212 | * become unlocked relatively soon, but do not wish to hold up migration |
| 1213 | * (for example) by holding the reference while waiting for the page to |
| 1214 | * come unlocked. After this function returns, the caller should not |
| 1215 | * dereference @page. |
| 1216 | */ |
| 1217 | void put_and_wait_on_page_locked(struct page *page) |
| 1218 | { |
| 1219 | wait_queue_head_t *q; |
| 1220 | |
| 1221 | page = compound_head(page); |
| 1222 | q = page_waitqueue(page); |
| 1223 | wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP); |
| 1224 | } |
| 1225 | |
| 1226 | /** |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1227 | * 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] | 1228 | * @page: Page defining the wait queue of interest |
| 1229 | * @waiter: Waiter to add to the queue |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1230 | * |
| 1231 | * Add an arbitrary @waiter to the wait queue for the nominated @page. |
| 1232 | */ |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1233 | void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter) |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1234 | { |
| 1235 | wait_queue_head_t *q = page_waitqueue(page); |
| 1236 | unsigned long flags; |
| 1237 | |
| 1238 | spin_lock_irqsave(&q->lock, flags); |
Linus Torvalds | 9c3a815 | 2017-08-28 16:45:40 -0700 | [diff] [blame] | 1239 | __add_wait_queue_entry_tail(q, waiter); |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1240 | SetPageWaiters(page); |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1241 | spin_unlock_irqrestore(&q->lock, flags); |
| 1242 | } |
| 1243 | EXPORT_SYMBOL_GPL(add_page_wait_queue); |
| 1244 | |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1245 | #ifndef clear_bit_unlock_is_negative_byte |
| 1246 | |
| 1247 | /* |
| 1248 | * PG_waiters is the high bit in the same byte as PG_lock. |
| 1249 | * |
| 1250 | * On x86 (and on many other architectures), we can clear PG_lock and |
| 1251 | * test the sign bit at the same time. But if the architecture does |
| 1252 | * not support that special operation, we just do this all by hand |
| 1253 | * instead. |
| 1254 | * |
| 1255 | * The read of PG_waiters has to be after (or concurrently with) PG_locked |
| 1256 | * being cleared, but a memory barrier should be unneccssary since it is |
| 1257 | * in the same byte as PG_locked. |
| 1258 | */ |
| 1259 | static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem) |
| 1260 | { |
| 1261 | clear_bit_unlock(nr, mem); |
| 1262 | /* smp_mb__after_atomic(); */ |
Olof Johansson | 98473f9 | 2016-12-29 14:16:07 -0800 | [diff] [blame] | 1263 | return test_bit(PG_waiters, mem); |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | #endif |
| 1267 | |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1268 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1269 | * unlock_page - unlock a locked page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | * @page: the page |
| 1271 | * |
| 1272 | * Unlocks the page and wakes up sleepers in ___wait_on_page_locked(). |
| 1273 | * Also wakes sleepers in wait_on_page_writeback() because the wakeup |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 1274 | * mechanism between PageLocked pages and PageWriteback pages is shared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. |
| 1276 | * |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1277 | * Note that this depends on PG_waiters being the sign bit in the byte |
| 1278 | * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to |
| 1279 | * clear the PG_locked bit and test PG_waiters at the same time fairly |
| 1280 | * portably (architectures that do LL/SC can test any bit, while x86 can |
| 1281 | * test the sign bit). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | */ |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 1283 | void unlock_page(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | { |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1285 | BUILD_BUG_ON(PG_waiters != 7); |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 1286 | page = compound_head(page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1287 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1288 | if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags)) |
| 1289 | wake_up_page_bit(page, PG_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | } |
| 1291 | EXPORT_SYMBOL(unlock_page); |
| 1292 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1293 | /** |
| 1294 | * end_page_writeback - end writeback against a page |
| 1295 | * @page: the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | */ |
| 1297 | void end_page_writeback(struct page *page) |
| 1298 | { |
Mel Gorman | 888cf2d | 2014-06-04 16:10:34 -0700 | [diff] [blame] | 1299 | /* |
| 1300 | * TestClearPageReclaim could be used here but it is an atomic |
| 1301 | * operation and overkill in this particular case. Failing to |
| 1302 | * shuffle a page marked for immediate reclaim is too mild to |
| 1303 | * justify taking an atomic operation penalty at the end of |
| 1304 | * ever page writeback. |
| 1305 | */ |
| 1306 | if (PageReclaim(page)) { |
| 1307 | ClearPageReclaim(page); |
Miklos Szeredi | ac6aadb | 2008-04-28 02:12:38 -0700 | [diff] [blame] | 1308 | rotate_reclaimable_page(page); |
Mel Gorman | 888cf2d | 2014-06-04 16:10:34 -0700 | [diff] [blame] | 1309 | } |
Miklos Szeredi | ac6aadb | 2008-04-28 02:12:38 -0700 | [diff] [blame] | 1310 | |
| 1311 | if (!test_clear_page_writeback(page)) |
| 1312 | BUG(); |
| 1313 | |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1314 | smp_mb__after_atomic(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | wake_up_page(page, PG_writeback); |
| 1316 | } |
| 1317 | EXPORT_SYMBOL(end_page_writeback); |
| 1318 | |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1319 | /* |
| 1320 | * After completing I/O on a page, call this routine to update the page |
| 1321 | * flags appropriately |
| 1322 | */ |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 1323 | void page_endio(struct page *page, bool is_write, int err) |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1324 | { |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 1325 | if (!is_write) { |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1326 | if (!err) { |
| 1327 | SetPageUptodate(page); |
| 1328 | } else { |
| 1329 | ClearPageUptodate(page); |
| 1330 | SetPageError(page); |
| 1331 | } |
| 1332 | unlock_page(page); |
Mike Christie | abf5454 | 2016-08-04 14:23:34 -0600 | [diff] [blame] | 1333 | } else { |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1334 | if (err) { |
Minchan Kim | dd8416c | 2017-02-24 14:59:59 -0800 | [diff] [blame] | 1335 | struct address_space *mapping; |
| 1336 | |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1337 | SetPageError(page); |
Minchan Kim | dd8416c | 2017-02-24 14:59:59 -0800 | [diff] [blame] | 1338 | mapping = page_mapping(page); |
| 1339 | if (mapping) |
| 1340 | mapping_set_error(mapping, err); |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1341 | } |
| 1342 | end_page_writeback(page); |
| 1343 | } |
| 1344 | } |
| 1345 | EXPORT_SYMBOL_GPL(page_endio); |
| 1346 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1347 | /** |
| 1348 | * __lock_page - get a lock on the page, assuming we need to sleep to get it |
Randy Dunlap | 8706675 | 2017-02-22 15:44:44 -0800 | [diff] [blame] | 1349 | * @__page: the page to lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1351 | void __lock_page(struct page *__page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1353 | struct page *page = compound_head(__page); |
| 1354 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1355 | wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, |
| 1356 | EXCLUSIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | } |
| 1358 | EXPORT_SYMBOL(__lock_page); |
| 1359 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1360 | int __lock_page_killable(struct page *__page) |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 1361 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1362 | struct page *page = compound_head(__page); |
| 1363 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1364 | return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, |
| 1365 | EXCLUSIVE); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 1366 | } |
Evgeniy Polyakov | 18bc0bb | 2009-02-09 17:02:42 +0300 | [diff] [blame] | 1367 | EXPORT_SYMBOL_GPL(__lock_page_killable); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 1368 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 1369 | /* |
| 1370 | * Return values: |
| 1371 | * 1 - page is locked; mmap_sem is still held. |
| 1372 | * 0 - page is not locked. |
| 1373 | * mmap_sem has been released (up_read()), unless flags had both |
| 1374 | * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in |
| 1375 | * which case mmap_sem is still held. |
| 1376 | * |
| 1377 | * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1 |
| 1378 | * with the page locked and the mmap_sem unperturbed. |
| 1379 | */ |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1380 | int __lock_page_or_retry(struct page *page, struct mm_struct *mm, |
| 1381 | unsigned int flags) |
| 1382 | { |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1383 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
| 1384 | /* |
| 1385 | * CAUTION! In this case, mmap_sem is not released |
| 1386 | * even though return 0. |
| 1387 | */ |
| 1388 | if (flags & FAULT_FLAG_RETRY_NOWAIT) |
| 1389 | return 0; |
| 1390 | |
| 1391 | up_read(&mm->mmap_sem); |
| 1392 | if (flags & FAULT_FLAG_KILLABLE) |
| 1393 | wait_on_page_locked_killable(page); |
| 1394 | else |
Gleb Natapov | 318b275 | 2011-03-22 16:30:51 -0700 | [diff] [blame] | 1395 | wait_on_page_locked(page); |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1396 | return 0; |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1397 | } else { |
| 1398 | if (flags & FAULT_FLAG_KILLABLE) { |
| 1399 | int ret; |
| 1400 | |
| 1401 | ret = __lock_page_killable(page); |
| 1402 | if (ret) { |
| 1403 | up_read(&mm->mmap_sem); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | } else |
| 1407 | __lock_page(page); |
| 1408 | return 1; |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1412 | /** |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1413 | * page_cache_next_miss() - Find the next gap in the page cache. |
| 1414 | * @mapping: Mapping. |
| 1415 | * @index: Index. |
| 1416 | * @max_scan: Maximum range to search. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1417 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1418 | * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the |
| 1419 | * gap with the lowest index. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1420 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1421 | * This function may be called under the rcu_read_lock. However, this will |
| 1422 | * not atomically search a snapshot of the cache at a single point in time. |
| 1423 | * For example, if a gap is created at index 5, then subsequently a gap is |
| 1424 | * created at index 10, page_cache_next_miss covering both indices may |
| 1425 | * return 10 if called under the rcu_read_lock. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1426 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1427 | * Return: The index of the gap if found, otherwise an index outside the |
| 1428 | * range specified (in which case 'return - index >= max_scan' will be true). |
| 1429 | * In the rare case of index wrap-around, 0 will be returned. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1430 | */ |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1431 | pgoff_t page_cache_next_miss(struct address_space *mapping, |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1432 | pgoff_t index, unsigned long max_scan) |
| 1433 | { |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1434 | XA_STATE(xas, &mapping->i_pages, index); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1435 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1436 | while (max_scan--) { |
| 1437 | void *entry = xas_next(&xas); |
| 1438 | if (!entry || xa_is_value(entry)) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1439 | break; |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1440 | if (xas.xa_index == 0) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1444 | return xas.xa_index; |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1445 | } |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1446 | EXPORT_SYMBOL(page_cache_next_miss); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1447 | |
| 1448 | /** |
Laurent Dufour | 2346a56 | 2019-05-13 17:21:29 -0700 | [diff] [blame] | 1449 | * page_cache_prev_miss() - Find the previous gap in the page cache. |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1450 | * @mapping: Mapping. |
| 1451 | * @index: Index. |
| 1452 | * @max_scan: Maximum range to search. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1453 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1454 | * Search the range [max(index - max_scan + 1, 0), index] for the |
| 1455 | * gap with the highest index. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1456 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1457 | * This function may be called under the rcu_read_lock. However, this will |
| 1458 | * not atomically search a snapshot of the cache at a single point in time. |
| 1459 | * For example, if a gap is created at index 10, then subsequently a gap is |
| 1460 | * created at index 5, page_cache_prev_miss() covering both indices may |
| 1461 | * return 5 if called under the rcu_read_lock. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1462 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1463 | * Return: The index of the gap if found, otherwise an index outside the |
| 1464 | * range specified (in which case 'index - return >= max_scan' will be true). |
| 1465 | * In the rare case of wrap-around, ULONG_MAX will be returned. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1466 | */ |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1467 | pgoff_t page_cache_prev_miss(struct address_space *mapping, |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1468 | pgoff_t index, unsigned long max_scan) |
| 1469 | { |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1470 | XA_STATE(xas, &mapping->i_pages, index); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1471 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1472 | while (max_scan--) { |
| 1473 | void *entry = xas_prev(&xas); |
| 1474 | if (!entry || xa_is_value(entry)) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1475 | break; |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1476 | if (xas.xa_index == ULONG_MAX) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1477 | break; |
| 1478 | } |
| 1479 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1480 | return xas.xa_index; |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1481 | } |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1482 | EXPORT_SYMBOL(page_cache_prev_miss); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1483 | |
| 1484 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1485 | * find_get_entry - find and get a page cache entry |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1486 | * @mapping: the address_space to search |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1487 | * @offset: the page cache index |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1488 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1489 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 1490 | * page cache page, it is returned with an increased refcount. |
| 1491 | * |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1492 | * If the slot holds a shadow entry of a previously evicted page, or a |
| 1493 | * swap entry from shmem/tmpfs, it is returned. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1494 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1495 | * Return: the found page or shadow entry, %NULL if nothing is found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | */ |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1497 | struct page *find_get_entry(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | { |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1499 | XA_STATE(xas, &mapping->i_pages, offset); |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1500 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1502 | rcu_read_lock(); |
| 1503 | repeat: |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1504 | xas_reset(&xas); |
| 1505 | page = xas_load(&xas); |
| 1506 | if (xas_retry(&xas, page)) |
| 1507 | goto repeat; |
| 1508 | /* |
| 1509 | * A shadow entry of a recently evicted page, or a swap entry from |
| 1510 | * shmem/tmpfs. Return it without attempting to raise page count. |
| 1511 | */ |
| 1512 | if (!page || xa_is_value(page)) |
| 1513 | goto out; |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1514 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1515 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1516 | goto repeat; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1517 | |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1518 | /* |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1519 | * Has the page moved or been split? |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1520 | * This is part of the lockless pagecache protocol. See |
| 1521 | * include/linux/pagemap.h for details. |
| 1522 | */ |
| 1523 | if (unlikely(page != xas_reload(&xas))) { |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1524 | put_page(page); |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1525 | goto repeat; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1526 | } |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1527 | page = find_subpage(page, offset); |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 1528 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1529 | rcu_read_unlock(); |
| 1530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | return page; |
| 1532 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1533 | EXPORT_SYMBOL(find_get_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1535 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1536 | * find_lock_entry - locate, pin and lock a page cache entry |
| 1537 | * @mapping: the address_space to search |
| 1538 | * @offset: the page cache index |
| 1539 | * |
| 1540 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 1541 | * page cache page, it is returned locked and with an increased |
| 1542 | * refcount. |
| 1543 | * |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1544 | * If the slot holds a shadow entry of a previously evicted page, or a |
| 1545 | * swap entry from shmem/tmpfs, it is returned. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1546 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1547 | * find_lock_entry() may sleep. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1548 | * |
| 1549 | * Return: the found page or shadow entry, %NULL if nothing is found. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1550 | */ |
| 1551 | struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | { |
| 1553 | struct page *page; |
| 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | repeat: |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1556 | page = find_get_entry(mapping, offset); |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1557 | if (page && !xa_is_value(page)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1558 | lock_page(page); |
| 1559 | /* Has the page been truncated? */ |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1560 | if (unlikely(page_mapping(page) != mapping)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1561 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1562 | put_page(page); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1563 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1565 | VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | return page; |
| 1568 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1569 | EXPORT_SYMBOL(find_lock_entry); |
| 1570 | |
| 1571 | /** |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1572 | * pagecache_get_page - find and get a page reference |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1573 | * @mapping: the address_space to search |
| 1574 | * @offset: the page index |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1575 | * @fgp_flags: PCG flags |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 1576 | * @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] | 1577 | * |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1578 | * Looks up the page cache slot at @mapping & @offset. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1579 | * |
Randy Dunlap | 7532518 | 2014-07-30 16:08:37 -0700 | [diff] [blame] | 1580 | * PCG flags modify how the page is returned. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1581 | * |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 1582 | * @fgp_flags can be: |
| 1583 | * |
| 1584 | * - FGP_ACCESSED: the page will be marked accessed |
| 1585 | * - FGP_LOCK: Page is return locked |
| 1586 | * - FGP_CREAT: If page is not present then a new page is allocated using |
| 1587 | * @gfp_mask and added to the page cache and the VM's LRU |
| 1588 | * list. The page is returned locked and with an increased |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1589 | * refcount. |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 1590 | * - FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do |
| 1591 | * its own locking dance if the page is already in cache, or unlock the page |
| 1592 | * before returning if we had to add the page to pagecache. |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1593 | * |
| 1594 | * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even |
| 1595 | * if the GFP flags specified for FGP_CREAT are atomic. |
| 1596 | * |
| 1597 | * If there is a page cache page, it is returned with an increased refcount. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1598 | * |
| 1599 | * Return: the found page or %NULL otherwise. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1600 | */ |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1601 | struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset, |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 1602 | int fgp_flags, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1604 | struct page *page; |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | repeat: |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1607 | page = find_get_entry(mapping, offset); |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 1608 | if (xa_is_value(page)) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1609 | page = NULL; |
| 1610 | if (!page) |
| 1611 | goto no_page; |
| 1612 | |
| 1613 | if (fgp_flags & FGP_LOCK) { |
| 1614 | if (fgp_flags & FGP_NOWAIT) { |
| 1615 | if (!trylock_page(page)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1616 | put_page(page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1617 | return NULL; |
| 1618 | } |
| 1619 | } else { |
| 1620 | lock_page(page); |
| 1621 | } |
| 1622 | |
| 1623 | /* Has the page been truncated? */ |
| 1624 | if (unlikely(page->mapping != mapping)) { |
| 1625 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1626 | put_page(page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1627 | goto repeat; |
| 1628 | } |
| 1629 | VM_BUG_ON_PAGE(page->index != offset, page); |
| 1630 | } |
| 1631 | |
Kirill Tkhai | c16eb00 | 2018-12-28 00:37:35 -0800 | [diff] [blame] | 1632 | if (fgp_flags & FGP_ACCESSED) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1633 | mark_page_accessed(page); |
| 1634 | |
| 1635 | no_page: |
| 1636 | if (!page && (fgp_flags & FGP_CREAT)) { |
| 1637 | int err; |
| 1638 | if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping)) |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 1639 | gfp_mask |= __GFP_WRITE; |
| 1640 | if (fgp_flags & FGP_NOFS) |
| 1641 | gfp_mask &= ~__GFP_FS; |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1642 | |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 1643 | page = __page_cache_alloc(gfp_mask); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1644 | if (!page) |
| 1645 | return NULL; |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1646 | |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 1647 | if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP)))) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1648 | fgp_flags |= FGP_LOCK; |
| 1649 | |
Hugh Dickins | eb39d61 | 2014-08-06 16:06:43 -0700 | [diff] [blame] | 1650 | /* Init accessed so avoid atomic mark_page_accessed later */ |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1651 | if (fgp_flags & FGP_ACCESSED) |
Hugh Dickins | eb39d61 | 2014-08-06 16:06:43 -0700 | [diff] [blame] | 1652 | __SetPageReferenced(page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1653 | |
Matthew Wilcox | abc1be1 | 2018-04-20 14:56:20 -0700 | [diff] [blame] | 1654 | err = add_to_page_cache_lru(page, mapping, offset, gfp_mask); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1655 | if (unlikely(err)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1656 | put_page(page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1657 | page = NULL; |
| 1658 | if (err == -EEXIST) |
| 1659 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 1661 | |
| 1662 | /* |
| 1663 | * add_to_page_cache_lru locks the page, and for mmap we expect |
| 1664 | * an unlocked page. |
| 1665 | */ |
| 1666 | if (page && (fgp_flags & FGP_FOR_MMAP)) |
| 1667 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return page; |
| 1671 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1672 | EXPORT_SYMBOL(pagecache_get_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | |
| 1674 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1675 | * find_get_entries - gang pagecache lookup |
| 1676 | * @mapping: The address_space to search |
| 1677 | * @start: The starting page cache index |
| 1678 | * @nr_entries: The maximum number of entries |
| 1679 | * @entries: Where the resulting entries are placed |
| 1680 | * @indices: The cache indices corresponding to the entries in @entries |
| 1681 | * |
| 1682 | * find_get_entries() will search for and return a group of up to |
| 1683 | * @nr_entries entries in the mapping. The entries are placed at |
| 1684 | * @entries. find_get_entries() takes a reference against any actual |
| 1685 | * pages it returns. |
| 1686 | * |
| 1687 | * The search returns a group of mapping-contiguous page cache entries |
| 1688 | * with ascending indexes. There may be holes in the indices due to |
| 1689 | * not-present pages. |
| 1690 | * |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1691 | * Any shadow entries of evicted pages, or swap entries from |
| 1692 | * shmem/tmpfs, are included in the returned array. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1693 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1694 | * Return: the number of pages and shadow entries which were found. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1695 | */ |
| 1696 | unsigned find_get_entries(struct address_space *mapping, |
| 1697 | pgoff_t start, unsigned int nr_entries, |
| 1698 | struct page **entries, pgoff_t *indices) |
| 1699 | { |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1700 | XA_STATE(xas, &mapping->i_pages, start); |
| 1701 | struct page *page; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1702 | unsigned int ret = 0; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1703 | |
| 1704 | if (!nr_entries) |
| 1705 | return 0; |
| 1706 | |
| 1707 | rcu_read_lock(); |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1708 | xas_for_each(&xas, page, ULONG_MAX) { |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1709 | if (xas_retry(&xas, page)) |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1710 | continue; |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1711 | /* |
| 1712 | * A shadow entry of a recently evicted page, a swap |
| 1713 | * entry from shmem/tmpfs or a DAX entry. Return it |
| 1714 | * without attempting to raise page count. |
| 1715 | */ |
| 1716 | if (xa_is_value(page)) |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1717 | goto export; |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1718 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1719 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1720 | goto retry; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1721 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1722 | /* Has the page moved or been split? */ |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1723 | if (unlikely(page != xas_reload(&xas))) |
| 1724 | goto put_page; |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1725 | page = find_subpage(page, xas.xa_index); |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1726 | |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1727 | export: |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1728 | indices[ret] = xas.xa_index; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1729 | entries[ret] = page; |
| 1730 | if (++ret == nr_entries) |
| 1731 | break; |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1732 | continue; |
| 1733 | put_page: |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1734 | put_page(page); |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1735 | retry: |
| 1736 | xas_reset(&xas); |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1737 | } |
| 1738 | rcu_read_unlock(); |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
| 1742 | /** |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1743 | * find_get_pages_range - gang pagecache lookup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | * @mapping: The address_space to search |
| 1745 | * @start: The starting page index |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1746 | * @end: The final page index (inclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | * @nr_pages: The maximum number of pages |
| 1748 | * @pages: Where the resulting pages are placed |
| 1749 | * |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1750 | * find_get_pages_range() will search for and return a group of up to @nr_pages |
| 1751 | * pages in the mapping starting at index @start and up to index @end |
| 1752 | * (inclusive). The pages are placed at @pages. find_get_pages_range() takes |
| 1753 | * a reference against the returned pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | * |
| 1755 | * The search returns a group of mapping-contiguous pages with ascending |
| 1756 | * indexes. There may be holes in the indices due to not-present pages. |
Jan Kara | d72dc8a | 2017-09-06 16:21:18 -0700 | [diff] [blame] | 1757 | * We also update @start to index the next page for the traversal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1759 | * Return: the number of pages which were found. If this number is |
| 1760 | * smaller than @nr_pages, the end of specified range has been |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1761 | * reached. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | */ |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1763 | unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start, |
| 1764 | pgoff_t end, unsigned int nr_pages, |
| 1765 | struct page **pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | { |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1767 | XA_STATE(xas, &mapping->i_pages, *start); |
| 1768 | struct page *page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1769 | unsigned ret = 0; |
| 1770 | |
| 1771 | if (unlikely(!nr_pages)) |
| 1772 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1774 | rcu_read_lock(); |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1775 | xas_for_each(&xas, page, end) { |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1776 | if (xas_retry(&xas, page)) |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1777 | continue; |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1778 | /* Skip over shadow, swap and DAX entries */ |
| 1779 | if (xa_is_value(page)) |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1780 | continue; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1781 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1782 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1783 | goto retry; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1784 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1785 | /* Has the page moved or been split? */ |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1786 | if (unlikely(page != xas_reload(&xas))) |
| 1787 | goto put_page; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1788 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1789 | pages[ret] = find_subpage(page, xas.xa_index); |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1790 | if (++ret == nr_pages) { |
Yu Zhao | 5d3ee42 | 2019-03-05 15:49:17 -0800 | [diff] [blame] | 1791 | *start = xas.xa_index + 1; |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1792 | goto out; |
| 1793 | } |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1794 | continue; |
| 1795 | put_page: |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1796 | put_page(page); |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1797 | retry: |
| 1798 | xas_reset(&xas); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1799 | } |
Hugh Dickins | 5b280c0 | 2011-03-22 16:33:07 -0700 | [diff] [blame] | 1800 | |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1801 | /* |
| 1802 | * We come here when there is no page beyond @end. We take care to not |
| 1803 | * overflow the index @start as it confuses some of the callers. This |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1804 | * breaks the iteration when there is a page at index -1 but that is |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1805 | * already broken anyway. |
| 1806 | */ |
| 1807 | if (end == (pgoff_t)-1) |
| 1808 | *start = (pgoff_t)-1; |
| 1809 | else |
| 1810 | *start = end + 1; |
| 1811 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1812 | rcu_read_unlock(); |
Jan Kara | d72dc8a | 2017-09-06 16:21:18 -0700 | [diff] [blame] | 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | return ret; |
| 1815 | } |
| 1816 | |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1817 | /** |
| 1818 | * find_get_pages_contig - gang contiguous pagecache lookup |
| 1819 | * @mapping: The address_space to search |
| 1820 | * @index: The starting page index |
| 1821 | * @nr_pages: The maximum number of pages |
| 1822 | * @pages: Where the resulting pages are placed |
| 1823 | * |
| 1824 | * find_get_pages_contig() works exactly like find_get_pages(), except |
| 1825 | * that the returned number of pages are guaranteed to be contiguous. |
| 1826 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1827 | * Return: the number of pages which were found. |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1828 | */ |
| 1829 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, |
| 1830 | unsigned int nr_pages, struct page **pages) |
| 1831 | { |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1832 | XA_STATE(xas, &mapping->i_pages, index); |
| 1833 | struct page *page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1834 | unsigned int ret = 0; |
| 1835 | |
| 1836 | if (unlikely(!nr_pages)) |
| 1837 | return 0; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1838 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1839 | rcu_read_lock(); |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1840 | for (page = xas_load(&xas); page; page = xas_next(&xas)) { |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1841 | if (xas_retry(&xas, page)) |
| 1842 | continue; |
| 1843 | /* |
| 1844 | * If the entry has been swapped out, we can stop looking. |
| 1845 | * No current caller is looking for DAX entries. |
| 1846 | */ |
| 1847 | if (xa_is_value(page)) |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1848 | break; |
Hugh Dickins | 9d8aa4e | 2011-03-22 16:33:06 -0700 | [diff] [blame] | 1849 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1850 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1851 | goto retry; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1852 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1853 | /* Has the page moved or been split? */ |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1854 | if (unlikely(page != xas_reload(&xas))) |
| 1855 | goto put_page; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1856 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1857 | pages[ret] = find_subpage(page, xas.xa_index); |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1858 | if (++ret == nr_pages) |
| 1859 | break; |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1860 | continue; |
| 1861 | put_page: |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1862 | put_page(page); |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1863 | retry: |
| 1864 | xas_reset(&xas); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1865 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1866 | rcu_read_unlock(); |
| 1867 | return ret; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1868 | } |
David Howells | ef71c15 | 2007-05-09 02:33:44 -0700 | [diff] [blame] | 1869 | EXPORT_SYMBOL(find_get_pages_contig); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1870 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1871 | /** |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1872 | * find_get_pages_range_tag - find and return pages in given range matching @tag |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1873 | * @mapping: the address_space to search |
| 1874 | * @index: the starting page index |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1875 | * @end: The final page index (inclusive) |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1876 | * @tag: the tag index |
| 1877 | * @nr_pages: the maximum number of pages |
| 1878 | * @pages: where the resulting pages are placed |
| 1879 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | * 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] | 1881 | * @tag. We update @index to index the next page for the traversal. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1882 | * |
| 1883 | * Return: the number of pages which were found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | */ |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1885 | unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index, |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1886 | pgoff_t end, xa_mark_t tag, unsigned int nr_pages, |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1887 | struct page **pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | { |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1889 | XA_STATE(xas, &mapping->i_pages, *index); |
| 1890 | struct page *page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1891 | unsigned ret = 0; |
| 1892 | |
| 1893 | if (unlikely(!nr_pages)) |
| 1894 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1896 | rcu_read_lock(); |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1897 | xas_for_each_marked(&xas, page, end, tag) { |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1898 | if (xas_retry(&xas, page)) |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1899 | continue; |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1900 | /* |
| 1901 | * Shadow entries should never be tagged, but this iteration |
| 1902 | * is lockless so there is a window for page reclaim to evict |
| 1903 | * a page we saw tagged. Skip over it. |
| 1904 | */ |
| 1905 | if (xa_is_value(page)) |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1906 | continue; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1907 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1908 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1909 | goto retry; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1910 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1911 | /* Has the page moved or been split? */ |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1912 | if (unlikely(page != xas_reload(&xas))) |
| 1913 | goto put_page; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1914 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1915 | pages[ret] = find_subpage(page, xas.xa_index); |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1916 | if (++ret == nr_pages) { |
Yu Zhao | 5d3ee42 | 2019-03-05 15:49:17 -0800 | [diff] [blame] | 1917 | *index = xas.xa_index + 1; |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1918 | goto out; |
| 1919 | } |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1920 | continue; |
| 1921 | put_page: |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 1922 | put_page(page); |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1923 | retry: |
| 1924 | xas_reset(&xas); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1925 | } |
Hugh Dickins | 5b280c0 | 2011-03-22 16:33:07 -0700 | [diff] [blame] | 1926 | |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1927 | /* |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1928 | * We come here when we got to @end. We take care to not overflow the |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1929 | * index @index as it confuses some of the callers. This breaks the |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1930 | * iteration when there is a page at index -1 but that is already |
| 1931 | * broken anyway. |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1932 | */ |
| 1933 | if (end == (pgoff_t)-1) |
| 1934 | *index = (pgoff_t)-1; |
| 1935 | else |
| 1936 | *index = end + 1; |
| 1937 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1938 | rcu_read_unlock(); |
| 1939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | return ret; |
| 1941 | } |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1942 | EXPORT_SYMBOL(find_get_pages_range_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1944 | /* |
| 1945 | * CD/DVDs are error prone. When a medium error occurs, the driver may fail |
| 1946 | * a _large_ part of the i/o request. Imagine the worst scenario: |
| 1947 | * |
| 1948 | * ---R__________________________________________B__________ |
| 1949 | * ^ reading here ^ bad block(assume 4k) |
| 1950 | * |
| 1951 | * read(R) => miss => readahead(R...B) => media error => frustrating retries |
| 1952 | * => failing the whole request => read(R) => read(R+1) => |
| 1953 | * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) => |
| 1954 | * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) => |
| 1955 | * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ...... |
| 1956 | * |
| 1957 | * It is going insane. Fix it by quickly scaling down the readahead size. |
| 1958 | */ |
| 1959 | static void shrink_readahead_size_eio(struct file *filp, |
| 1960 | struct file_ra_state *ra) |
| 1961 | { |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1962 | ra->ra_pages /= 4; |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1965 | /** |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 1966 | * generic_file_buffered_read - generic file read routine |
| 1967 | * @iocb: the iocb to read |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 1968 | * @iter: data destination |
| 1969 | * @written: already copied |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1970 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | * This is a generic file read routine, and uses the |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1972 | * mapping->a_ops->readpage() function for the actual low-level stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | * |
| 1974 | * This is really ugly. But the goto's actually try to clarify some |
| 1975 | * of the logic when it comes to error handling etc. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1976 | * |
| 1977 | * Return: |
| 1978 | * * total number of bytes copied, including those the were already @written |
| 1979 | * * negative error code if nothing was copied |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | */ |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 1981 | static ssize_t generic_file_buffered_read(struct kiocb *iocb, |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 1982 | struct iov_iter *iter, ssize_t written) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | { |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 1984 | struct file *filp = iocb->ki_filp; |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 1985 | struct address_space *mapping = filp->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | struct inode *inode = mapping->host; |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 1987 | struct file_ra_state *ra = &filp->f_ra; |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 1988 | loff_t *ppos = &iocb->ki_pos; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1989 | pgoff_t index; |
| 1990 | pgoff_t last_index; |
| 1991 | pgoff_t prev_index; |
| 1992 | unsigned long offset; /* offset into pagecache page */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 1993 | unsigned int prev_offset; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 1994 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
Wei Fang | c2a9737 | 2016-10-07 17:01:52 -0700 | [diff] [blame] | 1996 | if (unlikely(*ppos >= inode->i_sb->s_maxbytes)) |
Linus Torvalds | d05c5f7 | 2016-12-14 12:45:25 -0800 | [diff] [blame] | 1997 | return 0; |
Wei Fang | c2a9737 | 2016-10-07 17:01:52 -0700 | [diff] [blame] | 1998 | iov_iter_truncate(iter, inode->i_sb->s_maxbytes); |
| 1999 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2000 | index = *ppos >> PAGE_SHIFT; |
| 2001 | prev_index = ra->prev_pos >> PAGE_SHIFT; |
| 2002 | prev_offset = ra->prev_pos & (PAGE_SIZE-1); |
| 2003 | last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT; |
| 2004 | offset = *ppos & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | for (;;) { |
| 2007 | struct page *page; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2008 | pgoff_t end_index; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2009 | loff_t isize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | unsigned long nr, ret; |
| 2011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | find_page: |
Michal Hocko | 5abf186 | 2017-02-03 13:13:29 -0800 | [diff] [blame] | 2014 | if (fatal_signal_pending(current)) { |
| 2015 | error = -EINTR; |
| 2016 | goto out; |
| 2017 | } |
| 2018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | page = find_get_page(mapping, index); |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 2020 | if (!page) { |
Milosz Tanski | 3239d83 | 2017-08-29 16:13:19 +0200 | [diff] [blame] | 2021 | if (iocb->ki_flags & IOCB_NOWAIT) |
| 2022 | goto would_block; |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 2023 | page_cache_sync_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2024 | ra, filp, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 2025 | index, last_index - index); |
| 2026 | page = find_get_page(mapping, index); |
| 2027 | if (unlikely(page == NULL)) |
| 2028 | goto no_cached_page; |
| 2029 | } |
| 2030 | if (PageReadahead(page)) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 2031 | page_cache_async_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2032 | ra, filp, page, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 2033 | index, last_index - index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | } |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2035 | if (!PageUptodate(page)) { |
Milosz Tanski | 3239d83 | 2017-08-29 16:13:19 +0200 | [diff] [blame] | 2036 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 2037 | put_page(page); |
| 2038 | goto would_block; |
| 2039 | } |
| 2040 | |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2041 | /* |
| 2042 | * See comment in do_read_cache_page on why |
| 2043 | * wait_on_page_locked is used to avoid unnecessarily |
| 2044 | * serialisations and why it's safe. |
| 2045 | */ |
Bart Van Assche | c4b209a | 2016-10-07 16:58:33 -0700 | [diff] [blame] | 2046 | error = wait_on_page_locked_killable(page); |
| 2047 | if (unlikely(error)) |
| 2048 | goto readpage_error; |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2049 | if (PageUptodate(page)) |
| 2050 | goto page_ok; |
| 2051 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2052 | if (inode->i_blkbits == PAGE_SHIFT || |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2053 | !mapping->a_ops->is_partially_uptodate) |
| 2054 | goto page_not_up_to_date; |
Eryu Guan | 6d6d36b | 2016-11-01 15:43:07 +0800 | [diff] [blame] | 2055 | /* pipes can't handle partially uptodate pages */ |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 2056 | if (unlikely(iov_iter_is_pipe(iter))) |
Eryu Guan | 6d6d36b | 2016-11-01 15:43:07 +0800 | [diff] [blame] | 2057 | goto page_not_up_to_date; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 2058 | if (!trylock_page(page)) |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2059 | goto page_not_up_to_date; |
Dave Hansen | 8d056cb | 2010-11-11 14:05:15 -0800 | [diff] [blame] | 2060 | /* Did it get truncated before we got the lock? */ |
| 2061 | if (!page->mapping) |
| 2062 | goto page_not_up_to_date_locked; |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2063 | if (!mapping->a_ops->is_partially_uptodate(page, |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2064 | offset, iter->count)) |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2065 | goto page_not_up_to_date_locked; |
| 2066 | unlock_page(page); |
| 2067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | page_ok: |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2069 | /* |
| 2070 | * i_size must be checked after we know the page is Uptodate. |
| 2071 | * |
| 2072 | * Checking i_size after the check allows us to calculate |
| 2073 | * the correct value for "nr", which means the zero-filled |
| 2074 | * part of the page is not copied back to userspace (unless |
| 2075 | * another truncate extends the file - this is desired though). |
| 2076 | */ |
| 2077 | |
| 2078 | isize = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2079 | end_index = (isize - 1) >> PAGE_SHIFT; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2080 | if (unlikely(!isize || index > end_index)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2081 | put_page(page); |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2082 | goto out; |
| 2083 | } |
| 2084 | |
| 2085 | /* 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] | 2086 | nr = PAGE_SIZE; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2087 | if (index == end_index) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2088 | nr = ((isize - 1) & ~PAGE_MASK) + 1; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2089 | if (nr <= offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2090 | put_page(page); |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2091 | goto out; |
| 2092 | } |
| 2093 | } |
| 2094 | nr = nr - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | |
| 2096 | /* If users can be writing to this page using arbitrary |
| 2097 | * virtual addresses, take care about potential aliasing |
| 2098 | * before reading the page on the kernel side. |
| 2099 | */ |
| 2100 | if (mapping_writably_mapped(mapping)) |
| 2101 | flush_dcache_page(page); |
| 2102 | |
| 2103 | /* |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 2104 | * When a sequential read accesses a page several times, |
| 2105 | * only mark it as accessed the first time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 2107 | if (prev_index != index || offset != prev_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | mark_page_accessed(page); |
| 2109 | prev_index = index; |
| 2110 | |
| 2111 | /* |
| 2112 | * Ok, we have the page, and it's up-to-date, so |
| 2113 | * now we can copy it to user space... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | */ |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2115 | |
| 2116 | ret = copy_page_to_iter(page, offset, nr, iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | offset += ret; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2118 | index += offset >> PAGE_SHIFT; |
| 2119 | offset &= ~PAGE_MASK; |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 2120 | prev_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2122 | put_page(page); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2123 | written += ret; |
| 2124 | if (!iov_iter_count(iter)) |
| 2125 | goto out; |
| 2126 | if (ret < nr) { |
| 2127 | error = -EFAULT; |
| 2128 | goto out; |
| 2129 | } |
| 2130 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | |
| 2132 | page_not_up_to_date: |
| 2133 | /* Get exclusive access to the page ... */ |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 2134 | error = lock_page_killable(page); |
| 2135 | if (unlikely(error)) |
| 2136 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2138 | page_not_up_to_date_locked: |
Nick Piggin | da6052f | 2006-09-25 23:31:35 -0700 | [diff] [blame] | 2139 | /* Did it get truncated before we got the lock? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | if (!page->mapping) { |
| 2141 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2142 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | continue; |
| 2144 | } |
| 2145 | |
| 2146 | /* Did somebody else fill it already? */ |
| 2147 | if (PageUptodate(page)) { |
| 2148 | unlock_page(page); |
| 2149 | goto page_ok; |
| 2150 | } |
| 2151 | |
| 2152 | readpage: |
Jeff Moyer | 91803b4 | 2010-05-26 11:49:40 -0400 | [diff] [blame] | 2153 | /* |
| 2154 | * A previous I/O error may have been due to temporary |
| 2155 | * failures, eg. multipath errors. |
| 2156 | * PG_error will be set again if readpage fails. |
| 2157 | */ |
| 2158 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | /* Start the actual read. The read will unlock the page. */ |
| 2160 | error = mapping->a_ops->readpage(filp, page); |
| 2161 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2162 | if (unlikely(error)) { |
| 2163 | if (error == AOP_TRUNCATED_PAGE) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2164 | put_page(page); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2165 | error = 0; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2166 | goto find_page; |
| 2167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | goto readpage_error; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | |
| 2171 | if (!PageUptodate(page)) { |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 2172 | error = lock_page_killable(page); |
| 2173 | if (unlikely(error)) |
| 2174 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | if (!PageUptodate(page)) { |
| 2176 | if (page->mapping == NULL) { |
| 2177 | /* |
Christoph Hellwig | 2ecdc82 | 2010-01-26 17:27:20 +0100 | [diff] [blame] | 2178 | * invalidate_mapping_pages got it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | */ |
| 2180 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2181 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | goto find_page; |
| 2183 | } |
| 2184 | unlock_page(page); |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2185 | shrink_readahead_size_eio(filp, ra); |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 2186 | error = -EIO; |
| 2187 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } |
| 2189 | unlock_page(page); |
| 2190 | } |
| 2191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | goto page_ok; |
| 2193 | |
| 2194 | readpage_error: |
| 2195 | /* UHHUH! A synchronous read error occurred. Report it */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2196 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | goto out; |
| 2198 | |
| 2199 | no_cached_page: |
| 2200 | /* |
| 2201 | * Ok, it wasn't cached, so we need to create a new |
| 2202 | * page.. |
| 2203 | */ |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2204 | page = page_cache_alloc(mapping); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2205 | if (!page) { |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2206 | error = -ENOMEM; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2207 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | } |
Michal Hocko | 6afdb85 | 2015-06-24 16:58:06 -0700 | [diff] [blame] | 2209 | error = add_to_page_cache_lru(page, mapping, index, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 2210 | mapping_gfp_constraint(mapping, GFP_KERNEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | if (error) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2212 | put_page(page); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2213 | if (error == -EEXIST) { |
| 2214 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | goto find_page; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | goto out; |
| 2218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | goto readpage; |
| 2220 | } |
| 2221 | |
Milosz Tanski | 3239d83 | 2017-08-29 16:13:19 +0200 | [diff] [blame] | 2222 | would_block: |
| 2223 | error = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | out: |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2225 | ra->prev_pos = prev_index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2226 | ra->prev_pos <<= PAGE_SHIFT; |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2227 | ra->prev_pos |= prev_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2229 | *ppos = ((loff_t)index << PAGE_SHIFT) + offset; |
Krishna Kumar | 0c6aa26 | 2008-10-15 22:01:13 -0700 | [diff] [blame] | 2230 | file_accessed(filp); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2231 | return written ? written : error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2234 | /** |
Al Viro | 6abd232 | 2014-04-04 14:20:57 -0400 | [diff] [blame] | 2235 | * generic_file_read_iter - generic filesystem read routine |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2236 | * @iocb: kernel I/O control block |
Al Viro | 6abd232 | 2014-04-04 14:20:57 -0400 | [diff] [blame] | 2237 | * @iter: destination for the data read |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2238 | * |
Al Viro | 6abd232 | 2014-04-04 14:20:57 -0400 | [diff] [blame] | 2239 | * This is the "read_iter()" routine for all filesystems |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | * that can use the page cache directly. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2241 | * Return: |
| 2242 | * * number of bytes copied, even for partial reads |
| 2243 | * * negative error code if nothing was read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | */ |
| 2245 | ssize_t |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2246 | generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | { |
Nicolai Stange | e7080a4 | 2016-03-25 14:22:14 -0700 | [diff] [blame] | 2248 | size_t count = iov_iter_count(iter); |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2249 | ssize_t retval = 0; |
Nicolai Stange | e7080a4 | 2016-03-25 14:22:14 -0700 | [diff] [blame] | 2250 | |
| 2251 | if (!count) |
| 2252 | goto out; /* skip atime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 2254 | if (iocb->ki_flags & IOCB_DIRECT) { |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2255 | struct file *file = iocb->ki_filp; |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2256 | struct address_space *mapping = file->f_mapping; |
| 2257 | struct inode *inode = mapping->host; |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 2258 | loff_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | size = i_size_read(inode); |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 2261 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 2262 | if (filemap_range_has_page(mapping, iocb->ki_pos, |
| 2263 | iocb->ki_pos + count - 1)) |
| 2264 | return -EAGAIN; |
| 2265 | } else { |
| 2266 | retval = filemap_write_and_wait_range(mapping, |
| 2267 | iocb->ki_pos, |
| 2268 | iocb->ki_pos + count - 1); |
| 2269 | if (retval < 0) |
| 2270 | goto out; |
| 2271 | } |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2272 | |
Christoph Hellwig | 0d5b0cf | 2016-10-03 09:48:08 +1100 | [diff] [blame] | 2273 | file_accessed(file); |
| 2274 | |
Al Viro | 5ecda13 | 2017-04-13 14:13:36 -0400 | [diff] [blame] | 2275 | retval = mapping->a_ops->direct_IO(iocb, iter); |
Al Viro | c3a6902 | 2016-10-10 13:26:27 -0400 | [diff] [blame] | 2276 | if (retval >= 0) { |
Christoph Hellwig | c64fb5c | 2016-04-07 08:51:55 -0700 | [diff] [blame] | 2277 | iocb->ki_pos += retval; |
Al Viro | 5ecda13 | 2017-04-13 14:13:36 -0400 | [diff] [blame] | 2278 | count -= retval; |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2279 | } |
Al Viro | 5b47d59 | 2017-05-08 13:54:47 -0400 | [diff] [blame] | 2280 | iov_iter_revert(iter, count - iov_iter_count(iter)); |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 2281 | |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2282 | /* |
| 2283 | * Btrfs can have a short DIO read if we encounter |
| 2284 | * compressed extents, so if there was an error, or if |
| 2285 | * we've already read everything we wanted to, or if |
| 2286 | * there was a short read because we hit EOF, go ahead |
| 2287 | * and return. Otherwise fallthrough to buffered io for |
Matthew Wilcox | fbbbad4 | 2015-02-16 15:58:53 -0800 | [diff] [blame] | 2288 | * the rest of the read. Buffered reads will not work for |
| 2289 | * DAX files, so don't bother trying. |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2290 | */ |
Al Viro | 5ecda13 | 2017-04-13 14:13:36 -0400 | [diff] [blame] | 2291 | if (retval < 0 || !count || iocb->ki_pos >= size || |
Christoph Hellwig | 0d5b0cf | 2016-10-03 09:48:08 +1100 | [diff] [blame] | 2292 | IS_DAX(inode)) |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2293 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2296 | retval = generic_file_buffered_read(iocb, iter, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | out: |
| 2298 | return retval; |
| 2299 | } |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2300 | EXPORT_SYMBOL(generic_file_read_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | #define MMAP_LOTSAMISS (100) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2304 | static struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, |
| 2305 | struct file *fpin) |
| 2306 | { |
| 2307 | int flags = vmf->flags; |
| 2308 | |
| 2309 | if (fpin) |
| 2310 | return fpin; |
| 2311 | |
| 2312 | /* |
| 2313 | * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or |
| 2314 | * anything, so we only pin the file and drop the mmap_sem if only |
| 2315 | * FAULT_FLAG_ALLOW_RETRY is set. |
| 2316 | */ |
| 2317 | if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) == |
| 2318 | FAULT_FLAG_ALLOW_RETRY) { |
| 2319 | fpin = get_file(vmf->vma->vm_file); |
| 2320 | up_read(&vmf->vma->vm_mm->mmap_sem); |
| 2321 | } |
| 2322 | return fpin; |
| 2323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2325 | /* |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2326 | * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem |
| 2327 | * @vmf - the vm_fault for this fault. |
| 2328 | * @page - the page to lock. |
| 2329 | * @fpin - the pointer to the file we may pin (or is already pinned). |
| 2330 | * |
| 2331 | * This works similar to lock_page_or_retry in that it can drop the mmap_sem. |
| 2332 | * It differs in that it actually returns the page locked if it returns 1 and 0 |
| 2333 | * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin |
| 2334 | * will point to the pinned file and needs to be fput()'ed at a later point. |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2335 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2336 | static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page, |
| 2337 | struct file **fpin) |
| 2338 | { |
| 2339 | if (trylock_page(page)) |
| 2340 | return 1; |
| 2341 | |
Linus Torvalds | 8b0f9fa | 2019-03-15 11:26:07 -0700 | [diff] [blame] | 2342 | /* |
| 2343 | * NOTE! This will make us return with VM_FAULT_RETRY, but with |
| 2344 | * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT |
| 2345 | * is supposed to work. We have way too many special cases.. |
| 2346 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2347 | if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) |
| 2348 | return 0; |
| 2349 | |
| 2350 | *fpin = maybe_unlock_mmap_for_io(vmf, *fpin); |
| 2351 | if (vmf->flags & FAULT_FLAG_KILLABLE) { |
| 2352 | if (__lock_page_killable(page)) { |
| 2353 | /* |
| 2354 | * We didn't have the right flags to drop the mmap_sem, |
| 2355 | * but all fault_handlers only check for fatal signals |
| 2356 | * if we return VM_FAULT_RETRY, so we need to drop the |
| 2357 | * mmap_sem here and return 0 if we don't have a fpin. |
| 2358 | */ |
| 2359 | if (*fpin == NULL) |
| 2360 | up_read(&vmf->vma->vm_mm->mmap_sem); |
| 2361 | return 0; |
| 2362 | } |
| 2363 | } else |
| 2364 | __lock_page(page); |
| 2365 | return 1; |
| 2366 | } |
| 2367 | |
| 2368 | |
| 2369 | /* |
| 2370 | * Synchronous readahead happens when we don't even find a page in the page |
| 2371 | * cache at all. We don't want to perform IO under the mmap sem, so if we have |
| 2372 | * to drop the mmap sem we return the file that was pinned in order for us to do |
| 2373 | * that. If we didn't pin a file then we return NULL. The file that is |
| 2374 | * returned needs to be fput()'ed when we're done with it. |
| 2375 | */ |
| 2376 | static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2377 | { |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2378 | struct file *file = vmf->vma->vm_file; |
| 2379 | struct file_ra_state *ra = &file->f_ra; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2380 | struct address_space *mapping = file->f_mapping; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2381 | struct file *fpin = NULL; |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2382 | pgoff_t offset = vmf->pgoff; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2383 | |
| 2384 | /* If we don't want any read-ahead, don't bother */ |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2385 | if (vmf->vma->vm_flags & VM_RAND_READ) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2386 | return fpin; |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 2387 | if (!ra->ra_pages) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2388 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2389 | |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2390 | if (vmf->vma->vm_flags & VM_SEQ_READ) { |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2391 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Wu Fengguang | 7ffc59b | 2009-06-16 15:31:38 -0700 | [diff] [blame] | 2392 | page_cache_sync_readahead(mapping, ra, file, offset, |
| 2393 | ra->ra_pages); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2394 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
Andi Kleen | 207d04b | 2011-05-24 17:12:29 -0700 | [diff] [blame] | 2397 | /* Avoid banging the cache line if not needed */ |
| 2398 | if (ra->mmap_miss < MMAP_LOTSAMISS * 10) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2399 | ra->mmap_miss++; |
| 2400 | |
| 2401 | /* |
| 2402 | * Do we miss much more than hit in this file? If so, |
| 2403 | * stop bothering with read-ahead. It will only hurt. |
| 2404 | */ |
| 2405 | if (ra->mmap_miss > MMAP_LOTSAMISS) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2406 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2407 | |
Wu Fengguang | d30a110 | 2009-06-16 15:31:30 -0700 | [diff] [blame] | 2408 | /* |
| 2409 | * mmap read-around |
| 2410 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2411 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Roman Gushchin | 600e19a | 2015-11-05 18:47:08 -0800 | [diff] [blame] | 2412 | ra->start = max_t(long, 0, offset - ra->ra_pages / 2); |
| 2413 | ra->size = ra->ra_pages; |
| 2414 | ra->async_size = ra->ra_pages / 4; |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 2415 | ra_submit(ra, mapping, file); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2416 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* |
| 2420 | * Asynchronous readahead happens when we find the page and PG_readahead, |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2421 | * so we want to possibly extend the readahead further. We return the file that |
| 2422 | * was pinned if we have to drop the mmap_sem in order to do IO. |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2423 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2424 | static struct file *do_async_mmap_readahead(struct vm_fault *vmf, |
| 2425 | struct page *page) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2426 | { |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2427 | struct file *file = vmf->vma->vm_file; |
| 2428 | struct file_ra_state *ra = &file->f_ra; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2429 | struct address_space *mapping = file->f_mapping; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2430 | struct file *fpin = NULL; |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2431 | pgoff_t offset = vmf->pgoff; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2432 | |
| 2433 | /* If we don't want any read-ahead, don't bother */ |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2434 | if (vmf->vma->vm_flags & VM_RAND_READ) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2435 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2436 | if (ra->mmap_miss > 0) |
| 2437 | ra->mmap_miss--; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2438 | if (PageReadahead(page)) { |
| 2439 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Wu Fengguang | 2fad6f5 | 2009-06-16 15:31:29 -0700 | [diff] [blame] | 2440 | page_cache_async_readahead(mapping, ra, file, |
| 2441 | page, offset, ra->ra_pages); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2442 | } |
| 2443 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2446 | /** |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2447 | * filemap_fault - read in file data for page fault handling |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2448 | * @vmf: struct vm_fault containing details of the fault |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2449 | * |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2450 | * filemap_fault() is invoked via the vma operations vector for a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | * mapped memory region to read in file data during a page fault. |
| 2452 | * |
| 2453 | * The goto's are kind of ugly, but this streamlines the normal case of having |
| 2454 | * it in the page cache, and handles the special cases reasonably without |
| 2455 | * having a lot of duplicated code. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 2456 | * |
| 2457 | * vma->vm_mm->mmap_sem must be held on entry. |
| 2458 | * |
| 2459 | * If our return value has VM_FAULT_RETRY set, it's because |
| 2460 | * lock_page_or_retry() returned 0. |
| 2461 | * The mmap_sem has usually been released in this case. |
| 2462 | * See __lock_page_or_retry() for the exception. |
| 2463 | * |
| 2464 | * If our return value does not have VM_FAULT_RETRY set, the mmap_sem |
| 2465 | * has not been released. |
| 2466 | * |
| 2467 | * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2468 | * |
| 2469 | * Return: bitwise-OR of %VM_FAULT_ codes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | */ |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2471 | vm_fault_t filemap_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | { |
| 2473 | int error; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2474 | struct file *file = vmf->vma->vm_file; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2475 | struct file *fpin = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | struct address_space *mapping = file->f_mapping; |
| 2477 | struct file_ra_state *ra = &file->f_ra; |
| 2478 | struct inode *inode = mapping->host; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2479 | pgoff_t offset = vmf->pgoff; |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2480 | pgoff_t max_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | struct page *page; |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2482 | vm_fault_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2484 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
| 2485 | if (unlikely(offset >= max_off)) |
Linus Torvalds | 5307cc1 | 2007-10-31 09:19:46 -0700 | [diff] [blame] | 2486 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | /* |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2489 | * Do we have something in the page cache already? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2491 | page = find_get_page(mapping, offset); |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 2492 | if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | /* |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2494 | * We found the page, so try async readahead before |
| 2495 | * waiting for the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2497 | fpin = do_async_mmap_readahead(vmf, page); |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 2498 | } else if (!page) { |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2499 | /* No page in the page cache at all */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2500 | count_vm_event(PGMAJFAULT); |
Roman Gushchin | 2262185 | 2017-07-06 15:40:25 -0700 | [diff] [blame] | 2501 | count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2502 | ret = VM_FAULT_MAJOR; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2503 | fpin = do_sync_mmap_readahead(vmf); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2504 | retry_find: |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 2505 | page = pagecache_get_page(mapping, offset, |
| 2506 | FGP_CREAT|FGP_FOR_MMAP, |
| 2507 | vmf->gfp_mask); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2508 | if (!page) { |
| 2509 | if (fpin) |
| 2510 | goto out_retry; |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 2511 | return vmf_error(-ENOMEM); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2515 | if (!lock_page_maybe_drop_mmap(vmf, page, &fpin)) |
| 2516 | goto out_retry; |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 2517 | |
| 2518 | /* Did it get truncated? */ |
| 2519 | if (unlikely(page->mapping != mapping)) { |
| 2520 | unlock_page(page); |
| 2521 | put_page(page); |
| 2522 | goto retry_find; |
| 2523 | } |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2524 | VM_BUG_ON_PAGE(page->index != offset, page); |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 2525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | /* |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2527 | * We have a locked page in the page cache, now we need to check |
| 2528 | * 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] | 2529 | */ |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2530 | if (unlikely(!PageUptodate(page))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | goto page_not_uptodate; |
| 2532 | |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2533 | /* |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2534 | * We've made it this far and we had to drop our mmap_sem, now is the |
| 2535 | * time to return to the upper layer and have it re-find the vma and |
| 2536 | * redo the fault. |
| 2537 | */ |
| 2538 | if (fpin) { |
| 2539 | unlock_page(page); |
| 2540 | goto out_retry; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2544 | * Found the page and have a reference on it. |
| 2545 | * We must recheck i_size under page lock. |
| 2546 | */ |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2547 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
| 2548 | if (unlikely(offset >= max_off)) { |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2549 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2550 | put_page(page); |
Linus Torvalds | 5307cc1 | 2007-10-31 09:19:46 -0700 | [diff] [blame] | 2551 | return VM_FAULT_SIGBUS; |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2554 | vmf->page = page; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 2555 | return ret | VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | page_not_uptodate: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | /* |
| 2559 | * Umm, take care of errors if the page isn't up-to-date. |
| 2560 | * Try to re-read it _once_. We do this synchronously, |
| 2561 | * because there really aren't any performance issues here |
| 2562 | * and we need to check for errors. |
| 2563 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | ClearPageError(page); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2565 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2566 | error = mapping->a_ops->readpage(file, page); |
Miklos Szeredi | 3ef0f72 | 2008-05-14 16:05:37 -0700 | [diff] [blame] | 2567 | if (!error) { |
| 2568 | wait_on_page_locked(page); |
| 2569 | if (!PageUptodate(page)) |
| 2570 | error = -EIO; |
| 2571 | } |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2572 | if (fpin) |
| 2573 | goto out_retry; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2574 | put_page(page); |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2575 | |
| 2576 | if (!error || error == AOP_TRUNCATED_PAGE) |
| 2577 | goto retry_find; |
| 2578 | |
| 2579 | /* Things didn't work out. Return zero to tell the mm layer so. */ |
| 2580 | shrink_readahead_size_eio(file, ra); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2581 | return VM_FAULT_SIGBUS; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2582 | |
| 2583 | out_retry: |
| 2584 | /* |
| 2585 | * We dropped the mmap_sem, we need to return to the fault handler to |
| 2586 | * re-find the vma and come back and find our hopefully still populated |
| 2587 | * page. |
| 2588 | */ |
| 2589 | if (page) |
| 2590 | put_page(page); |
| 2591 | if (fpin) |
| 2592 | fput(fpin); |
| 2593 | return ret | VM_FAULT_RETRY; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2594 | } |
| 2595 | EXPORT_SYMBOL(filemap_fault); |
| 2596 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2597 | void filemap_map_pages(struct vm_fault *vmf, |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 2598 | pgoff_t start_pgoff, pgoff_t end_pgoff) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2599 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2600 | struct file *file = vmf->vma->vm_file; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2601 | struct address_space *mapping = file->f_mapping; |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 2602 | pgoff_t last_pgoff = start_pgoff; |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2603 | unsigned long max_idx; |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2604 | XA_STATE(xas, &mapping->i_pages, start_pgoff); |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 2605 | struct page *page; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2606 | |
| 2607 | rcu_read_lock(); |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2608 | xas_for_each(&xas, page, end_pgoff) { |
| 2609 | if (xas_retry(&xas, page)) |
| 2610 | continue; |
| 2611 | if (xa_is_value(page)) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2612 | goto next; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2613 | |
Michal Hocko | e0975b2 | 2018-12-28 00:38:36 -0800 | [diff] [blame] | 2614 | /* |
| 2615 | * Check for a locked page first, as a speculative |
| 2616 | * reference may adversely influence page migration. |
| 2617 | */ |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 2618 | if (PageLocked(page)) |
Michal Hocko | e0975b2 | 2018-12-28 00:38:36 -0800 | [diff] [blame] | 2619 | goto next; |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 2620 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2621 | goto next; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2622 | |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 2623 | /* Has the page moved or been split? */ |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2624 | if (unlikely(page != xas_reload(&xas))) |
| 2625 | goto skip; |
Matthew Wilcox | 5fd4ca2 | 2019-05-13 17:16:44 -0700 | [diff] [blame] | 2626 | page = find_subpage(page, xas.xa_index); |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2627 | |
| 2628 | if (!PageUptodate(page) || |
| 2629 | PageReadahead(page) || |
| 2630 | PageHWPoison(page)) |
| 2631 | goto skip; |
| 2632 | if (!trylock_page(page)) |
| 2633 | goto skip; |
| 2634 | |
| 2635 | if (page->mapping != mapping || !PageUptodate(page)) |
| 2636 | goto unlock; |
| 2637 | |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2638 | max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); |
| 2639 | if (page->index >= max_idx) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2640 | goto unlock; |
| 2641 | |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2642 | if (file->f_ra.mmap_miss > 0) |
| 2643 | file->f_ra.mmap_miss--; |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2644 | |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2645 | vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT; |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2646 | if (vmf->pte) |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2647 | vmf->pte += xas.xa_index - last_pgoff; |
| 2648 | last_pgoff = xas.xa_index; |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2649 | if (alloc_set_pte(vmf, NULL, page)) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2650 | goto unlock; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2651 | unlock_page(page); |
| 2652 | goto next; |
| 2653 | unlock: |
| 2654 | unlock_page(page); |
| 2655 | skip: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2656 | put_page(page); |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2657 | next: |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2658 | /* Huge page is mapped? No need to proceed. */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2659 | if (pmd_trans_huge(*vmf->pmd)) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2660 | break; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2661 | } |
| 2662 | rcu_read_unlock(); |
| 2663 | } |
| 2664 | EXPORT_SYMBOL(filemap_map_pages); |
| 2665 | |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2666 | vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf) |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2667 | { |
| 2668 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2669 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2670 | vm_fault_t ret = VM_FAULT_LOCKED; |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2671 | |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2672 | sb_start_pagefault(inode->i_sb); |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2673 | file_update_time(vmf->vma->vm_file); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2674 | lock_page(page); |
| 2675 | if (page->mapping != inode->i_mapping) { |
| 2676 | unlock_page(page); |
| 2677 | ret = VM_FAULT_NOPAGE; |
| 2678 | goto out; |
| 2679 | } |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2680 | /* |
| 2681 | * We mark the page dirty already here so that when freeze is in |
| 2682 | * progress, we are guaranteed that writeback during freezing will |
| 2683 | * see the dirty page and writeprotect it again. |
| 2684 | */ |
| 2685 | set_page_dirty(page); |
Darrick J. Wong | 1d1d1a7 | 2013-02-21 16:42:51 -0800 | [diff] [blame] | 2686 | wait_for_stable_page(page); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2687 | out: |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2688 | sb_end_pagefault(inode->i_sb); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2689 | return ret; |
| 2690 | } |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2691 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2692 | const struct vm_operations_struct generic_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2693 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2694 | .map_pages = filemap_map_pages, |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2695 | .page_mkwrite = filemap_page_mkwrite, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | }; |
| 2697 | |
| 2698 | /* This is used for a general mmap of a disk file */ |
| 2699 | |
| 2700 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 2701 | { |
| 2702 | struct address_space *mapping = file->f_mapping; |
| 2703 | |
| 2704 | if (!mapping->a_ops->readpage) |
| 2705 | return -ENOEXEC; |
| 2706 | file_accessed(file); |
| 2707 | vma->vm_ops = &generic_file_vm_ops; |
| 2708 | return 0; |
| 2709 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | |
| 2711 | /* |
| 2712 | * This is for filesystems which do not implement ->writepage. |
| 2713 | */ |
| 2714 | int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) |
| 2715 | { |
| 2716 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 2717 | return -EINVAL; |
| 2718 | return generic_file_mmap(file, vma); |
| 2719 | } |
| 2720 | #else |
Souptick Joarder | 4b96a37 | 2018-10-26 15:04:03 -0700 | [diff] [blame] | 2721 | vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf) |
Arnd Bergmann | 45397228 | 2018-04-13 15:35:27 -0700 | [diff] [blame] | 2722 | { |
Souptick Joarder | 4b96a37 | 2018-10-26 15:04:03 -0700 | [diff] [blame] | 2723 | return VM_FAULT_SIGBUS; |
Arnd Bergmann | 45397228 | 2018-04-13 15:35:27 -0700 | [diff] [blame] | 2724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 2726 | { |
| 2727 | return -ENOSYS; |
| 2728 | } |
| 2729 | int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma) |
| 2730 | { |
| 2731 | return -ENOSYS; |
| 2732 | } |
| 2733 | #endif /* CONFIG_MMU */ |
| 2734 | |
Arnd Bergmann | 45397228 | 2018-04-13 15:35:27 -0700 | [diff] [blame] | 2735 | EXPORT_SYMBOL(filemap_page_mkwrite); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | EXPORT_SYMBOL(generic_file_mmap); |
| 2737 | EXPORT_SYMBOL(generic_file_readonly_mmap); |
| 2738 | |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2739 | static struct page *wait_on_page_read(struct page *page) |
| 2740 | { |
| 2741 | if (!IS_ERR(page)) { |
| 2742 | wait_on_page_locked(page); |
| 2743 | if (!PageUptodate(page)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2744 | put_page(page); |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2745 | page = ERR_PTR(-EIO); |
| 2746 | } |
| 2747 | } |
| 2748 | return page; |
| 2749 | } |
| 2750 | |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2751 | static struct page *do_read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2752 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2753 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2754 | void *data, |
| 2755 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2757 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | int err; |
| 2759 | repeat: |
| 2760 | page = find_get_page(mapping, index); |
| 2761 | if (!page) { |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2762 | page = __page_cache_alloc(gfp); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2763 | if (!page) |
| 2764 | return ERR_PTR(-ENOMEM); |
Dave Kleikamp | e6f67b8 | 2011-12-21 11:05:48 -0600 | [diff] [blame] | 2765 | err = add_to_page_cache_lru(page, mapping, index, gfp); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2766 | if (unlikely(err)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2767 | put_page(page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2768 | if (err == -EEXIST) |
| 2769 | goto repeat; |
Matthew Wilcox | 22ecdb4 | 2017-12-04 04:02:00 -0500 | [diff] [blame] | 2770 | /* Presumably ENOMEM for xarray node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | return ERR_PTR(err); |
| 2772 | } |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2773 | |
| 2774 | filler: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | err = filler(data, page); |
| 2776 | if (err < 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2777 | put_page(page); |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2778 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | } |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2780 | |
| 2781 | page = wait_on_page_read(page); |
| 2782 | if (IS_ERR(page)) |
| 2783 | return page; |
| 2784 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | if (PageUptodate(page)) |
| 2787 | goto out; |
| 2788 | |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * Page is not up to date and may be locked due one of the following |
| 2791 | * case a: Page is being filled and the page lock is held |
| 2792 | * case b: Read/write error clearing the page uptodate status |
| 2793 | * case c: Truncation in progress (page locked) |
| 2794 | * case d: Reclaim in progress |
| 2795 | * |
| 2796 | * Case a, the page will be up to date when the page is unlocked. |
| 2797 | * There is no need to serialise on the page lock here as the page |
| 2798 | * is pinned so the lock gives no additional protection. Even if the |
| 2799 | * the page is truncated, the data is still valid if PageUptodate as |
| 2800 | * it's a race vs truncate race. |
| 2801 | * Case b, the page will not be up to date |
| 2802 | * Case c, the page may be truncated but in itself, the data may still |
| 2803 | * be valid after IO completes as it's a read vs truncate race. The |
| 2804 | * operation must restart if the page is not uptodate on unlock but |
| 2805 | * otherwise serialising on page lock to stabilise the mapping gives |
| 2806 | * no additional guarantees to the caller as the page lock is |
| 2807 | * released before return. |
| 2808 | * Case d, similar to truncation. If reclaim holds the page lock, it |
| 2809 | * will be a race with remove_mapping that determines if the mapping |
| 2810 | * is valid on unlock but otherwise the data is valid and there is |
| 2811 | * no need to serialise with page lock. |
| 2812 | * |
| 2813 | * As the page lock gives no additional guarantee, we optimistically |
| 2814 | * wait on the page to be unlocked and check if it's up to date and |
| 2815 | * use the page if it is. Otherwise, the page lock is required to |
| 2816 | * distinguish between the different cases. The motivation is that we |
| 2817 | * avoid spurious serialisations and wakeups when multiple processes |
| 2818 | * wait on the same page for IO to complete. |
| 2819 | */ |
| 2820 | wait_on_page_locked(page); |
| 2821 | if (PageUptodate(page)) |
| 2822 | goto out; |
| 2823 | |
| 2824 | /* Distinguish between all the cases under the safety of the lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | lock_page(page); |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2826 | |
| 2827 | /* Case c or d, restart the operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | if (!page->mapping) { |
| 2829 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2830 | put_page(page); |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2831 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | } |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2833 | |
| 2834 | /* Someone else locked and filled the page in a very small window */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | if (PageUptodate(page)) { |
| 2836 | unlock_page(page); |
| 2837 | goto out; |
| 2838 | } |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2839 | goto filler; |
| 2840 | |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 2841 | out: |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2842 | mark_page_accessed(page); |
| 2843 | return page; |
| 2844 | } |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2845 | |
| 2846 | /** |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2847 | * read_cache_page - read into page cache, fill it if needed |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2848 | * @mapping: the page's address_space |
| 2849 | * @index: the page index |
| 2850 | * @filler: function to perform the read |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2851 | * @data: first arg to filler(data, page) function, often left as NULL |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2852 | * |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2853 | * 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] | 2854 | * 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] | 2855 | * |
| 2856 | * If the page does not get brought uptodate, return -EIO. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2857 | * |
| 2858 | * Return: up to date page on success, ERR_PTR() on failure. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2859 | */ |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2860 | struct page *read_cache_page(struct address_space *mapping, |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2861 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2862 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2863 | void *data) |
| 2864 | { |
| 2865 | return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping)); |
| 2866 | } |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2867 | EXPORT_SYMBOL(read_cache_page); |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2868 | |
| 2869 | /** |
| 2870 | * read_cache_page_gfp - read into page cache, using specified page allocation flags. |
| 2871 | * @mapping: the page's address_space |
| 2872 | * @index: the page index |
| 2873 | * @gfp: the page allocator flags to use if allocating |
| 2874 | * |
| 2875 | * 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] | 2876 | * any new page allocations done using the specified allocation flags. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2877 | * |
| 2878 | * If the page does not get brought uptodate, return -EIO. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2879 | * |
| 2880 | * Return: up to date page on success, ERR_PTR() on failure. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2881 | */ |
| 2882 | struct page *read_cache_page_gfp(struct address_space *mapping, |
| 2883 | pgoff_t index, |
| 2884 | gfp_t gfp) |
| 2885 | { |
| 2886 | filler_t *filler = (filler_t *)mapping->a_ops->readpage; |
| 2887 | |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2888 | return do_read_cache_page(mapping, index, filler, NULL, gfp); |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2889 | } |
| 2890 | EXPORT_SYMBOL(read_cache_page_gfp); |
| 2891 | |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2892 | /* |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2893 | * Don't operate on ranges the page cache doesn't support, and don't exceed the |
| 2894 | * LFS limits. If pos is under the limit it becomes a short access. If it |
| 2895 | * exceeds the limit we return -EFBIG. |
| 2896 | */ |
| 2897 | static int generic_access_check_limits(struct file *file, loff_t pos, |
| 2898 | loff_t *count) |
| 2899 | { |
| 2900 | struct inode *inode = file->f_mapping->host; |
| 2901 | loff_t max_size = inode->i_sb->s_maxbytes; |
| 2902 | |
| 2903 | if (!(file->f_flags & O_LARGEFILE)) |
| 2904 | max_size = MAX_NON_LFS; |
| 2905 | |
| 2906 | if (unlikely(pos >= max_size)) |
| 2907 | return -EFBIG; |
| 2908 | *count = min(*count, max_size - pos); |
| 2909 | return 0; |
| 2910 | } |
| 2911 | |
| 2912 | static int generic_write_check_limits(struct file *file, loff_t pos, |
| 2913 | loff_t *count) |
| 2914 | { |
| 2915 | loff_t limit = rlimit(RLIMIT_FSIZE); |
| 2916 | |
| 2917 | if (limit != RLIM_INFINITY) { |
| 2918 | if (pos >= limit) { |
| 2919 | send_sig(SIGXFSZ, current, 0); |
| 2920 | return -EFBIG; |
| 2921 | } |
| 2922 | *count = min(*count, limit - pos); |
| 2923 | } |
| 2924 | |
| 2925 | return generic_access_check_limits(file, pos, count); |
| 2926 | } |
| 2927 | |
| 2928 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | * Performs necessary checks before doing a write |
| 2930 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2931 | * Can adjust writing position or amount of bytes to write. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | * Returns appropriate error code that caller should return or |
| 2933 | * zero in case that write should be allowed. |
| 2934 | */ |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2935 | 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] | 2936 | { |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2937 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | struct inode *inode = file->f_mapping->host; |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2939 | loff_t count; |
| 2940 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2942 | if (!iov_iter_count(from)) |
| 2943 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | |
Al Viro | 0fa6b00 | 2015-04-04 04:05:48 -0400 | [diff] [blame] | 2945 | /* FIXME: this is for backwards compatibility with 2.4 */ |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 2946 | if (iocb->ki_flags & IOCB_APPEND) |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2947 | iocb->ki_pos = i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 2949 | if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) |
| 2950 | return -EINVAL; |
| 2951 | |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2952 | count = iov_iter_count(from); |
| 2953 | ret = generic_write_check_limits(file, iocb->ki_pos, &count); |
| 2954 | if (ret) |
| 2955 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2957 | iov_iter_truncate(from, count); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2958 | return iov_iter_count(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | } |
| 2960 | EXPORT_SYMBOL(generic_write_checks); |
| 2961 | |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 2962 | /* |
| 2963 | * Performs necessary checks before doing a clone. |
| 2964 | * |
| 2965 | * Can adjust amount of bytes to clone. |
| 2966 | * Returns appropriate error code that caller should return or |
| 2967 | * zero in case the clone should be allowed. |
| 2968 | */ |
| 2969 | int generic_remap_checks(struct file *file_in, loff_t pos_in, |
| 2970 | struct file *file_out, loff_t pos_out, |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 2971 | loff_t *req_count, unsigned int remap_flags) |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 2972 | { |
| 2973 | struct inode *inode_in = file_in->f_mapping->host; |
| 2974 | struct inode *inode_out = file_out->f_mapping->host; |
| 2975 | uint64_t count = *req_count; |
| 2976 | uint64_t bcount; |
| 2977 | loff_t size_in, size_out; |
| 2978 | loff_t bs = inode_out->i_sb->s_blocksize; |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2979 | int ret; |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 2980 | |
| 2981 | /* The start of both ranges must be aligned to an fs block. */ |
| 2982 | if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs)) |
| 2983 | return -EINVAL; |
| 2984 | |
| 2985 | /* Ensure offsets don't wrap. */ |
| 2986 | if (pos_in + count < pos_in || pos_out + count < pos_out) |
| 2987 | return -EINVAL; |
| 2988 | |
| 2989 | size_in = i_size_read(inode_in); |
| 2990 | size_out = i_size_read(inode_out); |
| 2991 | |
| 2992 | /* Dedupe requires both ranges to be within EOF. */ |
Darrick J. Wong | 3d28193 | 2018-10-30 10:41:34 +1100 | [diff] [blame] | 2993 | if ((remap_flags & REMAP_FILE_DEDUP) && |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 2994 | (pos_in >= size_in || pos_in + count > size_in || |
| 2995 | pos_out >= size_out || pos_out + count > size_out)) |
| 2996 | return -EINVAL; |
| 2997 | |
| 2998 | /* Ensure the infile range is within the infile. */ |
| 2999 | if (pos_in >= size_in) |
| 3000 | return -EINVAL; |
| 3001 | count = min(count, size_in - (uint64_t)pos_in); |
| 3002 | |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 3003 | ret = generic_access_check_limits(file_in, pos_in, &count); |
| 3004 | if (ret) |
| 3005 | return ret; |
| 3006 | |
| 3007 | ret = generic_write_check_limits(file_out, pos_out, &count); |
| 3008 | if (ret) |
| 3009 | return ret; |
| 3010 | |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3011 | /* |
| 3012 | * If the user wanted us to link to the infile's EOF, round up to the |
| 3013 | * next block boundary for this check. |
| 3014 | * |
| 3015 | * Otherwise, make sure the count is also block-aligned, having |
| 3016 | * already confirmed the starting offsets' block alignment. |
| 3017 | */ |
| 3018 | if (pos_in + count == size_in) { |
| 3019 | bcount = ALIGN(size_in, bs) - pos_in; |
| 3020 | } else { |
| 3021 | if (!IS_ALIGNED(count, bs)) |
Darrick J. Wong | eca3654 | 2018-10-30 10:42:10 +1100 | [diff] [blame] | 3022 | count = ALIGN_DOWN(count, bs); |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3023 | bcount = count; |
| 3024 | } |
| 3025 | |
| 3026 | /* Don't allow overlapped cloning within the same file. */ |
| 3027 | if (inode_in == inode_out && |
| 3028 | pos_out + bcount > pos_in && |
| 3029 | pos_out < pos_in + bcount) |
| 3030 | return -EINVAL; |
| 3031 | |
Darrick J. Wong | eca3654 | 2018-10-30 10:42:10 +1100 | [diff] [blame] | 3032 | /* |
| 3033 | * We shortened the request but the caller can't deal with that, so |
| 3034 | * bounce the request back to userspace. |
| 3035 | */ |
| 3036 | if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN)) |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3037 | return -EINVAL; |
| 3038 | |
Darrick J. Wong | eca3654 | 2018-10-30 10:42:10 +1100 | [diff] [blame] | 3039 | *req_count = count; |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3040 | return 0; |
| 3041 | } |
| 3042 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3043 | int pagecache_write_begin(struct file *file, struct address_space *mapping, |
| 3044 | loff_t pos, unsigned len, unsigned flags, |
| 3045 | struct page **pagep, void **fsdata) |
| 3046 | { |
| 3047 | const struct address_space_operations *aops = mapping->a_ops; |
| 3048 | |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 3049 | return aops->write_begin(file, mapping, pos, len, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3050 | pagep, fsdata); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3051 | } |
| 3052 | EXPORT_SYMBOL(pagecache_write_begin); |
| 3053 | |
| 3054 | int pagecache_write_end(struct file *file, struct address_space *mapping, |
| 3055 | loff_t pos, unsigned len, unsigned copied, |
| 3056 | struct page *page, void *fsdata) |
| 3057 | { |
| 3058 | const struct address_space_operations *aops = mapping->a_ops; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3059 | |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 3060 | return aops->write_end(file, mapping, pos, len, copied, page, fsdata); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3061 | } |
| 3062 | EXPORT_SYMBOL(pagecache_write_end); |
| 3063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | ssize_t |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 3065 | generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | { |
| 3067 | struct file *file = iocb->ki_filp; |
| 3068 | struct address_space *mapping = file->f_mapping; |
| 3069 | struct inode *inode = mapping->host; |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 3070 | loff_t pos = iocb->ki_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | ssize_t written; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3072 | size_t write_len; |
| 3073 | pgoff_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | |
Al Viro | 0c94933 | 2014-03-22 06:51:37 -0400 | [diff] [blame] | 3075 | write_len = iov_iter_count(from); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3076 | end = (pos + write_len - 1) >> PAGE_SHIFT; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3077 | |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 3078 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 3079 | /* If there are pages to writeback, return */ |
| 3080 | if (filemap_range_has_page(inode->i_mapping, pos, |
zhengbin | 35f12f0 | 2019-03-05 15:44:21 -0800 | [diff] [blame] | 3081 | pos + write_len - 1)) |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 3082 | return -EAGAIN; |
| 3083 | } else { |
| 3084 | written = filemap_write_and_wait_range(mapping, pos, |
| 3085 | pos + write_len - 1); |
| 3086 | if (written) |
| 3087 | goto out; |
| 3088 | } |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3089 | |
| 3090 | /* |
| 3091 | * After a write we want buffered reads to be sure to go to disk to get |
| 3092 | * the new data. We invalidate clean cached page from the region we're |
| 3093 | * 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] | 3094 | * without clobbering -EIOCBQUEUED from ->direct_IO(). |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3095 | */ |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 3096 | written = invalidate_inode_pages2_range(mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3097 | pos >> PAGE_SHIFT, end); |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 3098 | /* |
| 3099 | * If a page can not be invalidated, return 0 to fall back |
| 3100 | * to buffered write. |
| 3101 | */ |
| 3102 | if (written) { |
| 3103 | if (written == -EBUSY) |
| 3104 | return 0; |
| 3105 | goto out; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
Al Viro | 639a93a5 | 2017-04-13 14:10:15 -0400 | [diff] [blame] | 3108 | written = mapping->a_ops->direct_IO(iocb, from); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3109 | |
| 3110 | /* |
| 3111 | * Finally, try again to invalidate clean pages which might have been |
| 3112 | * cached by non-direct readahead, or faulted in by get_user_pages() |
| 3113 | * if the source of the write was an mmap'ed region of the file |
| 3114 | * we're writing. Either one is a pretty crazy thing to do, |
| 3115 | * so we don't support it 100%. If this invalidation |
| 3116 | * fails, tough, the write still worked... |
Lukas Czerner | 332391a | 2017-09-21 08:16:29 -0600 | [diff] [blame] | 3117 | * |
| 3118 | * Most of the time we do not need this since dio_complete() will do |
| 3119 | * the invalidation for us. However there are some file systems that |
| 3120 | * do not end up with dio_complete() being called, so let's not break |
| 3121 | * them by removing it completely |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3122 | */ |
Lukas Czerner | 332391a | 2017-09-21 08:16:29 -0600 | [diff] [blame] | 3123 | if (mapping->nrpages) |
| 3124 | invalidate_inode_pages2_range(mapping, |
| 3125 | pos >> PAGE_SHIFT, end); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | if (written > 0) { |
Namhyung Kim | 0116651 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 3128 | pos += written; |
Al Viro | 639a93a5 | 2017-04-13 14:10:15 -0400 | [diff] [blame] | 3129 | write_len -= written; |
Namhyung Kim | 0116651 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 3130 | if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { |
| 3131 | i_size_write(inode, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | mark_inode_dirty(inode); |
| 3133 | } |
Al Viro | 5cb6c6c | 2014-02-11 20:58:20 -0500 | [diff] [blame] | 3134 | iocb->ki_pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | } |
Al Viro | 639a93a5 | 2017-04-13 14:10:15 -0400 | [diff] [blame] | 3136 | iov_iter_revert(from, write_len - iov_iter_count(from)); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3137 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | return written; |
| 3139 | } |
| 3140 | EXPORT_SYMBOL(generic_file_direct_write); |
| 3141 | |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3142 | /* |
| 3143 | * Find or create a page at the given pagecache position. Return the locked |
| 3144 | * page. This function is specifically for buffered writes. |
| 3145 | */ |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3146 | struct page *grab_cache_page_write_begin(struct address_space *mapping, |
| 3147 | pgoff_t index, unsigned flags) |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3148 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3149 | struct page *page; |
Johannes Weiner | bbddabe | 2016-05-20 16:56:28 -0700 | [diff] [blame] | 3150 | int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT; |
Johannes Weiner | 0faa70c | 2012-01-10 15:07:53 -0800 | [diff] [blame] | 3151 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3152 | if (flags & AOP_FLAG_NOFS) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3153 | fgp_flags |= FGP_NOFS; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3154 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3155 | page = pagecache_get_page(mapping, index, fgp_flags, |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 3156 | mapping_gfp_mask(mapping)); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3157 | if (page) |
| 3158 | wait_for_stable_page(page); |
| 3159 | |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3160 | return page; |
| 3161 | } |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3162 | EXPORT_SYMBOL(grab_cache_page_write_begin); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3163 | |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3164 | ssize_t generic_perform_write(struct file *file, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3165 | struct iov_iter *i, loff_t pos) |
| 3166 | { |
| 3167 | struct address_space *mapping = file->f_mapping; |
| 3168 | const struct address_space_operations *a_ops = mapping->a_ops; |
| 3169 | long status = 0; |
| 3170 | ssize_t written = 0; |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 3171 | unsigned int flags = 0; |
| 3172 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3173 | do { |
| 3174 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3175 | unsigned long offset; /* Offset into pagecache page */ |
| 3176 | unsigned long bytes; /* Bytes to write to page */ |
| 3177 | size_t copied; /* Bytes copied from user */ |
| 3178 | void *fsdata; |
| 3179 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3180 | offset = (pos & (PAGE_SIZE - 1)); |
| 3181 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3182 | iov_iter_count(i)); |
| 3183 | |
| 3184 | again: |
Linus Torvalds | 00a3d66 | 2015-10-07 08:32:38 +0100 | [diff] [blame] | 3185 | /* |
| 3186 | * Bring in the user page that we will copy from _first_. |
| 3187 | * Otherwise there's a nasty deadlock on copying from the |
| 3188 | * same page as we're writing to, without it being marked |
| 3189 | * up-to-date. |
| 3190 | * |
| 3191 | * Not only is this an optimisation, but it is also required |
| 3192 | * to check that the address is actually valid, when atomic |
| 3193 | * usercopies are used, below. |
| 3194 | */ |
| 3195 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
| 3196 | status = -EFAULT; |
| 3197 | break; |
| 3198 | } |
| 3199 | |
Jan Kara | 296291c | 2015-10-22 13:32:21 -0700 | [diff] [blame] | 3200 | if (fatal_signal_pending(current)) { |
| 3201 | status = -EINTR; |
| 3202 | break; |
| 3203 | } |
| 3204 | |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 3205 | status = a_ops->write_begin(file, mapping, pos, bytes, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3206 | &page, &fsdata); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3207 | if (unlikely(status < 0)) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3208 | break; |
| 3209 | |
anfei zhou | 931e80e | 2010-02-02 13:44:02 -0800 | [diff] [blame] | 3210 | if (mapping_writably_mapped(mapping)) |
| 3211 | flush_dcache_page(page); |
Linus Torvalds | 00a3d66 | 2015-10-07 08:32:38 +0100 | [diff] [blame] | 3212 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3213 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3214 | flush_dcache_page(page); |
| 3215 | |
| 3216 | status = a_ops->write_end(file, mapping, pos, bytes, copied, |
| 3217 | page, fsdata); |
| 3218 | if (unlikely(status < 0)) |
| 3219 | break; |
| 3220 | copied = status; |
| 3221 | |
| 3222 | cond_resched(); |
| 3223 | |
Nick Piggin | 124d3b7 | 2008-02-02 15:01:17 +0100 | [diff] [blame] | 3224 | iov_iter_advance(i, copied); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3225 | if (unlikely(copied == 0)) { |
| 3226 | /* |
| 3227 | * If we were unable to copy any data at all, we must |
| 3228 | * fall back to a single segment length write. |
| 3229 | * |
| 3230 | * If we didn't fallback here, we could livelock |
| 3231 | * because not all segments in the iov can be copied at |
| 3232 | * once without a pagefault. |
| 3233 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3234 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3235 | iov_iter_single_seg_count(i)); |
| 3236 | goto again; |
| 3237 | } |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3238 | pos += copied; |
| 3239 | written += copied; |
| 3240 | |
| 3241 | balance_dirty_pages_ratelimited(mapping); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3242 | } while (iov_iter_count(i)); |
| 3243 | |
| 3244 | return written ? written : status; |
| 3245 | } |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3246 | EXPORT_SYMBOL(generic_perform_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3248 | /** |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3249 | * __generic_file_write_iter - write data to a file |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3250 | * @iocb: IO state structure (file, offset, etc.) |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3251 | * @from: iov_iter with data to write |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3252 | * |
| 3253 | * This function does all the work needed for actually writing data to a |
| 3254 | * file. It does all basic checks, removes SUID from the file, updates |
| 3255 | * modification times and calls proper subroutines depending on whether we |
| 3256 | * do direct IO or a standard buffered write. |
| 3257 | * |
| 3258 | * It expects i_mutex to be grabbed unless we work on a block device or similar |
| 3259 | * object which does not need locking at all. |
| 3260 | * |
| 3261 | * This function does *not* take care of syncing data in case of O_SYNC write. |
| 3262 | * A caller has to handle it. This is mainly due to the fact that we want to |
| 3263 | * avoid syncing under i_mutex. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3264 | * |
| 3265 | * Return: |
| 3266 | * * number of bytes written, even for truncated writes |
| 3267 | * * negative error code if no data has been written at all |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3268 | */ |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3269 | 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] | 3270 | { |
| 3271 | struct file *file = iocb->ki_filp; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3272 | struct address_space * mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | struct inode *inode = mapping->host; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3274 | ssize_t written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | ssize_t err; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3276 | ssize_t status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | /* We can write back this queue in page reclaim */ |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 3279 | current->backing_dev_info = inode_to_bdi(inode); |
Jan Kara | 5fa8e0a | 2015-05-21 16:05:53 +0200 | [diff] [blame] | 3280 | err = file_remove_privs(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | if (err) |
| 3282 | goto out; |
| 3283 | |
Josef Bacik | c3b2da3 | 2012-03-26 09:59:21 -0400 | [diff] [blame] | 3284 | err = file_update_time(file); |
| 3285 | if (err) |
| 3286 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 3288 | if (iocb->ki_flags & IOCB_DIRECT) { |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3289 | loff_t pos, endbyte; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3290 | |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 3291 | written = generic_file_direct_write(iocb, from); |
Matthew Wilcox | fbbbad4 | 2015-02-16 15:58:53 -0800 | [diff] [blame] | 3292 | /* |
| 3293 | * If the write stopped short of completing, fall back to |
| 3294 | * buffered writes. Some filesystems do this for writes to |
| 3295 | * holes, for example. For DAX files, a buffered write will |
| 3296 | * not succeed (even if it did, DAX does not handle dirty |
| 3297 | * page-cache pages correctly). |
| 3298 | */ |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3299 | if (written < 0 || !iov_iter_count(from) || IS_DAX(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | goto out; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3301 | |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3302 | status = generic_perform_write(file, from, pos = iocb->ki_pos); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3303 | /* |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3304 | * If generic_perform_write() returned a synchronous error |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3305 | * then we want to return the number of bytes which were |
| 3306 | * direct-written, or the error code if that was zero. Note |
| 3307 | * that this differs from normal direct-io semantics, which |
| 3308 | * will return -EFOO even if some bytes were written. |
| 3309 | */ |
Al Viro | 60bb452 | 2014-08-08 12:39:16 -0400 | [diff] [blame] | 3310 | if (unlikely(status < 0)) { |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3311 | err = status; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3312 | goto out; |
| 3313 | } |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3314 | /* |
| 3315 | * We need to ensure that the page cache pages are written to |
| 3316 | * disk and invalidated to preserve the expected O_DIRECT |
| 3317 | * semantics. |
| 3318 | */ |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3319 | endbyte = pos + status - 1; |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3320 | err = filemap_write_and_wait_range(mapping, pos, endbyte); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3321 | if (err == 0) { |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3322 | iocb->ki_pos = endbyte + 1; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3323 | written += status; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3324 | invalidate_mapping_pages(mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3325 | pos >> PAGE_SHIFT, |
| 3326 | endbyte >> PAGE_SHIFT); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3327 | } else { |
| 3328 | /* |
| 3329 | * We don't know how much we wrote, so just return |
| 3330 | * the number of bytes which were direct-written |
| 3331 | */ |
| 3332 | } |
| 3333 | } else { |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3334 | written = generic_perform_write(file, from, iocb->ki_pos); |
| 3335 | if (likely(written > 0)) |
| 3336 | iocb->ki_pos += written; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | out: |
| 3339 | current->backing_dev_info = NULL; |
| 3340 | return written ? written : err; |
| 3341 | } |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3342 | EXPORT_SYMBOL(__generic_file_write_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3344 | /** |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3345 | * generic_file_write_iter - write data to a file |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3346 | * @iocb: IO state structure |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3347 | * @from: iov_iter with data to write |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3348 | * |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3349 | * 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] | 3350 | * filesystems. It takes care of syncing the file in case of O_SYNC file |
| 3351 | * and acquires i_mutex as needed. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3352 | * Return: |
| 3353 | * * negative error code if no data has been written at all of |
| 3354 | * vfs_fsync_range() failed for a synchronous write |
| 3355 | * * number of bytes written, even for truncated writes |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3356 | */ |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3357 | 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] | 3358 | { |
| 3359 | struct file *file = iocb->ki_filp; |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 3360 | struct inode *inode = file->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3363 | inode_lock(inode); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 3364 | ret = generic_write_checks(iocb, from); |
| 3365 | if (ret > 0) |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 3366 | ret = __generic_file_write_iter(iocb, from); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3367 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | |
Christoph Hellwig | e259221 | 2016-04-07 08:52:01 -0700 | [diff] [blame] | 3369 | if (ret > 0) |
| 3370 | ret = generic_write_sync(iocb, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | return ret; |
| 3372 | } |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3373 | EXPORT_SYMBOL(generic_file_write_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3375 | /** |
| 3376 | * try_to_release_page() - release old fs-specific metadata on a page |
| 3377 | * |
| 3378 | * @page: the page which the kernel is trying to free |
| 3379 | * @gfp_mask: memory allocation flags (and I/O mode) |
| 3380 | * |
| 3381 | * The address_space is to try to release any data against the page |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3382 | * (presumably at page->private). |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3383 | * |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 3384 | * This may also be called if PG_fscache is set on a page, indicating that the |
| 3385 | * page is known to the local caching routines. |
| 3386 | * |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3387 | * 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] | 3388 | * 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] | 3389 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3390 | * Return: %1 if the release was successful, otherwise return zero. |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3391 | */ |
| 3392 | int try_to_release_page(struct page *page, gfp_t gfp_mask) |
| 3393 | { |
| 3394 | struct address_space * const mapping = page->mapping; |
| 3395 | |
| 3396 | BUG_ON(!PageLocked(page)); |
| 3397 | if (PageWriteback(page)) |
| 3398 | return 0; |
| 3399 | |
| 3400 | if (mapping && mapping->a_ops->releasepage) |
| 3401 | return mapping->a_ops->releasepage(page, gfp_mask); |
| 3402 | return try_to_free_buffers(page); |
| 3403 | } |
| 3404 | |
| 3405 | EXPORT_SYMBOL(try_to_release_page); |