blob: 82f3b9a3a9406626d9bc1de9ddff3516dcb764c2 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/filemap.c
4 *
5 * Copyright (C) 1994-1999 Linus Torvalds
6 */
7
8/*
9 * This file handles the generic file mmap semantics used by
10 * most "normal" filesystems (but you don't /have/ to use this:
11 * the NFS filesystem used to do this differently, for example)
12 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080015#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070018#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
23#include <linux/swap.h>
24#include <linux/mman.h>
25#include <linux/pagemap.h>
26#include <linux/file.h>
27#include <linux/uio.h>
Josef Bacikcfcbfb12019-05-13 17:21:04 -070028#include <linux/error-injection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/hash.h>
30#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070031#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/pagevec.h>
33#include <linux/blkdev.h>
34#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080035#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070036#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080037#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060038#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080039#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070040#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070041#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070042#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080043#include "internal.h"
44
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070045#define CREATE_TRACE_POINTS
46#include <trace/events/filemap.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * FIXME: remove all knowledge of the buffer layer from the core VM
50 */
Jan Kara148f9482009-08-17 19:52:36 +020051#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/mman.h>
54
55/*
56 * Shared mappings implemented 30.11.1994. It's not fully working yet,
57 * though.
58 *
59 * Shared mappings now work. 15.8.1995 Bruno.
60 *
61 * finished 'unifying' the page and buffer cache and SMP-threaded the
62 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
63 *
64 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
65 */
66
67/*
68 * Lock ordering:
69 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080070 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070072 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070073 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080075 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080076 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080079 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070080 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070081 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 * ->mmap_sem
84 * ->lock_page (access_process_vm)
85 *
Al Viroccad2362014-02-11 22:36:48 -050086 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070087 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060089 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110090 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070091 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080093 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ->anon_vma.lock (vma_adjust)
95 *
96 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070097 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070099 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -0700100 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700102 * ->i_pages lock (try_to_unmap_one)
Andrey Ryabininf4b7e272019-03-05 15:49:39 -0800103 * ->pgdat->lru_lock (follow_page->mark_page_accessed)
104 * ->pgdat->lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700106 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600107 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100108 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700109 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600110 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100111 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
113 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800114 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700115 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500118static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700119 struct page *page, void *shadow)
120{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500121 XA_STATE(xas, &mapping->i_pages, page->index);
122 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800123
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500124 mapping_set_update(&xas, mapping);
125
126 /* hugetlb pages are represented by a single entry in the xarray */
127 if (!PageHuge(page)) {
128 xas_set_order(&xas, page->index, compound_order(page));
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700129 nr = compound_nr(page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500130 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700131
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700132 VM_BUG_ON_PAGE(!PageLocked(page), page);
133 VM_BUG_ON_PAGE(PageTail(page), page);
134 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700135
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500136 xas_store(&xas, shadow);
137 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200138
Jan Kara23006382017-11-15 17:37:26 -0800139 page->mapping = NULL;
140 /* Leave page->index set: truncation lookup relies upon it */
141
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200142 if (shadow) {
143 mapping->nrexceptional += nr;
144 /*
145 * Make sure the nrexceptional update is committed before
146 * the nrpages update so that final truncate racing
147 * with reclaim does not see both counters 0 at the
148 * same time and miss a shadow entry.
149 */
150 smp_wmb();
151 }
152 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700153}
154
Jan Kara5ecc4d82017-11-15 17:37:29 -0800155static void unaccount_page_cache_page(struct address_space *mapping,
156 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800158 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600160 /*
161 * if we're uptodate, flush out into the cleancache, otherwise
162 * invalidate any existing cleancache entries. We can't leave
163 * stale data around in the cleancache once our page is gone
164 */
165 if (PageUptodate(page) && PageMappedToDisk(page))
166 cleancache_put_page(page);
167 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400168 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600169
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700170 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800171 VM_BUG_ON_PAGE(page_mapped(page), page);
172 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
173 int mapcount;
174
175 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
176 current->comm, page_to_pfn(page));
177 dump_page(page, "still mapped when deleted");
178 dump_stack();
179 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
180
181 mapcount = page_mapcount(page);
182 if (mapping_exiting(mapping) &&
183 page_count(page) >= mapcount + 2) {
184 /*
185 * All vmas have already been torn down, so it's
186 * a good bet that actually the page is unmapped,
187 * and we'd prefer not to leak it: if we're wrong,
188 * some other bad page check should catch it later.
189 */
190 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700191 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800192 }
193 }
194
Jan Kara76253fb2017-11-15 17:37:22 -0800195 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800196 if (PageHuge(page))
197 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800198
Jan Kara5ecc4d82017-11-15 17:37:29 -0800199 nr = hpage_nr_pages(page);
200
201 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
202 if (PageSwapBacked(page)) {
203 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
204 if (PageTransHuge(page))
205 __dec_node_page_state(page, NR_SHMEM_THPS);
206 } else {
207 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800208 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800209
210 /*
211 * At this point page must be either written or cleaned by
212 * truncate. Dirty page here signals a bug and loss of
213 * unwritten data.
214 *
215 * This fixes dirty accounting after removing the page entirely
216 * but leaves PageDirty set: it has no effect for truncated
217 * page and anyway will be cleared before returning page into
218 * buddy allocator.
219 */
220 if (WARN_ON_ONCE(PageDirty(page)))
221 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
222}
223
224/*
225 * Delete a page from the page cache and free it. Caller has to make
226 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700227 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800228 */
229void __delete_from_page_cache(struct page *page, void *shadow)
230{
231 struct address_space *mapping = page->mapping;
232
233 trace_mm_filemap_delete_from_page_cache(page);
234
235 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500236 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Jan Kara59c66c52017-11-15 17:37:18 -0800239static void page_cache_free_page(struct address_space *mapping,
240 struct page *page)
241{
242 void (*freepage)(struct page *);
243
244 freepage = mapping->a_ops->freepage;
245 if (freepage)
246 freepage(page);
247
248 if (PageTransHuge(page) && !PageHuge(page)) {
249 page_ref_sub(page, HPAGE_PMD_NR);
250 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
251 } else {
252 put_page(page);
253 }
254}
255
Minchan Kim702cfbf2011-03-22 16:32:43 -0700256/**
257 * delete_from_page_cache - delete page from page cache
258 * @page: the page which the kernel is trying to remove from page cache
259 *
260 * This must be called only on pages that have been verified to be in the page
261 * cache and locked. It will never put the page into the free list, the caller
262 * has a reference on the page.
263 */
264void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700266 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400267 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Matt Mackallcd7619d2005-05-01 08:59:01 -0700269 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700270 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700271 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700272 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500273
Jan Kara59c66c52017-11-15 17:37:18 -0800274 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700275}
276EXPORT_SYMBOL(delete_from_page_cache);
277
Jan Karaaa65c292017-11-15 17:37:33 -0800278/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500279 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800280 * @mapping: the mapping to which pages belong
281 * @pvec: pagevec with pages to delete
282 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700283 * The function walks over mapping->i_pages and removes pages passed in @pvec
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700284 * from the mapping. The function expects @pvec to be sorted by page index.
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
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700287 * @pvec and takes care to delete all corresponding tail pages from the
288 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800289 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700290 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800291 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500292static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800293 struct pagevec *pvec)
294{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500295 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800296 int total_pages = 0;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700297 int i = 0, tail_pages = 0;
Jan Karaaa65c292017-11-15 17:37:33 -0800298 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800299
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500300 mapping_set_update(&xas, mapping);
301 xas_for_each(&xas, page, ULONG_MAX) {
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700302 if (i >= pagevec_count(pvec) && !tail_pages)
Jan Karaaa65c292017-11-15 17:37:33 -0800303 break;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400304 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800305 continue;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700306 if (!tail_pages) {
307 /*
308 * Some page got inserted in our range? Skip it. We
309 * have our pages locked so they are protected from
310 * being removed.
311 */
312 if (page != pvec->pages[i]) {
313 VM_BUG_ON_PAGE(page->index >
314 pvec->pages[i]->index, page);
315 continue;
316 }
317 WARN_ON_ONCE(!PageLocked(page));
318 if (PageTransHuge(page) && !PageHuge(page))
319 tail_pages = HPAGE_PMD_NR - 1;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700320 page->mapping = NULL;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700321 /*
322 * Leave page->index set: truncation lookup relies
323 * upon it
324 */
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700325 i++;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700326 } else {
327 VM_BUG_ON_PAGE(page->index + HPAGE_PMD_NR - tail_pages
328 != pvec->pages[i]->index, page);
329 tail_pages--;
330 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500331 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800332 total_pages++;
333 }
334 mapping->nrpages -= total_pages;
335}
336
337void delete_from_page_cache_batch(struct address_space *mapping,
338 struct pagevec *pvec)
339{
340 int i;
341 unsigned long flags;
342
343 if (!pagevec_count(pvec))
344 return;
345
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700346 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800347 for (i = 0; i < pagevec_count(pvec); i++) {
348 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
349
350 unaccount_page_cache_page(mapping, pvec->pages[i]);
351 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500352 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700353 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800354
355 for (i = 0; i < pagevec_count(pvec); i++)
356 page_cache_free_page(mapping, pvec->pages[i]);
357}
358
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200359int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700360{
361 int ret = 0;
362 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700363 if (test_bit(AS_ENOSPC, &mapping->flags) &&
364 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700365 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700366 if (test_bit(AS_EIO, &mapping->flags) &&
367 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700368 ret = -EIO;
369 return ret;
370}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200371EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700372
Jeff Layton76341ca2017-07-06 07:02:22 -0400373static int filemap_check_and_keep_errors(struct address_space *mapping)
374{
375 /* Check for outstanding write errors */
376 if (test_bit(AS_EIO, &mapping->flags))
377 return -EIO;
378 if (test_bit(AS_ENOSPC, &mapping->flags))
379 return -ENOSPC;
380 return 0;
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700384 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700385 * @mapping: address space structure to write
386 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800387 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700388 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700390 * Start writeback against all of a mapping's dirty pages that lie
391 * within the byte offsets <start, end> inclusive.
392 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700394 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * these two operations is that if a dirty page/buffer is encountered, it must
396 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800397 *
398 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800400int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
401 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 int ret;
404 struct writeback_control wbc = {
405 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800406 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700407 .range_start = start,
408 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 };
410
Konstantin Khlebnikovc3aab9a02019-09-23 15:34:45 -0700411 if (!mapping_cap_writeback_dirty(mapping) ||
412 !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414
Tejun Heob16b1de2015-06-02 08:39:48 -0600415 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600417 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return ret;
419}
420
421static inline int __filemap_fdatawrite(struct address_space *mapping,
422 int sync_mode)
423{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700424 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427int filemap_fdatawrite(struct address_space *mapping)
428{
429 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
430}
431EXPORT_SYMBOL(filemap_fdatawrite);
432
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400433int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800434 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
437}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400438EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Randy Dunlap485bb992006-06-23 02:03:49 -0700440/**
441 * filemap_flush - mostly a non-blocking flush
442 * @mapping: target address_space
443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * This is a mostly non-blocking flush. Not suitable for data-integrity
445 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800446 *
447 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
449int filemap_flush(struct address_space *mapping)
450{
451 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
452}
453EXPORT_SYMBOL(filemap_flush);
454
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500455/**
456 * filemap_range_has_page - check if a page exists in range.
457 * @mapping: address space within which to check
458 * @start_byte: offset in bytes where the range starts
459 * @end_byte: offset in bytes where the range ends (inclusive)
460 *
461 * Find at least one page in the range supplied, usually used to check if
462 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800463 *
464 * Return: %true if at least one page exists in the specified range,
465 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500466 */
467bool filemap_range_has_page(struct address_space *mapping,
468 loff_t start_byte, loff_t end_byte)
469{
Jan Karaf7b68042017-09-06 16:21:40 -0700470 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500471 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
472 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500473
474 if (end_byte < start_byte)
475 return false;
476
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500477 rcu_read_lock();
478 for (;;) {
479 page = xas_find(&xas, max);
480 if (xas_retry(&xas, page))
481 continue;
482 /* Shadow entries don't count */
483 if (xa_is_value(page))
484 continue;
485 /*
486 * We don't need to try to pin this page; we're about to
487 * release the RCU lock anyway. It is enough to know that
488 * there was a page here recently.
489 */
490 break;
491 }
492 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500493
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500494 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500495}
496EXPORT_SYMBOL(filemap_range_has_page);
497
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400498static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800499 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300501 pgoff_t index = start_byte >> PAGE_SHIFT;
502 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct pagevec pvec;
504 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200506 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400507 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Mel Gorman86679822017-11-15 17:37:52 -0800509 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800510 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned i;
512
Jan Kara312e9d22017-11-15 17:35:05 -0800513 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800514 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800515 if (!nr_pages)
516 break;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 for (i = 0; i < nr_pages; i++) {
519 struct page *page = pvec.pages[i];
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400522 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 pagevec_release(&pvec);
525 cond_resched();
526 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800527}
528
529/**
530 * filemap_fdatawait_range - wait for writeback to complete
531 * @mapping: address space structure to wait for
532 * @start_byte: offset in bytes where the range starts
533 * @end_byte: offset in bytes where the range ends (inclusive)
534 *
535 * Walk the list of under-writeback pages of the given address space
536 * in the given range and wait for all of them. Check error status of
537 * the address space and return it.
538 *
539 * Since the error status of the address space is cleared by this function,
540 * callers are responsible for checking the return value and handling and/or
541 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800542 *
543 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800544 */
545int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
546 loff_t end_byte)
547{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400548 __filemap_fdatawait_range(mapping, start_byte, end_byte);
549 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200551EXPORT_SYMBOL(filemap_fdatawait_range);
552
553/**
Ross Zwisleraa0bfcd2019-06-20 17:05:37 -0400554 * filemap_fdatawait_range_keep_errors - wait for writeback to complete
555 * @mapping: address space structure to wait for
556 * @start_byte: offset in bytes where the range starts
557 * @end_byte: offset in bytes where the range ends (inclusive)
558 *
559 * Walk the list of under-writeback pages of the given address space in the
560 * given range and wait for all of them. Unlike filemap_fdatawait_range(),
561 * this function does not clear error status of the address space.
562 *
563 * Use this function if callers don't handle errors themselves. Expected
564 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
565 * fsfreeze(8)
566 */
567int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
568 loff_t start_byte, loff_t end_byte)
569{
570 __filemap_fdatawait_range(mapping, start_byte, end_byte);
571 return filemap_check_and_keep_errors(mapping);
572}
573EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
574
575/**
Jeff Laytona823e452017-07-28 07:24:43 -0400576 * file_fdatawait_range - wait for writeback to complete
577 * @file: file pointing to address space structure to wait for
578 * @start_byte: offset in bytes where the range starts
579 * @end_byte: offset in bytes where the range ends (inclusive)
580 *
581 * Walk the list of under-writeback pages of the address space that file
582 * refers to, in the given range and wait for all of them. Check error
583 * status of the address space vs. the file->f_wb_err cursor and return it.
584 *
585 * Since the error status of the file is advanced by this function,
586 * callers are responsible for checking the return value and handling and/or
587 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800588 *
589 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400590 */
591int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
592{
593 struct address_space *mapping = file->f_mapping;
594
595 __filemap_fdatawait_range(mapping, start_byte, end_byte);
596 return file_check_and_advance_wb_err(file);
597}
598EXPORT_SYMBOL(file_fdatawait_range);
599
600/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800601 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
602 * @mapping: address space structure to wait for
603 *
604 * Walk the list of under-writeback pages of the given address space
605 * and wait for all of them. Unlike filemap_fdatawait(), this function
606 * does not clear error status of the address space.
607 *
608 * Use this function if callers don't handle errors themselves. Expected
609 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
610 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800611 *
612 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800613 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400614int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800615{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400616 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400617 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800618}
Jeff Layton76341ca2017-07-06 07:02:22 -0400619EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800620
Jeff Layton9326c9b2017-07-26 10:21:11 -0400621static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400623 return (!dax_mapping(mapping) && mapping->nrpages) ||
624 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627int filemap_write_and_wait(struct address_space *mapping)
628{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800629 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jeff Layton9326c9b2017-07-26 10:21:11 -0400631 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800632 err = filemap_fdatawrite(mapping);
633 /*
634 * Even if the above returned error, the pages may be
635 * written partially (e.g. -ENOSPC), so we wait for it.
636 * But the -EIO is special case, it may indicate the worst
637 * thing (e.g. bug) happened, so we avoid waiting for it.
638 */
639 if (err != -EIO) {
640 int err2 = filemap_fdatawait(mapping);
641 if (!err)
642 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400643 } else {
644 /* Clear any previously stored errors */
645 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800646 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700647 } else {
648 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800650 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800652EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Randy Dunlap485bb992006-06-23 02:03:49 -0700654/**
655 * filemap_write_and_wait_range - write out & wait on a file range
656 * @mapping: the address_space for the pages
657 * @lstart: offset in bytes where the range starts
658 * @lend: offset in bytes where the range ends (inclusive)
659 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800660 * Write out and wait upon file offsets lstart->lend, inclusive.
661 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300662 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800663 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800664 *
665 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667int filemap_write_and_wait_range(struct address_space *mapping,
668 loff_t lstart, loff_t lend)
669{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800670 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jeff Layton9326c9b2017-07-26 10:21:11 -0400672 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800673 err = __filemap_fdatawrite_range(mapping, lstart, lend,
674 WB_SYNC_ALL);
675 /* See comment of filemap_write_and_wait() */
676 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200677 int err2 = filemap_fdatawait_range(mapping,
678 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800679 if (!err)
680 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400681 } else {
682 /* Clear any previously stored errors */
683 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800684 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700685 } else {
686 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Chris Masonf6995582009-04-15 13:22:37 -0400690EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jeff Layton5660e132017-07-06 07:02:25 -0400692void __filemap_set_wb_err(struct address_space *mapping, int err)
693{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400694 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400695
696 trace_filemap_set_wb_err(mapping, eseq);
697}
698EXPORT_SYMBOL(__filemap_set_wb_err);
699
700/**
701 * file_check_and_advance_wb_err - report wb error (if any) that was previously
702 * and advance wb_err to current one
703 * @file: struct file on which the error is being reported
704 *
705 * When userland calls fsync (or something like nfsd does the equivalent), we
706 * want to report any writeback errors that occurred since the last fsync (or
707 * since the file was opened if there haven't been any).
708 *
709 * Grab the wb_err from the mapping. If it matches what we have in the file,
710 * then just quickly return 0. The file is all caught up.
711 *
712 * If it doesn't match, then take the mapping value, set the "seen" flag in
713 * it and try to swap it into place. If it works, or another task beat us
714 * to it with the new value, then update the f_wb_err and return the error
715 * portion. The error at this point must be reported via proper channels
716 * (a'la fsync, or NFS COMMIT operation, etc.).
717 *
718 * While we handle mapping->wb_err with atomic operations, the f_wb_err
719 * value is protected by the f_lock since we must ensure that it reflects
720 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800721 *
722 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400723 */
724int file_check_and_advance_wb_err(struct file *file)
725{
726 int err = 0;
727 errseq_t old = READ_ONCE(file->f_wb_err);
728 struct address_space *mapping = file->f_mapping;
729
730 /* Locklessly handle the common case where nothing has changed */
731 if (errseq_check(&mapping->wb_err, old)) {
732 /* Something changed, must use slow path */
733 spin_lock(&file->f_lock);
734 old = file->f_wb_err;
735 err = errseq_check_and_advance(&mapping->wb_err,
736 &file->f_wb_err);
737 trace_file_check_and_advance_wb_err(file, old);
738 spin_unlock(&file->f_lock);
739 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700740
741 /*
742 * We're mostly using this function as a drop in replacement for
743 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
744 * that the legacy code would have had on these flags.
745 */
746 clear_bit(AS_EIO, &mapping->flags);
747 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400748 return err;
749}
750EXPORT_SYMBOL(file_check_and_advance_wb_err);
751
752/**
753 * file_write_and_wait_range - write out & wait on a file range
754 * @file: file pointing to address_space with pages
755 * @lstart: offset in bytes where the range starts
756 * @lend: offset in bytes where the range ends (inclusive)
757 *
758 * Write out and wait upon file offsets lstart->lend, inclusive.
759 *
760 * Note that @lend is inclusive (describes the last byte to be written) so
761 * that this function can be used to write to the very end-of-file (end = -1).
762 *
763 * After writing out and waiting on the data, we check and advance the
764 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800765 *
766 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400767 */
768int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
769{
770 int err = 0, err2;
771 struct address_space *mapping = file->f_mapping;
772
Jeff Layton9326c9b2017-07-26 10:21:11 -0400773 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400774 err = __filemap_fdatawrite_range(mapping, lstart, lend,
775 WB_SYNC_ALL);
776 /* See comment of filemap_write_and_wait() */
777 if (err != -EIO)
778 __filemap_fdatawait_range(mapping, lstart, lend);
779 }
780 err2 = file_check_and_advance_wb_err(file);
781 if (!err)
782 err = err2;
783 return err;
784}
785EXPORT_SYMBOL(file_write_and_wait_range);
786
Randy Dunlap485bb992006-06-23 02:03:49 -0700787/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700788 * replace_page_cache_page - replace a pagecache page with a new one
789 * @old: page to be replaced
790 * @new: page to replace with
791 * @gfp_mask: allocation mode
792 *
793 * This function replaces a page in the pagecache with a new one. On
794 * success it acquires the pagecache reference for the new page and
795 * drops it for the old page. Both the old and new pages must be
796 * locked. This function does not add the new page to the LRU, the
797 * caller must do that.
798 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500799 * The remove + add is atomic. This function cannot fail.
Mike Rapoporta862f682019-03-05 15:48:42 -0800800 *
801 * Return: %0
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700802 */
803int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
804{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500805 struct address_space *mapping = old->mapping;
806 void (*freepage)(struct page *) = mapping->a_ops->freepage;
807 pgoff_t offset = old->index;
808 XA_STATE(xas, &mapping->i_pages, offset);
809 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700810
Sasha Levin309381fea2014-01-23 15:52:54 -0800811 VM_BUG_ON_PAGE(!PageLocked(old), old);
812 VM_BUG_ON_PAGE(!PageLocked(new), new);
813 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700814
Matthew Wilcox74d60952017-11-17 10:01:45 -0500815 get_page(new);
816 new->mapping = mapping;
817 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700818
Matthew Wilcox74d60952017-11-17 10:01:45 -0500819 xas_lock_irqsave(&xas, flags);
820 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700821
Matthew Wilcox74d60952017-11-17 10:01:45 -0500822 old->mapping = NULL;
823 /* hugetlb pages do not participate in page cache accounting. */
824 if (!PageHuge(old))
825 __dec_node_page_state(new, NR_FILE_PAGES);
826 if (!PageHuge(new))
827 __inc_node_page_state(new, NR_FILE_PAGES);
828 if (PageSwapBacked(old))
829 __dec_node_page_state(new, NR_SHMEM);
830 if (PageSwapBacked(new))
831 __inc_node_page_state(new, NR_SHMEM);
832 xas_unlock_irqrestore(&xas, flags);
833 mem_cgroup_migrate(old, new);
834 if (freepage)
835 freepage(old);
836 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700837
Matthew Wilcox74d60952017-11-17 10:01:45 -0500838 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700839}
840EXPORT_SYMBOL_GPL(replace_page_cache_page);
841
Johannes Weinera5289102014-04-03 14:47:51 -0700842static int __add_to_page_cache_locked(struct page *page,
843 struct address_space *mapping,
844 pgoff_t offset, gfp_t gfp_mask,
845 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500847 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700848 int huge = PageHuge(page);
849 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700850 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500851 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700852
Sasha Levin309381fea2014-01-23 15:52:54 -0800853 VM_BUG_ON_PAGE(!PageLocked(page), page);
854 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500855 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700856
Johannes Weiner00501b52014-08-08 14:19:20 -0700857 if (!huge) {
858 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800859 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700860 if (error)
861 return error;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300864 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700865 page->mapping = mapping;
866 page->index = offset;
867
Matthew Wilcox74d60952017-11-17 10:01:45 -0500868 do {
869 xas_lock_irq(&xas);
870 old = xas_load(&xas);
871 if (old && !xa_is_value(old))
872 xas_set_err(&xas, -EEXIST);
873 xas_store(&xas, page);
874 if (xas_error(&xas))
875 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700876
Matthew Wilcox74d60952017-11-17 10:01:45 -0500877 if (xa_is_value(old)) {
878 mapping->nrexceptional--;
879 if (shadowp)
880 *shadowp = old;
881 }
882 mapping->nrpages++;
883
884 /* hugetlb pages do not participate in page cache accounting */
885 if (!huge)
886 __inc_node_page_state(page, NR_FILE_PAGES);
887unlock:
888 xas_unlock_irq(&xas);
889 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
890
891 if (xas_error(&xas))
892 goto error;
893
Johannes Weiner00501b52014-08-08 14:19:20 -0700894 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800895 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700896 trace_mm_filemap_add_to_page_cache(page);
897 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500898error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700899 page->mapping = NULL;
900 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700901 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800902 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300903 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500904 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Josef Bacikcfcbfb12019-05-13 17:21:04 -0700906ALLOW_ERROR_INJECTION(__add_to_page_cache_locked, ERRNO);
Johannes Weinera5289102014-04-03 14:47:51 -0700907
908/**
909 * add_to_page_cache_locked - add a locked page to the pagecache
910 * @page: page to add
911 * @mapping: the page's address_space
912 * @offset: page index
913 * @gfp_mask: page allocation mode
914 *
915 * This function is used to add a page to the pagecache. It must be locked.
916 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800917 *
918 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700919 */
920int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
921 pgoff_t offset, gfp_t gfp_mask)
922{
923 return __add_to_page_cache_locked(page, mapping, offset,
924 gfp_mask, NULL);
925}
Nick Piggine2867812008-07-25 19:45:30 -0700926EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400929 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Johannes Weinera5289102014-04-03 14:47:51 -0700931 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700932 int ret;
933
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800934 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700935 ret = __add_to_page_cache_locked(page, mapping, offset,
936 gfp_mask, &shadow);
937 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800938 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700939 else {
940 /*
941 * The page might have been evicted from cache only
942 * recently, in which case it should be activated like
943 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700944 * The exception is pages getting rewritten; evicting other
945 * data from the working set, only to cache data that will
946 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700947 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700948 WARN_ON_ONCE(PageActive(page));
949 if (!(gfp_mask & __GFP_WRITE) && shadow)
950 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700951 lru_cache_add(page);
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return ret;
954}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300955EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Paul Jackson44110fe2006-03-24 03:16:04 -0800957#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700958struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800959{
Miao Xiec0ff7452010-05-24 14:32:08 -0700960 int n;
961 struct page *page;
962
Paul Jackson44110fe2006-03-24 03:16:04 -0800963 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700964 unsigned int cpuset_mems_cookie;
965 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700966 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700967 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700968 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700969 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700970
Miao Xiec0ff7452010-05-24 14:32:08 -0700971 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800972 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700973 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800974}
Nick Piggin2ae88142006-10-28 10:38:23 -0700975EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800976#endif
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/*
979 * In order to wait for pages to become available there must be
980 * waitqueues associated with pages. By using a hash table of
981 * waitqueues where the bucket discipline is to maintain all
982 * waiters on the same queue and wake all when any of the pages
983 * become available, and for the woken contexts to check to be
984 * sure the appropriate page became available, this saves space
985 * at a cost of "thundering herd" phenomena during rare hash
986 * collisions.
987 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000988#define PAGE_WAIT_TABLE_BITS 8
989#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
990static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
991
992static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Nicholas Piggin62906022016-12-25 13:00:30 +1000994 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
Nicholas Piggin62906022016-12-25 13:00:30 +1000996
997void __init pagecache_init(void)
998{
999 int i;
1000
1001 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
1002 init_waitqueue_head(&page_wait_table[i]);
1003
1004 page_writeback_init();
1005}
1006
Linus Torvalds3510ca22017-08-27 13:55:12 -07001007/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +10001008struct wait_page_key {
1009 struct page *page;
1010 int bit_nr;
1011 int page_match;
1012};
1013
1014struct wait_page_queue {
1015 struct page *page;
1016 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001017 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +10001018};
1019
Ingo Molnarac6424b2017-06-20 12:06:13 +02001020static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +10001021{
1022 struct wait_page_key *key = arg;
1023 struct wait_page_queue *wait_page
1024 = container_of(wait, struct wait_page_queue, wait);
1025
1026 if (wait_page->page != key->page)
1027 return 0;
1028 key->page_match = 1;
1029
1030 if (wait_page->bit_nr != key->bit_nr)
1031 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001032
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001033 /*
1034 * Stop walking if it's locked.
1035 * Is this safe if put_and_wait_on_page_locked() is in use?
1036 * Yes: the waker must hold a reference to this page, and if PG_locked
1037 * has now already been set by another task, that task must also hold
1038 * a reference to the *same usage* of this page; so there is no need
1039 * to walk on to wake even the put_and_wait_on_page_locked() callers.
1040 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001041 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001042 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001043
1044 return autoremove_wake_function(wait, mode, sync, key);
1045}
1046
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001047static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001048{
1049 wait_queue_head_t *q = page_waitqueue(page);
1050 struct wait_page_key key;
1051 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001052 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001053
1054 key.page = page;
1055 key.bit_nr = bit_nr;
1056 key.page_match = 0;
1057
Tim Chen11a19c72017-08-25 09:13:55 -07001058 bookmark.flags = 0;
1059 bookmark.private = NULL;
1060 bookmark.func = NULL;
1061 INIT_LIST_HEAD(&bookmark.entry);
1062
Nicholas Piggin62906022016-12-25 13:00:30 +10001063 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001064 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1065
1066 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1067 /*
1068 * Take a breather from holding the lock,
1069 * allow pages that finish wake up asynchronously
1070 * to acquire the lock and remove themselves
1071 * from wait queue
1072 */
1073 spin_unlock_irqrestore(&q->lock, flags);
1074 cpu_relax();
1075 spin_lock_irqsave(&q->lock, flags);
1076 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1077 }
1078
Nicholas Piggin62906022016-12-25 13:00:30 +10001079 /*
1080 * It is possible for other pages to have collided on the waitqueue
1081 * hash, so in that case check for a page match. That prevents a long-
1082 * term waiter
1083 *
1084 * It is still possible to miss a case here, when we woke page waiters
1085 * and removed them from the waitqueue, but there are still other
1086 * page waiters.
1087 */
1088 if (!waitqueue_active(q) || !key.page_match) {
1089 ClearPageWaiters(page);
1090 /*
1091 * It's possible to miss clearing Waiters here, when we woke
1092 * our page waiters, but the hashed waitqueue has waiters for
1093 * other pages on it.
1094 *
1095 * That's okay, it's a rare case. The next waker will clear it.
1096 */
1097 }
1098 spin_unlock_irqrestore(&q->lock, flags);
1099}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001100
1101static void wake_up_page(struct page *page, int bit)
1102{
1103 if (!PageWaiters(page))
1104 return;
1105 wake_up_page_bit(page, bit);
1106}
Nicholas Piggin62906022016-12-25 13:00:30 +10001107
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001108/*
1109 * A choice of three behaviors for wait_on_page_bit_common():
1110 */
1111enum behavior {
1112 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
1113 * __lock_page() waiting on then setting PG_locked.
1114 */
1115 SHARED, /* Hold ref to page and check the bit when woken, like
1116 * wait_on_page_writeback() waiting on PG_writeback.
1117 */
1118 DROP, /* Drop ref to page before wait, no check when woken,
1119 * like put_and_wait_on_page_locked() on PG_locked.
1120 */
1121};
1122
Nicholas Piggin62906022016-12-25 13:00:30 +10001123static inline int wait_on_page_bit_common(wait_queue_head_t *q,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001124 struct page *page, int bit_nr, int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001125{
1126 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001127 wait_queue_entry_t *wait = &wait_page.wait;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001128 bool bit_is_set;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001129 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001130 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001131 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001132 int ret = 0;
1133
Johannes Weinereb414682018-10-26 15:06:27 -07001134 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001135 !PageUptodate(page) && PageWorkingset(page)) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001136 if (!PageSwapBacked(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001137 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001138 delayacct = true;
1139 }
Johannes Weinereb414682018-10-26 15:06:27 -07001140 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001141 thrashing = true;
1142 }
1143
Nicholas Piggin62906022016-12-25 13:00:30 +10001144 init_wait(wait);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001145 wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001146 wait->func = wake_page_function;
1147 wait_page.page = page;
1148 wait_page.bit_nr = bit_nr;
1149
1150 for (;;) {
1151 spin_lock_irq(&q->lock);
1152
Ingo Molnar2055da92017-06-20 12:06:46 +02001153 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001154 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001155 SetPageWaiters(page);
1156 }
1157
1158 set_current_state(state);
1159
1160 spin_unlock_irq(&q->lock);
1161
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001162 bit_is_set = test_bit(bit_nr, &page->flags);
1163 if (behavior == DROP)
1164 put_page(page);
Nicholas Piggin62906022016-12-25 13:00:30 +10001165
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001166 if (likely(bit_is_set))
1167 io_schedule();
1168
1169 if (behavior == EXCLUSIVE) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001170 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1171 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001172 } else if (behavior == SHARED) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001173 if (!test_bit(bit_nr, &page->flags))
1174 break;
1175 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001176
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001177 if (signal_pending_state(state, current)) {
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001178 ret = -EINTR;
1179 break;
1180 }
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001181
1182 if (behavior == DROP) {
1183 /*
1184 * We can no longer safely access page->flags:
1185 * even if CONFIG_MEMORY_HOTREMOVE is not enabled,
1186 * there is a risk of waiting forever on a page reused
1187 * for something that keeps it locked indefinitely.
1188 * But best check for -EINTR above before breaking.
1189 */
1190 break;
1191 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001192 }
1193
1194 finish_wait(q, wait);
1195
Johannes Weinereb414682018-10-26 15:06:27 -07001196 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001197 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001198 delayacct_thrashing_end();
1199 psi_memstall_leave(&pflags);
1200 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001201
Nicholas Piggin62906022016-12-25 13:00:30 +10001202 /*
1203 * A signal could leave PageWaiters set. Clearing it here if
1204 * !waitqueue_active would be possible (by open-coding finish_wait),
1205 * but still fail to catch it in the case of wait hash collision. We
1206 * already can fail to clear wait hash collision cases, so don't
1207 * bother with signals either.
1208 */
1209
1210 return ret;
1211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Harvey Harrison920c7a52008-02-04 22:29:26 -08001213void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Nicholas Piggin62906022016-12-25 13:00:30 +10001215 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001216 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218EXPORT_SYMBOL(wait_on_page_bit);
1219
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001220int wait_on_page_bit_killable(struct page *page, int bit_nr)
1221{
Nicholas Piggin62906022016-12-25 13:00:30 +10001222 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001223 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001224}
David Howells4343d002017-11-02 15:27:52 +00001225EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/**
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001228 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1229 * @page: The page to wait for.
1230 *
1231 * The caller should hold a reference on @page. They expect the page to
1232 * become unlocked relatively soon, but do not wish to hold up migration
1233 * (for example) by holding the reference while waiting for the page to
1234 * come unlocked. After this function returns, the caller should not
1235 * dereference @page.
1236 */
1237void put_and_wait_on_page_locked(struct page *page)
1238{
1239 wait_queue_head_t *q;
1240
1241 page = compound_head(page);
1242 q = page_waitqueue(page);
1243 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP);
1244}
1245
1246/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001247 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001248 * @page: Page defining the wait queue of interest
1249 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001250 *
1251 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1252 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001253void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001254{
1255 wait_queue_head_t *q = page_waitqueue(page);
1256 unsigned long flags;
1257
1258 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001259 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001260 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001261 spin_unlock_irqrestore(&q->lock, flags);
1262}
1263EXPORT_SYMBOL_GPL(add_page_wait_queue);
1264
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001265#ifndef clear_bit_unlock_is_negative_byte
1266
1267/*
1268 * PG_waiters is the high bit in the same byte as PG_lock.
1269 *
1270 * On x86 (and on many other architectures), we can clear PG_lock and
1271 * test the sign bit at the same time. But if the architecture does
1272 * not support that special operation, we just do this all by hand
1273 * instead.
1274 *
1275 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1276 * being cleared, but a memory barrier should be unneccssary since it is
1277 * in the same byte as PG_locked.
1278 */
1279static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1280{
1281 clear_bit_unlock(nr, mem);
1282 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001283 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001284}
1285
1286#endif
1287
David Howells385e1ca5f2009-04-03 16:42:39 +01001288/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001289 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 * @page: the page
1291 *
1292 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1293 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001294 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1296 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001297 * Note that this depends on PG_waiters being the sign bit in the byte
1298 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1299 * clear the PG_locked bit and test PG_waiters at the same time fairly
1300 * portably (architectures that do LL/SC can test any bit, while x86 can
1301 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001303void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001305 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001306 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001307 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001308 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1309 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311EXPORT_SYMBOL(unlock_page);
1312
Randy Dunlap485bb992006-06-23 02:03:49 -07001313/**
1314 * end_page_writeback - end writeback against a page
1315 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
1317void end_page_writeback(struct page *page)
1318{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001319 /*
1320 * TestClearPageReclaim could be used here but it is an atomic
1321 * operation and overkill in this particular case. Failing to
1322 * shuffle a page marked for immediate reclaim is too mild to
1323 * justify taking an atomic operation penalty at the end of
1324 * ever page writeback.
1325 */
1326 if (PageReclaim(page)) {
1327 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001328 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001329 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001330
1331 if (!test_clear_page_writeback(page))
1332 BUG();
1333
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001334 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 wake_up_page(page, PG_writeback);
1336}
1337EXPORT_SYMBOL(end_page_writeback);
1338
Matthew Wilcox57d99842014-06-04 16:07:45 -07001339/*
1340 * After completing I/O on a page, call this routine to update the page
1341 * flags appropriately
1342 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001343void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001344{
Jens Axboec11f0c02016-08-05 08:11:04 -06001345 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001346 if (!err) {
1347 SetPageUptodate(page);
1348 } else {
1349 ClearPageUptodate(page);
1350 SetPageError(page);
1351 }
1352 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001353 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001354 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001355 struct address_space *mapping;
1356
Matthew Wilcox57d99842014-06-04 16:07:45 -07001357 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001358 mapping = page_mapping(page);
1359 if (mapping)
1360 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001361 }
1362 end_page_writeback(page);
1363 }
1364}
1365EXPORT_SYMBOL_GPL(page_endio);
1366
Randy Dunlap485bb992006-06-23 02:03:49 -07001367/**
1368 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001369 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001371void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Nicholas Piggin62906022016-12-25 13:00:30 +10001373 struct page *page = compound_head(__page);
1374 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001375 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE,
1376 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378EXPORT_SYMBOL(__lock_page);
1379
Nicholas Piggin62906022016-12-25 13:00:30 +10001380int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001381{
Nicholas Piggin62906022016-12-25 13:00:30 +10001382 struct page *page = compound_head(__page);
1383 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001384 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE,
1385 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001386}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001387EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001388
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001389/*
1390 * Return values:
1391 * 1 - page is locked; mmap_sem is still held.
1392 * 0 - page is not locked.
1393 * mmap_sem has been released (up_read()), unless flags had both
1394 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1395 * which case mmap_sem is still held.
1396 *
1397 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1398 * with the page locked and the mmap_sem unperturbed.
1399 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001400int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1401 unsigned int flags)
1402{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001403 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1404 /*
1405 * CAUTION! In this case, mmap_sem is not released
1406 * even though return 0.
1407 */
1408 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1409 return 0;
1410
1411 up_read(&mm->mmap_sem);
1412 if (flags & FAULT_FLAG_KILLABLE)
1413 wait_on_page_locked_killable(page);
1414 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001415 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001416 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001417 } else {
1418 if (flags & FAULT_FLAG_KILLABLE) {
1419 int ret;
1420
1421 ret = __lock_page_killable(page);
1422 if (ret) {
1423 up_read(&mm->mmap_sem);
1424 return 0;
1425 }
1426 } else
1427 __lock_page(page);
1428 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001429 }
1430}
1431
Randy Dunlap485bb992006-06-23 02:03:49 -07001432/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001433 * page_cache_next_miss() - Find the next gap in the page cache.
1434 * @mapping: Mapping.
1435 * @index: Index.
1436 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001437 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001438 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1439 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001440 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001441 * This function may be called under the rcu_read_lock. However, this will
1442 * not atomically search a snapshot of the cache at a single point in time.
1443 * For example, if a gap is created at index 5, then subsequently a gap is
1444 * created at index 10, page_cache_next_miss covering both indices may
1445 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001446 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001447 * Return: The index of the gap if found, otherwise an index outside the
1448 * range specified (in which case 'return - index >= max_scan' will be true).
1449 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001450 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001451pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001452 pgoff_t index, unsigned long max_scan)
1453{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001454 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001455
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001456 while (max_scan--) {
1457 void *entry = xas_next(&xas);
1458 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001459 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001460 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001461 break;
1462 }
1463
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001464 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001465}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001466EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001467
1468/**
Laurent Dufour2346a562019-05-13 17:21:29 -07001469 * page_cache_prev_miss() - Find the previous gap in the page cache.
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001470 * @mapping: Mapping.
1471 * @index: Index.
1472 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001473 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001474 * Search the range [max(index - max_scan + 1, 0), index] for the
1475 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001476 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001477 * This function may be called under the rcu_read_lock. However, this will
1478 * not atomically search a snapshot of the cache at a single point in time.
1479 * For example, if a gap is created at index 10, then subsequently a gap is
1480 * created at index 5, page_cache_prev_miss() covering both indices may
1481 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001482 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001483 * Return: The index of the gap if found, otherwise an index outside the
1484 * range specified (in which case 'index - return >= max_scan' will be true).
1485 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001486 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001487pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001488 pgoff_t index, unsigned long max_scan)
1489{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001490 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001491
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001492 while (max_scan--) {
1493 void *entry = xas_prev(&xas);
1494 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001495 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001496 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001497 break;
1498 }
1499
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001500 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001501}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001502EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001503
1504/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001505 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001506 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001507 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001508 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001509 * Looks up the page cache slot at @mapping & @offset. If there is a
1510 * page cache page, it is returned with an increased refcount.
1511 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001512 * If the slot holds a shadow entry of a previously evicted page, or a
1513 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001514 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001515 * Return: the found page or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001517struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001519 XA_STATE(xas, &mapping->i_pages, offset);
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001520 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Nick Piggina60637c2008-07-25 19:45:31 -07001522 rcu_read_lock();
1523repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001524 xas_reset(&xas);
1525 page = xas_load(&xas);
1526 if (xas_retry(&xas, page))
1527 goto repeat;
1528 /*
1529 * A shadow entry of a recently evicted page, or a swap entry from
1530 * shmem/tmpfs. Return it without attempting to raise page count.
1531 */
1532 if (!page || xa_is_value(page))
1533 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001534
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001535 head = compound_head(page);
1536 if (!page_cache_get_speculative(head))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001537 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001538
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001539 /* The page was split under us? */
1540 if (compound_head(page) != head) {
1541 put_page(head);
1542 goto repeat;
1543 }
1544
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001545 /*
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001546 * Has the page moved?
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001547 * This is part of the lockless pagecache protocol. See
1548 * include/linux/pagemap.h for details.
1549 */
1550 if (unlikely(page != xas_reload(&xas))) {
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001551 put_page(head);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001552 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001553 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001554out:
Nick Piggina60637c2008-07-25 19:45:31 -07001555 rcu_read_unlock();
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return page;
1558}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001559EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Randy Dunlap485bb992006-06-23 02:03:49 -07001561/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001562 * find_lock_entry - locate, pin and lock a page cache entry
1563 * @mapping: the address_space to search
1564 * @offset: the page cache index
1565 *
1566 * Looks up the page cache slot at @mapping & @offset. If there is a
1567 * page cache page, it is returned locked and with an increased
1568 * refcount.
1569 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001570 * If the slot holds a shadow entry of a previously evicted page, or a
1571 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001572 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001573 * find_lock_entry() may sleep.
Mike Rapoporta862f682019-03-05 15:48:42 -08001574 *
1575 * Return: the found page or shadow entry, %NULL if nothing is found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001576 */
1577struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct page *page;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001582 page = find_get_entry(mapping, offset);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001583 if (page && !xa_is_value(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001584 lock_page(page);
1585 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001586 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001587 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001588 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001589 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001591 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return page;
1594}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001595EXPORT_SYMBOL(find_lock_entry);
1596
1597/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001598 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001599 * @mapping: the address_space to search
1600 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001601 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001602 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001603 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001604 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001605 *
Randy Dunlap75325182014-07-30 16:08:37 -07001606 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001607 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001608 * @fgp_flags can be:
1609 *
1610 * - FGP_ACCESSED: the page will be marked accessed
1611 * - FGP_LOCK: Page is return locked
1612 * - FGP_CREAT: If page is not present then a new page is allocated using
1613 * @gfp_mask and added to the page cache and the VM's LRU
1614 * list. The page is returned locked and with an increased
Mike Rapoporta862f682019-03-05 15:48:42 -08001615 * refcount.
Josef Bacika75d4c32019-03-13 11:44:14 -07001616 * - FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1617 * its own locking dance if the page is already in cache, or unlock the page
1618 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001619 *
1620 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1621 * if the GFP flags specified for FGP_CREAT are atomic.
1622 *
1623 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001624 *
1625 * Return: the found page or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001626 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001627struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001628 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Nick Piggineb2be182007-10-16 01:24:57 -07001630 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001633 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001634 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001635 page = NULL;
1636 if (!page)
1637 goto no_page;
1638
1639 if (fgp_flags & FGP_LOCK) {
1640 if (fgp_flags & FGP_NOWAIT) {
1641 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001642 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001643 return NULL;
1644 }
1645 } else {
1646 lock_page(page);
1647 }
1648
1649 /* Has the page been truncated? */
1650 if (unlikely(page->mapping != mapping)) {
1651 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001652 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001653 goto repeat;
1654 }
1655 VM_BUG_ON_PAGE(page->index != offset, page);
1656 }
1657
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001658 if (fgp_flags & FGP_ACCESSED)
Mel Gorman2457aec2014-06-04 16:10:31 -07001659 mark_page_accessed(page);
1660
1661no_page:
1662 if (!page && (fgp_flags & FGP_CREAT)) {
1663 int err;
1664 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001665 gfp_mask |= __GFP_WRITE;
1666 if (fgp_flags & FGP_NOFS)
1667 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001668
Michal Hocko45f87de2014-12-29 20:30:35 +01001669 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001670 if (!page)
1671 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001672
Josef Bacika75d4c32019-03-13 11:44:14 -07001673 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001674 fgp_flags |= FGP_LOCK;
1675
Hugh Dickinseb39d612014-08-06 16:06:43 -07001676 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001677 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001678 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001679
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001680 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001681 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001682 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001683 page = NULL;
1684 if (err == -EEXIST)
1685 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001687
1688 /*
1689 * add_to_page_cache_lru locks the page, and for mmap we expect
1690 * an unlocked page.
1691 */
1692 if (page && (fgp_flags & FGP_FOR_MMAP))
1693 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return page;
1697}
Mel Gorman2457aec2014-06-04 16:10:31 -07001698EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001701 * find_get_entries - gang pagecache lookup
1702 * @mapping: The address_space to search
1703 * @start: The starting page cache index
1704 * @nr_entries: The maximum number of entries
1705 * @entries: Where the resulting entries are placed
1706 * @indices: The cache indices corresponding to the entries in @entries
1707 *
1708 * find_get_entries() will search for and return a group of up to
1709 * @nr_entries entries in the mapping. The entries are placed at
1710 * @entries. find_get_entries() takes a reference against any actual
1711 * pages it returns.
1712 *
1713 * The search returns a group of mapping-contiguous page cache entries
1714 * with ascending indexes. There may be holes in the indices due to
1715 * not-present pages.
1716 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001717 * Any shadow entries of evicted pages, or swap entries from
1718 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001719 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001720 * Return: the number of pages and shadow entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001721 */
1722unsigned find_get_entries(struct address_space *mapping,
1723 pgoff_t start, unsigned int nr_entries,
1724 struct page **entries, pgoff_t *indices)
1725{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001726 XA_STATE(xas, &mapping->i_pages, start);
1727 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001728 unsigned int ret = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001729
1730 if (!nr_entries)
1731 return 0;
1732
1733 rcu_read_lock();
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001734 xas_for_each(&xas, page, ULONG_MAX) {
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001735 struct page *head;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001736 if (xas_retry(&xas, page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001737 continue;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001738 /*
1739 * A shadow entry of a recently evicted page, a swap
1740 * entry from shmem/tmpfs or a DAX entry. Return it
1741 * without attempting to raise page count.
1742 */
1743 if (xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001744 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001745
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001746 head = compound_head(page);
1747 if (!page_cache_get_speculative(head))
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001748 goto retry;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001749
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001750 /* The page was split under us? */
1751 if (compound_head(page) != head)
1752 goto put_page;
1753
1754 /* Has the page moved? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001755 if (unlikely(page != xas_reload(&xas)))
1756 goto put_page;
1757
Johannes Weiner0cd61442014-04-03 14:47:46 -07001758export:
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001759 indices[ret] = xas.xa_index;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001760 entries[ret] = page;
1761 if (++ret == nr_entries)
1762 break;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001763 continue;
1764put_page:
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001765 put_page(head);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001766retry:
1767 xas_reset(&xas);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001768 }
1769 rcu_read_unlock();
1770 return ret;
1771}
1772
1773/**
Jan Karab947cee2017-09-06 16:21:21 -07001774 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 * @mapping: The address_space to search
1776 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001777 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * @nr_pages: The maximum number of pages
1779 * @pages: Where the resulting pages are placed
1780 *
Jan Karab947cee2017-09-06 16:21:21 -07001781 * find_get_pages_range() will search for and return a group of up to @nr_pages
1782 * pages in the mapping starting at index @start and up to index @end
1783 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1784 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 *
1786 * The search returns a group of mapping-contiguous pages with ascending
1787 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001788 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001790 * Return: the number of pages which were found. If this number is
1791 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07001792 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 */
Jan Karab947cee2017-09-06 16:21:21 -07001794unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1795 pgoff_t end, unsigned int nr_pages,
1796 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001798 XA_STATE(xas, &mapping->i_pages, *start);
1799 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001800 unsigned ret = 0;
1801
1802 if (unlikely(!nr_pages))
1803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Nick Piggina60637c2008-07-25 19:45:31 -07001805 rcu_read_lock();
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001806 xas_for_each(&xas, page, end) {
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001807 struct page *head;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001808 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001809 continue;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001810 /* Skip over shadow, swap and DAX entries */
1811 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001812 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001813
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001814 head = compound_head(page);
1815 if (!page_cache_get_speculative(head))
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001816 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001817
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001818 /* The page was split under us? */
1819 if (compound_head(page) != head)
1820 goto put_page;
1821
1822 /* Has the page moved? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001823 if (unlikely(page != xas_reload(&xas)))
1824 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001825
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001826 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001827 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001828 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07001829 goto out;
1830 }
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001831 continue;
1832put_page:
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001833 put_page(head);
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001834retry:
1835 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001836 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001837
Jan Karab947cee2017-09-06 16:21:21 -07001838 /*
1839 * We come here when there is no page beyond @end. We take care to not
1840 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001841 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07001842 * already broken anyway.
1843 */
1844 if (end == (pgoff_t)-1)
1845 *start = (pgoff_t)-1;
1846 else
1847 *start = end + 1;
1848out:
Nick Piggina60637c2008-07-25 19:45:31 -07001849 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return ret;
1852}
1853
Jens Axboeebf43502006-04-27 08:46:01 +02001854/**
1855 * find_get_pages_contig - gang contiguous pagecache lookup
1856 * @mapping: The address_space to search
1857 * @index: The starting page index
1858 * @nr_pages: The maximum number of pages
1859 * @pages: Where the resulting pages are placed
1860 *
1861 * find_get_pages_contig() works exactly like find_get_pages(), except
1862 * that the returned number of pages are guaranteed to be contiguous.
1863 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001864 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02001865 */
1866unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1867 unsigned int nr_pages, struct page **pages)
1868{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001869 XA_STATE(xas, &mapping->i_pages, index);
1870 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001871 unsigned int ret = 0;
1872
1873 if (unlikely(!nr_pages))
1874 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001875
Nick Piggina60637c2008-07-25 19:45:31 -07001876 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001877 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001878 struct page *head;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001879 if (xas_retry(&xas, page))
1880 continue;
1881 /*
1882 * If the entry has been swapped out, we can stop looking.
1883 * No current caller is looking for DAX entries.
1884 */
1885 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001886 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001887
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001888 head = compound_head(page);
1889 if (!page_cache_get_speculative(head))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001890 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001891
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001892 /* The page was split under us? */
1893 if (compound_head(page) != head)
1894 goto put_page;
1895
1896 /* Has the page moved? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001897 if (unlikely(page != xas_reload(&xas)))
1898 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001899
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001900 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001901 if (++ret == nr_pages)
1902 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001903 continue;
1904put_page:
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001905 put_page(head);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001906retry:
1907 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02001908 }
Nick Piggina60637c2008-07-25 19:45:31 -07001909 rcu_read_unlock();
1910 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001911}
David Howellsef71c152007-05-09 02:33:44 -07001912EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001913
Randy Dunlap485bb992006-06-23 02:03:49 -07001914/**
Jan Kara72b045a2017-11-15 17:34:33 -08001915 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001916 * @mapping: the address_space to search
1917 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001918 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001919 * @tag: the tag index
1920 * @nr_pages: the maximum number of pages
1921 * @pages: where the resulting pages are placed
1922 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001924 * @tag. We update @index to index the next page for the traversal.
Mike Rapoporta862f682019-03-05 15:48:42 -08001925 *
1926 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
Jan Kara72b045a2017-11-15 17:34:33 -08001928unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001929 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08001930 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001932 XA_STATE(xas, &mapping->i_pages, *index);
1933 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001934 unsigned ret = 0;
1935
1936 if (unlikely(!nr_pages))
1937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Nick Piggina60637c2008-07-25 19:45:31 -07001939 rcu_read_lock();
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001940 xas_for_each_marked(&xas, page, end, tag) {
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001941 struct page *head;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001942 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001943 continue;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001944 /*
1945 * Shadow entries should never be tagged, but this iteration
1946 * is lockless so there is a window for page reclaim to evict
1947 * a page we saw tagged. Skip over it.
1948 */
1949 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07001950 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001951
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001952 head = compound_head(page);
1953 if (!page_cache_get_speculative(head))
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001954 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001955
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001956 /* The page was split under us? */
1957 if (compound_head(page) != head)
1958 goto put_page;
1959
1960 /* Has the page moved? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001961 if (unlikely(page != xas_reload(&xas)))
1962 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001963
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001964 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001965 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001966 *index = xas.xa_index + 1;
Jan Kara72b045a2017-11-15 17:34:33 -08001967 goto out;
1968 }
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001969 continue;
1970put_page:
Linus Torvalds69bf4b62019-07-05 19:55:18 -07001971 put_page(head);
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001972retry:
1973 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001974 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001975
Jan Kara72b045a2017-11-15 17:34:33 -08001976 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001977 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08001978 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001979 * iteration when there is a page at index -1 but that is already
1980 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08001981 */
1982 if (end == (pgoff_t)-1)
1983 *index = (pgoff_t)-1;
1984 else
1985 *index = end + 1;
1986out:
Nick Piggina60637c2008-07-25 19:45:31 -07001987 rcu_read_unlock();
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return ret;
1990}
Jan Kara72b045a2017-11-15 17:34:33 -08001991EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001993/*
1994 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1995 * a _large_ part of the i/o request. Imagine the worst scenario:
1996 *
1997 * ---R__________________________________________B__________
1998 * ^ reading here ^ bad block(assume 4k)
1999 *
2000 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2001 * => failing the whole request => read(R) => read(R+1) =>
2002 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2003 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2004 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2005 *
2006 * It is going insane. Fix it by quickly scaling down the readahead size.
2007 */
2008static void shrink_readahead_size_eio(struct file *filp,
2009 struct file_ra_state *ra)
2010{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002011 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002012}
2013
Randy Dunlap485bb992006-06-23 02:03:49 -07002014/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002015 * generic_file_buffered_read - generic file read routine
2016 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002017 * @iter: data destination
2018 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002021 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 *
2023 * This is really ugly. But the goto's actually try to clarify some
2024 * of the logic when it comes to error handling etc.
Mike Rapoporta862f682019-03-05 15:48:42 -08002025 *
2026 * Return:
2027 * * total number of bytes copied, including those the were already @written
2028 * * negative error code if nothing was copied
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002030static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002031 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002033 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002034 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002036 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002037 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002038 pgoff_t index;
2039 pgoff_t last_index;
2040 pgoff_t prev_index;
2041 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002042 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002043 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Wei Fangc2a97372016-10-07 17:01:52 -07002045 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002046 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002047 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2048
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002049 index = *ppos >> PAGE_SHIFT;
2050 prev_index = ra->prev_pos >> PAGE_SHIFT;
2051 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2052 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2053 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 for (;;) {
2056 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002057 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002058 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 unsigned long nr, ret;
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002063 if (fatal_signal_pending(current)) {
2064 error = -EINTR;
2065 goto out;
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002069 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002070 if (iocb->ki_flags & IOCB_NOWAIT)
2071 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002072 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002073 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002074 index, last_index - index);
2075 page = find_get_page(mapping, index);
2076 if (unlikely(page == NULL))
2077 goto no_cached_page;
2078 }
2079 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002080 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002081 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002082 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002084 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002085 if (iocb->ki_flags & IOCB_NOWAIT) {
2086 put_page(page);
2087 goto would_block;
2088 }
2089
Mel Gormanebded022016-03-15 14:55:39 -07002090 /*
2091 * See comment in do_read_cache_page on why
2092 * wait_on_page_locked is used to avoid unnecessarily
2093 * serialisations and why it's safe.
2094 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002095 error = wait_on_page_locked_killable(page);
2096 if (unlikely(error))
2097 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002098 if (PageUptodate(page))
2099 goto page_ok;
2100
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002101 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002102 !mapping->a_ops->is_partially_uptodate)
2103 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002104 /* pipes can't handle partially uptodate pages */
David Howells00e23702018-10-22 13:07:28 +01002105 if (unlikely(iov_iter_is_pipe(iter)))
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002106 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002107 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002108 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002109 /* Did it get truncated before we got the lock? */
2110 if (!page->mapping)
2111 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002112 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002113 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002114 goto page_not_up_to_date_locked;
2115 unlock_page(page);
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002118 /*
2119 * i_size must be checked after we know the page is Uptodate.
2120 *
2121 * Checking i_size after the check allows us to calculate
2122 * the correct value for "nr", which means the zero-filled
2123 * part of the page is not copied back to userspace (unless
2124 * another truncate extends the file - this is desired though).
2125 */
2126
2127 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002128 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002129 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002130 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002131 goto out;
2132 }
2133
2134 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002135 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002136 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002137 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002138 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002139 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002140 goto out;
2141 }
2142 }
2143 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* If users can be writing to this page using arbitrary
2146 * virtual addresses, take care about potential aliasing
2147 * before reading the page on the kernel side.
2148 */
2149 if (mapping_writably_mapped(mapping))
2150 flush_dcache_page(page);
2151
2152 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002153 * When a sequential read accesses a page several times,
2154 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
Jan Karaec0f1632007-05-06 14:49:25 -07002156 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 mark_page_accessed(page);
2158 prev_index = index;
2159
2160 /*
2161 * Ok, we have the page, and it's up-to-date, so
2162 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 */
Al Viro6e58e792014-02-03 17:07:03 -05002164
2165 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002167 index += offset >> PAGE_SHIFT;
2168 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002169 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002171 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002172 written += ret;
2173 if (!iov_iter_count(iter))
2174 goto out;
2175 if (ret < nr) {
2176 error = -EFAULT;
2177 goto out;
2178 }
2179 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181page_not_up_to_date:
2182 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002183 error = lock_page_killable(page);
2184 if (unlikely(error))
2185 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002187page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002188 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!page->mapping) {
2190 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002191 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 continue;
2193 }
2194
2195 /* Did somebody else fill it already? */
2196 if (PageUptodate(page)) {
2197 unlock_page(page);
2198 goto page_ok;
2199 }
2200
2201readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002202 /*
2203 * A previous I/O error may have been due to temporary
2204 * failures, eg. multipath errors.
2205 * PG_error will be set again if readpage fails.
2206 */
2207 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /* Start the actual read. The read will unlock the page. */
2209 error = mapping->a_ops->readpage(filp, page);
2210
Zach Brown994fc28c2005-12-15 14:28:17 -08002211 if (unlikely(error)) {
2212 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002213 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002214 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002215 goto find_page;
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002221 error = lock_page_killable(page);
2222 if (unlikely(error))
2223 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (!PageUptodate(page)) {
2225 if (page->mapping == NULL) {
2226 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002227 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
2229 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002230 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto find_page;
2232 }
2233 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002234 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002235 error = -EIO;
2236 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238 unlock_page(page);
2239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto page_ok;
2242
2243readpage_error:
2244 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002245 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 goto out;
2247
2248no_cached_page:
2249 /*
2250 * Ok, it wasn't cached, so we need to create a new
2251 * page..
2252 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002253 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002254 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002255 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002258 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002259 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002261 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002262 if (error == -EEXIST) {
2263 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto out;
2267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 goto readpage;
2269 }
2270
Milosz Tanski3239d832017-08-29 16:13:19 +02002271would_block:
2272 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002274 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002275 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002276 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002278 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002279 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002280 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Randy Dunlap485bb992006-06-23 02:03:49 -07002283/**
Al Viro6abd2322014-04-04 14:20:57 -04002284 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002285 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002286 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002287 *
Al Viro6abd2322014-04-04 14:20:57 -04002288 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 * that can use the page cache directly.
Mike Rapoporta862f682019-03-05 15:48:42 -08002290 * Return:
2291 * * number of bytes copied, even for partial reads
2292 * * negative error code if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
2294ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002295generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002297 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002298 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002299
2300 if (!count)
2301 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Al Viro2ba48ce2015-04-09 13:52:01 -04002303 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002304 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002305 struct address_space *mapping = file->f_mapping;
2306 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002307 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002310 if (iocb->ki_flags & IOCB_NOWAIT) {
2311 if (filemap_range_has_page(mapping, iocb->ki_pos,
2312 iocb->ki_pos + count - 1))
2313 return -EAGAIN;
2314 } else {
2315 retval = filemap_write_and_wait_range(mapping,
2316 iocb->ki_pos,
2317 iocb->ki_pos + count - 1);
2318 if (retval < 0)
2319 goto out;
2320 }
Al Viroed978a82014-03-05 22:53:04 -05002321
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002322 file_accessed(file);
2323
Al Viro5ecda132017-04-13 14:13:36 -04002324 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002325 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002326 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002327 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002328 }
Al Viro5b47d592017-05-08 13:54:47 -04002329 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002330
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002331 /*
2332 * Btrfs can have a short DIO read if we encounter
2333 * compressed extents, so if there was an error, or if
2334 * we've already read everything we wanted to, or if
2335 * there was a short read because we hit EOF, go ahead
2336 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002337 * the rest of the read. Buffered reads will not work for
2338 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002339 */
Al Viro5ecda132017-04-13 14:13:36 -04002340 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002341 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002342 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002345 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346out:
2347 return retval;
2348}
Al Viroed978a82014-03-05 22:53:04 -05002349EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352#define MMAP_LOTSAMISS (100)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002353static struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
2354 struct file *fpin)
2355{
2356 int flags = vmf->flags;
2357
2358 if (fpin)
2359 return fpin;
2360
2361 /*
2362 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2363 * anything, so we only pin the file and drop the mmap_sem if only
2364 * FAULT_FLAG_ALLOW_RETRY is set.
2365 */
2366 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2367 FAULT_FLAG_ALLOW_RETRY) {
2368 fpin = get_file(vmf->vma->vm_file);
2369 up_read(&vmf->vma->vm_mm->mmap_sem);
2370 }
2371 return fpin;
2372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Linus Torvaldsef00e082009-06-16 15:31:25 -07002374/*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002375 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2376 * @vmf - the vm_fault for this fault.
2377 * @page - the page to lock.
2378 * @fpin - the pointer to the file we may pin (or is already pinned).
2379 *
2380 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2381 * It differs in that it actually returns the page locked if it returns 1 and 0
2382 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2383 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002384 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002385static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2386 struct file **fpin)
2387{
2388 if (trylock_page(page))
2389 return 1;
2390
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002391 /*
2392 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2393 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2394 * is supposed to work. We have way too many special cases..
2395 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002396 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2397 return 0;
2398
2399 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2400 if (vmf->flags & FAULT_FLAG_KILLABLE) {
2401 if (__lock_page_killable(page)) {
2402 /*
2403 * We didn't have the right flags to drop the mmap_sem,
2404 * but all fault_handlers only check for fatal signals
2405 * if we return VM_FAULT_RETRY, so we need to drop the
2406 * mmap_sem here and return 0 if we don't have a fpin.
2407 */
2408 if (*fpin == NULL)
2409 up_read(&vmf->vma->vm_mm->mmap_sem);
2410 return 0;
2411 }
2412 } else
2413 __lock_page(page);
2414 return 1;
2415}
2416
2417
2418/*
2419 * Synchronous readahead happens when we don't even find a page in the page
2420 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2421 * to drop the mmap sem we return the file that was pinned in order for us to do
2422 * that. If we didn't pin a file then we return NULL. The file that is
2423 * returned needs to be fput()'ed when we're done with it.
2424 */
2425static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
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;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002436 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002437 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002438
Josef Bacik2a1180f2019-03-13 11:44:18 -07002439 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002440 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002441 page_cache_sync_readahead(mapping, ra, file, offset,
2442 ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002443 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002444 }
2445
Andi Kleen207d04b2011-05-24 17:12:29 -07002446 /* Avoid banging the cache line if not needed */
2447 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002448 ra->mmap_miss++;
2449
2450 /*
2451 * Do we miss much more than hit in this file? If so,
2452 * stop bothering with read-ahead. It will only hurt.
2453 */
2454 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002455 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002456
Wu Fengguangd30a1102009-06-16 15:31:30 -07002457 /*
2458 * mmap read-around
2459 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002460 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002461 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2462 ra->size = ra->ra_pages;
2463 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002464 ra_submit(ra, mapping, file);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002465 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002466}
2467
2468/*
2469 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002470 * so we want to possibly extend the readahead further. We return the file that
2471 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002472 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002473static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
2474 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002475{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002476 struct file *file = vmf->vma->vm_file;
2477 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002478 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002479 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002480 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002481
2482 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002483 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002484 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002485 if (ra->mmap_miss > 0)
2486 ra->mmap_miss--;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002487 if (PageReadahead(page)) {
2488 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002489 page_cache_async_readahead(mapping, ra, file,
2490 page, offset, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002491 }
2492 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002493}
2494
Randy Dunlap485bb992006-06-23 02:03:49 -07002495/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002496 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002497 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002498 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002499 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 * mapped memory region to read in file data during a page fault.
2501 *
2502 * The goto's are kind of ugly, but this streamlines the normal case of having
2503 * it in the page cache, and handles the special cases reasonably without
2504 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002505 *
2506 * vma->vm_mm->mmap_sem must be held on entry.
2507 *
Yang Shia4985832019-07-11 20:55:29 -07002508 * If our return value has VM_FAULT_RETRY set, it's because the mmap_sem
2509 * may be dropped before doing I/O or by lock_page_maybe_drop_mmap().
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002510 *
2511 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2512 * has not been released.
2513 *
2514 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08002515 *
2516 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002518vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002521 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002522 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct address_space *mapping = file->f_mapping;
2524 struct file_ra_state *ra = &file->f_ra;
2525 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002526 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002527 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002529 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002531 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2532 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002533 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002536 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002538 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002539 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002541 * We found the page, so try async readahead before
2542 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002544 fpin = do_async_mmap_readahead(vmf, page);
Shaohua Li45cac652012-10-08 16:32:19 -07002545 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002546 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002547 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002548 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002549 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002550 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002551retry_find:
Josef Bacika75d4c32019-03-13 11:44:14 -07002552 page = pagecache_get_page(mapping, offset,
2553 FGP_CREAT|FGP_FOR_MMAP,
2554 vmf->gfp_mask);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002555 if (!page) {
2556 if (fpin)
2557 goto out_retry;
Josef Bacika75d4c32019-03-13 11:44:14 -07002558 return vmf_error(-ENOMEM);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002562 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
2563 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002564
2565 /* Did it get truncated? */
2566 if (unlikely(page->mapping != mapping)) {
2567 unlock_page(page);
2568 put_page(page);
2569 goto retry_find;
2570 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002571 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002574 * We have a locked page in the page cache, now we need to check
2575 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 */
Nick Piggind00806b2007-07-19 01:46:57 -07002577 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto page_not_uptodate;
2579
Linus Torvaldsef00e082009-06-16 15:31:25 -07002580 /*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002581 * We've made it this far and we had to drop our mmap_sem, now is the
2582 * time to return to the upper layer and have it re-find the vma and
2583 * redo the fault.
2584 */
2585 if (fpin) {
2586 unlock_page(page);
2587 goto out_retry;
2588 }
2589
2590 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002591 * Found the page and have a reference on it.
2592 * We must recheck i_size under page lock.
2593 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002594 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2595 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002596 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002597 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002598 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002599 }
2600
Nick Piggind0217ac2007-07-19 01:47:03 -07002601 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002602 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
2606 * Umm, take care of errors if the page isn't up-to-date.
2607 * Try to re-read it _once_. We do this synchronously,
2608 * because there really aren't any performance issues here
2609 * and we need to check for errors.
2610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 ClearPageError(page);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002612 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002613 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002614 if (!error) {
2615 wait_on_page_locked(page);
2616 if (!PageUptodate(page))
2617 error = -EIO;
2618 }
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002619 if (fpin)
2620 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002621 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002622
2623 if (!error || error == AOP_TRUNCATED_PAGE)
2624 goto retry_find;
2625
2626 /* Things didn't work out. Return zero to tell the mm layer so. */
2627 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002628 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002629
2630out_retry:
2631 /*
2632 * We dropped the mmap_sem, we need to return to the fault handler to
2633 * re-find the vma and come back and find our hopefully still populated
2634 * page.
2635 */
2636 if (page)
2637 put_page(page);
2638 if (fpin)
2639 fput(fpin);
2640 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002641}
2642EXPORT_SYMBOL(filemap_fault);
2643
Jan Kara82b0f8c2016-12-14 15:06:58 -08002644void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002645 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002646{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002647 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002648 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002649 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002650 unsigned long max_idx;
Matthew Wilcox070e8072018-05-17 00:08:30 -04002651 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Linus Torvalds69bf4b62019-07-05 19:55:18 -07002652 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002653
2654 rcu_read_lock();
Matthew Wilcox070e8072018-05-17 00:08:30 -04002655 xas_for_each(&xas, page, end_pgoff) {
2656 if (xas_retry(&xas, page))
2657 continue;
2658 if (xa_is_value(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002659 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002660
Linus Torvalds69bf4b62019-07-05 19:55:18 -07002661 head = compound_head(page);
2662
Michal Hockoe0975b22018-12-28 00:38:36 -08002663 /*
2664 * Check for a locked page first, as a speculative
2665 * reference may adversely influence page migration.
2666 */
Linus Torvalds69bf4b62019-07-05 19:55:18 -07002667 if (PageLocked(head))
Michal Hockoe0975b22018-12-28 00:38:36 -08002668 goto next;
Linus Torvalds69bf4b62019-07-05 19:55:18 -07002669 if (!page_cache_get_speculative(head))
Matthew Wilcox070e8072018-05-17 00:08:30 -04002670 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002671
Linus Torvalds69bf4b62019-07-05 19:55:18 -07002672 /* The page was split under us? */
2673 if (compound_head(page) != head)
2674 goto skip;
2675
2676 /* Has the page moved? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002677 if (unlikely(page != xas_reload(&xas)))
2678 goto skip;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002679
2680 if (!PageUptodate(page) ||
2681 PageReadahead(page) ||
2682 PageHWPoison(page))
2683 goto skip;
2684 if (!trylock_page(page))
2685 goto skip;
2686
2687 if (page->mapping != mapping || !PageUptodate(page))
2688 goto unlock;
2689
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002690 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2691 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002692 goto unlock;
2693
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002694 if (file->f_ra.mmap_miss > 0)
2695 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002696
Matthew Wilcox070e8072018-05-17 00:08:30 -04002697 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002698 if (vmf->pte)
Matthew Wilcox070e8072018-05-17 00:08:30 -04002699 vmf->pte += xas.xa_index - last_pgoff;
2700 last_pgoff = xas.xa_index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002701 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002702 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002703 unlock_page(page);
2704 goto next;
2705unlock:
2706 unlock_page(page);
2707skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002708 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002709next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002710 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002711 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002712 break;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002713 }
2714 rcu_read_unlock();
2715}
2716EXPORT_SYMBOL(filemap_map_pages);
2717
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002718vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002719{
2720 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002721 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002722 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002723
Jan Kara14da9202012-06-12 16:20:37 +02002724 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002725 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002726 lock_page(page);
2727 if (page->mapping != inode->i_mapping) {
2728 unlock_page(page);
2729 ret = VM_FAULT_NOPAGE;
2730 goto out;
2731 }
Jan Kara14da9202012-06-12 16:20:37 +02002732 /*
2733 * We mark the page dirty already here so that when freeze is in
2734 * progress, we are guaranteed that writeback during freezing will
2735 * see the dirty page and writeprotect it again.
2736 */
2737 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002738 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002739out:
Jan Kara14da9202012-06-12 16:20:37 +02002740 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002741 return ret;
2742}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002743
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002744const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002745 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002746 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002747 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748};
2749
2750/* This is used for a general mmap of a disk file */
2751
2752int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2753{
2754 struct address_space *mapping = file->f_mapping;
2755
2756 if (!mapping->a_ops->readpage)
2757 return -ENOEXEC;
2758 file_accessed(file);
2759 vma->vm_ops = &generic_file_vm_ops;
2760 return 0;
2761}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763/*
2764 * This is for filesystems which do not implement ->writepage.
2765 */
2766int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2767{
2768 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2769 return -EINVAL;
2770 return generic_file_mmap(file, vma);
2771}
2772#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002773vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann45397222018-04-13 15:35:27 -07002774{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002775 return VM_FAULT_SIGBUS;
Arnd Bergmann45397222018-04-13 15:35:27 -07002776}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2778{
2779 return -ENOSYS;
2780}
2781int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2782{
2783 return -ENOSYS;
2784}
2785#endif /* CONFIG_MMU */
2786
Arnd Bergmann45397222018-04-13 15:35:27 -07002787EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788EXPORT_SYMBOL(generic_file_mmap);
2789EXPORT_SYMBOL(generic_file_readonly_mmap);
2790
Sasha Levin67f9fd92014-04-03 14:48:18 -07002791static struct page *wait_on_page_read(struct page *page)
2792{
2793 if (!IS_ERR(page)) {
2794 wait_on_page_locked(page);
2795 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002796 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002797 page = ERR_PTR(-EIO);
2798 }
2799 }
2800 return page;
2801}
2802
Mel Gorman32b63522016-03-15 14:55:36 -07002803static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002804 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002805 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002806 void *data,
2807 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Nick Piggineb2be182007-10-16 01:24:57 -07002809 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 int err;
2811repeat:
2812 page = find_get_page(mapping, index);
2813 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002814 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002815 if (!page)
2816 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002817 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002818 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002819 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002820 if (err == -EEXIST)
2821 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05002822 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return ERR_PTR(err);
2824 }
Mel Gorman32b63522016-03-15 14:55:36 -07002825
2826filler:
Christoph Hellwig6c45b452019-07-11 20:55:20 -07002827 if (filler)
2828 err = filler(data, page);
2829 else
2830 err = mapping->a_ops->readpage(data, page);
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002833 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002834 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Mel Gorman32b63522016-03-15 14:55:36 -07002836
2837 page = wait_on_page_read(page);
2838 if (IS_ERR(page))
2839 return page;
2840 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (PageUptodate(page))
2843 goto out;
2844
Mel Gormanebded022016-03-15 14:55:39 -07002845 /*
2846 * Page is not up to date and may be locked due one of the following
2847 * case a: Page is being filled and the page lock is held
2848 * case b: Read/write error clearing the page uptodate status
2849 * case c: Truncation in progress (page locked)
2850 * case d: Reclaim in progress
2851 *
2852 * Case a, the page will be up to date when the page is unlocked.
2853 * There is no need to serialise on the page lock here as the page
2854 * is pinned so the lock gives no additional protection. Even if the
2855 * the page is truncated, the data is still valid if PageUptodate as
2856 * it's a race vs truncate race.
2857 * Case b, the page will not be up to date
2858 * Case c, the page may be truncated but in itself, the data may still
2859 * be valid after IO completes as it's a read vs truncate race. The
2860 * operation must restart if the page is not uptodate on unlock but
2861 * otherwise serialising on page lock to stabilise the mapping gives
2862 * no additional guarantees to the caller as the page lock is
2863 * released before return.
2864 * Case d, similar to truncation. If reclaim holds the page lock, it
2865 * will be a race with remove_mapping that determines if the mapping
2866 * is valid on unlock but otherwise the data is valid and there is
2867 * no need to serialise with page lock.
2868 *
2869 * As the page lock gives no additional guarantee, we optimistically
2870 * wait on the page to be unlocked and check if it's up to date and
2871 * use the page if it is. Otherwise, the page lock is required to
2872 * distinguish between the different cases. The motivation is that we
2873 * avoid spurious serialisations and wakeups when multiple processes
2874 * wait on the same page for IO to complete.
2875 */
2876 wait_on_page_locked(page);
2877 if (PageUptodate(page))
2878 goto out;
2879
2880 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002882
2883 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (!page->mapping) {
2885 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002886 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002887 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Mel Gormanebded022016-03-15 14:55:39 -07002889
2890 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (PageUptodate(page)) {
2892 unlock_page(page);
2893 goto out;
2894 }
Mel Gorman32b63522016-03-15 14:55:36 -07002895 goto filler;
2896
David Howellsc855ff32007-05-09 13:42:20 +01002897out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002898 mark_page_accessed(page);
2899 return page;
2900}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002901
2902/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002903 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002904 * @mapping: the page's address_space
2905 * @index: the page index
2906 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002907 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002908 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002909 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002910 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002911 *
2912 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002913 *
2914 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002915 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002916struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002917 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002918 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002919 void *data)
2920{
Christoph Hellwigd322a8e2019-07-11 20:55:17 -07002921 return do_read_cache_page(mapping, index, filler, data,
2922 mapping_gfp_mask(mapping));
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002923}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002924EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002925
2926/**
2927 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2928 * @mapping: the page's address_space
2929 * @index: the page index
2930 * @gfp: the page allocator flags to use if allocating
2931 *
2932 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002933 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002934 *
2935 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002936 *
2937 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002938 */
2939struct page *read_cache_page_gfp(struct address_space *mapping,
2940 pgoff_t index,
2941 gfp_t gfp)
2942{
Christoph Hellwig6c45b452019-07-11 20:55:20 -07002943 return do_read_cache_page(mapping, index, NULL, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002944}
2945EXPORT_SYMBOL(read_cache_page_gfp);
2946
Nick Piggin2f718ff2007-10-16 01:24:59 -07002947/*
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002948 * Don't operate on ranges the page cache doesn't support, and don't exceed the
2949 * LFS limits. If pos is under the limit it becomes a short access. If it
2950 * exceeds the limit we return -EFBIG.
2951 */
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002952static int generic_write_check_limits(struct file *file, loff_t pos,
2953 loff_t *count)
2954{
Amir Goldstein646955c2019-06-05 08:04:48 -07002955 struct inode *inode = file->f_mapping->host;
2956 loff_t max_size = inode->i_sb->s_maxbytes;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002957 loff_t limit = rlimit(RLIMIT_FSIZE);
2958
2959 if (limit != RLIM_INFINITY) {
2960 if (pos >= limit) {
2961 send_sig(SIGXFSZ, current, 0);
2962 return -EFBIG;
2963 }
2964 *count = min(*count, limit - pos);
2965 }
2966
Amir Goldstein646955c2019-06-05 08:04:48 -07002967 if (!(file->f_flags & O_LARGEFILE))
2968 max_size = MAX_NON_LFS;
2969
2970 if (unlikely(pos >= max_size))
2971 return -EFBIG;
2972
2973 *count = min(*count, max_size - pos);
2974
2975 return 0;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002976}
2977
2978/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * Performs necessary checks before doing a write
2980 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002981 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * Returns appropriate error code that caller should return or
2983 * zero in case that write should be allowed.
2984 */
Al Viro3309dd02015-04-09 12:55:47 -04002985inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Al Viro3309dd02015-04-09 12:55:47 -04002987 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 struct inode *inode = file->f_mapping->host;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002989 loff_t count;
2990 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Darrick J. Wongdc617f22019-08-20 07:55:16 -07002992 if (IS_SWAPFILE(inode))
2993 return -ETXTBSY;
2994
Al Viro3309dd02015-04-09 12:55:47 -04002995 if (!iov_iter_count(from))
2996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Al Viro0fa6b002015-04-04 04:05:48 -04002998 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002999 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04003000 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003002 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
3003 return -EINVAL;
3004
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003005 count = iov_iter_count(from);
3006 ret = generic_write_check_limits(file, iocb->ki_pos, &count);
3007 if (ret)
3008 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003010 iov_iter_truncate(from, count);
Al Viro3309dd02015-04-09 12:55:47 -04003011 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013EXPORT_SYMBOL(generic_write_checks);
3014
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003015/*
3016 * Performs necessary checks before doing a clone.
3017 *
Amir Goldstein646955c2019-06-05 08:04:48 -07003018 * Can adjust amount of bytes to clone via @req_count argument.
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003019 * Returns appropriate error code that caller should return or
3020 * zero in case the clone should be allowed.
3021 */
3022int generic_remap_checks(struct file *file_in, loff_t pos_in,
3023 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003024 loff_t *req_count, unsigned int remap_flags)
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003025{
3026 struct inode *inode_in = file_in->f_mapping->host;
3027 struct inode *inode_out = file_out->f_mapping->host;
3028 uint64_t count = *req_count;
3029 uint64_t bcount;
3030 loff_t size_in, size_out;
3031 loff_t bs = inode_out->i_sb->s_blocksize;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003032 int ret;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003033
3034 /* The start of both ranges must be aligned to an fs block. */
3035 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
3036 return -EINVAL;
3037
3038 /* Ensure offsets don't wrap. */
3039 if (pos_in + count < pos_in || pos_out + count < pos_out)
3040 return -EINVAL;
3041
3042 size_in = i_size_read(inode_in);
3043 size_out = i_size_read(inode_out);
3044
3045 /* Dedupe requires both ranges to be within EOF. */
Darrick J. Wong3d281932018-10-30 10:41:34 +11003046 if ((remap_flags & REMAP_FILE_DEDUP) &&
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003047 (pos_in >= size_in || pos_in + count > size_in ||
3048 pos_out >= size_out || pos_out + count > size_out))
3049 return -EINVAL;
3050
3051 /* Ensure the infile range is within the infile. */
3052 if (pos_in >= size_in)
3053 return -EINVAL;
3054 count = min(count, size_in - (uint64_t)pos_in);
3055
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003056 ret = generic_write_check_limits(file_out, pos_out, &count);
3057 if (ret)
3058 return ret;
3059
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003060 /*
3061 * If the user wanted us to link to the infile's EOF, round up to the
3062 * next block boundary for this check.
3063 *
3064 * Otherwise, make sure the count is also block-aligned, having
3065 * already confirmed the starting offsets' block alignment.
3066 */
3067 if (pos_in + count == size_in) {
3068 bcount = ALIGN(size_in, bs) - pos_in;
3069 } else {
3070 if (!IS_ALIGNED(count, bs))
Darrick J. Wongeca36542018-10-30 10:42:10 +11003071 count = ALIGN_DOWN(count, bs);
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003072 bcount = count;
3073 }
3074
3075 /* Don't allow overlapped cloning within the same file. */
3076 if (inode_in == inode_out &&
3077 pos_out + bcount > pos_in &&
3078 pos_out < pos_in + bcount)
3079 return -EINVAL;
3080
Darrick J. Wongeca36542018-10-30 10:42:10 +11003081 /*
3082 * We shortened the request but the caller can't deal with that, so
3083 * bounce the request back to userspace.
3084 */
3085 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003086 return -EINVAL;
3087
Darrick J. Wongeca36542018-10-30 10:42:10 +11003088 *req_count = count;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003089 return 0;
3090}
3091
Amir Goldsteina3171352019-06-05 08:04:48 -07003092
3093/*
3094 * Performs common checks before doing a file copy/clone
3095 * from @file_in to @file_out.
3096 */
3097int generic_file_rw_checks(struct file *file_in, struct file *file_out)
3098{
3099 struct inode *inode_in = file_inode(file_in);
3100 struct inode *inode_out = file_inode(file_out);
3101
3102 /* Don't copy dirs, pipes, sockets... */
3103 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
3104 return -EISDIR;
3105 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
3106 return -EINVAL;
3107
3108 if (!(file_in->f_mode & FMODE_READ) ||
3109 !(file_out->f_mode & FMODE_WRITE) ||
3110 (file_out->f_flags & O_APPEND))
3111 return -EBADF;
3112
3113 return 0;
3114}
3115
Amir Goldstein96e6e8f2019-06-05 08:04:49 -07003116/*
3117 * Performs necessary checks before doing a file copy
3118 *
3119 * Can adjust amount of bytes to copy via @req_count argument.
3120 * Returns appropriate error code that caller should return or
3121 * zero in case the copy should be allowed.
3122 */
3123int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
3124 struct file *file_out, loff_t pos_out,
3125 size_t *req_count, unsigned int flags)
3126{
3127 struct inode *inode_in = file_inode(file_in);
3128 struct inode *inode_out = file_inode(file_out);
3129 uint64_t count = *req_count;
3130 loff_t size_in;
3131 int ret;
3132
3133 ret = generic_file_rw_checks(file_in, file_out);
3134 if (ret)
3135 return ret;
3136
3137 /* Don't touch certain kinds of inodes */
3138 if (IS_IMMUTABLE(inode_out))
3139 return -EPERM;
3140
3141 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
3142 return -ETXTBSY;
3143
3144 /* Ensure offsets don't wrap. */
3145 if (pos_in + count < pos_in || pos_out + count < pos_out)
3146 return -EOVERFLOW;
3147
3148 /* Shorten the copy to EOF */
3149 size_in = i_size_read(inode_in);
3150 if (pos_in >= size_in)
3151 count = 0;
3152 else
3153 count = min(count, size_in - (uint64_t)pos_in);
3154
3155 ret = generic_write_check_limits(file_out, pos_out, &count);
3156 if (ret)
3157 return ret;
3158
3159 /* Don't allow overlapped copying within the same file. */
3160 if (inode_in == inode_out &&
3161 pos_out + count > pos_in &&
3162 pos_out < pos_in + count)
3163 return -EINVAL;
3164
3165 *req_count = count;
3166 return 0;
3167}
3168
Nick Pigginafddba42007-10-16 01:25:01 -07003169int pagecache_write_begin(struct file *file, struct address_space *mapping,
3170 loff_t pos, unsigned len, unsigned flags,
3171 struct page **pagep, void **fsdata)
3172{
3173 const struct address_space_operations *aops = mapping->a_ops;
3174
Nick Piggin4e02ed42008-10-29 14:00:55 -07003175 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003176 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003177}
3178EXPORT_SYMBOL(pagecache_write_begin);
3179
3180int pagecache_write_end(struct file *file, struct address_space *mapping,
3181 loff_t pos, unsigned len, unsigned copied,
3182 struct page *page, void *fsdata)
3183{
3184 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003185
Nick Piggin4e02ed42008-10-29 14:00:55 -07003186 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003187}
3188EXPORT_SYMBOL(pagecache_write_end);
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003191generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 struct file *file = iocb->ki_filp;
3194 struct address_space *mapping = file->f_mapping;
3195 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003196 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003198 size_t write_len;
3199 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Al Viro0c949332014-03-22 06:51:37 -04003201 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003202 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003203
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003204 if (iocb->ki_flags & IOCB_NOWAIT) {
3205 /* If there are pages to writeback, return */
3206 if (filemap_range_has_page(inode->i_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003207 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003208 return -EAGAIN;
3209 } else {
3210 written = filemap_write_and_wait_range(mapping, pos,
3211 pos + write_len - 1);
3212 if (written)
3213 goto out;
3214 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003215
3216 /*
3217 * After a write we want buffered reads to be sure to go to disk to get
3218 * the new data. We invalidate clean cached page from the region we're
3219 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003220 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003221 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003222 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003223 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003224 /*
3225 * If a page can not be invalidated, return 0 to fall back
3226 * to buffered write.
3227 */
3228 if (written) {
3229 if (written == -EBUSY)
3230 return 0;
3231 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003232 }
3233
Al Viro639a93a52017-04-13 14:10:15 -04003234 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003235
3236 /*
3237 * Finally, try again to invalidate clean pages which might have been
3238 * cached by non-direct readahead, or faulted in by get_user_pages()
3239 * if the source of the write was an mmap'ed region of the file
3240 * we're writing. Either one is a pretty crazy thing to do,
3241 * so we don't support it 100%. If this invalidation
3242 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003243 *
3244 * Most of the time we do not need this since dio_complete() will do
3245 * the invalidation for us. However there are some file systems that
3246 * do not end up with dio_complete() being called, so let's not break
3247 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003248 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003249 if (mapping->nrpages)
3250 invalidate_inode_pages2_range(mapping,
3251 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003254 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003255 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003256 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3257 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 mark_inode_dirty(inode);
3259 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003260 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Al Viro639a93a52017-04-13 14:10:15 -04003262 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003263out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return written;
3265}
3266EXPORT_SYMBOL(generic_file_direct_write);
3267
Nick Piggineb2be182007-10-16 01:24:57 -07003268/*
3269 * Find or create a page at the given pagecache position. Return the locked
3270 * page. This function is specifically for buffered writes.
3271 */
Nick Piggin54566b22009-01-04 12:00:53 -08003272struct page *grab_cache_page_write_begin(struct address_space *mapping,
3273 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003274{
Nick Piggineb2be182007-10-16 01:24:57 -07003275 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003276 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003277
Nick Piggin54566b22009-01-04 12:00:53 -08003278 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003279 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003280
Mel Gorman2457aec2014-06-04 16:10:31 -07003281 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003282 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003283 if (page)
3284 wait_for_stable_page(page);
3285
Nick Piggineb2be182007-10-16 01:24:57 -07003286 return page;
3287}
Nick Piggin54566b22009-01-04 12:00:53 -08003288EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003289
Al Viro3b93f912014-02-11 21:34:08 -05003290ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003291 struct iov_iter *i, loff_t pos)
3292{
3293 struct address_space *mapping = file->f_mapping;
3294 const struct address_space_operations *a_ops = mapping->a_ops;
3295 long status = 0;
3296 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003297 unsigned int flags = 0;
3298
Nick Pigginafddba42007-10-16 01:25:01 -07003299 do {
3300 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003301 unsigned long offset; /* Offset into pagecache page */
3302 unsigned long bytes; /* Bytes to write to page */
3303 size_t copied; /* Bytes copied from user */
3304 void *fsdata;
3305
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003306 offset = (pos & (PAGE_SIZE - 1));
3307 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003308 iov_iter_count(i));
3309
3310again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003311 /*
3312 * Bring in the user page that we will copy from _first_.
3313 * Otherwise there's a nasty deadlock on copying from the
3314 * same page as we're writing to, without it being marked
3315 * up-to-date.
3316 *
3317 * Not only is this an optimisation, but it is also required
3318 * to check that the address is actually valid, when atomic
3319 * usercopies are used, below.
3320 */
3321 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3322 status = -EFAULT;
3323 break;
3324 }
3325
Jan Kara296291c2015-10-22 13:32:21 -07003326 if (fatal_signal_pending(current)) {
3327 status = -EINTR;
3328 break;
3329 }
3330
Nick Piggin674b8922007-10-16 01:25:03 -07003331 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003332 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003333 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003334 break;
3335
anfei zhou931e80e2010-02-02 13:44:02 -08003336 if (mapping_writably_mapped(mapping))
3337 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003338
Nick Pigginafddba42007-10-16 01:25:01 -07003339 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003340 flush_dcache_page(page);
3341
3342 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3343 page, fsdata);
3344 if (unlikely(status < 0))
3345 break;
3346 copied = status;
3347
3348 cond_resched();
3349
Nick Piggin124d3b72008-02-02 15:01:17 +01003350 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003351 if (unlikely(copied == 0)) {
3352 /*
3353 * If we were unable to copy any data at all, we must
3354 * fall back to a single segment length write.
3355 *
3356 * If we didn't fallback here, we could livelock
3357 * because not all segments in the iov can be copied at
3358 * once without a pagefault.
3359 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003360 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003361 iov_iter_single_seg_count(i));
3362 goto again;
3363 }
Nick Pigginafddba42007-10-16 01:25:01 -07003364 pos += copied;
3365 written += copied;
3366
3367 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003368 } while (iov_iter_count(i));
3369
3370 return written ? written : status;
3371}
Al Viro3b93f912014-02-11 21:34:08 -05003372EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Jan Karae4dd9de2009-08-17 18:10:06 +02003374/**
Al Viro81742022014-04-03 03:17:43 -04003375 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003376 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003377 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003378 *
3379 * This function does all the work needed for actually writing data to a
3380 * file. It does all basic checks, removes SUID from the file, updates
3381 * modification times and calls proper subroutines depending on whether we
3382 * do direct IO or a standard buffered write.
3383 *
3384 * It expects i_mutex to be grabbed unless we work on a block device or similar
3385 * object which does not need locking at all.
3386 *
3387 * This function does *not* take care of syncing data in case of O_SYNC write.
3388 * A caller has to handle it. This is mainly due to the fact that we want to
3389 * avoid syncing under i_mutex.
Mike Rapoporta862f682019-03-05 15:48:42 -08003390 *
3391 * Return:
3392 * * number of bytes written, even for truncated writes
3393 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003394 */
Al Viro81742022014-04-03 03:17:43 -04003395ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003398 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003400 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003402 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003405 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003406 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (err)
3408 goto out;
3409
Josef Bacikc3b2da32012-03-26 09:59:21 -04003410 err = file_update_time(file);
3411 if (err)
3412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Al Viro2ba48ce2015-04-09 13:52:01 -04003414 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003415 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003416
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003417 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003418 /*
3419 * If the write stopped short of completing, fall back to
3420 * buffered writes. Some filesystems do this for writes to
3421 * holes, for example. For DAX files, a buffered write will
3422 * not succeed (even if it did, DAX does not handle dirty
3423 * page-cache pages correctly).
3424 */
Al Viro0b8def92015-04-07 10:22:53 -04003425 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003427
Al Viro0b8def92015-04-07 10:22:53 -04003428 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003429 /*
Al Viro3b93f912014-02-11 21:34:08 -05003430 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003431 * then we want to return the number of bytes which were
3432 * direct-written, or the error code if that was zero. Note
3433 * that this differs from normal direct-io semantics, which
3434 * will return -EFOO even if some bytes were written.
3435 */
Al Viro60bb4522014-08-08 12:39:16 -04003436 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003437 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003438 goto out;
3439 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003440 /*
3441 * We need to ensure that the page cache pages are written to
3442 * disk and invalidated to preserve the expected O_DIRECT
3443 * semantics.
3444 */
Al Viro3b93f912014-02-11 21:34:08 -05003445 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003446 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003447 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003448 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003449 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003450 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003451 pos >> PAGE_SHIFT,
3452 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003453 } else {
3454 /*
3455 * We don't know how much we wrote, so just return
3456 * the number of bytes which were direct-written
3457 */
3458 }
3459 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003460 written = generic_perform_write(file, from, iocb->ki_pos);
3461 if (likely(written > 0))
3462 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464out:
3465 current->backing_dev_info = NULL;
3466 return written ? written : err;
3467}
Al Viro81742022014-04-03 03:17:43 -04003468EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Jan Karae4dd9de2009-08-17 18:10:06 +02003470/**
Al Viro81742022014-04-03 03:17:43 -04003471 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003472 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003473 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003474 *
Al Viro81742022014-04-03 03:17:43 -04003475 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003476 * filesystems. It takes care of syncing the file in case of O_SYNC file
3477 * and acquires i_mutex as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003478 * Return:
3479 * * negative error code if no data has been written at all of
3480 * vfs_fsync_range() failed for a synchronous write
3481 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003482 */
Al Viro81742022014-04-03 03:17:43 -04003483ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
3485 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003486 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Al Viro59551022016-01-22 15:40:57 -05003489 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003490 ret = generic_write_checks(iocb, from);
3491 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003492 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003493 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Christoph Hellwige2592212016-04-07 08:52:01 -07003495 if (ret > 0)
3496 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return ret;
3498}
Al Viro81742022014-04-03 03:17:43 -04003499EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
David Howellscf9a2ae2006-08-29 19:05:54 +01003501/**
3502 * try_to_release_page() - release old fs-specific metadata on a page
3503 *
3504 * @page: the page which the kernel is trying to free
3505 * @gfp_mask: memory allocation flags (and I/O mode)
3506 *
3507 * The address_space is to try to release any data against the page
Mike Rapoporta862f682019-03-05 15:48:42 -08003508 * (presumably at page->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003509 *
David Howells266cf652009-04-03 16:42:36 +01003510 * This may also be called if PG_fscache is set on a page, indicating that the
3511 * page is known to the local caching routines.
3512 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003513 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003514 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003515 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003516 * Return: %1 if the release was successful, otherwise return zero.
David Howellscf9a2ae2006-08-29 19:05:54 +01003517 */
3518int try_to_release_page(struct page *page, gfp_t gfp_mask)
3519{
3520 struct address_space * const mapping = page->mapping;
3521
3522 BUG_ON(!PageLocked(page));
3523 if (PageWriteback(page))
3524 return 0;
3525
3526 if (mapping && mapping->a_ops->releasepage)
3527 return mapping->a_ops->releasepage(page, gfp_mask);
3528 return try_to_free_buffers(page);
3529}
3530
3531EXPORT_SYMBOL(try_to_release_page);