blob: 4157f858a9c685c922a7d6ddac46b9dd754ab91b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/filemap.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
11 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080014#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070017#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/mman.h>
24#include <linux/pagemap.h>
25#include <linux/file.h>
26#include <linux/uio.h>
27#include <linux/hash.h>
28#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070029#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/pagevec.h>
31#include <linux/blkdev.h>
32#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080033#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070034#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060036#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080037#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070038#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070039#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070040#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080041#include "internal.h"
42
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070043#define CREATE_TRACE_POINTS
44#include <trace/events/filemap.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * FIXME: remove all knowledge of the buffer layer from the core VM
48 */
Jan Kara148f9482009-08-17 19:52:36 +020049#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/mman.h>
52
53/*
54 * Shared mappings implemented 30.11.1994. It's not fully working yet,
55 * though.
56 *
57 * Shared mappings now work. 15.8.1995 Bruno.
58 *
59 * finished 'unifying' the page and buffer cache and SMP-threaded the
60 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
61 *
62 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
63 */
64
65/*
66 * Lock ordering:
67 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080068 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070070 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070071 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080073 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080074 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080077 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070078 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070079 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
81 * ->mmap_sem
82 * ->lock_page (access_process_vm)
83 *
Al Viroccad2362014-02-11 22:36:48 -050084 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070085 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060087 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110088 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070089 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080091 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ->anon_vma.lock (vma_adjust)
93 *
94 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070095 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070097 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070098 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700100 * ->i_pages lock (try_to_unmap_one)
Andrey Ryabininf4b7e272019-03-05 15:49:39 -0800101 * ->pgdat->lru_lock (follow_page->mark_page_accessed)
102 * ->pgdat->lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700104 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600105 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100106 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700107 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600108 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100109 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
111 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800112 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700113 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500116static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700117 struct page *page, void *shadow)
118{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500119 XA_STATE(xas, &mapping->i_pages, page->index);
120 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800121
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500122 mapping_set_update(&xas, mapping);
123
124 /* hugetlb pages are represented by a single entry in the xarray */
125 if (!PageHuge(page)) {
126 xas_set_order(&xas, page->index, compound_order(page));
127 nr = 1U << compound_order(page);
128 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700129
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700130 VM_BUG_ON_PAGE(!PageLocked(page), page);
131 VM_BUG_ON_PAGE(PageTail(page), page);
132 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700133
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500134 xas_store(&xas, shadow);
135 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200136
Jan Kara23006382017-11-15 17:37:26 -0800137 page->mapping = NULL;
138 /* Leave page->index set: truncation lookup relies upon it */
139
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200140 if (shadow) {
141 mapping->nrexceptional += nr;
142 /*
143 * Make sure the nrexceptional update is committed before
144 * the nrpages update so that final truncate racing
145 * with reclaim does not see both counters 0 at the
146 * same time and miss a shadow entry.
147 */
148 smp_wmb();
149 }
150 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700151}
152
Jan Kara5ecc4d82017-11-15 17:37:29 -0800153static void unaccount_page_cache_page(struct address_space *mapping,
154 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800156 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600158 /*
159 * if we're uptodate, flush out into the cleancache, otherwise
160 * invalidate any existing cleancache entries. We can't leave
161 * stale data around in the cleancache once our page is gone
162 */
163 if (PageUptodate(page) && PageMappedToDisk(page))
164 cleancache_put_page(page);
165 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400166 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600167
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700168 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800169 VM_BUG_ON_PAGE(page_mapped(page), page);
170 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
171 int mapcount;
172
173 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
174 current->comm, page_to_pfn(page));
175 dump_page(page, "still mapped when deleted");
176 dump_stack();
177 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
178
179 mapcount = page_mapcount(page);
180 if (mapping_exiting(mapping) &&
181 page_count(page) >= mapcount + 2) {
182 /*
183 * All vmas have already been torn down, so it's
184 * a good bet that actually the page is unmapped,
185 * and we'd prefer not to leak it: if we're wrong,
186 * some other bad page check should catch it later.
187 */
188 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700189 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800190 }
191 }
192
Jan Kara76253fb2017-11-15 17:37:22 -0800193 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800194 if (PageHuge(page))
195 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800196
Jan Kara5ecc4d82017-11-15 17:37:29 -0800197 nr = hpage_nr_pages(page);
198
199 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
200 if (PageSwapBacked(page)) {
201 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
202 if (PageTransHuge(page))
203 __dec_node_page_state(page, NR_SHMEM_THPS);
204 } else {
205 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800206 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800207
208 /*
209 * At this point page must be either written or cleaned by
210 * truncate. Dirty page here signals a bug and loss of
211 * unwritten data.
212 *
213 * This fixes dirty accounting after removing the page entirely
214 * but leaves PageDirty set: it has no effect for truncated
215 * page and anyway will be cleared before returning page into
216 * buddy allocator.
217 */
218 if (WARN_ON_ONCE(PageDirty(page)))
219 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
220}
221
222/*
223 * Delete a page from the page cache and free it. Caller has to make
224 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700225 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800226 */
227void __delete_from_page_cache(struct page *page, void *shadow)
228{
229 struct address_space *mapping = page->mapping;
230
231 trace_mm_filemap_delete_from_page_cache(page);
232
233 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500234 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Jan Kara59c66c52017-11-15 17:37:18 -0800237static void page_cache_free_page(struct address_space *mapping,
238 struct page *page)
239{
240 void (*freepage)(struct page *);
241
242 freepage = mapping->a_ops->freepage;
243 if (freepage)
244 freepage(page);
245
246 if (PageTransHuge(page) && !PageHuge(page)) {
247 page_ref_sub(page, HPAGE_PMD_NR);
248 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
249 } else {
250 put_page(page);
251 }
252}
253
Minchan Kim702cfbf2011-03-22 16:32:43 -0700254/**
255 * delete_from_page_cache - delete page from page cache
256 * @page: the page which the kernel is trying to remove from page cache
257 *
258 * This must be called only on pages that have been verified to be in the page
259 * cache and locked. It will never put the page into the free list, the caller
260 * has a reference on the page.
261 */
262void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700264 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400265 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Matt Mackallcd7619d2005-05-01 08:59:01 -0700267 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700268 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700269 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700270 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500271
Jan Kara59c66c52017-11-15 17:37:18 -0800272 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700273}
274EXPORT_SYMBOL(delete_from_page_cache);
275
Jan Karaaa65c292017-11-15 17:37:33 -0800276/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500277 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800278 * @mapping: the mapping to which pages belong
279 * @pvec: pagevec with pages to delete
280 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700281 * The function walks over mapping->i_pages and removes pages passed in @pvec
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700282 * from the mapping. The function expects @pvec to be sorted by page index
283 * and is optimised for it to be dense.
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700284 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800285 * modified). The function expects only THP head pages to be present in the
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700286 * @pvec.
Jan Karaaa65c292017-11-15 17:37:33 -0800287 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700288 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800289 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500290static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800291 struct pagevec *pvec)
292{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500293 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800294 int total_pages = 0;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700295 int i = 0;
Jan Karaaa65c292017-11-15 17:37:33 -0800296 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800297
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500298 mapping_set_update(&xas, mapping);
299 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700300 if (i >= pagevec_count(pvec))
Jan Karaaa65c292017-11-15 17:37:33 -0800301 break;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700302
303 /* A swap/dax/shadow entry got inserted? Skip it. */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400304 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800305 continue;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700306 /*
307 * A page got inserted in our range? Skip it. We have our
308 * pages locked so they are protected from being removed.
309 * If we see a page whose index is higher than ours, it
310 * means our page has been removed, which shouldn't be
311 * possible because we're holding the PageLock.
312 */
313 if (page != pvec->pages[i]) {
314 VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index,
315 page);
316 continue;
Jan Karaaa65c292017-11-15 17:37:33 -0800317 }
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700318
319 WARN_ON_ONCE(!PageLocked(page));
320
321 if (page->index == xas.xa_index)
322 page->mapping = NULL;
323 /* Leave page->index set: truncation lookup relies on it */
324
325 /*
326 * Move to the next page in the vector if this is a regular
327 * page or the index is of the last sub-page of this compound
328 * page.
329 */
330 if (page->index + (1UL << compound_order(page)) - 1 ==
331 xas.xa_index)
332 i++;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500333 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800334 total_pages++;
335 }
336 mapping->nrpages -= total_pages;
337}
338
339void delete_from_page_cache_batch(struct address_space *mapping,
340 struct pagevec *pvec)
341{
342 int i;
343 unsigned long flags;
344
345 if (!pagevec_count(pvec))
346 return;
347
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700348 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800349 for (i = 0; i < pagevec_count(pvec); i++) {
350 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
351
352 unaccount_page_cache_page(mapping, pvec->pages[i]);
353 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500354 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700355 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800356
357 for (i = 0; i < pagevec_count(pvec); i++)
358 page_cache_free_page(mapping, pvec->pages[i]);
359}
360
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200361int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700362{
363 int ret = 0;
364 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700365 if (test_bit(AS_ENOSPC, &mapping->flags) &&
366 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700367 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700368 if (test_bit(AS_EIO, &mapping->flags) &&
369 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700370 ret = -EIO;
371 return ret;
372}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200373EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700374
Jeff Layton76341ca2017-07-06 07:02:22 -0400375static int filemap_check_and_keep_errors(struct address_space *mapping)
376{
377 /* Check for outstanding write errors */
378 if (test_bit(AS_EIO, &mapping->flags))
379 return -EIO;
380 if (test_bit(AS_ENOSPC, &mapping->flags))
381 return -ENOSPC;
382 return 0;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700386 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700387 * @mapping: address space structure to write
388 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800389 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700390 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700392 * Start writeback against all of a mapping's dirty pages that lie
393 * within the byte offsets <start, end> inclusive.
394 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700396 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * these two operations is that if a dirty page/buffer is encountered, it must
398 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800399 *
400 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800402int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
403 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 int ret;
406 struct writeback_control wbc = {
407 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800408 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700409 .range_start = start,
410 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 };
412
413 if (!mapping_cap_writeback_dirty(mapping))
414 return 0;
415
Tejun Heob16b1de2015-06-02 08:39:48 -0600416 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600418 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return ret;
420}
421
422static inline int __filemap_fdatawrite(struct address_space *mapping,
423 int sync_mode)
424{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700425 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428int filemap_fdatawrite(struct address_space *mapping)
429{
430 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
431}
432EXPORT_SYMBOL(filemap_fdatawrite);
433
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400434int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800435 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
438}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400439EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Randy Dunlap485bb992006-06-23 02:03:49 -0700441/**
442 * filemap_flush - mostly a non-blocking flush
443 * @mapping: target address_space
444 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * This is a mostly non-blocking flush. Not suitable for data-integrity
446 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800447 *
448 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
450int filemap_flush(struct address_space *mapping)
451{
452 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
453}
454EXPORT_SYMBOL(filemap_flush);
455
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500456/**
457 * filemap_range_has_page - check if a page exists in range.
458 * @mapping: address space within which to check
459 * @start_byte: offset in bytes where the range starts
460 * @end_byte: offset in bytes where the range ends (inclusive)
461 *
462 * Find at least one page in the range supplied, usually used to check if
463 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800464 *
465 * Return: %true if at least one page exists in the specified range,
466 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500467 */
468bool filemap_range_has_page(struct address_space *mapping,
469 loff_t start_byte, loff_t end_byte)
470{
Jan Karaf7b68042017-09-06 16:21:40 -0700471 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500472 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
473 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500474
475 if (end_byte < start_byte)
476 return false;
477
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500478 rcu_read_lock();
479 for (;;) {
480 page = xas_find(&xas, max);
481 if (xas_retry(&xas, page))
482 continue;
483 /* Shadow entries don't count */
484 if (xa_is_value(page))
485 continue;
486 /*
487 * We don't need to try to pin this page; we're about to
488 * release the RCU lock anyway. It is enough to know that
489 * there was a page here recently.
490 */
491 break;
492 }
493 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500494
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500495 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500496}
497EXPORT_SYMBOL(filemap_range_has_page);
498
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400499static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800500 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300502 pgoff_t index = start_byte >> PAGE_SHIFT;
503 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct pagevec pvec;
505 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200507 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Mel Gorman86679822017-11-15 17:37:52 -0800510 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800511 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unsigned i;
513
Jan Kara312e9d22017-11-15 17:35:05 -0800514 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800515 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800516 if (!nr_pages)
517 break;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 for (i = 0; i < nr_pages; i++) {
520 struct page *page = pvec.pages[i];
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400523 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 pagevec_release(&pvec);
526 cond_resched();
527 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800528}
529
530/**
531 * filemap_fdatawait_range - wait for writeback to complete
532 * @mapping: address space structure to wait for
533 * @start_byte: offset in bytes where the range starts
534 * @end_byte: offset in bytes where the range ends (inclusive)
535 *
536 * Walk the list of under-writeback pages of the given address space
537 * in the given range and wait for all of them. Check error status of
538 * the address space and return it.
539 *
540 * Since the error status of the address space is cleared by this function,
541 * callers are responsible for checking the return value and handling and/or
542 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800543 *
544 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800545 */
546int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
547 loff_t end_byte)
548{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400549 __filemap_fdatawait_range(mapping, start_byte, end_byte);
550 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200552EXPORT_SYMBOL(filemap_fdatawait_range);
553
554/**
Jeff Laytona823e452017-07-28 07:24:43 -0400555 * file_fdatawait_range - wait for writeback to complete
556 * @file: file pointing to address space structure to wait for
557 * @start_byte: offset in bytes where the range starts
558 * @end_byte: offset in bytes where the range ends (inclusive)
559 *
560 * Walk the list of under-writeback pages of the address space that file
561 * refers to, in the given range and wait for all of them. Check error
562 * status of the address space vs. the file->f_wb_err cursor and return it.
563 *
564 * Since the error status of the file is advanced by this function,
565 * callers are responsible for checking the return value and handling and/or
566 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800567 *
568 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400569 */
570int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
571{
572 struct address_space *mapping = file->f_mapping;
573
574 __filemap_fdatawait_range(mapping, start_byte, end_byte);
575 return file_check_and_advance_wb_err(file);
576}
577EXPORT_SYMBOL(file_fdatawait_range);
578
579/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800580 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
581 * @mapping: address space structure to wait for
582 *
583 * Walk the list of under-writeback pages of the given address space
584 * and wait for all of them. Unlike filemap_fdatawait(), this function
585 * does not clear error status of the address space.
586 *
587 * Use this function if callers don't handle errors themselves. Expected
588 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
589 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800590 *
591 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800592 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400593int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800594{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400595 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400596 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800597}
Jeff Layton76341ca2017-07-06 07:02:22 -0400598EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800599
Jeff Layton9326c9b2017-07-26 10:21:11 -0400600static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400602 return (!dax_mapping(mapping) && mapping->nrpages) ||
603 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606int filemap_write_and_wait(struct address_space *mapping)
607{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800608 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jeff Layton9326c9b2017-07-26 10:21:11 -0400610 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800611 err = filemap_fdatawrite(mapping);
612 /*
613 * Even if the above returned error, the pages may be
614 * written partially (e.g. -ENOSPC), so we wait for it.
615 * But the -EIO is special case, it may indicate the worst
616 * thing (e.g. bug) happened, so we avoid waiting for it.
617 */
618 if (err != -EIO) {
619 int err2 = filemap_fdatawait(mapping);
620 if (!err)
621 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400622 } else {
623 /* Clear any previously stored errors */
624 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800625 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700626 } else {
627 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800629 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800631EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Randy Dunlap485bb992006-06-23 02:03:49 -0700633/**
634 * filemap_write_and_wait_range - write out & wait on a file range
635 * @mapping: the address_space for the pages
636 * @lstart: offset in bytes where the range starts
637 * @lend: offset in bytes where the range ends (inclusive)
638 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800639 * Write out and wait upon file offsets lstart->lend, inclusive.
640 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300641 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800642 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800643 *
644 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646int filemap_write_and_wait_range(struct address_space *mapping,
647 loff_t lstart, loff_t lend)
648{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800649 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jeff Layton9326c9b2017-07-26 10:21:11 -0400651 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800652 err = __filemap_fdatawrite_range(mapping, lstart, lend,
653 WB_SYNC_ALL);
654 /* See comment of filemap_write_and_wait() */
655 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200656 int err2 = filemap_fdatawait_range(mapping,
657 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800658 if (!err)
659 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400660 } else {
661 /* Clear any previously stored errors */
662 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800663 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700664 } else {
665 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800667 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
Chris Masonf6995582009-04-15 13:22:37 -0400669EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jeff Layton5660e132017-07-06 07:02:25 -0400671void __filemap_set_wb_err(struct address_space *mapping, int err)
672{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400673 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400674
675 trace_filemap_set_wb_err(mapping, eseq);
676}
677EXPORT_SYMBOL(__filemap_set_wb_err);
678
679/**
680 * file_check_and_advance_wb_err - report wb error (if any) that was previously
681 * and advance wb_err to current one
682 * @file: struct file on which the error is being reported
683 *
684 * When userland calls fsync (or something like nfsd does the equivalent), we
685 * want to report any writeback errors that occurred since the last fsync (or
686 * since the file was opened if there haven't been any).
687 *
688 * Grab the wb_err from the mapping. If it matches what we have in the file,
689 * then just quickly return 0. The file is all caught up.
690 *
691 * If it doesn't match, then take the mapping value, set the "seen" flag in
692 * it and try to swap it into place. If it works, or another task beat us
693 * to it with the new value, then update the f_wb_err and return the error
694 * portion. The error at this point must be reported via proper channels
695 * (a'la fsync, or NFS COMMIT operation, etc.).
696 *
697 * While we handle mapping->wb_err with atomic operations, the f_wb_err
698 * value is protected by the f_lock since we must ensure that it reflects
699 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800700 *
701 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400702 */
703int file_check_and_advance_wb_err(struct file *file)
704{
705 int err = 0;
706 errseq_t old = READ_ONCE(file->f_wb_err);
707 struct address_space *mapping = file->f_mapping;
708
709 /* Locklessly handle the common case where nothing has changed */
710 if (errseq_check(&mapping->wb_err, old)) {
711 /* Something changed, must use slow path */
712 spin_lock(&file->f_lock);
713 old = file->f_wb_err;
714 err = errseq_check_and_advance(&mapping->wb_err,
715 &file->f_wb_err);
716 trace_file_check_and_advance_wb_err(file, old);
717 spin_unlock(&file->f_lock);
718 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700719
720 /*
721 * We're mostly using this function as a drop in replacement for
722 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
723 * that the legacy code would have had on these flags.
724 */
725 clear_bit(AS_EIO, &mapping->flags);
726 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400727 return err;
728}
729EXPORT_SYMBOL(file_check_and_advance_wb_err);
730
731/**
732 * file_write_and_wait_range - write out & wait on a file range
733 * @file: file pointing to address_space with pages
734 * @lstart: offset in bytes where the range starts
735 * @lend: offset in bytes where the range ends (inclusive)
736 *
737 * Write out and wait upon file offsets lstart->lend, inclusive.
738 *
739 * Note that @lend is inclusive (describes the last byte to be written) so
740 * that this function can be used to write to the very end-of-file (end = -1).
741 *
742 * After writing out and waiting on the data, we check and advance the
743 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800744 *
745 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400746 */
747int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
748{
749 int err = 0, err2;
750 struct address_space *mapping = file->f_mapping;
751
Jeff Layton9326c9b2017-07-26 10:21:11 -0400752 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400753 err = __filemap_fdatawrite_range(mapping, lstart, lend,
754 WB_SYNC_ALL);
755 /* See comment of filemap_write_and_wait() */
756 if (err != -EIO)
757 __filemap_fdatawait_range(mapping, lstart, lend);
758 }
759 err2 = file_check_and_advance_wb_err(file);
760 if (!err)
761 err = err2;
762 return err;
763}
764EXPORT_SYMBOL(file_write_and_wait_range);
765
Randy Dunlap485bb992006-06-23 02:03:49 -0700766/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700767 * replace_page_cache_page - replace a pagecache page with a new one
768 * @old: page to be replaced
769 * @new: page to replace with
770 * @gfp_mask: allocation mode
771 *
772 * This function replaces a page in the pagecache with a new one. On
773 * success it acquires the pagecache reference for the new page and
774 * drops it for the old page. Both the old and new pages must be
775 * locked. This function does not add the new page to the LRU, the
776 * caller must do that.
777 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500778 * The remove + add is atomic. This function cannot fail.
Mike Rapoporta862f682019-03-05 15:48:42 -0800779 *
780 * Return: %0
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700781 */
782int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
783{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500784 struct address_space *mapping = old->mapping;
785 void (*freepage)(struct page *) = mapping->a_ops->freepage;
786 pgoff_t offset = old->index;
787 XA_STATE(xas, &mapping->i_pages, offset);
788 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700789
Sasha Levin309381fea2014-01-23 15:52:54 -0800790 VM_BUG_ON_PAGE(!PageLocked(old), old);
791 VM_BUG_ON_PAGE(!PageLocked(new), new);
792 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700793
Matthew Wilcox74d60952017-11-17 10:01:45 -0500794 get_page(new);
795 new->mapping = mapping;
796 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700797
Matthew Wilcox74d60952017-11-17 10:01:45 -0500798 xas_lock_irqsave(&xas, flags);
799 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700800
Matthew Wilcox74d60952017-11-17 10:01:45 -0500801 old->mapping = NULL;
802 /* hugetlb pages do not participate in page cache accounting. */
803 if (!PageHuge(old))
804 __dec_node_page_state(new, NR_FILE_PAGES);
805 if (!PageHuge(new))
806 __inc_node_page_state(new, NR_FILE_PAGES);
807 if (PageSwapBacked(old))
808 __dec_node_page_state(new, NR_SHMEM);
809 if (PageSwapBacked(new))
810 __inc_node_page_state(new, NR_SHMEM);
811 xas_unlock_irqrestore(&xas, flags);
812 mem_cgroup_migrate(old, new);
813 if (freepage)
814 freepage(old);
815 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700816
Matthew Wilcox74d60952017-11-17 10:01:45 -0500817 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700818}
819EXPORT_SYMBOL_GPL(replace_page_cache_page);
820
Johannes Weinera5289102014-04-03 14:47:51 -0700821static int __add_to_page_cache_locked(struct page *page,
822 struct address_space *mapping,
823 pgoff_t offset, gfp_t gfp_mask,
824 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500826 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700827 int huge = PageHuge(page);
828 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700829 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500830 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700831
Sasha Levin309381fea2014-01-23 15:52:54 -0800832 VM_BUG_ON_PAGE(!PageLocked(page), page);
833 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500834 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700835
Johannes Weiner00501b52014-08-08 14:19:20 -0700836 if (!huge) {
837 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800838 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700839 if (error)
840 return error;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300843 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700844 page->mapping = mapping;
845 page->index = offset;
846
Matthew Wilcox74d60952017-11-17 10:01:45 -0500847 do {
848 xas_lock_irq(&xas);
849 old = xas_load(&xas);
850 if (old && !xa_is_value(old))
851 xas_set_err(&xas, -EEXIST);
852 xas_store(&xas, page);
853 if (xas_error(&xas))
854 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700855
Matthew Wilcox74d60952017-11-17 10:01:45 -0500856 if (xa_is_value(old)) {
857 mapping->nrexceptional--;
858 if (shadowp)
859 *shadowp = old;
860 }
861 mapping->nrpages++;
862
863 /* hugetlb pages do not participate in page cache accounting */
864 if (!huge)
865 __inc_node_page_state(page, NR_FILE_PAGES);
866unlock:
867 xas_unlock_irq(&xas);
868 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
869
870 if (xas_error(&xas))
871 goto error;
872
Johannes Weiner00501b52014-08-08 14:19:20 -0700873 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800874 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700875 trace_mm_filemap_add_to_page_cache(page);
876 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500877error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700878 page->mapping = NULL;
879 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700880 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800881 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300882 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500883 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Johannes Weinera5289102014-04-03 14:47:51 -0700885
886/**
887 * add_to_page_cache_locked - add a locked page to the pagecache
888 * @page: page to add
889 * @mapping: the page's address_space
890 * @offset: page index
891 * @gfp_mask: page allocation mode
892 *
893 * This function is used to add a page to the pagecache. It must be locked.
894 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800895 *
896 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700897 */
898int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
899 pgoff_t offset, gfp_t gfp_mask)
900{
901 return __add_to_page_cache_locked(page, mapping, offset,
902 gfp_mask, NULL);
903}
Nick Piggine2867812008-07-25 19:45:30 -0700904EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400907 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Johannes Weinera5289102014-04-03 14:47:51 -0700909 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700910 int ret;
911
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800912 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700913 ret = __add_to_page_cache_locked(page, mapping, offset,
914 gfp_mask, &shadow);
915 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800916 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700917 else {
918 /*
919 * The page might have been evicted from cache only
920 * recently, in which case it should be activated like
921 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700922 * The exception is pages getting rewritten; evicting other
923 * data from the working set, only to cache data that will
924 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700925 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700926 WARN_ON_ONCE(PageActive(page));
927 if (!(gfp_mask & __GFP_WRITE) && shadow)
928 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700929 lru_cache_add(page);
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return ret;
932}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300933EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Paul Jackson44110fe2006-03-24 03:16:04 -0800935#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700936struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800937{
Miao Xiec0ff7452010-05-24 14:32:08 -0700938 int n;
939 struct page *page;
940
Paul Jackson44110fe2006-03-24 03:16:04 -0800941 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700942 unsigned int cpuset_mems_cookie;
943 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700944 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700945 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700946 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700947 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700948
Miao Xiec0ff7452010-05-24 14:32:08 -0700949 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800950 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700951 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800952}
Nick Piggin2ae88142006-10-28 10:38:23 -0700953EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800954#endif
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/*
957 * In order to wait for pages to become available there must be
958 * waitqueues associated with pages. By using a hash table of
959 * waitqueues where the bucket discipline is to maintain all
960 * waiters on the same queue and wake all when any of the pages
961 * become available, and for the woken contexts to check to be
962 * sure the appropriate page became available, this saves space
963 * at a cost of "thundering herd" phenomena during rare hash
964 * collisions.
965 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000966#define PAGE_WAIT_TABLE_BITS 8
967#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
968static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
969
970static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Nicholas Piggin62906022016-12-25 13:00:30 +1000972 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
Nicholas Piggin62906022016-12-25 13:00:30 +1000974
975void __init pagecache_init(void)
976{
977 int i;
978
979 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
980 init_waitqueue_head(&page_wait_table[i]);
981
982 page_writeback_init();
983}
984
Linus Torvalds3510ca22017-08-27 13:55:12 -0700985/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000986struct wait_page_key {
987 struct page *page;
988 int bit_nr;
989 int page_match;
990};
991
992struct wait_page_queue {
993 struct page *page;
994 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200995 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000996};
997
Ingo Molnarac6424b2017-06-20 12:06:13 +0200998static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000999{
1000 struct wait_page_key *key = arg;
1001 struct wait_page_queue *wait_page
1002 = container_of(wait, struct wait_page_queue, wait);
1003
1004 if (wait_page->page != key->page)
1005 return 0;
1006 key->page_match = 1;
1007
1008 if (wait_page->bit_nr != key->bit_nr)
1009 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001010
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001011 /*
1012 * Stop walking if it's locked.
1013 * Is this safe if put_and_wait_on_page_locked() is in use?
1014 * Yes: the waker must hold a reference to this page, and if PG_locked
1015 * has now already been set by another task, that task must also hold
1016 * a reference to the *same usage* of this page; so there is no need
1017 * to walk on to wake even the put_and_wait_on_page_locked() callers.
1018 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001019 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001020 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001021
1022 return autoremove_wake_function(wait, mode, sync, key);
1023}
1024
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001025static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001026{
1027 wait_queue_head_t *q = page_waitqueue(page);
1028 struct wait_page_key key;
1029 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001030 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001031
1032 key.page = page;
1033 key.bit_nr = bit_nr;
1034 key.page_match = 0;
1035
Tim Chen11a19c72017-08-25 09:13:55 -07001036 bookmark.flags = 0;
1037 bookmark.private = NULL;
1038 bookmark.func = NULL;
1039 INIT_LIST_HEAD(&bookmark.entry);
1040
Nicholas Piggin62906022016-12-25 13:00:30 +10001041 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001042 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1043
1044 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1045 /*
1046 * Take a breather from holding the lock,
1047 * allow pages that finish wake up asynchronously
1048 * to acquire the lock and remove themselves
1049 * from wait queue
1050 */
1051 spin_unlock_irqrestore(&q->lock, flags);
1052 cpu_relax();
1053 spin_lock_irqsave(&q->lock, flags);
1054 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1055 }
1056
Nicholas Piggin62906022016-12-25 13:00:30 +10001057 /*
1058 * It is possible for other pages to have collided on the waitqueue
1059 * hash, so in that case check for a page match. That prevents a long-
1060 * term waiter
1061 *
1062 * It is still possible to miss a case here, when we woke page waiters
1063 * and removed them from the waitqueue, but there are still other
1064 * page waiters.
1065 */
1066 if (!waitqueue_active(q) || !key.page_match) {
1067 ClearPageWaiters(page);
1068 /*
1069 * It's possible to miss clearing Waiters here, when we woke
1070 * our page waiters, but the hashed waitqueue has waiters for
1071 * other pages on it.
1072 *
1073 * That's okay, it's a rare case. The next waker will clear it.
1074 */
1075 }
1076 spin_unlock_irqrestore(&q->lock, flags);
1077}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001078
1079static void wake_up_page(struct page *page, int bit)
1080{
1081 if (!PageWaiters(page))
1082 return;
1083 wake_up_page_bit(page, bit);
1084}
Nicholas Piggin62906022016-12-25 13:00:30 +10001085
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001086/*
1087 * A choice of three behaviors for wait_on_page_bit_common():
1088 */
1089enum behavior {
1090 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
1091 * __lock_page() waiting on then setting PG_locked.
1092 */
1093 SHARED, /* Hold ref to page and check the bit when woken, like
1094 * wait_on_page_writeback() waiting on PG_writeback.
1095 */
1096 DROP, /* Drop ref to page before wait, no check when woken,
1097 * like put_and_wait_on_page_locked() on PG_locked.
1098 */
1099};
1100
Nicholas Piggin62906022016-12-25 13:00:30 +10001101static inline int wait_on_page_bit_common(wait_queue_head_t *q,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001102 struct page *page, int bit_nr, int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001103{
1104 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001105 wait_queue_entry_t *wait = &wait_page.wait;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001106 bool bit_is_set;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001107 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001108 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001109 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001110 int ret = 0;
1111
Johannes Weinereb414682018-10-26 15:06:27 -07001112 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001113 !PageUptodate(page) && PageWorkingset(page)) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001114 if (!PageSwapBacked(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001115 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001116 delayacct = true;
1117 }
Johannes Weinereb414682018-10-26 15:06:27 -07001118 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001119 thrashing = true;
1120 }
1121
Nicholas Piggin62906022016-12-25 13:00:30 +10001122 init_wait(wait);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001123 wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001124 wait->func = wake_page_function;
1125 wait_page.page = page;
1126 wait_page.bit_nr = bit_nr;
1127
1128 for (;;) {
1129 spin_lock_irq(&q->lock);
1130
Ingo Molnar2055da92017-06-20 12:06:46 +02001131 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001132 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001133 SetPageWaiters(page);
1134 }
1135
1136 set_current_state(state);
1137
1138 spin_unlock_irq(&q->lock);
1139
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001140 bit_is_set = test_bit(bit_nr, &page->flags);
1141 if (behavior == DROP)
1142 put_page(page);
Nicholas Piggin62906022016-12-25 13:00:30 +10001143
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001144 if (likely(bit_is_set))
1145 io_schedule();
1146
1147 if (behavior == EXCLUSIVE) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001148 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1149 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001150 } else if (behavior == SHARED) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001151 if (!test_bit(bit_nr, &page->flags))
1152 break;
1153 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001154
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001155 if (signal_pending_state(state, current)) {
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001156 ret = -EINTR;
1157 break;
1158 }
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001159
1160 if (behavior == DROP) {
1161 /*
1162 * We can no longer safely access page->flags:
1163 * even if CONFIG_MEMORY_HOTREMOVE is not enabled,
1164 * there is a risk of waiting forever on a page reused
1165 * for something that keeps it locked indefinitely.
1166 * But best check for -EINTR above before breaking.
1167 */
1168 break;
1169 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001170 }
1171
1172 finish_wait(q, wait);
1173
Johannes Weinereb414682018-10-26 15:06:27 -07001174 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001175 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001176 delayacct_thrashing_end();
1177 psi_memstall_leave(&pflags);
1178 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001179
Nicholas Piggin62906022016-12-25 13:00:30 +10001180 /*
1181 * A signal could leave PageWaiters set. Clearing it here if
1182 * !waitqueue_active would be possible (by open-coding finish_wait),
1183 * but still fail to catch it in the case of wait hash collision. We
1184 * already can fail to clear wait hash collision cases, so don't
1185 * bother with signals either.
1186 */
1187
1188 return ret;
1189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Harvey Harrison920c7a52008-02-04 22:29:26 -08001191void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Nicholas Piggin62906022016-12-25 13:00:30 +10001193 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001194 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196EXPORT_SYMBOL(wait_on_page_bit);
1197
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001198int wait_on_page_bit_killable(struct page *page, int bit_nr)
1199{
Nicholas Piggin62906022016-12-25 13:00:30 +10001200 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001201 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001202}
David Howells4343d002017-11-02 15:27:52 +00001203EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/**
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001206 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1207 * @page: The page to wait for.
1208 *
1209 * The caller should hold a reference on @page. They expect the page to
1210 * become unlocked relatively soon, but do not wish to hold up migration
1211 * (for example) by holding the reference while waiting for the page to
1212 * come unlocked. After this function returns, the caller should not
1213 * dereference @page.
1214 */
1215void put_and_wait_on_page_locked(struct page *page)
1216{
1217 wait_queue_head_t *q;
1218
1219 page = compound_head(page);
1220 q = page_waitqueue(page);
1221 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP);
1222}
1223
1224/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001225 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001226 * @page: Page defining the wait queue of interest
1227 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001228 *
1229 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1230 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001231void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001232{
1233 wait_queue_head_t *q = page_waitqueue(page);
1234 unsigned long flags;
1235
1236 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001237 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001238 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001239 spin_unlock_irqrestore(&q->lock, flags);
1240}
1241EXPORT_SYMBOL_GPL(add_page_wait_queue);
1242
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001243#ifndef clear_bit_unlock_is_negative_byte
1244
1245/*
1246 * PG_waiters is the high bit in the same byte as PG_lock.
1247 *
1248 * On x86 (and on many other architectures), we can clear PG_lock and
1249 * test the sign bit at the same time. But if the architecture does
1250 * not support that special operation, we just do this all by hand
1251 * instead.
1252 *
1253 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1254 * being cleared, but a memory barrier should be unneccssary since it is
1255 * in the same byte as PG_locked.
1256 */
1257static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1258{
1259 clear_bit_unlock(nr, mem);
1260 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001261 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001262}
1263
1264#endif
1265
David Howells385e1ca5f2009-04-03 16:42:39 +01001266/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001267 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * @page: the page
1269 *
1270 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1271 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001272 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1274 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001275 * Note that this depends on PG_waiters being the sign bit in the byte
1276 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1277 * clear the PG_locked bit and test PG_waiters at the same time fairly
1278 * portably (architectures that do LL/SC can test any bit, while x86 can
1279 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001281void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001283 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001284 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001285 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001286 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1287 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289EXPORT_SYMBOL(unlock_page);
1290
Randy Dunlap485bb992006-06-23 02:03:49 -07001291/**
1292 * end_page_writeback - end writeback against a page
1293 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
1295void end_page_writeback(struct page *page)
1296{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001297 /*
1298 * TestClearPageReclaim could be used here but it is an atomic
1299 * operation and overkill in this particular case. Failing to
1300 * shuffle a page marked for immediate reclaim is too mild to
1301 * justify taking an atomic operation penalty at the end of
1302 * ever page writeback.
1303 */
1304 if (PageReclaim(page)) {
1305 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001306 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001307 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001308
1309 if (!test_clear_page_writeback(page))
1310 BUG();
1311
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001312 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 wake_up_page(page, PG_writeback);
1314}
1315EXPORT_SYMBOL(end_page_writeback);
1316
Matthew Wilcox57d99842014-06-04 16:07:45 -07001317/*
1318 * After completing I/O on a page, call this routine to update the page
1319 * flags appropriately
1320 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001321void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001322{
Jens Axboec11f0c02016-08-05 08:11:04 -06001323 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001324 if (!err) {
1325 SetPageUptodate(page);
1326 } else {
1327 ClearPageUptodate(page);
1328 SetPageError(page);
1329 }
1330 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001331 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001332 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001333 struct address_space *mapping;
1334
Matthew Wilcox57d99842014-06-04 16:07:45 -07001335 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001336 mapping = page_mapping(page);
1337 if (mapping)
1338 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001339 }
1340 end_page_writeback(page);
1341 }
1342}
1343EXPORT_SYMBOL_GPL(page_endio);
1344
Randy Dunlap485bb992006-06-23 02:03:49 -07001345/**
1346 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001347 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001349void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Nicholas Piggin62906022016-12-25 13:00:30 +10001351 struct page *page = compound_head(__page);
1352 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001353 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE,
1354 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356EXPORT_SYMBOL(__lock_page);
1357
Nicholas Piggin62906022016-12-25 13:00:30 +10001358int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001359{
Nicholas Piggin62906022016-12-25 13:00:30 +10001360 struct page *page = compound_head(__page);
1361 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001362 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE,
1363 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001364}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001365EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001366
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001367/*
1368 * Return values:
1369 * 1 - page is locked; mmap_sem is still held.
1370 * 0 - page is not locked.
1371 * mmap_sem has been released (up_read()), unless flags had both
1372 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1373 * which case mmap_sem is still held.
1374 *
1375 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1376 * with the page locked and the mmap_sem unperturbed.
1377 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001378int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1379 unsigned int flags)
1380{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001381 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1382 /*
1383 * CAUTION! In this case, mmap_sem is not released
1384 * even though return 0.
1385 */
1386 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1387 return 0;
1388
1389 up_read(&mm->mmap_sem);
1390 if (flags & FAULT_FLAG_KILLABLE)
1391 wait_on_page_locked_killable(page);
1392 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001393 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001394 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001395 } else {
1396 if (flags & FAULT_FLAG_KILLABLE) {
1397 int ret;
1398
1399 ret = __lock_page_killable(page);
1400 if (ret) {
1401 up_read(&mm->mmap_sem);
1402 return 0;
1403 }
1404 } else
1405 __lock_page(page);
1406 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001407 }
1408}
1409
Randy Dunlap485bb992006-06-23 02:03:49 -07001410/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001411 * page_cache_next_miss() - Find the next gap in the page cache.
1412 * @mapping: Mapping.
1413 * @index: Index.
1414 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001415 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001416 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1417 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001418 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001419 * This function may be called under the rcu_read_lock. However, this will
1420 * not atomically search a snapshot of the cache at a single point in time.
1421 * For example, if a gap is created at index 5, then subsequently a gap is
1422 * created at index 10, page_cache_next_miss covering both indices may
1423 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001424 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001425 * Return: The index of the gap if found, otherwise an index outside the
1426 * range specified (in which case 'return - index >= max_scan' will be true).
1427 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001428 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001429pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001430 pgoff_t index, unsigned long max_scan)
1431{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001432 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001433
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001434 while (max_scan--) {
1435 void *entry = xas_next(&xas);
1436 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001437 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001438 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001439 break;
1440 }
1441
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001442 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001443}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001444EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001445
1446/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001447 * page_cache_prev_miss() - Find the next gap in the page cache.
1448 * @mapping: Mapping.
1449 * @index: Index.
1450 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001451 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001452 * Search the range [max(index - max_scan + 1, 0), index] for the
1453 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001454 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001455 * This function may be called under the rcu_read_lock. However, this will
1456 * not atomically search a snapshot of the cache at a single point in time.
1457 * For example, if a gap is created at index 10, then subsequently a gap is
1458 * created at index 5, page_cache_prev_miss() covering both indices may
1459 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001460 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001461 * Return: The index of the gap if found, otherwise an index outside the
1462 * range specified (in which case 'index - return >= max_scan' will be true).
1463 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001464 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001465pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001466 pgoff_t index, unsigned long max_scan)
1467{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001468 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001469
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001470 while (max_scan--) {
1471 void *entry = xas_prev(&xas);
1472 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001473 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001474 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001475 break;
1476 }
1477
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001478 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001479}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001480EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001481
1482/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001483 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001484 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001485 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001486 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001487 * Looks up the page cache slot at @mapping & @offset. If there is a
1488 * page cache page, it is returned with an increased refcount.
1489 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001490 * If the slot holds a shadow entry of a previously evicted page, or a
1491 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001492 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001493 * Return: the found page or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001495struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001497 XA_STATE(xas, &mapping->i_pages, offset);
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001498 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Nick Piggina60637c2008-07-25 19:45:31 -07001500 rcu_read_lock();
1501repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001502 xas_reset(&xas);
1503 page = xas_load(&xas);
1504 if (xas_retry(&xas, page))
1505 goto repeat;
1506 /*
1507 * A shadow entry of a recently evicted page, or a swap entry from
1508 * shmem/tmpfs. Return it without attempting to raise page count.
1509 */
1510 if (!page || xa_is_value(page))
1511 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001512
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001513 if (!page_cache_get_speculative(page))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001514 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001515
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001516 /*
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001517 * Has the page moved or been split?
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001518 * This is part of the lockless pagecache protocol. See
1519 * include/linux/pagemap.h for details.
1520 */
1521 if (unlikely(page != xas_reload(&xas))) {
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001522 put_page(page);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001523 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001524 }
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001525 page = find_subpage(page, offset);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001526out:
Nick Piggina60637c2008-07-25 19:45:31 -07001527 rcu_read_unlock();
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return page;
1530}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001531EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Randy Dunlap485bb992006-06-23 02:03:49 -07001533/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001534 * find_lock_entry - locate, pin and lock a page cache entry
1535 * @mapping: the address_space to search
1536 * @offset: the page cache index
1537 *
1538 * Looks up the page cache slot at @mapping & @offset. If there is a
1539 * page cache page, it is returned locked and with an increased
1540 * refcount.
1541 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001542 * If the slot holds a shadow entry of a previously evicted page, or a
1543 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001544 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001545 * find_lock_entry() may sleep.
Mike Rapoporta862f682019-03-05 15:48:42 -08001546 *
1547 * Return: the found page or shadow entry, %NULL if nothing is found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001548 */
1549struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct page *page;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001554 page = find_get_entry(mapping, offset);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001555 if (page && !xa_is_value(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001556 lock_page(page);
1557 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001558 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001559 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001561 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001563 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return page;
1566}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001567EXPORT_SYMBOL(find_lock_entry);
1568
1569/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001570 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001571 * @mapping: the address_space to search
1572 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001573 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001574 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001575 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001576 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001577 *
Randy Dunlap75325182014-07-30 16:08:37 -07001578 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001579 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001580 * @fgp_flags can be:
1581 *
1582 * - FGP_ACCESSED: the page will be marked accessed
1583 * - FGP_LOCK: Page is return locked
1584 * - FGP_CREAT: If page is not present then a new page is allocated using
1585 * @gfp_mask and added to the page cache and the VM's LRU
1586 * list. The page is returned locked and with an increased
Mike Rapoporta862f682019-03-05 15:48:42 -08001587 * refcount.
Josef Bacika75d4c32019-03-13 11:44:14 -07001588 * - FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1589 * its own locking dance if the page is already in cache, or unlock the page
1590 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001591 *
1592 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1593 * if the GFP flags specified for FGP_CREAT are atomic.
1594 *
1595 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001596 *
1597 * Return: the found page or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001598 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001599struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001600 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Nick Piggineb2be182007-10-16 01:24:57 -07001602 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001605 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001606 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001607 page = NULL;
1608 if (!page)
1609 goto no_page;
1610
1611 if (fgp_flags & FGP_LOCK) {
1612 if (fgp_flags & FGP_NOWAIT) {
1613 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001614 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001615 return NULL;
1616 }
1617 } else {
1618 lock_page(page);
1619 }
1620
1621 /* Has the page been truncated? */
1622 if (unlikely(page->mapping != mapping)) {
1623 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001624 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001625 goto repeat;
1626 }
1627 VM_BUG_ON_PAGE(page->index != offset, page);
1628 }
1629
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001630 if (fgp_flags & FGP_ACCESSED)
Mel Gorman2457aec2014-06-04 16:10:31 -07001631 mark_page_accessed(page);
1632
1633no_page:
1634 if (!page && (fgp_flags & FGP_CREAT)) {
1635 int err;
1636 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001637 gfp_mask |= __GFP_WRITE;
1638 if (fgp_flags & FGP_NOFS)
1639 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001640
Michal Hocko45f87de2014-12-29 20:30:35 +01001641 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001642 if (!page)
1643 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001644
Josef Bacika75d4c32019-03-13 11:44:14 -07001645 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001646 fgp_flags |= FGP_LOCK;
1647
Hugh Dickinseb39d612014-08-06 16:06:43 -07001648 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001649 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001650 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001651
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001652 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001653 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001654 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001655 page = NULL;
1656 if (err == -EEXIST)
1657 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001659
1660 /*
1661 * add_to_page_cache_lru locks the page, and for mmap we expect
1662 * an unlocked page.
1663 */
1664 if (page && (fgp_flags & FGP_FOR_MMAP))
1665 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return page;
1669}
Mel Gorman2457aec2014-06-04 16:10:31 -07001670EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001673 * find_get_entries - gang pagecache lookup
1674 * @mapping: The address_space to search
1675 * @start: The starting page cache index
1676 * @nr_entries: The maximum number of entries
1677 * @entries: Where the resulting entries are placed
1678 * @indices: The cache indices corresponding to the entries in @entries
1679 *
1680 * find_get_entries() will search for and return a group of up to
1681 * @nr_entries entries in the mapping. The entries are placed at
1682 * @entries. find_get_entries() takes a reference against any actual
1683 * pages it returns.
1684 *
1685 * The search returns a group of mapping-contiguous page cache entries
1686 * with ascending indexes. There may be holes in the indices due to
1687 * not-present pages.
1688 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001689 * Any shadow entries of evicted pages, or swap entries from
1690 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001691 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001692 * Return: the number of pages and shadow entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001693 */
1694unsigned find_get_entries(struct address_space *mapping,
1695 pgoff_t start, unsigned int nr_entries,
1696 struct page **entries, pgoff_t *indices)
1697{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001698 XA_STATE(xas, &mapping->i_pages, start);
1699 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001700 unsigned int ret = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001701
1702 if (!nr_entries)
1703 return 0;
1704
1705 rcu_read_lock();
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001706 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001707 if (xas_retry(&xas, page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001708 continue;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001709 /*
1710 * A shadow entry of a recently evicted page, a swap
1711 * entry from shmem/tmpfs or a DAX entry. Return it
1712 * without attempting to raise page count.
1713 */
1714 if (xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001715 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001716
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001717 if (!page_cache_get_speculative(page))
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001718 goto retry;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001719
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001720 /* Has the page moved or been split? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001721 if (unlikely(page != xas_reload(&xas)))
1722 goto put_page;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001723 page = find_subpage(page, xas.xa_index);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001724
Johannes Weiner0cd61442014-04-03 14:47:46 -07001725export:
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001726 indices[ret] = xas.xa_index;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001727 entries[ret] = page;
1728 if (++ret == nr_entries)
1729 break;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001730 continue;
1731put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001732 put_page(page);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001733retry:
1734 xas_reset(&xas);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001735 }
1736 rcu_read_unlock();
1737 return ret;
1738}
1739
1740/**
Jan Karab947cee2017-09-06 16:21:21 -07001741 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 * @mapping: The address_space to search
1743 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001744 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * @nr_pages: The maximum number of pages
1746 * @pages: Where the resulting pages are placed
1747 *
Jan Karab947cee2017-09-06 16:21:21 -07001748 * find_get_pages_range() will search for and return a group of up to @nr_pages
1749 * pages in the mapping starting at index @start and up to index @end
1750 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1751 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
1753 * The search returns a group of mapping-contiguous pages with ascending
1754 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001755 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001757 * Return: the number of pages which were found. If this number is
1758 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07001759 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Jan Karab947cee2017-09-06 16:21:21 -07001761unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1762 pgoff_t end, unsigned int nr_pages,
1763 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001765 XA_STATE(xas, &mapping->i_pages, *start);
1766 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001767 unsigned ret = 0;
1768
1769 if (unlikely(!nr_pages))
1770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Nick Piggina60637c2008-07-25 19:45:31 -07001772 rcu_read_lock();
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001773 xas_for_each(&xas, page, end) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001774 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001775 continue;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001776 /* Skip over shadow, swap and DAX entries */
1777 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001778 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001779
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001780 if (!page_cache_get_speculative(page))
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001781 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001782
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001783 /* Has the page moved or been split? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001784 if (unlikely(page != xas_reload(&xas)))
1785 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001786
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001787 pages[ret] = find_subpage(page, xas.xa_index);
Jan Karab947cee2017-09-06 16:21:21 -07001788 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001789 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07001790 goto out;
1791 }
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001792 continue;
1793put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001794 put_page(page);
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001795retry:
1796 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001797 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001798
Jan Karab947cee2017-09-06 16:21:21 -07001799 /*
1800 * We come here when there is no page beyond @end. We take care to not
1801 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001802 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07001803 * already broken anyway.
1804 */
1805 if (end == (pgoff_t)-1)
1806 *start = (pgoff_t)-1;
1807 else
1808 *start = end + 1;
1809out:
Nick Piggina60637c2008-07-25 19:45:31 -07001810 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return ret;
1813}
1814
Jens Axboeebf43502006-04-27 08:46:01 +02001815/**
1816 * find_get_pages_contig - gang contiguous pagecache lookup
1817 * @mapping: The address_space to search
1818 * @index: The starting page index
1819 * @nr_pages: The maximum number of pages
1820 * @pages: Where the resulting pages are placed
1821 *
1822 * find_get_pages_contig() works exactly like find_get_pages(), except
1823 * that the returned number of pages are guaranteed to be contiguous.
1824 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001825 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02001826 */
1827unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1828 unsigned int nr_pages, struct page **pages)
1829{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001830 XA_STATE(xas, &mapping->i_pages, index);
1831 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001832 unsigned int ret = 0;
1833
1834 if (unlikely(!nr_pages))
1835 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001836
Nick Piggina60637c2008-07-25 19:45:31 -07001837 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001838 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001839 if (xas_retry(&xas, page))
1840 continue;
1841 /*
1842 * If the entry has been swapped out, we can stop looking.
1843 * No current caller is looking for DAX entries.
1844 */
1845 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001846 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001847
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001848 if (!page_cache_get_speculative(page))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001849 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001850
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001851 /* Has the page moved or been split? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001852 if (unlikely(page != xas_reload(&xas)))
1853 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001854
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001855 pages[ret] = find_subpage(page, xas.xa_index);
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001856 if (++ret == nr_pages)
1857 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001858 continue;
1859put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001860 put_page(page);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001861retry:
1862 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02001863 }
Nick Piggina60637c2008-07-25 19:45:31 -07001864 rcu_read_unlock();
1865 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001866}
David Howellsef71c152007-05-09 02:33:44 -07001867EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001868
Randy Dunlap485bb992006-06-23 02:03:49 -07001869/**
Jan Kara72b045a2017-11-15 17:34:33 -08001870 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001871 * @mapping: the address_space to search
1872 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001873 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001874 * @tag: the tag index
1875 * @nr_pages: the maximum number of pages
1876 * @pages: where the resulting pages are placed
1877 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001879 * @tag. We update @index to index the next page for the traversal.
Mike Rapoporta862f682019-03-05 15:48:42 -08001880 *
1881 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
Jan Kara72b045a2017-11-15 17:34:33 -08001883unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001884 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08001885 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001887 XA_STATE(xas, &mapping->i_pages, *index);
1888 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001889 unsigned ret = 0;
1890
1891 if (unlikely(!nr_pages))
1892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Nick Piggina60637c2008-07-25 19:45:31 -07001894 rcu_read_lock();
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001895 xas_for_each_marked(&xas, page, end, tag) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001896 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001897 continue;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001898 /*
1899 * Shadow entries should never be tagged, but this iteration
1900 * is lockless so there is a window for page reclaim to evict
1901 * a page we saw tagged. Skip over it.
1902 */
1903 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07001904 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001905
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001906 if (!page_cache_get_speculative(page))
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001907 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001908
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001909 /* Has the page moved or been split? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001910 if (unlikely(page != xas_reload(&xas)))
1911 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001912
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001913 pages[ret] = find_subpage(page, xas.xa_index);
Jan Kara72b045a2017-11-15 17:34:33 -08001914 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001915 *index = xas.xa_index + 1;
Jan Kara72b045a2017-11-15 17:34:33 -08001916 goto out;
1917 }
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001918 continue;
1919put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001920 put_page(page);
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001921retry:
1922 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001923 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001924
Jan Kara72b045a2017-11-15 17:34:33 -08001925 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001926 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08001927 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001928 * iteration when there is a page at index -1 but that is already
1929 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08001930 */
1931 if (end == (pgoff_t)-1)
1932 *index = (pgoff_t)-1;
1933 else
1934 *index = end + 1;
1935out:
Nick Piggina60637c2008-07-25 19:45:31 -07001936 rcu_read_unlock();
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return ret;
1939}
Jan Kara72b045a2017-11-15 17:34:33 -08001940EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001942/**
1943 * find_get_entries_tag - find and return entries that match @tag
1944 * @mapping: the address_space to search
1945 * @start: the starting page cache index
1946 * @tag: the tag index
1947 * @nr_entries: the maximum number of entries
1948 * @entries: where the resulting entries are placed
1949 * @indices: the cache indices corresponding to the entries in @entries
1950 *
1951 * Like find_get_entries, except we only return entries which are tagged with
1952 * @tag.
Mike Rapoporta862f682019-03-05 15:48:42 -08001953 *
1954 * Return: the number of entries which were found.
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001955 */
1956unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001957 xa_mark_t tag, unsigned int nr_entries,
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001958 struct page **entries, pgoff_t *indices)
1959{
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001960 XA_STATE(xas, &mapping->i_pages, start);
1961 struct page *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001962 unsigned int ret = 0;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001963
1964 if (!nr_entries)
1965 return 0;
1966
1967 rcu_read_lock();
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001968 xas_for_each_marked(&xas, page, ULONG_MAX, tag) {
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001969 if (xas_retry(&xas, page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001970 continue;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001971 /*
1972 * A shadow entry of a recently evicted page, a swap
1973 * entry from shmem/tmpfs or a DAX entry. Return it
1974 * without attempting to raise page count.
1975 */
1976 if (xa_is_value(page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001977 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001978
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001979 if (!page_cache_get_speculative(page))
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001980 goto retry;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001981
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001982 /* Has the page moved or been split? */
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001983 if (unlikely(page != xas_reload(&xas)))
1984 goto put_page;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001985 page = find_subpage(page, xas.xa_index);
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001986
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001987export:
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001988 indices[ret] = xas.xa_index;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001989 entries[ret] = page;
1990 if (++ret == nr_entries)
1991 break;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001992 continue;
1993put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001994 put_page(page);
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001995retry:
1996 xas_reset(&xas);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001997 }
1998 rcu_read_unlock();
1999 return ret;
2000}
2001EXPORT_SYMBOL(find_get_entries_tag);
2002
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002003/*
2004 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2005 * a _large_ part of the i/o request. Imagine the worst scenario:
2006 *
2007 * ---R__________________________________________B__________
2008 * ^ reading here ^ bad block(assume 4k)
2009 *
2010 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2011 * => failing the whole request => read(R) => read(R+1) =>
2012 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2013 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2014 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2015 *
2016 * It is going insane. Fix it by quickly scaling down the readahead size.
2017 */
2018static void shrink_readahead_size_eio(struct file *filp,
2019 struct file_ra_state *ra)
2020{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002021 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002022}
2023
Randy Dunlap485bb992006-06-23 02:03:49 -07002024/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002025 * generic_file_buffered_read - generic file read routine
2026 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002027 * @iter: data destination
2028 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002029 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002031 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 *
2033 * This is really ugly. But the goto's actually try to clarify some
2034 * of the logic when it comes to error handling etc.
Mike Rapoporta862f682019-03-05 15:48:42 -08002035 *
2036 * Return:
2037 * * total number of bytes copied, including those the were already @written
2038 * * negative error code if nothing was copied
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002040static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002041 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002043 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002044 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002046 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002047 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002048 pgoff_t index;
2049 pgoff_t last_index;
2050 pgoff_t prev_index;
2051 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002052 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002053 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Wei Fangc2a97372016-10-07 17:01:52 -07002055 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002056 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002057 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002059 index = *ppos >> PAGE_SHIFT;
2060 prev_index = ra->prev_pos >> PAGE_SHIFT;
2061 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2062 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2063 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 for (;;) {
2066 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002067 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002068 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 unsigned long nr, ret;
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002073 if (fatal_signal_pending(current)) {
2074 error = -EINTR;
2075 goto out;
2076 }
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002079 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002080 if (iocb->ki_flags & IOCB_NOWAIT)
2081 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002082 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002083 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002084 index, last_index - index);
2085 page = find_get_page(mapping, index);
2086 if (unlikely(page == NULL))
2087 goto no_cached_page;
2088 }
2089 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002090 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002091 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002092 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002094 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002095 if (iocb->ki_flags & IOCB_NOWAIT) {
2096 put_page(page);
2097 goto would_block;
2098 }
2099
Mel Gormanebded022016-03-15 14:55:39 -07002100 /*
2101 * See comment in do_read_cache_page on why
2102 * wait_on_page_locked is used to avoid unnecessarily
2103 * serialisations and why it's safe.
2104 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002105 error = wait_on_page_locked_killable(page);
2106 if (unlikely(error))
2107 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002108 if (PageUptodate(page))
2109 goto page_ok;
2110
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002112 !mapping->a_ops->is_partially_uptodate)
2113 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002114 /* pipes can't handle partially uptodate pages */
David Howells00e23702018-10-22 13:07:28 +01002115 if (unlikely(iov_iter_is_pipe(iter)))
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002116 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002117 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002118 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002119 /* Did it get truncated before we got the lock? */
2120 if (!page->mapping)
2121 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002122 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002123 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002124 goto page_not_up_to_date_locked;
2125 unlock_page(page);
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002128 /*
2129 * i_size must be checked after we know the page is Uptodate.
2130 *
2131 * Checking i_size after the check allows us to calculate
2132 * the correct value for "nr", which means the zero-filled
2133 * part of the page is not copied back to userspace (unless
2134 * another truncate extends the file - this is desired though).
2135 */
2136
2137 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002138 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002139 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002141 goto out;
2142 }
2143
2144 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002146 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002148 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002149 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002150 goto out;
2151 }
2152 }
2153 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /* If users can be writing to this page using arbitrary
2156 * virtual addresses, take care about potential aliasing
2157 * before reading the page on the kernel side.
2158 */
2159 if (mapping_writably_mapped(mapping))
2160 flush_dcache_page(page);
2161
2162 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002163 * When a sequential read accesses a page several times,
2164 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 */
Jan Karaec0f1632007-05-06 14:49:25 -07002166 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 mark_page_accessed(page);
2168 prev_index = index;
2169
2170 /*
2171 * Ok, we have the page, and it's up-to-date, so
2172 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Al Viro6e58e792014-02-03 17:07:03 -05002174
2175 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002177 index += offset >> PAGE_SHIFT;
2178 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002179 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002181 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002182 written += ret;
2183 if (!iov_iter_count(iter))
2184 goto out;
2185 if (ret < nr) {
2186 error = -EFAULT;
2187 goto out;
2188 }
2189 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191page_not_up_to_date:
2192 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002193 error = lock_page_killable(page);
2194 if (unlikely(error))
2195 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002197page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002198 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!page->mapping) {
2200 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002201 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 continue;
2203 }
2204
2205 /* Did somebody else fill it already? */
2206 if (PageUptodate(page)) {
2207 unlock_page(page);
2208 goto page_ok;
2209 }
2210
2211readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002212 /*
2213 * A previous I/O error may have been due to temporary
2214 * failures, eg. multipath errors.
2215 * PG_error will be set again if readpage fails.
2216 */
2217 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* Start the actual read. The read will unlock the page. */
2219 error = mapping->a_ops->readpage(filp, page);
2220
Zach Brown994fc28c2005-12-15 14:28:17 -08002221 if (unlikely(error)) {
2222 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002223 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002224 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002225 goto find_page;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002231 error = lock_page_killable(page);
2232 if (unlikely(error))
2233 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (!PageUptodate(page)) {
2235 if (page->mapping == NULL) {
2236 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002237 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 */
2239 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002240 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto find_page;
2242 }
2243 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002244 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002245 error = -EIO;
2246 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 unlock_page(page);
2249 }
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto page_ok;
2252
2253readpage_error:
2254 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002255 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto out;
2257
2258no_cached_page:
2259 /*
2260 * Ok, it wasn't cached, so we need to create a new
2261 * page..
2262 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002263 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002264 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002265 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002268 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002269 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002271 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002272 if (error == -EEXIST) {
2273 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto out;
2277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto readpage;
2279 }
2280
Milosz Tanski3239d832017-08-29 16:13:19 +02002281would_block:
2282 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002284 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002285 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002286 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002288 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002289 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002290 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Randy Dunlap485bb992006-06-23 02:03:49 -07002293/**
Al Viro6abd2322014-04-04 14:20:57 -04002294 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002295 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002296 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002297 *
Al Viro6abd2322014-04-04 14:20:57 -04002298 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * that can use the page cache directly.
Mike Rapoporta862f682019-03-05 15:48:42 -08002300 * Return:
2301 * * number of bytes copied, even for partial reads
2302 * * negative error code if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 */
2304ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002305generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002307 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002308 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002309
2310 if (!count)
2311 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Al Viro2ba48ce2015-04-09 13:52:01 -04002313 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002314 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002315 struct address_space *mapping = file->f_mapping;
2316 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002317 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002320 if (iocb->ki_flags & IOCB_NOWAIT) {
2321 if (filemap_range_has_page(mapping, iocb->ki_pos,
2322 iocb->ki_pos + count - 1))
2323 return -EAGAIN;
2324 } else {
2325 retval = filemap_write_and_wait_range(mapping,
2326 iocb->ki_pos,
2327 iocb->ki_pos + count - 1);
2328 if (retval < 0)
2329 goto out;
2330 }
Al Viroed978a82014-03-05 22:53:04 -05002331
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002332 file_accessed(file);
2333
Al Viro5ecda132017-04-13 14:13:36 -04002334 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002335 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002336 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002337 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002338 }
Al Viro5b47d592017-05-08 13:54:47 -04002339 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002340
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002341 /*
2342 * Btrfs can have a short DIO read if we encounter
2343 * compressed extents, so if there was an error, or if
2344 * we've already read everything we wanted to, or if
2345 * there was a short read because we hit EOF, go ahead
2346 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002347 * the rest of the read. Buffered reads will not work for
2348 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002349 */
Al Viro5ecda132017-04-13 14:13:36 -04002350 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002351 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002355 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356out:
2357 return retval;
2358}
Al Viroed978a82014-03-05 22:53:04 -05002359EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362#define MMAP_LOTSAMISS (100)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002363static struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
2364 struct file *fpin)
2365{
2366 int flags = vmf->flags;
2367
2368 if (fpin)
2369 return fpin;
2370
2371 /*
2372 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2373 * anything, so we only pin the file and drop the mmap_sem if only
2374 * FAULT_FLAG_ALLOW_RETRY is set.
2375 */
2376 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2377 FAULT_FLAG_ALLOW_RETRY) {
2378 fpin = get_file(vmf->vma->vm_file);
2379 up_read(&vmf->vma->vm_mm->mmap_sem);
2380 }
2381 return fpin;
2382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Linus Torvaldsef00e082009-06-16 15:31:25 -07002384/*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002385 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2386 * @vmf - the vm_fault for this fault.
2387 * @page - the page to lock.
2388 * @fpin - the pointer to the file we may pin (or is already pinned).
2389 *
2390 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2391 * It differs in that it actually returns the page locked if it returns 1 and 0
2392 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2393 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002394 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002395static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2396 struct file **fpin)
2397{
2398 if (trylock_page(page))
2399 return 1;
2400
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002401 /*
2402 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2403 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2404 * is supposed to work. We have way too many special cases..
2405 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002406 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2407 return 0;
2408
2409 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2410 if (vmf->flags & FAULT_FLAG_KILLABLE) {
2411 if (__lock_page_killable(page)) {
2412 /*
2413 * We didn't have the right flags to drop the mmap_sem,
2414 * but all fault_handlers only check for fatal signals
2415 * if we return VM_FAULT_RETRY, so we need to drop the
2416 * mmap_sem here and return 0 if we don't have a fpin.
2417 */
2418 if (*fpin == NULL)
2419 up_read(&vmf->vma->vm_mm->mmap_sem);
2420 return 0;
2421 }
2422 } else
2423 __lock_page(page);
2424 return 1;
2425}
2426
2427
2428/*
2429 * Synchronous readahead happens when we don't even find a page in the page
2430 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2431 * to drop the mmap sem we return the file that was pinned in order for us to do
2432 * that. If we didn't pin a file then we return NULL. The file that is
2433 * returned needs to be fput()'ed when we're done with it.
2434 */
2435static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002436{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002437 struct file *file = vmf->vma->vm_file;
2438 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002439 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002440 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002441 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002442
2443 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002444 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002445 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002446 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002447 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002448
Josef Bacik2a1180f2019-03-13 11:44:18 -07002449 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002450 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002451 page_cache_sync_readahead(mapping, ra, file, offset,
2452 ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002453 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002454 }
2455
Andi Kleen207d04b2011-05-24 17:12:29 -07002456 /* Avoid banging the cache line if not needed */
2457 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002458 ra->mmap_miss++;
2459
2460 /*
2461 * Do we miss much more than hit in this file? If so,
2462 * stop bothering with read-ahead. It will only hurt.
2463 */
2464 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002465 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002466
Wu Fengguangd30a1102009-06-16 15:31:30 -07002467 /*
2468 * mmap read-around
2469 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002470 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002471 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2472 ra->size = ra->ra_pages;
2473 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002474 ra_submit(ra, mapping, file);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002475 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002476}
2477
2478/*
2479 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002480 * so we want to possibly extend the readahead further. We return the file that
2481 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002482 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002483static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
2484 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002485{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002486 struct file *file = vmf->vma->vm_file;
2487 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002488 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002489 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002490 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002491
2492 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002493 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002494 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002495 if (ra->mmap_miss > 0)
2496 ra->mmap_miss--;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002497 if (PageReadahead(page)) {
2498 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002499 page_cache_async_readahead(mapping, ra, file,
2500 page, offset, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002501 }
2502 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002503}
2504
Randy Dunlap485bb992006-06-23 02:03:49 -07002505/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002506 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002507 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002508 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002509 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * mapped memory region to read in file data during a page fault.
2511 *
2512 * The goto's are kind of ugly, but this streamlines the normal case of having
2513 * it in the page cache, and handles the special cases reasonably without
2514 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002515 *
2516 * vma->vm_mm->mmap_sem must be held on entry.
2517 *
2518 * If our return value has VM_FAULT_RETRY set, it's because
2519 * lock_page_or_retry() returned 0.
2520 * The mmap_sem has usually been released in this case.
2521 * See __lock_page_or_retry() for the exception.
2522 *
2523 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2524 * has not been released.
2525 *
2526 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08002527 *
2528 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002530vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002533 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002534 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 struct address_space *mapping = file->f_mapping;
2536 struct file_ra_state *ra = &file->f_ra;
2537 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002538 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002539 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002541 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002543 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2544 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002545 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002548 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002550 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002551 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002553 * We found the page, so try async readahead before
2554 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002556 fpin = do_async_mmap_readahead(vmf, page);
Shaohua Li45cac652012-10-08 16:32:19 -07002557 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002558 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002559 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002560 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002561 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002562 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002563retry_find:
Josef Bacika75d4c32019-03-13 11:44:14 -07002564 page = pagecache_get_page(mapping, offset,
2565 FGP_CREAT|FGP_FOR_MMAP,
2566 vmf->gfp_mask);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002567 if (!page) {
2568 if (fpin)
2569 goto out_retry;
Josef Bacika75d4c32019-03-13 11:44:14 -07002570 return vmf_error(-ENOMEM);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002574 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
2575 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002576
2577 /* Did it get truncated? */
2578 if (unlikely(page->mapping != mapping)) {
2579 unlock_page(page);
2580 put_page(page);
2581 goto retry_find;
2582 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002583 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002586 * We have a locked page in the page cache, now we need to check
2587 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 */
Nick Piggind00806b2007-07-19 01:46:57 -07002589 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 goto page_not_uptodate;
2591
Linus Torvaldsef00e082009-06-16 15:31:25 -07002592 /*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002593 * We've made it this far and we had to drop our mmap_sem, now is the
2594 * time to return to the upper layer and have it re-find the vma and
2595 * redo the fault.
2596 */
2597 if (fpin) {
2598 unlock_page(page);
2599 goto out_retry;
2600 }
2601
2602 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002603 * Found the page and have a reference on it.
2604 * We must recheck i_size under page lock.
2605 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002606 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2607 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002608 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002609 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002610 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002611 }
2612
Nick Piggind0217ac2007-07-19 01:47:03 -07002613 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002614 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /*
2618 * Umm, take care of errors if the page isn't up-to-date.
2619 * Try to re-read it _once_. We do this synchronously,
2620 * because there really aren't any performance issues here
2621 * and we need to check for errors.
2622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 ClearPageError(page);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002624 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002625 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002626 if (!error) {
2627 wait_on_page_locked(page);
2628 if (!PageUptodate(page))
2629 error = -EIO;
2630 }
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002631 if (fpin)
2632 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002633 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002634
2635 if (!error || error == AOP_TRUNCATED_PAGE)
2636 goto retry_find;
2637
2638 /* Things didn't work out. Return zero to tell the mm layer so. */
2639 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002640 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002641
2642out_retry:
2643 /*
2644 * We dropped the mmap_sem, we need to return to the fault handler to
2645 * re-find the vma and come back and find our hopefully still populated
2646 * page.
2647 */
2648 if (page)
2649 put_page(page);
2650 if (fpin)
2651 fput(fpin);
2652 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002653}
2654EXPORT_SYMBOL(filemap_fault);
2655
Jan Kara82b0f8c2016-12-14 15:06:58 -08002656void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002657 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002658{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002659 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002660 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002661 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002662 unsigned long max_idx;
Matthew Wilcox070e8072018-05-17 00:08:30 -04002663 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002664 struct page *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002665
2666 rcu_read_lock();
Matthew Wilcox070e8072018-05-17 00:08:30 -04002667 xas_for_each(&xas, page, end_pgoff) {
2668 if (xas_retry(&xas, page))
2669 continue;
2670 if (xa_is_value(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002671 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002672
Michal Hockoe0975b22018-12-28 00:38:36 -08002673 /*
2674 * Check for a locked page first, as a speculative
2675 * reference may adversely influence page migration.
2676 */
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002677 if (PageLocked(page))
Michal Hockoe0975b22018-12-28 00:38:36 -08002678 goto next;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002679 if (!page_cache_get_speculative(page))
Matthew Wilcox070e8072018-05-17 00:08:30 -04002680 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002681
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002682 /* Has the page moved or been split? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002683 if (unlikely(page != xas_reload(&xas)))
2684 goto skip;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002685 page = find_subpage(page, xas.xa_index);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002686
2687 if (!PageUptodate(page) ||
2688 PageReadahead(page) ||
2689 PageHWPoison(page))
2690 goto skip;
2691 if (!trylock_page(page))
2692 goto skip;
2693
2694 if (page->mapping != mapping || !PageUptodate(page))
2695 goto unlock;
2696
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002697 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2698 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002699 goto unlock;
2700
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002701 if (file->f_ra.mmap_miss > 0)
2702 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002703
Matthew Wilcox070e8072018-05-17 00:08:30 -04002704 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002705 if (vmf->pte)
Matthew Wilcox070e8072018-05-17 00:08:30 -04002706 vmf->pte += xas.xa_index - last_pgoff;
2707 last_pgoff = xas.xa_index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002708 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002709 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002710 unlock_page(page);
2711 goto next;
2712unlock:
2713 unlock_page(page);
2714skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002715 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002716next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002717 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002718 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002719 break;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002720 }
2721 rcu_read_unlock();
2722}
2723EXPORT_SYMBOL(filemap_map_pages);
2724
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002725vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002726{
2727 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002728 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002729 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002730
Jan Kara14da9202012-06-12 16:20:37 +02002731 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002732 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002733 lock_page(page);
2734 if (page->mapping != inode->i_mapping) {
2735 unlock_page(page);
2736 ret = VM_FAULT_NOPAGE;
2737 goto out;
2738 }
Jan Kara14da9202012-06-12 16:20:37 +02002739 /*
2740 * We mark the page dirty already here so that when freeze is in
2741 * progress, we are guaranteed that writeback during freezing will
2742 * see the dirty page and writeprotect it again.
2743 */
2744 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002745 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002746out:
Jan Kara14da9202012-06-12 16:20:37 +02002747 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002748 return ret;
2749}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002750
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002751const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002752 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002753 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002754 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755};
2756
2757/* This is used for a general mmap of a disk file */
2758
2759int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2760{
2761 struct address_space *mapping = file->f_mapping;
2762
2763 if (!mapping->a_ops->readpage)
2764 return -ENOEXEC;
2765 file_accessed(file);
2766 vma->vm_ops = &generic_file_vm_ops;
2767 return 0;
2768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770/*
2771 * This is for filesystems which do not implement ->writepage.
2772 */
2773int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2774{
2775 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2776 return -EINVAL;
2777 return generic_file_mmap(file, vma);
2778}
2779#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002780vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07002781{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002782 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07002783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2785{
2786 return -ENOSYS;
2787}
2788int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2789{
2790 return -ENOSYS;
2791}
2792#endif /* CONFIG_MMU */
2793
Arnd Bergmann453972282018-04-13 15:35:27 -07002794EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795EXPORT_SYMBOL(generic_file_mmap);
2796EXPORT_SYMBOL(generic_file_readonly_mmap);
2797
Sasha Levin67f9fd92014-04-03 14:48:18 -07002798static struct page *wait_on_page_read(struct page *page)
2799{
2800 if (!IS_ERR(page)) {
2801 wait_on_page_locked(page);
2802 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002803 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002804 page = ERR_PTR(-EIO);
2805 }
2806 }
2807 return page;
2808}
2809
Mel Gorman32b63522016-03-15 14:55:36 -07002810static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002811 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002812 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002813 void *data,
2814 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Nick Piggineb2be182007-10-16 01:24:57 -07002816 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 int err;
2818repeat:
2819 page = find_get_page(mapping, index);
2820 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002821 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002822 if (!page)
2823 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002824 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002825 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002826 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002827 if (err == -EEXIST)
2828 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05002829 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return ERR_PTR(err);
2831 }
Mel Gorman32b63522016-03-15 14:55:36 -07002832
2833filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 err = filler(data, page);
2835 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002836 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002837 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
Mel Gorman32b63522016-03-15 14:55:36 -07002839
2840 page = wait_on_page_read(page);
2841 if (IS_ERR(page))
2842 return page;
2843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (PageUptodate(page))
2846 goto out;
2847
Mel Gormanebded022016-03-15 14:55:39 -07002848 /*
2849 * Page is not up to date and may be locked due one of the following
2850 * case a: Page is being filled and the page lock is held
2851 * case b: Read/write error clearing the page uptodate status
2852 * case c: Truncation in progress (page locked)
2853 * case d: Reclaim in progress
2854 *
2855 * Case a, the page will be up to date when the page is unlocked.
2856 * There is no need to serialise on the page lock here as the page
2857 * is pinned so the lock gives no additional protection. Even if the
2858 * the page is truncated, the data is still valid if PageUptodate as
2859 * it's a race vs truncate race.
2860 * Case b, the page will not be up to date
2861 * Case c, the page may be truncated but in itself, the data may still
2862 * be valid after IO completes as it's a read vs truncate race. The
2863 * operation must restart if the page is not uptodate on unlock but
2864 * otherwise serialising on page lock to stabilise the mapping gives
2865 * no additional guarantees to the caller as the page lock is
2866 * released before return.
2867 * Case d, similar to truncation. If reclaim holds the page lock, it
2868 * will be a race with remove_mapping that determines if the mapping
2869 * is valid on unlock but otherwise the data is valid and there is
2870 * no need to serialise with page lock.
2871 *
2872 * As the page lock gives no additional guarantee, we optimistically
2873 * wait on the page to be unlocked and check if it's up to date and
2874 * use the page if it is. Otherwise, the page lock is required to
2875 * distinguish between the different cases. The motivation is that we
2876 * avoid spurious serialisations and wakeups when multiple processes
2877 * wait on the same page for IO to complete.
2878 */
2879 wait_on_page_locked(page);
2880 if (PageUptodate(page))
2881 goto out;
2882
2883 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002885
2886 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!page->mapping) {
2888 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002889 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002890 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Mel Gormanebded022016-03-15 14:55:39 -07002892
2893 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (PageUptodate(page)) {
2895 unlock_page(page);
2896 goto out;
2897 }
Mel Gorman32b63522016-03-15 14:55:36 -07002898 goto filler;
2899
David Howellsc855ff32007-05-09 13:42:20 +01002900out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002901 mark_page_accessed(page);
2902 return page;
2903}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002904
2905/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002906 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002907 * @mapping: the page's address_space
2908 * @index: the page index
2909 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002910 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002911 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002912 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002913 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002914 *
2915 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002916 *
2917 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002918 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002919struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002920 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002921 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002922 void *data)
2923{
2924 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2925}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002926EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002927
2928/**
2929 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2930 * @mapping: the page's address_space
2931 * @index: the page index
2932 * @gfp: the page allocator flags to use if allocating
2933 *
2934 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002935 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002936 *
2937 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002938 *
2939 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002940 */
2941struct page *read_cache_page_gfp(struct address_space *mapping,
2942 pgoff_t index,
2943 gfp_t gfp)
2944{
2945 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2946
Sasha Levin67f9fd92014-04-03 14:48:18 -07002947 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002948}
2949EXPORT_SYMBOL(read_cache_page_gfp);
2950
Nick Piggin2f718ff2007-10-16 01:24:59 -07002951/*
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002952 * Don't operate on ranges the page cache doesn't support, and don't exceed the
2953 * LFS limits. If pos is under the limit it becomes a short access. If it
2954 * exceeds the limit we return -EFBIG.
2955 */
2956static int generic_access_check_limits(struct file *file, loff_t pos,
2957 loff_t *count)
2958{
2959 struct inode *inode = file->f_mapping->host;
2960 loff_t max_size = inode->i_sb->s_maxbytes;
2961
2962 if (!(file->f_flags & O_LARGEFILE))
2963 max_size = MAX_NON_LFS;
2964
2965 if (unlikely(pos >= max_size))
2966 return -EFBIG;
2967 *count = min(*count, max_size - pos);
2968 return 0;
2969}
2970
2971static int generic_write_check_limits(struct file *file, loff_t pos,
2972 loff_t *count)
2973{
2974 loff_t limit = rlimit(RLIMIT_FSIZE);
2975
2976 if (limit != RLIM_INFINITY) {
2977 if (pos >= limit) {
2978 send_sig(SIGXFSZ, current, 0);
2979 return -EFBIG;
2980 }
2981 *count = min(*count, limit - pos);
2982 }
2983
2984 return generic_access_check_limits(file, pos, count);
2985}
2986
2987/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * Performs necessary checks before doing a write
2989 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002990 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 * Returns appropriate error code that caller should return or
2992 * zero in case that write should be allowed.
2993 */
Al Viro3309dd02015-04-09 12:55:47 -04002994inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Al Viro3309dd02015-04-09 12:55:47 -04002996 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 struct inode *inode = file->f_mapping->host;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002998 loff_t count;
2999 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Al Viro3309dd02015-04-09 12:55:47 -04003001 if (!iov_iter_count(from))
3002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Al Viro0fa6b002015-04-04 04:05:48 -04003004 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04003005 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04003006 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003008 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
3009 return -EINVAL;
3010
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003011 count = iov_iter_count(from);
3012 ret = generic_write_check_limits(file, iocb->ki_pos, &count);
3013 if (ret)
3014 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003016 iov_iter_truncate(from, count);
Al Viro3309dd02015-04-09 12:55:47 -04003017 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019EXPORT_SYMBOL(generic_write_checks);
3020
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003021/*
3022 * Performs necessary checks before doing a clone.
3023 *
3024 * Can adjust amount of bytes to clone.
3025 * Returns appropriate error code that caller should return or
3026 * zero in case the clone should be allowed.
3027 */
3028int generic_remap_checks(struct file *file_in, loff_t pos_in,
3029 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003030 loff_t *req_count, unsigned int remap_flags)
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003031{
3032 struct inode *inode_in = file_in->f_mapping->host;
3033 struct inode *inode_out = file_out->f_mapping->host;
3034 uint64_t count = *req_count;
3035 uint64_t bcount;
3036 loff_t size_in, size_out;
3037 loff_t bs = inode_out->i_sb->s_blocksize;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003038 int ret;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003039
3040 /* The start of both ranges must be aligned to an fs block. */
3041 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
3042 return -EINVAL;
3043
3044 /* Ensure offsets don't wrap. */
3045 if (pos_in + count < pos_in || pos_out + count < pos_out)
3046 return -EINVAL;
3047
3048 size_in = i_size_read(inode_in);
3049 size_out = i_size_read(inode_out);
3050
3051 /* Dedupe requires both ranges to be within EOF. */
Darrick J. Wong3d281932018-10-30 10:41:34 +11003052 if ((remap_flags & REMAP_FILE_DEDUP) &&
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003053 (pos_in >= size_in || pos_in + count > size_in ||
3054 pos_out >= size_out || pos_out + count > size_out))
3055 return -EINVAL;
3056
3057 /* Ensure the infile range is within the infile. */
3058 if (pos_in >= size_in)
3059 return -EINVAL;
3060 count = min(count, size_in - (uint64_t)pos_in);
3061
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003062 ret = generic_access_check_limits(file_in, pos_in, &count);
3063 if (ret)
3064 return ret;
3065
3066 ret = generic_write_check_limits(file_out, pos_out, &count);
3067 if (ret)
3068 return ret;
3069
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003070 /*
3071 * If the user wanted us to link to the infile's EOF, round up to the
3072 * next block boundary for this check.
3073 *
3074 * Otherwise, make sure the count is also block-aligned, having
3075 * already confirmed the starting offsets' block alignment.
3076 */
3077 if (pos_in + count == size_in) {
3078 bcount = ALIGN(size_in, bs) - pos_in;
3079 } else {
3080 if (!IS_ALIGNED(count, bs))
Darrick J. Wongeca36542018-10-30 10:42:10 +11003081 count = ALIGN_DOWN(count, bs);
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003082 bcount = count;
3083 }
3084
3085 /* Don't allow overlapped cloning within the same file. */
3086 if (inode_in == inode_out &&
3087 pos_out + bcount > pos_in &&
3088 pos_out < pos_in + bcount)
3089 return -EINVAL;
3090
Darrick J. Wongeca36542018-10-30 10:42:10 +11003091 /*
3092 * We shortened the request but the caller can't deal with that, so
3093 * bounce the request back to userspace.
3094 */
3095 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003096 return -EINVAL;
3097
Darrick J. Wongeca36542018-10-30 10:42:10 +11003098 *req_count = count;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003099 return 0;
3100}
3101
Nick Pigginafddba42007-10-16 01:25:01 -07003102int pagecache_write_begin(struct file *file, struct address_space *mapping,
3103 loff_t pos, unsigned len, unsigned flags,
3104 struct page **pagep, void **fsdata)
3105{
3106 const struct address_space_operations *aops = mapping->a_ops;
3107
Nick Piggin4e02ed42008-10-29 14:00:55 -07003108 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003109 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003110}
3111EXPORT_SYMBOL(pagecache_write_begin);
3112
3113int pagecache_write_end(struct file *file, struct address_space *mapping,
3114 loff_t pos, unsigned len, unsigned copied,
3115 struct page *page, void *fsdata)
3116{
3117 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003118
Nick Piggin4e02ed42008-10-29 14:00:55 -07003119 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003120}
3121EXPORT_SYMBOL(pagecache_write_end);
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003124generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
3126 struct file *file = iocb->ki_filp;
3127 struct address_space *mapping = file->f_mapping;
3128 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003129 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003131 size_t write_len;
3132 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Al Viro0c949332014-03-22 06:51:37 -04003134 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003135 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003136
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003137 if (iocb->ki_flags & IOCB_NOWAIT) {
3138 /* If there are pages to writeback, return */
3139 if (filemap_range_has_page(inode->i_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003140 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003141 return -EAGAIN;
3142 } else {
3143 written = filemap_write_and_wait_range(mapping, pos,
3144 pos + write_len - 1);
3145 if (written)
3146 goto out;
3147 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003148
3149 /*
3150 * After a write we want buffered reads to be sure to go to disk to get
3151 * the new data. We invalidate clean cached page from the region we're
3152 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003153 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003154 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003155 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003156 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003157 /*
3158 * If a page can not be invalidated, return 0 to fall back
3159 * to buffered write.
3160 */
3161 if (written) {
3162 if (written == -EBUSY)
3163 return 0;
3164 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003165 }
3166
Al Viro639a93a52017-04-13 14:10:15 -04003167 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003168
3169 /*
3170 * Finally, try again to invalidate clean pages which might have been
3171 * cached by non-direct readahead, or faulted in by get_user_pages()
3172 * if the source of the write was an mmap'ed region of the file
3173 * we're writing. Either one is a pretty crazy thing to do,
3174 * so we don't support it 100%. If this invalidation
3175 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003176 *
3177 * Most of the time we do not need this since dio_complete() will do
3178 * the invalidation for us. However there are some file systems that
3179 * do not end up with dio_complete() being called, so let's not break
3180 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003181 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003182 if (mapping->nrpages)
3183 invalidate_inode_pages2_range(mapping,
3184 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003187 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003188 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003189 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3190 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 mark_inode_dirty(inode);
3192 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003193 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
Al Viro639a93a52017-04-13 14:10:15 -04003195 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003196out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return written;
3198}
3199EXPORT_SYMBOL(generic_file_direct_write);
3200
Nick Piggineb2be182007-10-16 01:24:57 -07003201/*
3202 * Find or create a page at the given pagecache position. Return the locked
3203 * page. This function is specifically for buffered writes.
3204 */
Nick Piggin54566b22009-01-04 12:00:53 -08003205struct page *grab_cache_page_write_begin(struct address_space *mapping,
3206 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003207{
Nick Piggineb2be182007-10-16 01:24:57 -07003208 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003209 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003210
Nick Piggin54566b22009-01-04 12:00:53 -08003211 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003212 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003213
Mel Gorman2457aec2014-06-04 16:10:31 -07003214 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003215 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003216 if (page)
3217 wait_for_stable_page(page);
3218
Nick Piggineb2be182007-10-16 01:24:57 -07003219 return page;
3220}
Nick Piggin54566b22009-01-04 12:00:53 -08003221EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003222
Al Viro3b93f912014-02-11 21:34:08 -05003223ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003224 struct iov_iter *i, loff_t pos)
3225{
3226 struct address_space *mapping = file->f_mapping;
3227 const struct address_space_operations *a_ops = mapping->a_ops;
3228 long status = 0;
3229 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003230 unsigned int flags = 0;
3231
Nick Pigginafddba42007-10-16 01:25:01 -07003232 do {
3233 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003234 unsigned long offset; /* Offset into pagecache page */
3235 unsigned long bytes; /* Bytes to write to page */
3236 size_t copied; /* Bytes copied from user */
3237 void *fsdata;
3238
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003239 offset = (pos & (PAGE_SIZE - 1));
3240 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003241 iov_iter_count(i));
3242
3243again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003244 /*
3245 * Bring in the user page that we will copy from _first_.
3246 * Otherwise there's a nasty deadlock on copying from the
3247 * same page as we're writing to, without it being marked
3248 * up-to-date.
3249 *
3250 * Not only is this an optimisation, but it is also required
3251 * to check that the address is actually valid, when atomic
3252 * usercopies are used, below.
3253 */
3254 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3255 status = -EFAULT;
3256 break;
3257 }
3258
Jan Kara296291c2015-10-22 13:32:21 -07003259 if (fatal_signal_pending(current)) {
3260 status = -EINTR;
3261 break;
3262 }
3263
Nick Piggin674b8922007-10-16 01:25:03 -07003264 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003265 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003266 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003267 break;
3268
anfei zhou931e80e2010-02-02 13:44:02 -08003269 if (mapping_writably_mapped(mapping))
3270 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003271
Nick Pigginafddba42007-10-16 01:25:01 -07003272 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003273 flush_dcache_page(page);
3274
3275 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3276 page, fsdata);
3277 if (unlikely(status < 0))
3278 break;
3279 copied = status;
3280
3281 cond_resched();
3282
Nick Piggin124d3b72008-02-02 15:01:17 +01003283 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003284 if (unlikely(copied == 0)) {
3285 /*
3286 * If we were unable to copy any data at all, we must
3287 * fall back to a single segment length write.
3288 *
3289 * If we didn't fallback here, we could livelock
3290 * because not all segments in the iov can be copied at
3291 * once without a pagefault.
3292 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003293 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003294 iov_iter_single_seg_count(i));
3295 goto again;
3296 }
Nick Pigginafddba42007-10-16 01:25:01 -07003297 pos += copied;
3298 written += copied;
3299
3300 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003301 } while (iov_iter_count(i));
3302
3303 return written ? written : status;
3304}
Al Viro3b93f912014-02-11 21:34:08 -05003305EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Jan Karae4dd9de2009-08-17 18:10:06 +02003307/**
Al Viro81742022014-04-03 03:17:43 -04003308 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003309 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003310 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003311 *
3312 * This function does all the work needed for actually writing data to a
3313 * file. It does all basic checks, removes SUID from the file, updates
3314 * modification times and calls proper subroutines depending on whether we
3315 * do direct IO or a standard buffered write.
3316 *
3317 * It expects i_mutex to be grabbed unless we work on a block device or similar
3318 * object which does not need locking at all.
3319 *
3320 * This function does *not* take care of syncing data in case of O_SYNC write.
3321 * A caller has to handle it. This is mainly due to the fact that we want to
3322 * avoid syncing under i_mutex.
Mike Rapoporta862f682019-03-05 15:48:42 -08003323 *
3324 * Return:
3325 * * number of bytes written, even for truncated writes
3326 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003327 */
Al Viro81742022014-04-03 03:17:43 -04003328ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
3330 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003331 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003333 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003335 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003338 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003339 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (err)
3341 goto out;
3342
Josef Bacikc3b2da32012-03-26 09:59:21 -04003343 err = file_update_time(file);
3344 if (err)
3345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Al Viro2ba48ce2015-04-09 13:52:01 -04003347 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003348 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003349
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003350 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003351 /*
3352 * If the write stopped short of completing, fall back to
3353 * buffered writes. Some filesystems do this for writes to
3354 * holes, for example. For DAX files, a buffered write will
3355 * not succeed (even if it did, DAX does not handle dirty
3356 * page-cache pages correctly).
3357 */
Al Viro0b8def92015-04-07 10:22:53 -04003358 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003360
Al Viro0b8def92015-04-07 10:22:53 -04003361 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003362 /*
Al Viro3b93f912014-02-11 21:34:08 -05003363 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003364 * then we want to return the number of bytes which were
3365 * direct-written, or the error code if that was zero. Note
3366 * that this differs from normal direct-io semantics, which
3367 * will return -EFOO even if some bytes were written.
3368 */
Al Viro60bb4522014-08-08 12:39:16 -04003369 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003370 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003371 goto out;
3372 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003373 /*
3374 * We need to ensure that the page cache pages are written to
3375 * disk and invalidated to preserve the expected O_DIRECT
3376 * semantics.
3377 */
Al Viro3b93f912014-02-11 21:34:08 -05003378 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003379 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003380 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003381 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003382 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003383 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003384 pos >> PAGE_SHIFT,
3385 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003386 } else {
3387 /*
3388 * We don't know how much we wrote, so just return
3389 * the number of bytes which were direct-written
3390 */
3391 }
3392 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003393 written = generic_perform_write(file, from, iocb->ki_pos);
3394 if (likely(written > 0))
3395 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397out:
3398 current->backing_dev_info = NULL;
3399 return written ? written : err;
3400}
Al Viro81742022014-04-03 03:17:43 -04003401EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Jan Karae4dd9de2009-08-17 18:10:06 +02003403/**
Al Viro81742022014-04-03 03:17:43 -04003404 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003405 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003406 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003407 *
Al Viro81742022014-04-03 03:17:43 -04003408 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003409 * filesystems. It takes care of syncing the file in case of O_SYNC file
3410 * and acquires i_mutex as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003411 * Return:
3412 * * negative error code if no data has been written at all of
3413 * vfs_fsync_range() failed for a synchronous write
3414 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003415 */
Al Viro81742022014-04-03 03:17:43 -04003416ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
3418 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003419 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Al Viro59551022016-01-22 15:40:57 -05003422 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003423 ret = generic_write_checks(iocb, from);
3424 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003425 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003426 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Christoph Hellwige2592212016-04-07 08:52:01 -07003428 if (ret > 0)
3429 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return ret;
3431}
Al Viro81742022014-04-03 03:17:43 -04003432EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
David Howellscf9a2ae2006-08-29 19:05:54 +01003434/**
3435 * try_to_release_page() - release old fs-specific metadata on a page
3436 *
3437 * @page: the page which the kernel is trying to free
3438 * @gfp_mask: memory allocation flags (and I/O mode)
3439 *
3440 * The address_space is to try to release any data against the page
Mike Rapoporta862f682019-03-05 15:48:42 -08003441 * (presumably at page->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003442 *
David Howells266cf652009-04-03 16:42:36 +01003443 * This may also be called if PG_fscache is set on a page, indicating that the
3444 * page is known to the local caching routines.
3445 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003446 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003447 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003448 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003449 * Return: %1 if the release was successful, otherwise return zero.
David Howellscf9a2ae2006-08-29 19:05:54 +01003450 */
3451int try_to_release_page(struct page *page, gfp_t gfp_mask)
3452{
3453 struct address_space * const mapping = page->mapping;
3454
3455 BUG_ON(!PageLocked(page));
3456 if (PageWriteback(page))
3457 return 0;
3458
3459 if (mapping && mapping->a_ops->releasepage)
3460 return mapping->a_ops->releasepage(page, gfp_mask);
3461 return try_to_free_buffers(page);
3462}
3463
3464EXPORT_SYMBOL(try_to_release_page);