blob: 361181838dc56bb61026f5c7e4eb14882eeea115 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080014#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070016#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/mman.h>
23#include <linux/pagemap.h>
24#include <linux/file.h>
25#include <linux/uio.h>
26#include <linux/hash.h>
27#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070028#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pagevec.h>
30#include <linux/blkdev.h>
31#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080032#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070033#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
Johannes Weiner00501b52014-08-08 14:19:20 -070034#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060036#include <linux/cleancache.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070037#include <linux/rmap.h>
Johannes Weinerd6687a22018-10-26 15:06:08 -070038#include <linux/delayacct.h>
Johannes Weiner3df0e592018-10-26 15:06:27 -070039#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080040#include "internal.h"
41
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070042#define CREATE_TRACE_POINTS
43#include <trace/events/filemap.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * FIXME: remove all knowledge of the buffer layer from the core VM
47 */
Jan Kara148f9482009-08-17 19:52:36 +020048#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/mman.h>
51
52/*
53 * Shared mappings implemented 30.11.1994. It's not fully working yet,
54 * though.
55 *
56 * Shared mappings now work. 15.8.1995 Bruno.
57 *
58 * finished 'unifying' the page and buffer cache and SMP-threaded the
59 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
60 *
61 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
62 */
63
64/*
65 * Lock ordering:
66 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080067 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070069 * ->swap_lock (exclusive_swap_page, others)
70 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080072 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080073 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080076 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070077 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
79 *
80 * ->mmap_sem
81 * ->lock_page (access_process_vm)
82 *
Al Viroccad2362014-02-11 22:36:48 -050083 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070084 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060086 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110087 * sb_lock (fs/fs-writeback.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * ->mapping->tree_lock (__sync_single_inode)
89 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080090 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * ->anon_vma.lock (vma_adjust)
92 *
93 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070094 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070096 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070097 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * ->private_lock (try_to_unmap_one)
99 * ->tree_lock (try_to_unmap_one)
Mel Gormana52633d2016-07-28 15:45:28 -0700100 * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
101 * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * ->private_lock (page_remove_rmap->set_page_dirty)
103 * ->tree_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600104 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100105 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700106 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600107 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100108 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
110 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800111 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700112 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700115static int page_cache_tree_insert(struct address_space *mapping,
116 struct page *page, void **shadowp)
117{
118 struct radix_tree_node *node;
119 void **slot;
120 int error;
121
122 error = __radix_tree_create(&mapping->page_tree, page->index, 0,
123 &node, &slot);
124 if (error)
125 return error;
126 if (*slot) {
127 void *p;
128
129 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
130 if (!radix_tree_exceptional_entry(p))
131 return -EEXIST;
132
133 mapping->nrexceptional--;
134 if (!dax_mapping(mapping)) {
135 if (shadowp)
136 *shadowp = p;
137 if (node)
138 workingset_node_shadows_dec(node);
139 } else {
140 /* DAX can replace empty locked entry with a hole */
141 WARN_ON_ONCE(p !=
142 (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
143 RADIX_DAX_ENTRY_LOCK));
144 /* DAX accounts exceptional entries as normal pages */
145 if (node)
146 workingset_node_pages_dec(node);
147 /* Wakeup waiters for exceptional entry lock */
148 dax_wake_mapping_entry_waiter(mapping, page->index,
Ross Zwisler87fa6f32017-01-10 16:57:15 -0800149 true);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700150 }
151 }
152 radix_tree_replace_slot(slot, page);
153 mapping->nrpages++;
154 if (node) {
155 workingset_node_pages_inc(node);
156 /*
157 * Don't track node that contains actual pages.
158 *
159 * Avoid acquiring the list_lru lock if already
160 * untracked. The list_empty() test is safe as
161 * node->private_list is protected by
162 * mapping->tree_lock.
163 */
164 if (!list_empty(&node->private_list))
165 list_lru_del(&workingset_shadow_nodes,
166 &node->private_list);
167 }
168 return 0;
169}
170
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700171static void page_cache_tree_delete(struct address_space *mapping,
172 struct page *page, void *shadow)
173{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700174 int i, nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700175
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700176 VM_BUG_ON_PAGE(!PageLocked(page), page);
177 VM_BUG_ON_PAGE(PageTail(page), page);
178 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700179
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700180 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200181 struct radix_tree_node *node;
182 void **slot;
183
184 __radix_tree_lookup(&mapping->page_tree, page->index + i,
185 &node, &slot);
186
187 radix_tree_clear_tags(&mapping->page_tree, node, slot);
188
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700189 if (!node) {
190 VM_BUG_ON_PAGE(nr != 1, page);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200191 /*
192 * We need a node to properly account shadow
193 * entries. Don't plant any without. XXX
194 */
195 shadow = NULL;
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700196 }
Johannes Weiner449dd692014-04-03 14:47:56 -0700197
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200198 radix_tree_replace_slot(slot, shadow);
199
200 if (!node)
201 break;
202
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700203 workingset_node_pages_dec(node);
204 if (shadow)
205 workingset_node_shadows_inc(node);
206 else
207 if (__radix_tree_delete_node(&mapping->page_tree, node))
208 continue;
209
210 /*
211 * Track node that only contains shadow entries. DAX mappings
212 * contain no shadow entries and may contain other exceptional
213 * entries so skip those.
214 *
215 * Avoid acquiring the list_lru lock if already tracked.
216 * The list_empty() test is safe as node->private_list is
217 * protected by mapping->tree_lock.
218 */
219 if (!dax_mapping(mapping) && !workingset_node_pages(node) &&
220 list_empty(&node->private_list)) {
221 node->private_data = mapping;
222 list_lru_add(&workingset_shadow_nodes,
223 &node->private_list);
224 }
Johannes Weiner449dd692014-04-03 14:47:56 -0700225 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200226
227 if (shadow) {
228 mapping->nrexceptional += nr;
229 /*
230 * Make sure the nrexceptional update is committed before
231 * the nrpages update so that final truncate racing
232 * with reclaim does not see both counters 0 at the
233 * same time and miss a shadow entry.
234 */
235 smp_wmb();
236 }
237 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
Minchan Kime64a7822011-03-22 16:32:44 -0700241 * Delete a page from the page cache and free it. Caller has to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * sure the page is locked and that nobody else uses it - or that usage
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700243 * is safe. The caller must hold the mapping's tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Johannes Weiner62cccb82016-03-15 14:57:22 -0700245void __delete_from_page_cache(struct page *page, void *shadow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct address_space *mapping = page->mapping;
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700248 int nr = hpage_nr_pages(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -0700250 trace_mm_filemap_delete_from_page_cache(page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600251 /*
252 * if we're uptodate, flush out into the cleancache, otherwise
253 * invalidate any existing cleancache entries. We can't leave
254 * stale data around in the cleancache once our page is gone
255 */
256 if (PageUptodate(page) && PageMappedToDisk(page))
257 cleancache_put_page(page);
258 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400259 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600260
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700261 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800262 VM_BUG_ON_PAGE(page_mapped(page), page);
263 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
264 int mapcount;
265
266 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
267 current->comm, page_to_pfn(page));
268 dump_page(page, "still mapped when deleted");
269 dump_stack();
270 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
271
272 mapcount = page_mapcount(page);
273 if (mapping_exiting(mapping) &&
274 page_count(page) >= mapcount + 2) {
275 /*
276 * All vmas have already been torn down, so it's
277 * a good bet that actually the page is unmapped,
278 * and we'd prefer not to leak it: if we're wrong,
279 * some other bad page check should catch it later.
280 */
281 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700282 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800283 }
284 }
285
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700286 page_cache_tree_delete(mapping, page, shadow);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700289 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700290
Michal Hocko4165b9b2015-06-24 16:57:24 -0700291 /* hugetlb pages do not participate in page cache accounting. */
292 if (!PageHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700293 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700294 if (PageSwapBacked(page)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700295 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700296 if (PageTransHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700297 __dec_node_page_state(page, NR_SHMEM_THPS);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700298 } else {
299 VM_BUG_ON_PAGE(PageTransHuge(page) && !PageHuge(page), page);
300 }
Linus Torvalds3a692792007-12-19 14:05:13 -0800301
302 /*
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700303 * At this point page must be either written or cleaned by truncate.
304 * Dirty page here signals a bug and loss of unwritten data.
Linus Torvalds3a692792007-12-19 14:05:13 -0800305 *
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700306 * This fixes dirty accounting after removing the page entirely but
307 * leaves PageDirty set: it has no effect for truncated page and
308 * anyway will be cleared before returning page into buddy allocator.
Linus Torvalds3a692792007-12-19 14:05:13 -0800309 */
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700310 if (WARN_ON_ONCE(PageDirty(page)))
Johannes Weiner62cccb82016-03-15 14:57:22 -0700311 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Minchan Kim702cfbf2011-03-22 16:32:43 -0700314/**
315 * delete_from_page_cache - delete page from page cache
316 * @page: the page which the kernel is trying to remove from page cache
317 *
318 * This must be called only on pages that have been verified to be in the page
319 * cache and locked. It will never put the page into the free list, the caller
320 * has a reference on the page.
321 */
322void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700324 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400325 unsigned long flags;
Linus Torvalds6072d132010-12-01 13:35:19 -0500326 void (*freepage)(struct page *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Matt Mackallcd7619d2005-05-01 08:59:01 -0700328 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds6072d132010-12-01 13:35:19 -0500330 freepage = mapping->a_ops->freepage;
Greg Thelenc4843a72015-05-22 17:13:16 -0400331
Greg Thelenc4843a72015-05-22 17:13:16 -0400332 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700333 __delete_from_page_cache(page, NULL);
Greg Thelenc4843a72015-05-22 17:13:16 -0400334 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500335
336 if (freepage)
337 freepage(page);
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700338
339 if (PageTransHuge(page) && !PageHuge(page)) {
340 page_ref_sub(page, HPAGE_PMD_NR);
341 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
342 } else {
343 put_page(page);
344 }
Minchan Kim97cecb52011-03-22 16:30:53 -0700345}
346EXPORT_SYMBOL(delete_from_page_cache);
347
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200348int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700349{
350 int ret = 0;
351 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700352 if (test_bit(AS_ENOSPC, &mapping->flags) &&
353 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700354 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700355 if (test_bit(AS_EIO, &mapping->flags) &&
356 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700357 ret = -EIO;
358 return ret;
359}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200360EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700363 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700364 * @mapping: address space structure to write
365 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800366 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700367 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700369 * Start writeback against all of a mapping's dirty pages that lie
370 * within the byte offsets <start, end> inclusive.
371 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700373 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * these two operations is that if a dirty page/buffer is encountered, it must
375 * be waited upon, and not just skipped over.
376 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800377int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
378 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 int ret;
381 struct writeback_control wbc = {
382 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800383 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700384 .range_start = start,
385 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 };
387
Konstantin Khlebnikov063620b2019-09-23 15:34:45 -0700388 if (!mapping_cap_writeback_dirty(mapping) ||
389 !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391
Tejun Heob16b1de2015-06-02 08:39:48 -0600392 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600394 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return ret;
396}
397
398static inline int __filemap_fdatawrite(struct address_space *mapping,
399 int sync_mode)
400{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700401 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404int filemap_fdatawrite(struct address_space *mapping)
405{
406 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
407}
408EXPORT_SYMBOL(filemap_fdatawrite);
409
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400410int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800411 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
414}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400415EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Randy Dunlap485bb992006-06-23 02:03:49 -0700417/**
418 * filemap_flush - mostly a non-blocking flush
419 * @mapping: target address_space
420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * This is a mostly non-blocking flush. Not suitable for data-integrity
422 * purposes - I/O may not be started against all dirty pages.
423 */
424int filemap_flush(struct address_space *mapping)
425{
426 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
427}
428EXPORT_SYMBOL(filemap_flush);
429
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800430static int __filemap_fdatawait_range(struct address_space *mapping,
431 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300433 pgoff_t index = start_byte >> PAGE_SHIFT;
434 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct pagevec pvec;
436 int nr_pages;
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800437 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200439 if (end_byte < start_byte)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 pagevec_init(&pvec, 0);
Jan Kara7e95dd52017-11-15 17:35:05 -0800443 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned i;
445
Jan Kara7e95dd52017-11-15 17:35:05 -0800446 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara4d93a432017-11-15 17:35:19 -0800447 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara7e95dd52017-11-15 17:35:05 -0800448 if (!nr_pages)
449 break;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 for (i = 0; i < nr_pages; i++) {
452 struct page *page = pvec.pages[i];
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800455 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ret = -EIO;
457 }
458 pagevec_release(&pvec);
459 cond_resched();
460 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700461out:
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800462 return ret;
463}
464
465/**
466 * filemap_fdatawait_range - wait for writeback to complete
467 * @mapping: address space structure to wait for
468 * @start_byte: offset in bytes where the range starts
469 * @end_byte: offset in bytes where the range ends (inclusive)
470 *
471 * Walk the list of under-writeback pages of the given address space
472 * in the given range and wait for all of them. Check error status of
473 * the address space and return it.
474 *
475 * Since the error status of the address space is cleared by this function,
476 * callers are responsible for checking the return value and handling and/or
477 * reporting the error.
478 */
479int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
480 loff_t end_byte)
481{
482 int ret, ret2;
483
484 ret = __filemap_fdatawait_range(mapping, start_byte, end_byte);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700485 ret2 = filemap_check_errors(mapping);
486 if (!ret)
487 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 return ret;
490}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200491EXPORT_SYMBOL(filemap_fdatawait_range);
492
493/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800494 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
495 * @mapping: address space structure to wait for
496 *
497 * Walk the list of under-writeback pages of the given address space
498 * and wait for all of them. Unlike filemap_fdatawait(), this function
499 * does not clear error status of the address space.
500 *
501 * Use this function if callers don't handle errors themselves. Expected
502 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
503 * fsfreeze(8)
504 */
505void filemap_fdatawait_keep_errors(struct address_space *mapping)
506{
507 loff_t i_size = i_size_read(mapping->host);
508
509 if (i_size == 0)
510 return;
511
512 __filemap_fdatawait_range(mapping, 0, i_size - 1);
513}
514
515/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700516 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700518 *
519 * Walk the list of under-writeback pages of the given address space
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800520 * and wait for all of them. Check error status of the address space
521 * and return it.
522 *
523 * Since the error status of the address space is cleared by this function,
524 * callers are responsible for checking the return value and handling and/or
525 * reporting the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
527int filemap_fdatawait(struct address_space *mapping)
528{
529 loff_t i_size = i_size_read(mapping->host);
530
531 if (i_size == 0)
532 return 0;
533
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200534 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536EXPORT_SYMBOL(filemap_fdatawait);
537
538int filemap_write_and_wait(struct address_space *mapping)
539{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800540 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800542 if ((!dax_mapping(mapping) && mapping->nrpages) ||
543 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800544 err = filemap_fdatawrite(mapping);
545 /*
546 * Even if the above returned error, the pages may be
547 * written partially (e.g. -ENOSPC), so we wait for it.
548 * But the -EIO is special case, it may indicate the worst
549 * thing (e.g. bug) happened, so we avoid waiting for it.
550 */
551 if (err != -EIO) {
552 int err2 = filemap_fdatawait(mapping);
553 if (!err)
554 err = err2;
555 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700556 } else {
557 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800561EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Randy Dunlap485bb992006-06-23 02:03:49 -0700563/**
564 * filemap_write_and_wait_range - write out & wait on a file range
565 * @mapping: the address_space for the pages
566 * @lstart: offset in bytes where the range starts
567 * @lend: offset in bytes where the range ends (inclusive)
568 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800569 * Write out and wait upon file offsets lstart->lend, inclusive.
570 *
571 * Note that `lend' is inclusive (describes the last byte to be written) so
572 * that this function can be used to write to the very end-of-file (end = -1).
573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574int filemap_write_and_wait_range(struct address_space *mapping,
575 loff_t lstart, loff_t lend)
576{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800577 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800579 if ((!dax_mapping(mapping) && mapping->nrpages) ||
580 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800581 err = __filemap_fdatawrite_range(mapping, lstart, lend,
582 WB_SYNC_ALL);
583 /* See comment of filemap_write_and_wait() */
584 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200585 int err2 = filemap_fdatawait_range(mapping,
586 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800587 if (!err)
588 err = err2;
589 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700590 } else {
591 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
Chris Masonf6995582009-04-15 13:22:37 -0400595EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Randy Dunlap485bb992006-06-23 02:03:49 -0700597/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700598 * replace_page_cache_page - replace a pagecache page with a new one
599 * @old: page to be replaced
600 * @new: page to replace with
601 * @gfp_mask: allocation mode
602 *
603 * This function replaces a page in the pagecache with a new one. On
604 * success it acquires the pagecache reference for the new page and
605 * drops it for the old page. Both the old and new pages must be
606 * locked. This function does not add the new page to the LRU, the
607 * caller must do that.
608 *
609 * The remove + add is atomic. The only way this function can fail is
610 * memory allocation failure.
611 */
612int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
613{
614 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700615
Sasha Levin309381fea2014-01-23 15:52:54 -0800616 VM_BUG_ON_PAGE(!PageLocked(old), old);
617 VM_BUG_ON_PAGE(!PageLocked(new), new);
618 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700619
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -0700620 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700621 if (!error) {
622 struct address_space *mapping = old->mapping;
623 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400624 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700625
626 pgoff_t offset = old->index;
627 freepage = mapping->a_ops->freepage;
628
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300629 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700630 new->mapping = mapping;
631 new->index = offset;
632
Greg Thelenc4843a72015-05-22 17:13:16 -0400633 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700634 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700635 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700636 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700637
638 /*
639 * hugetlb pages do not participate in page cache accounting.
640 */
641 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700642 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700643 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700644 __inc_node_page_state(new, NR_SHMEM);
Greg Thelenc4843a72015-05-22 17:13:16 -0400645 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700646 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700647 radix_tree_preload_end();
648 if (freepage)
649 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300650 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700651 }
652
653 return error;
654}
655EXPORT_SYMBOL_GPL(replace_page_cache_page);
656
Johannes Weinera5289102014-04-03 14:47:51 -0700657static int __add_to_page_cache_locked(struct page *page,
658 struct address_space *mapping,
659 pgoff_t offset, gfp_t gfp_mask,
660 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Johannes Weiner00501b52014-08-08 14:19:20 -0700662 int huge = PageHuge(page);
663 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700664 int error;
665
Sasha Levin309381fea2014-01-23 15:52:54 -0800666 VM_BUG_ON_PAGE(!PageLocked(page), page);
667 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700668
Johannes Weiner00501b52014-08-08 14:19:20 -0700669 if (!huge) {
670 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800671 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700672 if (error)
673 return error;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -0700676 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700677 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700678 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800679 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700680 return error;
681 }
682
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300683 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700684 page->mapping = mapping;
685 page->index = offset;
686
687 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700688 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700689 radix_tree_preload_end();
690 if (unlikely(error))
691 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700692
693 /* hugetlb pages do not participate in page cache accounting. */
694 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700695 __inc_node_page_state(page, NR_FILE_PAGES);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700696 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700697 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800698 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700699 trace_mm_filemap_add_to_page_cache(page);
700 return 0;
701err_insert:
702 page->mapping = NULL;
703 /* Leave page->index set: truncation relies upon it */
704 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700705 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800706 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return error;
709}
Johannes Weinera5289102014-04-03 14:47:51 -0700710
711/**
712 * add_to_page_cache_locked - add a locked page to the pagecache
713 * @page: page to add
714 * @mapping: the page's address_space
715 * @offset: page index
716 * @gfp_mask: page allocation mode
717 *
718 * This function is used to add a page to the pagecache. It must be locked.
719 * This function does not add the page to the LRU. The caller must do that.
720 */
721int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
722 pgoff_t offset, gfp_t gfp_mask)
723{
724 return __add_to_page_cache_locked(page, mapping, offset,
725 gfp_mask, NULL);
726}
Nick Piggine2867812008-07-25 19:45:30 -0700727EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400730 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Johannes Weinera5289102014-04-03 14:47:51 -0700732 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700733 int ret;
734
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800735 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700736 ret = __add_to_page_cache_locked(page, mapping, offset,
737 gfp_mask, &shadow);
738 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800739 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700740 else {
741 /*
742 * The page might have been evicted from cache only
743 * recently, in which case it should be activated like
744 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700745 * The exception is pages getting rewritten; evicting other
746 * data from the working set, only to cache data that will
747 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700748 */
Johannes Weinera2383322018-10-26 15:06:04 -0700749 WARN_ON_ONCE(PageActive(page));
750 if (!(gfp_mask & __GFP_WRITE) && shadow)
751 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700752 lru_cache_add(page);
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return ret;
755}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300756EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Paul Jackson44110fe2006-03-24 03:16:04 -0800758#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700759struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800760{
Miao Xiec0ff7452010-05-24 14:32:08 -0700761 int n;
762 struct page *page;
763
Paul Jackson44110fe2006-03-24 03:16:04 -0800764 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700765 unsigned int cpuset_mems_cookie;
766 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700767 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700768 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700769 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700770 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700771
Miao Xiec0ff7452010-05-24 14:32:08 -0700772 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800773 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700774 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800775}
Nick Piggin2ae88142006-10-28 10:38:23 -0700776EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800777#endif
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/*
780 * In order to wait for pages to become available there must be
781 * waitqueues associated with pages. By using a hash table of
782 * waitqueues where the bucket discipline is to maintain all
783 * waiters on the same queue and wake all when any of the pages
784 * become available, and for the woken contexts to check to be
785 * sure the appropriate page became available, this saves space
786 * at a cost of "thundering herd" phenomena during rare hash
787 * collisions.
788 */
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000789#define PAGE_WAIT_TABLE_BITS 8
790#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
791static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
792
793static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000795 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000797
798void __init pagecache_init(void)
799{
800 int i;
801
802 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
803 init_waitqueue_head(&page_wait_table[i]);
804
805 page_writeback_init();
806}
807
808struct wait_page_key {
809 struct page *page;
810 int bit_nr;
811 int page_match;
812};
813
814struct wait_page_queue {
815 struct page *page;
816 int bit_nr;
817 wait_queue_t wait;
818};
819
820static int wake_page_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
821{
822 struct wait_page_key *key = arg;
823 struct wait_page_queue *wait_page
824 = container_of(wait, struct wait_page_queue, wait);
825
826 if (wait_page->page != key->page)
827 return 0;
828 key->page_match = 1;
829
830 if (wait_page->bit_nr != key->bit_nr)
831 return 0;
832 if (test_bit(key->bit_nr, &key->page->flags))
833 return 0;
834
835 return autoremove_wake_function(wait, mode, sync, key);
836}
837
838void wake_up_page_bit(struct page *page, int bit_nr)
839{
840 wait_queue_head_t *q = page_waitqueue(page);
841 struct wait_page_key key;
842 unsigned long flags;
843
844 key.page = page;
845 key.bit_nr = bit_nr;
846 key.page_match = 0;
847
848 spin_lock_irqsave(&q->lock, flags);
849 __wake_up_locked_key(q, TASK_NORMAL, &key);
850 /*
851 * It is possible for other pages to have collided on the waitqueue
852 * hash, so in that case check for a page match. That prevents a long-
853 * term waiter
854 *
855 * It is still possible to miss a case here, when we woke page waiters
856 * and removed them from the waitqueue, but there are still other
857 * page waiters.
858 */
859 if (!waitqueue_active(q) || !key.page_match) {
860 ClearPageWaiters(page);
861 /*
862 * It's possible to miss clearing Waiters here, when we woke
863 * our page waiters, but the hashed waitqueue has waiters for
864 * other pages on it.
865 *
866 * That's okay, it's a rare case. The next waker will clear it.
867 */
868 }
869 spin_unlock_irqrestore(&q->lock, flags);
870}
871EXPORT_SYMBOL(wake_up_page_bit);
872
873static inline int wait_on_page_bit_common(wait_queue_head_t *q,
874 struct page *page, int bit_nr, int state, bool lock)
875{
876 struct wait_page_queue wait_page;
877 wait_queue_t *wait = &wait_page.wait;
Johannes Weinerd6687a22018-10-26 15:06:08 -0700878 bool thrashing = false;
Johannes Weiner3df0e592018-10-26 15:06:27 -0700879 unsigned long pflags;
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000880 int ret = 0;
881
Johannes Weiner3df0e592018-10-26 15:06:27 -0700882 if (bit_nr == PG_locked &&
Johannes Weinerd6687a22018-10-26 15:06:08 -0700883 !PageUptodate(page) && PageWorkingset(page)) {
Johannes Weiner3df0e592018-10-26 15:06:27 -0700884 if (!PageSwapBacked(page))
885 delayacct_thrashing_start();
886 psi_memstall_enter(&pflags);
Johannes Weinerd6687a22018-10-26 15:06:08 -0700887 thrashing = true;
888 }
889
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000890 init_wait(wait);
891 wait->func = wake_page_function;
892 wait_page.page = page;
893 wait_page.bit_nr = bit_nr;
894
895 for (;;) {
896 spin_lock_irq(&q->lock);
897
898 if (likely(list_empty(&wait->task_list))) {
899 if (lock)
900 __add_wait_queue_tail_exclusive(q, wait);
901 else
902 __add_wait_queue(q, wait);
903 SetPageWaiters(page);
904 }
905
906 set_current_state(state);
907
908 spin_unlock_irq(&q->lock);
909
910 if (likely(test_bit(bit_nr, &page->flags))) {
911 io_schedule();
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000912 }
913
914 if (lock) {
915 if (!test_and_set_bit_lock(bit_nr, &page->flags))
916 break;
917 } else {
918 if (!test_bit(bit_nr, &page->flags))
919 break;
920 }
Linus Torvalds7394f692017-08-27 16:25:09 -0700921
922 if (unlikely(signal_pending_state(state, current))) {
923 ret = -EINTR;
924 break;
925 }
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000926 }
927
928 finish_wait(q, wait);
929
Johannes Weiner3df0e592018-10-26 15:06:27 -0700930 if (thrashing) {
931 if (!PageSwapBacked(page))
932 delayacct_thrashing_end();
933 psi_memstall_leave(&pflags);
934 }
Johannes Weinerd6687a22018-10-26 15:06:08 -0700935
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000936 /*
937 * A signal could leave PageWaiters set. Clearing it here if
938 * !waitqueue_active would be possible (by open-coding finish_wait),
939 * but still fail to catch it in the case of wait hash collision. We
940 * already can fail to clear wait hash collision cases, so don't
941 * bother with signals either.
942 */
943
944 return ret;
945}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Harvey Harrison920c7a52008-02-04 22:29:26 -0800947void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000949 wait_queue_head_t *q = page_waitqueue(page);
950 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952EXPORT_SYMBOL(wait_on_page_bit);
953
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700954int wait_on_page_bit_killable(struct page *page, int bit_nr)
955{
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000956 wait_queue_head_t *q = page_waitqueue(page);
957 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100961 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700962 * @page: Page defining the wait queue of interest
963 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100964 *
965 * Add an arbitrary @waiter to the wait queue for the nominated @page.
966 */
967void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
968{
969 wait_queue_head_t *q = page_waitqueue(page);
970 unsigned long flags;
971
972 spin_lock_irqsave(&q->lock, flags);
973 __add_wait_queue(q, waiter);
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000974 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +0100975 spin_unlock_irqrestore(&q->lock, flags);
976}
977EXPORT_SYMBOL_GPL(add_page_wait_queue);
978
979/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700980 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * @page: the page
982 *
983 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
984 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +0900985 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
987 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700988 * The mb is necessary to enforce ordering between the clear_bit and the read
989 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800991void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800993 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800994 VM_BUG_ON_PAGE(!PageLocked(page), page);
Nick Piggin8413ac92008-10-18 20:26:59 -0700995 clear_bit_unlock(PG_locked, &page->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100996 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 wake_up_page(page, PG_locked);
998}
999EXPORT_SYMBOL(unlock_page);
1000
Randy Dunlap485bb992006-06-23 02:03:49 -07001001/**
1002 * end_page_writeback - end writeback against a page
1003 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
1005void end_page_writeback(struct page *page)
1006{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001007 /*
1008 * TestClearPageReclaim could be used here but it is an atomic
1009 * operation and overkill in this particular case. Failing to
1010 * shuffle a page marked for immediate reclaim is too mild to
1011 * justify taking an atomic operation penalty at the end of
1012 * ever page writeback.
1013 */
1014 if (PageReclaim(page)) {
1015 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001016 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001017 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001018
1019 if (!test_clear_page_writeback(page))
1020 BUG();
1021
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001022 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 wake_up_page(page, PG_writeback);
1024}
1025EXPORT_SYMBOL(end_page_writeback);
1026
Matthew Wilcox57d99842014-06-04 16:07:45 -07001027/*
1028 * After completing I/O on a page, call this routine to update the page
1029 * flags appropriately
1030 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001031void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001032{
Jens Axboec11f0c02016-08-05 08:11:04 -06001033 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001034 if (!err) {
1035 SetPageUptodate(page);
1036 } else {
1037 ClearPageUptodate(page);
1038 SetPageError(page);
1039 }
1040 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001041 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001042 if (err) {
Minchan Kim2c290ee2017-02-24 14:59:59 -08001043 struct address_space *mapping;
1044
Matthew Wilcox57d99842014-06-04 16:07:45 -07001045 SetPageError(page);
Minchan Kim2c290ee2017-02-24 14:59:59 -08001046 mapping = page_mapping(page);
1047 if (mapping)
1048 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001049 }
1050 end_page_writeback(page);
1051 }
1052}
1053EXPORT_SYMBOL_GPL(page_endio);
1054
Randy Dunlap485bb992006-06-23 02:03:49 -07001055/**
1056 * __lock_page - get a lock on the page, assuming we need to sleep to get it
1057 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001059void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001061 struct page *page = compound_head(__page);
1062 wait_queue_head_t *q = page_waitqueue(page);
1063 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065EXPORT_SYMBOL(__lock_page);
1066
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001067int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001068{
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001069 struct page *page = compound_head(__page);
1070 wait_queue_head_t *q = page_waitqueue(page);
1071 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001072}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001073EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001074
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001075/*
1076 * Return values:
1077 * 1 - page is locked; mmap_sem is still held.
1078 * 0 - page is not locked.
1079 * mmap_sem has been released (up_read()), unless flags had both
1080 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1081 * which case mmap_sem is still held.
1082 *
1083 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1084 * with the page locked and the mmap_sem unperturbed.
1085 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001086int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1087 unsigned int flags)
1088{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001089 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1090 /*
1091 * CAUTION! In this case, mmap_sem is not released
1092 * even though return 0.
1093 */
1094 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1095 return 0;
1096
1097 up_read(&mm->mmap_sem);
1098 if (flags & FAULT_FLAG_KILLABLE)
1099 wait_on_page_locked_killable(page);
1100 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001101 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001102 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001103 } else {
1104 if (flags & FAULT_FLAG_KILLABLE) {
1105 int ret;
1106
1107 ret = __lock_page_killable(page);
1108 if (ret) {
1109 up_read(&mm->mmap_sem);
1110 return 0;
1111 }
1112 } else
1113 __lock_page(page);
1114 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001115 }
1116}
1117
Randy Dunlap485bb992006-06-23 02:03:49 -07001118/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001119 * page_cache_next_hole - find the next hole (not-present entry)
1120 * @mapping: mapping
1121 * @index: index
1122 * @max_scan: maximum range to search
1123 *
1124 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1125 * lowest indexed hole.
1126 *
1127 * Returns: the index of the hole if found, otherwise returns an index
1128 * outside of the set specified (in which case 'return - index >=
1129 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1130 * be returned.
1131 *
1132 * page_cache_next_hole may be called under rcu_read_lock. However,
1133 * like radix_tree_gang_lookup, this will not atomically search a
1134 * snapshot of the tree at a single point in time. For example, if a
1135 * hole is created at index 5, then subsequently a hole is created at
1136 * index 10, page_cache_next_hole covering both indexes may return 10
1137 * if called under rcu_read_lock.
1138 */
1139pgoff_t page_cache_next_hole(struct address_space *mapping,
1140 pgoff_t index, unsigned long max_scan)
1141{
1142 unsigned long i;
1143
1144 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001145 struct page *page;
1146
1147 page = radix_tree_lookup(&mapping->page_tree, index);
1148 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001149 break;
1150 index++;
1151 if (index == 0)
1152 break;
1153 }
1154
1155 return index;
1156}
1157EXPORT_SYMBOL(page_cache_next_hole);
1158
1159/**
1160 * page_cache_prev_hole - find the prev hole (not-present entry)
1161 * @mapping: mapping
1162 * @index: index
1163 * @max_scan: maximum range to search
1164 *
1165 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1166 * the first hole.
1167 *
1168 * Returns: the index of the hole if found, otherwise returns an index
1169 * outside of the set specified (in which case 'index - return >=
1170 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1171 * will be returned.
1172 *
1173 * page_cache_prev_hole may be called under rcu_read_lock. However,
1174 * like radix_tree_gang_lookup, this will not atomically search a
1175 * snapshot of the tree at a single point in time. For example, if a
1176 * hole is created at index 10, then subsequently a hole is created at
1177 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1178 * called under rcu_read_lock.
1179 */
1180pgoff_t page_cache_prev_hole(struct address_space *mapping,
1181 pgoff_t index, unsigned long max_scan)
1182{
1183 unsigned long i;
1184
1185 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001186 struct page *page;
1187
1188 page = radix_tree_lookup(&mapping->page_tree, index);
1189 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001190 break;
1191 index--;
1192 if (index == ULONG_MAX)
1193 break;
1194 }
1195
1196 return index;
1197}
1198EXPORT_SYMBOL(page_cache_prev_hole);
1199
1200/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001201 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001202 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001203 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001204 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001205 * Looks up the page cache slot at @mapping & @offset. If there is a
1206 * page cache page, it is returned with an increased refcount.
1207 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001208 * If the slot holds a shadow entry of a previously evicted page, or a
1209 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001210 *
1211 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001213struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Nick Piggina60637c2008-07-25 19:45:31 -07001215 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001216 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Nick Piggina60637c2008-07-25 19:45:31 -07001218 rcu_read_lock();
1219repeat:
1220 page = NULL;
1221 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1222 if (pagep) {
1223 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001224 if (unlikely(!page))
1225 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001226 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001227 if (radix_tree_deref_retry(page))
1228 goto repeat;
1229 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001230 * A shadow entry of a recently evicted page,
1231 * or a swap entry from shmem/tmpfs. Return
1232 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001233 */
1234 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001235 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001236
1237 head = compound_head(page);
1238 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001239 goto repeat;
1240
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001241 /* The page was split under us? */
1242 if (compound_head(page) != head) {
1243 put_page(head);
1244 goto repeat;
1245 }
1246
Nick Piggina60637c2008-07-25 19:45:31 -07001247 /*
1248 * Has the page moved?
1249 * This is part of the lockless pagecache protocol. See
1250 * include/linux/pagemap.h for details.
1251 */
1252 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001253 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001254 goto repeat;
1255 }
1256 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001257out:
Nick Piggina60637c2008-07-25 19:45:31 -07001258 rcu_read_unlock();
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return page;
1261}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001262EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Randy Dunlap485bb992006-06-23 02:03:49 -07001264/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001265 * find_lock_entry - locate, pin and lock a page cache entry
1266 * @mapping: the address_space to search
1267 * @offset: the page cache index
1268 *
1269 * Looks up the page cache slot at @mapping & @offset. If there is a
1270 * page cache page, it is returned locked and with an increased
1271 * refcount.
1272 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001273 * If the slot holds a shadow entry of a previously evicted page, or a
1274 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001275 *
1276 * Otherwise, %NULL is returned.
1277 *
1278 * find_lock_entry() may sleep.
1279 */
1280struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct page *page;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001285 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001286 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001287 lock_page(page);
1288 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001289 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001290 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001292 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001294 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return page;
1297}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001298EXPORT_SYMBOL(find_lock_entry);
1299
1300/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001301 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001302 * @mapping: the address_space to search
1303 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001304 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001305 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001306 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001307 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001308 *
Randy Dunlap75325182014-07-30 16:08:37 -07001309 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001310 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001311 * FGP_ACCESSED: the page will be marked accessed
1312 * FGP_LOCK: Page is return locked
1313 * FGP_CREAT: If page is not present then a new page is allocated using
Michal Hocko45f87de2014-12-29 20:30:35 +01001314 * @gfp_mask and added to the page cache and the VM's LRU
1315 * list. The page is returned locked and with an increased
1316 * refcount. Otherwise, %NULL is returned.
Josef Bacik091c93e2019-01-02 00:21:01 +09001317 * FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1318 * its own locking dance if the page is already in cache, or unlock the page
1319 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001320 *
1321 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1322 * if the GFP flags specified for FGP_CREAT are atomic.
1323 *
1324 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001325 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001326struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001327 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Nick Piggineb2be182007-10-16 01:24:57 -07001329 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001332 page = find_get_entry(mapping, offset);
1333 if (radix_tree_exceptional_entry(page))
1334 page = NULL;
1335 if (!page)
1336 goto no_page;
1337
1338 if (fgp_flags & FGP_LOCK) {
1339 if (fgp_flags & FGP_NOWAIT) {
1340 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001341 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001342 return NULL;
1343 }
1344 } else {
1345 lock_page(page);
1346 }
1347
1348 /* Has the page been truncated? */
1349 if (unlikely(page->mapping != mapping)) {
1350 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001351 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001352 goto repeat;
1353 }
1354 VM_BUG_ON_PAGE(page->index != offset, page);
1355 }
1356
1357 if (page && (fgp_flags & FGP_ACCESSED))
1358 mark_page_accessed(page);
1359
1360no_page:
1361 if (!page && (fgp_flags & FGP_CREAT)) {
1362 int err;
1363 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001364 gfp_mask |= __GFP_WRITE;
1365 if (fgp_flags & FGP_NOFS)
1366 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001367
Michal Hocko45f87de2014-12-29 20:30:35 +01001368 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001369 if (!page)
1370 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001371
Josef Bacik091c93e2019-01-02 00:21:01 +09001372 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001373 fgp_flags |= FGP_LOCK;
1374
Hugh Dickinseb39d612014-08-06 16:06:43 -07001375 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001376 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001377 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001378
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -07001379 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001380 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001381 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001382 page = NULL;
1383 if (err == -EEXIST)
1384 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Josef Bacik091c93e2019-01-02 00:21:01 +09001386
1387 /*
1388 * add_to_page_cache_lru lock's the page, and for mmap we expect
1389 * a unlocked page.
1390 */
1391 if (page && (fgp_flags & FGP_FOR_MMAP))
1392 unlock_page(page);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return page;
1397}
Mel Gorman2457aec2014-06-04 16:10:31 -07001398EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001401 * find_get_entries - gang pagecache lookup
1402 * @mapping: The address_space to search
1403 * @start: The starting page cache index
1404 * @nr_entries: The maximum number of entries
1405 * @entries: Where the resulting entries are placed
1406 * @indices: The cache indices corresponding to the entries in @entries
1407 *
1408 * find_get_entries() will search for and return a group of up to
1409 * @nr_entries entries in the mapping. The entries are placed at
1410 * @entries. find_get_entries() takes a reference against any actual
1411 * pages it returns.
1412 *
1413 * The search returns a group of mapping-contiguous page cache entries
1414 * with ascending indexes. There may be holes in the indices due to
1415 * not-present pages.
1416 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001417 * Any shadow entries of evicted pages, or swap entries from
1418 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001419 *
1420 * find_get_entries() returns the number of pages and shadow entries
1421 * which were found.
1422 */
1423unsigned find_get_entries(struct address_space *mapping,
1424 pgoff_t start, unsigned int nr_entries,
1425 struct page **entries, pgoff_t *indices)
1426{
1427 void **slot;
1428 unsigned int ret = 0;
1429 struct radix_tree_iter iter;
1430
1431 if (!nr_entries)
1432 return 0;
1433
1434 rcu_read_lock();
Johannes Weiner0cd61442014-04-03 14:47:46 -07001435 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001436 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001437repeat:
1438 page = radix_tree_deref_slot(slot);
1439 if (unlikely(!page))
1440 continue;
1441 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001442 if (radix_tree_deref_retry(page)) {
1443 slot = radix_tree_iter_retry(&iter);
1444 continue;
1445 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001446 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001447 * A shadow entry of a recently evicted page, a swap
1448 * entry from shmem/tmpfs or a DAX entry. Return it
1449 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001450 */
1451 goto export;
1452 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001453
1454 head = compound_head(page);
1455 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001456 goto repeat;
1457
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001458 /* The page was split under us? */
1459 if (compound_head(page) != head) {
1460 put_page(head);
1461 goto repeat;
1462 }
1463
Johannes Weiner0cd61442014-04-03 14:47:46 -07001464 /* Has the page moved? */
1465 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001466 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001467 goto repeat;
1468 }
1469export:
1470 indices[ret] = iter.index;
1471 entries[ret] = page;
1472 if (++ret == nr_entries)
1473 break;
1474 }
1475 rcu_read_unlock();
1476 return ret;
1477}
1478
1479/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * find_get_pages - gang pagecache lookup
1481 * @mapping: The address_space to search
1482 * @start: The starting page index
1483 * @nr_pages: The maximum number of pages
1484 * @pages: Where the resulting pages are placed
1485 *
1486 * find_get_pages() will search for and return a group of up to
1487 * @nr_pages pages in the mapping. The pages are placed at @pages.
1488 * find_get_pages() takes a reference against the returned pages.
1489 *
1490 * The search returns a group of mapping-contiguous pages with ascending
1491 * indexes. There may be holes in the indices due to not-present pages.
1492 *
1493 * find_get_pages() returns the number of pages which were found.
1494 */
1495unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1496 unsigned int nr_pages, struct page **pages)
1497{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001498 struct radix_tree_iter iter;
1499 void **slot;
1500 unsigned ret = 0;
1501
1502 if (unlikely(!nr_pages))
1503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Nick Piggina60637c2008-07-25 19:45:31 -07001505 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001506 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001507 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001508repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001509 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001510 if (unlikely(!page))
1511 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001512
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001513 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001514 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001515 slot = radix_tree_iter_retry(&iter);
1516 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001517 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001518 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001519 * A shadow entry of a recently evicted page,
1520 * or a swap entry from shmem/tmpfs. Skip
1521 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001522 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001523 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001524 }
Nick Piggina60637c2008-07-25 19:45:31 -07001525
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001526 head = compound_head(page);
1527 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001528 goto repeat;
1529
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001530 /* The page was split under us? */
1531 if (compound_head(page) != head) {
1532 put_page(head);
1533 goto repeat;
1534 }
1535
Nick Piggina60637c2008-07-25 19:45:31 -07001536 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001537 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001538 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001539 goto repeat;
1540 }
1541
1542 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001543 if (++ret == nr_pages)
1544 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001545 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001546
Nick Piggina60637c2008-07-25 19:45:31 -07001547 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return ret;
1549}
1550
Jens Axboeebf43502006-04-27 08:46:01 +02001551/**
1552 * find_get_pages_contig - gang contiguous pagecache lookup
1553 * @mapping: The address_space to search
1554 * @index: The starting page index
1555 * @nr_pages: The maximum number of pages
1556 * @pages: Where the resulting pages are placed
1557 *
1558 * find_get_pages_contig() works exactly like find_get_pages(), except
1559 * that the returned number of pages are guaranteed to be contiguous.
1560 *
1561 * find_get_pages_contig() returns the number of pages which were found.
1562 */
1563unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1564 unsigned int nr_pages, struct page **pages)
1565{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001566 struct radix_tree_iter iter;
1567 void **slot;
1568 unsigned int ret = 0;
1569
1570 if (unlikely(!nr_pages))
1571 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001572
Nick Piggina60637c2008-07-25 19:45:31 -07001573 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001574 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001575 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001576repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001577 page = radix_tree_deref_slot(slot);
1578 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001579 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001580 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001581
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001582 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001583 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001584 slot = radix_tree_iter_retry(&iter);
1585 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001586 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001587 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001588 * A shadow entry of a recently evicted page,
1589 * or a swap entry from shmem/tmpfs. Stop
1590 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001591 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001592 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001593 }
Nick Piggina60637c2008-07-25 19:45:31 -07001594
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001595 head = compound_head(page);
1596 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001597 goto repeat;
1598
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001599 /* The page was split under us? */
1600 if (compound_head(page) != head) {
1601 put_page(head);
1602 goto repeat;
1603 }
1604
Nick Piggina60637c2008-07-25 19:45:31 -07001605 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001606 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001607 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001608 goto repeat;
1609 }
1610
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001611 /*
1612 * must check mapping and index after taking the ref.
1613 * otherwise we can get both false positives and false
1614 * negatives, which is just confusing to the caller.
1615 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001616 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001617 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001618 break;
1619 }
1620
Nick Piggina60637c2008-07-25 19:45:31 -07001621 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001622 if (++ret == nr_pages)
1623 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001624 }
Nick Piggina60637c2008-07-25 19:45:31 -07001625 rcu_read_unlock();
1626 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001627}
David Howellsef71c152007-05-09 02:33:44 -07001628EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001629
Randy Dunlap485bb992006-06-23 02:03:49 -07001630/**
Jan Karafb296a22017-11-15 17:34:33 -08001631 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001632 * @mapping: the address_space to search
1633 * @index: the starting page index
Jan Karafb296a22017-11-15 17:34:33 -08001634 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001635 * @tag: the tag index
1636 * @nr_pages: the maximum number of pages
1637 * @pages: where the resulting pages are placed
1638 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001640 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
Jan Karafb296a22017-11-15 17:34:33 -08001642unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1643 pgoff_t end, int tag, unsigned int nr_pages,
1644 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001646 struct radix_tree_iter iter;
1647 void **slot;
1648 unsigned ret = 0;
1649
1650 if (unlikely(!nr_pages))
1651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Nick Piggina60637c2008-07-25 19:45:31 -07001653 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001654 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1655 &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001656 struct page *head, *page;
Jan Karafb296a22017-11-15 17:34:33 -08001657
1658 if (iter.index > end)
1659 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001660repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001661 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001662 if (unlikely(!page))
1663 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001664
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001665 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001666 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001667 slot = radix_tree_iter_retry(&iter);
1668 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001669 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001670 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001671 * A shadow entry of a recently evicted page.
1672 *
1673 * Those entries should never be tagged, but
1674 * this tree walk is lockless and the tags are
1675 * looked up in bulk, one radix tree node at a
1676 * time, so there is a sizable window for page
1677 * reclaim to evict a page we saw tagged.
1678 *
1679 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001680 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001681 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001682 }
Nick Piggina60637c2008-07-25 19:45:31 -07001683
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001684 head = compound_head(page);
1685 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001686 goto repeat;
1687
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001688 /* The page was split under us? */
1689 if (compound_head(page) != head) {
1690 put_page(head);
1691 goto repeat;
1692 }
1693
Nick Piggina60637c2008-07-25 19:45:31 -07001694 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001695 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001696 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001697 goto repeat;
1698 }
1699
1700 pages[ret] = page;
Jan Karafb296a22017-11-15 17:34:33 -08001701 if (++ret == nr_pages) {
1702 *index = pages[ret - 1]->index + 1;
1703 goto out;
1704 }
Nick Piggina60637c2008-07-25 19:45:31 -07001705 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001706
Jan Karafb296a22017-11-15 17:34:33 -08001707 /*
1708 * We come here when we got at @end. We take care to not overflow the
1709 * index @index as it confuses some of the callers. This breaks the
1710 * iteration when there is page at index -1 but that is already broken
1711 * anyway.
1712 */
1713 if (end == (pgoff_t)-1)
1714 *index = (pgoff_t)-1;
1715 else
1716 *index = end + 1;
1717out:
Nick Piggina60637c2008-07-25 19:45:31 -07001718 rcu_read_unlock();
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return ret;
1721}
Jan Karafb296a22017-11-15 17:34:33 -08001722EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001724/**
1725 * find_get_entries_tag - find and return entries that match @tag
1726 * @mapping: the address_space to search
1727 * @start: the starting page cache index
1728 * @tag: the tag index
1729 * @nr_entries: the maximum number of entries
1730 * @entries: where the resulting entries are placed
1731 * @indices: the cache indices corresponding to the entries in @entries
1732 *
1733 * Like find_get_entries, except we only return entries which are tagged with
1734 * @tag.
1735 */
1736unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1737 int tag, unsigned int nr_entries,
1738 struct page **entries, pgoff_t *indices)
1739{
1740 void **slot;
1741 unsigned int ret = 0;
1742 struct radix_tree_iter iter;
1743
1744 if (!nr_entries)
1745 return 0;
1746
1747 rcu_read_lock();
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001748 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1749 &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001750 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001751repeat:
1752 page = radix_tree_deref_slot(slot);
1753 if (unlikely(!page))
1754 continue;
1755 if (radix_tree_exception(page)) {
1756 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001757 slot = radix_tree_iter_retry(&iter);
1758 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001759 }
1760
1761 /*
1762 * A shadow entry of a recently evicted page, a swap
1763 * entry from shmem/tmpfs or a DAX entry. Return it
1764 * without attempting to raise page count.
1765 */
1766 goto export;
1767 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001768
1769 head = compound_head(page);
1770 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001771 goto repeat;
1772
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001773 /* The page was split under us? */
1774 if (compound_head(page) != head) {
1775 put_page(head);
1776 goto repeat;
1777 }
1778
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001779 /* Has the page moved? */
1780 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001781 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001782 goto repeat;
1783 }
1784export:
1785 indices[ret] = iter.index;
1786 entries[ret] = page;
1787 if (++ret == nr_entries)
1788 break;
1789 }
1790 rcu_read_unlock();
1791 return ret;
1792}
1793EXPORT_SYMBOL(find_get_entries_tag);
1794
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001795/*
1796 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1797 * a _large_ part of the i/o request. Imagine the worst scenario:
1798 *
1799 * ---R__________________________________________B__________
1800 * ^ reading here ^ bad block(assume 4k)
1801 *
1802 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1803 * => failing the whole request => read(R) => read(R+1) =>
1804 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1805 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1806 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1807 *
1808 * It is going insane. Fix it by quickly scaling down the readahead size.
1809 */
1810static void shrink_readahead_size_eio(struct file *filp,
1811 struct file_ra_state *ra)
1812{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001813 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001814}
1815
Randy Dunlap485bb992006-06-23 02:03:49 -07001816/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001817 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001818 * @filp: the file to read
1819 * @ppos: current file position
Al Viro6e58e792014-02-03 17:07:03 -05001820 * @iter: data destination
1821 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001822 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001824 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *
1826 * This is really ugly. But the goto's actually try to clarify some
1827 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Al Viro6e58e792014-02-03 17:07:03 -05001829static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
1830 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001832 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001834 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001835 pgoff_t index;
1836 pgoff_t last_index;
1837 pgoff_t prev_index;
1838 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001839 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001840 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Wei Fangc2a97372016-10-07 17:01:52 -07001842 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvalds48466c42016-12-14 12:45:25 -08001843 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07001844 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
1845
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001846 index = *ppos >> PAGE_SHIFT;
1847 prev_index = ra->prev_pos >> PAGE_SHIFT;
1848 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1849 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1850 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 for (;;) {
1853 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001854 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001855 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 unsigned long nr, ret;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859find_page:
Michal Hockob67c7d32017-02-03 13:13:29 -08001860 if (fatal_signal_pending(current)) {
1861 error = -EINTR;
1862 goto out;
1863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001866 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001867 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001868 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001869 index, last_index - index);
1870 page = find_get_page(mapping, index);
1871 if (unlikely(page == NULL))
1872 goto no_cached_page;
1873 }
1874 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001875 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001876 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001877 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001879 if (!PageUptodate(page)) {
Mel Gormanebded022016-03-15 14:55:39 -07001880 /*
1881 * See comment in do_read_cache_page on why
1882 * wait_on_page_locked is used to avoid unnecessarily
1883 * serialisations and why it's safe.
1884 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07001885 error = wait_on_page_locked_killable(page);
1886 if (unlikely(error))
1887 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07001888 if (PageUptodate(page))
1889 goto page_ok;
1890
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001891 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001892 !mapping->a_ops->is_partially_uptodate)
1893 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08001894 /* pipes can't handle partially uptodate pages */
1895 if (unlikely(iter->type & ITER_PIPE))
1896 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001897 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001898 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001899 /* Did it get truncated before we got the lock? */
1900 if (!page->mapping)
1901 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001902 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05001903 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001904 goto page_not_up_to_date_locked;
1905 unlock_page(page);
1906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001908 /*
1909 * i_size must be checked after we know the page is Uptodate.
1910 *
1911 * Checking i_size after the check allows us to calculate
1912 * the correct value for "nr", which means the zero-filled
1913 * part of the page is not copied back to userspace (unless
1914 * another truncate extends the file - this is desired though).
1915 */
1916
1917 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001918 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001919 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001920 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001921 goto out;
1922 }
1923
1924 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001925 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001926 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001927 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001928 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001929 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001930 goto out;
1931 }
1932 }
1933 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* If users can be writing to this page using arbitrary
1936 * virtual addresses, take care about potential aliasing
1937 * before reading the page on the kernel side.
1938 */
1939 if (mapping_writably_mapped(mapping))
1940 flush_dcache_page(page);
1941
1942 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001943 * When a sequential read accesses a page several times,
1944 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
Jan Karaec0f1632007-05-06 14:49:25 -07001946 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 mark_page_accessed(page);
1948 prev_index = index;
1949
1950 /*
1951 * Ok, we have the page, and it's up-to-date, so
1952 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
Al Viro6e58e792014-02-03 17:07:03 -05001954
1955 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001957 index += offset >> PAGE_SHIFT;
1958 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001959 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001961 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001962 written += ret;
1963 if (!iov_iter_count(iter))
1964 goto out;
1965 if (ret < nr) {
1966 error = -EFAULT;
1967 goto out;
1968 }
1969 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971page_not_up_to_date:
1972 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001973 error = lock_page_killable(page);
1974 if (unlikely(error))
1975 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001977page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001978 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (!page->mapping) {
1980 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001981 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 continue;
1983 }
1984
1985 /* Did somebody else fill it already? */
1986 if (PageUptodate(page)) {
1987 unlock_page(page);
1988 goto page_ok;
1989 }
1990
1991readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001992 /*
1993 * A previous I/O error may have been due to temporary
1994 * failures, eg. multipath errors.
1995 * PG_error will be set again if readpage fails.
1996 */
1997 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /* Start the actual read. The read will unlock the page. */
1999 error = mapping->a_ops->readpage(filp, page);
2000
Zach Brown994fc28c2005-12-15 14:28:17 -08002001 if (unlikely(error)) {
2002 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002003 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002004 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002005 goto find_page;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002011 error = lock_page_killable(page);
2012 if (unlikely(error))
2013 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (!PageUptodate(page)) {
2015 if (page->mapping == NULL) {
2016 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002017 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 */
2019 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002020 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto find_page;
2022 }
2023 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002024 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002025 error = -EIO;
2026 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 unlock_page(page);
2029 }
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto page_ok;
2032
2033readpage_error:
2034 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002035 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 goto out;
2037
2038no_cached_page:
2039 /*
2040 * Ok, it wasn't cached, so we need to create a new
2041 * page..
2042 */
Nick Piggineb2be182007-10-16 01:24:57 -07002043 page = page_cache_alloc_cold(mapping);
2044 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002045 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002048 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002049 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002051 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002052 if (error == -EEXIST) {
2053 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 goto out;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 goto readpage;
2059 }
2060
2061out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002062 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002063 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002064 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002067 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002068 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Randy Dunlap485bb992006-06-23 02:03:49 -07002071/**
Al Viro6abd2322014-04-04 14:20:57 -04002072 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002073 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002074 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002075 *
Al Viro6abd2322014-04-04 14:20:57 -04002076 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 * that can use the page cache directly.
2078 */
2079ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002080generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Al Viroed978a82014-03-05 22:53:04 -05002082 struct file *file = iocb->ki_filp;
2083 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002084 size_t count = iov_iter_count(iter);
2085
2086 if (!count)
2087 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Al Viro2ba48ce2015-04-09 13:52:01 -04002089 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viroed978a82014-03-05 22:53:04 -05002090 struct address_space *mapping = file->f_mapping;
2091 struct inode *inode = mapping->host;
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002092 struct iov_iter data = *iter;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002093 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 size = i_size_read(inode);
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002096 retval = filemap_write_and_wait_range(mapping, iocb->ki_pos,
2097 iocb->ki_pos + count - 1);
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002098 if (retval < 0)
2099 goto out;
Al Viroed978a82014-03-05 22:53:04 -05002100
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002101 file_accessed(file);
2102
2103 retval = mapping->a_ops->direct_IO(iocb, &data);
Al Viroc3a69022016-10-10 13:26:27 -04002104 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002105 iocb->ki_pos += retval;
Al Viroed978a82014-03-05 22:53:04 -05002106 iov_iter_advance(iter, retval);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002107 }
Josef Bacik66f998f2010-05-23 11:00:54 -04002108
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002109 /*
2110 * Btrfs can have a short DIO read if we encounter
2111 * compressed extents, so if there was an error, or if
2112 * we've already read everything we wanted to, or if
2113 * there was a short read because we hit EOF, go ahead
2114 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002115 * the rest of the read. Buffered reads will not work for
2116 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002117 */
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002118 if (retval < 0 || !iov_iter_count(iter) || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002119 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002123 retval = do_generic_file_read(file, &iocb->ki_pos, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124out:
2125 return retval;
2126}
Al Viroed978a82014-03-05 22:53:04 -05002127EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130#define MMAP_LOTSAMISS (100)
2131
Josef Bacikf9593722019-01-02 11:11:01 +09002132static struct file *maybe_unlock_mmap_for_io(struct vm_area_struct *vma,
2133 unsigned long flags, struct file *fpin)
2134{
2135 if (fpin)
2136 return fpin;
2137
2138 /*
2139 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2140 * anything, so we only pin the file and drop the mmap_sem if only
2141 * FAULT_FLAG_ALLOW_RETRY is set.
2142 */
2143 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2144 FAULT_FLAG_ALLOW_RETRY) {
2145 fpin = get_file(vma->vm_file);
2146 up_read(&vma->vm_mm->mmap_sem);
2147 }
2148 return fpin;
2149}
2150
Linus Torvaldsef00e082009-06-16 15:31:25 -07002151/*
Josef Bacikf9593722019-01-02 11:11:01 +09002152 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2153 * @vmf - the vm_fault for this fault.
2154 * @page - the page to lock.
2155 * @fpin - the pointer to the file we may pin (or is already pinned).
2156 *
2157 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2158 * It differs in that it actually returns the page locked if it returns 1 and 0
2159 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2160 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002161 */
Josef Bacikf9593722019-01-02 11:11:01 +09002162static int lock_page_maybe_drop_mmap(struct vm_area_struct *vma,
2163 unsigned long flags, struct page *page, struct file **fpin)
2164{
2165 if (trylock_page(page))
2166 return 1;
2167
Linus Torvalds572b9da2019-03-15 11:26:07 -07002168 /*
2169 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2170 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2171 * is supposed to work. We have way too many special cases..
2172 */
Josef Bacikf9593722019-01-02 11:11:01 +09002173 if (flags & FAULT_FLAG_RETRY_NOWAIT)
2174 return 0;
2175 *fpin = maybe_unlock_mmap_for_io(vma, flags, *fpin);
2176 if (flags & FAULT_FLAG_KILLABLE) {
2177 if (__lock_page_killable(page)) {
2178 /*
2179 * We didn't have the right flags to drop the mmap_sem,
2180 * but all fault_handlers only check for fatal signals
2181 * if we return VM_FAULT_RETRY, so we need to drop the
2182 * mmap_sem here and return 0 if we don't have a fpin.
2183 */
2184 if (*fpin == NULL)
2185 up_read(&vma->vm_mm->mmap_sem);
2186 return 0;
2187 }
2188 } else
2189 __lock_page(page);
2190 return 1;
2191}
2192
2193/*
2194 * Synchronous readahead happens when we don't even find a page in the page
2195 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2196 * to drop the mmap sem we return the file that was pinned in order for us to do
2197 * that. If we didn't pin a file then we return NULL. The file that is
2198 * returned needs to be fput()'ed when we're done with it.
2199 */
2200static struct file *do_sync_mmap_readahead(struct vm_area_struct *vma,
2201 unsigned long flags,
Linus Torvaldsef00e082009-06-16 15:31:25 -07002202 struct file_ra_state *ra,
2203 struct file *file,
2204 pgoff_t offset)
2205{
Josef Bacikf9593722019-01-02 11:11:01 +09002206 struct file *fpin = NULL;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002207 struct address_space *mapping = file->f_mapping;
2208
2209 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002210 if (vma->vm_flags & VM_RAND_READ)
Josef Bacikf9593722019-01-02 11:11:01 +09002211 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002212 if (!ra->ra_pages)
Josef Bacikf9593722019-01-02 11:11:01 +09002213 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002214
Joe Perches64363aa2013-07-08 16:00:18 -07002215 if (vma->vm_flags & VM_SEQ_READ) {
Josef Bacikf9593722019-01-02 11:11:01 +09002216 fpin = maybe_unlock_mmap_for_io(vma, flags, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002217 page_cache_sync_readahead(mapping, ra, file, offset,
2218 ra->ra_pages);
Josef Bacikf9593722019-01-02 11:11:01 +09002219 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002220 }
2221
Andi Kleen207d04b2011-05-24 17:12:29 -07002222 /* Avoid banging the cache line if not needed */
2223 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002224 ra->mmap_miss++;
2225
2226 /*
2227 * Do we miss much more than hit in this file? If so,
2228 * stop bothering with read-ahead. It will only hurt.
2229 */
2230 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacikf9593722019-01-02 11:11:01 +09002231 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002232
Wu Fengguangd30a1102009-06-16 15:31:30 -07002233 /*
2234 * mmap read-around
2235 */
Josef Bacikf9593722019-01-02 11:11:01 +09002236 fpin = maybe_unlock_mmap_for_io(vma, flags, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002237 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2238 ra->size = ra->ra_pages;
2239 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002240 ra_submit(ra, mapping, file);
Josef Bacikf9593722019-01-02 11:11:01 +09002241 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002242}
2243
2244/*
2245 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacikf9593722019-01-02 11:11:01 +09002246 * so we want to possibly extend the readahead further. We return the file that
2247 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002248 */
Josef Bacikf9593722019-01-02 11:11:01 +09002249static struct file *do_async_mmap_readahead(struct vm_area_struct *vma,
2250 unsigned long flags,
Linus Torvaldsef00e082009-06-16 15:31:25 -07002251 struct file_ra_state *ra,
2252 struct file *file,
2253 struct page *page,
2254 pgoff_t offset)
2255{
2256 struct address_space *mapping = file->f_mapping;
Josef Bacikf9593722019-01-02 11:11:01 +09002257 struct file *fpin = NULL;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002258
2259 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002260 if (vma->vm_flags & VM_RAND_READ)
Josef Bacikf9593722019-01-02 11:11:01 +09002261 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002262 if (ra->mmap_miss > 0)
2263 ra->mmap_miss--;
Josef Bacikf9593722019-01-02 11:11:01 +09002264 if (PageReadahead(page)) {
2265 fpin = maybe_unlock_mmap_for_io(vma, flags, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002266 page_cache_async_readahead(mapping, ra, file,
2267 page, offset, ra->ra_pages);
Josef Bacikf9593722019-01-02 11:11:01 +09002268 }
2269 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002270}
2271
Randy Dunlap485bb992006-06-23 02:03:49 -07002272/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002273 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002274 * @vma: vma in which the fault was taken
2275 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002276 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002277 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * mapped memory region to read in file data during a page fault.
2279 *
2280 * The goto's are kind of ugly, but this streamlines the normal case of having
2281 * it in the page cache, and handles the special cases reasonably without
2282 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002283 *
2284 * vma->vm_mm->mmap_sem must be held on entry.
2285 *
2286 * If our return value has VM_FAULT_RETRY set, it's because
2287 * lock_page_or_retry() returned 0.
2288 * The mmap_sem has usually been released in this case.
2289 * See __lock_page_or_retry() for the exception.
2290 *
2291 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2292 * has not been released.
2293 *
2294 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
Nick Piggind0217ac2007-07-19 01:47:03 -07002296int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07002299 struct file *file = vma->vm_file;
Josef Bacikf9593722019-01-02 11:11:01 +09002300 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 struct address_space *mapping = file->f_mapping;
2302 struct file_ra_state *ra = &file->f_ra;
2303 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002304 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct page *page;
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07002306 loff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07002307 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002309 size = round_up(i_size_read(inode), PAGE_SIZE);
2310 if (offset >= size >> PAGE_SHIFT)
Linus Torvalds5307cc12007-10-31 09:19:46 -07002311 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002314 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002316 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002317 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002319 * We found the page, so try async readahead before
2320 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
Josef Bacikf9593722019-01-02 11:11:01 +09002322 fpin = do_async_mmap_readahead(vma, vmf->flags, ra,
2323 file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002324 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002325 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002326 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07002327 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002328 ret = VM_FAULT_MAJOR;
Josef Bacikf9593722019-01-02 11:11:01 +09002329 fpin = do_sync_mmap_readahead(vma, vmf->flags, ra,
2330 file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002331retry_find:
Josef Bacik091c93e2019-01-02 00:21:01 +09002332 page = pagecache_get_page(mapping, offset,
2333 FGP_CREAT|FGP_FOR_MMAP,
2334 vmf->gfp_mask);
Josef Bacikf9593722019-01-02 11:11:01 +09002335 if (!page) {
2336 if (fpin)
2337 goto out_retry;
Josef Bacik091c93e2019-01-02 00:21:01 +09002338 return VM_FAULT_OOM;
Josef Bacikf9593722019-01-02 11:11:01 +09002339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Josef Bacikf9593722019-01-02 11:11:01 +09002341 if (!lock_page_maybe_drop_mmap(vma, vmf->flags, page, &fpin))
2342 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002343
2344 /* Did it get truncated? */
2345 if (unlikely(page->mapping != mapping)) {
2346 unlock_page(page);
2347 put_page(page);
2348 goto retry_find;
2349 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002350 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002353 * We have a locked page in the page cache, now we need to check
2354 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 */
Nick Piggind00806b2007-07-19 01:46:57 -07002356 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 goto page_not_uptodate;
2358
Linus Torvaldsef00e082009-06-16 15:31:25 -07002359 /*
Josef Bacikf9593722019-01-02 11:11:01 +09002360 * We've made it this far and we had to drop our mmap_sem, now is the
2361 * time to return to the upper layer and have it re-find the vma and
2362 * redo the fault.
2363 */
2364 if (fpin) {
2365 unlock_page(page);
2366 goto out_retry;
2367 }
2368
2369 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002370 * Found the page and have a reference on it.
2371 * We must recheck i_size under page lock.
2372 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002373 size = round_up(i_size_read(inode), PAGE_SIZE);
2374 if (unlikely(offset >= size >> PAGE_SHIFT)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002375 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002376 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002377 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002378 }
2379
Nick Piggind0217ac2007-07-19 01:47:03 -07002380 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002381 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 /*
2385 * Umm, take care of errors if the page isn't up-to-date.
2386 * Try to re-read it _once_. We do this synchronously,
2387 * because there really aren't any performance issues here
2388 * and we need to check for errors.
2389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ClearPageError(page);
Josef Bacikf9593722019-01-02 11:11:01 +09002391 fpin = maybe_unlock_mmap_for_io(vma, vmf->flags, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002392 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002393 if (!error) {
2394 wait_on_page_locked(page);
2395 if (!PageUptodate(page))
2396 error = -EIO;
2397 }
Josef Bacikf9593722019-01-02 11:11:01 +09002398 if (fpin)
2399 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002400 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002401
2402 if (!error || error == AOP_TRUNCATED_PAGE)
2403 goto retry_find;
2404
2405 /* Things didn't work out. Return zero to tell the mm layer so. */
2406 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002407 return VM_FAULT_SIGBUS;
Josef Bacikf9593722019-01-02 11:11:01 +09002408
2409out_retry:
2410 /*
2411 * We dropped the mmap_sem, we need to return to the fault handler to
2412 * re-find the vma and come back and find our hopefully still populated
2413 * page.
2414 */
2415 if (page)
2416 put_page(page);
2417 if (fpin)
2418 fput(fpin);
2419 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002420}
2421EXPORT_SYMBOL(filemap_fault);
2422
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002423void filemap_map_pages(struct fault_env *fe,
2424 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002425{
2426 struct radix_tree_iter iter;
2427 void **slot;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002428 struct file *file = fe->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002429 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002430 pgoff_t last_pgoff = start_pgoff;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002431 loff_t size;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002432 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002433
2434 rcu_read_lock();
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002435 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
2436 start_pgoff) {
2437 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002438 break;
2439repeat:
2440 page = radix_tree_deref_slot(slot);
2441 if (unlikely(!page))
2442 goto next;
2443 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002444 if (radix_tree_deref_retry(page)) {
2445 slot = radix_tree_iter_retry(&iter);
2446 continue;
2447 }
2448 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002449 }
2450
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002451 head = compound_head(page);
2452 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002453 goto repeat;
2454
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002455 /* The page was split under us? */
2456 if (compound_head(page) != head) {
2457 put_page(head);
2458 goto repeat;
2459 }
2460
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002461 /* Has the page moved? */
2462 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002463 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002464 goto repeat;
2465 }
2466
2467 if (!PageUptodate(page) ||
2468 PageReadahead(page) ||
2469 PageHWPoison(page))
2470 goto skip;
2471 if (!trylock_page(page))
2472 goto skip;
2473
2474 if (page->mapping != mapping || !PageUptodate(page))
2475 goto unlock;
2476
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002477 size = round_up(i_size_read(mapping->host), PAGE_SIZE);
2478 if (page->index >= size >> PAGE_SHIFT)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002479 goto unlock;
2480
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002481 if (file->f_ra.mmap_miss > 0)
2482 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002483
2484 fe->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2485 if (fe->pte)
2486 fe->pte += iter.index - last_pgoff;
2487 last_pgoff = iter.index;
2488 if (alloc_set_pte(fe, NULL, page))
2489 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002490 unlock_page(page);
2491 goto next;
2492unlock:
2493 unlock_page(page);
2494skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002495 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002496next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002497 /* Huge page is mapped? No need to proceed. */
2498 if (pmd_trans_huge(*fe->pmd))
2499 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002500 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002501 break;
2502 }
2503 rcu_read_unlock();
2504}
2505EXPORT_SYMBOL(filemap_map_pages);
2506
Jan Kara4fcf1c62012-06-12 16:20:29 +02002507int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2508{
2509 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002510 struct inode *inode = file_inode(vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002511 int ret = VM_FAULT_LOCKED;
2512
Jan Kara14da9202012-06-12 16:20:37 +02002513 sb_start_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002514 file_update_time(vma->vm_file);
2515 lock_page(page);
2516 if (page->mapping != inode->i_mapping) {
2517 unlock_page(page);
2518 ret = VM_FAULT_NOPAGE;
2519 goto out;
2520 }
Jan Kara14da9202012-06-12 16:20:37 +02002521 /*
2522 * We mark the page dirty already here so that when freeze is in
2523 * progress, we are guaranteed that writeback during freezing will
2524 * see the dirty page and writeprotect it again.
2525 */
2526 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002527 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002528out:
Jan Kara14da9202012-06-12 16:20:37 +02002529 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002530 return ret;
2531}
2532EXPORT_SYMBOL(filemap_page_mkwrite);
2533
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002534const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002535 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002536 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002537 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538};
2539
2540/* This is used for a general mmap of a disk file */
2541
2542int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2543{
2544 struct address_space *mapping = file->f_mapping;
2545
2546 if (!mapping->a_ops->readpage)
2547 return -ENOEXEC;
2548 file_accessed(file);
2549 vma->vm_ops = &generic_file_vm_ops;
2550 return 0;
2551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553/*
2554 * This is for filesystems which do not implement ->writepage.
2555 */
2556int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2557{
2558 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2559 return -EINVAL;
2560 return generic_file_mmap(file, vma);
2561}
2562#else
2563int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2564{
2565 return -ENOSYS;
2566}
2567int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2568{
2569 return -ENOSYS;
2570}
2571#endif /* CONFIG_MMU */
2572
2573EXPORT_SYMBOL(generic_file_mmap);
2574EXPORT_SYMBOL(generic_file_readonly_mmap);
2575
Sasha Levin67f9fd92014-04-03 14:48:18 -07002576static struct page *wait_on_page_read(struct page *page)
2577{
2578 if (!IS_ERR(page)) {
2579 wait_on_page_locked(page);
2580 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002581 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002582 page = ERR_PTR(-EIO);
2583 }
2584 }
2585 return page;
2586}
2587
Mel Gorman32b63522016-03-15 14:55:36 -07002588static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002589 pgoff_t index,
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002590 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002591 void *data,
2592 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
Nick Piggineb2be182007-10-16 01:24:57 -07002594 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 int err;
2596repeat:
2597 page = find_get_page(mapping, index);
2598 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002599 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002600 if (!page)
2601 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002602 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002603 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002604 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002605 if (err == -EEXIST)
2606 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return ERR_PTR(err);
2609 }
Mel Gorman32b63522016-03-15 14:55:36 -07002610
2611filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 err = filler(data, page);
2613 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002614 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002615 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
Mel Gorman32b63522016-03-15 14:55:36 -07002617
2618 page = wait_on_page_read(page);
2619 if (IS_ERR(page))
2620 return page;
2621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (PageUptodate(page))
2624 goto out;
2625
Mel Gormanebded022016-03-15 14:55:39 -07002626 /*
2627 * Page is not up to date and may be locked due one of the following
2628 * case a: Page is being filled and the page lock is held
2629 * case b: Read/write error clearing the page uptodate status
2630 * case c: Truncation in progress (page locked)
2631 * case d: Reclaim in progress
2632 *
2633 * Case a, the page will be up to date when the page is unlocked.
2634 * There is no need to serialise on the page lock here as the page
2635 * is pinned so the lock gives no additional protection. Even if the
2636 * the page is truncated, the data is still valid if PageUptodate as
2637 * it's a race vs truncate race.
2638 * Case b, the page will not be up to date
2639 * Case c, the page may be truncated but in itself, the data may still
2640 * be valid after IO completes as it's a read vs truncate race. The
2641 * operation must restart if the page is not uptodate on unlock but
2642 * otherwise serialising on page lock to stabilise the mapping gives
2643 * no additional guarantees to the caller as the page lock is
2644 * released before return.
2645 * Case d, similar to truncation. If reclaim holds the page lock, it
2646 * will be a race with remove_mapping that determines if the mapping
2647 * is valid on unlock but otherwise the data is valid and there is
2648 * no need to serialise with page lock.
2649 *
2650 * As the page lock gives no additional guarantee, we optimistically
2651 * wait on the page to be unlocked and check if it's up to date and
2652 * use the page if it is. Otherwise, the page lock is required to
2653 * distinguish between the different cases. The motivation is that we
2654 * avoid spurious serialisations and wakeups when multiple processes
2655 * wait on the same page for IO to complete.
2656 */
2657 wait_on_page_locked(page);
2658 if (PageUptodate(page))
2659 goto out;
2660
2661 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002663
2664 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (!page->mapping) {
2666 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002667 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002668 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
Mel Gormanebded022016-03-15 14:55:39 -07002670
2671 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (PageUptodate(page)) {
2673 unlock_page(page);
2674 goto out;
2675 }
Mel Gorman32b63522016-03-15 14:55:36 -07002676 goto filler;
2677
David Howellsc855ff32007-05-09 13:42:20 +01002678out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002679 mark_page_accessed(page);
2680 return page;
2681}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002682
2683/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002684 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002685 * @mapping: the page's address_space
2686 * @index: the page index
2687 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002688 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002689 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002690 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002691 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002692 *
2693 * If the page does not get brought uptodate, return -EIO.
2694 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002695struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002696 pgoff_t index,
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002697 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002698 void *data)
2699{
2700 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2701}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002702EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002703
2704/**
2705 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2706 * @mapping: the page's address_space
2707 * @index: the page index
2708 * @gfp: the page allocator flags to use if allocating
2709 *
2710 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002711 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002712 *
2713 * If the page does not get brought uptodate, return -EIO.
2714 */
2715struct page *read_cache_page_gfp(struct address_space *mapping,
2716 pgoff_t index,
2717 gfp_t gfp)
2718{
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002719 filler_t *filler = mapping->a_ops->readpage;
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002720
Sasha Levin67f9fd92014-04-03 14:48:18 -07002721 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002722}
2723EXPORT_SYMBOL(read_cache_page_gfp);
2724
Nick Piggin2f718ff2007-10-16 01:24:59 -07002725/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 * Performs necessary checks before doing a write
2727 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002728 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 * Returns appropriate error code that caller should return or
2730 * zero in case that write should be allowed.
2731 */
Al Viro3309dd02015-04-09 12:55:47 -04002732inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Al Viro3309dd02015-04-09 12:55:47 -04002734 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002736 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002737 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Al Viro3309dd02015-04-09 12:55:47 -04002739 if (!iov_iter_count(from))
2740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Al Viro0fa6b002015-04-04 04:05:48 -04002742 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002743 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002744 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Al Viro3309dd02015-04-09 12:55:47 -04002746 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Al Viro0fa6b002015-04-04 04:05:48 -04002748 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002749 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002750 send_sig(SIGXFSZ, current, 0);
2751 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Al Viro3309dd02015-04-09 12:55:47 -04002753 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
2755
2756 /*
2757 * LFS rule
2758 */
Al Viro3309dd02015-04-09 12:55:47 -04002759 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002761 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002763 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765
2766 /*
2767 * Are we about to exceed the fs block limit ?
2768 *
2769 * If we have written data it becomes a short write. If we have
2770 * exceeded without writing data we send a signal and return EFBIG.
2771 * Linus frestrict idea will clean these up nicely..
2772 */
Al Viro3309dd02015-04-09 12:55:47 -04002773 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2774 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Al Viro3309dd02015-04-09 12:55:47 -04002776 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2777 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779EXPORT_SYMBOL(generic_write_checks);
2780
Nick Pigginafddba42007-10-16 01:25:01 -07002781int pagecache_write_begin(struct file *file, struct address_space *mapping,
2782 loff_t pos, unsigned len, unsigned flags,
2783 struct page **pagep, void **fsdata)
2784{
2785 const struct address_space_operations *aops = mapping->a_ops;
2786
Nick Piggin4e02ed42008-10-29 14:00:55 -07002787 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002788 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002789}
2790EXPORT_SYMBOL(pagecache_write_begin);
2791
2792int pagecache_write_end(struct file *file, struct address_space *mapping,
2793 loff_t pos, unsigned len, unsigned copied,
2794 struct page *page, void *fsdata)
2795{
2796 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002797
Nick Piggin4e02ed42008-10-29 14:00:55 -07002798 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002799}
2800EXPORT_SYMBOL(pagecache_write_end);
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002803generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 struct file *file = iocb->ki_filp;
2806 struct address_space *mapping = file->f_mapping;
2807 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002808 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002810 size_t write_len;
2811 pgoff_t end;
Al Viro26978b82014-03-10 14:08:45 -04002812 struct iov_iter data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Al Viro0c949332014-03-22 06:51:37 -04002814 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002815 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002816
Nick Piggin48b47c52009-01-06 14:40:22 -08002817 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002818 if (written)
2819 goto out;
2820
2821 /*
2822 * After a write we want buffered reads to be sure to go to disk to get
2823 * the new data. We invalidate clean cached page from the region we're
2824 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002825 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002826 */
2827 if (mapping->nrpages) {
2828 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002829 pos >> PAGE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002830 /*
2831 * If a page can not be invalidated, return 0 to fall back
2832 * to buffered write.
2833 */
2834 if (written) {
2835 if (written == -EBUSY)
2836 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002837 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002838 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002839 }
2840
Al Viro26978b82014-03-10 14:08:45 -04002841 data = *from;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002842 written = mapping->a_ops->direct_IO(iocb, &data);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002843
2844 /*
2845 * Finally, try again to invalidate clean pages which might have been
2846 * cached by non-direct readahead, or faulted in by get_user_pages()
2847 * if the source of the write was an mmap'ed region of the file
2848 * we're writing. Either one is a pretty crazy thing to do,
2849 * so we don't support it 100%. If this invalidation
2850 * fails, tough, the write still worked...
2851 */
2852 if (mapping->nrpages) {
2853 invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002854 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002855 }
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002858 pos += written;
Al Virof8579f82014-03-03 22:03:20 -05002859 iov_iter_advance(from, written);
Namhyung Kim01166512010-10-26 14:21:58 -07002860 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2861 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 mark_inode_dirty(inode);
2863 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002864 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002866out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return written;
2868}
2869EXPORT_SYMBOL(generic_file_direct_write);
2870
Nick Piggineb2be182007-10-16 01:24:57 -07002871/*
2872 * Find or create a page at the given pagecache position. Return the locked
2873 * page. This function is specifically for buffered writes.
2874 */
Nick Piggin54566b22009-01-04 12:00:53 -08002875struct page *grab_cache_page_write_begin(struct address_space *mapping,
2876 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002877{
Nick Piggineb2be182007-10-16 01:24:57 -07002878 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07002879 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002880
Nick Piggin54566b22009-01-04 12:00:53 -08002881 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002882 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002883
Mel Gorman2457aec2014-06-04 16:10:31 -07002884 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002885 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002886 if (page)
2887 wait_for_stable_page(page);
2888
Nick Piggineb2be182007-10-16 01:24:57 -07002889 return page;
2890}
Nick Piggin54566b22009-01-04 12:00:53 -08002891EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002892
Al Viro3b93f912014-02-11 21:34:08 -05002893ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07002894 struct iov_iter *i, loff_t pos)
2895{
2896 struct address_space *mapping = file->f_mapping;
2897 const struct address_space_operations *a_ops = mapping->a_ops;
2898 long status = 0;
2899 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002900 unsigned int flags = 0;
2901
2902 /*
2903 * Copies from kernel address space cannot fail (NFSD is a big user).
2904 */
Al Viro777eda22014-12-17 04:46:46 -05002905 if (!iter_is_iovec(i))
Nick Piggin674b8922007-10-16 01:25:03 -07002906 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002907
2908 do {
2909 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002910 unsigned long offset; /* Offset into pagecache page */
2911 unsigned long bytes; /* Bytes to write to page */
2912 size_t copied; /* Bytes copied from user */
2913 void *fsdata;
2914
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002915 offset = (pos & (PAGE_SIZE - 1));
2916 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002917 iov_iter_count(i));
2918
2919again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01002920 /*
2921 * Bring in the user page that we will copy from _first_.
2922 * Otherwise there's a nasty deadlock on copying from the
2923 * same page as we're writing to, without it being marked
2924 * up-to-date.
2925 *
2926 * Not only is this an optimisation, but it is also required
2927 * to check that the address is actually valid, when atomic
2928 * usercopies are used, below.
2929 */
2930 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2931 status = -EFAULT;
2932 break;
2933 }
2934
Jan Kara296291c2015-10-22 13:32:21 -07002935 if (fatal_signal_pending(current)) {
2936 status = -EINTR;
2937 break;
2938 }
2939
Nick Piggin674b8922007-10-16 01:25:03 -07002940 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002941 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07002942 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07002943 break;
2944
anfei zhou931e80e2010-02-02 13:44:02 -08002945 if (mapping_writably_mapped(mapping))
2946 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01002947
Nick Pigginafddba42007-10-16 01:25:01 -07002948 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07002949 flush_dcache_page(page);
2950
2951 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2952 page, fsdata);
2953 if (unlikely(status < 0))
2954 break;
2955 copied = status;
2956
2957 cond_resched();
2958
Nick Piggin124d3b72008-02-02 15:01:17 +01002959 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002960 if (unlikely(copied == 0)) {
2961 /*
2962 * If we were unable to copy any data at all, we must
2963 * fall back to a single segment length write.
2964 *
2965 * If we didn't fallback here, we could livelock
2966 * because not all segments in the iov can be copied at
2967 * once without a pagefault.
2968 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002969 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002970 iov_iter_single_seg_count(i));
2971 goto again;
2972 }
Nick Pigginafddba42007-10-16 01:25:01 -07002973 pos += copied;
2974 written += copied;
2975
2976 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07002977 } while (iov_iter_count(i));
2978
2979 return written ? written : status;
2980}
Al Viro3b93f912014-02-11 21:34:08 -05002981EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Jan Karae4dd9de2009-08-17 18:10:06 +02002983/**
Al Viro81742022014-04-03 03:17:43 -04002984 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002985 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04002986 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002987 *
2988 * This function does all the work needed for actually writing data to a
2989 * file. It does all basic checks, removes SUID from the file, updates
2990 * modification times and calls proper subroutines depending on whether we
2991 * do direct IO or a standard buffered write.
2992 *
2993 * It expects i_mutex to be grabbed unless we work on a block device or similar
2994 * object which does not need locking at all.
2995 *
2996 * This function does *not* take care of syncing data in case of O_SYNC write.
2997 * A caller has to handle it. This is mainly due to the fact that we want to
2998 * avoid syncing under i_mutex.
2999 */
Al Viro81742022014-04-03 03:17:43 -04003000ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
3002 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003003 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003005 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003007 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003010 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003011 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (err)
3013 goto out;
3014
Josef Bacikc3b2da32012-03-26 09:59:21 -04003015 err = file_update_time(file);
3016 if (err)
3017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Al Viro2ba48ce2015-04-09 13:52:01 -04003019 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003020 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003021
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003022 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003023 /*
3024 * If the write stopped short of completing, fall back to
3025 * buffered writes. Some filesystems do this for writes to
3026 * holes, for example. For DAX files, a buffered write will
3027 * not succeed (even if it did, DAX does not handle dirty
3028 * page-cache pages correctly).
3029 */
Al Viro0b8def92015-04-07 10:22:53 -04003030 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003032
Al Viro0b8def92015-04-07 10:22:53 -04003033 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003034 /*
Al Viro3b93f912014-02-11 21:34:08 -05003035 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003036 * then we want to return the number of bytes which were
3037 * direct-written, or the error code if that was zero. Note
3038 * that this differs from normal direct-io semantics, which
3039 * will return -EFOO even if some bytes were written.
3040 */
Al Viro60bb4522014-08-08 12:39:16 -04003041 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003042 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003043 goto out;
3044 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003045 /*
3046 * We need to ensure that the page cache pages are written to
3047 * disk and invalidated to preserve the expected O_DIRECT
3048 * semantics.
3049 */
Al Viro3b93f912014-02-11 21:34:08 -05003050 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003051 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003052 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003053 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003054 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003055 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003056 pos >> PAGE_SHIFT,
3057 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003058 } else {
3059 /*
3060 * We don't know how much we wrote, so just return
3061 * the number of bytes which were direct-written
3062 */
3063 }
3064 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003065 written = generic_perform_write(file, from, iocb->ki_pos);
3066 if (likely(written > 0))
3067 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069out:
3070 current->backing_dev_info = NULL;
3071 return written ? written : err;
3072}
Al Viro81742022014-04-03 03:17:43 -04003073EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Jan Karae4dd9de2009-08-17 18:10:06 +02003075/**
Al Viro81742022014-04-03 03:17:43 -04003076 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003077 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003078 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003079 *
Al Viro81742022014-04-03 03:17:43 -04003080 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003081 * filesystems. It takes care of syncing the file in case of O_SYNC file
3082 * and acquires i_mutex as needed.
3083 */
Al Viro81742022014-04-03 03:17:43 -04003084ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003087 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Al Viro59551022016-01-22 15:40:57 -05003090 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003091 ret = generic_write_checks(iocb, from);
3092 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003093 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003094 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Christoph Hellwige2592212016-04-07 08:52:01 -07003096 if (ret > 0)
3097 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return ret;
3099}
Al Viro81742022014-04-03 03:17:43 -04003100EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
David Howellscf9a2ae2006-08-29 19:05:54 +01003102/**
3103 * try_to_release_page() - release old fs-specific metadata on a page
3104 *
3105 * @page: the page which the kernel is trying to free
3106 * @gfp_mask: memory allocation flags (and I/O mode)
3107 *
3108 * The address_space is to try to release any data against the page
3109 * (presumably at page->private). If the release was successful, return `1'.
3110 * Otherwise return zero.
3111 *
David Howells266cf652009-04-03 16:42:36 +01003112 * This may also be called if PG_fscache is set on a page, indicating that the
3113 * page is known to the local caching routines.
3114 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003115 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003116 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003117 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003118 */
3119int try_to_release_page(struct page *page, gfp_t gfp_mask)
3120{
3121 struct address_space * const mapping = page->mapping;
3122
3123 BUG_ON(!PageLocked(page));
3124 if (PageWriteback(page))
3125 return 0;
3126
3127 if (mapping && mapping->a_ops->releasepage)
3128 return mapping->a_ops->releasepage(page, gfp_mask);
3129 return try_to_free_buffers(page);
3130}
3131
3132EXPORT_SYMBOL(try_to_release_page);