blob: c5af80c43d367305fc86ed7f4719e0e28a5ed146 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070017#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/mman.h>
24#include <linux/pagemap.h>
25#include <linux/file.h>
26#include <linux/uio.h>
Josef Bacikcfcbfb12019-05-13 17:21:04 -070027#include <linux/error-injection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/hash.h>
29#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070030#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/pagevec.h>
32#include <linux/blkdev.h>
33#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080034#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070035#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080036#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060037#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080038#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070039#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070040#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070041#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080042#include "internal.h"
43
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070044#define CREATE_TRACE_POINTS
45#include <trace/events/filemap.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * FIXME: remove all knowledge of the buffer layer from the core VM
49 */
Jan Kara148f9482009-08-17 19:52:36 +020050#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/mman.h>
53
54/*
55 * Shared mappings implemented 30.11.1994. It's not fully working yet,
56 * though.
57 *
58 * Shared mappings now work. 15.8.1995 Bruno.
59 *
60 * finished 'unifying' the page and buffer cache and SMP-threaded the
61 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
62 *
63 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
64 */
65
66/*
67 * Lock ordering:
68 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080069 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070071 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070072 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080074 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080075 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080078 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070079 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070080 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * ->mmap_sem
83 * ->lock_page (access_process_vm)
84 *
Al Viroccad2362014-02-11 22:36:48 -050085 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070086 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060088 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110089 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070090 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080092 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * ->anon_vma.lock (vma_adjust)
94 *
95 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070096 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070098 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070099 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700101 * ->i_pages lock (try_to_unmap_one)
Andrey Ryabininf4b7e272019-03-05 15:49:39 -0800102 * ->pgdat->lru_lock (follow_page->mark_page_accessed)
103 * ->pgdat->lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700105 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600106 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100107 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700108 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600109 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100110 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
112 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800113 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700114 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500117static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700118 struct page *page, void *shadow)
119{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500120 XA_STATE(xas, &mapping->i_pages, page->index);
121 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800122
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500123 mapping_set_update(&xas, mapping);
124
125 /* hugetlb pages are represented by a single entry in the xarray */
126 if (!PageHuge(page)) {
127 xas_set_order(&xas, page->index, compound_order(page));
128 nr = 1U << compound_order(page);
129 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700130
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700131 VM_BUG_ON_PAGE(!PageLocked(page), page);
132 VM_BUG_ON_PAGE(PageTail(page), page);
133 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700134
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500135 xas_store(&xas, shadow);
136 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200137
Jan Kara23006382017-11-15 17:37:26 -0800138 page->mapping = NULL;
139 /* Leave page->index set: truncation lookup relies upon it */
140
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200141 if (shadow) {
142 mapping->nrexceptional += nr;
143 /*
144 * Make sure the nrexceptional update is committed before
145 * the nrpages update so that final truncate racing
146 * with reclaim does not see both counters 0 at the
147 * same time and miss a shadow entry.
148 */
149 smp_wmb();
150 }
151 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700152}
153
Jan Kara5ecc4d82017-11-15 17:37:29 -0800154static void unaccount_page_cache_page(struct address_space *mapping,
155 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800157 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600159 /*
160 * if we're uptodate, flush out into the cleancache, otherwise
161 * invalidate any existing cleancache entries. We can't leave
162 * stale data around in the cleancache once our page is gone
163 */
164 if (PageUptodate(page) && PageMappedToDisk(page))
165 cleancache_put_page(page);
166 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400167 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600168
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700169 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800170 VM_BUG_ON_PAGE(page_mapped(page), page);
171 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
172 int mapcount;
173
174 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
175 current->comm, page_to_pfn(page));
176 dump_page(page, "still mapped when deleted");
177 dump_stack();
178 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
179
180 mapcount = page_mapcount(page);
181 if (mapping_exiting(mapping) &&
182 page_count(page) >= mapcount + 2) {
183 /*
184 * All vmas have already been torn down, so it's
185 * a good bet that actually the page is unmapped,
186 * and we'd prefer not to leak it: if we're wrong,
187 * some other bad page check should catch it later.
188 */
189 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700190 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800191 }
192 }
193
Jan Kara76253fb2017-11-15 17:37:22 -0800194 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800195 if (PageHuge(page))
196 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800197
Jan Kara5ecc4d82017-11-15 17:37:29 -0800198 nr = hpage_nr_pages(page);
199
200 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
201 if (PageSwapBacked(page)) {
202 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
203 if (PageTransHuge(page))
204 __dec_node_page_state(page, NR_SHMEM_THPS);
205 } else {
206 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800207 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800208
209 /*
210 * At this point page must be either written or cleaned by
211 * truncate. Dirty page here signals a bug and loss of
212 * unwritten data.
213 *
214 * This fixes dirty accounting after removing the page entirely
215 * but leaves PageDirty set: it has no effect for truncated
216 * page and anyway will be cleared before returning page into
217 * buddy allocator.
218 */
219 if (WARN_ON_ONCE(PageDirty(page)))
220 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
221}
222
223/*
224 * Delete a page from the page cache and free it. Caller has to make
225 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700226 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800227 */
228void __delete_from_page_cache(struct page *page, void *shadow)
229{
230 struct address_space *mapping = page->mapping;
231
232 trace_mm_filemap_delete_from_page_cache(page);
233
234 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500235 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Jan Kara59c66c52017-11-15 17:37:18 -0800238static void page_cache_free_page(struct address_space *mapping,
239 struct page *page)
240{
241 void (*freepage)(struct page *);
242
243 freepage = mapping->a_ops->freepage;
244 if (freepage)
245 freepage(page);
246
247 if (PageTransHuge(page) && !PageHuge(page)) {
248 page_ref_sub(page, HPAGE_PMD_NR);
249 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
250 } else {
251 put_page(page);
252 }
253}
254
Minchan Kim702cfbf2011-03-22 16:32:43 -0700255/**
256 * delete_from_page_cache - delete page from page cache
257 * @page: the page which the kernel is trying to remove from page cache
258 *
259 * This must be called only on pages that have been verified to be in the page
260 * cache and locked. It will never put the page into the free list, the caller
261 * has a reference on the page.
262 */
263void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700265 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400266 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Matt Mackallcd7619d2005-05-01 08:59:01 -0700268 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700269 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700270 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700271 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500272
Jan Kara59c66c52017-11-15 17:37:18 -0800273 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700274}
275EXPORT_SYMBOL(delete_from_page_cache);
276
Jan Karaaa65c292017-11-15 17:37:33 -0800277/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500278 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800279 * @mapping: the mapping to which pages belong
280 * @pvec: pagevec with pages to delete
281 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700282 * The function walks over mapping->i_pages and removes pages passed in @pvec
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700283 * from the mapping. The function expects @pvec to be sorted by page index
284 * and is optimised for it to be dense.
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700285 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800286 * modified). The function expects only THP head pages to be present in the
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700287 * @pvec.
Jan Karaaa65c292017-11-15 17:37:33 -0800288 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700289 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800290 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500291static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800292 struct pagevec *pvec)
293{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500294 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800295 int total_pages = 0;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700296 int i = 0;
Jan Karaaa65c292017-11-15 17:37:33 -0800297 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800298
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500299 mapping_set_update(&xas, mapping);
300 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700301 if (i >= pagevec_count(pvec))
Jan Karaaa65c292017-11-15 17:37:33 -0800302 break;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700303
304 /* A swap/dax/shadow entry got inserted? Skip it. */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400305 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800306 continue;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700307 /*
308 * A page got inserted in our range? Skip it. We have our
309 * pages locked so they are protected from being removed.
310 * If we see a page whose index is higher than ours, it
311 * means our page has been removed, which shouldn't be
312 * possible because we're holding the PageLock.
313 */
314 if (page != pvec->pages[i]) {
315 VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index,
316 page);
317 continue;
Jan Karaaa65c292017-11-15 17:37:33 -0800318 }
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700319
320 WARN_ON_ONCE(!PageLocked(page));
321
322 if (page->index == xas.xa_index)
323 page->mapping = NULL;
324 /* Leave page->index set: truncation lookup relies on it */
325
326 /*
327 * Move to the next page in the vector if this is a regular
328 * page or the index is of the last sub-page of this compound
329 * page.
330 */
331 if (page->index + (1UL << compound_order(page)) - 1 ==
332 xas.xa_index)
333 i++;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500334 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800335 total_pages++;
336 }
337 mapping->nrpages -= total_pages;
338}
339
340void delete_from_page_cache_batch(struct address_space *mapping,
341 struct pagevec *pvec)
342{
343 int i;
344 unsigned long flags;
345
346 if (!pagevec_count(pvec))
347 return;
348
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700349 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800350 for (i = 0; i < pagevec_count(pvec); i++) {
351 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
352
353 unaccount_page_cache_page(mapping, pvec->pages[i]);
354 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500355 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700356 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800357
358 for (i = 0; i < pagevec_count(pvec); i++)
359 page_cache_free_page(mapping, pvec->pages[i]);
360}
361
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200362int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700363{
364 int ret = 0;
365 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700366 if (test_bit(AS_ENOSPC, &mapping->flags) &&
367 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700368 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700369 if (test_bit(AS_EIO, &mapping->flags) &&
370 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700371 ret = -EIO;
372 return ret;
373}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200374EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700375
Jeff Layton76341ca2017-07-06 07:02:22 -0400376static int filemap_check_and_keep_errors(struct address_space *mapping)
377{
378 /* Check for outstanding write errors */
379 if (test_bit(AS_EIO, &mapping->flags))
380 return -EIO;
381 if (test_bit(AS_ENOSPC, &mapping->flags))
382 return -ENOSPC;
383 return 0;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700387 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700388 * @mapping: address space structure to write
389 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800390 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700391 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700393 * Start writeback against all of a mapping's dirty pages that lie
394 * within the byte offsets <start, end> inclusive.
395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700397 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * these two operations is that if a dirty page/buffer is encountered, it must
399 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800400 *
401 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800403int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
404 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 int ret;
407 struct writeback_control wbc = {
408 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800409 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700410 .range_start = start,
411 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 };
413
414 if (!mapping_cap_writeback_dirty(mapping))
415 return 0;
416
Tejun Heob16b1de2015-06-02 08:39:48 -0600417 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600419 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return ret;
421}
422
423static inline int __filemap_fdatawrite(struct address_space *mapping,
424 int sync_mode)
425{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700426 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429int filemap_fdatawrite(struct address_space *mapping)
430{
431 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
432}
433EXPORT_SYMBOL(filemap_fdatawrite);
434
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400435int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800436 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
439}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400440EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Randy Dunlap485bb992006-06-23 02:03:49 -0700442/**
443 * filemap_flush - mostly a non-blocking flush
444 * @mapping: target address_space
445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * This is a mostly non-blocking flush. Not suitable for data-integrity
447 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800448 *
449 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451int filemap_flush(struct address_space *mapping)
452{
453 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
454}
455EXPORT_SYMBOL(filemap_flush);
456
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500457/**
458 * filemap_range_has_page - check if a page exists in range.
459 * @mapping: address space within which to check
460 * @start_byte: offset in bytes where the range starts
461 * @end_byte: offset in bytes where the range ends (inclusive)
462 *
463 * Find at least one page in the range supplied, usually used to check if
464 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800465 *
466 * Return: %true if at least one page exists in the specified range,
467 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500468 */
469bool filemap_range_has_page(struct address_space *mapping,
470 loff_t start_byte, loff_t end_byte)
471{
Jan Karaf7b68042017-09-06 16:21:40 -0700472 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500473 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
474 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500475
476 if (end_byte < start_byte)
477 return false;
478
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500479 rcu_read_lock();
480 for (;;) {
481 page = xas_find(&xas, max);
482 if (xas_retry(&xas, page))
483 continue;
484 /* Shadow entries don't count */
485 if (xa_is_value(page))
486 continue;
487 /*
488 * We don't need to try to pin this page; we're about to
489 * release the RCU lock anyway. It is enough to know that
490 * there was a page here recently.
491 */
492 break;
493 }
494 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500495
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500496 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500497}
498EXPORT_SYMBOL(filemap_range_has_page);
499
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400500static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800501 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300503 pgoff_t index = start_byte >> PAGE_SHIFT;
504 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct pagevec pvec;
506 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200508 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mel Gorman86679822017-11-15 17:37:52 -0800511 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800512 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned i;
514
Jan Kara312e9d22017-11-15 17:35:05 -0800515 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800516 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800517 if (!nr_pages)
518 break;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 for (i = 0; i < nr_pages; i++) {
521 struct page *page = pvec.pages[i];
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400524 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 pagevec_release(&pvec);
527 cond_resched();
528 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800529}
530
531/**
532 * filemap_fdatawait_range - wait for writeback to complete
533 * @mapping: address space structure to wait for
534 * @start_byte: offset in bytes where the range starts
535 * @end_byte: offset in bytes where the range ends (inclusive)
536 *
537 * Walk the list of under-writeback pages of the given address space
538 * in the given range and wait for all of them. Check error status of
539 * the address space and return it.
540 *
541 * Since the error status of the address space is cleared by this function,
542 * callers are responsible for checking the return value and handling and/or
543 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800544 *
545 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800546 */
547int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
548 loff_t end_byte)
549{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400550 __filemap_fdatawait_range(mapping, start_byte, end_byte);
551 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200553EXPORT_SYMBOL(filemap_fdatawait_range);
554
555/**
Jeff Laytona823e452017-07-28 07:24:43 -0400556 * file_fdatawait_range - wait for writeback to complete
557 * @file: file pointing to address space structure to wait for
558 * @start_byte: offset in bytes where the range starts
559 * @end_byte: offset in bytes where the range ends (inclusive)
560 *
561 * Walk the list of under-writeback pages of the address space that file
562 * refers to, in the given range and wait for all of them. Check error
563 * status of the address space vs. the file->f_wb_err cursor and return it.
564 *
565 * Since the error status of the file is advanced by this function,
566 * callers are responsible for checking the return value and handling and/or
567 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800568 *
569 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400570 */
571int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
572{
573 struct address_space *mapping = file->f_mapping;
574
575 __filemap_fdatawait_range(mapping, start_byte, end_byte);
576 return file_check_and_advance_wb_err(file);
577}
578EXPORT_SYMBOL(file_fdatawait_range);
579
580/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800581 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
582 * @mapping: address space structure to wait for
583 *
584 * Walk the list of under-writeback pages of the given address space
585 * and wait for all of them. Unlike filemap_fdatawait(), this function
586 * does not clear error status of the address space.
587 *
588 * Use this function if callers don't handle errors themselves. Expected
589 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
590 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800591 *
592 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800593 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400594int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800595{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400596 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400597 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800598}
Jeff Layton76341ca2017-07-06 07:02:22 -0400599EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800600
Jeff Layton9326c9b2017-07-26 10:21:11 -0400601static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400603 return (!dax_mapping(mapping) && mapping->nrpages) ||
604 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607int filemap_write_and_wait(struct address_space *mapping)
608{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800609 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jeff Layton9326c9b2017-07-26 10:21:11 -0400611 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800612 err = filemap_fdatawrite(mapping);
613 /*
614 * Even if the above returned error, the pages may be
615 * written partially (e.g. -ENOSPC), so we wait for it.
616 * But the -EIO is special case, it may indicate the worst
617 * thing (e.g. bug) happened, so we avoid waiting for it.
618 */
619 if (err != -EIO) {
620 int err2 = filemap_fdatawait(mapping);
621 if (!err)
622 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400623 } else {
624 /* Clear any previously stored errors */
625 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800626 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700627 } else {
628 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800632EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Randy Dunlap485bb992006-06-23 02:03:49 -0700634/**
635 * filemap_write_and_wait_range - write out & wait on a file range
636 * @mapping: the address_space for the pages
637 * @lstart: offset in bytes where the range starts
638 * @lend: offset in bytes where the range ends (inclusive)
639 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800640 * Write out and wait upon file offsets lstart->lend, inclusive.
641 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300642 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800643 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800644 *
645 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647int filemap_write_and_wait_range(struct address_space *mapping,
648 loff_t lstart, loff_t lend)
649{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800650 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jeff Layton9326c9b2017-07-26 10:21:11 -0400652 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800653 err = __filemap_fdatawrite_range(mapping, lstart, lend,
654 WB_SYNC_ALL);
655 /* See comment of filemap_write_and_wait() */
656 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200657 int err2 = filemap_fdatawait_range(mapping,
658 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800659 if (!err)
660 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400661 } else {
662 /* Clear any previously stored errors */
663 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800664 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700665 } else {
666 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
Chris Masonf6995582009-04-15 13:22:37 -0400670EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jeff Layton5660e132017-07-06 07:02:25 -0400672void __filemap_set_wb_err(struct address_space *mapping, int err)
673{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400674 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400675
676 trace_filemap_set_wb_err(mapping, eseq);
677}
678EXPORT_SYMBOL(__filemap_set_wb_err);
679
680/**
681 * file_check_and_advance_wb_err - report wb error (if any) that was previously
682 * and advance wb_err to current one
683 * @file: struct file on which the error is being reported
684 *
685 * When userland calls fsync (or something like nfsd does the equivalent), we
686 * want to report any writeback errors that occurred since the last fsync (or
687 * since the file was opened if there haven't been any).
688 *
689 * Grab the wb_err from the mapping. If it matches what we have in the file,
690 * then just quickly return 0. The file is all caught up.
691 *
692 * If it doesn't match, then take the mapping value, set the "seen" flag in
693 * it and try to swap it into place. If it works, or another task beat us
694 * to it with the new value, then update the f_wb_err and return the error
695 * portion. The error at this point must be reported via proper channels
696 * (a'la fsync, or NFS COMMIT operation, etc.).
697 *
698 * While we handle mapping->wb_err with atomic operations, the f_wb_err
699 * value is protected by the f_lock since we must ensure that it reflects
700 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800701 *
702 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400703 */
704int file_check_and_advance_wb_err(struct file *file)
705{
706 int err = 0;
707 errseq_t old = READ_ONCE(file->f_wb_err);
708 struct address_space *mapping = file->f_mapping;
709
710 /* Locklessly handle the common case where nothing has changed */
711 if (errseq_check(&mapping->wb_err, old)) {
712 /* Something changed, must use slow path */
713 spin_lock(&file->f_lock);
714 old = file->f_wb_err;
715 err = errseq_check_and_advance(&mapping->wb_err,
716 &file->f_wb_err);
717 trace_file_check_and_advance_wb_err(file, old);
718 spin_unlock(&file->f_lock);
719 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700720
721 /*
722 * We're mostly using this function as a drop in replacement for
723 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
724 * that the legacy code would have had on these flags.
725 */
726 clear_bit(AS_EIO, &mapping->flags);
727 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400728 return err;
729}
730EXPORT_SYMBOL(file_check_and_advance_wb_err);
731
732/**
733 * file_write_and_wait_range - write out & wait on a file range
734 * @file: file pointing to address_space with pages
735 * @lstart: offset in bytes where the range starts
736 * @lend: offset in bytes where the range ends (inclusive)
737 *
738 * Write out and wait upon file offsets lstart->lend, inclusive.
739 *
740 * Note that @lend is inclusive (describes the last byte to be written) so
741 * that this function can be used to write to the very end-of-file (end = -1).
742 *
743 * After writing out and waiting on the data, we check and advance the
744 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800745 *
746 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400747 */
748int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
749{
750 int err = 0, err2;
751 struct address_space *mapping = file->f_mapping;
752
Jeff Layton9326c9b2017-07-26 10:21:11 -0400753 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400754 err = __filemap_fdatawrite_range(mapping, lstart, lend,
755 WB_SYNC_ALL);
756 /* See comment of filemap_write_and_wait() */
757 if (err != -EIO)
758 __filemap_fdatawait_range(mapping, lstart, lend);
759 }
760 err2 = file_check_and_advance_wb_err(file);
761 if (!err)
762 err = err2;
763 return err;
764}
765EXPORT_SYMBOL(file_write_and_wait_range);
766
Randy Dunlap485bb992006-06-23 02:03:49 -0700767/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700768 * replace_page_cache_page - replace a pagecache page with a new one
769 * @old: page to be replaced
770 * @new: page to replace with
771 * @gfp_mask: allocation mode
772 *
773 * This function replaces a page in the pagecache with a new one. On
774 * success it acquires the pagecache reference for the new page and
775 * drops it for the old page. Both the old and new pages must be
776 * locked. This function does not add the new page to the LRU, the
777 * caller must do that.
778 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500779 * The remove + add is atomic. This function cannot fail.
Mike Rapoporta862f682019-03-05 15:48:42 -0800780 *
781 * Return: %0
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700782 */
783int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
784{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500785 struct address_space *mapping = old->mapping;
786 void (*freepage)(struct page *) = mapping->a_ops->freepage;
787 pgoff_t offset = old->index;
788 XA_STATE(xas, &mapping->i_pages, offset);
789 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700790
Sasha Levin309381fea2014-01-23 15:52:54 -0800791 VM_BUG_ON_PAGE(!PageLocked(old), old);
792 VM_BUG_ON_PAGE(!PageLocked(new), new);
793 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700794
Matthew Wilcox74d60952017-11-17 10:01:45 -0500795 get_page(new);
796 new->mapping = mapping;
797 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700798
Matthew Wilcox74d60952017-11-17 10:01:45 -0500799 xas_lock_irqsave(&xas, flags);
800 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700801
Matthew Wilcox74d60952017-11-17 10:01:45 -0500802 old->mapping = NULL;
803 /* hugetlb pages do not participate in page cache accounting. */
804 if (!PageHuge(old))
805 __dec_node_page_state(new, NR_FILE_PAGES);
806 if (!PageHuge(new))
807 __inc_node_page_state(new, NR_FILE_PAGES);
808 if (PageSwapBacked(old))
809 __dec_node_page_state(new, NR_SHMEM);
810 if (PageSwapBacked(new))
811 __inc_node_page_state(new, NR_SHMEM);
812 xas_unlock_irqrestore(&xas, flags);
813 mem_cgroup_migrate(old, new);
814 if (freepage)
815 freepage(old);
816 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700817
Matthew Wilcox74d60952017-11-17 10:01:45 -0500818 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700819}
820EXPORT_SYMBOL_GPL(replace_page_cache_page);
821
Johannes Weinera5289102014-04-03 14:47:51 -0700822static int __add_to_page_cache_locked(struct page *page,
823 struct address_space *mapping,
824 pgoff_t offset, gfp_t gfp_mask,
825 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500827 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700828 int huge = PageHuge(page);
829 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700830 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500831 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700832
Sasha Levin309381fea2014-01-23 15:52:54 -0800833 VM_BUG_ON_PAGE(!PageLocked(page), page);
834 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500835 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700836
Johannes Weiner00501b52014-08-08 14:19:20 -0700837 if (!huge) {
838 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800839 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700840 if (error)
841 return error;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300844 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700845 page->mapping = mapping;
846 page->index = offset;
847
Matthew Wilcox74d60952017-11-17 10:01:45 -0500848 do {
849 xas_lock_irq(&xas);
850 old = xas_load(&xas);
851 if (old && !xa_is_value(old))
852 xas_set_err(&xas, -EEXIST);
853 xas_store(&xas, page);
854 if (xas_error(&xas))
855 goto unlock;
Michal Hocko4165b9b42015-06-24 16:57:24 -0700856
Matthew Wilcox74d60952017-11-17 10:01:45 -0500857 if (xa_is_value(old)) {
858 mapping->nrexceptional--;
859 if (shadowp)
860 *shadowp = old;
861 }
862 mapping->nrpages++;
863
864 /* hugetlb pages do not participate in page cache accounting */
865 if (!huge)
866 __inc_node_page_state(page, NR_FILE_PAGES);
867unlock:
868 xas_unlock_irq(&xas);
869 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
870
871 if (xas_error(&xas))
872 goto error;
873
Johannes Weiner00501b52014-08-08 14:19:20 -0700874 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800875 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700876 trace_mm_filemap_add_to_page_cache(page);
877 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500878error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700879 page->mapping = NULL;
880 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700881 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800882 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300883 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500884 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Josef Bacikcfcbfb12019-05-13 17:21:04 -0700886ALLOW_ERROR_INJECTION(__add_to_page_cache_locked, ERRNO);
Johannes Weinera5289102014-04-03 14:47:51 -0700887
888/**
889 * add_to_page_cache_locked - add a locked page to the pagecache
890 * @page: page to add
891 * @mapping: the page's address_space
892 * @offset: page index
893 * @gfp_mask: page allocation mode
894 *
895 * This function is used to add a page to the pagecache. It must be locked.
896 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800897 *
898 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700899 */
900int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
901 pgoff_t offset, gfp_t gfp_mask)
902{
903 return __add_to_page_cache_locked(page, mapping, offset,
904 gfp_mask, NULL);
905}
Nick Piggine2867812008-07-25 19:45:30 -0700906EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400909 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Johannes Weinera5289102014-04-03 14:47:51 -0700911 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700912 int ret;
913
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800914 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700915 ret = __add_to_page_cache_locked(page, mapping, offset,
916 gfp_mask, &shadow);
917 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800918 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700919 else {
920 /*
921 * The page might have been evicted from cache only
922 * recently, in which case it should be activated like
923 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700924 * The exception is pages getting rewritten; evicting other
925 * data from the working set, only to cache data that will
926 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700927 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700928 WARN_ON_ONCE(PageActive(page));
929 if (!(gfp_mask & __GFP_WRITE) && shadow)
930 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700931 lru_cache_add(page);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return ret;
934}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300935EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Paul Jackson44110fe2006-03-24 03:16:04 -0800937#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700938struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800939{
Miao Xiec0ff7452010-05-24 14:32:08 -0700940 int n;
941 struct page *page;
942
Paul Jackson44110fe2006-03-24 03:16:04 -0800943 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700944 unsigned int cpuset_mems_cookie;
945 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700946 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700947 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700948 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700949 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700950
Miao Xiec0ff7452010-05-24 14:32:08 -0700951 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800952 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700953 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800954}
Nick Piggin2ae88142006-10-28 10:38:23 -0700955EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800956#endif
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
959 * In order to wait for pages to become available there must be
960 * waitqueues associated with pages. By using a hash table of
961 * waitqueues where the bucket discipline is to maintain all
962 * waiters on the same queue and wake all when any of the pages
963 * become available, and for the woken contexts to check to be
964 * sure the appropriate page became available, this saves space
965 * at a cost of "thundering herd" phenomena during rare hash
966 * collisions.
967 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000968#define PAGE_WAIT_TABLE_BITS 8
969#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
970static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
971
972static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Nicholas Piggin62906022016-12-25 13:00:30 +1000974 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
Nicholas Piggin62906022016-12-25 13:00:30 +1000976
977void __init pagecache_init(void)
978{
979 int i;
980
981 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
982 init_waitqueue_head(&page_wait_table[i]);
983
984 page_writeback_init();
985}
986
Linus Torvalds3510ca22017-08-27 13:55:12 -0700987/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000988struct wait_page_key {
989 struct page *page;
990 int bit_nr;
991 int page_match;
992};
993
994struct wait_page_queue {
995 struct page *page;
996 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200997 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000998};
999
Ingo Molnarac6424b2017-06-20 12:06:13 +02001000static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +10001001{
1002 struct wait_page_key *key = arg;
1003 struct wait_page_queue *wait_page
1004 = container_of(wait, struct wait_page_queue, wait);
1005
1006 if (wait_page->page != key->page)
1007 return 0;
1008 key->page_match = 1;
1009
1010 if (wait_page->bit_nr != key->bit_nr)
1011 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001012
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001013 /*
1014 * Stop walking if it's locked.
1015 * Is this safe if put_and_wait_on_page_locked() is in use?
1016 * Yes: the waker must hold a reference to this page, and if PG_locked
1017 * has now already been set by another task, that task must also hold
1018 * a reference to the *same usage* of this page; so there is no need
1019 * to walk on to wake even the put_and_wait_on_page_locked() callers.
1020 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001021 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001022 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001023
1024 return autoremove_wake_function(wait, mode, sync, key);
1025}
1026
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001027static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001028{
1029 wait_queue_head_t *q = page_waitqueue(page);
1030 struct wait_page_key key;
1031 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001032 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001033
1034 key.page = page;
1035 key.bit_nr = bit_nr;
1036 key.page_match = 0;
1037
Tim Chen11a19c72017-08-25 09:13:55 -07001038 bookmark.flags = 0;
1039 bookmark.private = NULL;
1040 bookmark.func = NULL;
1041 INIT_LIST_HEAD(&bookmark.entry);
1042
Nicholas Piggin62906022016-12-25 13:00:30 +10001043 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001044 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1045
1046 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1047 /*
1048 * Take a breather from holding the lock,
1049 * allow pages that finish wake up asynchronously
1050 * to acquire the lock and remove themselves
1051 * from wait queue
1052 */
1053 spin_unlock_irqrestore(&q->lock, flags);
1054 cpu_relax();
1055 spin_lock_irqsave(&q->lock, flags);
1056 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1057 }
1058
Nicholas Piggin62906022016-12-25 13:00:30 +10001059 /*
1060 * It is possible for other pages to have collided on the waitqueue
1061 * hash, so in that case check for a page match. That prevents a long-
1062 * term waiter
1063 *
1064 * It is still possible to miss a case here, when we woke page waiters
1065 * and removed them from the waitqueue, but there are still other
1066 * page waiters.
1067 */
1068 if (!waitqueue_active(q) || !key.page_match) {
1069 ClearPageWaiters(page);
1070 /*
1071 * It's possible to miss clearing Waiters here, when we woke
1072 * our page waiters, but the hashed waitqueue has waiters for
1073 * other pages on it.
1074 *
1075 * That's okay, it's a rare case. The next waker will clear it.
1076 */
1077 }
1078 spin_unlock_irqrestore(&q->lock, flags);
1079}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001080
1081static void wake_up_page(struct page *page, int bit)
1082{
1083 if (!PageWaiters(page))
1084 return;
1085 wake_up_page_bit(page, bit);
1086}
Nicholas Piggin62906022016-12-25 13:00:30 +10001087
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001088/*
1089 * A choice of three behaviors for wait_on_page_bit_common():
1090 */
1091enum behavior {
1092 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
1093 * __lock_page() waiting on then setting PG_locked.
1094 */
1095 SHARED, /* Hold ref to page and check the bit when woken, like
1096 * wait_on_page_writeback() waiting on PG_writeback.
1097 */
1098 DROP, /* Drop ref to page before wait, no check when woken,
1099 * like put_and_wait_on_page_locked() on PG_locked.
1100 */
1101};
1102
Nicholas Piggin62906022016-12-25 13:00:30 +10001103static inline int wait_on_page_bit_common(wait_queue_head_t *q,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001104 struct page *page, int bit_nr, int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001105{
1106 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001107 wait_queue_entry_t *wait = &wait_page.wait;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001108 bool bit_is_set;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001109 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001110 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001111 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001112 int ret = 0;
1113
Johannes Weinereb414682018-10-26 15:06:27 -07001114 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001115 !PageUptodate(page) && PageWorkingset(page)) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001116 if (!PageSwapBacked(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001117 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001118 delayacct = true;
1119 }
Johannes Weinereb414682018-10-26 15:06:27 -07001120 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001121 thrashing = true;
1122 }
1123
Nicholas Piggin62906022016-12-25 13:00:30 +10001124 init_wait(wait);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001125 wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001126 wait->func = wake_page_function;
1127 wait_page.page = page;
1128 wait_page.bit_nr = bit_nr;
1129
1130 for (;;) {
1131 spin_lock_irq(&q->lock);
1132
Ingo Molnar2055da92017-06-20 12:06:46 +02001133 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001134 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001135 SetPageWaiters(page);
1136 }
1137
1138 set_current_state(state);
1139
1140 spin_unlock_irq(&q->lock);
1141
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001142 bit_is_set = test_bit(bit_nr, &page->flags);
1143 if (behavior == DROP)
1144 put_page(page);
Nicholas Piggin62906022016-12-25 13:00:30 +10001145
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001146 if (likely(bit_is_set))
1147 io_schedule();
1148
1149 if (behavior == EXCLUSIVE) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001150 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1151 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001152 } else if (behavior == SHARED) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001153 if (!test_bit(bit_nr, &page->flags))
1154 break;
1155 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001156
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001157 if (signal_pending_state(state, current)) {
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001158 ret = -EINTR;
1159 break;
1160 }
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001161
1162 if (behavior == DROP) {
1163 /*
1164 * We can no longer safely access page->flags:
1165 * even if CONFIG_MEMORY_HOTREMOVE is not enabled,
1166 * there is a risk of waiting forever on a page reused
1167 * for something that keeps it locked indefinitely.
1168 * But best check for -EINTR above before breaking.
1169 */
1170 break;
1171 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001172 }
1173
1174 finish_wait(q, wait);
1175
Johannes Weinereb414682018-10-26 15:06:27 -07001176 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001177 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001178 delayacct_thrashing_end();
1179 psi_memstall_leave(&pflags);
1180 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001181
Nicholas Piggin62906022016-12-25 13:00:30 +10001182 /*
1183 * A signal could leave PageWaiters set. Clearing it here if
1184 * !waitqueue_active would be possible (by open-coding finish_wait),
1185 * but still fail to catch it in the case of wait hash collision. We
1186 * already can fail to clear wait hash collision cases, so don't
1187 * bother with signals either.
1188 */
1189
1190 return ret;
1191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Harvey Harrison920c7a52008-02-04 22:29:26 -08001193void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Nicholas Piggin62906022016-12-25 13:00:30 +10001195 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001196 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198EXPORT_SYMBOL(wait_on_page_bit);
1199
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001200int wait_on_page_bit_killable(struct page *page, int bit_nr)
1201{
Nicholas Piggin62906022016-12-25 13:00:30 +10001202 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001203 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001204}
David Howells4343d002017-11-02 15:27:52 +00001205EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001208 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1209 * @page: The page to wait for.
1210 *
1211 * The caller should hold a reference on @page. They expect the page to
1212 * become unlocked relatively soon, but do not wish to hold up migration
1213 * (for example) by holding the reference while waiting for the page to
1214 * come unlocked. After this function returns, the caller should not
1215 * dereference @page.
1216 */
1217void put_and_wait_on_page_locked(struct page *page)
1218{
1219 wait_queue_head_t *q;
1220
1221 page = compound_head(page);
1222 q = page_waitqueue(page);
1223 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP);
1224}
1225
1226/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001227 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001228 * @page: Page defining the wait queue of interest
1229 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001230 *
1231 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1232 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001233void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001234{
1235 wait_queue_head_t *q = page_waitqueue(page);
1236 unsigned long flags;
1237
1238 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001239 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001240 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001241 spin_unlock_irqrestore(&q->lock, flags);
1242}
1243EXPORT_SYMBOL_GPL(add_page_wait_queue);
1244
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001245#ifndef clear_bit_unlock_is_negative_byte
1246
1247/*
1248 * PG_waiters is the high bit in the same byte as PG_lock.
1249 *
1250 * On x86 (and on many other architectures), we can clear PG_lock and
1251 * test the sign bit at the same time. But if the architecture does
1252 * not support that special operation, we just do this all by hand
1253 * instead.
1254 *
1255 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1256 * being cleared, but a memory barrier should be unneccssary since it is
1257 * in the same byte as PG_locked.
1258 */
1259static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1260{
1261 clear_bit_unlock(nr, mem);
1262 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001263 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001264}
1265
1266#endif
1267
David Howells385e1ca5f2009-04-03 16:42:39 +01001268/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001269 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * @page: the page
1271 *
1272 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1273 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001274 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1276 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001277 * Note that this depends on PG_waiters being the sign bit in the byte
1278 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1279 * clear the PG_locked bit and test PG_waiters at the same time fairly
1280 * portably (architectures that do LL/SC can test any bit, while x86 can
1281 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001283void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001285 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001286 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001287 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001288 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1289 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291EXPORT_SYMBOL(unlock_page);
1292
Randy Dunlap485bb992006-06-23 02:03:49 -07001293/**
1294 * end_page_writeback - end writeback against a page
1295 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297void end_page_writeback(struct page *page)
1298{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001299 /*
1300 * TestClearPageReclaim could be used here but it is an atomic
1301 * operation and overkill in this particular case. Failing to
1302 * shuffle a page marked for immediate reclaim is too mild to
1303 * justify taking an atomic operation penalty at the end of
1304 * ever page writeback.
1305 */
1306 if (PageReclaim(page)) {
1307 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001308 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001309 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001310
1311 if (!test_clear_page_writeback(page))
1312 BUG();
1313
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001314 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 wake_up_page(page, PG_writeback);
1316}
1317EXPORT_SYMBOL(end_page_writeback);
1318
Matthew Wilcox57d99842014-06-04 16:07:45 -07001319/*
1320 * After completing I/O on a page, call this routine to update the page
1321 * flags appropriately
1322 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001323void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001324{
Jens Axboec11f0c02016-08-05 08:11:04 -06001325 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001326 if (!err) {
1327 SetPageUptodate(page);
1328 } else {
1329 ClearPageUptodate(page);
1330 SetPageError(page);
1331 }
1332 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001333 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001334 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001335 struct address_space *mapping;
1336
Matthew Wilcox57d99842014-06-04 16:07:45 -07001337 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001338 mapping = page_mapping(page);
1339 if (mapping)
1340 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001341 }
1342 end_page_writeback(page);
1343 }
1344}
1345EXPORT_SYMBOL_GPL(page_endio);
1346
Randy Dunlap485bb992006-06-23 02:03:49 -07001347/**
1348 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001349 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001351void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Nicholas Piggin62906022016-12-25 13:00:30 +10001353 struct page *page = compound_head(__page);
1354 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001355 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE,
1356 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358EXPORT_SYMBOL(__lock_page);
1359
Nicholas Piggin62906022016-12-25 13:00:30 +10001360int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001361{
Nicholas Piggin62906022016-12-25 13:00:30 +10001362 struct page *page = compound_head(__page);
1363 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001364 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE,
1365 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001366}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001367EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001368
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001369/*
1370 * Return values:
1371 * 1 - page is locked; mmap_sem is still held.
1372 * 0 - page is not locked.
1373 * mmap_sem has been released (up_read()), unless flags had both
1374 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1375 * which case mmap_sem is still held.
1376 *
1377 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1378 * with the page locked and the mmap_sem unperturbed.
1379 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001380int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1381 unsigned int flags)
1382{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001383 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1384 /*
1385 * CAUTION! In this case, mmap_sem is not released
1386 * even though return 0.
1387 */
1388 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1389 return 0;
1390
1391 up_read(&mm->mmap_sem);
1392 if (flags & FAULT_FLAG_KILLABLE)
1393 wait_on_page_locked_killable(page);
1394 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001395 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001396 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001397 } else {
1398 if (flags & FAULT_FLAG_KILLABLE) {
1399 int ret;
1400
1401 ret = __lock_page_killable(page);
1402 if (ret) {
1403 up_read(&mm->mmap_sem);
1404 return 0;
1405 }
1406 } else
1407 __lock_page(page);
1408 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001409 }
1410}
1411
Randy Dunlap485bb992006-06-23 02:03:49 -07001412/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001413 * page_cache_next_miss() - Find the next gap in the page cache.
1414 * @mapping: Mapping.
1415 * @index: Index.
1416 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001417 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001418 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1419 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001420 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001421 * This function may be called under the rcu_read_lock. However, this will
1422 * not atomically search a snapshot of the cache at a single point in time.
1423 * For example, if a gap is created at index 5, then subsequently a gap is
1424 * created at index 10, page_cache_next_miss covering both indices may
1425 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001426 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001427 * Return: The index of the gap if found, otherwise an index outside the
1428 * range specified (in which case 'return - index >= max_scan' will be true).
1429 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001430 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001431pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001432 pgoff_t index, unsigned long max_scan)
1433{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001434 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001435
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001436 while (max_scan--) {
1437 void *entry = xas_next(&xas);
1438 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001439 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001440 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001441 break;
1442 }
1443
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001444 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001445}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001446EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001447
1448/**
Laurent Dufour2346a562019-05-13 17:21:29 -07001449 * page_cache_prev_miss() - Find the previous gap in the page cache.
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001450 * @mapping: Mapping.
1451 * @index: Index.
1452 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001453 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001454 * Search the range [max(index - max_scan + 1, 0), index] for the
1455 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001456 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001457 * This function may be called under the rcu_read_lock. However, this will
1458 * not atomically search a snapshot of the cache at a single point in time.
1459 * For example, if a gap is created at index 10, then subsequently a gap is
1460 * created at index 5, page_cache_prev_miss() covering both indices may
1461 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001462 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001463 * Return: The index of the gap if found, otherwise an index outside the
1464 * range specified (in which case 'index - return >= max_scan' will be true).
1465 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001466 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001467pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001468 pgoff_t index, unsigned long max_scan)
1469{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001470 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001471
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001472 while (max_scan--) {
1473 void *entry = xas_prev(&xas);
1474 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001475 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001476 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001477 break;
1478 }
1479
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001480 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001481}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001482EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001483
1484/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001485 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001486 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001487 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001488 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001489 * Looks up the page cache slot at @mapping & @offset. If there is a
1490 * page cache page, it is returned with an increased refcount.
1491 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001492 * If the slot holds a shadow entry of a previously evicted page, or a
1493 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001494 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001495 * Return: the found page or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001497struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001499 XA_STATE(xas, &mapping->i_pages, offset);
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001500 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Nick Piggina60637c2008-07-25 19:45:31 -07001502 rcu_read_lock();
1503repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001504 xas_reset(&xas);
1505 page = xas_load(&xas);
1506 if (xas_retry(&xas, page))
1507 goto repeat;
1508 /*
1509 * A shadow entry of a recently evicted page, or a swap entry from
1510 * shmem/tmpfs. Return it without attempting to raise page count.
1511 */
1512 if (!page || xa_is_value(page))
1513 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001514
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001515 if (!page_cache_get_speculative(page))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001516 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001517
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001518 /*
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001519 * Has the page moved or been split?
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001520 * This is part of the lockless pagecache protocol. See
1521 * include/linux/pagemap.h for details.
1522 */
1523 if (unlikely(page != xas_reload(&xas))) {
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001524 put_page(page);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001525 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001526 }
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001527 page = find_subpage(page, offset);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001528out:
Nick Piggina60637c2008-07-25 19:45:31 -07001529 rcu_read_unlock();
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return page;
1532}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001533EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Randy Dunlap485bb992006-06-23 02:03:49 -07001535/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001536 * find_lock_entry - locate, pin and lock a page cache entry
1537 * @mapping: the address_space to search
1538 * @offset: the page cache index
1539 *
1540 * Looks up the page cache slot at @mapping & @offset. If there is a
1541 * page cache page, it is returned locked and with an increased
1542 * refcount.
1543 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001544 * If the slot holds a shadow entry of a previously evicted page, or a
1545 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001546 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001547 * find_lock_entry() may sleep.
Mike Rapoporta862f682019-03-05 15:48:42 -08001548 *
1549 * Return: the found page or shadow entry, %NULL if nothing is found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001550 */
1551struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 struct page *page;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001556 page = find_get_entry(mapping, offset);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001557 if (page && !xa_is_value(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001558 lock_page(page);
1559 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001560 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001561 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001562 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001563 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001565 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return page;
1568}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001569EXPORT_SYMBOL(find_lock_entry);
1570
1571/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001572 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001573 * @mapping: the address_space to search
1574 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001575 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001576 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001577 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001578 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001579 *
Randy Dunlap75325182014-07-30 16:08:37 -07001580 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001581 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001582 * @fgp_flags can be:
1583 *
1584 * - FGP_ACCESSED: the page will be marked accessed
1585 * - FGP_LOCK: Page is return locked
1586 * - FGP_CREAT: If page is not present then a new page is allocated using
1587 * @gfp_mask and added to the page cache and the VM's LRU
1588 * list. The page is returned locked and with an increased
Mike Rapoporta862f682019-03-05 15:48:42 -08001589 * refcount.
Josef Bacika75d4c32019-03-13 11:44:14 -07001590 * - FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1591 * its own locking dance if the page is already in cache, or unlock the page
1592 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001593 *
1594 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1595 * if the GFP flags specified for FGP_CREAT are atomic.
1596 *
1597 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001598 *
1599 * Return: the found page or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001600 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001601struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001602 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Nick Piggineb2be182007-10-16 01:24:57 -07001604 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001607 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001608 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001609 page = NULL;
1610 if (!page)
1611 goto no_page;
1612
1613 if (fgp_flags & FGP_LOCK) {
1614 if (fgp_flags & FGP_NOWAIT) {
1615 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001617 return NULL;
1618 }
1619 } else {
1620 lock_page(page);
1621 }
1622
1623 /* Has the page been truncated? */
1624 if (unlikely(page->mapping != mapping)) {
1625 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001626 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001627 goto repeat;
1628 }
1629 VM_BUG_ON_PAGE(page->index != offset, page);
1630 }
1631
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001632 if (fgp_flags & FGP_ACCESSED)
Mel Gorman2457aec2014-06-04 16:10:31 -07001633 mark_page_accessed(page);
1634
1635no_page:
1636 if (!page && (fgp_flags & FGP_CREAT)) {
1637 int err;
1638 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001639 gfp_mask |= __GFP_WRITE;
1640 if (fgp_flags & FGP_NOFS)
1641 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001642
Michal Hocko45f87de2014-12-29 20:30:35 +01001643 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001644 if (!page)
1645 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001646
Josef Bacika75d4c32019-03-13 11:44:14 -07001647 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001648 fgp_flags |= FGP_LOCK;
1649
Hugh Dickinseb39d612014-08-06 16:06:43 -07001650 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001651 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001652 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001653
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001654 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001655 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001656 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001657 page = NULL;
1658 if (err == -EEXIST)
1659 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001661
1662 /*
1663 * add_to_page_cache_lru locks the page, and for mmap we expect
1664 * an unlocked page.
1665 */
1666 if (page && (fgp_flags & FGP_FOR_MMAP))
1667 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return page;
1671}
Mel Gorman2457aec2014-06-04 16:10:31 -07001672EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001675 * find_get_entries - gang pagecache lookup
1676 * @mapping: The address_space to search
1677 * @start: The starting page cache index
1678 * @nr_entries: The maximum number of entries
1679 * @entries: Where the resulting entries are placed
1680 * @indices: The cache indices corresponding to the entries in @entries
1681 *
1682 * find_get_entries() will search for and return a group of up to
1683 * @nr_entries entries in the mapping. The entries are placed at
1684 * @entries. find_get_entries() takes a reference against any actual
1685 * pages it returns.
1686 *
1687 * The search returns a group of mapping-contiguous page cache entries
1688 * with ascending indexes. There may be holes in the indices due to
1689 * not-present pages.
1690 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001691 * Any shadow entries of evicted pages, or swap entries from
1692 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001693 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001694 * Return: the number of pages and shadow entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001695 */
1696unsigned find_get_entries(struct address_space *mapping,
1697 pgoff_t start, unsigned int nr_entries,
1698 struct page **entries, pgoff_t *indices)
1699{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001700 XA_STATE(xas, &mapping->i_pages, start);
1701 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001702 unsigned int ret = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001703
1704 if (!nr_entries)
1705 return 0;
1706
1707 rcu_read_lock();
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001708 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001709 if (xas_retry(&xas, page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001710 continue;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001711 /*
1712 * A shadow entry of a recently evicted page, a swap
1713 * entry from shmem/tmpfs or a DAX entry. Return it
1714 * without attempting to raise page count.
1715 */
1716 if (xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001717 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001718
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001719 if (!page_cache_get_speculative(page))
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001720 goto retry;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001721
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001722 /* Has the page moved or been split? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001723 if (unlikely(page != xas_reload(&xas)))
1724 goto put_page;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001725 page = find_subpage(page, xas.xa_index);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001726
Johannes Weiner0cd61442014-04-03 14:47:46 -07001727export:
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001728 indices[ret] = xas.xa_index;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001729 entries[ret] = page;
1730 if (++ret == nr_entries)
1731 break;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001732 continue;
1733put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001734 put_page(page);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001735retry:
1736 xas_reset(&xas);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001737 }
1738 rcu_read_unlock();
1739 return ret;
1740}
1741
1742/**
Jan Karab947cee2017-09-06 16:21:21 -07001743 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * @mapping: The address_space to search
1745 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001746 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * @nr_pages: The maximum number of pages
1748 * @pages: Where the resulting pages are placed
1749 *
Jan Karab947cee2017-09-06 16:21:21 -07001750 * find_get_pages_range() will search for and return a group of up to @nr_pages
1751 * pages in the mapping starting at index @start and up to index @end
1752 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1753 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 *
1755 * The search returns a group of mapping-contiguous pages with ascending
1756 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001757 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001759 * Return: the number of pages which were found. If this number is
1760 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07001761 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Jan Karab947cee2017-09-06 16:21:21 -07001763unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1764 pgoff_t end, unsigned int nr_pages,
1765 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001767 XA_STATE(xas, &mapping->i_pages, *start);
1768 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001769 unsigned ret = 0;
1770
1771 if (unlikely(!nr_pages))
1772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Nick Piggina60637c2008-07-25 19:45:31 -07001774 rcu_read_lock();
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001775 xas_for_each(&xas, page, end) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001776 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001777 continue;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001778 /* Skip over shadow, swap and DAX entries */
1779 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001780 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001781
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001782 if (!page_cache_get_speculative(page))
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001783 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001784
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001785 /* Has the page moved or been split? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001786 if (unlikely(page != xas_reload(&xas)))
1787 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001788
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001789 pages[ret] = find_subpage(page, xas.xa_index);
Jan Karab947cee2017-09-06 16:21:21 -07001790 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001791 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07001792 goto out;
1793 }
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001794 continue;
1795put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001796 put_page(page);
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001797retry:
1798 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001799 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001800
Jan Karab947cee2017-09-06 16:21:21 -07001801 /*
1802 * We come here when there is no page beyond @end. We take care to not
1803 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001804 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07001805 * already broken anyway.
1806 */
1807 if (end == (pgoff_t)-1)
1808 *start = (pgoff_t)-1;
1809 else
1810 *start = end + 1;
1811out:
Nick Piggina60637c2008-07-25 19:45:31 -07001812 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return ret;
1815}
1816
Jens Axboeebf43502006-04-27 08:46:01 +02001817/**
1818 * find_get_pages_contig - gang contiguous pagecache lookup
1819 * @mapping: The address_space to search
1820 * @index: The starting page index
1821 * @nr_pages: The maximum number of pages
1822 * @pages: Where the resulting pages are placed
1823 *
1824 * find_get_pages_contig() works exactly like find_get_pages(), except
1825 * that the returned number of pages are guaranteed to be contiguous.
1826 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001827 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02001828 */
1829unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1830 unsigned int nr_pages, struct page **pages)
1831{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001832 XA_STATE(xas, &mapping->i_pages, index);
1833 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001834 unsigned int ret = 0;
1835
1836 if (unlikely(!nr_pages))
1837 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001838
Nick Piggina60637c2008-07-25 19:45:31 -07001839 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001840 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001841 if (xas_retry(&xas, page))
1842 continue;
1843 /*
1844 * If the entry has been swapped out, we can stop looking.
1845 * No current caller is looking for DAX entries.
1846 */
1847 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001848 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001849
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001850 if (!page_cache_get_speculative(page))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001851 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001852
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001853 /* Has the page moved or been split? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001854 if (unlikely(page != xas_reload(&xas)))
1855 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001856
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001857 pages[ret] = find_subpage(page, xas.xa_index);
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001858 if (++ret == nr_pages)
1859 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001860 continue;
1861put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001862 put_page(page);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001863retry:
1864 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02001865 }
Nick Piggina60637c2008-07-25 19:45:31 -07001866 rcu_read_unlock();
1867 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001868}
David Howellsef71c152007-05-09 02:33:44 -07001869EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001870
Randy Dunlap485bb992006-06-23 02:03:49 -07001871/**
Jan Kara72b045a2017-11-15 17:34:33 -08001872 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001873 * @mapping: the address_space to search
1874 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001875 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001876 * @tag: the tag index
1877 * @nr_pages: the maximum number of pages
1878 * @pages: where the resulting pages are placed
1879 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001881 * @tag. We update @index to index the next page for the traversal.
Mike Rapoporta862f682019-03-05 15:48:42 -08001882 *
1883 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Jan Kara72b045a2017-11-15 17:34:33 -08001885unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001886 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08001887 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001889 XA_STATE(xas, &mapping->i_pages, *index);
1890 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001891 unsigned ret = 0;
1892
1893 if (unlikely(!nr_pages))
1894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Nick Piggina60637c2008-07-25 19:45:31 -07001896 rcu_read_lock();
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001897 xas_for_each_marked(&xas, page, end, tag) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001898 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001899 continue;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001900 /*
1901 * Shadow entries should never be tagged, but this iteration
1902 * is lockless so there is a window for page reclaim to evict
1903 * a page we saw tagged. Skip over it.
1904 */
1905 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07001906 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001907
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001908 if (!page_cache_get_speculative(page))
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001909 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001910
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001911 /* Has the page moved or been split? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001912 if (unlikely(page != xas_reload(&xas)))
1913 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001914
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001915 pages[ret] = find_subpage(page, xas.xa_index);
Jan Kara72b045a2017-11-15 17:34:33 -08001916 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001917 *index = xas.xa_index + 1;
Jan Kara72b045a2017-11-15 17:34:33 -08001918 goto out;
1919 }
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001920 continue;
1921put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001922 put_page(page);
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001923retry:
1924 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001925 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001926
Jan Kara72b045a2017-11-15 17:34:33 -08001927 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001928 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08001929 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001930 * iteration when there is a page at index -1 but that is already
1931 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08001932 */
1933 if (end == (pgoff_t)-1)
1934 *index = (pgoff_t)-1;
1935 else
1936 *index = end + 1;
1937out:
Nick Piggina60637c2008-07-25 19:45:31 -07001938 rcu_read_unlock();
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return ret;
1941}
Jan Kara72b045a2017-11-15 17:34:33 -08001942EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001944/*
1945 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1946 * a _large_ part of the i/o request. Imagine the worst scenario:
1947 *
1948 * ---R__________________________________________B__________
1949 * ^ reading here ^ bad block(assume 4k)
1950 *
1951 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1952 * => failing the whole request => read(R) => read(R+1) =>
1953 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1954 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1955 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1956 *
1957 * It is going insane. Fix it by quickly scaling down the readahead size.
1958 */
1959static void shrink_readahead_size_eio(struct file *filp,
1960 struct file_ra_state *ra)
1961{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001962 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001963}
1964
Randy Dunlap485bb992006-06-23 02:03:49 -07001965/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001966 * generic_file_buffered_read - generic file read routine
1967 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05001968 * @iter: data destination
1969 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001970 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001972 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 *
1974 * This is really ugly. But the goto's actually try to clarify some
1975 * of the logic when it comes to error handling etc.
Mike Rapoporta862f682019-03-05 15:48:42 -08001976 *
1977 * Return:
1978 * * total number of bytes copied, including those the were already @written
1979 * * negative error code if nothing was copied
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001981static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05001982 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001984 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001985 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001987 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001988 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001989 pgoff_t index;
1990 pgoff_t last_index;
1991 pgoff_t prev_index;
1992 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001993 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001994 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Wei Fangc2a97372016-10-07 17:01:52 -07001996 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08001997 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07001998 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
1999
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002000 index = *ppos >> PAGE_SHIFT;
2001 prev_index = ra->prev_pos >> PAGE_SHIFT;
2002 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2003 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2004 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 for (;;) {
2007 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002008 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002009 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 unsigned long nr, ret;
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002014 if (fatal_signal_pending(current)) {
2015 error = -EINTR;
2016 goto out;
2017 }
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002020 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002021 if (iocb->ki_flags & IOCB_NOWAIT)
2022 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002023 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002024 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002025 index, last_index - index);
2026 page = find_get_page(mapping, index);
2027 if (unlikely(page == NULL))
2028 goto no_cached_page;
2029 }
2030 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002031 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002032 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002033 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002035 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002036 if (iocb->ki_flags & IOCB_NOWAIT) {
2037 put_page(page);
2038 goto would_block;
2039 }
2040
Mel Gormanebded022016-03-15 14:55:39 -07002041 /*
2042 * See comment in do_read_cache_page on why
2043 * wait_on_page_locked is used to avoid unnecessarily
2044 * serialisations and why it's safe.
2045 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002046 error = wait_on_page_locked_killable(page);
2047 if (unlikely(error))
2048 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002049 if (PageUptodate(page))
2050 goto page_ok;
2051
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002052 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002053 !mapping->a_ops->is_partially_uptodate)
2054 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002055 /* pipes can't handle partially uptodate pages */
David Howells00e23702018-10-22 13:07:28 +01002056 if (unlikely(iov_iter_is_pipe(iter)))
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002057 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002058 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002059 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002060 /* Did it get truncated before we got the lock? */
2061 if (!page->mapping)
2062 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002063 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002064 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002065 goto page_not_up_to_date_locked;
2066 unlock_page(page);
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002069 /*
2070 * i_size must be checked after we know the page is Uptodate.
2071 *
2072 * Checking i_size after the check allows us to calculate
2073 * the correct value for "nr", which means the zero-filled
2074 * part of the page is not copied back to userspace (unless
2075 * another truncate extends the file - this is desired though).
2076 */
2077
2078 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002079 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002080 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002082 goto out;
2083 }
2084
2085 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002086 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002087 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002088 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002089 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002091 goto out;
2092 }
2093 }
2094 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /* If users can be writing to this page using arbitrary
2097 * virtual addresses, take care about potential aliasing
2098 * before reading the page on the kernel side.
2099 */
2100 if (mapping_writably_mapped(mapping))
2101 flush_dcache_page(page);
2102
2103 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002104 * When a sequential read accesses a page several times,
2105 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 */
Jan Karaec0f1632007-05-06 14:49:25 -07002107 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 mark_page_accessed(page);
2109 prev_index = index;
2110
2111 /*
2112 * Ok, we have the page, and it's up-to-date, so
2113 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Al Viro6e58e792014-02-03 17:07:03 -05002115
2116 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 index += offset >> PAGE_SHIFT;
2119 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002120 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002122 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002123 written += ret;
2124 if (!iov_iter_count(iter))
2125 goto out;
2126 if (ret < nr) {
2127 error = -EFAULT;
2128 goto out;
2129 }
2130 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132page_not_up_to_date:
2133 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002134 error = lock_page_killable(page);
2135 if (unlikely(error))
2136 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002138page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002139 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (!page->mapping) {
2141 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002142 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 continue;
2144 }
2145
2146 /* Did somebody else fill it already? */
2147 if (PageUptodate(page)) {
2148 unlock_page(page);
2149 goto page_ok;
2150 }
2151
2152readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002153 /*
2154 * A previous I/O error may have been due to temporary
2155 * failures, eg. multipath errors.
2156 * PG_error will be set again if readpage fails.
2157 */
2158 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* Start the actual read. The read will unlock the page. */
2160 error = mapping->a_ops->readpage(filp, page);
2161
Zach Brown994fc28c2005-12-15 14:28:17 -08002162 if (unlikely(error)) {
2163 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002164 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002165 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002166 goto find_page;
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002172 error = lock_page_killable(page);
2173 if (unlikely(error))
2174 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!PageUptodate(page)) {
2176 if (page->mapping == NULL) {
2177 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002178 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 */
2180 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002181 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 goto find_page;
2183 }
2184 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002185 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002186 error = -EIO;
2187 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 unlock_page(page);
2190 }
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 goto page_ok;
2193
2194readpage_error:
2195 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002196 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto out;
2198
2199no_cached_page:
2200 /*
2201 * Ok, it wasn't cached, so we need to create a new
2202 * page..
2203 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002204 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002205 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002206 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002209 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002210 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002212 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002213 if (error == -EEXIST) {
2214 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto out;
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto readpage;
2220 }
2221
Milosz Tanski3239d832017-08-29 16:13:19 +02002222would_block:
2223 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002225 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002226 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002227 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002229 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002230 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002231 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Randy Dunlap485bb992006-06-23 02:03:49 -07002234/**
Al Viro6abd2322014-04-04 14:20:57 -04002235 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002236 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002237 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002238 *
Al Viro6abd2322014-04-04 14:20:57 -04002239 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 * that can use the page cache directly.
Mike Rapoporta862f682019-03-05 15:48:42 -08002241 * Return:
2242 * * number of bytes copied, even for partial reads
2243 * * negative error code if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
2245ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002246generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002248 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002249 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002250
2251 if (!count)
2252 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Al Viro2ba48ce2015-04-09 13:52:01 -04002254 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002255 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002256 struct address_space *mapping = file->f_mapping;
2257 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002258 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002261 if (iocb->ki_flags & IOCB_NOWAIT) {
2262 if (filemap_range_has_page(mapping, iocb->ki_pos,
2263 iocb->ki_pos + count - 1))
2264 return -EAGAIN;
2265 } else {
2266 retval = filemap_write_and_wait_range(mapping,
2267 iocb->ki_pos,
2268 iocb->ki_pos + count - 1);
2269 if (retval < 0)
2270 goto out;
2271 }
Al Viroed978a82014-03-05 22:53:04 -05002272
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002273 file_accessed(file);
2274
Al Viro5ecda132017-04-13 14:13:36 -04002275 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002276 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002277 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002278 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002279 }
Al Viro5b47d592017-05-08 13:54:47 -04002280 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002281
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002282 /*
2283 * Btrfs can have a short DIO read if we encounter
2284 * compressed extents, so if there was an error, or if
2285 * we've already read everything we wanted to, or if
2286 * there was a short read because we hit EOF, go ahead
2287 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002288 * the rest of the read. Buffered reads will not work for
2289 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002290 */
Al Viro5ecda132017-04-13 14:13:36 -04002291 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002292 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002296 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297out:
2298 return retval;
2299}
Al Viroed978a82014-03-05 22:53:04 -05002300EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#define MMAP_LOTSAMISS (100)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002304static struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
2305 struct file *fpin)
2306{
2307 int flags = vmf->flags;
2308
2309 if (fpin)
2310 return fpin;
2311
2312 /*
2313 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2314 * anything, so we only pin the file and drop the mmap_sem if only
2315 * FAULT_FLAG_ALLOW_RETRY is set.
2316 */
2317 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2318 FAULT_FLAG_ALLOW_RETRY) {
2319 fpin = get_file(vmf->vma->vm_file);
2320 up_read(&vmf->vma->vm_mm->mmap_sem);
2321 }
2322 return fpin;
2323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Linus Torvaldsef00e082009-06-16 15:31:25 -07002325/*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002326 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2327 * @vmf - the vm_fault for this fault.
2328 * @page - the page to lock.
2329 * @fpin - the pointer to the file we may pin (or is already pinned).
2330 *
2331 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2332 * It differs in that it actually returns the page locked if it returns 1 and 0
2333 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2334 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002335 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002336static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2337 struct file **fpin)
2338{
2339 if (trylock_page(page))
2340 return 1;
2341
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002342 /*
2343 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2344 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2345 * is supposed to work. We have way too many special cases..
2346 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002347 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2348 return 0;
2349
2350 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2351 if (vmf->flags & FAULT_FLAG_KILLABLE) {
2352 if (__lock_page_killable(page)) {
2353 /*
2354 * We didn't have the right flags to drop the mmap_sem,
2355 * but all fault_handlers only check for fatal signals
2356 * if we return VM_FAULT_RETRY, so we need to drop the
2357 * mmap_sem here and return 0 if we don't have a fpin.
2358 */
2359 if (*fpin == NULL)
2360 up_read(&vmf->vma->vm_mm->mmap_sem);
2361 return 0;
2362 }
2363 } else
2364 __lock_page(page);
2365 return 1;
2366}
2367
2368
2369/*
2370 * Synchronous readahead happens when we don't even find a page in the page
2371 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2372 * to drop the mmap sem we return the file that was pinned in order for us to do
2373 * that. If we didn't pin a file then we return NULL. The file that is
2374 * returned needs to be fput()'ed when we're done with it.
2375 */
2376static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002377{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002378 struct file *file = vmf->vma->vm_file;
2379 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002380 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002381 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002382 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002383
2384 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002385 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002386 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002387 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002388 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002389
Josef Bacik2a1180f2019-03-13 11:44:18 -07002390 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002391 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002392 page_cache_sync_readahead(mapping, ra, file, offset,
2393 ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002394 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002395 }
2396
Andi Kleen207d04b2011-05-24 17:12:29 -07002397 /* Avoid banging the cache line if not needed */
2398 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002399 ra->mmap_miss++;
2400
2401 /*
2402 * Do we miss much more than hit in this file? If so,
2403 * stop bothering with read-ahead. It will only hurt.
2404 */
2405 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002406 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002407
Wu Fengguangd30a1102009-06-16 15:31:30 -07002408 /*
2409 * mmap read-around
2410 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002411 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002412 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2413 ra->size = ra->ra_pages;
2414 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002415 ra_submit(ra, mapping, file);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002416 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002417}
2418
2419/*
2420 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002421 * so we want to possibly extend the readahead further. We return the file that
2422 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002423 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002424static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
2425 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002426{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002427 struct file *file = vmf->vma->vm_file;
2428 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002429 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002430 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002431 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002432
2433 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002434 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002435 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002436 if (ra->mmap_miss > 0)
2437 ra->mmap_miss--;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002438 if (PageReadahead(page)) {
2439 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002440 page_cache_async_readahead(mapping, ra, file,
2441 page, offset, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002442 }
2443 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002444}
2445
Randy Dunlap485bb992006-06-23 02:03:49 -07002446/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002447 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002448 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002449 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002450 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 * mapped memory region to read in file data during a page fault.
2452 *
2453 * The goto's are kind of ugly, but this streamlines the normal case of having
2454 * it in the page cache, and handles the special cases reasonably without
2455 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002456 *
2457 * vma->vm_mm->mmap_sem must be held on entry.
2458 *
2459 * If our return value has VM_FAULT_RETRY set, it's because
2460 * lock_page_or_retry() returned 0.
2461 * The mmap_sem has usually been released in this case.
2462 * See __lock_page_or_retry() for the exception.
2463 *
2464 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2465 * has not been released.
2466 *
2467 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08002468 *
2469 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002471vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002474 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002475 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct address_space *mapping = file->f_mapping;
2477 struct file_ra_state *ra = &file->f_ra;
2478 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002479 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002480 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002482 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002484 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2485 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002486 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002489 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002491 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002492 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002494 * We found the page, so try async readahead before
2495 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002497 fpin = do_async_mmap_readahead(vmf, page);
Shaohua Li45cac652012-10-08 16:32:19 -07002498 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002499 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002500 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002501 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002502 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002503 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002504retry_find:
Josef Bacika75d4c32019-03-13 11:44:14 -07002505 page = pagecache_get_page(mapping, offset,
2506 FGP_CREAT|FGP_FOR_MMAP,
2507 vmf->gfp_mask);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002508 if (!page) {
2509 if (fpin)
2510 goto out_retry;
Josef Bacika75d4c32019-03-13 11:44:14 -07002511 return vmf_error(-ENOMEM);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002515 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
2516 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002517
2518 /* Did it get truncated? */
2519 if (unlikely(page->mapping != mapping)) {
2520 unlock_page(page);
2521 put_page(page);
2522 goto retry_find;
2523 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002524 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002527 * We have a locked page in the page cache, now we need to check
2528 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 */
Nick Piggind00806b2007-07-19 01:46:57 -07002530 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 goto page_not_uptodate;
2532
Linus Torvaldsef00e082009-06-16 15:31:25 -07002533 /*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002534 * We've made it this far and we had to drop our mmap_sem, now is the
2535 * time to return to the upper layer and have it re-find the vma and
2536 * redo the fault.
2537 */
2538 if (fpin) {
2539 unlock_page(page);
2540 goto out_retry;
2541 }
2542
2543 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002544 * Found the page and have a reference on it.
2545 * We must recheck i_size under page lock.
2546 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002547 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2548 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002549 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002550 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002551 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002552 }
2553
Nick Piggind0217ac2007-07-19 01:47:03 -07002554 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002555 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /*
2559 * Umm, take care of errors if the page isn't up-to-date.
2560 * Try to re-read it _once_. We do this synchronously,
2561 * because there really aren't any performance issues here
2562 * and we need to check for errors.
2563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 ClearPageError(page);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002565 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002566 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002567 if (!error) {
2568 wait_on_page_locked(page);
2569 if (!PageUptodate(page))
2570 error = -EIO;
2571 }
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002572 if (fpin)
2573 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002574 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002575
2576 if (!error || error == AOP_TRUNCATED_PAGE)
2577 goto retry_find;
2578
2579 /* Things didn't work out. Return zero to tell the mm layer so. */
2580 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002581 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002582
2583out_retry:
2584 /*
2585 * We dropped the mmap_sem, we need to return to the fault handler to
2586 * re-find the vma and come back and find our hopefully still populated
2587 * page.
2588 */
2589 if (page)
2590 put_page(page);
2591 if (fpin)
2592 fput(fpin);
2593 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002594}
2595EXPORT_SYMBOL(filemap_fault);
2596
Jan Kara82b0f8c2016-12-14 15:06:58 -08002597void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002598 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002599{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002600 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002601 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002602 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002603 unsigned long max_idx;
Matthew Wilcox070e8072018-05-17 00:08:30 -04002604 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002605 struct page *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002606
2607 rcu_read_lock();
Matthew Wilcox070e8072018-05-17 00:08:30 -04002608 xas_for_each(&xas, page, end_pgoff) {
2609 if (xas_retry(&xas, page))
2610 continue;
2611 if (xa_is_value(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002612 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002613
Michal Hockoe0975b22018-12-28 00:38:36 -08002614 /*
2615 * Check for a locked page first, as a speculative
2616 * reference may adversely influence page migration.
2617 */
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002618 if (PageLocked(page))
Michal Hockoe0975b22018-12-28 00:38:36 -08002619 goto next;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002620 if (!page_cache_get_speculative(page))
Matthew Wilcox070e8072018-05-17 00:08:30 -04002621 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002622
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002623 /* Has the page moved or been split? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002624 if (unlikely(page != xas_reload(&xas)))
2625 goto skip;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002626 page = find_subpage(page, xas.xa_index);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002627
2628 if (!PageUptodate(page) ||
2629 PageReadahead(page) ||
2630 PageHWPoison(page))
2631 goto skip;
2632 if (!trylock_page(page))
2633 goto skip;
2634
2635 if (page->mapping != mapping || !PageUptodate(page))
2636 goto unlock;
2637
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002638 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2639 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002640 goto unlock;
2641
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002642 if (file->f_ra.mmap_miss > 0)
2643 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002644
Matthew Wilcox070e8072018-05-17 00:08:30 -04002645 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002646 if (vmf->pte)
Matthew Wilcox070e8072018-05-17 00:08:30 -04002647 vmf->pte += xas.xa_index - last_pgoff;
2648 last_pgoff = xas.xa_index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002649 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002650 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002651 unlock_page(page);
2652 goto next;
2653unlock:
2654 unlock_page(page);
2655skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002656 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002657next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002658 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002659 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002660 break;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002661 }
2662 rcu_read_unlock();
2663}
2664EXPORT_SYMBOL(filemap_map_pages);
2665
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002666vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002667{
2668 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002669 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002670 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002671
Jan Kara14da9202012-06-12 16:20:37 +02002672 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002673 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002674 lock_page(page);
2675 if (page->mapping != inode->i_mapping) {
2676 unlock_page(page);
2677 ret = VM_FAULT_NOPAGE;
2678 goto out;
2679 }
Jan Kara14da9202012-06-12 16:20:37 +02002680 /*
2681 * We mark the page dirty already here so that when freeze is in
2682 * progress, we are guaranteed that writeback during freezing will
2683 * see the dirty page and writeprotect it again.
2684 */
2685 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002686 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002687out:
Jan Kara14da9202012-06-12 16:20:37 +02002688 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002689 return ret;
2690}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002691
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002692const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002693 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002694 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002695 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696};
2697
2698/* This is used for a general mmap of a disk file */
2699
2700int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2701{
2702 struct address_space *mapping = file->f_mapping;
2703
2704 if (!mapping->a_ops->readpage)
2705 return -ENOEXEC;
2706 file_accessed(file);
2707 vma->vm_ops = &generic_file_vm_ops;
2708 return 0;
2709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711/*
2712 * This is for filesystems which do not implement ->writepage.
2713 */
2714int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2715{
2716 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2717 return -EINVAL;
2718 return generic_file_mmap(file, vma);
2719}
2720#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002721vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07002722{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002723 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07002724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2726{
2727 return -ENOSYS;
2728}
2729int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2730{
2731 return -ENOSYS;
2732}
2733#endif /* CONFIG_MMU */
2734
Arnd Bergmann453972282018-04-13 15:35:27 -07002735EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736EXPORT_SYMBOL(generic_file_mmap);
2737EXPORT_SYMBOL(generic_file_readonly_mmap);
2738
Sasha Levin67f9fd92014-04-03 14:48:18 -07002739static struct page *wait_on_page_read(struct page *page)
2740{
2741 if (!IS_ERR(page)) {
2742 wait_on_page_locked(page);
2743 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002744 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002745 page = ERR_PTR(-EIO);
2746 }
2747 }
2748 return page;
2749}
2750
Mel Gorman32b63522016-03-15 14:55:36 -07002751static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002752 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002753 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002754 void *data,
2755 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Nick Piggineb2be182007-10-16 01:24:57 -07002757 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 int err;
2759repeat:
2760 page = find_get_page(mapping, index);
2761 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002762 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002763 if (!page)
2764 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002765 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002766 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002767 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002768 if (err == -EEXIST)
2769 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05002770 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return ERR_PTR(err);
2772 }
Mel Gorman32b63522016-03-15 14:55:36 -07002773
2774filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 err = filler(data, page);
2776 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002777 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002778 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Mel Gorman32b63522016-03-15 14:55:36 -07002780
2781 page = wait_on_page_read(page);
2782 if (IS_ERR(page))
2783 return page;
2784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (PageUptodate(page))
2787 goto out;
2788
Mel Gormanebded022016-03-15 14:55:39 -07002789 /*
2790 * Page is not up to date and may be locked due one of the following
2791 * case a: Page is being filled and the page lock is held
2792 * case b: Read/write error clearing the page uptodate status
2793 * case c: Truncation in progress (page locked)
2794 * case d: Reclaim in progress
2795 *
2796 * Case a, the page will be up to date when the page is unlocked.
2797 * There is no need to serialise on the page lock here as the page
2798 * is pinned so the lock gives no additional protection. Even if the
2799 * the page is truncated, the data is still valid if PageUptodate as
2800 * it's a race vs truncate race.
2801 * Case b, the page will not be up to date
2802 * Case c, the page may be truncated but in itself, the data may still
2803 * be valid after IO completes as it's a read vs truncate race. The
2804 * operation must restart if the page is not uptodate on unlock but
2805 * otherwise serialising on page lock to stabilise the mapping gives
2806 * no additional guarantees to the caller as the page lock is
2807 * released before return.
2808 * Case d, similar to truncation. If reclaim holds the page lock, it
2809 * will be a race with remove_mapping that determines if the mapping
2810 * is valid on unlock but otherwise the data is valid and there is
2811 * no need to serialise with page lock.
2812 *
2813 * As the page lock gives no additional guarantee, we optimistically
2814 * wait on the page to be unlocked and check if it's up to date and
2815 * use the page if it is. Otherwise, the page lock is required to
2816 * distinguish between the different cases. The motivation is that we
2817 * avoid spurious serialisations and wakeups when multiple processes
2818 * wait on the same page for IO to complete.
2819 */
2820 wait_on_page_locked(page);
2821 if (PageUptodate(page))
2822 goto out;
2823
2824 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002826
2827 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!page->mapping) {
2829 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002830 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002831 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Mel Gormanebded022016-03-15 14:55:39 -07002833
2834 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (PageUptodate(page)) {
2836 unlock_page(page);
2837 goto out;
2838 }
Mel Gorman32b63522016-03-15 14:55:36 -07002839 goto filler;
2840
David Howellsc855ff32007-05-09 13:42:20 +01002841out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002842 mark_page_accessed(page);
2843 return page;
2844}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002845
2846/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002847 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002848 * @mapping: the page's address_space
2849 * @index: the page index
2850 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002851 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002852 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002853 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002854 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002855 *
2856 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002857 *
2858 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002859 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002860struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002861 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002862 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002863 void *data)
2864{
2865 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2866}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002867EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002868
2869/**
2870 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2871 * @mapping: the page's address_space
2872 * @index: the page index
2873 * @gfp: the page allocator flags to use if allocating
2874 *
2875 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002876 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002877 *
2878 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002879 *
2880 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002881 */
2882struct page *read_cache_page_gfp(struct address_space *mapping,
2883 pgoff_t index,
2884 gfp_t gfp)
2885{
2886 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2887
Sasha Levin67f9fd92014-04-03 14:48:18 -07002888 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002889}
2890EXPORT_SYMBOL(read_cache_page_gfp);
2891
Nick Piggin2f718ff2007-10-16 01:24:59 -07002892/*
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002893 * Don't operate on ranges the page cache doesn't support, and don't exceed the
2894 * LFS limits. If pos is under the limit it becomes a short access. If it
2895 * exceeds the limit we return -EFBIG.
2896 */
2897static int generic_access_check_limits(struct file *file, loff_t pos,
2898 loff_t *count)
2899{
2900 struct inode *inode = file->f_mapping->host;
2901 loff_t max_size = inode->i_sb->s_maxbytes;
2902
2903 if (!(file->f_flags & O_LARGEFILE))
2904 max_size = MAX_NON_LFS;
2905
2906 if (unlikely(pos >= max_size))
2907 return -EFBIG;
2908 *count = min(*count, max_size - pos);
2909 return 0;
2910}
2911
2912static int generic_write_check_limits(struct file *file, loff_t pos,
2913 loff_t *count)
2914{
2915 loff_t limit = rlimit(RLIMIT_FSIZE);
2916
2917 if (limit != RLIM_INFINITY) {
2918 if (pos >= limit) {
2919 send_sig(SIGXFSZ, current, 0);
2920 return -EFBIG;
2921 }
2922 *count = min(*count, limit - pos);
2923 }
2924
2925 return generic_access_check_limits(file, pos, count);
2926}
2927
2928/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 * Performs necessary checks before doing a write
2930 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002931 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 * Returns appropriate error code that caller should return or
2933 * zero in case that write should be allowed.
2934 */
Al Viro3309dd02015-04-09 12:55:47 -04002935inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Al Viro3309dd02015-04-09 12:55:47 -04002937 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct inode *inode = file->f_mapping->host;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002939 loff_t count;
2940 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Al Viro3309dd02015-04-09 12:55:47 -04002942 if (!iov_iter_count(from))
2943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Al Viro0fa6b002015-04-04 04:05:48 -04002945 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002946 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002947 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002949 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2950 return -EINVAL;
2951
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002952 count = iov_iter_count(from);
2953 ret = generic_write_check_limits(file, iocb->ki_pos, &count);
2954 if (ret)
2955 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002957 iov_iter_truncate(from, count);
Al Viro3309dd02015-04-09 12:55:47 -04002958 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960EXPORT_SYMBOL(generic_write_checks);
2961
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002962/*
2963 * Performs necessary checks before doing a clone.
2964 *
2965 * Can adjust amount of bytes to clone.
2966 * Returns appropriate error code that caller should return or
2967 * zero in case the clone should be allowed.
2968 */
2969int generic_remap_checks(struct file *file_in, loff_t pos_in,
2970 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002971 loff_t *req_count, unsigned int remap_flags)
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002972{
2973 struct inode *inode_in = file_in->f_mapping->host;
2974 struct inode *inode_out = file_out->f_mapping->host;
2975 uint64_t count = *req_count;
2976 uint64_t bcount;
2977 loff_t size_in, size_out;
2978 loff_t bs = inode_out->i_sb->s_blocksize;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002979 int ret;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002980
2981 /* The start of both ranges must be aligned to an fs block. */
2982 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
2983 return -EINVAL;
2984
2985 /* Ensure offsets don't wrap. */
2986 if (pos_in + count < pos_in || pos_out + count < pos_out)
2987 return -EINVAL;
2988
2989 size_in = i_size_read(inode_in);
2990 size_out = i_size_read(inode_out);
2991
2992 /* Dedupe requires both ranges to be within EOF. */
Darrick J. Wong3d281932018-10-30 10:41:34 +11002993 if ((remap_flags & REMAP_FILE_DEDUP) &&
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002994 (pos_in >= size_in || pos_in + count > size_in ||
2995 pos_out >= size_out || pos_out + count > size_out))
2996 return -EINVAL;
2997
2998 /* Ensure the infile range is within the infile. */
2999 if (pos_in >= size_in)
3000 return -EINVAL;
3001 count = min(count, size_in - (uint64_t)pos_in);
3002
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003003 ret = generic_access_check_limits(file_in, pos_in, &count);
3004 if (ret)
3005 return ret;
3006
3007 ret = generic_write_check_limits(file_out, pos_out, &count);
3008 if (ret)
3009 return ret;
3010
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003011 /*
3012 * If the user wanted us to link to the infile's EOF, round up to the
3013 * next block boundary for this check.
3014 *
3015 * Otherwise, make sure the count is also block-aligned, having
3016 * already confirmed the starting offsets' block alignment.
3017 */
3018 if (pos_in + count == size_in) {
3019 bcount = ALIGN(size_in, bs) - pos_in;
3020 } else {
3021 if (!IS_ALIGNED(count, bs))
Darrick J. Wongeca36542018-10-30 10:42:10 +11003022 count = ALIGN_DOWN(count, bs);
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003023 bcount = count;
3024 }
3025
3026 /* Don't allow overlapped cloning within the same file. */
3027 if (inode_in == inode_out &&
3028 pos_out + bcount > pos_in &&
3029 pos_out < pos_in + bcount)
3030 return -EINVAL;
3031
Darrick J. Wongeca36542018-10-30 10:42:10 +11003032 /*
3033 * We shortened the request but the caller can't deal with that, so
3034 * bounce the request back to userspace.
3035 */
3036 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003037 return -EINVAL;
3038
Darrick J. Wongeca36542018-10-30 10:42:10 +11003039 *req_count = count;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003040 return 0;
3041}
3042
Nick Pigginafddba42007-10-16 01:25:01 -07003043int pagecache_write_begin(struct file *file, struct address_space *mapping,
3044 loff_t pos, unsigned len, unsigned flags,
3045 struct page **pagep, void **fsdata)
3046{
3047 const struct address_space_operations *aops = mapping->a_ops;
3048
Nick Piggin4e02ed42008-10-29 14:00:55 -07003049 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003050 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003051}
3052EXPORT_SYMBOL(pagecache_write_begin);
3053
3054int pagecache_write_end(struct file *file, struct address_space *mapping,
3055 loff_t pos, unsigned len, unsigned copied,
3056 struct page *page, void *fsdata)
3057{
3058 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003059
Nick Piggin4e02ed42008-10-29 14:00:55 -07003060 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003061}
3062EXPORT_SYMBOL(pagecache_write_end);
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003065generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 struct file *file = iocb->ki_filp;
3068 struct address_space *mapping = file->f_mapping;
3069 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003070 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003072 size_t write_len;
3073 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Al Viro0c949332014-03-22 06:51:37 -04003075 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003076 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003077
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003078 if (iocb->ki_flags & IOCB_NOWAIT) {
3079 /* If there are pages to writeback, return */
3080 if (filemap_range_has_page(inode->i_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003081 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003082 return -EAGAIN;
3083 } else {
3084 written = filemap_write_and_wait_range(mapping, pos,
3085 pos + write_len - 1);
3086 if (written)
3087 goto out;
3088 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003089
3090 /*
3091 * After a write we want buffered reads to be sure to go to disk to get
3092 * the new data. We invalidate clean cached page from the region we're
3093 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003094 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003095 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003096 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003097 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003098 /*
3099 * If a page can not be invalidated, return 0 to fall back
3100 * to buffered write.
3101 */
3102 if (written) {
3103 if (written == -EBUSY)
3104 return 0;
3105 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003106 }
3107
Al Viro639a93a52017-04-13 14:10:15 -04003108 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003109
3110 /*
3111 * Finally, try again to invalidate clean pages which might have been
3112 * cached by non-direct readahead, or faulted in by get_user_pages()
3113 * if the source of the write was an mmap'ed region of the file
3114 * we're writing. Either one is a pretty crazy thing to do,
3115 * so we don't support it 100%. If this invalidation
3116 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003117 *
3118 * Most of the time we do not need this since dio_complete() will do
3119 * the invalidation for us. However there are some file systems that
3120 * do not end up with dio_complete() being called, so let's not break
3121 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003122 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003123 if (mapping->nrpages)
3124 invalidate_inode_pages2_range(mapping,
3125 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003128 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003129 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003130 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3131 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 mark_inode_dirty(inode);
3133 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003134 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
Al Viro639a93a52017-04-13 14:10:15 -04003136 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003137out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return written;
3139}
3140EXPORT_SYMBOL(generic_file_direct_write);
3141
Nick Piggineb2be182007-10-16 01:24:57 -07003142/*
3143 * Find or create a page at the given pagecache position. Return the locked
3144 * page. This function is specifically for buffered writes.
3145 */
Nick Piggin54566b22009-01-04 12:00:53 -08003146struct page *grab_cache_page_write_begin(struct address_space *mapping,
3147 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003148{
Nick Piggineb2be182007-10-16 01:24:57 -07003149 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003150 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003151
Nick Piggin54566b22009-01-04 12:00:53 -08003152 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003153 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003154
Mel Gorman2457aec2014-06-04 16:10:31 -07003155 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003156 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003157 if (page)
3158 wait_for_stable_page(page);
3159
Nick Piggineb2be182007-10-16 01:24:57 -07003160 return page;
3161}
Nick Piggin54566b22009-01-04 12:00:53 -08003162EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003163
Al Viro3b93f912014-02-11 21:34:08 -05003164ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003165 struct iov_iter *i, loff_t pos)
3166{
3167 struct address_space *mapping = file->f_mapping;
3168 const struct address_space_operations *a_ops = mapping->a_ops;
3169 long status = 0;
3170 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003171 unsigned int flags = 0;
3172
Nick Pigginafddba42007-10-16 01:25:01 -07003173 do {
3174 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003175 unsigned long offset; /* Offset into pagecache page */
3176 unsigned long bytes; /* Bytes to write to page */
3177 size_t copied; /* Bytes copied from user */
3178 void *fsdata;
3179
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003180 offset = (pos & (PAGE_SIZE - 1));
3181 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003182 iov_iter_count(i));
3183
3184again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003185 /*
3186 * Bring in the user page that we will copy from _first_.
3187 * Otherwise there's a nasty deadlock on copying from the
3188 * same page as we're writing to, without it being marked
3189 * up-to-date.
3190 *
3191 * Not only is this an optimisation, but it is also required
3192 * to check that the address is actually valid, when atomic
3193 * usercopies are used, below.
3194 */
3195 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3196 status = -EFAULT;
3197 break;
3198 }
3199
Jan Kara296291c2015-10-22 13:32:21 -07003200 if (fatal_signal_pending(current)) {
3201 status = -EINTR;
3202 break;
3203 }
3204
Nick Piggin674b8922007-10-16 01:25:03 -07003205 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003206 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003207 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003208 break;
3209
anfei zhou931e80e2010-02-02 13:44:02 -08003210 if (mapping_writably_mapped(mapping))
3211 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003212
Nick Pigginafddba42007-10-16 01:25:01 -07003213 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003214 flush_dcache_page(page);
3215
3216 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3217 page, fsdata);
3218 if (unlikely(status < 0))
3219 break;
3220 copied = status;
3221
3222 cond_resched();
3223
Nick Piggin124d3b72008-02-02 15:01:17 +01003224 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003225 if (unlikely(copied == 0)) {
3226 /*
3227 * If we were unable to copy any data at all, we must
3228 * fall back to a single segment length write.
3229 *
3230 * If we didn't fallback here, we could livelock
3231 * because not all segments in the iov can be copied at
3232 * once without a pagefault.
3233 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003234 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003235 iov_iter_single_seg_count(i));
3236 goto again;
3237 }
Nick Pigginafddba42007-10-16 01:25:01 -07003238 pos += copied;
3239 written += copied;
3240
3241 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003242 } while (iov_iter_count(i));
3243
3244 return written ? written : status;
3245}
Al Viro3b93f912014-02-11 21:34:08 -05003246EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Jan Karae4dd9de2009-08-17 18:10:06 +02003248/**
Al Viro81742022014-04-03 03:17:43 -04003249 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003250 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003251 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003252 *
3253 * This function does all the work needed for actually writing data to a
3254 * file. It does all basic checks, removes SUID from the file, updates
3255 * modification times and calls proper subroutines depending on whether we
3256 * do direct IO or a standard buffered write.
3257 *
3258 * It expects i_mutex to be grabbed unless we work on a block device or similar
3259 * object which does not need locking at all.
3260 *
3261 * This function does *not* take care of syncing data in case of O_SYNC write.
3262 * A caller has to handle it. This is mainly due to the fact that we want to
3263 * avoid syncing under i_mutex.
Mike Rapoporta862f682019-03-05 15:48:42 -08003264 *
3265 * Return:
3266 * * number of bytes written, even for truncated writes
3267 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003268 */
Al Viro81742022014-04-03 03:17:43 -04003269ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003272 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003274 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003276 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003279 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003280 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (err)
3282 goto out;
3283
Josef Bacikc3b2da32012-03-26 09:59:21 -04003284 err = file_update_time(file);
3285 if (err)
3286 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Al Viro2ba48ce2015-04-09 13:52:01 -04003288 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003289 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003290
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003291 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003292 /*
3293 * If the write stopped short of completing, fall back to
3294 * buffered writes. Some filesystems do this for writes to
3295 * holes, for example. For DAX files, a buffered write will
3296 * not succeed (even if it did, DAX does not handle dirty
3297 * page-cache pages correctly).
3298 */
Al Viro0b8def92015-04-07 10:22:53 -04003299 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003301
Al Viro0b8def92015-04-07 10:22:53 -04003302 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003303 /*
Al Viro3b93f912014-02-11 21:34:08 -05003304 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003305 * then we want to return the number of bytes which were
3306 * direct-written, or the error code if that was zero. Note
3307 * that this differs from normal direct-io semantics, which
3308 * will return -EFOO even if some bytes were written.
3309 */
Al Viro60bb4522014-08-08 12:39:16 -04003310 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003311 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003312 goto out;
3313 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003314 /*
3315 * We need to ensure that the page cache pages are written to
3316 * disk and invalidated to preserve the expected O_DIRECT
3317 * semantics.
3318 */
Al Viro3b93f912014-02-11 21:34:08 -05003319 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003320 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003321 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003322 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003323 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003324 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003325 pos >> PAGE_SHIFT,
3326 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003327 } else {
3328 /*
3329 * We don't know how much we wrote, so just return
3330 * the number of bytes which were direct-written
3331 */
3332 }
3333 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003334 written = generic_perform_write(file, from, iocb->ki_pos);
3335 if (likely(written > 0))
3336 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338out:
3339 current->backing_dev_info = NULL;
3340 return written ? written : err;
3341}
Al Viro81742022014-04-03 03:17:43 -04003342EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Jan Karae4dd9de2009-08-17 18:10:06 +02003344/**
Al Viro81742022014-04-03 03:17:43 -04003345 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003346 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003347 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003348 *
Al Viro81742022014-04-03 03:17:43 -04003349 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003350 * filesystems. It takes care of syncing the file in case of O_SYNC file
3351 * and acquires i_mutex as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003352 * Return:
3353 * * negative error code if no data has been written at all of
3354 * vfs_fsync_range() failed for a synchronous write
3355 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003356 */
Al Viro81742022014-04-03 03:17:43 -04003357ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003360 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Al Viro59551022016-01-22 15:40:57 -05003363 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003364 ret = generic_write_checks(iocb, from);
3365 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003366 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003367 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Christoph Hellwige2592212016-04-07 08:52:01 -07003369 if (ret > 0)
3370 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return ret;
3372}
Al Viro81742022014-04-03 03:17:43 -04003373EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
David Howellscf9a2ae2006-08-29 19:05:54 +01003375/**
3376 * try_to_release_page() - release old fs-specific metadata on a page
3377 *
3378 * @page: the page which the kernel is trying to free
3379 * @gfp_mask: memory allocation flags (and I/O mode)
3380 *
3381 * The address_space is to try to release any data against the page
Mike Rapoporta862f682019-03-05 15:48:42 -08003382 * (presumably at page->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003383 *
David Howells266cf652009-04-03 16:42:36 +01003384 * This may also be called if PG_fscache is set on a page, indicating that the
3385 * page is known to the local caching routines.
3386 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003387 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003388 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003389 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003390 * Return: %1 if the release was successful, otherwise return zero.
David Howellscf9a2ae2006-08-29 19:05:54 +01003391 */
3392int try_to_release_page(struct page *page, gfp_t gfp_mask)
3393{
3394 struct address_space * const mapping = page->mapping;
3395
3396 BUG_ON(!PageLocked(page));
3397 if (PageWriteback(page))
3398 return 0;
3399
3400 if (mapping && mapping->a_ops->releasepage)
3401 return mapping->a_ops->releasepage(page, gfp_mask);
3402 return try_to_free_buffers(page);
3403}
3404
3405EXPORT_SYMBOL(try_to_release_page);