blob: 01a0d6a5358eefa72f24b30dd6bcfa7fbedea6ed [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
388 if (!mapping_cap_writeback_dirty(mapping))
389 return 0;
390
Tejun Heob16b1de2015-06-02 08:39:48 -0600391 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600393 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return ret;
395}
396
397static inline int __filemap_fdatawrite(struct address_space *mapping,
398 int sync_mode)
399{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700400 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403int filemap_fdatawrite(struct address_space *mapping)
404{
405 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
406}
407EXPORT_SYMBOL(filemap_fdatawrite);
408
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400409int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800410 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
413}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400414EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Randy Dunlap485bb992006-06-23 02:03:49 -0700416/**
417 * filemap_flush - mostly a non-blocking flush
418 * @mapping: target address_space
419 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * This is a mostly non-blocking flush. Not suitable for data-integrity
421 * purposes - I/O may not be started against all dirty pages.
422 */
423int filemap_flush(struct address_space *mapping)
424{
425 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
426}
427EXPORT_SYMBOL(filemap_flush);
428
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800429static int __filemap_fdatawait_range(struct address_space *mapping,
430 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300432 pgoff_t index = start_byte >> PAGE_SHIFT;
433 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct pagevec pvec;
435 int nr_pages;
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800436 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200438 if (end_byte < start_byte)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 pagevec_init(&pvec, 0);
Jan Kara7e95dd52017-11-15 17:35:05 -0800442 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned i;
444
Jan Kara7e95dd52017-11-15 17:35:05 -0800445 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara4d93a432017-11-15 17:35:19 -0800446 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara7e95dd52017-11-15 17:35:05 -0800447 if (!nr_pages)
448 break;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 for (i = 0; i < nr_pages; i++) {
451 struct page *page = pvec.pages[i];
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800454 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ret = -EIO;
456 }
457 pagevec_release(&pvec);
458 cond_resched();
459 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700460out:
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800461 return ret;
462}
463
464/**
465 * filemap_fdatawait_range - wait for writeback to complete
466 * @mapping: address space structure to wait for
467 * @start_byte: offset in bytes where the range starts
468 * @end_byte: offset in bytes where the range ends (inclusive)
469 *
470 * Walk the list of under-writeback pages of the given address space
471 * in the given range and wait for all of them. Check error status of
472 * the address space and return it.
473 *
474 * Since the error status of the address space is cleared by this function,
475 * callers are responsible for checking the return value and handling and/or
476 * reporting the error.
477 */
478int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
479 loff_t end_byte)
480{
481 int ret, ret2;
482
483 ret = __filemap_fdatawait_range(mapping, start_byte, end_byte);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700484 ret2 = filemap_check_errors(mapping);
485 if (!ret)
486 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 return ret;
489}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200490EXPORT_SYMBOL(filemap_fdatawait_range);
491
492/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800493 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
494 * @mapping: address space structure to wait for
495 *
496 * Walk the list of under-writeback pages of the given address space
497 * and wait for all of them. Unlike filemap_fdatawait(), this function
498 * does not clear error status of the address space.
499 *
500 * Use this function if callers don't handle errors themselves. Expected
501 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
502 * fsfreeze(8)
503 */
504void filemap_fdatawait_keep_errors(struct address_space *mapping)
505{
506 loff_t i_size = i_size_read(mapping->host);
507
508 if (i_size == 0)
509 return;
510
511 __filemap_fdatawait_range(mapping, 0, i_size - 1);
512}
513
514/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700515 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700517 *
518 * Walk the list of under-writeback pages of the given address space
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800519 * and wait for all of them. Check error status of the address space
520 * and return it.
521 *
522 * Since the error status of the address space is cleared by this function,
523 * callers are responsible for checking the return value and handling and/or
524 * reporting the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526int filemap_fdatawait(struct address_space *mapping)
527{
528 loff_t i_size = i_size_read(mapping->host);
529
530 if (i_size == 0)
531 return 0;
532
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200533 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535EXPORT_SYMBOL(filemap_fdatawait);
536
537int filemap_write_and_wait(struct address_space *mapping)
538{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800539 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800541 if ((!dax_mapping(mapping) && mapping->nrpages) ||
542 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800543 err = filemap_fdatawrite(mapping);
544 /*
545 * Even if the above returned error, the pages may be
546 * written partially (e.g. -ENOSPC), so we wait for it.
547 * But the -EIO is special case, it may indicate the worst
548 * thing (e.g. bug) happened, so we avoid waiting for it.
549 */
550 if (err != -EIO) {
551 int err2 = filemap_fdatawait(mapping);
552 if (!err)
553 err = err2;
554 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700555 } else {
556 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800560EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Randy Dunlap485bb992006-06-23 02:03:49 -0700562/**
563 * filemap_write_and_wait_range - write out & wait on a file range
564 * @mapping: the address_space for the pages
565 * @lstart: offset in bytes where the range starts
566 * @lend: offset in bytes where the range ends (inclusive)
567 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800568 * Write out and wait upon file offsets lstart->lend, inclusive.
569 *
570 * Note that `lend' is inclusive (describes the last byte to be written) so
571 * that this function can be used to write to the very end-of-file (end = -1).
572 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573int filemap_write_and_wait_range(struct address_space *mapping,
574 loff_t lstart, loff_t lend)
575{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800576 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800578 if ((!dax_mapping(mapping) && mapping->nrpages) ||
579 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800580 err = __filemap_fdatawrite_range(mapping, lstart, lend,
581 WB_SYNC_ALL);
582 /* See comment of filemap_write_and_wait() */
583 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200584 int err2 = filemap_fdatawait_range(mapping,
585 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800586 if (!err)
587 err = err2;
588 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700589 } else {
590 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800592 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
Chris Masonf6995582009-04-15 13:22:37 -0400594EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Randy Dunlap485bb992006-06-23 02:03:49 -0700596/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700597 * replace_page_cache_page - replace a pagecache page with a new one
598 * @old: page to be replaced
599 * @new: page to replace with
600 * @gfp_mask: allocation mode
601 *
602 * This function replaces a page in the pagecache with a new one. On
603 * success it acquires the pagecache reference for the new page and
604 * drops it for the old page. Both the old and new pages must be
605 * locked. This function does not add the new page to the LRU, the
606 * caller must do that.
607 *
608 * The remove + add is atomic. The only way this function can fail is
609 * memory allocation failure.
610 */
611int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
612{
613 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700614
Sasha Levin309381fea2014-01-23 15:52:54 -0800615 VM_BUG_ON_PAGE(!PageLocked(old), old);
616 VM_BUG_ON_PAGE(!PageLocked(new), new);
617 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700618
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -0700619 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700620 if (!error) {
621 struct address_space *mapping = old->mapping;
622 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400623 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700624
625 pgoff_t offset = old->index;
626 freepage = mapping->a_ops->freepage;
627
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300628 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700629 new->mapping = mapping;
630 new->index = offset;
631
Greg Thelenc4843a72015-05-22 17:13:16 -0400632 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700633 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700634 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700635 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700636
637 /*
638 * hugetlb pages do not participate in page cache accounting.
639 */
640 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700641 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700642 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700643 __inc_node_page_state(new, NR_SHMEM);
Greg Thelenc4843a72015-05-22 17:13:16 -0400644 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700645 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700646 radix_tree_preload_end();
647 if (freepage)
648 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300649 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700650 }
651
652 return error;
653}
654EXPORT_SYMBOL_GPL(replace_page_cache_page);
655
Johannes Weinera5289102014-04-03 14:47:51 -0700656static int __add_to_page_cache_locked(struct page *page,
657 struct address_space *mapping,
658 pgoff_t offset, gfp_t gfp_mask,
659 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Johannes Weiner00501b52014-08-08 14:19:20 -0700661 int huge = PageHuge(page);
662 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700663 int error;
664
Sasha Levin309381fea2014-01-23 15:52:54 -0800665 VM_BUG_ON_PAGE(!PageLocked(page), page);
666 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700667
Johannes Weiner00501b52014-08-08 14:19:20 -0700668 if (!huge) {
669 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800670 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700671 if (error)
672 return error;
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -0700675 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700676 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700677 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800678 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700679 return error;
680 }
681
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300682 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700683 page->mapping = mapping;
684 page->index = offset;
685
686 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700687 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700688 radix_tree_preload_end();
689 if (unlikely(error))
690 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700691
692 /* hugetlb pages do not participate in page cache accounting. */
693 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700694 __inc_node_page_state(page, NR_FILE_PAGES);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700695 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700696 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800697 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700698 trace_mm_filemap_add_to_page_cache(page);
699 return 0;
700err_insert:
701 page->mapping = NULL;
702 /* Leave page->index set: truncation relies upon it */
703 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700704 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800705 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return error;
708}
Johannes Weinera5289102014-04-03 14:47:51 -0700709
710/**
711 * add_to_page_cache_locked - add a locked page to the pagecache
712 * @page: page to add
713 * @mapping: the page's address_space
714 * @offset: page index
715 * @gfp_mask: page allocation mode
716 *
717 * This function is used to add a page to the pagecache. It must be locked.
718 * This function does not add the page to the LRU. The caller must do that.
719 */
720int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
721 pgoff_t offset, gfp_t gfp_mask)
722{
723 return __add_to_page_cache_locked(page, mapping, offset,
724 gfp_mask, NULL);
725}
Nick Piggine2867812008-07-25 19:45:30 -0700726EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400729 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Johannes Weinera5289102014-04-03 14:47:51 -0700731 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700732 int ret;
733
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800734 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700735 ret = __add_to_page_cache_locked(page, mapping, offset,
736 gfp_mask, &shadow);
737 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800738 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700739 else {
740 /*
741 * The page might have been evicted from cache only
742 * recently, in which case it should be activated like
743 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700744 * The exception is pages getting rewritten; evicting other
745 * data from the working set, only to cache data that will
746 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700747 */
Johannes Weinera2383322018-10-26 15:06:04 -0700748 WARN_ON_ONCE(PageActive(page));
749 if (!(gfp_mask & __GFP_WRITE) && shadow)
750 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700751 lru_cache_add(page);
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return ret;
754}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300755EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Paul Jackson44110fe2006-03-24 03:16:04 -0800757#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700758struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800759{
Miao Xiec0ff7452010-05-24 14:32:08 -0700760 int n;
761 struct page *page;
762
Paul Jackson44110fe2006-03-24 03:16:04 -0800763 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700764 unsigned int cpuset_mems_cookie;
765 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700766 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700767 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700768 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700769 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700770
Miao Xiec0ff7452010-05-24 14:32:08 -0700771 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800772 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700773 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800774}
Nick Piggin2ae88142006-10-28 10:38:23 -0700775EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800776#endif
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * In order to wait for pages to become available there must be
780 * waitqueues associated with pages. By using a hash table of
781 * waitqueues where the bucket discipline is to maintain all
782 * waiters on the same queue and wake all when any of the pages
783 * become available, and for the woken contexts to check to be
784 * sure the appropriate page became available, this saves space
785 * at a cost of "thundering herd" phenomena during rare hash
786 * collisions.
787 */
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000788#define PAGE_WAIT_TABLE_BITS 8
789#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
790static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
791
792static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000794 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000796
797void __init pagecache_init(void)
798{
799 int i;
800
801 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
802 init_waitqueue_head(&page_wait_table[i]);
803
804 page_writeback_init();
805}
806
807struct wait_page_key {
808 struct page *page;
809 int bit_nr;
810 int page_match;
811};
812
813struct wait_page_queue {
814 struct page *page;
815 int bit_nr;
816 wait_queue_t wait;
817};
818
819static int wake_page_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
820{
821 struct wait_page_key *key = arg;
822 struct wait_page_queue *wait_page
823 = container_of(wait, struct wait_page_queue, wait);
824
825 if (wait_page->page != key->page)
826 return 0;
827 key->page_match = 1;
828
829 if (wait_page->bit_nr != key->bit_nr)
830 return 0;
831 if (test_bit(key->bit_nr, &key->page->flags))
832 return 0;
833
834 return autoremove_wake_function(wait, mode, sync, key);
835}
836
837void wake_up_page_bit(struct page *page, int bit_nr)
838{
839 wait_queue_head_t *q = page_waitqueue(page);
840 struct wait_page_key key;
841 unsigned long flags;
842
843 key.page = page;
844 key.bit_nr = bit_nr;
845 key.page_match = 0;
846
847 spin_lock_irqsave(&q->lock, flags);
848 __wake_up_locked_key(q, TASK_NORMAL, &key);
849 /*
850 * It is possible for other pages to have collided on the waitqueue
851 * hash, so in that case check for a page match. That prevents a long-
852 * term waiter
853 *
854 * It is still possible to miss a case here, when we woke page waiters
855 * and removed them from the waitqueue, but there are still other
856 * page waiters.
857 */
858 if (!waitqueue_active(q) || !key.page_match) {
859 ClearPageWaiters(page);
860 /*
861 * It's possible to miss clearing Waiters here, when we woke
862 * our page waiters, but the hashed waitqueue has waiters for
863 * other pages on it.
864 *
865 * That's okay, it's a rare case. The next waker will clear it.
866 */
867 }
868 spin_unlock_irqrestore(&q->lock, flags);
869}
870EXPORT_SYMBOL(wake_up_page_bit);
871
872static inline int wait_on_page_bit_common(wait_queue_head_t *q,
873 struct page *page, int bit_nr, int state, bool lock)
874{
875 struct wait_page_queue wait_page;
876 wait_queue_t *wait = &wait_page.wait;
Johannes Weinerd6687a22018-10-26 15:06:08 -0700877 bool thrashing = false;
Johannes Weiner3df0e592018-10-26 15:06:27 -0700878 unsigned long pflags;
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000879 int ret = 0;
880
Johannes Weiner3df0e592018-10-26 15:06:27 -0700881 if (bit_nr == PG_locked &&
Johannes Weinerd6687a22018-10-26 15:06:08 -0700882 !PageUptodate(page) && PageWorkingset(page)) {
Johannes Weiner3df0e592018-10-26 15:06:27 -0700883 if (!PageSwapBacked(page))
884 delayacct_thrashing_start();
885 psi_memstall_enter(&pflags);
Johannes Weinerd6687a22018-10-26 15:06:08 -0700886 thrashing = true;
887 }
888
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000889 init_wait(wait);
890 wait->func = wake_page_function;
891 wait_page.page = page;
892 wait_page.bit_nr = bit_nr;
893
894 for (;;) {
895 spin_lock_irq(&q->lock);
896
897 if (likely(list_empty(&wait->task_list))) {
898 if (lock)
899 __add_wait_queue_tail_exclusive(q, wait);
900 else
901 __add_wait_queue(q, wait);
902 SetPageWaiters(page);
903 }
904
905 set_current_state(state);
906
907 spin_unlock_irq(&q->lock);
908
909 if (likely(test_bit(bit_nr, &page->flags))) {
910 io_schedule();
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000911 }
912
913 if (lock) {
914 if (!test_and_set_bit_lock(bit_nr, &page->flags))
915 break;
916 } else {
917 if (!test_bit(bit_nr, &page->flags))
918 break;
919 }
Linus Torvalds7394f692017-08-27 16:25:09 -0700920
921 if (unlikely(signal_pending_state(state, current))) {
922 ret = -EINTR;
923 break;
924 }
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000925 }
926
927 finish_wait(q, wait);
928
Johannes Weiner3df0e592018-10-26 15:06:27 -0700929 if (thrashing) {
930 if (!PageSwapBacked(page))
931 delayacct_thrashing_end();
932 psi_memstall_leave(&pflags);
933 }
Johannes Weinerd6687a22018-10-26 15:06:08 -0700934
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000935 /*
936 * A signal could leave PageWaiters set. Clearing it here if
937 * !waitqueue_active would be possible (by open-coding finish_wait),
938 * but still fail to catch it in the case of wait hash collision. We
939 * already can fail to clear wait hash collision cases, so don't
940 * bother with signals either.
941 */
942
943 return ret;
944}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Harvey Harrison920c7a52008-02-04 22:29:26 -0800946void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000948 wait_queue_head_t *q = page_waitqueue(page);
949 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951EXPORT_SYMBOL(wait_on_page_bit);
952
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700953int wait_on_page_bit_killable(struct page *page, int bit_nr)
954{
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000955 wait_queue_head_t *q = page_waitqueue(page);
956 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100960 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700961 * @page: Page defining the wait queue of interest
962 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100963 *
964 * Add an arbitrary @waiter to the wait queue for the nominated @page.
965 */
966void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
967{
968 wait_queue_head_t *q = page_waitqueue(page);
969 unsigned long flags;
970
971 spin_lock_irqsave(&q->lock, flags);
972 __add_wait_queue(q, waiter);
Nicholas Piggin49e19db2016-12-25 13:00:30 +1000973 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +0100974 spin_unlock_irqrestore(&q->lock, flags);
975}
976EXPORT_SYMBOL_GPL(add_page_wait_queue);
977
978/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700979 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * @page: the page
981 *
982 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
983 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +0900984 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
986 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700987 * The mb is necessary to enforce ordering between the clear_bit and the read
988 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800990void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800992 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800993 VM_BUG_ON_PAGE(!PageLocked(page), page);
Nick Piggin8413ac92008-10-18 20:26:59 -0700994 clear_bit_unlock(PG_locked, &page->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100995 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 wake_up_page(page, PG_locked);
997}
998EXPORT_SYMBOL(unlock_page);
999
Randy Dunlap485bb992006-06-23 02:03:49 -07001000/**
1001 * end_page_writeback - end writeback against a page
1002 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
1004void end_page_writeback(struct page *page)
1005{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001006 /*
1007 * TestClearPageReclaim could be used here but it is an atomic
1008 * operation and overkill in this particular case. Failing to
1009 * shuffle a page marked for immediate reclaim is too mild to
1010 * justify taking an atomic operation penalty at the end of
1011 * ever page writeback.
1012 */
1013 if (PageReclaim(page)) {
1014 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001015 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001016 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001017
1018 if (!test_clear_page_writeback(page))
1019 BUG();
1020
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001021 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 wake_up_page(page, PG_writeback);
1023}
1024EXPORT_SYMBOL(end_page_writeback);
1025
Matthew Wilcox57d99842014-06-04 16:07:45 -07001026/*
1027 * After completing I/O on a page, call this routine to update the page
1028 * flags appropriately
1029 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001030void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001031{
Jens Axboec11f0c02016-08-05 08:11:04 -06001032 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001033 if (!err) {
1034 SetPageUptodate(page);
1035 } else {
1036 ClearPageUptodate(page);
1037 SetPageError(page);
1038 }
1039 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001040 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001041 if (err) {
Minchan Kim2c290ee2017-02-24 14:59:59 -08001042 struct address_space *mapping;
1043
Matthew Wilcox57d99842014-06-04 16:07:45 -07001044 SetPageError(page);
Minchan Kim2c290ee2017-02-24 14:59:59 -08001045 mapping = page_mapping(page);
1046 if (mapping)
1047 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001048 }
1049 end_page_writeback(page);
1050 }
1051}
1052EXPORT_SYMBOL_GPL(page_endio);
1053
Randy Dunlap485bb992006-06-23 02:03:49 -07001054/**
1055 * __lock_page - get a lock on the page, assuming we need to sleep to get it
1056 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001058void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001060 struct page *page = compound_head(__page);
1061 wait_queue_head_t *q = page_waitqueue(page);
1062 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064EXPORT_SYMBOL(__lock_page);
1065
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001066int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001067{
Nicholas Piggin49e19db2016-12-25 13:00:30 +10001068 struct page *page = compound_head(__page);
1069 wait_queue_head_t *q = page_waitqueue(page);
1070 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001071}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001072EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001073
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001074/*
1075 * Return values:
1076 * 1 - page is locked; mmap_sem is still held.
1077 * 0 - page is not locked.
1078 * mmap_sem has been released (up_read()), unless flags had both
1079 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1080 * which case mmap_sem is still held.
1081 *
1082 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1083 * with the page locked and the mmap_sem unperturbed.
1084 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001085int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1086 unsigned int flags)
1087{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001088 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1089 /*
1090 * CAUTION! In this case, mmap_sem is not released
1091 * even though return 0.
1092 */
1093 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1094 return 0;
1095
1096 up_read(&mm->mmap_sem);
1097 if (flags & FAULT_FLAG_KILLABLE)
1098 wait_on_page_locked_killable(page);
1099 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001100 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001101 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001102 } else {
1103 if (flags & FAULT_FLAG_KILLABLE) {
1104 int ret;
1105
1106 ret = __lock_page_killable(page);
1107 if (ret) {
1108 up_read(&mm->mmap_sem);
1109 return 0;
1110 }
1111 } else
1112 __lock_page(page);
1113 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001114 }
1115}
1116
Randy Dunlap485bb992006-06-23 02:03:49 -07001117/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001118 * page_cache_next_hole - find the next hole (not-present entry)
1119 * @mapping: mapping
1120 * @index: index
1121 * @max_scan: maximum range to search
1122 *
1123 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1124 * lowest indexed hole.
1125 *
1126 * Returns: the index of the hole if found, otherwise returns an index
1127 * outside of the set specified (in which case 'return - index >=
1128 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1129 * be returned.
1130 *
1131 * page_cache_next_hole may be called under rcu_read_lock. However,
1132 * like radix_tree_gang_lookup, this will not atomically search a
1133 * snapshot of the tree at a single point in time. For example, if a
1134 * hole is created at index 5, then subsequently a hole is created at
1135 * index 10, page_cache_next_hole covering both indexes may return 10
1136 * if called under rcu_read_lock.
1137 */
1138pgoff_t page_cache_next_hole(struct address_space *mapping,
1139 pgoff_t index, unsigned long max_scan)
1140{
1141 unsigned long i;
1142
1143 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001144 struct page *page;
1145
1146 page = radix_tree_lookup(&mapping->page_tree, index);
1147 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001148 break;
1149 index++;
1150 if (index == 0)
1151 break;
1152 }
1153
1154 return index;
1155}
1156EXPORT_SYMBOL(page_cache_next_hole);
1157
1158/**
1159 * page_cache_prev_hole - find the prev hole (not-present entry)
1160 * @mapping: mapping
1161 * @index: index
1162 * @max_scan: maximum range to search
1163 *
1164 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1165 * the first hole.
1166 *
1167 * Returns: the index of the hole if found, otherwise returns an index
1168 * outside of the set specified (in which case 'index - return >=
1169 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1170 * will be returned.
1171 *
1172 * page_cache_prev_hole may be called under rcu_read_lock. However,
1173 * like radix_tree_gang_lookup, this will not atomically search a
1174 * snapshot of the tree at a single point in time. For example, if a
1175 * hole is created at index 10, then subsequently a hole is created at
1176 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1177 * called under rcu_read_lock.
1178 */
1179pgoff_t page_cache_prev_hole(struct address_space *mapping,
1180 pgoff_t index, unsigned long max_scan)
1181{
1182 unsigned long i;
1183
1184 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001185 struct page *page;
1186
1187 page = radix_tree_lookup(&mapping->page_tree, index);
1188 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001189 break;
1190 index--;
1191 if (index == ULONG_MAX)
1192 break;
1193 }
1194
1195 return index;
1196}
1197EXPORT_SYMBOL(page_cache_prev_hole);
1198
1199/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001200 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001201 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001202 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001203 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001204 * Looks up the page cache slot at @mapping & @offset. If there is a
1205 * page cache page, it is returned with an increased refcount.
1206 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001207 * If the slot holds a shadow entry of a previously evicted page, or a
1208 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001209 *
1210 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001212struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Nick Piggina60637c2008-07-25 19:45:31 -07001214 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001215 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Nick Piggina60637c2008-07-25 19:45:31 -07001217 rcu_read_lock();
1218repeat:
1219 page = NULL;
1220 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1221 if (pagep) {
1222 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001223 if (unlikely(!page))
1224 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001225 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001226 if (radix_tree_deref_retry(page))
1227 goto repeat;
1228 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001229 * A shadow entry of a recently evicted page,
1230 * or a swap entry from shmem/tmpfs. Return
1231 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001232 */
1233 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001234 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001235
1236 head = compound_head(page);
1237 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001238 goto repeat;
1239
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001240 /* The page was split under us? */
1241 if (compound_head(page) != head) {
1242 put_page(head);
1243 goto repeat;
1244 }
1245
Nick Piggina60637c2008-07-25 19:45:31 -07001246 /*
1247 * Has the page moved?
1248 * This is part of the lockless pagecache protocol. See
1249 * include/linux/pagemap.h for details.
1250 */
1251 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001252 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001253 goto repeat;
1254 }
1255 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001256out:
Nick Piggina60637c2008-07-25 19:45:31 -07001257 rcu_read_unlock();
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return page;
1260}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001261EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Randy Dunlap485bb992006-06-23 02:03:49 -07001263/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001264 * find_lock_entry - locate, pin and lock a page cache entry
1265 * @mapping: the address_space to search
1266 * @offset: the page cache index
1267 *
1268 * Looks up the page cache slot at @mapping & @offset. If there is a
1269 * page cache page, it is returned locked and with an increased
1270 * refcount.
1271 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001272 * If the slot holds a shadow entry of a previously evicted page, or a
1273 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001274 *
1275 * Otherwise, %NULL is returned.
1276 *
1277 * find_lock_entry() may sleep.
1278 */
1279struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct page *page;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001284 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001285 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001286 lock_page(page);
1287 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001288 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001289 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001290 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001291 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001293 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return page;
1296}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001297EXPORT_SYMBOL(find_lock_entry);
1298
1299/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001300 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001301 * @mapping: the address_space to search
1302 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001303 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001304 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001305 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001306 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001307 *
Randy Dunlap75325182014-07-30 16:08:37 -07001308 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001309 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001310 * FGP_ACCESSED: the page will be marked accessed
1311 * FGP_LOCK: Page is return locked
1312 * FGP_CREAT: If page is not present then a new page is allocated using
Michal Hocko45f87de2014-12-29 20:30:35 +01001313 * @gfp_mask and added to the page cache and the VM's LRU
1314 * list. The page is returned locked and with an increased
1315 * refcount. Otherwise, %NULL is returned.
Josef Bacik091c93e2019-01-02 00:21:01 +09001316 * FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1317 * its own locking dance if the page is already in cache, or unlock the page
1318 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001319 *
1320 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1321 * if the GFP flags specified for FGP_CREAT are atomic.
1322 *
1323 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001324 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001325struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001326 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Nick Piggineb2be182007-10-16 01:24:57 -07001328 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001331 page = find_get_entry(mapping, offset);
1332 if (radix_tree_exceptional_entry(page))
1333 page = NULL;
1334 if (!page)
1335 goto no_page;
1336
1337 if (fgp_flags & FGP_LOCK) {
1338 if (fgp_flags & FGP_NOWAIT) {
1339 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001340 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001341 return NULL;
1342 }
1343 } else {
1344 lock_page(page);
1345 }
1346
1347 /* Has the page been truncated? */
1348 if (unlikely(page->mapping != mapping)) {
1349 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001350 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001351 goto repeat;
1352 }
1353 VM_BUG_ON_PAGE(page->index != offset, page);
1354 }
1355
1356 if (page && (fgp_flags & FGP_ACCESSED))
1357 mark_page_accessed(page);
1358
1359no_page:
1360 if (!page && (fgp_flags & FGP_CREAT)) {
1361 int err;
1362 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001363 gfp_mask |= __GFP_WRITE;
1364 if (fgp_flags & FGP_NOFS)
1365 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001366
Michal Hocko45f87de2014-12-29 20:30:35 +01001367 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001368 if (!page)
1369 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001370
Josef Bacik091c93e2019-01-02 00:21:01 +09001371 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001372 fgp_flags |= FGP_LOCK;
1373
Hugh Dickinseb39d612014-08-06 16:06:43 -07001374 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001375 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001376 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001377
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -07001378 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001379 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001380 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001381 page = NULL;
1382 if (err == -EEXIST)
1383 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Josef Bacik091c93e2019-01-02 00:21:01 +09001385
1386 /*
1387 * add_to_page_cache_lru lock's the page, and for mmap we expect
1388 * a unlocked page.
1389 */
1390 if (page && (fgp_flags & FGP_FOR_MMAP))
1391 unlock_page(page);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return page;
1396}
Mel Gorman2457aec2014-06-04 16:10:31 -07001397EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001400 * find_get_entries - gang pagecache lookup
1401 * @mapping: The address_space to search
1402 * @start: The starting page cache index
1403 * @nr_entries: The maximum number of entries
1404 * @entries: Where the resulting entries are placed
1405 * @indices: The cache indices corresponding to the entries in @entries
1406 *
1407 * find_get_entries() will search for and return a group of up to
1408 * @nr_entries entries in the mapping. The entries are placed at
1409 * @entries. find_get_entries() takes a reference against any actual
1410 * pages it returns.
1411 *
1412 * The search returns a group of mapping-contiguous page cache entries
1413 * with ascending indexes. There may be holes in the indices due to
1414 * not-present pages.
1415 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001416 * Any shadow entries of evicted pages, or swap entries from
1417 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001418 *
1419 * find_get_entries() returns the number of pages and shadow entries
1420 * which were found.
1421 */
1422unsigned find_get_entries(struct address_space *mapping,
1423 pgoff_t start, unsigned int nr_entries,
1424 struct page **entries, pgoff_t *indices)
1425{
1426 void **slot;
1427 unsigned int ret = 0;
1428 struct radix_tree_iter iter;
1429
1430 if (!nr_entries)
1431 return 0;
1432
1433 rcu_read_lock();
Johannes Weiner0cd61442014-04-03 14:47:46 -07001434 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001435 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001436repeat:
1437 page = radix_tree_deref_slot(slot);
1438 if (unlikely(!page))
1439 continue;
1440 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001441 if (radix_tree_deref_retry(page)) {
1442 slot = radix_tree_iter_retry(&iter);
1443 continue;
1444 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001445 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001446 * A shadow entry of a recently evicted page, a swap
1447 * entry from shmem/tmpfs or a DAX entry. Return it
1448 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001449 */
1450 goto export;
1451 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001452
1453 head = compound_head(page);
1454 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001455 goto repeat;
1456
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001457 /* The page was split under us? */
1458 if (compound_head(page) != head) {
1459 put_page(head);
1460 goto repeat;
1461 }
1462
Johannes Weiner0cd61442014-04-03 14:47:46 -07001463 /* Has the page moved? */
1464 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001465 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001466 goto repeat;
1467 }
1468export:
1469 indices[ret] = iter.index;
1470 entries[ret] = page;
1471 if (++ret == nr_entries)
1472 break;
1473 }
1474 rcu_read_unlock();
1475 return ret;
1476}
1477
1478/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * find_get_pages - gang pagecache lookup
1480 * @mapping: The address_space to search
1481 * @start: The starting page index
1482 * @nr_pages: The maximum number of pages
1483 * @pages: Where the resulting pages are placed
1484 *
1485 * find_get_pages() will search for and return a group of up to
1486 * @nr_pages pages in the mapping. The pages are placed at @pages.
1487 * find_get_pages() takes a reference against the returned pages.
1488 *
1489 * The search returns a group of mapping-contiguous pages with ascending
1490 * indexes. There may be holes in the indices due to not-present pages.
1491 *
1492 * find_get_pages() returns the number of pages which were found.
1493 */
1494unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1495 unsigned int nr_pages, struct page **pages)
1496{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001497 struct radix_tree_iter iter;
1498 void **slot;
1499 unsigned ret = 0;
1500
1501 if (unlikely(!nr_pages))
1502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Nick Piggina60637c2008-07-25 19:45:31 -07001504 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001505 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001506 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001507repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001508 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001509 if (unlikely(!page))
1510 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001511
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001512 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001513 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001514 slot = radix_tree_iter_retry(&iter);
1515 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001516 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001517 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001518 * A shadow entry of a recently evicted page,
1519 * or a swap entry from shmem/tmpfs. Skip
1520 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001521 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001522 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001523 }
Nick Piggina60637c2008-07-25 19:45:31 -07001524
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001525 head = compound_head(page);
1526 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001527 goto repeat;
1528
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001529 /* The page was split under us? */
1530 if (compound_head(page) != head) {
1531 put_page(head);
1532 goto repeat;
1533 }
1534
Nick Piggina60637c2008-07-25 19:45:31 -07001535 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001536 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001537 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001538 goto repeat;
1539 }
1540
1541 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001542 if (++ret == nr_pages)
1543 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001544 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001545
Nick Piggina60637c2008-07-25 19:45:31 -07001546 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return ret;
1548}
1549
Jens Axboeebf43502006-04-27 08:46:01 +02001550/**
1551 * find_get_pages_contig - gang contiguous pagecache lookup
1552 * @mapping: The address_space to search
1553 * @index: The starting page index
1554 * @nr_pages: The maximum number of pages
1555 * @pages: Where the resulting pages are placed
1556 *
1557 * find_get_pages_contig() works exactly like find_get_pages(), except
1558 * that the returned number of pages are guaranteed to be contiguous.
1559 *
1560 * find_get_pages_contig() returns the number of pages which were found.
1561 */
1562unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1563 unsigned int nr_pages, struct page **pages)
1564{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001565 struct radix_tree_iter iter;
1566 void **slot;
1567 unsigned int ret = 0;
1568
1569 if (unlikely(!nr_pages))
1570 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001571
Nick Piggina60637c2008-07-25 19:45:31 -07001572 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001573 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001574 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001575repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001576 page = radix_tree_deref_slot(slot);
1577 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001578 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001579 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001580
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001581 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001582 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001583 slot = radix_tree_iter_retry(&iter);
1584 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001585 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001586 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001587 * A shadow entry of a recently evicted page,
1588 * or a swap entry from shmem/tmpfs. Stop
1589 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001590 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001591 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001592 }
Nick Piggina60637c2008-07-25 19:45:31 -07001593
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001594 head = compound_head(page);
1595 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001596 goto repeat;
1597
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001598 /* The page was split under us? */
1599 if (compound_head(page) != head) {
1600 put_page(head);
1601 goto repeat;
1602 }
1603
Nick Piggina60637c2008-07-25 19:45:31 -07001604 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001605 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001606 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001607 goto repeat;
1608 }
1609
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001610 /*
1611 * must check mapping and index after taking the ref.
1612 * otherwise we can get both false positives and false
1613 * negatives, which is just confusing to the caller.
1614 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001615 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001617 break;
1618 }
1619
Nick Piggina60637c2008-07-25 19:45:31 -07001620 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001621 if (++ret == nr_pages)
1622 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001623 }
Nick Piggina60637c2008-07-25 19:45:31 -07001624 rcu_read_unlock();
1625 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001626}
David Howellsef71c152007-05-09 02:33:44 -07001627EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001628
Randy Dunlap485bb992006-06-23 02:03:49 -07001629/**
Jan Karafb296a22017-11-15 17:34:33 -08001630 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001631 * @mapping: the address_space to search
1632 * @index: the starting page index
Jan Karafb296a22017-11-15 17:34:33 -08001633 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001634 * @tag: the tag index
1635 * @nr_pages: the maximum number of pages
1636 * @pages: where the resulting pages are placed
1637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001639 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
Jan Karafb296a22017-11-15 17:34:33 -08001641unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1642 pgoff_t end, int tag, unsigned int nr_pages,
1643 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001645 struct radix_tree_iter iter;
1646 void **slot;
1647 unsigned ret = 0;
1648
1649 if (unlikely(!nr_pages))
1650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Nick Piggina60637c2008-07-25 19:45:31 -07001652 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001653 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1654 &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001655 struct page *head, *page;
Jan Karafb296a22017-11-15 17:34:33 -08001656
1657 if (iter.index > end)
1658 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001659repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001660 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001661 if (unlikely(!page))
1662 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001663
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001664 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001665 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001666 slot = radix_tree_iter_retry(&iter);
1667 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001668 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001669 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001670 * A shadow entry of a recently evicted page.
1671 *
1672 * Those entries should never be tagged, but
1673 * this tree walk is lockless and the tags are
1674 * looked up in bulk, one radix tree node at a
1675 * time, so there is a sizable window for page
1676 * reclaim to evict a page we saw tagged.
1677 *
1678 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001679 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001680 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001681 }
Nick Piggina60637c2008-07-25 19:45:31 -07001682
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001683 head = compound_head(page);
1684 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001685 goto repeat;
1686
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001687 /* The page was split under us? */
1688 if (compound_head(page) != head) {
1689 put_page(head);
1690 goto repeat;
1691 }
1692
Nick Piggina60637c2008-07-25 19:45:31 -07001693 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001694 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001695 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001696 goto repeat;
1697 }
1698
1699 pages[ret] = page;
Jan Karafb296a22017-11-15 17:34:33 -08001700 if (++ret == nr_pages) {
1701 *index = pages[ret - 1]->index + 1;
1702 goto out;
1703 }
Nick Piggina60637c2008-07-25 19:45:31 -07001704 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001705
Jan Karafb296a22017-11-15 17:34:33 -08001706 /*
1707 * We come here when we got at @end. We take care to not overflow the
1708 * index @index as it confuses some of the callers. This breaks the
1709 * iteration when there is page at index -1 but that is already broken
1710 * anyway.
1711 */
1712 if (end == (pgoff_t)-1)
1713 *index = (pgoff_t)-1;
1714 else
1715 *index = end + 1;
1716out:
Nick Piggina60637c2008-07-25 19:45:31 -07001717 rcu_read_unlock();
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return ret;
1720}
Jan Karafb296a22017-11-15 17:34:33 -08001721EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001723/**
1724 * find_get_entries_tag - find and return entries that match @tag
1725 * @mapping: the address_space to search
1726 * @start: the starting page cache index
1727 * @tag: the tag index
1728 * @nr_entries: the maximum number of entries
1729 * @entries: where the resulting entries are placed
1730 * @indices: the cache indices corresponding to the entries in @entries
1731 *
1732 * Like find_get_entries, except we only return entries which are tagged with
1733 * @tag.
1734 */
1735unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1736 int tag, unsigned int nr_entries,
1737 struct page **entries, pgoff_t *indices)
1738{
1739 void **slot;
1740 unsigned int ret = 0;
1741 struct radix_tree_iter iter;
1742
1743 if (!nr_entries)
1744 return 0;
1745
1746 rcu_read_lock();
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001747 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1748 &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001749 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001750repeat:
1751 page = radix_tree_deref_slot(slot);
1752 if (unlikely(!page))
1753 continue;
1754 if (radix_tree_exception(page)) {
1755 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001756 slot = radix_tree_iter_retry(&iter);
1757 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001758 }
1759
1760 /*
1761 * A shadow entry of a recently evicted page, a swap
1762 * entry from shmem/tmpfs or a DAX entry. Return it
1763 * without attempting to raise page count.
1764 */
1765 goto export;
1766 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001767
1768 head = compound_head(page);
1769 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001770 goto repeat;
1771
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001772 /* The page was split under us? */
1773 if (compound_head(page) != head) {
1774 put_page(head);
1775 goto repeat;
1776 }
1777
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001778 /* Has the page moved? */
1779 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001780 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001781 goto repeat;
1782 }
1783export:
1784 indices[ret] = iter.index;
1785 entries[ret] = page;
1786 if (++ret == nr_entries)
1787 break;
1788 }
1789 rcu_read_unlock();
1790 return ret;
1791}
1792EXPORT_SYMBOL(find_get_entries_tag);
1793
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001794/*
1795 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1796 * a _large_ part of the i/o request. Imagine the worst scenario:
1797 *
1798 * ---R__________________________________________B__________
1799 * ^ reading here ^ bad block(assume 4k)
1800 *
1801 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1802 * => failing the whole request => read(R) => read(R+1) =>
1803 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1804 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1805 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1806 *
1807 * It is going insane. Fix it by quickly scaling down the readahead size.
1808 */
1809static void shrink_readahead_size_eio(struct file *filp,
1810 struct file_ra_state *ra)
1811{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001812 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001813}
1814
Randy Dunlap485bb992006-06-23 02:03:49 -07001815/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001816 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001817 * @filp: the file to read
1818 * @ppos: current file position
Al Viro6e58e792014-02-03 17:07:03 -05001819 * @iter: data destination
1820 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001821 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001823 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 *
1825 * This is really ugly. But the goto's actually try to clarify some
1826 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Al Viro6e58e792014-02-03 17:07:03 -05001828static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
1829 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001831 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001833 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001834 pgoff_t index;
1835 pgoff_t last_index;
1836 pgoff_t prev_index;
1837 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001838 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001839 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Wei Fangc2a97372016-10-07 17:01:52 -07001841 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvalds48466c42016-12-14 12:45:25 -08001842 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07001843 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
1844
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001845 index = *ppos >> PAGE_SHIFT;
1846 prev_index = ra->prev_pos >> PAGE_SHIFT;
1847 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1848 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1849 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 for (;;) {
1852 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001853 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001854 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 unsigned long nr, ret;
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858find_page:
Michal Hockob67c7d32017-02-03 13:13:29 -08001859 if (fatal_signal_pending(current)) {
1860 error = -EINTR;
1861 goto out;
1862 }
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001865 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001866 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001867 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001868 index, last_index - index);
1869 page = find_get_page(mapping, index);
1870 if (unlikely(page == NULL))
1871 goto no_cached_page;
1872 }
1873 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001874 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001875 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001876 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001878 if (!PageUptodate(page)) {
Mel Gormanebded022016-03-15 14:55:39 -07001879 /*
1880 * See comment in do_read_cache_page on why
1881 * wait_on_page_locked is used to avoid unnecessarily
1882 * serialisations and why it's safe.
1883 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07001884 error = wait_on_page_locked_killable(page);
1885 if (unlikely(error))
1886 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07001887 if (PageUptodate(page))
1888 goto page_ok;
1889
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001890 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001891 !mapping->a_ops->is_partially_uptodate)
1892 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08001893 /* pipes can't handle partially uptodate pages */
1894 if (unlikely(iter->type & ITER_PIPE))
1895 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001896 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001897 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001898 /* Did it get truncated before we got the lock? */
1899 if (!page->mapping)
1900 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001901 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05001902 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001903 goto page_not_up_to_date_locked;
1904 unlock_page(page);
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001907 /*
1908 * i_size must be checked after we know the page is Uptodate.
1909 *
1910 * Checking i_size after the check allows us to calculate
1911 * the correct value for "nr", which means the zero-filled
1912 * part of the page is not copied back to userspace (unless
1913 * another truncate extends the file - this is desired though).
1914 */
1915
1916 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001917 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001918 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001919 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001920 goto out;
1921 }
1922
1923 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001924 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001925 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001926 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001927 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001928 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001929 goto out;
1930 }
1931 }
1932 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* If users can be writing to this page using arbitrary
1935 * virtual addresses, take care about potential aliasing
1936 * before reading the page on the kernel side.
1937 */
1938 if (mapping_writably_mapped(mapping))
1939 flush_dcache_page(page);
1940
1941 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001942 * When a sequential read accesses a page several times,
1943 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 */
Jan Karaec0f1632007-05-06 14:49:25 -07001945 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 mark_page_accessed(page);
1947 prev_index = index;
1948
1949 /*
1950 * Ok, we have the page, and it's up-to-date, so
1951 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
Al Viro6e58e792014-02-03 17:07:03 -05001953
1954 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001956 index += offset >> PAGE_SHIFT;
1957 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001958 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001960 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001961 written += ret;
1962 if (!iov_iter_count(iter))
1963 goto out;
1964 if (ret < nr) {
1965 error = -EFAULT;
1966 goto out;
1967 }
1968 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970page_not_up_to_date:
1971 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001972 error = lock_page_killable(page);
1973 if (unlikely(error))
1974 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001976page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001977 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (!page->mapping) {
1979 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001980 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 continue;
1982 }
1983
1984 /* Did somebody else fill it already? */
1985 if (PageUptodate(page)) {
1986 unlock_page(page);
1987 goto page_ok;
1988 }
1989
1990readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001991 /*
1992 * A previous I/O error may have been due to temporary
1993 * failures, eg. multipath errors.
1994 * PG_error will be set again if readpage fails.
1995 */
1996 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /* Start the actual read. The read will unlock the page. */
1998 error = mapping->a_ops->readpage(filp, page);
1999
Zach Brown994fc28c2005-12-15 14:28:17 -08002000 if (unlikely(error)) {
2001 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002002 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002003 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002004 goto find_page;
2005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002010 error = lock_page_killable(page);
2011 if (unlikely(error))
2012 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (!PageUptodate(page)) {
2014 if (page->mapping == NULL) {
2015 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002016 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
2018 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002019 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 goto find_page;
2021 }
2022 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002023 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002024 error = -EIO;
2025 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 unlock_page(page);
2028 }
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto page_ok;
2031
2032readpage_error:
2033 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002034 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 goto out;
2036
2037no_cached_page:
2038 /*
2039 * Ok, it wasn't cached, so we need to create a new
2040 * page..
2041 */
Nick Piggineb2be182007-10-16 01:24:57 -07002042 page = page_cache_alloc_cold(mapping);
2043 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002044 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002047 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002048 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002050 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002051 if (error == -EEXIST) {
2052 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 goto out;
2056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 goto readpage;
2058 }
2059
2060out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002061 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002063 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002065 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002066 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002067 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Randy Dunlap485bb992006-06-23 02:03:49 -07002070/**
Al Viro6abd2322014-04-04 14:20:57 -04002071 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002072 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002073 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002074 *
Al Viro6abd2322014-04-04 14:20:57 -04002075 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * that can use the page cache directly.
2077 */
2078ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002079generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Al Viroed978a82014-03-05 22:53:04 -05002081 struct file *file = iocb->ki_filp;
2082 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002083 size_t count = iov_iter_count(iter);
2084
2085 if (!count)
2086 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Al Viro2ba48ce2015-04-09 13:52:01 -04002088 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viroed978a82014-03-05 22:53:04 -05002089 struct address_space *mapping = file->f_mapping;
2090 struct inode *inode = mapping->host;
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002091 struct iov_iter data = *iter;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002092 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 size = i_size_read(inode);
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002095 retval = filemap_write_and_wait_range(mapping, iocb->ki_pos,
2096 iocb->ki_pos + count - 1);
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002097 if (retval < 0)
2098 goto out;
Al Viroed978a82014-03-05 22:53:04 -05002099
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002100 file_accessed(file);
2101
2102 retval = mapping->a_ops->direct_IO(iocb, &data);
Al Viroc3a69022016-10-10 13:26:27 -04002103 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002104 iocb->ki_pos += retval;
Al Viroed978a82014-03-05 22:53:04 -05002105 iov_iter_advance(iter, retval);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002106 }
Josef Bacik66f998f2010-05-23 11:00:54 -04002107
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002108 /*
2109 * Btrfs can have a short DIO read if we encounter
2110 * compressed extents, so if there was an error, or if
2111 * we've already read everything we wanted to, or if
2112 * there was a short read because we hit EOF, go ahead
2113 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002114 * the rest of the read. Buffered reads will not work for
2115 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002116 */
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002117 if (retval < 0 || !iov_iter_count(iter) || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002118 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002122 retval = do_generic_file_read(file, &iocb->ki_pos, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123out:
2124 return retval;
2125}
Al Viroed978a82014-03-05 22:53:04 -05002126EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129#define MMAP_LOTSAMISS (100)
2130
Josef Bacikf9593722019-01-02 11:11:01 +09002131static struct file *maybe_unlock_mmap_for_io(struct vm_area_struct *vma,
2132 unsigned long flags, struct file *fpin)
2133{
2134 if (fpin)
2135 return fpin;
2136
2137 /*
2138 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2139 * anything, so we only pin the file and drop the mmap_sem if only
2140 * FAULT_FLAG_ALLOW_RETRY is set.
2141 */
2142 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2143 FAULT_FLAG_ALLOW_RETRY) {
2144 fpin = get_file(vma->vm_file);
2145 up_read(&vma->vm_mm->mmap_sem);
2146 }
2147 return fpin;
2148}
2149
Linus Torvaldsef00e082009-06-16 15:31:25 -07002150/*
Josef Bacikf9593722019-01-02 11:11:01 +09002151 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2152 * @vmf - the vm_fault for this fault.
2153 * @page - the page to lock.
2154 * @fpin - the pointer to the file we may pin (or is already pinned).
2155 *
2156 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2157 * It differs in that it actually returns the page locked if it returns 1 and 0
2158 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2159 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002160 */
Josef Bacikf9593722019-01-02 11:11:01 +09002161static int lock_page_maybe_drop_mmap(struct vm_area_struct *vma,
2162 unsigned long flags, struct page *page, struct file **fpin)
2163{
2164 if (trylock_page(page))
2165 return 1;
2166
Linus Torvalds572b9da2019-03-15 11:26:07 -07002167 /*
2168 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2169 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2170 * is supposed to work. We have way too many special cases..
2171 */
Josef Bacikf9593722019-01-02 11:11:01 +09002172 if (flags & FAULT_FLAG_RETRY_NOWAIT)
2173 return 0;
2174 *fpin = maybe_unlock_mmap_for_io(vma, flags, *fpin);
2175 if (flags & FAULT_FLAG_KILLABLE) {
2176 if (__lock_page_killable(page)) {
2177 /*
2178 * We didn't have the right flags to drop the mmap_sem,
2179 * but all fault_handlers only check for fatal signals
2180 * if we return VM_FAULT_RETRY, so we need to drop the
2181 * mmap_sem here and return 0 if we don't have a fpin.
2182 */
2183 if (*fpin == NULL)
2184 up_read(&vma->vm_mm->mmap_sem);
2185 return 0;
2186 }
2187 } else
2188 __lock_page(page);
2189 return 1;
2190}
2191
2192/*
2193 * Synchronous readahead happens when we don't even find a page in the page
2194 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2195 * to drop the mmap sem we return the file that was pinned in order for us to do
2196 * that. If we didn't pin a file then we return NULL. The file that is
2197 * returned needs to be fput()'ed when we're done with it.
2198 */
2199static struct file *do_sync_mmap_readahead(struct vm_area_struct *vma,
2200 unsigned long flags,
Linus Torvaldsef00e082009-06-16 15:31:25 -07002201 struct file_ra_state *ra,
2202 struct file *file,
2203 pgoff_t offset)
2204{
Josef Bacikf9593722019-01-02 11:11:01 +09002205 struct file *fpin = NULL;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002206 struct address_space *mapping = file->f_mapping;
2207
2208 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002209 if (vma->vm_flags & VM_RAND_READ)
Josef Bacikf9593722019-01-02 11:11:01 +09002210 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002211 if (!ra->ra_pages)
Josef Bacikf9593722019-01-02 11:11:01 +09002212 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002213
Joe Perches64363aa2013-07-08 16:00:18 -07002214 if (vma->vm_flags & VM_SEQ_READ) {
Josef Bacikf9593722019-01-02 11:11:01 +09002215 fpin = maybe_unlock_mmap_for_io(vma, flags, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002216 page_cache_sync_readahead(mapping, ra, file, offset,
2217 ra->ra_pages);
Josef Bacikf9593722019-01-02 11:11:01 +09002218 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002219 }
2220
Andi Kleen207d04b2011-05-24 17:12:29 -07002221 /* Avoid banging the cache line if not needed */
2222 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002223 ra->mmap_miss++;
2224
2225 /*
2226 * Do we miss much more than hit in this file? If so,
2227 * stop bothering with read-ahead. It will only hurt.
2228 */
2229 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacikf9593722019-01-02 11:11:01 +09002230 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002231
Wu Fengguangd30a1102009-06-16 15:31:30 -07002232 /*
2233 * mmap read-around
2234 */
Josef Bacikf9593722019-01-02 11:11:01 +09002235 fpin = maybe_unlock_mmap_for_io(vma, flags, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002236 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2237 ra->size = ra->ra_pages;
2238 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002239 ra_submit(ra, mapping, file);
Josef Bacikf9593722019-01-02 11:11:01 +09002240 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002241}
2242
2243/*
2244 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacikf9593722019-01-02 11:11:01 +09002245 * so we want to possibly extend the readahead further. We return the file that
2246 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002247 */
Josef Bacikf9593722019-01-02 11:11:01 +09002248static struct file *do_async_mmap_readahead(struct vm_area_struct *vma,
2249 unsigned long flags,
Linus Torvaldsef00e082009-06-16 15:31:25 -07002250 struct file_ra_state *ra,
2251 struct file *file,
2252 struct page *page,
2253 pgoff_t offset)
2254{
2255 struct address_space *mapping = file->f_mapping;
Josef Bacikf9593722019-01-02 11:11:01 +09002256 struct file *fpin = NULL;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002257
2258 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002259 if (vma->vm_flags & VM_RAND_READ)
Josef Bacikf9593722019-01-02 11:11:01 +09002260 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002261 if (ra->mmap_miss > 0)
2262 ra->mmap_miss--;
Josef Bacikf9593722019-01-02 11:11:01 +09002263 if (PageReadahead(page)) {
2264 fpin = maybe_unlock_mmap_for_io(vma, flags, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002265 page_cache_async_readahead(mapping, ra, file,
2266 page, offset, ra->ra_pages);
Josef Bacikf9593722019-01-02 11:11:01 +09002267 }
2268 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002269}
2270
Randy Dunlap485bb992006-06-23 02:03:49 -07002271/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002272 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002273 * @vma: vma in which the fault was taken
2274 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002275 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002276 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * mapped memory region to read in file data during a page fault.
2278 *
2279 * The goto's are kind of ugly, but this streamlines the normal case of having
2280 * it in the page cache, and handles the special cases reasonably without
2281 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002282 *
2283 * vma->vm_mm->mmap_sem must be held on entry.
2284 *
2285 * If our return value has VM_FAULT_RETRY set, it's because
2286 * lock_page_or_retry() returned 0.
2287 * The mmap_sem has usually been released in this case.
2288 * See __lock_page_or_retry() for the exception.
2289 *
2290 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2291 * has not been released.
2292 *
2293 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 */
Nick Piggind0217ac2007-07-19 01:47:03 -07002295int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07002298 struct file *file = vma->vm_file;
Josef Bacikf9593722019-01-02 11:11:01 +09002299 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct address_space *mapping = file->f_mapping;
2301 struct file_ra_state *ra = &file->f_ra;
2302 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002303 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 struct page *page;
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07002305 loff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07002306 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002308 size = round_up(i_size_read(inode), PAGE_SIZE);
2309 if (offset >= size >> PAGE_SHIFT)
Linus Torvalds5307cc12007-10-31 09:19:46 -07002310 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002313 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002315 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002316 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002318 * We found the page, so try async readahead before
2319 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 */
Josef Bacikf9593722019-01-02 11:11:01 +09002321 fpin = do_async_mmap_readahead(vma, vmf->flags, ra,
2322 file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002323 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002324 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002325 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07002326 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002327 ret = VM_FAULT_MAJOR;
Josef Bacikf9593722019-01-02 11:11:01 +09002328 fpin = do_sync_mmap_readahead(vma, vmf->flags, ra,
2329 file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002330retry_find:
Josef Bacik091c93e2019-01-02 00:21:01 +09002331 page = pagecache_get_page(mapping, offset,
2332 FGP_CREAT|FGP_FOR_MMAP,
2333 vmf->gfp_mask);
Josef Bacikf9593722019-01-02 11:11:01 +09002334 if (!page) {
2335 if (fpin)
2336 goto out_retry;
Josef Bacik091c93e2019-01-02 00:21:01 +09002337 return VM_FAULT_OOM;
Josef Bacikf9593722019-01-02 11:11:01 +09002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
Josef Bacikf9593722019-01-02 11:11:01 +09002340 if (!lock_page_maybe_drop_mmap(vma, vmf->flags, page, &fpin))
2341 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002342
2343 /* Did it get truncated? */
2344 if (unlikely(page->mapping != mapping)) {
2345 unlock_page(page);
2346 put_page(page);
2347 goto retry_find;
2348 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002349 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002352 * We have a locked page in the page cache, now we need to check
2353 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 */
Nick Piggind00806b2007-07-19 01:46:57 -07002355 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 goto page_not_uptodate;
2357
Linus Torvaldsef00e082009-06-16 15:31:25 -07002358 /*
Josef Bacikf9593722019-01-02 11:11:01 +09002359 * We've made it this far and we had to drop our mmap_sem, now is the
2360 * time to return to the upper layer and have it re-find the vma and
2361 * redo the fault.
2362 */
2363 if (fpin) {
2364 unlock_page(page);
2365 goto out_retry;
2366 }
2367
2368 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002369 * Found the page and have a reference on it.
2370 * We must recheck i_size under page lock.
2371 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002372 size = round_up(i_size_read(inode), PAGE_SIZE);
2373 if (unlikely(offset >= size >> PAGE_SHIFT)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002374 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002375 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002376 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002377 }
2378
Nick Piggind0217ac2007-07-19 01:47:03 -07002379 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002380 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * Umm, take care of errors if the page isn't up-to-date.
2385 * Try to re-read it _once_. We do this synchronously,
2386 * because there really aren't any performance issues here
2387 * and we need to check for errors.
2388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 ClearPageError(page);
Josef Bacikf9593722019-01-02 11:11:01 +09002390 fpin = maybe_unlock_mmap_for_io(vma, vmf->flags, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002391 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002392 if (!error) {
2393 wait_on_page_locked(page);
2394 if (!PageUptodate(page))
2395 error = -EIO;
2396 }
Josef Bacikf9593722019-01-02 11:11:01 +09002397 if (fpin)
2398 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002399 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002400
2401 if (!error || error == AOP_TRUNCATED_PAGE)
2402 goto retry_find;
2403
2404 /* Things didn't work out. Return zero to tell the mm layer so. */
2405 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002406 return VM_FAULT_SIGBUS;
Josef Bacikf9593722019-01-02 11:11:01 +09002407
2408out_retry:
2409 /*
2410 * We dropped the mmap_sem, we need to return to the fault handler to
2411 * re-find the vma and come back and find our hopefully still populated
2412 * page.
2413 */
2414 if (page)
2415 put_page(page);
2416 if (fpin)
2417 fput(fpin);
2418 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002419}
2420EXPORT_SYMBOL(filemap_fault);
2421
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002422void filemap_map_pages(struct fault_env *fe,
2423 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002424{
2425 struct radix_tree_iter iter;
2426 void **slot;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002427 struct file *file = fe->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002428 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002429 pgoff_t last_pgoff = start_pgoff;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002430 loff_t size;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002431 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002432
2433 rcu_read_lock();
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002434 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
2435 start_pgoff) {
2436 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002437 break;
2438repeat:
2439 page = radix_tree_deref_slot(slot);
2440 if (unlikely(!page))
2441 goto next;
2442 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002443 if (radix_tree_deref_retry(page)) {
2444 slot = radix_tree_iter_retry(&iter);
2445 continue;
2446 }
2447 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002448 }
2449
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002450 head = compound_head(page);
2451 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002452 goto repeat;
2453
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002454 /* The page was split under us? */
2455 if (compound_head(page) != head) {
2456 put_page(head);
2457 goto repeat;
2458 }
2459
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002460 /* Has the page moved? */
2461 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002462 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002463 goto repeat;
2464 }
2465
2466 if (!PageUptodate(page) ||
2467 PageReadahead(page) ||
2468 PageHWPoison(page))
2469 goto skip;
2470 if (!trylock_page(page))
2471 goto skip;
2472
2473 if (page->mapping != mapping || !PageUptodate(page))
2474 goto unlock;
2475
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002476 size = round_up(i_size_read(mapping->host), PAGE_SIZE);
2477 if (page->index >= size >> PAGE_SHIFT)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002478 goto unlock;
2479
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002480 if (file->f_ra.mmap_miss > 0)
2481 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002482
2483 fe->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2484 if (fe->pte)
2485 fe->pte += iter.index - last_pgoff;
2486 last_pgoff = iter.index;
2487 if (alloc_set_pte(fe, NULL, page))
2488 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002489 unlock_page(page);
2490 goto next;
2491unlock:
2492 unlock_page(page);
2493skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002494 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002495next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002496 /* Huge page is mapped? No need to proceed. */
2497 if (pmd_trans_huge(*fe->pmd))
2498 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002499 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002500 break;
2501 }
2502 rcu_read_unlock();
2503}
2504EXPORT_SYMBOL(filemap_map_pages);
2505
Jan Kara4fcf1c62012-06-12 16:20:29 +02002506int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2507{
2508 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002509 struct inode *inode = file_inode(vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002510 int ret = VM_FAULT_LOCKED;
2511
Jan Kara14da9202012-06-12 16:20:37 +02002512 sb_start_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002513 file_update_time(vma->vm_file);
2514 lock_page(page);
2515 if (page->mapping != inode->i_mapping) {
2516 unlock_page(page);
2517 ret = VM_FAULT_NOPAGE;
2518 goto out;
2519 }
Jan Kara14da9202012-06-12 16:20:37 +02002520 /*
2521 * We mark the page dirty already here so that when freeze is in
2522 * progress, we are guaranteed that writeback during freezing will
2523 * see the dirty page and writeprotect it again.
2524 */
2525 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002526 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002527out:
Jan Kara14da9202012-06-12 16:20:37 +02002528 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002529 return ret;
2530}
2531EXPORT_SYMBOL(filemap_page_mkwrite);
2532
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002533const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002534 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002535 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002536 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537};
2538
2539/* This is used for a general mmap of a disk file */
2540
2541int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2542{
2543 struct address_space *mapping = file->f_mapping;
2544
2545 if (!mapping->a_ops->readpage)
2546 return -ENOEXEC;
2547 file_accessed(file);
2548 vma->vm_ops = &generic_file_vm_ops;
2549 return 0;
2550}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552/*
2553 * This is for filesystems which do not implement ->writepage.
2554 */
2555int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2556{
2557 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2558 return -EINVAL;
2559 return generic_file_mmap(file, vma);
2560}
2561#else
2562int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2563{
2564 return -ENOSYS;
2565}
2566int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2567{
2568 return -ENOSYS;
2569}
2570#endif /* CONFIG_MMU */
2571
2572EXPORT_SYMBOL(generic_file_mmap);
2573EXPORT_SYMBOL(generic_file_readonly_mmap);
2574
Sasha Levin67f9fd92014-04-03 14:48:18 -07002575static struct page *wait_on_page_read(struct page *page)
2576{
2577 if (!IS_ERR(page)) {
2578 wait_on_page_locked(page);
2579 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002580 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002581 page = ERR_PTR(-EIO);
2582 }
2583 }
2584 return page;
2585}
2586
Mel Gorman32b63522016-03-15 14:55:36 -07002587static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002588 pgoff_t index,
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002589 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002590 void *data,
2591 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Nick Piggineb2be182007-10-16 01:24:57 -07002593 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int err;
2595repeat:
2596 page = find_get_page(mapping, index);
2597 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002598 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002599 if (!page)
2600 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002601 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002602 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002603 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002604 if (err == -EEXIST)
2605 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return ERR_PTR(err);
2608 }
Mel Gorman32b63522016-03-15 14:55:36 -07002609
2610filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 err = filler(data, page);
2612 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002613 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002614 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Mel Gorman32b63522016-03-15 14:55:36 -07002616
2617 page = wait_on_page_read(page);
2618 if (IS_ERR(page))
2619 return page;
2620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (PageUptodate(page))
2623 goto out;
2624
Mel Gormanebded022016-03-15 14:55:39 -07002625 /*
2626 * Page is not up to date and may be locked due one of the following
2627 * case a: Page is being filled and the page lock is held
2628 * case b: Read/write error clearing the page uptodate status
2629 * case c: Truncation in progress (page locked)
2630 * case d: Reclaim in progress
2631 *
2632 * Case a, the page will be up to date when the page is unlocked.
2633 * There is no need to serialise on the page lock here as the page
2634 * is pinned so the lock gives no additional protection. Even if the
2635 * the page is truncated, the data is still valid if PageUptodate as
2636 * it's a race vs truncate race.
2637 * Case b, the page will not be up to date
2638 * Case c, the page may be truncated but in itself, the data may still
2639 * be valid after IO completes as it's a read vs truncate race. The
2640 * operation must restart if the page is not uptodate on unlock but
2641 * otherwise serialising on page lock to stabilise the mapping gives
2642 * no additional guarantees to the caller as the page lock is
2643 * released before return.
2644 * Case d, similar to truncation. If reclaim holds the page lock, it
2645 * will be a race with remove_mapping that determines if the mapping
2646 * is valid on unlock but otherwise the data is valid and there is
2647 * no need to serialise with page lock.
2648 *
2649 * As the page lock gives no additional guarantee, we optimistically
2650 * wait on the page to be unlocked and check if it's up to date and
2651 * use the page if it is. Otherwise, the page lock is required to
2652 * distinguish between the different cases. The motivation is that we
2653 * avoid spurious serialisations and wakeups when multiple processes
2654 * wait on the same page for IO to complete.
2655 */
2656 wait_on_page_locked(page);
2657 if (PageUptodate(page))
2658 goto out;
2659
2660 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002662
2663 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (!page->mapping) {
2665 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002666 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002667 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Mel Gormanebded022016-03-15 14:55:39 -07002669
2670 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (PageUptodate(page)) {
2672 unlock_page(page);
2673 goto out;
2674 }
Mel Gorman32b63522016-03-15 14:55:36 -07002675 goto filler;
2676
David Howellsc855ff32007-05-09 13:42:20 +01002677out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002678 mark_page_accessed(page);
2679 return page;
2680}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002681
2682/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002683 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002684 * @mapping: the page's address_space
2685 * @index: the page index
2686 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002687 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002688 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002689 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002690 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002691 *
2692 * If the page does not get brought uptodate, return -EIO.
2693 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002694struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002695 pgoff_t index,
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002696 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002697 void *data)
2698{
2699 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2700}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002701EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002702
2703/**
2704 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2705 * @mapping: the page's address_space
2706 * @index: the page index
2707 * @gfp: the page allocator flags to use if allocating
2708 *
2709 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002710 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002711 *
2712 * If the page does not get brought uptodate, return -EIO.
2713 */
2714struct page *read_cache_page_gfp(struct address_space *mapping,
2715 pgoff_t index,
2716 gfp_t gfp)
2717{
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002718 filler_t *filler = mapping->a_ops->readpage;
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002719
Sasha Levin67f9fd92014-04-03 14:48:18 -07002720 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002721}
2722EXPORT_SYMBOL(read_cache_page_gfp);
2723
Nick Piggin2f718ff2007-10-16 01:24:59 -07002724/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 * Performs necessary checks before doing a write
2726 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002727 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * Returns appropriate error code that caller should return or
2729 * zero in case that write should be allowed.
2730 */
Al Viro3309dd02015-04-09 12:55:47 -04002731inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Al Viro3309dd02015-04-09 12:55:47 -04002733 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002735 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002736 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Al Viro3309dd02015-04-09 12:55:47 -04002738 if (!iov_iter_count(from))
2739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Al Viro0fa6b002015-04-04 04:05:48 -04002741 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002742 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002743 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Al Viro3309dd02015-04-09 12:55:47 -04002745 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Al Viro0fa6b002015-04-04 04:05:48 -04002747 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002748 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002749 send_sig(SIGXFSZ, current, 0);
2750 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
Al Viro3309dd02015-04-09 12:55:47 -04002752 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
2754
2755 /*
2756 * LFS rule
2757 */
Al Viro3309dd02015-04-09 12:55:47 -04002758 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002760 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002762 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764
2765 /*
2766 * Are we about to exceed the fs block limit ?
2767 *
2768 * If we have written data it becomes a short write. If we have
2769 * exceeded without writing data we send a signal and return EFBIG.
2770 * Linus frestrict idea will clean these up nicely..
2771 */
Al Viro3309dd02015-04-09 12:55:47 -04002772 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2773 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Al Viro3309dd02015-04-09 12:55:47 -04002775 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2776 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778EXPORT_SYMBOL(generic_write_checks);
2779
Nick Pigginafddba42007-10-16 01:25:01 -07002780int pagecache_write_begin(struct file *file, struct address_space *mapping,
2781 loff_t pos, unsigned len, unsigned flags,
2782 struct page **pagep, void **fsdata)
2783{
2784 const struct address_space_operations *aops = mapping->a_ops;
2785
Nick Piggin4e02ed42008-10-29 14:00:55 -07002786 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002787 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002788}
2789EXPORT_SYMBOL(pagecache_write_begin);
2790
2791int pagecache_write_end(struct file *file, struct address_space *mapping,
2792 loff_t pos, unsigned len, unsigned copied,
2793 struct page *page, void *fsdata)
2794{
2795 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002796
Nick Piggin4e02ed42008-10-29 14:00:55 -07002797 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002798}
2799EXPORT_SYMBOL(pagecache_write_end);
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002802generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 struct file *file = iocb->ki_filp;
2805 struct address_space *mapping = file->f_mapping;
2806 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002807 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002809 size_t write_len;
2810 pgoff_t end;
Al Viro26978b82014-03-10 14:08:45 -04002811 struct iov_iter data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Al Viro0c949332014-03-22 06:51:37 -04002813 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002814 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002815
Nick Piggin48b47c52009-01-06 14:40:22 -08002816 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002817 if (written)
2818 goto out;
2819
2820 /*
2821 * After a write we want buffered reads to be sure to go to disk to get
2822 * the new data. We invalidate clean cached page from the region we're
2823 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002824 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002825 */
2826 if (mapping->nrpages) {
2827 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002828 pos >> PAGE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002829 /*
2830 * If a page can not be invalidated, return 0 to fall back
2831 * to buffered write.
2832 */
2833 if (written) {
2834 if (written == -EBUSY)
2835 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002836 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002837 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002838 }
2839
Al Viro26978b82014-03-10 14:08:45 -04002840 data = *from;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002841 written = mapping->a_ops->direct_IO(iocb, &data);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002842
2843 /*
2844 * Finally, try again to invalidate clean pages which might have been
2845 * cached by non-direct readahead, or faulted in by get_user_pages()
2846 * if the source of the write was an mmap'ed region of the file
2847 * we're writing. Either one is a pretty crazy thing to do,
2848 * so we don't support it 100%. If this invalidation
2849 * fails, tough, the write still worked...
2850 */
2851 if (mapping->nrpages) {
2852 invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002853 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002854 }
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002857 pos += written;
Al Virof8579f82014-03-03 22:03:20 -05002858 iov_iter_advance(from, written);
Namhyung Kim01166512010-10-26 14:21:58 -07002859 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2860 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 mark_inode_dirty(inode);
2862 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002863 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002865out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return written;
2867}
2868EXPORT_SYMBOL(generic_file_direct_write);
2869
Nick Piggineb2be182007-10-16 01:24:57 -07002870/*
2871 * Find or create a page at the given pagecache position. Return the locked
2872 * page. This function is specifically for buffered writes.
2873 */
Nick Piggin54566b22009-01-04 12:00:53 -08002874struct page *grab_cache_page_write_begin(struct address_space *mapping,
2875 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002876{
Nick Piggineb2be182007-10-16 01:24:57 -07002877 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07002878 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002879
Nick Piggin54566b22009-01-04 12:00:53 -08002880 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002881 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002882
Mel Gorman2457aec2014-06-04 16:10:31 -07002883 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002884 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002885 if (page)
2886 wait_for_stable_page(page);
2887
Nick Piggineb2be182007-10-16 01:24:57 -07002888 return page;
2889}
Nick Piggin54566b22009-01-04 12:00:53 -08002890EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002891
Al Viro3b93f912014-02-11 21:34:08 -05002892ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07002893 struct iov_iter *i, loff_t pos)
2894{
2895 struct address_space *mapping = file->f_mapping;
2896 const struct address_space_operations *a_ops = mapping->a_ops;
2897 long status = 0;
2898 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002899 unsigned int flags = 0;
2900
2901 /*
2902 * Copies from kernel address space cannot fail (NFSD is a big user).
2903 */
Al Viro777eda22014-12-17 04:46:46 -05002904 if (!iter_is_iovec(i))
Nick Piggin674b8922007-10-16 01:25:03 -07002905 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002906
2907 do {
2908 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002909 unsigned long offset; /* Offset into pagecache page */
2910 unsigned long bytes; /* Bytes to write to page */
2911 size_t copied; /* Bytes copied from user */
2912 void *fsdata;
2913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 offset = (pos & (PAGE_SIZE - 1));
2915 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002916 iov_iter_count(i));
2917
2918again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01002919 /*
2920 * Bring in the user page that we will copy from _first_.
2921 * Otherwise there's a nasty deadlock on copying from the
2922 * same page as we're writing to, without it being marked
2923 * up-to-date.
2924 *
2925 * Not only is this an optimisation, but it is also required
2926 * to check that the address is actually valid, when atomic
2927 * usercopies are used, below.
2928 */
2929 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2930 status = -EFAULT;
2931 break;
2932 }
2933
Jan Kara296291c2015-10-22 13:32:21 -07002934 if (fatal_signal_pending(current)) {
2935 status = -EINTR;
2936 break;
2937 }
2938
Nick Piggin674b8922007-10-16 01:25:03 -07002939 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002940 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07002941 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07002942 break;
2943
anfei zhou931e80e2010-02-02 13:44:02 -08002944 if (mapping_writably_mapped(mapping))
2945 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01002946
Nick Pigginafddba42007-10-16 01:25:01 -07002947 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07002948 flush_dcache_page(page);
2949
2950 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2951 page, fsdata);
2952 if (unlikely(status < 0))
2953 break;
2954 copied = status;
2955
2956 cond_resched();
2957
Nick Piggin124d3b72008-02-02 15:01:17 +01002958 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002959 if (unlikely(copied == 0)) {
2960 /*
2961 * If we were unable to copy any data at all, we must
2962 * fall back to a single segment length write.
2963 *
2964 * If we didn't fallback here, we could livelock
2965 * because not all segments in the iov can be copied at
2966 * once without a pagefault.
2967 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002968 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002969 iov_iter_single_seg_count(i));
2970 goto again;
2971 }
Nick Pigginafddba42007-10-16 01:25:01 -07002972 pos += copied;
2973 written += copied;
2974
2975 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07002976 } while (iov_iter_count(i));
2977
2978 return written ? written : status;
2979}
Al Viro3b93f912014-02-11 21:34:08 -05002980EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Jan Karae4dd9de2009-08-17 18:10:06 +02002982/**
Al Viro81742022014-04-03 03:17:43 -04002983 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002984 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04002985 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002986 *
2987 * This function does all the work needed for actually writing data to a
2988 * file. It does all basic checks, removes SUID from the file, updates
2989 * modification times and calls proper subroutines depending on whether we
2990 * do direct IO or a standard buffered write.
2991 *
2992 * It expects i_mutex to be grabbed unless we work on a block device or similar
2993 * object which does not need locking at all.
2994 *
2995 * This function does *not* take care of syncing data in case of O_SYNC write.
2996 * A caller has to handle it. This is mainly due to the fact that we want to
2997 * avoid syncing under i_mutex.
2998 */
Al Viro81742022014-04-03 03:17:43 -04002999ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
3001 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003002 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003004 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003006 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003009 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003010 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (err)
3012 goto out;
3013
Josef Bacikc3b2da32012-03-26 09:59:21 -04003014 err = file_update_time(file);
3015 if (err)
3016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Al Viro2ba48ce2015-04-09 13:52:01 -04003018 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003019 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003020
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003021 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003022 /*
3023 * If the write stopped short of completing, fall back to
3024 * buffered writes. Some filesystems do this for writes to
3025 * holes, for example. For DAX files, a buffered write will
3026 * not succeed (even if it did, DAX does not handle dirty
3027 * page-cache pages correctly).
3028 */
Al Viro0b8def92015-04-07 10:22:53 -04003029 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003031
Al Viro0b8def92015-04-07 10:22:53 -04003032 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003033 /*
Al Viro3b93f912014-02-11 21:34:08 -05003034 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003035 * then we want to return the number of bytes which were
3036 * direct-written, or the error code if that was zero. Note
3037 * that this differs from normal direct-io semantics, which
3038 * will return -EFOO even if some bytes were written.
3039 */
Al Viro60bb4522014-08-08 12:39:16 -04003040 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003041 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003042 goto out;
3043 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003044 /*
3045 * We need to ensure that the page cache pages are written to
3046 * disk and invalidated to preserve the expected O_DIRECT
3047 * semantics.
3048 */
Al Viro3b93f912014-02-11 21:34:08 -05003049 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003050 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003051 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003052 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003053 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003054 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003055 pos >> PAGE_SHIFT,
3056 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003057 } else {
3058 /*
3059 * We don't know how much we wrote, so just return
3060 * the number of bytes which were direct-written
3061 */
3062 }
3063 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003064 written = generic_perform_write(file, from, iocb->ki_pos);
3065 if (likely(written > 0))
3066 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068out:
3069 current->backing_dev_info = NULL;
3070 return written ? written : err;
3071}
Al Viro81742022014-04-03 03:17:43 -04003072EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Jan Karae4dd9de2009-08-17 18:10:06 +02003074/**
Al Viro81742022014-04-03 03:17:43 -04003075 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003076 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003077 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003078 *
Al Viro81742022014-04-03 03:17:43 -04003079 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003080 * filesystems. It takes care of syncing the file in case of O_SYNC file
3081 * and acquires i_mutex as needed.
3082 */
Al Viro81742022014-04-03 03:17:43 -04003083ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003086 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Al Viro59551022016-01-22 15:40:57 -05003089 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003090 ret = generic_write_checks(iocb, from);
3091 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003092 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003093 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Christoph Hellwige2592212016-04-07 08:52:01 -07003095 if (ret > 0)
3096 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return ret;
3098}
Al Viro81742022014-04-03 03:17:43 -04003099EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
David Howellscf9a2ae2006-08-29 19:05:54 +01003101/**
3102 * try_to_release_page() - release old fs-specific metadata on a page
3103 *
3104 * @page: the page which the kernel is trying to free
3105 * @gfp_mask: memory allocation flags (and I/O mode)
3106 *
3107 * The address_space is to try to release any data against the page
3108 * (presumably at page->private). If the release was successful, return `1'.
3109 * Otherwise return zero.
3110 *
David Howells266cf652009-04-03 16:42:36 +01003111 * This may also be called if PG_fscache is set on a page, indicating that the
3112 * page is known to the local caching routines.
3113 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003114 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003115 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003116 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003117 */
3118int try_to_release_page(struct page *page, gfp_t gfp_mask)
3119{
3120 struct address_space * const mapping = page->mapping;
3121
3122 BUG_ON(!PageLocked(page));
3123 if (PageWriteback(page))
3124 return 0;
3125
3126 if (mapping && mapping->a_ops->releasepage)
3127 return mapping->a_ops->releasepage(page, gfp_mask);
3128 return try_to_free_buffers(page);
3129}
3130
3131EXPORT_SYMBOL(try_to_release_page);