blob: 4fcd8ee9801420c7ee865dfc7184b103c870ab1d [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>
Nick Piggin0f8053a2006-03-22 00:08:33 -080038#include "internal.h"
39
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070040#define CREATE_TRACE_POINTS
41#include <trace/events/filemap.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * FIXME: remove all knowledge of the buffer layer from the core VM
45 */
Jan Kara148f9482009-08-17 19:52:36 +020046#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/mman.h>
49
Vinayak Menonb9e4cc2b2018-06-11 18:58:39 +053050int want_old_faultaround_pte = 1;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
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 */
Vinayak Menoncd89baf2015-07-13 13:24:55 +0530256 if (PageUptodate(page) && PageMappedToDisk(page)) {
257 count_vm_event(PGPGOUTCLEAN);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600258 cleancache_put_page(page);
Vinayak Menoncd89baf2015-07-13 13:24:55 +0530259 } else {
Dan Magenheimer31677602011-09-21 11:56:28 -0400260 cleancache_invalidate_page(mapping, page);
Vinayak Menoncd89baf2015-07-13 13:24:55 +0530261 }
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600262
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700263 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800264 VM_BUG_ON_PAGE(page_mapped(page), page);
265 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
266 int mapcount;
267
268 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
269 current->comm, page_to_pfn(page));
270 dump_page(page, "still mapped when deleted");
271 dump_stack();
272 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
273
274 mapcount = page_mapcount(page);
275 if (mapping_exiting(mapping) &&
276 page_count(page) >= mapcount + 2) {
277 /*
278 * All vmas have already been torn down, so it's
279 * a good bet that actually the page is unmapped,
280 * and we'd prefer not to leak it: if we're wrong,
281 * some other bad page check should catch it later.
282 */
283 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700284 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800285 }
286 }
287
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700288 page_cache_tree_delete(mapping, page, shadow);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700291 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700292
Michal Hocko4165b9b2015-06-24 16:57:24 -0700293 /* hugetlb pages do not participate in page cache accounting. */
294 if (!PageHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700295 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700296 if (PageSwapBacked(page)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700297 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700298 if (PageTransHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700299 __dec_node_page_state(page, NR_SHMEM_THPS);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700300 } else {
301 VM_BUG_ON_PAGE(PageTransHuge(page) && !PageHuge(page), page);
302 }
Linus Torvalds3a692792007-12-19 14:05:13 -0800303
304 /*
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700305 * At this point page must be either written or cleaned by truncate.
306 * Dirty page here signals a bug and loss of unwritten data.
Linus Torvalds3a692792007-12-19 14:05:13 -0800307 *
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700308 * This fixes dirty accounting after removing the page entirely but
309 * leaves PageDirty set: it has no effect for truncated page and
310 * anyway will be cleared before returning page into buddy allocator.
Linus Torvalds3a692792007-12-19 14:05:13 -0800311 */
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700312 if (WARN_ON_ONCE(PageDirty(page)))
Johannes Weiner62cccb82016-03-15 14:57:22 -0700313 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Minchan Kim702cfbf2011-03-22 16:32:43 -0700316/**
317 * delete_from_page_cache - delete page from page cache
318 * @page: the page which the kernel is trying to remove from page cache
319 *
320 * This must be called only on pages that have been verified to be in the page
321 * cache and locked. It will never put the page into the free list, the caller
322 * has a reference on the page.
323 */
324void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700326 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400327 unsigned long flags;
Linus Torvalds6072d132010-12-01 13:35:19 -0500328 void (*freepage)(struct page *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Matt Mackallcd7619d2005-05-01 08:59:01 -0700330 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds6072d132010-12-01 13:35:19 -0500332 freepage = mapping->a_ops->freepage;
Greg Thelenc4843a72015-05-22 17:13:16 -0400333
Greg Thelenc4843a72015-05-22 17:13:16 -0400334 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700335 __delete_from_page_cache(page, NULL);
Greg Thelenc4843a72015-05-22 17:13:16 -0400336 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500337
338 if (freepage)
339 freepage(page);
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700340
341 if (PageTransHuge(page) && !PageHuge(page)) {
342 page_ref_sub(page, HPAGE_PMD_NR);
343 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
344 } else {
345 put_page(page);
346 }
Minchan Kim97cecb52011-03-22 16:30:53 -0700347}
348EXPORT_SYMBOL(delete_from_page_cache);
349
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200350int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700351{
352 int ret = 0;
353 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700354 if (test_bit(AS_ENOSPC, &mapping->flags) &&
355 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700356 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700357 if (test_bit(AS_EIO, &mapping->flags) &&
358 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700359 ret = -EIO;
360 return ret;
361}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200362EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700365 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700366 * @mapping: address space structure to write
367 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800368 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700369 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700371 * Start writeback against all of a mapping's dirty pages that lie
372 * within the byte offsets <start, end> inclusive.
373 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700375 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * these two operations is that if a dirty page/buffer is encountered, it must
377 * be waited upon, and not just skipped over.
378 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800379int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
380 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 int ret;
383 struct writeback_control wbc = {
384 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800385 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700386 .range_start = start,
387 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 };
389
390 if (!mapping_cap_writeback_dirty(mapping))
391 return 0;
392
Tejun Heob16b1de2015-06-02 08:39:48 -0600393 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600395 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return ret;
397}
398
399static inline int __filemap_fdatawrite(struct address_space *mapping,
400 int sync_mode)
401{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700402 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405int filemap_fdatawrite(struct address_space *mapping)
406{
407 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
408}
409EXPORT_SYMBOL(filemap_fdatawrite);
410
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400411int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800412 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
415}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400416EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Randy Dunlap485bb992006-06-23 02:03:49 -0700418/**
419 * filemap_flush - mostly a non-blocking flush
420 * @mapping: target address_space
421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * This is a mostly non-blocking flush. Not suitable for data-integrity
423 * purposes - I/O may not be started against all dirty pages.
424 */
425int filemap_flush(struct address_space *mapping)
426{
427 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
428}
429EXPORT_SYMBOL(filemap_flush);
430
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800431static int __filemap_fdatawait_range(struct address_space *mapping,
432 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300434 pgoff_t index = start_byte >> PAGE_SHIFT;
435 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct pagevec pvec;
437 int nr_pages;
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800438 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200440 if (end_byte < start_byte)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 pagevec_init(&pvec, 0);
Jan Kara7e95dd52017-11-15 17:35:05 -0800444 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 unsigned i;
446
Jan Kara7e95dd52017-11-15 17:35:05 -0800447 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara4d93a432017-11-15 17:35:19 -0800448 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara7e95dd52017-11-15 17:35:05 -0800449 if (!nr_pages)
450 break;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for (i = 0; i < nr_pages; i++) {
453 struct page *page = pvec.pages[i];
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800456 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ret = -EIO;
458 }
459 pagevec_release(&pvec);
460 cond_resched();
461 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700462out:
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800463 return ret;
464}
465
466/**
467 * filemap_fdatawait_range - wait for writeback to complete
468 * @mapping: address space structure to wait for
469 * @start_byte: offset in bytes where the range starts
470 * @end_byte: offset in bytes where the range ends (inclusive)
471 *
472 * Walk the list of under-writeback pages of the given address space
473 * in the given range and wait for all of them. Check error status of
474 * the address space and return it.
475 *
476 * Since the error status of the address space is cleared by this function,
477 * callers are responsible for checking the return value and handling and/or
478 * reporting the error.
479 */
480int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
481 loff_t end_byte)
482{
483 int ret, ret2;
484
485 ret = __filemap_fdatawait_range(mapping, start_byte, end_byte);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700486 ret2 = filemap_check_errors(mapping);
487 if (!ret)
488 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 return ret;
491}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200492EXPORT_SYMBOL(filemap_fdatawait_range);
493
494/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800495 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
496 * @mapping: address space structure to wait for
497 *
498 * Walk the list of under-writeback pages of the given address space
499 * and wait for all of them. Unlike filemap_fdatawait(), this function
500 * does not clear error status of the address space.
501 *
502 * Use this function if callers don't handle errors themselves. Expected
503 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
504 * fsfreeze(8)
505 */
506void filemap_fdatawait_keep_errors(struct address_space *mapping)
507{
508 loff_t i_size = i_size_read(mapping->host);
509
510 if (i_size == 0)
511 return;
512
513 __filemap_fdatawait_range(mapping, 0, i_size - 1);
514}
515
516/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700517 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700519 *
520 * Walk the list of under-writeback pages of the given address space
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800521 * and wait for all of them. Check error status of the address space
522 * and return it.
523 *
524 * Since the error status of the address space is cleared by this function,
525 * callers are responsible for checking the return value and handling and/or
526 * reporting the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528int filemap_fdatawait(struct address_space *mapping)
529{
530 loff_t i_size = i_size_read(mapping->host);
531
532 if (i_size == 0)
533 return 0;
534
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200535 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537EXPORT_SYMBOL(filemap_fdatawait);
538
539int filemap_write_and_wait(struct address_space *mapping)
540{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800541 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800543 if ((!dax_mapping(mapping) && mapping->nrpages) ||
544 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800545 err = filemap_fdatawrite(mapping);
546 /*
547 * Even if the above returned error, the pages may be
548 * written partially (e.g. -ENOSPC), so we wait for it.
549 * But the -EIO is special case, it may indicate the worst
550 * thing (e.g. bug) happened, so we avoid waiting for it.
551 */
552 if (err != -EIO) {
553 int err2 = filemap_fdatawait(mapping);
554 if (!err)
555 err = err2;
556 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700557 } else {
558 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800562EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Randy Dunlap485bb992006-06-23 02:03:49 -0700564/**
565 * filemap_write_and_wait_range - write out & wait on a file range
566 * @mapping: the address_space for the pages
567 * @lstart: offset in bytes where the range starts
568 * @lend: offset in bytes where the range ends (inclusive)
569 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800570 * Write out and wait upon file offsets lstart->lend, inclusive.
571 *
572 * Note that `lend' is inclusive (describes the last byte to be written) so
573 * that this function can be used to write to the very end-of-file (end = -1).
574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575int filemap_write_and_wait_range(struct address_space *mapping,
576 loff_t lstart, loff_t lend)
577{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800578 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800580 if ((!dax_mapping(mapping) && mapping->nrpages) ||
581 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800582 err = __filemap_fdatawrite_range(mapping, lstart, lend,
583 WB_SYNC_ALL);
584 /* See comment of filemap_write_and_wait() */
585 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200586 int err2 = filemap_fdatawait_range(mapping,
587 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800588 if (!err)
589 err = err2;
590 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700591 } else {
592 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
Chris Masonf6995582009-04-15 13:22:37 -0400596EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Randy Dunlap485bb992006-06-23 02:03:49 -0700598/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700599 * replace_page_cache_page - replace a pagecache page with a new one
600 * @old: page to be replaced
601 * @new: page to replace with
602 * @gfp_mask: allocation mode
603 *
604 * This function replaces a page in the pagecache with a new one. On
605 * success it acquires the pagecache reference for the new page and
606 * drops it for the old page. Both the old and new pages must be
607 * locked. This function does not add the new page to the LRU, the
608 * caller must do that.
609 *
610 * The remove + add is atomic. The only way this function can fail is
611 * memory allocation failure.
612 */
613int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
614{
615 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700616
Sasha Levin309381fea2014-01-23 15:52:54 -0800617 VM_BUG_ON_PAGE(!PageLocked(old), old);
618 VM_BUG_ON_PAGE(!PageLocked(new), new);
619 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700620
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -0700621 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700622 if (!error) {
623 struct address_space *mapping = old->mapping;
624 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400625 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700626
627 pgoff_t offset = old->index;
628 freepage = mapping->a_ops->freepage;
629
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300630 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700631 new->mapping = mapping;
632 new->index = offset;
633
Greg Thelenc4843a72015-05-22 17:13:16 -0400634 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700635 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700636 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700637 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700638
639 /*
640 * hugetlb pages do not participate in page cache accounting.
641 */
642 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700643 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700644 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700645 __inc_node_page_state(new, NR_SHMEM);
Greg Thelenc4843a72015-05-22 17:13:16 -0400646 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700647 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700648 radix_tree_preload_end();
649 if (freepage)
650 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300651 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700652 }
653
654 return error;
655}
656EXPORT_SYMBOL_GPL(replace_page_cache_page);
657
Johannes Weinera5289102014-04-03 14:47:51 -0700658static int __add_to_page_cache_locked(struct page *page,
659 struct address_space *mapping,
660 pgoff_t offset, gfp_t gfp_mask,
661 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Johannes Weiner00501b52014-08-08 14:19:20 -0700663 int huge = PageHuge(page);
664 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700665 int error;
666
Sasha Levin309381fea2014-01-23 15:52:54 -0800667 VM_BUG_ON_PAGE(!PageLocked(page), page);
668 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700669
Johannes Weiner00501b52014-08-08 14:19:20 -0700670 if (!huge) {
671 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800672 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700673 if (error)
674 return error;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -0700677 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700678 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700679 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800680 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700681 return error;
682 }
683
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300684 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700685 page->mapping = mapping;
686 page->index = offset;
687
688 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700689 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700690 radix_tree_preload_end();
691 if (unlikely(error))
692 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700693
694 /* hugetlb pages do not participate in page cache accounting. */
695 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700696 __inc_node_page_state(page, NR_FILE_PAGES);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700697 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700698 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800699 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700700 trace_mm_filemap_add_to_page_cache(page);
701 return 0;
702err_insert:
703 page->mapping = NULL;
704 /* Leave page->index set: truncation relies upon it */
705 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700706 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800707 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300708 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return error;
710}
Johannes Weinera5289102014-04-03 14:47:51 -0700711
712/**
713 * add_to_page_cache_locked - add a locked page to the pagecache
714 * @page: page to add
715 * @mapping: the page's address_space
716 * @offset: page index
717 * @gfp_mask: page allocation mode
718 *
719 * This function is used to add a page to the pagecache. It must be locked.
720 * This function does not add the page to the LRU. The caller must do that.
721 */
722int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
723 pgoff_t offset, gfp_t gfp_mask)
724{
725 return __add_to_page_cache_locked(page, mapping, offset,
726 gfp_mask, NULL);
727}
Nick Piggine2867812008-07-25 19:45:30 -0700728EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400731 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Johannes Weinera5289102014-04-03 14:47:51 -0700733 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700734 int ret;
735
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800736 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700737 ret = __add_to_page_cache_locked(page, mapping, offset,
738 gfp_mask, &shadow);
739 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800740 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700741 else {
742 /*
743 * The page might have been evicted from cache only
744 * recently, in which case it should be activated like
745 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700746 * The exception is pages getting rewritten; evicting other
747 * data from the working set, only to cache data that will
748 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700749 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700750 if (!(gfp_mask & __GFP_WRITE) &&
751 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700752 SetPageActive(page);
753 workingset_activation(page);
754 } else
755 ClearPageActive(page);
756 lru_cache_add(page);
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return ret;
759}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300760EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Paul Jackson44110fe2006-03-24 03:16:04 -0800762#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700763struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800764{
Miao Xiec0ff7452010-05-24 14:32:08 -0700765 int n;
766 struct page *page;
767
Paul Jackson44110fe2006-03-24 03:16:04 -0800768 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700769 unsigned int cpuset_mems_cookie;
770 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700771 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700772 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700773 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700774 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700775
Miao Xiec0ff7452010-05-24 14:32:08 -0700776 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800777 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700778 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800779}
Nick Piggin2ae88142006-10-28 10:38:23 -0700780EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800781#endif
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
784 * In order to wait for pages to become available there must be
785 * waitqueues associated with pages. By using a hash table of
786 * waitqueues where the bucket discipline is to maintain all
787 * waiters on the same queue and wake all when any of the pages
788 * become available, and for the woken contexts to check to be
789 * sure the appropriate page became available, this saves space
790 * at a cost of "thundering herd" phenomena during rare hash
791 * collisions.
792 */
NeilBrowna4796e32014-09-24 11:28:32 +1000793wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Linus Torvalds9dcb8b62016-10-26 10:15:30 -0700795 return bit_waitqueue(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
NeilBrowna4796e32014-09-24 11:28:32 +1000797EXPORT_SYMBOL(page_waitqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Harvey Harrison920c7a52008-02-04 22:29:26 -0800799void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
802
803 if (test_bit(bit_nr, &page->flags))
NeilBrown74316202014-07-07 15:16:04 +1000804 __wait_on_bit(page_waitqueue(page), &wait, bit_wait_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 TASK_UNINTERRUPTIBLE);
806}
807EXPORT_SYMBOL(wait_on_page_bit);
808
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700809int wait_on_page_bit_killable(struct page *page, int bit_nr)
810{
811 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
812
813 if (!test_bit(bit_nr, &page->flags))
814 return 0;
815
816 return __wait_on_bit(page_waitqueue(page), &wait,
NeilBrown74316202014-07-07 15:16:04 +1000817 bit_wait_io, TASK_KILLABLE);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700818}
819
NeilBrowncbbce822014-09-25 13:55:19 +1000820int wait_on_page_bit_killable_timeout(struct page *page,
821 int bit_nr, unsigned long timeout)
822{
823 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
824
825 wait.key.timeout = jiffies + timeout;
826 if (!test_bit(bit_nr, &page->flags))
827 return 0;
828 return __wait_on_bit(page_waitqueue(page), &wait,
829 bit_wait_io_timeout, TASK_KILLABLE);
830}
831EXPORT_SYMBOL_GPL(wait_on_page_bit_killable_timeout);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100834 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700835 * @page: Page defining the wait queue of interest
836 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100837 *
838 * Add an arbitrary @waiter to the wait queue for the nominated @page.
839 */
840void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
841{
842 wait_queue_head_t *q = page_waitqueue(page);
843 unsigned long flags;
844
845 spin_lock_irqsave(&q->lock, flags);
846 __add_wait_queue(q, waiter);
847 spin_unlock_irqrestore(&q->lock, flags);
848}
849EXPORT_SYMBOL_GPL(add_page_wait_queue);
850
851/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700852 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * @page: the page
854 *
855 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
856 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +0900857 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
859 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700860 * The mb is necessary to enforce ordering between the clear_bit and the read
861 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800863void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800865 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800866 VM_BUG_ON_PAGE(!PageLocked(page), page);
Nick Piggin8413ac92008-10-18 20:26:59 -0700867 clear_bit_unlock(PG_locked, &page->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100868 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 wake_up_page(page, PG_locked);
870}
871EXPORT_SYMBOL(unlock_page);
872
Randy Dunlap485bb992006-06-23 02:03:49 -0700873/**
874 * end_page_writeback - end writeback against a page
875 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
877void end_page_writeback(struct page *page)
878{
Mel Gorman888cf2d2014-06-04 16:10:34 -0700879 /*
880 * TestClearPageReclaim could be used here but it is an atomic
881 * operation and overkill in this particular case. Failing to
882 * shuffle a page marked for immediate reclaim is too mild to
883 * justify taking an atomic operation penalty at the end of
884 * ever page writeback.
885 */
886 if (PageReclaim(page)) {
887 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700888 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -0700889 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700890
891 if (!test_clear_page_writeback(page))
892 BUG();
893
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100894 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 wake_up_page(page, PG_writeback);
896}
897EXPORT_SYMBOL(end_page_writeback);
898
Matthew Wilcox57d99842014-06-04 16:07:45 -0700899/*
900 * After completing I/O on a page, call this routine to update the page
901 * flags appropriately
902 */
Jens Axboec11f0c02016-08-05 08:11:04 -0600903void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -0700904{
Jens Axboec11f0c02016-08-05 08:11:04 -0600905 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -0700906 if (!err) {
907 SetPageUptodate(page);
908 } else {
909 ClearPageUptodate(page);
910 SetPageError(page);
911 }
912 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -0600913 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -0700914 if (err) {
Minchan Kim2c290ee2017-02-24 14:59:59 -0800915 struct address_space *mapping;
916
Matthew Wilcox57d99842014-06-04 16:07:45 -0700917 SetPageError(page);
Minchan Kim2c290ee2017-02-24 14:59:59 -0800918 mapping = page_mapping(page);
919 if (mapping)
920 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -0700921 }
922 end_page_writeback(page);
923 }
924}
925EXPORT_SYMBOL_GPL(page_endio);
926
Randy Dunlap485bb992006-06-23 02:03:49 -0700927/**
928 * __lock_page - get a lock on the page, assuming we need to sleep to get it
929 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800931void __lock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800933 struct page *page_head = compound_head(page);
934 DEFINE_WAIT_BIT(wait, &page_head->flags, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800936 __wait_on_bit_lock(page_waitqueue(page_head), &wait, bit_wait_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 TASK_UNINTERRUPTIBLE);
938}
939EXPORT_SYMBOL(__lock_page);
940
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800941int __lock_page_killable(struct page *page)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500942{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800943 struct page *page_head = compound_head(page);
944 DEFINE_WAIT_BIT(wait, &page_head->flags, PG_locked);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500945
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800946 return __wait_on_bit_lock(page_waitqueue(page_head), &wait,
NeilBrown74316202014-07-07 15:16:04 +1000947 bit_wait_io, TASK_KILLABLE);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500948}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300949EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500950
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700951/*
952 * Return values:
953 * 1 - page is locked; mmap_sem is still held.
954 * 0 - page is not locked.
955 * mmap_sem has been released (up_read()), unless flags had both
956 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
957 * which case mmap_sem is still held.
958 *
959 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
960 * with the page locked and the mmap_sem unperturbed.
961 */
Michel Lespinassed065bd82010-10-26 14:21:57 -0700962int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
963 unsigned int flags)
964{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700965 if (flags & FAULT_FLAG_ALLOW_RETRY) {
966 /*
967 * CAUTION! In this case, mmap_sem is not released
968 * even though return 0.
969 */
970 if (flags & FAULT_FLAG_RETRY_NOWAIT)
971 return 0;
972
973 up_read(&mm->mmap_sem);
974 if (flags & FAULT_FLAG_KILLABLE)
975 wait_on_page_locked_killable(page);
976 else
Gleb Natapov318b2752011-03-22 16:30:51 -0700977 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -0700978 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700979 } else {
980 if (flags & FAULT_FLAG_KILLABLE) {
981 int ret;
982
983 ret = __lock_page_killable(page);
984 if (ret) {
985 up_read(&mm->mmap_sem);
986 return 0;
987 }
988 } else
989 __lock_page(page);
990 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -0700991 }
992}
993
Randy Dunlap485bb992006-06-23 02:03:49 -0700994/**
Johannes Weinere7b563b2014-04-03 14:47:44 -0700995 * page_cache_next_hole - find the next hole (not-present entry)
996 * @mapping: mapping
997 * @index: index
998 * @max_scan: maximum range to search
999 *
1000 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1001 * lowest indexed hole.
1002 *
1003 * Returns: the index of the hole if found, otherwise returns an index
1004 * outside of the set specified (in which case 'return - index >=
1005 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1006 * be returned.
1007 *
1008 * page_cache_next_hole may be called under rcu_read_lock. However,
1009 * like radix_tree_gang_lookup, this will not atomically search a
1010 * snapshot of the tree at a single point in time. For example, if a
1011 * hole is created at index 5, then subsequently a hole is created at
1012 * index 10, page_cache_next_hole covering both indexes may return 10
1013 * if called under rcu_read_lock.
1014 */
1015pgoff_t page_cache_next_hole(struct address_space *mapping,
1016 pgoff_t index, unsigned long max_scan)
1017{
1018 unsigned long i;
1019
1020 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001021 struct page *page;
1022
1023 page = radix_tree_lookup(&mapping->page_tree, index);
1024 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001025 break;
1026 index++;
1027 if (index == 0)
1028 break;
1029 }
1030
1031 return index;
1032}
1033EXPORT_SYMBOL(page_cache_next_hole);
1034
1035/**
1036 * page_cache_prev_hole - find the prev hole (not-present entry)
1037 * @mapping: mapping
1038 * @index: index
1039 * @max_scan: maximum range to search
1040 *
1041 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1042 * the first hole.
1043 *
1044 * Returns: the index of the hole if found, otherwise returns an index
1045 * outside of the set specified (in which case 'index - return >=
1046 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1047 * will be returned.
1048 *
1049 * page_cache_prev_hole may be called under rcu_read_lock. However,
1050 * like radix_tree_gang_lookup, this will not atomically search a
1051 * snapshot of the tree at a single point in time. For example, if a
1052 * hole is created at index 10, then subsequently a hole is created at
1053 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1054 * called under rcu_read_lock.
1055 */
1056pgoff_t page_cache_prev_hole(struct address_space *mapping,
1057 pgoff_t index, unsigned long max_scan)
1058{
1059 unsigned long i;
1060
1061 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001062 struct page *page;
1063
1064 page = radix_tree_lookup(&mapping->page_tree, index);
1065 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001066 break;
1067 index--;
1068 if (index == ULONG_MAX)
1069 break;
1070 }
1071
1072 return index;
1073}
1074EXPORT_SYMBOL(page_cache_prev_hole);
1075
1076/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001077 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001078 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001079 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001080 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001081 * Looks up the page cache slot at @mapping & @offset. If there is a
1082 * page cache page, it is returned with an increased refcount.
1083 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001084 * If the slot holds a shadow entry of a previously evicted page, or a
1085 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001086 *
1087 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001089struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Nick Piggina60637c2008-07-25 19:45:31 -07001091 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001092 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Nick Piggina60637c2008-07-25 19:45:31 -07001094 rcu_read_lock();
1095repeat:
1096 page = NULL;
1097 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1098 if (pagep) {
1099 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001100 if (unlikely(!page))
1101 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001102 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001103 if (radix_tree_deref_retry(page))
1104 goto repeat;
1105 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001106 * A shadow entry of a recently evicted page,
1107 * or a swap entry from shmem/tmpfs. Return
1108 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001109 */
1110 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001111 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001112
1113 head = compound_head(page);
1114 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001115 goto repeat;
1116
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001117 /* The page was split under us? */
1118 if (compound_head(page) != head) {
1119 put_page(head);
1120 goto repeat;
1121 }
1122
Nick Piggina60637c2008-07-25 19:45:31 -07001123 /*
1124 * Has the page moved?
1125 * This is part of the lockless pagecache protocol. See
1126 * include/linux/pagemap.h for details.
1127 */
1128 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001129 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001130 goto repeat;
1131 }
1132 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001133out:
Nick Piggina60637c2008-07-25 19:45:31 -07001134 rcu_read_unlock();
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return page;
1137}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001138EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Randy Dunlap485bb992006-06-23 02:03:49 -07001140/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001141 * find_lock_entry - locate, pin and lock a page cache entry
1142 * @mapping: the address_space to search
1143 * @offset: the page cache index
1144 *
1145 * Looks up the page cache slot at @mapping & @offset. If there is a
1146 * page cache page, it is returned locked and with an increased
1147 * refcount.
1148 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001149 * If the slot holds a shadow entry of a previously evicted page, or a
1150 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001151 *
1152 * Otherwise, %NULL is returned.
1153 *
1154 * find_lock_entry() may sleep.
1155 */
1156struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct page *page;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001161 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001162 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001163 lock_page(page);
1164 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001165 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001166 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001167 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001168 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001170 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return page;
1173}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001174EXPORT_SYMBOL(find_lock_entry);
1175
1176/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001177 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001178 * @mapping: the address_space to search
1179 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001180 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001181 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001182 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001183 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001184 *
Randy Dunlap75325182014-07-30 16:08:37 -07001185 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001186 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001187 * FGP_ACCESSED: the page will be marked accessed
1188 * FGP_LOCK: Page is return locked
1189 * FGP_CREAT: If page is not present then a new page is allocated using
Michal Hocko45f87de2014-12-29 20:30:35 +01001190 * @gfp_mask and added to the page cache and the VM's LRU
1191 * list. The page is returned locked and with an increased
1192 * refcount. Otherwise, %NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001193 *
1194 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1195 * if the GFP flags specified for FGP_CREAT are atomic.
1196 *
1197 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001198 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001199struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001200 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Nick Piggineb2be182007-10-16 01:24:57 -07001202 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001205 page = find_get_entry(mapping, offset);
1206 if (radix_tree_exceptional_entry(page))
1207 page = NULL;
1208 if (!page)
1209 goto no_page;
1210
1211 if (fgp_flags & FGP_LOCK) {
1212 if (fgp_flags & FGP_NOWAIT) {
1213 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001214 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001215 return NULL;
1216 }
1217 } else {
1218 lock_page(page);
1219 }
1220
1221 /* Has the page been truncated? */
1222 if (unlikely(page->mapping != mapping)) {
1223 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001224 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001225 goto repeat;
1226 }
1227 VM_BUG_ON_PAGE(page->index != offset, page);
1228 }
1229
1230 if (page && (fgp_flags & FGP_ACCESSED))
1231 mark_page_accessed(page);
1232
1233no_page:
1234 if (!page && (fgp_flags & FGP_CREAT)) {
1235 int err;
1236 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001237 gfp_mask |= __GFP_WRITE;
1238 if (fgp_flags & FGP_NOFS)
1239 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001240
Michal Hocko45f87de2014-12-29 20:30:35 +01001241 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001242 if (!page)
1243 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001244
1245 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1246 fgp_flags |= FGP_LOCK;
1247
Hugh Dickinseb39d612014-08-06 16:06:43 -07001248 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001249 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001250 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001251
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -07001252 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001253 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001254 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001255 page = NULL;
1256 if (err == -EEXIST)
1257 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return page;
1262}
Mel Gorman2457aec2014-06-04 16:10:31 -07001263EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001266 * find_get_entries - gang pagecache lookup
1267 * @mapping: The address_space to search
1268 * @start: The starting page cache index
1269 * @nr_entries: The maximum number of entries
1270 * @entries: Where the resulting entries are placed
1271 * @indices: The cache indices corresponding to the entries in @entries
1272 *
1273 * find_get_entries() will search for and return a group of up to
1274 * @nr_entries entries in the mapping. The entries are placed at
1275 * @entries. find_get_entries() takes a reference against any actual
1276 * pages it returns.
1277 *
1278 * The search returns a group of mapping-contiguous page cache entries
1279 * with ascending indexes. There may be holes in the indices due to
1280 * not-present pages.
1281 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001282 * Any shadow entries of evicted pages, or swap entries from
1283 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001284 *
1285 * find_get_entries() returns the number of pages and shadow entries
1286 * which were found.
1287 */
1288unsigned find_get_entries(struct address_space *mapping,
1289 pgoff_t start, unsigned int nr_entries,
1290 struct page **entries, pgoff_t *indices)
1291{
1292 void **slot;
1293 unsigned int ret = 0;
1294 struct radix_tree_iter iter;
1295
1296 if (!nr_entries)
1297 return 0;
1298
1299 rcu_read_lock();
Johannes Weiner0cd61442014-04-03 14:47:46 -07001300 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001301 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001302repeat:
1303 page = radix_tree_deref_slot(slot);
1304 if (unlikely(!page))
1305 continue;
1306 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001307 if (radix_tree_deref_retry(page)) {
1308 slot = radix_tree_iter_retry(&iter);
1309 continue;
1310 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001311 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001312 * A shadow entry of a recently evicted page, a swap
1313 * entry from shmem/tmpfs or a DAX entry. Return it
1314 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001315 */
1316 goto export;
1317 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001318
1319 head = compound_head(page);
1320 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001321 goto repeat;
1322
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001323 /* The page was split under us? */
1324 if (compound_head(page) != head) {
1325 put_page(head);
1326 goto repeat;
1327 }
1328
Johannes Weiner0cd61442014-04-03 14:47:46 -07001329 /* Has the page moved? */
1330 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001331 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001332 goto repeat;
1333 }
1334export:
1335 indices[ret] = iter.index;
1336 entries[ret] = page;
1337 if (++ret == nr_entries)
1338 break;
1339 }
1340 rcu_read_unlock();
1341 return ret;
1342}
1343
1344/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * find_get_pages - gang pagecache lookup
1346 * @mapping: The address_space to search
1347 * @start: The starting page index
1348 * @nr_pages: The maximum number of pages
1349 * @pages: Where the resulting pages are placed
1350 *
1351 * find_get_pages() will search for and return a group of up to
1352 * @nr_pages pages in the mapping. The pages are placed at @pages.
1353 * find_get_pages() takes a reference against the returned pages.
1354 *
1355 * The search returns a group of mapping-contiguous pages with ascending
1356 * indexes. There may be holes in the indices due to not-present pages.
1357 *
1358 * find_get_pages() returns the number of pages which were found.
1359 */
1360unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1361 unsigned int nr_pages, struct page **pages)
1362{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001363 struct radix_tree_iter iter;
1364 void **slot;
1365 unsigned ret = 0;
1366
1367 if (unlikely(!nr_pages))
1368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Nick Piggina60637c2008-07-25 19:45:31 -07001370 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001371 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001372 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001373repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001374 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001375 if (unlikely(!page))
1376 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001377
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001378 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001379 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001380 slot = radix_tree_iter_retry(&iter);
1381 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001382 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001383 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001384 * A shadow entry of a recently evicted page,
1385 * or a swap entry from shmem/tmpfs. Skip
1386 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001387 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001388 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001389 }
Nick Piggina60637c2008-07-25 19:45:31 -07001390
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001391 head = compound_head(page);
1392 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001393 goto repeat;
1394
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001395 /* The page was split under us? */
1396 if (compound_head(page) != head) {
1397 put_page(head);
1398 goto repeat;
1399 }
1400
Nick Piggina60637c2008-07-25 19:45:31 -07001401 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001402 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001403 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001404 goto repeat;
1405 }
1406
1407 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001408 if (++ret == nr_pages)
1409 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001410 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001411
Nick Piggina60637c2008-07-25 19:45:31 -07001412 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return ret;
1414}
1415
Jens Axboeebf43502006-04-27 08:46:01 +02001416/**
1417 * find_get_pages_contig - gang contiguous pagecache lookup
1418 * @mapping: The address_space to search
1419 * @index: The starting page index
1420 * @nr_pages: The maximum number of pages
1421 * @pages: Where the resulting pages are placed
1422 *
1423 * find_get_pages_contig() works exactly like find_get_pages(), except
1424 * that the returned number of pages are guaranteed to be contiguous.
1425 *
1426 * find_get_pages_contig() returns the number of pages which were found.
1427 */
1428unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1429 unsigned int nr_pages, struct page **pages)
1430{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001431 struct radix_tree_iter iter;
1432 void **slot;
1433 unsigned int ret = 0;
1434
1435 if (unlikely(!nr_pages))
1436 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001437
Nick Piggina60637c2008-07-25 19:45:31 -07001438 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001439 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001440 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001441repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001442 page = radix_tree_deref_slot(slot);
1443 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001444 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001445 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001446
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001447 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001448 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001449 slot = radix_tree_iter_retry(&iter);
1450 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001451 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001452 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001453 * A shadow entry of a recently evicted page,
1454 * or a swap entry from shmem/tmpfs. Stop
1455 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001456 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001457 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001458 }
Nick Piggina60637c2008-07-25 19:45:31 -07001459
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001460 head = compound_head(page);
1461 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001462 goto repeat;
1463
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001464 /* The page was split under us? */
1465 if (compound_head(page) != head) {
1466 put_page(head);
1467 goto repeat;
1468 }
1469
Nick Piggina60637c2008-07-25 19:45:31 -07001470 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001471 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001472 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001473 goto repeat;
1474 }
1475
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001476 /*
1477 * must check mapping and index after taking the ref.
1478 * otherwise we can get both false positives and false
1479 * negatives, which is just confusing to the caller.
1480 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001481 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001482 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001483 break;
1484 }
1485
Nick Piggina60637c2008-07-25 19:45:31 -07001486 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001487 if (++ret == nr_pages)
1488 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001489 }
Nick Piggina60637c2008-07-25 19:45:31 -07001490 rcu_read_unlock();
1491 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001492}
David Howellsef71c152007-05-09 02:33:44 -07001493EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001494
Randy Dunlap485bb992006-06-23 02:03:49 -07001495/**
Jan Karafb296a22017-11-15 17:34:33 -08001496 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001497 * @mapping: the address_space to search
1498 * @index: the starting page index
Jan Karafb296a22017-11-15 17:34:33 -08001499 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001500 * @tag: the tag index
1501 * @nr_pages: the maximum number of pages
1502 * @pages: where the resulting pages are placed
1503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001505 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
Jan Karafb296a22017-11-15 17:34:33 -08001507unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1508 pgoff_t end, int tag, unsigned int nr_pages,
1509 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001511 struct radix_tree_iter iter;
1512 void **slot;
1513 unsigned ret = 0;
1514
1515 if (unlikely(!nr_pages))
1516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Nick Piggina60637c2008-07-25 19:45:31 -07001518 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001519 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1520 &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001521 struct page *head, *page;
Jan Karafb296a22017-11-15 17:34:33 -08001522
1523 if (iter.index > end)
1524 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001525repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001526 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001527 if (unlikely(!page))
1528 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001529
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001530 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001531 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001532 slot = radix_tree_iter_retry(&iter);
1533 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001534 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001535 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001536 * A shadow entry of a recently evicted page.
1537 *
1538 * Those entries should never be tagged, but
1539 * this tree walk is lockless and the tags are
1540 * looked up in bulk, one radix tree node at a
1541 * time, so there is a sizable window for page
1542 * reclaim to evict a page we saw tagged.
1543 *
1544 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001545 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001546 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001547 }
Nick Piggina60637c2008-07-25 19:45:31 -07001548
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001549 head = compound_head(page);
1550 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001551 goto repeat;
1552
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001553 /* The page was split under us? */
1554 if (compound_head(page) != head) {
1555 put_page(head);
1556 goto repeat;
1557 }
1558
Nick Piggina60637c2008-07-25 19:45:31 -07001559 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001560 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001561 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001562 goto repeat;
1563 }
1564
1565 pages[ret] = page;
Jan Karafb296a22017-11-15 17:34:33 -08001566 if (++ret == nr_pages) {
1567 *index = pages[ret - 1]->index + 1;
1568 goto out;
1569 }
Nick Piggina60637c2008-07-25 19:45:31 -07001570 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001571
Jan Karafb296a22017-11-15 17:34:33 -08001572 /*
1573 * We come here when we got at @end. We take care to not overflow the
1574 * index @index as it confuses some of the callers. This breaks the
1575 * iteration when there is page at index -1 but that is already broken
1576 * anyway.
1577 */
1578 if (end == (pgoff_t)-1)
1579 *index = (pgoff_t)-1;
1580 else
1581 *index = end + 1;
1582out:
Nick Piggina60637c2008-07-25 19:45:31 -07001583 rcu_read_unlock();
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return ret;
1586}
Jan Karafb296a22017-11-15 17:34:33 -08001587EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001589/**
1590 * find_get_entries_tag - find and return entries that match @tag
1591 * @mapping: the address_space to search
1592 * @start: the starting page cache index
1593 * @tag: the tag index
1594 * @nr_entries: the maximum number of entries
1595 * @entries: where the resulting entries are placed
1596 * @indices: the cache indices corresponding to the entries in @entries
1597 *
1598 * Like find_get_entries, except we only return entries which are tagged with
1599 * @tag.
1600 */
1601unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1602 int tag, unsigned int nr_entries,
1603 struct page **entries, pgoff_t *indices)
1604{
1605 void **slot;
1606 unsigned int ret = 0;
1607 struct radix_tree_iter iter;
1608
1609 if (!nr_entries)
1610 return 0;
1611
1612 rcu_read_lock();
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001613 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1614 &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001615 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001616repeat:
1617 page = radix_tree_deref_slot(slot);
1618 if (unlikely(!page))
1619 continue;
1620 if (radix_tree_exception(page)) {
1621 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001622 slot = radix_tree_iter_retry(&iter);
1623 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001624 }
1625
1626 /*
1627 * A shadow entry of a recently evicted page, a swap
1628 * entry from shmem/tmpfs or a DAX entry. Return it
1629 * without attempting to raise page count.
1630 */
1631 goto export;
1632 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001633
1634 head = compound_head(page);
1635 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001636 goto repeat;
1637
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001638 /* The page was split under us? */
1639 if (compound_head(page) != head) {
1640 put_page(head);
1641 goto repeat;
1642 }
1643
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001644 /* Has the page moved? */
1645 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001646 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001647 goto repeat;
1648 }
1649export:
1650 indices[ret] = iter.index;
1651 entries[ret] = page;
1652 if (++ret == nr_entries)
1653 break;
1654 }
1655 rcu_read_unlock();
1656 return ret;
1657}
1658EXPORT_SYMBOL(find_get_entries_tag);
1659
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001660/*
1661 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1662 * a _large_ part of the i/o request. Imagine the worst scenario:
1663 *
1664 * ---R__________________________________________B__________
1665 * ^ reading here ^ bad block(assume 4k)
1666 *
1667 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1668 * => failing the whole request => read(R) => read(R+1) =>
1669 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1670 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1671 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1672 *
1673 * It is going insane. Fix it by quickly scaling down the readahead size.
1674 */
1675static void shrink_readahead_size_eio(struct file *filp,
1676 struct file_ra_state *ra)
1677{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001678 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001679}
1680
Randy Dunlap485bb992006-06-23 02:03:49 -07001681/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001682 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001683 * @filp: the file to read
1684 * @ppos: current file position
Al Viro6e58e792014-02-03 17:07:03 -05001685 * @iter: data destination
1686 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001687 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001689 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 *
1691 * This is really ugly. But the goto's actually try to clarify some
1692 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
Al Viro6e58e792014-02-03 17:07:03 -05001694static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
1695 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001697 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001699 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001700 pgoff_t index;
1701 pgoff_t last_index;
1702 pgoff_t prev_index;
1703 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001704 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001705 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Wei Fangc2a97372016-10-07 17:01:52 -07001707 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvalds48466c42016-12-14 12:45:25 -08001708 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07001709 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
1710
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001711 index = *ppos >> PAGE_SHIFT;
1712 prev_index = ra->prev_pos >> PAGE_SHIFT;
1713 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1714 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1715 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 for (;;) {
1718 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001719 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001720 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 unsigned long nr, ret;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724find_page:
Michal Hockob67c7d32017-02-03 13:13:29 -08001725 if (fatal_signal_pending(current)) {
1726 error = -EINTR;
1727 goto out;
1728 }
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001731 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001732 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001733 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001734 index, last_index - index);
1735 page = find_get_page(mapping, index);
1736 if (unlikely(page == NULL))
1737 goto no_cached_page;
1738 }
1739 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001740 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001741 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001742 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001744 if (!PageUptodate(page)) {
Mel Gormanebded022016-03-15 14:55:39 -07001745 /*
1746 * See comment in do_read_cache_page on why
1747 * wait_on_page_locked is used to avoid unnecessarily
1748 * serialisations and why it's safe.
1749 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07001750 error = wait_on_page_locked_killable(page);
1751 if (unlikely(error))
1752 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07001753 if (PageUptodate(page))
1754 goto page_ok;
1755
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001756 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001757 !mapping->a_ops->is_partially_uptodate)
1758 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08001759 /* pipes can't handle partially uptodate pages */
1760 if (unlikely(iter->type & ITER_PIPE))
1761 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001762 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001763 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001764 /* Did it get truncated before we got the lock? */
1765 if (!page->mapping)
1766 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001767 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05001768 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001769 goto page_not_up_to_date_locked;
1770 unlock_page(page);
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001773 /*
1774 * i_size must be checked after we know the page is Uptodate.
1775 *
1776 * Checking i_size after the check allows us to calculate
1777 * the correct value for "nr", which means the zero-filled
1778 * part of the page is not copied back to userspace (unless
1779 * another truncate extends the file - this is desired though).
1780 */
1781
1782 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001783 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001784 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001785 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001786 goto out;
1787 }
1788
1789 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001790 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001791 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001792 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001793 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001794 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001795 goto out;
1796 }
1797 }
1798 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* If users can be writing to this page using arbitrary
1801 * virtual addresses, take care about potential aliasing
1802 * before reading the page on the kernel side.
1803 */
1804 if (mapping_writably_mapped(mapping))
1805 flush_dcache_page(page);
1806
1807 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001808 * When a sequential read accesses a page several times,
1809 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Jan Karaec0f1632007-05-06 14:49:25 -07001811 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 mark_page_accessed(page);
1813 prev_index = index;
1814
1815 /*
1816 * Ok, we have the page, and it's up-to-date, so
1817 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
Al Viro6e58e792014-02-03 17:07:03 -05001819
1820 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001822 index += offset >> PAGE_SHIFT;
1823 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001824 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001826 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001827 written += ret;
1828 if (!iov_iter_count(iter))
1829 goto out;
1830 if (ret < nr) {
1831 error = -EFAULT;
1832 goto out;
1833 }
1834 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836page_not_up_to_date:
1837 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001838 error = lock_page_killable(page);
1839 if (unlikely(error))
1840 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001842page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001843 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!page->mapping) {
1845 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001846 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 continue;
1848 }
1849
1850 /* Did somebody else fill it already? */
1851 if (PageUptodate(page)) {
1852 unlock_page(page);
1853 goto page_ok;
1854 }
1855
1856readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001857 /*
1858 * A previous I/O error may have been due to temporary
1859 * failures, eg. multipath errors.
1860 * PG_error will be set again if readpage fails.
1861 */
1862 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* Start the actual read. The read will unlock the page. */
1864 error = mapping->a_ops->readpage(filp, page);
1865
Zach Brown994fc28c2005-12-15 14:28:17 -08001866 if (unlikely(error)) {
1867 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001868 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001869 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08001870 goto find_page;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08001873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04001876 error = lock_page_killable(page);
1877 if (unlikely(error))
1878 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!PageUptodate(page)) {
1880 if (page->mapping == NULL) {
1881 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001882 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
1884 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001885 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto find_page;
1887 }
1888 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07001889 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04001890 error = -EIO;
1891 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893 unlock_page(page);
1894 }
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto page_ok;
1897
1898readpage_error:
1899 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001900 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto out;
1902
1903no_cached_page:
1904 /*
1905 * Ok, it wasn't cached, so we need to create a new
1906 * page..
1907 */
Nick Piggineb2be182007-10-16 01:24:57 -07001908 page = page_cache_alloc_cold(mapping);
1909 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05001910 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07001911 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Michal Hocko6afdb852015-06-24 16:58:06 -07001913 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08001914 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001916 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001917 if (error == -EEXIST) {
1918 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05001920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto out;
1922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto readpage;
1924 }
1925
1926out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07001927 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001928 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07001929 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001931 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07001932 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05001933 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Randy Dunlap485bb992006-06-23 02:03:49 -07001936/**
Al Viro6abd2322014-04-04 14:20:57 -04001937 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001938 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04001939 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07001940 *
Al Viro6abd2322014-04-04 14:20:57 -04001941 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 * that can use the page cache directly.
1943 */
1944ssize_t
Al Viroed978a82014-03-05 22:53:04 -05001945generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Al Viroed978a82014-03-05 22:53:04 -05001947 struct file *file = iocb->ki_filp;
1948 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07001949 size_t count = iov_iter_count(iter);
1950
1951 if (!count)
1952 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Al Viro2ba48ce2015-04-09 13:52:01 -04001954 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viroed978a82014-03-05 22:53:04 -05001955 struct address_space *mapping = file->f_mapping;
1956 struct inode *inode = mapping->host;
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11001957 struct iov_iter data = *iter;
Badari Pulavarty543ade12006-09-30 23:28:48 -07001958 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 size = i_size_read(inode);
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001961 retval = filemap_write_and_wait_range(mapping, iocb->ki_pos,
1962 iocb->ki_pos + count - 1);
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11001963 if (retval < 0)
1964 goto out;
Al Viroed978a82014-03-05 22:53:04 -05001965
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11001966 file_accessed(file);
1967
1968 retval = mapping->a_ops->direct_IO(iocb, &data);
Al Viroc3a69022016-10-10 13:26:27 -04001969 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001970 iocb->ki_pos += retval;
Al Viroed978a82014-03-05 22:53:04 -05001971 iov_iter_advance(iter, retval);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001972 }
Josef Bacik66f998f2010-05-23 11:00:54 -04001973
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001974 /*
1975 * Btrfs can have a short DIO read if we encounter
1976 * compressed extents, so if there was an error, or if
1977 * we've already read everything we wanted to, or if
1978 * there was a short read because we hit EOF, go ahead
1979 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08001980 * the rest of the read. Buffered reads will not work for
1981 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001982 */
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001983 if (retval < 0 || !iov_iter_count(iter) || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11001984 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001988 retval = do_generic_file_read(file, &iocb->ki_pos, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989out:
1990 return retval;
1991}
Al Viroed978a82014-03-05 22:53:04 -05001992EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001995/**
1996 * page_cache_read - adds requested page to the page cache if not already there
1997 * @file: file to read
1998 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08001999 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002000 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * This adds the requested page to the page cache if it isn't already there,
2002 * and schedules an I/O to read in its contents from disk.
2003 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002004static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002007 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002008 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Zach Brown994fc28c2005-12-15 14:28:17 -08002010 do {
Michal Hockoc20cd452016-01-14 15:20:12 -08002011 page = __page_cache_alloc(gfp_mask|__GFP_COLD);
Zach Brown994fc28c2005-12-15 14:28:17 -08002012 if (!page)
2013 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Matthew Wilcoxf4c86fa2018-04-20 14:56:20 -07002015 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002016 if (ret == 0)
2017 ret = mapping->a_ops->readpage(file, page);
2018 else if (ret == -EEXIST)
2019 ret = 0; /* losing race to add is OK */
2020
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002021 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Zach Brown994fc28c2005-12-15 14:28:17 -08002023 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002024
Zach Brown994fc28c2005-12-15 14:28:17 -08002025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028#define MMAP_LOTSAMISS (100)
2029
Linus Torvaldsef00e082009-06-16 15:31:25 -07002030/*
2031 * Synchronous readahead happens when we don't even find
2032 * a page in the page cache at all.
2033 */
2034static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2035 struct file_ra_state *ra,
2036 struct file *file,
2037 pgoff_t offset)
2038{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002039 struct address_space *mapping = file->f_mapping;
2040
2041 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002042 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002043 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002044 if (!ra->ra_pages)
2045 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002046
Joe Perches64363aa2013-07-08 16:00:18 -07002047 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002048 page_cache_sync_readahead(mapping, ra, file, offset,
2049 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002050 return;
2051 }
2052
Andi Kleen207d04b2011-05-24 17:12:29 -07002053 /* Avoid banging the cache line if not needed */
2054 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002055 ra->mmap_miss++;
2056
2057 /*
2058 * Do we miss much more than hit in this file? If so,
2059 * stop bothering with read-ahead. It will only hurt.
2060 */
2061 if (ra->mmap_miss > MMAP_LOTSAMISS)
2062 return;
2063
Wu Fengguangd30a1102009-06-16 15:31:30 -07002064 /*
2065 * mmap read-around
2066 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002067 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2068 ra->size = ra->ra_pages;
2069 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002070 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002071}
2072
2073/*
2074 * Asynchronous readahead happens when we find the page and PG_readahead,
2075 * so we want to possibly extend the readahead further..
2076 */
2077static void do_async_mmap_readahead(struct vm_area_struct *vma,
2078 struct file_ra_state *ra,
2079 struct file *file,
2080 struct page *page,
2081 pgoff_t offset)
2082{
2083 struct address_space *mapping = file->f_mapping;
2084
2085 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002086 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002087 return;
2088 if (ra->mmap_miss > 0)
2089 ra->mmap_miss--;
2090 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002091 page_cache_async_readahead(mapping, ra, file,
2092 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002093}
2094
Randy Dunlap485bb992006-06-23 02:03:49 -07002095/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002096 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002097 * @vma: vma in which the fault was taken
2098 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002099 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002100 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * mapped memory region to read in file data during a page fault.
2102 *
2103 * The goto's are kind of ugly, but this streamlines the normal case of having
2104 * it in the page cache, and handles the special cases reasonably without
2105 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002106 *
2107 * vma->vm_mm->mmap_sem must be held on entry.
2108 *
2109 * If our return value has VM_FAULT_RETRY set, it's because
2110 * lock_page_or_retry() returned 0.
2111 * The mmap_sem has usually been released in this case.
2112 * See __lock_page_or_retry() for the exception.
2113 *
2114 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2115 * has not been released.
2116 *
2117 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 */
Nick Piggind0217ac2007-07-19 01:47:03 -07002119int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07002122 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct address_space *mapping = file->f_mapping;
2124 struct file_ra_state *ra = &file->f_ra;
2125 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002126 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct page *page;
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07002128 loff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07002129 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002131 size = round_up(i_size_read(inode), PAGE_SIZE);
2132 if (offset >= size >> PAGE_SHIFT)
Linus Torvalds5307cc12007-10-31 09:19:46 -07002133 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002136 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002138 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002139 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002141 * We found the page, so try async readahead before
2142 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002144 do_async_mmap_readahead(vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002145 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002146 /* No page in the page cache at all */
2147 do_sync_mmap_readahead(vma, ra, file, offset);
2148 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07002149 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002150 ret = VM_FAULT_MAJOR;
2151retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002152 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (!page)
2154 goto no_cached_page;
2155 }
2156
Michel Lespinassed88c0922010-11-02 13:05:18 -07002157 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002158 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002159 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002160 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002161
2162 /* Did it get truncated? */
2163 if (unlikely(page->mapping != mapping)) {
2164 unlock_page(page);
2165 put_page(page);
2166 goto retry_find;
2167 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002168 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002171 * We have a locked page in the page cache, now we need to check
2172 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Nick Piggind00806b2007-07-19 01:46:57 -07002174 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto page_not_uptodate;
2176
Linus Torvaldsef00e082009-06-16 15:31:25 -07002177 /*
2178 * Found the page and have a reference on it.
2179 * We must recheck i_size under page lock.
2180 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002181 size = round_up(i_size_read(inode), PAGE_SIZE);
2182 if (unlikely(offset >= size >> PAGE_SHIFT)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002183 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002184 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002185 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002186 }
2187
Nick Piggind0217ac2007-07-19 01:47:03 -07002188 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002189 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191no_cached_page:
2192 /*
2193 * We're only likely to ever get here if MADV_RANDOM is in
2194 * effect.
2195 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002196 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /*
2199 * The page we want has now been added to the page cache.
2200 * In the unlikely event that someone removed it in the
2201 * meantime, we'll just come back here and read it again.
2202 */
2203 if (error >= 0)
2204 goto retry_find;
2205
2206 /*
2207 * An error return from page_cache_read can result if the
2208 * system is low on memory, or a problem occurs while trying
2209 * to schedule I/O.
2210 */
2211 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002212 return VM_FAULT_OOM;
2213 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /*
2217 * Umm, take care of errors if the page isn't up-to-date.
2218 * Try to re-read it _once_. We do this synchronously,
2219 * because there really aren't any performance issues here
2220 * and we need to check for errors.
2221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002223 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002224 if (!error) {
2225 wait_on_page_locked(page);
2226 if (!PageUptodate(page))
2227 error = -EIO;
2228 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002229 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002230
2231 if (!error || error == AOP_TRUNCATED_PAGE)
2232 goto retry_find;
2233
2234 /* Things didn't work out. Return zero to tell the mm layer so. */
2235 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002236 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002237}
2238EXPORT_SYMBOL(filemap_fault);
2239
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002240void filemap_map_pages(struct fault_env *fe,
2241 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002242{
2243 struct radix_tree_iter iter;
2244 void **slot;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002245 struct file *file = fe->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002246 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002247 pgoff_t last_pgoff = start_pgoff;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002248 loff_t size;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002249 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002250
2251 rcu_read_lock();
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002252 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
2253 start_pgoff) {
2254 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002255 break;
2256repeat:
2257 page = radix_tree_deref_slot(slot);
2258 if (unlikely(!page))
2259 goto next;
2260 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002261 if (radix_tree_deref_retry(page)) {
2262 slot = radix_tree_iter_retry(&iter);
2263 continue;
2264 }
2265 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002266 }
2267
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002268 head = compound_head(page);
2269 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002270 goto repeat;
2271
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002272 /* The page was split under us? */
2273 if (compound_head(page) != head) {
2274 put_page(head);
2275 goto repeat;
2276 }
2277
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002278 /* Has the page moved? */
2279 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002280 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002281 goto repeat;
2282 }
2283
2284 if (!PageUptodate(page) ||
2285 PageReadahead(page) ||
2286 PageHWPoison(page))
2287 goto skip;
2288 if (!trylock_page(page))
2289 goto skip;
2290
2291 if (page->mapping != mapping || !PageUptodate(page))
2292 goto unlock;
2293
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002294 size = round_up(i_size_read(mapping->host), PAGE_SIZE);
2295 if (page->index >= size >> PAGE_SHIFT)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002296 goto unlock;
2297
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002298 if (file->f_ra.mmap_miss > 0)
2299 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002300
2301 fe->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2302 if (fe->pte)
2303 fe->pte += iter.index - last_pgoff;
2304 last_pgoff = iter.index;
Vinayak Menonb9e4cc2b2018-06-11 18:58:39 +05302305
2306 if (want_old_faultaround_pte) {
2307 if (fe->address == fe->fault_address)
2308 fe->flags &= ~FAULT_FLAG_PREFAULT_OLD;
2309 else
2310 fe->flags |= FAULT_FLAG_PREFAULT_OLD;
2311 }
2312
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002313 if (alloc_set_pte(fe, NULL, page))
2314 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002315 unlock_page(page);
2316 goto next;
2317unlock:
2318 unlock_page(page);
2319skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002320 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002321next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002322 /* Huge page is mapped? No need to proceed. */
2323 if (pmd_trans_huge(*fe->pmd))
2324 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002325 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002326 break;
2327 }
2328 rcu_read_unlock();
2329}
2330EXPORT_SYMBOL(filemap_map_pages);
2331
Jan Kara4fcf1c62012-06-12 16:20:29 +02002332int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2333{
2334 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002335 struct inode *inode = file_inode(vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002336 int ret = VM_FAULT_LOCKED;
2337
Jan Kara14da9202012-06-12 16:20:37 +02002338 sb_start_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002339 file_update_time(vma->vm_file);
2340 lock_page(page);
2341 if (page->mapping != inode->i_mapping) {
2342 unlock_page(page);
2343 ret = VM_FAULT_NOPAGE;
2344 goto out;
2345 }
Jan Kara14da9202012-06-12 16:20:37 +02002346 /*
2347 * We mark the page dirty already here so that when freeze is in
2348 * progress, we are guaranteed that writeback during freezing will
2349 * see the dirty page and writeprotect it again.
2350 */
2351 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002352 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002353out:
Jan Kara14da9202012-06-12 16:20:37 +02002354 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002355 return ret;
2356}
2357EXPORT_SYMBOL(filemap_page_mkwrite);
2358
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002359const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002360 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002361 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002362 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363};
2364
2365/* This is used for a general mmap of a disk file */
2366
2367int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2368{
2369 struct address_space *mapping = file->f_mapping;
2370
2371 if (!mapping->a_ops->readpage)
2372 return -ENOEXEC;
2373 file_accessed(file);
2374 vma->vm_ops = &generic_file_vm_ops;
2375 return 0;
2376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378/*
2379 * This is for filesystems which do not implement ->writepage.
2380 */
2381int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2382{
2383 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2384 return -EINVAL;
2385 return generic_file_mmap(file, vma);
2386}
2387#else
2388int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2389{
2390 return -ENOSYS;
2391}
2392int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2393{
2394 return -ENOSYS;
2395}
2396#endif /* CONFIG_MMU */
2397
2398EXPORT_SYMBOL(generic_file_mmap);
2399EXPORT_SYMBOL(generic_file_readonly_mmap);
2400
Sasha Levin67f9fd92014-04-03 14:48:18 -07002401static struct page *wait_on_page_read(struct page *page)
2402{
2403 if (!IS_ERR(page)) {
2404 wait_on_page_locked(page);
2405 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002406 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002407 page = ERR_PTR(-EIO);
2408 }
2409 }
2410 return page;
2411}
2412
Mel Gorman32b63522016-03-15 14:55:36 -07002413static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002414 pgoff_t index,
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002415 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002416 void *data,
2417 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Nick Piggineb2be182007-10-16 01:24:57 -07002419 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 int err;
2421repeat:
2422 page = find_get_page(mapping, index);
2423 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002424 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002425 if (!page)
2426 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002427 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002428 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002429 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002430 if (err == -EEXIST)
2431 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return ERR_PTR(err);
2434 }
Mel Gorman32b63522016-03-15 14:55:36 -07002435
2436filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 err = filler(data, page);
2438 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002439 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002440 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Mel Gorman32b63522016-03-15 14:55:36 -07002442
2443 page = wait_on_page_read(page);
2444 if (IS_ERR(page))
2445 return page;
2446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (PageUptodate(page))
2449 goto out;
2450
Mel Gormanebded022016-03-15 14:55:39 -07002451 /*
2452 * Page is not up to date and may be locked due one of the following
2453 * case a: Page is being filled and the page lock is held
2454 * case b: Read/write error clearing the page uptodate status
2455 * case c: Truncation in progress (page locked)
2456 * case d: Reclaim in progress
2457 *
2458 * Case a, the page will be up to date when the page is unlocked.
2459 * There is no need to serialise on the page lock here as the page
2460 * is pinned so the lock gives no additional protection. Even if the
2461 * the page is truncated, the data is still valid if PageUptodate as
2462 * it's a race vs truncate race.
2463 * Case b, the page will not be up to date
2464 * Case c, the page may be truncated but in itself, the data may still
2465 * be valid after IO completes as it's a read vs truncate race. The
2466 * operation must restart if the page is not uptodate on unlock but
2467 * otherwise serialising on page lock to stabilise the mapping gives
2468 * no additional guarantees to the caller as the page lock is
2469 * released before return.
2470 * Case d, similar to truncation. If reclaim holds the page lock, it
2471 * will be a race with remove_mapping that determines if the mapping
2472 * is valid on unlock but otherwise the data is valid and there is
2473 * no need to serialise with page lock.
2474 *
2475 * As the page lock gives no additional guarantee, we optimistically
2476 * wait on the page to be unlocked and check if it's up to date and
2477 * use the page if it is. Otherwise, the page lock is required to
2478 * distinguish between the different cases. The motivation is that we
2479 * avoid spurious serialisations and wakeups when multiple processes
2480 * wait on the same page for IO to complete.
2481 */
2482 wait_on_page_locked(page);
2483 if (PageUptodate(page))
2484 goto out;
2485
2486 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002488
2489 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (!page->mapping) {
2491 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002492 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002493 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Mel Gormanebded022016-03-15 14:55:39 -07002495
2496 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (PageUptodate(page)) {
2498 unlock_page(page);
2499 goto out;
2500 }
Mel Gorman32b63522016-03-15 14:55:36 -07002501 goto filler;
2502
David Howellsc855ff32007-05-09 13:42:20 +01002503out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002504 mark_page_accessed(page);
2505 return page;
2506}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002507
2508/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002509 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002510 * @mapping: the page's address_space
2511 * @index: the page index
2512 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002513 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002514 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002515 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002516 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002517 *
2518 * If the page does not get brought uptodate, return -EIO.
2519 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002520struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002521 pgoff_t index,
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002522 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002523 void *data)
2524{
2525 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2526}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002527EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002528
2529/**
2530 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2531 * @mapping: the page's address_space
2532 * @index: the page index
2533 * @gfp: the page allocator flags to use if allocating
2534 *
2535 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002536 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002537 *
2538 * If the page does not get brought uptodate, return -EIO.
2539 */
2540struct page *read_cache_page_gfp(struct address_space *mapping,
2541 pgoff_t index,
2542 gfp_t gfp)
2543{
Sami Tolvanen4fd840d2017-08-16 14:38:13 -07002544 filler_t *filler = mapping->a_ops->readpage;
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002545
Sasha Levin67f9fd92014-04-03 14:48:18 -07002546 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002547}
2548EXPORT_SYMBOL(read_cache_page_gfp);
2549
Nick Piggin2f718ff2007-10-16 01:24:59 -07002550/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * Performs necessary checks before doing a write
2552 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002553 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 * Returns appropriate error code that caller should return or
2555 * zero in case that write should be allowed.
2556 */
Al Viro3309dd02015-04-09 12:55:47 -04002557inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Al Viro3309dd02015-04-09 12:55:47 -04002559 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002561 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002562 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Al Viro3309dd02015-04-09 12:55:47 -04002564 if (!iov_iter_count(from))
2565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Al Viro0fa6b002015-04-04 04:05:48 -04002567 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002568 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002569 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Al Viro3309dd02015-04-09 12:55:47 -04002571 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Al Viro0fa6b002015-04-04 04:05:48 -04002573 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002574 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002575 send_sig(SIGXFSZ, current, 0);
2576 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Al Viro3309dd02015-04-09 12:55:47 -04002578 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580
2581 /*
2582 * LFS rule
2583 */
Al Viro3309dd02015-04-09 12:55:47 -04002584 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002586 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002588 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590
2591 /*
2592 * Are we about to exceed the fs block limit ?
2593 *
2594 * If we have written data it becomes a short write. If we have
2595 * exceeded without writing data we send a signal and return EFBIG.
2596 * Linus frestrict idea will clean these up nicely..
2597 */
Al Viro3309dd02015-04-09 12:55:47 -04002598 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2599 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Al Viro3309dd02015-04-09 12:55:47 -04002601 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2602 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604EXPORT_SYMBOL(generic_write_checks);
2605
Nick Pigginafddba42007-10-16 01:25:01 -07002606int pagecache_write_begin(struct file *file, struct address_space *mapping,
2607 loff_t pos, unsigned len, unsigned flags,
2608 struct page **pagep, void **fsdata)
2609{
2610 const struct address_space_operations *aops = mapping->a_ops;
2611
Nick Piggin4e02ed42008-10-29 14:00:55 -07002612 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002613 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002614}
2615EXPORT_SYMBOL(pagecache_write_begin);
2616
2617int pagecache_write_end(struct file *file, struct address_space *mapping,
2618 loff_t pos, unsigned len, unsigned copied,
2619 struct page *page, void *fsdata)
2620{
2621 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002622
Nick Piggin4e02ed42008-10-29 14:00:55 -07002623 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002624}
2625EXPORT_SYMBOL(pagecache_write_end);
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002628generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 struct file *file = iocb->ki_filp;
2631 struct address_space *mapping = file->f_mapping;
2632 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002633 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002635 size_t write_len;
2636 pgoff_t end;
Al Viro26978b82014-03-10 14:08:45 -04002637 struct iov_iter data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Al Viro0c949332014-03-22 06:51:37 -04002639 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002640 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002641
Nick Piggin48b47c52009-01-06 14:40:22 -08002642 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002643 if (written)
2644 goto out;
2645
2646 /*
2647 * After a write we want buffered reads to be sure to go to disk to get
2648 * the new data. We invalidate clean cached page from the region we're
2649 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002650 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002651 */
2652 if (mapping->nrpages) {
2653 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002654 pos >> PAGE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002655 /*
2656 * If a page can not be invalidated, return 0 to fall back
2657 * to buffered write.
2658 */
2659 if (written) {
2660 if (written == -EBUSY)
2661 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002662 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002663 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002664 }
2665
Al Viro26978b82014-03-10 14:08:45 -04002666 data = *from;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002667 written = mapping->a_ops->direct_IO(iocb, &data);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002668
2669 /*
2670 * Finally, try again to invalidate clean pages which might have been
2671 * cached by non-direct readahead, or faulted in by get_user_pages()
2672 * if the source of the write was an mmap'ed region of the file
2673 * we're writing. Either one is a pretty crazy thing to do,
2674 * so we don't support it 100%. If this invalidation
2675 * fails, tough, the write still worked...
2676 */
2677 if (mapping->nrpages) {
2678 invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002679 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002680 }
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002683 pos += written;
Al Virof8579f82014-03-03 22:03:20 -05002684 iov_iter_advance(from, written);
Namhyung Kim01166512010-10-26 14:21:58 -07002685 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2686 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 mark_inode_dirty(inode);
2688 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002689 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002691out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return written;
2693}
2694EXPORT_SYMBOL(generic_file_direct_write);
2695
Nick Piggineb2be182007-10-16 01:24:57 -07002696/*
2697 * Find or create a page at the given pagecache position. Return the locked
2698 * page. This function is specifically for buffered writes.
2699 */
Nick Piggin54566b22009-01-04 12:00:53 -08002700struct page *grab_cache_page_write_begin(struct address_space *mapping,
2701 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002702{
Nick Piggineb2be182007-10-16 01:24:57 -07002703 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07002704 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002705
Nick Piggin54566b22009-01-04 12:00:53 -08002706 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002707 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002708
Mel Gorman2457aec2014-06-04 16:10:31 -07002709 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002710 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002711 if (page)
2712 wait_for_stable_page(page);
2713
Nick Piggineb2be182007-10-16 01:24:57 -07002714 return page;
2715}
Nick Piggin54566b22009-01-04 12:00:53 -08002716EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002717
Al Viro3b93f912014-02-11 21:34:08 -05002718ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07002719 struct iov_iter *i, loff_t pos)
2720{
2721 struct address_space *mapping = file->f_mapping;
2722 const struct address_space_operations *a_ops = mapping->a_ops;
2723 long status = 0;
2724 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002725 unsigned int flags = 0;
2726
2727 /*
2728 * Copies from kernel address space cannot fail (NFSD is a big user).
2729 */
Al Viro777eda22014-12-17 04:46:46 -05002730 if (!iter_is_iovec(i))
Nick Piggin674b8922007-10-16 01:25:03 -07002731 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002732
2733 do {
2734 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002735 unsigned long offset; /* Offset into pagecache page */
2736 unsigned long bytes; /* Bytes to write to page */
2737 size_t copied; /* Bytes copied from user */
2738 void *fsdata;
2739
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002740 offset = (pos & (PAGE_SIZE - 1));
2741 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002742 iov_iter_count(i));
2743
2744again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01002745 /*
2746 * Bring in the user page that we will copy from _first_.
2747 * Otherwise there's a nasty deadlock on copying from the
2748 * same page as we're writing to, without it being marked
2749 * up-to-date.
2750 *
2751 * Not only is this an optimisation, but it is also required
2752 * to check that the address is actually valid, when atomic
2753 * usercopies are used, below.
2754 */
2755 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2756 status = -EFAULT;
2757 break;
2758 }
2759
Jan Kara296291c2015-10-22 13:32:21 -07002760 if (fatal_signal_pending(current)) {
2761 status = -EINTR;
2762 break;
2763 }
2764
Nick Piggin674b8922007-10-16 01:25:03 -07002765 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002766 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07002767 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07002768 break;
2769
anfei zhou931e80e2010-02-02 13:44:02 -08002770 if (mapping_writably_mapped(mapping))
2771 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01002772
Nick Pigginafddba42007-10-16 01:25:01 -07002773 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07002774 flush_dcache_page(page);
2775
2776 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2777 page, fsdata);
2778 if (unlikely(status < 0))
2779 break;
2780 copied = status;
2781
2782 cond_resched();
2783
Nick Piggin124d3b72008-02-02 15:01:17 +01002784 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002785 if (unlikely(copied == 0)) {
2786 /*
2787 * If we were unable to copy any data at all, we must
2788 * fall back to a single segment length write.
2789 *
2790 * If we didn't fallback here, we could livelock
2791 * because not all segments in the iov can be copied at
2792 * once without a pagefault.
2793 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002794 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002795 iov_iter_single_seg_count(i));
2796 goto again;
2797 }
Nick Pigginafddba42007-10-16 01:25:01 -07002798 pos += copied;
2799 written += copied;
2800
2801 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07002802 } while (iov_iter_count(i));
2803
2804 return written ? written : status;
2805}
Al Viro3b93f912014-02-11 21:34:08 -05002806EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Jan Karae4dd9de2009-08-17 18:10:06 +02002808/**
Al Viro81742022014-04-03 03:17:43 -04002809 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002810 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04002811 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002812 *
2813 * This function does all the work needed for actually writing data to a
2814 * file. It does all basic checks, removes SUID from the file, updates
2815 * modification times and calls proper subroutines depending on whether we
2816 * do direct IO or a standard buffered write.
2817 *
2818 * It expects i_mutex to be grabbed unless we work on a block device or similar
2819 * object which does not need locking at all.
2820 *
2821 * This function does *not* take care of syncing data in case of O_SYNC write.
2822 * A caller has to handle it. This is mainly due to the fact that we want to
2823 * avoid syncing under i_mutex.
2824 */
Al Viro81742022014-04-03 03:17:43 -04002825ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002828 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05002830 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05002832 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01002835 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02002836 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (err)
2838 goto out;
2839
Josef Bacikc3b2da32012-03-26 09:59:21 -04002840 err = file_update_time(file);
2841 if (err)
2842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Al Viro2ba48ce2015-04-09 13:52:01 -04002844 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04002845 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002846
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002847 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002848 /*
2849 * If the write stopped short of completing, fall back to
2850 * buffered writes. Some filesystems do this for writes to
2851 * holes, for example. For DAX files, a buffered write will
2852 * not succeed (even if it did, DAX does not handle dirty
2853 * page-cache pages correctly).
2854 */
Al Viro0b8def92015-04-07 10:22:53 -04002855 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05002857
Al Viro0b8def92015-04-07 10:22:53 -04002858 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002859 /*
Al Viro3b93f912014-02-11 21:34:08 -05002860 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002861 * then we want to return the number of bytes which were
2862 * direct-written, or the error code if that was zero. Note
2863 * that this differs from normal direct-io semantics, which
2864 * will return -EFOO even if some bytes were written.
2865 */
Al Viro60bb4522014-08-08 12:39:16 -04002866 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05002867 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002868 goto out;
2869 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002870 /*
2871 * We need to ensure that the page cache pages are written to
2872 * disk and invalidated to preserve the expected O_DIRECT
2873 * semantics.
2874 */
Al Viro3b93f912014-02-11 21:34:08 -05002875 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04002876 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002877 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04002878 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05002879 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002880 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002881 pos >> PAGE_SHIFT,
2882 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002883 } else {
2884 /*
2885 * We don't know how much we wrote, so just return
2886 * the number of bytes which were direct-written
2887 */
2888 }
2889 } else {
Al Viro0b8def92015-04-07 10:22:53 -04002890 written = generic_perform_write(file, from, iocb->ki_pos);
2891 if (likely(written > 0))
2892 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894out:
2895 current->backing_dev_info = NULL;
2896 return written ? written : err;
2897}
Al Viro81742022014-04-03 03:17:43 -04002898EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Jan Karae4dd9de2009-08-17 18:10:06 +02002900/**
Al Viro81742022014-04-03 03:17:43 -04002901 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002902 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04002903 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002904 *
Al Viro81742022014-04-03 03:17:43 -04002905 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02002906 * filesystems. It takes care of syncing the file in case of O_SYNC file
2907 * and acquires i_mutex as needed.
2908 */
Al Viro81742022014-04-03 03:17:43 -04002909ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02002912 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Al Viro59551022016-01-22 15:40:57 -05002915 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04002916 ret = generic_write_checks(iocb, from);
2917 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04002918 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05002919 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Christoph Hellwige2592212016-04-07 08:52:01 -07002921 if (ret > 0)
2922 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return ret;
2924}
Al Viro81742022014-04-03 03:17:43 -04002925EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
David Howellscf9a2ae2006-08-29 19:05:54 +01002927/**
2928 * try_to_release_page() - release old fs-specific metadata on a page
2929 *
2930 * @page: the page which the kernel is trying to free
2931 * @gfp_mask: memory allocation flags (and I/O mode)
2932 *
2933 * The address_space is to try to release any data against the page
2934 * (presumably at page->private). If the release was successful, return `1'.
2935 * Otherwise return zero.
2936 *
David Howells266cf652009-04-03 16:42:36 +01002937 * This may also be called if PG_fscache is set on a page, indicating that the
2938 * page is known to the local caching routines.
2939 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002940 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08002941 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01002942 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002943 */
2944int try_to_release_page(struct page *page, gfp_t gfp_mask)
2945{
2946 struct address_space * const mapping = page->mapping;
2947
2948 BUG_ON(!PageLocked(page));
2949 if (PageWriteback(page))
2950 return 0;
2951
2952 if (mapping && mapping->a_ops->releasepage)
2953 return mapping->a_ops->releasepage(page, gfp_mask);
2954 return try_to_free_buffers(page);
2955}
2956
2957EXPORT_SYMBOL(try_to_release_page);