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