blob: 3ad18fa560576551a5f7579d4fe7dcc7ef13750f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/filemap.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
11 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080014#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070017#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/mman.h>
24#include <linux/pagemap.h>
25#include <linux/file.h>
26#include <linux/uio.h>
Josef Bacikcfcbfb12019-05-13 17:21:04 -070027#include <linux/error-injection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/hash.h>
29#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070030#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/pagevec.h>
32#include <linux/blkdev.h>
33#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080034#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070035#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080036#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060037#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080038#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070039#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070040#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070041#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080042#include "internal.h"
43
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070044#define CREATE_TRACE_POINTS
45#include <trace/events/filemap.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * FIXME: remove all knowledge of the buffer layer from the core VM
49 */
Jan Kara148f9482009-08-17 19:52:36 +020050#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/mman.h>
53
54/*
55 * Shared mappings implemented 30.11.1994. It's not fully working yet,
56 * though.
57 *
58 * Shared mappings now work. 15.8.1995 Bruno.
59 *
60 * finished 'unifying' the page and buffer cache and SMP-threaded the
61 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
62 *
63 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
64 */
65
66/*
67 * Lock ordering:
68 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080069 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070071 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070072 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080074 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080075 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080078 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070079 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070080 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * ->mmap_sem
83 * ->lock_page (access_process_vm)
84 *
Al Viroccad2362014-02-11 22:36:48 -050085 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070086 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060088 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110089 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070090 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080092 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * ->anon_vma.lock (vma_adjust)
94 *
95 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070096 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070098 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070099 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700101 * ->i_pages lock (try_to_unmap_one)
Andrey Ryabininf4b7e272019-03-05 15:49:39 -0800102 * ->pgdat->lru_lock (follow_page->mark_page_accessed)
103 * ->pgdat->lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700105 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600106 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100107 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700108 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600109 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100110 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
112 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800113 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700114 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500117static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700118 struct page *page, void *shadow)
119{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500120 XA_STATE(xas, &mapping->i_pages, page->index);
121 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800122
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500123 mapping_set_update(&xas, mapping);
124
125 /* hugetlb pages are represented by a single entry in the xarray */
126 if (!PageHuge(page)) {
127 xas_set_order(&xas, page->index, compound_order(page));
128 nr = 1U << compound_order(page);
129 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700130
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700131 VM_BUG_ON_PAGE(!PageLocked(page), page);
132 VM_BUG_ON_PAGE(PageTail(page), page);
133 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700134
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500135 xas_store(&xas, shadow);
136 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200137
Jan Kara23006382017-11-15 17:37:26 -0800138 page->mapping = NULL;
139 /* Leave page->index set: truncation lookup relies upon it */
140
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200141 if (shadow) {
142 mapping->nrexceptional += nr;
143 /*
144 * Make sure the nrexceptional update is committed before
145 * the nrpages update so that final truncate racing
146 * with reclaim does not see both counters 0 at the
147 * same time and miss a shadow entry.
148 */
149 smp_wmb();
150 }
151 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700152}
153
Jan Kara5ecc4d82017-11-15 17:37:29 -0800154static void unaccount_page_cache_page(struct address_space *mapping,
155 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800157 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600159 /*
160 * if we're uptodate, flush out into the cleancache, otherwise
161 * invalidate any existing cleancache entries. We can't leave
162 * stale data around in the cleancache once our page is gone
163 */
164 if (PageUptodate(page) && PageMappedToDisk(page))
165 cleancache_put_page(page);
166 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400167 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600168
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700169 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800170 VM_BUG_ON_PAGE(page_mapped(page), page);
171 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
172 int mapcount;
173
174 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
175 current->comm, page_to_pfn(page));
176 dump_page(page, "still mapped when deleted");
177 dump_stack();
178 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
179
180 mapcount = page_mapcount(page);
181 if (mapping_exiting(mapping) &&
182 page_count(page) >= mapcount + 2) {
183 /*
184 * All vmas have already been torn down, so it's
185 * a good bet that actually the page is unmapped,
186 * and we'd prefer not to leak it: if we're wrong,
187 * some other bad page check should catch it later.
188 */
189 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700190 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800191 }
192 }
193
Jan Kara76253fb2017-11-15 17:37:22 -0800194 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800195 if (PageHuge(page))
196 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800197
Jan Kara5ecc4d82017-11-15 17:37:29 -0800198 nr = hpage_nr_pages(page);
199
200 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
201 if (PageSwapBacked(page)) {
202 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
203 if (PageTransHuge(page))
204 __dec_node_page_state(page, NR_SHMEM_THPS);
205 } else {
206 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800207 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800208
209 /*
210 * At this point page must be either written or cleaned by
211 * truncate. Dirty page here signals a bug and loss of
212 * unwritten data.
213 *
214 * This fixes dirty accounting after removing the page entirely
215 * but leaves PageDirty set: it has no effect for truncated
216 * page and anyway will be cleared before returning page into
217 * buddy allocator.
218 */
219 if (WARN_ON_ONCE(PageDirty(page)))
220 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
221}
222
223/*
224 * Delete a page from the page cache and free it. Caller has to make
225 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700226 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800227 */
228void __delete_from_page_cache(struct page *page, void *shadow)
229{
230 struct address_space *mapping = page->mapping;
231
232 trace_mm_filemap_delete_from_page_cache(page);
233
234 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500235 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Jan Kara59c66c52017-11-15 17:37:18 -0800238static void page_cache_free_page(struct address_space *mapping,
239 struct page *page)
240{
241 void (*freepage)(struct page *);
242
243 freepage = mapping->a_ops->freepage;
244 if (freepage)
245 freepage(page);
246
247 if (PageTransHuge(page) && !PageHuge(page)) {
248 page_ref_sub(page, HPAGE_PMD_NR);
249 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
250 } else {
251 put_page(page);
252 }
253}
254
Minchan Kim702cfbf2011-03-22 16:32:43 -0700255/**
256 * delete_from_page_cache - delete page from page cache
257 * @page: the page which the kernel is trying to remove from page cache
258 *
259 * This must be called only on pages that have been verified to be in the page
260 * cache and locked. It will never put the page into the free list, the caller
261 * has a reference on the page.
262 */
263void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700265 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400266 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Matt Mackallcd7619d2005-05-01 08:59:01 -0700268 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700269 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700270 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700271 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500272
Jan Kara59c66c52017-11-15 17:37:18 -0800273 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700274}
275EXPORT_SYMBOL(delete_from_page_cache);
276
Jan Karaaa65c292017-11-15 17:37:33 -0800277/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500278 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800279 * @mapping: the mapping to which pages belong
280 * @pvec: pagevec with pages to delete
281 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700282 * The function walks over mapping->i_pages and removes pages passed in @pvec
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700283 * from the mapping. The function expects @pvec to be sorted by page index
284 * and is optimised for it to be dense.
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700285 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800286 * modified). The function expects only THP head pages to be present in the
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700287 * @pvec.
Jan Karaaa65c292017-11-15 17:37:33 -0800288 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700289 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800290 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500291static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800292 struct pagevec *pvec)
293{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500294 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800295 int total_pages = 0;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700296 int i = 0;
Jan Karaaa65c292017-11-15 17:37:33 -0800297 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800298
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500299 mapping_set_update(&xas, mapping);
300 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700301 if (i >= pagevec_count(pvec))
Jan Karaaa65c292017-11-15 17:37:33 -0800302 break;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700303
304 /* A swap/dax/shadow entry got inserted? Skip it. */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400305 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800306 continue;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700307 /*
308 * A page got inserted in our range? Skip it. We have our
309 * pages locked so they are protected from being removed.
310 * If we see a page whose index is higher than ours, it
311 * means our page has been removed, which shouldn't be
312 * possible because we're holding the PageLock.
313 */
314 if (page != pvec->pages[i]) {
315 VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index,
316 page);
317 continue;
Jan Karaaa65c292017-11-15 17:37:33 -0800318 }
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -0700319
320 WARN_ON_ONCE(!PageLocked(page));
321
322 if (page->index == xas.xa_index)
323 page->mapping = NULL;
324 /* Leave page->index set: truncation lookup relies on it */
325
326 /*
327 * Move to the next page in the vector if this is a regular
328 * page or the index is of the last sub-page of this compound
329 * page.
330 */
331 if (page->index + (1UL << compound_order(page)) - 1 ==
332 xas.xa_index)
333 i++;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500334 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800335 total_pages++;
336 }
337 mapping->nrpages -= total_pages;
338}
339
340void delete_from_page_cache_batch(struct address_space *mapping,
341 struct pagevec *pvec)
342{
343 int i;
344 unsigned long flags;
345
346 if (!pagevec_count(pvec))
347 return;
348
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700349 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800350 for (i = 0; i < pagevec_count(pvec); i++) {
351 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
352
353 unaccount_page_cache_page(mapping, pvec->pages[i]);
354 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500355 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700356 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800357
358 for (i = 0; i < pagevec_count(pvec); i++)
359 page_cache_free_page(mapping, pvec->pages[i]);
360}
361
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200362int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700363{
364 int ret = 0;
365 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700366 if (test_bit(AS_ENOSPC, &mapping->flags) &&
367 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700368 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700369 if (test_bit(AS_EIO, &mapping->flags) &&
370 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700371 ret = -EIO;
372 return ret;
373}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200374EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700375
Jeff Layton76341ca2017-07-06 07:02:22 -0400376static int filemap_check_and_keep_errors(struct address_space *mapping)
377{
378 /* Check for outstanding write errors */
379 if (test_bit(AS_EIO, &mapping->flags))
380 return -EIO;
381 if (test_bit(AS_ENOSPC, &mapping->flags))
382 return -ENOSPC;
383 return 0;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700387 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700388 * @mapping: address space structure to write
389 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800390 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700391 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700393 * Start writeback against all of a mapping's dirty pages that lie
394 * within the byte offsets <start, end> inclusive.
395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700397 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * these two operations is that if a dirty page/buffer is encountered, it must
399 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800400 *
401 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800403int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
404 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 int ret;
407 struct writeback_control wbc = {
408 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800409 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700410 .range_start = start,
411 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 };
413
414 if (!mapping_cap_writeback_dirty(mapping))
415 return 0;
416
Tejun Heob16b1de2015-06-02 08:39:48 -0600417 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600419 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return ret;
421}
422
423static inline int __filemap_fdatawrite(struct address_space *mapping,
424 int sync_mode)
425{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700426 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429int filemap_fdatawrite(struct address_space *mapping)
430{
431 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
432}
433EXPORT_SYMBOL(filemap_fdatawrite);
434
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400435int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800436 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
439}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400440EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Randy Dunlap485bb992006-06-23 02:03:49 -0700442/**
443 * filemap_flush - mostly a non-blocking flush
444 * @mapping: target address_space
445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * This is a mostly non-blocking flush. Not suitable for data-integrity
447 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800448 *
449 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451int filemap_flush(struct address_space *mapping)
452{
453 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
454}
455EXPORT_SYMBOL(filemap_flush);
456
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500457/**
458 * filemap_range_has_page - check if a page exists in range.
459 * @mapping: address space within which to check
460 * @start_byte: offset in bytes where the range starts
461 * @end_byte: offset in bytes where the range ends (inclusive)
462 *
463 * Find at least one page in the range supplied, usually used to check if
464 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800465 *
466 * Return: %true if at least one page exists in the specified range,
467 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500468 */
469bool filemap_range_has_page(struct address_space *mapping,
470 loff_t start_byte, loff_t end_byte)
471{
Jan Karaf7b68042017-09-06 16:21:40 -0700472 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500473 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
474 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500475
476 if (end_byte < start_byte)
477 return false;
478
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500479 rcu_read_lock();
480 for (;;) {
481 page = xas_find(&xas, max);
482 if (xas_retry(&xas, page))
483 continue;
484 /* Shadow entries don't count */
485 if (xa_is_value(page))
486 continue;
487 /*
488 * We don't need to try to pin this page; we're about to
489 * release the RCU lock anyway. It is enough to know that
490 * there was a page here recently.
491 */
492 break;
493 }
494 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500495
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500496 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500497}
498EXPORT_SYMBOL(filemap_range_has_page);
499
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400500static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800501 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300503 pgoff_t index = start_byte >> PAGE_SHIFT;
504 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct pagevec pvec;
506 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200508 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mel Gorman86679822017-11-15 17:37:52 -0800511 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800512 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned i;
514
Jan Kara312e9d22017-11-15 17:35:05 -0800515 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800516 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800517 if (!nr_pages)
518 break;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 for (i = 0; i < nr_pages; i++) {
521 struct page *page = pvec.pages[i];
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400524 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 pagevec_release(&pvec);
527 cond_resched();
528 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800529}
530
531/**
532 * filemap_fdatawait_range - wait for writeback to complete
533 * @mapping: address space structure to wait for
534 * @start_byte: offset in bytes where the range starts
535 * @end_byte: offset in bytes where the range ends (inclusive)
536 *
537 * Walk the list of under-writeback pages of the given address space
538 * in the given range and wait for all of them. Check error status of
539 * the address space and return it.
540 *
541 * Since the error status of the address space is cleared by this function,
542 * callers are responsible for checking the return value and handling and/or
543 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800544 *
545 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800546 */
547int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
548 loff_t end_byte)
549{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400550 __filemap_fdatawait_range(mapping, start_byte, end_byte);
551 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200553EXPORT_SYMBOL(filemap_fdatawait_range);
554
555/**
Jeff Laytona823e452017-07-28 07:24:43 -0400556 * file_fdatawait_range - wait for writeback to complete
557 * @file: file pointing to address space structure to wait for
558 * @start_byte: offset in bytes where the range starts
559 * @end_byte: offset in bytes where the range ends (inclusive)
560 *
561 * Walk the list of under-writeback pages of the address space that file
562 * refers to, in the given range and wait for all of them. Check error
563 * status of the address space vs. the file->f_wb_err cursor and return it.
564 *
565 * Since the error status of the file is advanced by this function,
566 * callers are responsible for checking the return value and handling and/or
567 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800568 *
569 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400570 */
571int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
572{
573 struct address_space *mapping = file->f_mapping;
574
575 __filemap_fdatawait_range(mapping, start_byte, end_byte);
576 return file_check_and_advance_wb_err(file);
577}
578EXPORT_SYMBOL(file_fdatawait_range);
579
580/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800581 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
582 * @mapping: address space structure to wait for
583 *
584 * Walk the list of under-writeback pages of the given address space
585 * and wait for all of them. Unlike filemap_fdatawait(), this function
586 * does not clear error status of the address space.
587 *
588 * Use this function if callers don't handle errors themselves. Expected
589 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
590 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800591 *
592 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800593 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400594int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800595{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400596 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400597 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800598}
Jeff Layton76341ca2017-07-06 07:02:22 -0400599EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800600
Jeff Layton9326c9b2017-07-26 10:21:11 -0400601static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400603 return (!dax_mapping(mapping) && mapping->nrpages) ||
604 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607int filemap_write_and_wait(struct address_space *mapping)
608{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800609 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jeff Layton9326c9b2017-07-26 10:21:11 -0400611 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800612 err = filemap_fdatawrite(mapping);
613 /*
614 * Even if the above returned error, the pages may be
615 * written partially (e.g. -ENOSPC), so we wait for it.
616 * But the -EIO is special case, it may indicate the worst
617 * thing (e.g. bug) happened, so we avoid waiting for it.
618 */
619 if (err != -EIO) {
620 int err2 = filemap_fdatawait(mapping);
621 if (!err)
622 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400623 } else {
624 /* Clear any previously stored errors */
625 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800626 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700627 } else {
628 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800632EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Randy Dunlap485bb992006-06-23 02:03:49 -0700634/**
635 * filemap_write_and_wait_range - write out & wait on a file range
636 * @mapping: the address_space for the pages
637 * @lstart: offset in bytes where the range starts
638 * @lend: offset in bytes where the range ends (inclusive)
639 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800640 * Write out and wait upon file offsets lstart->lend, inclusive.
641 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300642 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800643 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800644 *
645 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647int filemap_write_and_wait_range(struct address_space *mapping,
648 loff_t lstart, loff_t lend)
649{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800650 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jeff Layton9326c9b2017-07-26 10:21:11 -0400652 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800653 err = __filemap_fdatawrite_range(mapping, lstart, lend,
654 WB_SYNC_ALL);
655 /* See comment of filemap_write_and_wait() */
656 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200657 int err2 = filemap_fdatawait_range(mapping,
658 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800659 if (!err)
660 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400661 } else {
662 /* Clear any previously stored errors */
663 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800664 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700665 } else {
666 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
Chris Masonf6995582009-04-15 13:22:37 -0400670EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jeff Layton5660e132017-07-06 07:02:25 -0400672void __filemap_set_wb_err(struct address_space *mapping, int err)
673{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400674 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400675
676 trace_filemap_set_wb_err(mapping, eseq);
677}
678EXPORT_SYMBOL(__filemap_set_wb_err);
679
680/**
681 * file_check_and_advance_wb_err - report wb error (if any) that was previously
682 * and advance wb_err to current one
683 * @file: struct file on which the error is being reported
684 *
685 * When userland calls fsync (or something like nfsd does the equivalent), we
686 * want to report any writeback errors that occurred since the last fsync (or
687 * since the file was opened if there haven't been any).
688 *
689 * Grab the wb_err from the mapping. If it matches what we have in the file,
690 * then just quickly return 0. The file is all caught up.
691 *
692 * If it doesn't match, then take the mapping value, set the "seen" flag in
693 * it and try to swap it into place. If it works, or another task beat us
694 * to it with the new value, then update the f_wb_err and return the error
695 * portion. The error at this point must be reported via proper channels
696 * (a'la fsync, or NFS COMMIT operation, etc.).
697 *
698 * While we handle mapping->wb_err with atomic operations, the f_wb_err
699 * value is protected by the f_lock since we must ensure that it reflects
700 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800701 *
702 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400703 */
704int file_check_and_advance_wb_err(struct file *file)
705{
706 int err = 0;
707 errseq_t old = READ_ONCE(file->f_wb_err);
708 struct address_space *mapping = file->f_mapping;
709
710 /* Locklessly handle the common case where nothing has changed */
711 if (errseq_check(&mapping->wb_err, old)) {
712 /* Something changed, must use slow path */
713 spin_lock(&file->f_lock);
714 old = file->f_wb_err;
715 err = errseq_check_and_advance(&mapping->wb_err,
716 &file->f_wb_err);
717 trace_file_check_and_advance_wb_err(file, old);
718 spin_unlock(&file->f_lock);
719 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700720
721 /*
722 * We're mostly using this function as a drop in replacement for
723 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
724 * that the legacy code would have had on these flags.
725 */
726 clear_bit(AS_EIO, &mapping->flags);
727 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400728 return err;
729}
730EXPORT_SYMBOL(file_check_and_advance_wb_err);
731
732/**
733 * file_write_and_wait_range - write out & wait on a file range
734 * @file: file pointing to address_space with pages
735 * @lstart: offset in bytes where the range starts
736 * @lend: offset in bytes where the range ends (inclusive)
737 *
738 * Write out and wait upon file offsets lstart->lend, inclusive.
739 *
740 * Note that @lend is inclusive (describes the last byte to be written) so
741 * that this function can be used to write to the very end-of-file (end = -1).
742 *
743 * After writing out and waiting on the data, we check and advance the
744 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800745 *
746 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400747 */
748int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
749{
750 int err = 0, err2;
751 struct address_space *mapping = file->f_mapping;
752
Jeff Layton9326c9b2017-07-26 10:21:11 -0400753 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400754 err = __filemap_fdatawrite_range(mapping, lstart, lend,
755 WB_SYNC_ALL);
756 /* See comment of filemap_write_and_wait() */
757 if (err != -EIO)
758 __filemap_fdatawait_range(mapping, lstart, lend);
759 }
760 err2 = file_check_and_advance_wb_err(file);
761 if (!err)
762 err = err2;
763 return err;
764}
765EXPORT_SYMBOL(file_write_and_wait_range);
766
Randy Dunlap485bb992006-06-23 02:03:49 -0700767/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700768 * replace_page_cache_page - replace a pagecache page with a new one
769 * @old: page to be replaced
770 * @new: page to replace with
771 * @gfp_mask: allocation mode
772 *
773 * This function replaces a page in the pagecache with a new one. On
774 * success it acquires the pagecache reference for the new page and
775 * drops it for the old page. Both the old and new pages must be
776 * locked. This function does not add the new page to the LRU, the
777 * caller must do that.
778 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500779 * The remove + add is atomic. This function cannot fail.
Mike Rapoporta862f682019-03-05 15:48:42 -0800780 *
781 * Return: %0
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700782 */
783int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
784{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500785 struct address_space *mapping = old->mapping;
786 void (*freepage)(struct page *) = mapping->a_ops->freepage;
787 pgoff_t offset = old->index;
788 XA_STATE(xas, &mapping->i_pages, offset);
789 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700790
Sasha Levin309381fea2014-01-23 15:52:54 -0800791 VM_BUG_ON_PAGE(!PageLocked(old), old);
792 VM_BUG_ON_PAGE(!PageLocked(new), new);
793 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700794
Matthew Wilcox74d60952017-11-17 10:01:45 -0500795 get_page(new);
796 new->mapping = mapping;
797 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700798
Matthew Wilcox74d60952017-11-17 10:01:45 -0500799 xas_lock_irqsave(&xas, flags);
800 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700801
Matthew Wilcox74d60952017-11-17 10:01:45 -0500802 old->mapping = NULL;
803 /* hugetlb pages do not participate in page cache accounting. */
804 if (!PageHuge(old))
805 __dec_node_page_state(new, NR_FILE_PAGES);
806 if (!PageHuge(new))
807 __inc_node_page_state(new, NR_FILE_PAGES);
808 if (PageSwapBacked(old))
809 __dec_node_page_state(new, NR_SHMEM);
810 if (PageSwapBacked(new))
811 __inc_node_page_state(new, NR_SHMEM);
812 xas_unlock_irqrestore(&xas, flags);
813 mem_cgroup_migrate(old, new);
814 if (freepage)
815 freepage(old);
816 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700817
Matthew Wilcox74d60952017-11-17 10:01:45 -0500818 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700819}
820EXPORT_SYMBOL_GPL(replace_page_cache_page);
821
Johannes Weinera5289102014-04-03 14:47:51 -0700822static int __add_to_page_cache_locked(struct page *page,
823 struct address_space *mapping,
824 pgoff_t offset, gfp_t gfp_mask,
825 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500827 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700828 int huge = PageHuge(page);
829 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700830 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500831 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700832
Sasha Levin309381fea2014-01-23 15:52:54 -0800833 VM_BUG_ON_PAGE(!PageLocked(page), page);
834 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500835 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700836
Johannes Weiner00501b52014-08-08 14:19:20 -0700837 if (!huge) {
838 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800839 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700840 if (error)
841 return error;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300844 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700845 page->mapping = mapping;
846 page->index = offset;
847
Matthew Wilcox74d60952017-11-17 10:01:45 -0500848 do {
849 xas_lock_irq(&xas);
850 old = xas_load(&xas);
851 if (old && !xa_is_value(old))
852 xas_set_err(&xas, -EEXIST);
853 xas_store(&xas, page);
854 if (xas_error(&xas))
855 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700856
Matthew Wilcox74d60952017-11-17 10:01:45 -0500857 if (xa_is_value(old)) {
858 mapping->nrexceptional--;
859 if (shadowp)
860 *shadowp = old;
861 }
862 mapping->nrpages++;
863
864 /* hugetlb pages do not participate in page cache accounting */
865 if (!huge)
866 __inc_node_page_state(page, NR_FILE_PAGES);
867unlock:
868 xas_unlock_irq(&xas);
869 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
870
871 if (xas_error(&xas))
872 goto error;
873
Johannes Weiner00501b52014-08-08 14:19:20 -0700874 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800875 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700876 trace_mm_filemap_add_to_page_cache(page);
877 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500878error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700879 page->mapping = NULL;
880 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700881 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800882 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300883 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500884 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Josef Bacikcfcbfb12019-05-13 17:21:04 -0700886ALLOW_ERROR_INJECTION(__add_to_page_cache_locked, ERRNO);
Johannes Weinera5289102014-04-03 14:47:51 -0700887
888/**
889 * add_to_page_cache_locked - add a locked page to the pagecache
890 * @page: page to add
891 * @mapping: the page's address_space
892 * @offset: page index
893 * @gfp_mask: page allocation mode
894 *
895 * This function is used to add a page to the pagecache. It must be locked.
896 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800897 *
898 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700899 */
900int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
901 pgoff_t offset, gfp_t gfp_mask)
902{
903 return __add_to_page_cache_locked(page, mapping, offset,
904 gfp_mask, NULL);
905}
Nick Piggine2867812008-07-25 19:45:30 -0700906EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400909 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Johannes Weinera5289102014-04-03 14:47:51 -0700911 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700912 int ret;
913
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800914 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700915 ret = __add_to_page_cache_locked(page, mapping, offset,
916 gfp_mask, &shadow);
917 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800918 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700919 else {
920 /*
921 * The page might have been evicted from cache only
922 * recently, in which case it should be activated like
923 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700924 * The exception is pages getting rewritten; evicting other
925 * data from the working set, only to cache data that will
926 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700927 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700928 WARN_ON_ONCE(PageActive(page));
929 if (!(gfp_mask & __GFP_WRITE) && shadow)
930 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700931 lru_cache_add(page);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return ret;
934}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300935EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Paul Jackson44110fe2006-03-24 03:16:04 -0800937#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700938struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800939{
Miao Xiec0ff7452010-05-24 14:32:08 -0700940 int n;
941 struct page *page;
942
Paul Jackson44110fe2006-03-24 03:16:04 -0800943 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700944 unsigned int cpuset_mems_cookie;
945 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700946 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700947 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700948 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700949 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700950
Miao Xiec0ff7452010-05-24 14:32:08 -0700951 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800952 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700953 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800954}
Nick Piggin2ae88142006-10-28 10:38:23 -0700955EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800956#endif
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
959 * In order to wait for pages to become available there must be
960 * waitqueues associated with pages. By using a hash table of
961 * waitqueues where the bucket discipline is to maintain all
962 * waiters on the same queue and wake all when any of the pages
963 * become available, and for the woken contexts to check to be
964 * sure the appropriate page became available, this saves space
965 * at a cost of "thundering herd" phenomena during rare hash
966 * collisions.
967 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000968#define PAGE_WAIT_TABLE_BITS 8
969#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
970static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
971
972static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Nicholas Piggin62906022016-12-25 13:00:30 +1000974 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
Nicholas Piggin62906022016-12-25 13:00:30 +1000976
977void __init pagecache_init(void)
978{
979 int i;
980
981 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
982 init_waitqueue_head(&page_wait_table[i]);
983
984 page_writeback_init();
985}
986
Linus Torvalds3510ca22017-08-27 13:55:12 -0700987/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000988struct wait_page_key {
989 struct page *page;
990 int bit_nr;
991 int page_match;
992};
993
994struct wait_page_queue {
995 struct page *page;
996 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200997 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000998};
999
Ingo Molnarac6424b2017-06-20 12:06:13 +02001000static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +10001001{
1002 struct wait_page_key *key = arg;
1003 struct wait_page_queue *wait_page
1004 = container_of(wait, struct wait_page_queue, wait);
1005
1006 if (wait_page->page != key->page)
1007 return 0;
1008 key->page_match = 1;
1009
1010 if (wait_page->bit_nr != key->bit_nr)
1011 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001012
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001013 /*
1014 * Stop walking if it's locked.
1015 * Is this safe if put_and_wait_on_page_locked() is in use?
1016 * Yes: the waker must hold a reference to this page, and if PG_locked
1017 * has now already been set by another task, that task must also hold
1018 * a reference to the *same usage* of this page; so there is no need
1019 * to walk on to wake even the put_and_wait_on_page_locked() callers.
1020 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001021 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001022 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001023
1024 return autoremove_wake_function(wait, mode, sync, key);
1025}
1026
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001027static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001028{
1029 wait_queue_head_t *q = page_waitqueue(page);
1030 struct wait_page_key key;
1031 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001032 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001033
1034 key.page = page;
1035 key.bit_nr = bit_nr;
1036 key.page_match = 0;
1037
Tim Chen11a19c72017-08-25 09:13:55 -07001038 bookmark.flags = 0;
1039 bookmark.private = NULL;
1040 bookmark.func = NULL;
1041 INIT_LIST_HEAD(&bookmark.entry);
1042
Nicholas Piggin62906022016-12-25 13:00:30 +10001043 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001044 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1045
1046 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1047 /*
1048 * Take a breather from holding the lock,
1049 * allow pages that finish wake up asynchronously
1050 * to acquire the lock and remove themselves
1051 * from wait queue
1052 */
1053 spin_unlock_irqrestore(&q->lock, flags);
1054 cpu_relax();
1055 spin_lock_irqsave(&q->lock, flags);
1056 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1057 }
1058
Nicholas Piggin62906022016-12-25 13:00:30 +10001059 /*
1060 * It is possible for other pages to have collided on the waitqueue
1061 * hash, so in that case check for a page match. That prevents a long-
1062 * term waiter
1063 *
1064 * It is still possible to miss a case here, when we woke page waiters
1065 * and removed them from the waitqueue, but there are still other
1066 * page waiters.
1067 */
1068 if (!waitqueue_active(q) || !key.page_match) {
1069 ClearPageWaiters(page);
1070 /*
1071 * It's possible to miss clearing Waiters here, when we woke
1072 * our page waiters, but the hashed waitqueue has waiters for
1073 * other pages on it.
1074 *
1075 * That's okay, it's a rare case. The next waker will clear it.
1076 */
1077 }
1078 spin_unlock_irqrestore(&q->lock, flags);
1079}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001080
1081static void wake_up_page(struct page *page, int bit)
1082{
1083 if (!PageWaiters(page))
1084 return;
1085 wake_up_page_bit(page, bit);
1086}
Nicholas Piggin62906022016-12-25 13:00:30 +10001087
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001088/*
1089 * A choice of three behaviors for wait_on_page_bit_common():
1090 */
1091enum behavior {
1092 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
1093 * __lock_page() waiting on then setting PG_locked.
1094 */
1095 SHARED, /* Hold ref to page and check the bit when woken, like
1096 * wait_on_page_writeback() waiting on PG_writeback.
1097 */
1098 DROP, /* Drop ref to page before wait, no check when woken,
1099 * like put_and_wait_on_page_locked() on PG_locked.
1100 */
1101};
1102
Nicholas Piggin62906022016-12-25 13:00:30 +10001103static inline int wait_on_page_bit_common(wait_queue_head_t *q,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001104 struct page *page, int bit_nr, int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001105{
1106 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001107 wait_queue_entry_t *wait = &wait_page.wait;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001108 bool bit_is_set;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001109 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001110 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001111 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001112 int ret = 0;
1113
Johannes Weinereb414682018-10-26 15:06:27 -07001114 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001115 !PageUptodate(page) && PageWorkingset(page)) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001116 if (!PageSwapBacked(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001117 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001118 delayacct = true;
1119 }
Johannes Weinereb414682018-10-26 15:06:27 -07001120 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001121 thrashing = true;
1122 }
1123
Nicholas Piggin62906022016-12-25 13:00:30 +10001124 init_wait(wait);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001125 wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001126 wait->func = wake_page_function;
1127 wait_page.page = page;
1128 wait_page.bit_nr = bit_nr;
1129
1130 for (;;) {
1131 spin_lock_irq(&q->lock);
1132
Ingo Molnar2055da92017-06-20 12:06:46 +02001133 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001134 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001135 SetPageWaiters(page);
1136 }
1137
1138 set_current_state(state);
1139
1140 spin_unlock_irq(&q->lock);
1141
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001142 bit_is_set = test_bit(bit_nr, &page->flags);
1143 if (behavior == DROP)
1144 put_page(page);
Nicholas Piggin62906022016-12-25 13:00:30 +10001145
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001146 if (likely(bit_is_set))
1147 io_schedule();
1148
1149 if (behavior == EXCLUSIVE) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001150 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1151 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001152 } else if (behavior == SHARED) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001153 if (!test_bit(bit_nr, &page->flags))
1154 break;
1155 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001156
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001157 if (signal_pending_state(state, current)) {
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001158 ret = -EINTR;
1159 break;
1160 }
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001161
1162 if (behavior == DROP) {
1163 /*
1164 * We can no longer safely access page->flags:
1165 * even if CONFIG_MEMORY_HOTREMOVE is not enabled,
1166 * there is a risk of waiting forever on a page reused
1167 * for something that keeps it locked indefinitely.
1168 * But best check for -EINTR above before breaking.
1169 */
1170 break;
1171 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001172 }
1173
1174 finish_wait(q, wait);
1175
Johannes Weinereb414682018-10-26 15:06:27 -07001176 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001177 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001178 delayacct_thrashing_end();
1179 psi_memstall_leave(&pflags);
1180 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001181
Nicholas Piggin62906022016-12-25 13:00:30 +10001182 /*
1183 * A signal could leave PageWaiters set. Clearing it here if
1184 * !waitqueue_active would be possible (by open-coding finish_wait),
1185 * but still fail to catch it in the case of wait hash collision. We
1186 * already can fail to clear wait hash collision cases, so don't
1187 * bother with signals either.
1188 */
1189
1190 return ret;
1191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Harvey Harrison920c7a52008-02-04 22:29:26 -08001193void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Nicholas Piggin62906022016-12-25 13:00:30 +10001195 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001196 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198EXPORT_SYMBOL(wait_on_page_bit);
1199
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001200int wait_on_page_bit_killable(struct page *page, int bit_nr)
1201{
Nicholas Piggin62906022016-12-25 13:00:30 +10001202 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001203 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001204}
David Howells4343d002017-11-02 15:27:52 +00001205EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001208 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1209 * @page: The page to wait for.
1210 *
1211 * The caller should hold a reference on @page. They expect the page to
1212 * become unlocked relatively soon, but do not wish to hold up migration
1213 * (for example) by holding the reference while waiting for the page to
1214 * come unlocked. After this function returns, the caller should not
1215 * dereference @page.
1216 */
1217void put_and_wait_on_page_locked(struct page *page)
1218{
1219 wait_queue_head_t *q;
1220
1221 page = compound_head(page);
1222 q = page_waitqueue(page);
1223 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP);
1224}
1225
1226/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001227 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001228 * @page: Page defining the wait queue of interest
1229 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001230 *
1231 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1232 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001233void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001234{
1235 wait_queue_head_t *q = page_waitqueue(page);
1236 unsigned long flags;
1237
1238 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001239 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001240 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001241 spin_unlock_irqrestore(&q->lock, flags);
1242}
1243EXPORT_SYMBOL_GPL(add_page_wait_queue);
1244
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001245#ifndef clear_bit_unlock_is_negative_byte
1246
1247/*
1248 * PG_waiters is the high bit in the same byte as PG_lock.
1249 *
1250 * On x86 (and on many other architectures), we can clear PG_lock and
1251 * test the sign bit at the same time. But if the architecture does
1252 * not support that special operation, we just do this all by hand
1253 * instead.
1254 *
1255 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1256 * being cleared, but a memory barrier should be unneccssary since it is
1257 * in the same byte as PG_locked.
1258 */
1259static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1260{
1261 clear_bit_unlock(nr, mem);
1262 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001263 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001264}
1265
1266#endif
1267
David Howells385e1ca5f2009-04-03 16:42:39 +01001268/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001269 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * @page: the page
1271 *
1272 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1273 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001274 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1276 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001277 * Note that this depends on PG_waiters being the sign bit in the byte
1278 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1279 * clear the PG_locked bit and test PG_waiters at the same time fairly
1280 * portably (architectures that do LL/SC can test any bit, while x86 can
1281 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001283void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001285 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001286 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001287 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001288 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1289 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291EXPORT_SYMBOL(unlock_page);
1292
Randy Dunlap485bb992006-06-23 02:03:49 -07001293/**
1294 * end_page_writeback - end writeback against a page
1295 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297void end_page_writeback(struct page *page)
1298{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001299 /*
1300 * TestClearPageReclaim could be used here but it is an atomic
1301 * operation and overkill in this particular case. Failing to
1302 * shuffle a page marked for immediate reclaim is too mild to
1303 * justify taking an atomic operation penalty at the end of
1304 * ever page writeback.
1305 */
1306 if (PageReclaim(page)) {
1307 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001308 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001309 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001310
1311 if (!test_clear_page_writeback(page))
1312 BUG();
1313
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001314 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 wake_up_page(page, PG_writeback);
1316}
1317EXPORT_SYMBOL(end_page_writeback);
1318
Matthew Wilcox57d99842014-06-04 16:07:45 -07001319/*
1320 * After completing I/O on a page, call this routine to update the page
1321 * flags appropriately
1322 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001323void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001324{
Jens Axboec11f0c02016-08-05 08:11:04 -06001325 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001326 if (!err) {
1327 SetPageUptodate(page);
1328 } else {
1329 ClearPageUptodate(page);
1330 SetPageError(page);
1331 }
1332 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001333 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001334 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001335 struct address_space *mapping;
1336
Matthew Wilcox57d99842014-06-04 16:07:45 -07001337 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001338 mapping = page_mapping(page);
1339 if (mapping)
1340 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001341 }
1342 end_page_writeback(page);
1343 }
1344}
1345EXPORT_SYMBOL_GPL(page_endio);
1346
Randy Dunlap485bb992006-06-23 02:03:49 -07001347/**
1348 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001349 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001351void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Nicholas Piggin62906022016-12-25 13:00:30 +10001353 struct page *page = compound_head(__page);
1354 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001355 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE,
1356 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358EXPORT_SYMBOL(__lock_page);
1359
Nicholas Piggin62906022016-12-25 13:00:30 +10001360int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001361{
Nicholas Piggin62906022016-12-25 13:00:30 +10001362 struct page *page = compound_head(__page);
1363 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001364 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE,
1365 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001366}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001367EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001368
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001369/*
1370 * Return values:
1371 * 1 - page is locked; mmap_sem is still held.
1372 * 0 - page is not locked.
1373 * mmap_sem has been released (up_read()), unless flags had both
1374 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1375 * which case mmap_sem is still held.
1376 *
1377 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1378 * with the page locked and the mmap_sem unperturbed.
1379 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001380int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1381 unsigned int flags)
1382{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001383 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1384 /*
1385 * CAUTION! In this case, mmap_sem is not released
1386 * even though return 0.
1387 */
1388 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1389 return 0;
1390
1391 up_read(&mm->mmap_sem);
1392 if (flags & FAULT_FLAG_KILLABLE)
1393 wait_on_page_locked_killable(page);
1394 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001395 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001396 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001397 } else {
1398 if (flags & FAULT_FLAG_KILLABLE) {
1399 int ret;
1400
1401 ret = __lock_page_killable(page);
1402 if (ret) {
1403 up_read(&mm->mmap_sem);
1404 return 0;
1405 }
1406 } else
1407 __lock_page(page);
1408 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001409 }
1410}
1411
Randy Dunlap485bb992006-06-23 02:03:49 -07001412/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001413 * page_cache_next_miss() - Find the next gap in the page cache.
1414 * @mapping: Mapping.
1415 * @index: Index.
1416 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001417 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001418 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1419 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001420 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001421 * This function may be called under the rcu_read_lock. However, this will
1422 * not atomically search a snapshot of the cache at a single point in time.
1423 * For example, if a gap is created at index 5, then subsequently a gap is
1424 * created at index 10, page_cache_next_miss covering both indices may
1425 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001426 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001427 * Return: The index of the gap if found, otherwise an index outside the
1428 * range specified (in which case 'return - index >= max_scan' will be true).
1429 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001430 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001431pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001432 pgoff_t index, unsigned long max_scan)
1433{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001434 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001435
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001436 while (max_scan--) {
1437 void *entry = xas_next(&xas);
1438 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001439 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001440 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001441 break;
1442 }
1443
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001444 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001445}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001446EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001447
1448/**
Laurent Dufour2346a562019-05-13 17:21:29 -07001449 * page_cache_prev_miss() - Find the previous gap in the page cache.
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001450 * @mapping: Mapping.
1451 * @index: Index.
1452 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001453 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001454 * Search the range [max(index - max_scan + 1, 0), index] for the
1455 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001456 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001457 * This function may be called under the rcu_read_lock. However, this will
1458 * not atomically search a snapshot of the cache at a single point in time.
1459 * For example, if a gap is created at index 10, then subsequently a gap is
1460 * created at index 5, page_cache_prev_miss() covering both indices may
1461 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001462 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001463 * Return: The index of the gap if found, otherwise an index outside the
1464 * range specified (in which case 'index - return >= max_scan' will be true).
1465 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001466 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001467pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001468 pgoff_t index, unsigned long max_scan)
1469{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001470 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001471
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001472 while (max_scan--) {
1473 void *entry = xas_prev(&xas);
1474 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001475 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001476 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001477 break;
1478 }
1479
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001480 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001481}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001482EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001483
1484/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001485 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001486 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001487 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001488 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001489 * Looks up the page cache slot at @mapping & @offset. If there is a
1490 * page cache page, it is returned with an increased refcount.
1491 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001492 * If the slot holds a shadow entry of a previously evicted page, or a
1493 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001494 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001495 * Return: the found page or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001497struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001499 XA_STATE(xas, &mapping->i_pages, offset);
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001500 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Nick Piggina60637c2008-07-25 19:45:31 -07001502 rcu_read_lock();
1503repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001504 xas_reset(&xas);
1505 page = xas_load(&xas);
1506 if (xas_retry(&xas, page))
1507 goto repeat;
1508 /*
1509 * A shadow entry of a recently evicted page, or a swap entry from
1510 * shmem/tmpfs. Return it without attempting to raise page count.
1511 */
1512 if (!page || xa_is_value(page))
1513 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001514
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001515 if (!page_cache_get_speculative(page))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001516 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001517
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001518 /*
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001519 * Has the page moved or been split?
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001520 * This is part of the lockless pagecache protocol. See
1521 * include/linux/pagemap.h for details.
1522 */
1523 if (unlikely(page != xas_reload(&xas))) {
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001524 put_page(page);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001525 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001526 }
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001527 page = find_subpage(page, offset);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001528out:
Nick Piggina60637c2008-07-25 19:45:31 -07001529 rcu_read_unlock();
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return page;
1532}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001533EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Randy Dunlap485bb992006-06-23 02:03:49 -07001535/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001536 * find_lock_entry - locate, pin and lock a page cache entry
1537 * @mapping: the address_space to search
1538 * @offset: the page cache index
1539 *
1540 * Looks up the page cache slot at @mapping & @offset. If there is a
1541 * page cache page, it is returned locked and with an increased
1542 * refcount.
1543 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001544 * If the slot holds a shadow entry of a previously evicted page, or a
1545 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001546 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001547 * find_lock_entry() may sleep.
Mike Rapoporta862f682019-03-05 15:48:42 -08001548 *
1549 * Return: the found page or shadow entry, %NULL if nothing is found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001550 */
1551struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 struct page *page;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001556 page = find_get_entry(mapping, offset);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001557 if (page && !xa_is_value(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001558 lock_page(page);
1559 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001560 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001561 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001562 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001563 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001565 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return page;
1568}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001569EXPORT_SYMBOL(find_lock_entry);
1570
1571/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001572 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001573 * @mapping: the address_space to search
1574 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001575 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001576 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001577 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001578 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001579 *
Randy Dunlap75325182014-07-30 16:08:37 -07001580 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001581 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001582 * @fgp_flags can be:
1583 *
1584 * - FGP_ACCESSED: the page will be marked accessed
1585 * - FGP_LOCK: Page is return locked
1586 * - FGP_CREAT: If page is not present then a new page is allocated using
1587 * @gfp_mask and added to the page cache and the VM's LRU
1588 * list. The page is returned locked and with an increased
Mike Rapoporta862f682019-03-05 15:48:42 -08001589 * refcount.
Josef Bacika75d4c32019-03-13 11:44:14 -07001590 * - FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1591 * its own locking dance if the page is already in cache, or unlock the page
1592 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001593 *
1594 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1595 * if the GFP flags specified for FGP_CREAT are atomic.
1596 *
1597 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001598 *
1599 * Return: the found page or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001600 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001601struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001602 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Nick Piggineb2be182007-10-16 01:24:57 -07001604 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001607 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001608 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001609 page = NULL;
1610 if (!page)
1611 goto no_page;
1612
1613 if (fgp_flags & FGP_LOCK) {
1614 if (fgp_flags & FGP_NOWAIT) {
1615 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001617 return NULL;
1618 }
1619 } else {
1620 lock_page(page);
1621 }
1622
1623 /* Has the page been truncated? */
1624 if (unlikely(page->mapping != mapping)) {
1625 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001626 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001627 goto repeat;
1628 }
1629 VM_BUG_ON_PAGE(page->index != offset, page);
1630 }
1631
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001632 if (fgp_flags & FGP_ACCESSED)
Mel Gorman2457aec2014-06-04 16:10:31 -07001633 mark_page_accessed(page);
1634
1635no_page:
1636 if (!page && (fgp_flags & FGP_CREAT)) {
1637 int err;
1638 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001639 gfp_mask |= __GFP_WRITE;
1640 if (fgp_flags & FGP_NOFS)
1641 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001642
Michal Hocko45f87de2014-12-29 20:30:35 +01001643 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001644 if (!page)
1645 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001646
Josef Bacika75d4c32019-03-13 11:44:14 -07001647 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001648 fgp_flags |= FGP_LOCK;
1649
Hugh Dickinseb39d612014-08-06 16:06:43 -07001650 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001651 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001652 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001653
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001654 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001655 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001656 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001657 page = NULL;
1658 if (err == -EEXIST)
1659 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001661
1662 /*
1663 * add_to_page_cache_lru locks the page, and for mmap we expect
1664 * an unlocked page.
1665 */
1666 if (page && (fgp_flags & FGP_FOR_MMAP))
1667 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return page;
1671}
Mel Gorman2457aec2014-06-04 16:10:31 -07001672EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001675 * find_get_entries - gang pagecache lookup
1676 * @mapping: The address_space to search
1677 * @start: The starting page cache index
1678 * @nr_entries: The maximum number of entries
1679 * @entries: Where the resulting entries are placed
1680 * @indices: The cache indices corresponding to the entries in @entries
1681 *
1682 * find_get_entries() will search for and return a group of up to
1683 * @nr_entries entries in the mapping. The entries are placed at
1684 * @entries. find_get_entries() takes a reference against any actual
1685 * pages it returns.
1686 *
1687 * The search returns a group of mapping-contiguous page cache entries
1688 * with ascending indexes. There may be holes in the indices due to
1689 * not-present pages.
1690 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001691 * Any shadow entries of evicted pages, or swap entries from
1692 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001693 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001694 * Return: the number of pages and shadow entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001695 */
1696unsigned find_get_entries(struct address_space *mapping,
1697 pgoff_t start, unsigned int nr_entries,
1698 struct page **entries, pgoff_t *indices)
1699{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001700 XA_STATE(xas, &mapping->i_pages, start);
1701 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001702 unsigned int ret = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001703
1704 if (!nr_entries)
1705 return 0;
1706
1707 rcu_read_lock();
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001708 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001709 if (xas_retry(&xas, page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001710 continue;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001711 /*
1712 * A shadow entry of a recently evicted page, a swap
1713 * entry from shmem/tmpfs or a DAX entry. Return it
1714 * without attempting to raise page count.
1715 */
1716 if (xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001717 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001718
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001719 if (!page_cache_get_speculative(page))
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001720 goto retry;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001721
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001722 /* Has the page moved or been split? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001723 if (unlikely(page != xas_reload(&xas)))
1724 goto put_page;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001725 page = find_subpage(page, xas.xa_index);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001726
Johannes Weiner0cd61442014-04-03 14:47:46 -07001727export:
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001728 indices[ret] = xas.xa_index;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001729 entries[ret] = page;
1730 if (++ret == nr_entries)
1731 break;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001732 continue;
1733put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001734 put_page(page);
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001735retry:
1736 xas_reset(&xas);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001737 }
1738 rcu_read_unlock();
1739 return ret;
1740}
1741
1742/**
Jan Karab947cee2017-09-06 16:21:21 -07001743 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * @mapping: The address_space to search
1745 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001746 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * @nr_pages: The maximum number of pages
1748 * @pages: Where the resulting pages are placed
1749 *
Jan Karab947cee2017-09-06 16:21:21 -07001750 * find_get_pages_range() will search for and return a group of up to @nr_pages
1751 * pages in the mapping starting at index @start and up to index @end
1752 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1753 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 *
1755 * The search returns a group of mapping-contiguous pages with ascending
1756 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001757 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001759 * Return: the number of pages which were found. If this number is
1760 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07001761 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Jan Karab947cee2017-09-06 16:21:21 -07001763unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1764 pgoff_t end, unsigned int nr_pages,
1765 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001767 XA_STATE(xas, &mapping->i_pages, *start);
1768 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001769 unsigned ret = 0;
1770
1771 if (unlikely(!nr_pages))
1772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Nick Piggina60637c2008-07-25 19:45:31 -07001774 rcu_read_lock();
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001775 xas_for_each(&xas, page, end) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001776 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001777 continue;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001778 /* Skip over shadow, swap and DAX entries */
1779 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001780 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001781
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001782 if (!page_cache_get_speculative(page))
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001783 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001784
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001785 /* Has the page moved or been split? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001786 if (unlikely(page != xas_reload(&xas)))
1787 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001788
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001789 pages[ret] = find_subpage(page, xas.xa_index);
Jan Karab947cee2017-09-06 16:21:21 -07001790 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001791 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07001792 goto out;
1793 }
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001794 continue;
1795put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001796 put_page(page);
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001797retry:
1798 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001799 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001800
Jan Karab947cee2017-09-06 16:21:21 -07001801 /*
1802 * We come here when there is no page beyond @end. We take care to not
1803 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001804 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07001805 * already broken anyway.
1806 */
1807 if (end == (pgoff_t)-1)
1808 *start = (pgoff_t)-1;
1809 else
1810 *start = end + 1;
1811out:
Nick Piggina60637c2008-07-25 19:45:31 -07001812 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return ret;
1815}
1816
Jens Axboeebf43502006-04-27 08:46:01 +02001817/**
1818 * find_get_pages_contig - gang contiguous pagecache lookup
1819 * @mapping: The address_space to search
1820 * @index: The starting page index
1821 * @nr_pages: The maximum number of pages
1822 * @pages: Where the resulting pages are placed
1823 *
1824 * find_get_pages_contig() works exactly like find_get_pages(), except
1825 * that the returned number of pages are guaranteed to be contiguous.
1826 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001827 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02001828 */
1829unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1830 unsigned int nr_pages, struct page **pages)
1831{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001832 XA_STATE(xas, &mapping->i_pages, index);
1833 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001834 unsigned int ret = 0;
1835
1836 if (unlikely(!nr_pages))
1837 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001838
Nick Piggina60637c2008-07-25 19:45:31 -07001839 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001840 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001841 if (xas_retry(&xas, page))
1842 continue;
1843 /*
1844 * If the entry has been swapped out, we can stop looking.
1845 * No current caller is looking for DAX entries.
1846 */
1847 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001848 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001849
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001850 if (!page_cache_get_speculative(page))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001851 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001852
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001853 /* Has the page moved or been split? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001854 if (unlikely(page != xas_reload(&xas)))
1855 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001856
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001857 pages[ret] = find_subpage(page, xas.xa_index);
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001858 if (++ret == nr_pages)
1859 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001860 continue;
1861put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001862 put_page(page);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001863retry:
1864 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02001865 }
Nick Piggina60637c2008-07-25 19:45:31 -07001866 rcu_read_unlock();
1867 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001868}
David Howellsef71c152007-05-09 02:33:44 -07001869EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001870
Randy Dunlap485bb992006-06-23 02:03:49 -07001871/**
Jan Kara72b045a2017-11-15 17:34:33 -08001872 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001873 * @mapping: the address_space to search
1874 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001875 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001876 * @tag: the tag index
1877 * @nr_pages: the maximum number of pages
1878 * @pages: where the resulting pages are placed
1879 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001881 * @tag. We update @index to index the next page for the traversal.
Mike Rapoporta862f682019-03-05 15:48:42 -08001882 *
1883 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Jan Kara72b045a2017-11-15 17:34:33 -08001885unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001886 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08001887 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001889 XA_STATE(xas, &mapping->i_pages, *index);
1890 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001891 unsigned ret = 0;
1892
1893 if (unlikely(!nr_pages))
1894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Nick Piggina60637c2008-07-25 19:45:31 -07001896 rcu_read_lock();
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001897 xas_for_each_marked(&xas, page, end, tag) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001898 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001899 continue;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001900 /*
1901 * Shadow entries should never be tagged, but this iteration
1902 * is lockless so there is a window for page reclaim to evict
1903 * a page we saw tagged. Skip over it.
1904 */
1905 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07001906 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001907
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001908 if (!page_cache_get_speculative(page))
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001909 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001910
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001911 /* Has the page moved or been split? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001912 if (unlikely(page != xas_reload(&xas)))
1913 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001914
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001915 pages[ret] = find_subpage(page, xas.xa_index);
Jan Kara72b045a2017-11-15 17:34:33 -08001916 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08001917 *index = xas.xa_index + 1;
Jan Kara72b045a2017-11-15 17:34:33 -08001918 goto out;
1919 }
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001920 continue;
1921put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001922 put_page(page);
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001923retry:
1924 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001925 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001926
Jan Kara72b045a2017-11-15 17:34:33 -08001927 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001928 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08001929 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001930 * iteration when there is a page at index -1 but that is already
1931 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08001932 */
1933 if (end == (pgoff_t)-1)
1934 *index = (pgoff_t)-1;
1935 else
1936 *index = end + 1;
1937out:
Nick Piggina60637c2008-07-25 19:45:31 -07001938 rcu_read_unlock();
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return ret;
1941}
Jan Kara72b045a2017-11-15 17:34:33 -08001942EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001944/**
1945 * find_get_entries_tag - find and return entries that match @tag
1946 * @mapping: the address_space to search
1947 * @start: the starting page cache index
1948 * @tag: the tag index
1949 * @nr_entries: the maximum number of entries
1950 * @entries: where the resulting entries are placed
1951 * @indices: the cache indices corresponding to the entries in @entries
1952 *
1953 * Like find_get_entries, except we only return entries which are tagged with
1954 * @tag.
Mike Rapoporta862f682019-03-05 15:48:42 -08001955 *
1956 * Return: the number of entries which were found.
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001957 */
1958unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001959 xa_mark_t tag, unsigned int nr_entries,
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001960 struct page **entries, pgoff_t *indices)
1961{
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001962 XA_STATE(xas, &mapping->i_pages, start);
1963 struct page *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001964 unsigned int ret = 0;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001965
1966 if (!nr_entries)
1967 return 0;
1968
1969 rcu_read_lock();
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001970 xas_for_each_marked(&xas, page, ULONG_MAX, tag) {
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001971 if (xas_retry(&xas, page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001972 continue;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001973 /*
1974 * A shadow entry of a recently evicted page, a swap
1975 * entry from shmem/tmpfs or a DAX entry. Return it
1976 * without attempting to raise page count.
1977 */
1978 if (xa_is_value(page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001979 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001980
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001981 if (!page_cache_get_speculative(page))
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001982 goto retry;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001983
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001984 /* Has the page moved or been split? */
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001985 if (unlikely(page != xas_reload(&xas)))
1986 goto put_page;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001987 page = find_subpage(page, xas.xa_index);
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001988
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001989export:
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001990 indices[ret] = xas.xa_index;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001991 entries[ret] = page;
1992 if (++ret == nr_entries)
1993 break;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001994 continue;
1995put_page:
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07001996 put_page(page);
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001997retry:
1998 xas_reset(&xas);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001999 }
2000 rcu_read_unlock();
2001 return ret;
2002}
2003EXPORT_SYMBOL(find_get_entries_tag);
2004
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002005/*
2006 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2007 * a _large_ part of the i/o request. Imagine the worst scenario:
2008 *
2009 * ---R__________________________________________B__________
2010 * ^ reading here ^ bad block(assume 4k)
2011 *
2012 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2013 * => failing the whole request => read(R) => read(R+1) =>
2014 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2015 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2016 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2017 *
2018 * It is going insane. Fix it by quickly scaling down the readahead size.
2019 */
2020static void shrink_readahead_size_eio(struct file *filp,
2021 struct file_ra_state *ra)
2022{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002023 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002024}
2025
Randy Dunlap485bb992006-06-23 02:03:49 -07002026/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002027 * generic_file_buffered_read - generic file read routine
2028 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002029 * @iter: data destination
2030 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002033 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *
2035 * This is really ugly. But the goto's actually try to clarify some
2036 * of the logic when it comes to error handling etc.
Mike Rapoporta862f682019-03-05 15:48:42 -08002037 *
2038 * Return:
2039 * * total number of bytes copied, including those the were already @written
2040 * * negative error code if nothing was copied
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002042static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002043 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002045 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002046 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002048 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002049 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002050 pgoff_t index;
2051 pgoff_t last_index;
2052 pgoff_t prev_index;
2053 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002054 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002055 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Wei Fangc2a97372016-10-07 17:01:52 -07002057 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002058 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002059 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2060
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002061 index = *ppos >> PAGE_SHIFT;
2062 prev_index = ra->prev_pos >> PAGE_SHIFT;
2063 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2064 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2065 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 for (;;) {
2068 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002069 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002070 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 unsigned long nr, ret;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002075 if (fatal_signal_pending(current)) {
2076 error = -EINTR;
2077 goto out;
2078 }
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002081 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002082 if (iocb->ki_flags & IOCB_NOWAIT)
2083 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002084 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002085 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002086 index, last_index - index);
2087 page = find_get_page(mapping, index);
2088 if (unlikely(page == NULL))
2089 goto no_cached_page;
2090 }
2091 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002092 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002093 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002094 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002096 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002097 if (iocb->ki_flags & IOCB_NOWAIT) {
2098 put_page(page);
2099 goto would_block;
2100 }
2101
Mel Gormanebded022016-03-15 14:55:39 -07002102 /*
2103 * See comment in do_read_cache_page on why
2104 * wait_on_page_locked is used to avoid unnecessarily
2105 * serialisations and why it's safe.
2106 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002107 error = wait_on_page_locked_killable(page);
2108 if (unlikely(error))
2109 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002110 if (PageUptodate(page))
2111 goto page_ok;
2112
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002113 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002114 !mapping->a_ops->is_partially_uptodate)
2115 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002116 /* pipes can't handle partially uptodate pages */
David Howells00e23702018-10-22 13:07:28 +01002117 if (unlikely(iov_iter_is_pipe(iter)))
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002118 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002119 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002120 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002121 /* Did it get truncated before we got the lock? */
2122 if (!page->mapping)
2123 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002124 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002125 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002126 goto page_not_up_to_date_locked;
2127 unlock_page(page);
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002130 /*
2131 * i_size must be checked after we know the page is Uptodate.
2132 *
2133 * Checking i_size after the check allows us to calculate
2134 * the correct value for "nr", which means the zero-filled
2135 * part of the page is not copied back to userspace (unless
2136 * another truncate extends the file - this is desired though).
2137 */
2138
2139 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002141 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002142 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002143 goto out;
2144 }
2145
2146 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002148 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002149 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002150 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002151 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002152 goto out;
2153 }
2154 }
2155 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 /* If users can be writing to this page using arbitrary
2158 * virtual addresses, take care about potential aliasing
2159 * before reading the page on the kernel side.
2160 */
2161 if (mapping_writably_mapped(mapping))
2162 flush_dcache_page(page);
2163
2164 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002165 * When a sequential read accesses a page several times,
2166 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 */
Jan Karaec0f1632007-05-06 14:49:25 -07002168 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 mark_page_accessed(page);
2170 prev_index = index;
2171
2172 /*
2173 * Ok, we have the page, and it's up-to-date, so
2174 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
Al Viro6e58e792014-02-03 17:07:03 -05002176
2177 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 index += offset >> PAGE_SHIFT;
2180 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002181 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002183 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002184 written += ret;
2185 if (!iov_iter_count(iter))
2186 goto out;
2187 if (ret < nr) {
2188 error = -EFAULT;
2189 goto out;
2190 }
2191 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193page_not_up_to_date:
2194 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002195 error = lock_page_killable(page);
2196 if (unlikely(error))
2197 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002199page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002200 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (!page->mapping) {
2202 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002203 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 continue;
2205 }
2206
2207 /* Did somebody else fill it already? */
2208 if (PageUptodate(page)) {
2209 unlock_page(page);
2210 goto page_ok;
2211 }
2212
2213readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002214 /*
2215 * A previous I/O error may have been due to temporary
2216 * failures, eg. multipath errors.
2217 * PG_error will be set again if readpage fails.
2218 */
2219 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* Start the actual read. The read will unlock the page. */
2221 error = mapping->a_ops->readpage(filp, page);
2222
Zach Brown994fc28c2005-12-15 14:28:17 -08002223 if (unlikely(error)) {
2224 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002225 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002226 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002227 goto find_page;
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002233 error = lock_page_killable(page);
2234 if (unlikely(error))
2235 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (!PageUptodate(page)) {
2237 if (page->mapping == NULL) {
2238 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002239 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 */
2241 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002242 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto find_page;
2244 }
2245 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002246 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002247 error = -EIO;
2248 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 unlock_page(page);
2251 }
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 goto page_ok;
2254
2255readpage_error:
2256 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002257 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto out;
2259
2260no_cached_page:
2261 /*
2262 * Ok, it wasn't cached, so we need to create a new
2263 * page..
2264 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002265 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002266 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002267 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002270 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002271 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002273 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002274 if (error == -EEXIST) {
2275 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto out;
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto readpage;
2281 }
2282
Milosz Tanski3239d832017-08-29 16:13:19 +02002283would_block:
2284 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002286 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002287 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002288 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002290 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002291 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002292 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Randy Dunlap485bb992006-06-23 02:03:49 -07002295/**
Al Viro6abd2322014-04-04 14:20:57 -04002296 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002297 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002298 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002299 *
Al Viro6abd2322014-04-04 14:20:57 -04002300 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 * that can use the page cache directly.
Mike Rapoporta862f682019-03-05 15:48:42 -08002302 * Return:
2303 * * number of bytes copied, even for partial reads
2304 * * negative error code if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 */
2306ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002307generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002309 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002310 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002311
2312 if (!count)
2313 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Al Viro2ba48ce2015-04-09 13:52:01 -04002315 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002316 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002317 struct address_space *mapping = file->f_mapping;
2318 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002319 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002322 if (iocb->ki_flags & IOCB_NOWAIT) {
2323 if (filemap_range_has_page(mapping, iocb->ki_pos,
2324 iocb->ki_pos + count - 1))
2325 return -EAGAIN;
2326 } else {
2327 retval = filemap_write_and_wait_range(mapping,
2328 iocb->ki_pos,
2329 iocb->ki_pos + count - 1);
2330 if (retval < 0)
2331 goto out;
2332 }
Al Viroed978a82014-03-05 22:53:04 -05002333
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002334 file_accessed(file);
2335
Al Viro5ecda132017-04-13 14:13:36 -04002336 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002337 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002338 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002339 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002340 }
Al Viro5b47d592017-05-08 13:54:47 -04002341 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002342
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002343 /*
2344 * Btrfs can have a short DIO read if we encounter
2345 * compressed extents, so if there was an error, or if
2346 * we've already read everything we wanted to, or if
2347 * there was a short read because we hit EOF, go ahead
2348 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002349 * the rest of the read. Buffered reads will not work for
2350 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002351 */
Al Viro5ecda132017-04-13 14:13:36 -04002352 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002353 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002357 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358out:
2359 return retval;
2360}
Al Viroed978a82014-03-05 22:53:04 -05002361EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364#define MMAP_LOTSAMISS (100)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002365static struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
2366 struct file *fpin)
2367{
2368 int flags = vmf->flags;
2369
2370 if (fpin)
2371 return fpin;
2372
2373 /*
2374 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2375 * anything, so we only pin the file and drop the mmap_sem if only
2376 * FAULT_FLAG_ALLOW_RETRY is set.
2377 */
2378 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2379 FAULT_FLAG_ALLOW_RETRY) {
2380 fpin = get_file(vmf->vma->vm_file);
2381 up_read(&vmf->vma->vm_mm->mmap_sem);
2382 }
2383 return fpin;
2384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Linus Torvaldsef00e082009-06-16 15:31:25 -07002386/*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002387 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2388 * @vmf - the vm_fault for this fault.
2389 * @page - the page to lock.
2390 * @fpin - the pointer to the file we may pin (or is already pinned).
2391 *
2392 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2393 * It differs in that it actually returns the page locked if it returns 1 and 0
2394 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2395 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002396 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002397static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2398 struct file **fpin)
2399{
2400 if (trylock_page(page))
2401 return 1;
2402
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002403 /*
2404 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2405 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2406 * is supposed to work. We have way too many special cases..
2407 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002408 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2409 return 0;
2410
2411 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2412 if (vmf->flags & FAULT_FLAG_KILLABLE) {
2413 if (__lock_page_killable(page)) {
2414 /*
2415 * We didn't have the right flags to drop the mmap_sem,
2416 * but all fault_handlers only check for fatal signals
2417 * if we return VM_FAULT_RETRY, so we need to drop the
2418 * mmap_sem here and return 0 if we don't have a fpin.
2419 */
2420 if (*fpin == NULL)
2421 up_read(&vmf->vma->vm_mm->mmap_sem);
2422 return 0;
2423 }
2424 } else
2425 __lock_page(page);
2426 return 1;
2427}
2428
2429
2430/*
2431 * Synchronous readahead happens when we don't even find a page in the page
2432 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2433 * to drop the mmap sem we return the file that was pinned in order for us to do
2434 * that. If we didn't pin a file then we return NULL. The file that is
2435 * returned needs to be fput()'ed when we're done with it.
2436 */
2437static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002438{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002439 struct file *file = vmf->vma->vm_file;
2440 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002441 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002442 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002443 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002444
2445 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002446 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002447 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002448 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002449 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002450
Josef Bacik2a1180f2019-03-13 11:44:18 -07002451 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002452 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002453 page_cache_sync_readahead(mapping, ra, file, offset,
2454 ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002455 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002456 }
2457
Andi Kleen207d04b2011-05-24 17:12:29 -07002458 /* Avoid banging the cache line if not needed */
2459 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002460 ra->mmap_miss++;
2461
2462 /*
2463 * Do we miss much more than hit in this file? If so,
2464 * stop bothering with read-ahead. It will only hurt.
2465 */
2466 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002467 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002468
Wu Fengguangd30a1102009-06-16 15:31:30 -07002469 /*
2470 * mmap read-around
2471 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002472 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002473 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2474 ra->size = ra->ra_pages;
2475 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002476 ra_submit(ra, mapping, file);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002477 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002478}
2479
2480/*
2481 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002482 * so we want to possibly extend the readahead further. We return the file that
2483 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002484 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002485static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
2486 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002487{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002488 struct file *file = vmf->vma->vm_file;
2489 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002490 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002491 struct file *fpin = NULL;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002492 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002493
2494 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002495 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002496 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002497 if (ra->mmap_miss > 0)
2498 ra->mmap_miss--;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002499 if (PageReadahead(page)) {
2500 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002501 page_cache_async_readahead(mapping, ra, file,
2502 page, offset, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002503 }
2504 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002505}
2506
Randy Dunlap485bb992006-06-23 02:03:49 -07002507/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002508 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002509 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002510 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002511 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * mapped memory region to read in file data during a page fault.
2513 *
2514 * The goto's are kind of ugly, but this streamlines the normal case of having
2515 * it in the page cache, and handles the special cases reasonably without
2516 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002517 *
2518 * vma->vm_mm->mmap_sem must be held on entry.
2519 *
2520 * If our return value has VM_FAULT_RETRY set, it's because
2521 * lock_page_or_retry() returned 0.
2522 * The mmap_sem has usually been released in this case.
2523 * See __lock_page_or_retry() for the exception.
2524 *
2525 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2526 * has not been released.
2527 *
2528 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08002529 *
2530 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002532vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002535 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002536 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 struct address_space *mapping = file->f_mapping;
2538 struct file_ra_state *ra = &file->f_ra;
2539 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002540 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002541 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002543 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002545 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2546 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002547 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002550 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002552 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002553 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002555 * We found the page, so try async readahead before
2556 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002558 fpin = do_async_mmap_readahead(vmf, page);
Shaohua Li45cac652012-10-08 16:32:19 -07002559 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002560 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002561 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002562 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002563 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002564 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002565retry_find:
Josef Bacika75d4c32019-03-13 11:44:14 -07002566 page = pagecache_get_page(mapping, offset,
2567 FGP_CREAT|FGP_FOR_MMAP,
2568 vmf->gfp_mask);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002569 if (!page) {
2570 if (fpin)
2571 goto out_retry;
Josef Bacika75d4c32019-03-13 11:44:14 -07002572 return vmf_error(-ENOMEM);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002576 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
2577 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002578
2579 /* Did it get truncated? */
2580 if (unlikely(page->mapping != mapping)) {
2581 unlock_page(page);
2582 put_page(page);
2583 goto retry_find;
2584 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002585 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002588 * We have a locked page in the page cache, now we need to check
2589 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 */
Nick Piggind00806b2007-07-19 01:46:57 -07002591 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 goto page_not_uptodate;
2593
Linus Torvaldsef00e082009-06-16 15:31:25 -07002594 /*
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002595 * We've made it this far and we had to drop our mmap_sem, now is the
2596 * time to return to the upper layer and have it re-find the vma and
2597 * redo the fault.
2598 */
2599 if (fpin) {
2600 unlock_page(page);
2601 goto out_retry;
2602 }
2603
2604 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002605 * Found the page and have a reference on it.
2606 * We must recheck i_size under page lock.
2607 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002608 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2609 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002610 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002611 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002612 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002613 }
2614
Nick Piggind0217ac2007-07-19 01:47:03 -07002615 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002616 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
2620 * Umm, take care of errors if the page isn't up-to-date.
2621 * Try to re-read it _once_. We do this synchronously,
2622 * because there really aren't any performance issues here
2623 * and we need to check for errors.
2624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 ClearPageError(page);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002626 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002627 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002628 if (!error) {
2629 wait_on_page_locked(page);
2630 if (!PageUptodate(page))
2631 error = -EIO;
2632 }
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002633 if (fpin)
2634 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002635 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002636
2637 if (!error || error == AOP_TRUNCATED_PAGE)
2638 goto retry_find;
2639
2640 /* Things didn't work out. Return zero to tell the mm layer so. */
2641 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002642 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002643
2644out_retry:
2645 /*
2646 * We dropped the mmap_sem, we need to return to the fault handler to
2647 * re-find the vma and come back and find our hopefully still populated
2648 * page.
2649 */
2650 if (page)
2651 put_page(page);
2652 if (fpin)
2653 fput(fpin);
2654 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002655}
2656EXPORT_SYMBOL(filemap_fault);
2657
Jan Kara82b0f8c2016-12-14 15:06:58 -08002658void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002659 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002660{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002661 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002662 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002663 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002664 unsigned long max_idx;
Matthew Wilcox070e8072018-05-17 00:08:30 -04002665 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002666 struct page *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002667
2668 rcu_read_lock();
Matthew Wilcox070e8072018-05-17 00:08:30 -04002669 xas_for_each(&xas, page, end_pgoff) {
2670 if (xas_retry(&xas, page))
2671 continue;
2672 if (xa_is_value(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002673 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002674
Michal Hockoe0975b22018-12-28 00:38:36 -08002675 /*
2676 * Check for a locked page first, as a speculative
2677 * reference may adversely influence page migration.
2678 */
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002679 if (PageLocked(page))
Michal Hockoe0975b22018-12-28 00:38:36 -08002680 goto next;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002681 if (!page_cache_get_speculative(page))
Matthew Wilcox070e8072018-05-17 00:08:30 -04002682 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002683
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002684 /* Has the page moved or been split? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002685 if (unlikely(page != xas_reload(&xas)))
2686 goto skip;
Matthew Wilcox5fd4ca22019-05-13 17:16:44 -07002687 page = find_subpage(page, xas.xa_index);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002688
2689 if (!PageUptodate(page) ||
2690 PageReadahead(page) ||
2691 PageHWPoison(page))
2692 goto skip;
2693 if (!trylock_page(page))
2694 goto skip;
2695
2696 if (page->mapping != mapping || !PageUptodate(page))
2697 goto unlock;
2698
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002699 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2700 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002701 goto unlock;
2702
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002703 if (file->f_ra.mmap_miss > 0)
2704 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002705
Matthew Wilcox070e8072018-05-17 00:08:30 -04002706 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002707 if (vmf->pte)
Matthew Wilcox070e8072018-05-17 00:08:30 -04002708 vmf->pte += xas.xa_index - last_pgoff;
2709 last_pgoff = xas.xa_index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002710 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002711 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002712 unlock_page(page);
2713 goto next;
2714unlock:
2715 unlock_page(page);
2716skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002717 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002718next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002719 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002720 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002721 break;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002722 }
2723 rcu_read_unlock();
2724}
2725EXPORT_SYMBOL(filemap_map_pages);
2726
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002727vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002728{
2729 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002730 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002731 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002732
Jan Kara14da9202012-06-12 16:20:37 +02002733 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002734 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002735 lock_page(page);
2736 if (page->mapping != inode->i_mapping) {
2737 unlock_page(page);
2738 ret = VM_FAULT_NOPAGE;
2739 goto out;
2740 }
Jan Kara14da9202012-06-12 16:20:37 +02002741 /*
2742 * We mark the page dirty already here so that when freeze is in
2743 * progress, we are guaranteed that writeback during freezing will
2744 * see the dirty page and writeprotect it again.
2745 */
2746 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002747 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002748out:
Jan Kara14da9202012-06-12 16:20:37 +02002749 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002750 return ret;
2751}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002752
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002753const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002754 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002755 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002756 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757};
2758
2759/* This is used for a general mmap of a disk file */
2760
2761int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2762{
2763 struct address_space *mapping = file->f_mapping;
2764
2765 if (!mapping->a_ops->readpage)
2766 return -ENOEXEC;
2767 file_accessed(file);
2768 vma->vm_ops = &generic_file_vm_ops;
2769 return 0;
2770}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772/*
2773 * This is for filesystems which do not implement ->writepage.
2774 */
2775int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2776{
2777 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2778 return -EINVAL;
2779 return generic_file_mmap(file, vma);
2780}
2781#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002782vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07002783{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002784 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07002785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2787{
2788 return -ENOSYS;
2789}
2790int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2791{
2792 return -ENOSYS;
2793}
2794#endif /* CONFIG_MMU */
2795
Arnd Bergmann453972282018-04-13 15:35:27 -07002796EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797EXPORT_SYMBOL(generic_file_mmap);
2798EXPORT_SYMBOL(generic_file_readonly_mmap);
2799
Sasha Levin67f9fd92014-04-03 14:48:18 -07002800static struct page *wait_on_page_read(struct page *page)
2801{
2802 if (!IS_ERR(page)) {
2803 wait_on_page_locked(page);
2804 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002805 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002806 page = ERR_PTR(-EIO);
2807 }
2808 }
2809 return page;
2810}
2811
Mel Gorman32b63522016-03-15 14:55:36 -07002812static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002813 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002814 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002815 void *data,
2816 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Nick Piggineb2be182007-10-16 01:24:57 -07002818 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int err;
2820repeat:
2821 page = find_get_page(mapping, index);
2822 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002823 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002824 if (!page)
2825 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002826 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002827 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002828 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002829 if (err == -EEXIST)
2830 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05002831 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return ERR_PTR(err);
2833 }
Mel Gorman32b63522016-03-15 14:55:36 -07002834
2835filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 err = filler(data, page);
2837 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002838 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002839 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Mel Gorman32b63522016-03-15 14:55:36 -07002841
2842 page = wait_on_page_read(page);
2843 if (IS_ERR(page))
2844 return page;
2845 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (PageUptodate(page))
2848 goto out;
2849
Mel Gormanebded022016-03-15 14:55:39 -07002850 /*
2851 * Page is not up to date and may be locked due one of the following
2852 * case a: Page is being filled and the page lock is held
2853 * case b: Read/write error clearing the page uptodate status
2854 * case c: Truncation in progress (page locked)
2855 * case d: Reclaim in progress
2856 *
2857 * Case a, the page will be up to date when the page is unlocked.
2858 * There is no need to serialise on the page lock here as the page
2859 * is pinned so the lock gives no additional protection. Even if the
2860 * the page is truncated, the data is still valid if PageUptodate as
2861 * it's a race vs truncate race.
2862 * Case b, the page will not be up to date
2863 * Case c, the page may be truncated but in itself, the data may still
2864 * be valid after IO completes as it's a read vs truncate race. The
2865 * operation must restart if the page is not uptodate on unlock but
2866 * otherwise serialising on page lock to stabilise the mapping gives
2867 * no additional guarantees to the caller as the page lock is
2868 * released before return.
2869 * Case d, similar to truncation. If reclaim holds the page lock, it
2870 * will be a race with remove_mapping that determines if the mapping
2871 * is valid on unlock but otherwise the data is valid and there is
2872 * no need to serialise with page lock.
2873 *
2874 * As the page lock gives no additional guarantee, we optimistically
2875 * wait on the page to be unlocked and check if it's up to date and
2876 * use the page if it is. Otherwise, the page lock is required to
2877 * distinguish between the different cases. The motivation is that we
2878 * avoid spurious serialisations and wakeups when multiple processes
2879 * wait on the same page for IO to complete.
2880 */
2881 wait_on_page_locked(page);
2882 if (PageUptodate(page))
2883 goto out;
2884
2885 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002887
2888 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (!page->mapping) {
2890 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002891 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002892 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
Mel Gormanebded022016-03-15 14:55:39 -07002894
2895 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (PageUptodate(page)) {
2897 unlock_page(page);
2898 goto out;
2899 }
Mel Gorman32b63522016-03-15 14:55:36 -07002900 goto filler;
2901
David Howellsc855ff32007-05-09 13:42:20 +01002902out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002903 mark_page_accessed(page);
2904 return page;
2905}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002906
2907/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002908 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002909 * @mapping: the page's address_space
2910 * @index: the page index
2911 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002912 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002913 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002914 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002915 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002916 *
2917 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002918 *
2919 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002920 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002921struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002922 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002923 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002924 void *data)
2925{
2926 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2927}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002928EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002929
2930/**
2931 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2932 * @mapping: the page's address_space
2933 * @index: the page index
2934 * @gfp: the page allocator flags to use if allocating
2935 *
2936 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002937 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002938 *
2939 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08002940 *
2941 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002942 */
2943struct page *read_cache_page_gfp(struct address_space *mapping,
2944 pgoff_t index,
2945 gfp_t gfp)
2946{
2947 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2948
Sasha Levin67f9fd92014-04-03 14:48:18 -07002949 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002950}
2951EXPORT_SYMBOL(read_cache_page_gfp);
2952
Nick Piggin2f718ff2007-10-16 01:24:59 -07002953/*
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002954 * Don't operate on ranges the page cache doesn't support, and don't exceed the
2955 * LFS limits. If pos is under the limit it becomes a short access. If it
2956 * exceeds the limit we return -EFBIG.
2957 */
2958static int generic_access_check_limits(struct file *file, loff_t pos,
2959 loff_t *count)
2960{
2961 struct inode *inode = file->f_mapping->host;
2962 loff_t max_size = inode->i_sb->s_maxbytes;
2963
2964 if (!(file->f_flags & O_LARGEFILE))
2965 max_size = MAX_NON_LFS;
2966
2967 if (unlikely(pos >= max_size))
2968 return -EFBIG;
2969 *count = min(*count, max_size - pos);
2970 return 0;
2971}
2972
2973static int generic_write_check_limits(struct file *file, loff_t pos,
2974 loff_t *count)
2975{
2976 loff_t limit = rlimit(RLIMIT_FSIZE);
2977
2978 if (limit != RLIM_INFINITY) {
2979 if (pos >= limit) {
2980 send_sig(SIGXFSZ, current, 0);
2981 return -EFBIG;
2982 }
2983 *count = min(*count, limit - pos);
2984 }
2985
2986 return generic_access_check_limits(file, pos, count);
2987}
2988
2989/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 * Performs necessary checks before doing a write
2991 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002992 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * Returns appropriate error code that caller should return or
2994 * zero in case that write should be allowed.
2995 */
Al Viro3309dd02015-04-09 12:55:47 -04002996inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Al Viro3309dd02015-04-09 12:55:47 -04002998 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct inode *inode = file->f_mapping->host;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003000 loff_t count;
3001 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Al Viro3309dd02015-04-09 12:55:47 -04003003 if (!iov_iter_count(from))
3004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Al Viro0fa6b002015-04-04 04:05:48 -04003006 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04003007 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04003008 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003010 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
3011 return -EINVAL;
3012
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003013 count = iov_iter_count(from);
3014 ret = generic_write_check_limits(file, iocb->ki_pos, &count);
3015 if (ret)
3016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003018 iov_iter_truncate(from, count);
Al Viro3309dd02015-04-09 12:55:47 -04003019 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021EXPORT_SYMBOL(generic_write_checks);
3022
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003023/*
3024 * Performs necessary checks before doing a clone.
3025 *
3026 * Can adjust amount of bytes to clone.
3027 * Returns appropriate error code that caller should return or
3028 * zero in case the clone should be allowed.
3029 */
3030int generic_remap_checks(struct file *file_in, loff_t pos_in,
3031 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003032 loff_t *req_count, unsigned int remap_flags)
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003033{
3034 struct inode *inode_in = file_in->f_mapping->host;
3035 struct inode *inode_out = file_out->f_mapping->host;
3036 uint64_t count = *req_count;
3037 uint64_t bcount;
3038 loff_t size_in, size_out;
3039 loff_t bs = inode_out->i_sb->s_blocksize;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003040 int ret;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003041
3042 /* The start of both ranges must be aligned to an fs block. */
3043 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
3044 return -EINVAL;
3045
3046 /* Ensure offsets don't wrap. */
3047 if (pos_in + count < pos_in || pos_out + count < pos_out)
3048 return -EINVAL;
3049
3050 size_in = i_size_read(inode_in);
3051 size_out = i_size_read(inode_out);
3052
3053 /* Dedupe requires both ranges to be within EOF. */
Darrick J. Wong3d281932018-10-30 10:41:34 +11003054 if ((remap_flags & REMAP_FILE_DEDUP) &&
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003055 (pos_in >= size_in || pos_in + count > size_in ||
3056 pos_out >= size_out || pos_out + count > size_out))
3057 return -EINVAL;
3058
3059 /* Ensure the infile range is within the infile. */
3060 if (pos_in >= size_in)
3061 return -EINVAL;
3062 count = min(count, size_in - (uint64_t)pos_in);
3063
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11003064 ret = generic_access_check_limits(file_in, pos_in, &count);
3065 if (ret)
3066 return ret;
3067
3068 ret = generic_write_check_limits(file_out, pos_out, &count);
3069 if (ret)
3070 return ret;
3071
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003072 /*
3073 * If the user wanted us to link to the infile's EOF, round up to the
3074 * next block boundary for this check.
3075 *
3076 * Otherwise, make sure the count is also block-aligned, having
3077 * already confirmed the starting offsets' block alignment.
3078 */
3079 if (pos_in + count == size_in) {
3080 bcount = ALIGN(size_in, bs) - pos_in;
3081 } else {
3082 if (!IS_ALIGNED(count, bs))
Darrick J. Wongeca36542018-10-30 10:42:10 +11003083 count = ALIGN_DOWN(count, bs);
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003084 bcount = count;
3085 }
3086
3087 /* Don't allow overlapped cloning within the same file. */
3088 if (inode_in == inode_out &&
3089 pos_out + bcount > pos_in &&
3090 pos_out < pos_in + bcount)
3091 return -EINVAL;
3092
Darrick J. Wongeca36542018-10-30 10:42:10 +11003093 /*
3094 * We shortened the request but the caller can't deal with that, so
3095 * bounce the request back to userspace.
3096 */
3097 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003098 return -EINVAL;
3099
Darrick J. Wongeca36542018-10-30 10:42:10 +11003100 *req_count = count;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003101 return 0;
3102}
3103
Nick Pigginafddba42007-10-16 01:25:01 -07003104int pagecache_write_begin(struct file *file, struct address_space *mapping,
3105 loff_t pos, unsigned len, unsigned flags,
3106 struct page **pagep, void **fsdata)
3107{
3108 const struct address_space_operations *aops = mapping->a_ops;
3109
Nick Piggin4e02ed42008-10-29 14:00:55 -07003110 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003111 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003112}
3113EXPORT_SYMBOL(pagecache_write_begin);
3114
3115int pagecache_write_end(struct file *file, struct address_space *mapping,
3116 loff_t pos, unsigned len, unsigned copied,
3117 struct page *page, void *fsdata)
3118{
3119 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003120
Nick Piggin4e02ed42008-10-29 14:00:55 -07003121 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003122}
3123EXPORT_SYMBOL(pagecache_write_end);
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003126generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 struct file *file = iocb->ki_filp;
3129 struct address_space *mapping = file->f_mapping;
3130 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003131 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003133 size_t write_len;
3134 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Al Viro0c949332014-03-22 06:51:37 -04003136 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003137 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003138
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003139 if (iocb->ki_flags & IOCB_NOWAIT) {
3140 /* If there are pages to writeback, return */
3141 if (filemap_range_has_page(inode->i_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003142 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003143 return -EAGAIN;
3144 } else {
3145 written = filemap_write_and_wait_range(mapping, pos,
3146 pos + write_len - 1);
3147 if (written)
3148 goto out;
3149 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003150
3151 /*
3152 * After a write we want buffered reads to be sure to go to disk to get
3153 * the new data. We invalidate clean cached page from the region we're
3154 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003155 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003156 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003157 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003158 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003159 /*
3160 * If a page can not be invalidated, return 0 to fall back
3161 * to buffered write.
3162 */
3163 if (written) {
3164 if (written == -EBUSY)
3165 return 0;
3166 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003167 }
3168
Al Viro639a93a52017-04-13 14:10:15 -04003169 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003170
3171 /*
3172 * Finally, try again to invalidate clean pages which might have been
3173 * cached by non-direct readahead, or faulted in by get_user_pages()
3174 * if the source of the write was an mmap'ed region of the file
3175 * we're writing. Either one is a pretty crazy thing to do,
3176 * so we don't support it 100%. If this invalidation
3177 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003178 *
3179 * Most of the time we do not need this since dio_complete() will do
3180 * the invalidation for us. However there are some file systems that
3181 * do not end up with dio_complete() being called, so let's not break
3182 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003183 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003184 if (mapping->nrpages)
3185 invalidate_inode_pages2_range(mapping,
3186 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003189 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003190 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003191 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3192 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 mark_inode_dirty(inode);
3194 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003195 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
Al Viro639a93a52017-04-13 14:10:15 -04003197 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003198out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return written;
3200}
3201EXPORT_SYMBOL(generic_file_direct_write);
3202
Nick Piggineb2be182007-10-16 01:24:57 -07003203/*
3204 * Find or create a page at the given pagecache position. Return the locked
3205 * page. This function is specifically for buffered writes.
3206 */
Nick Piggin54566b22009-01-04 12:00:53 -08003207struct page *grab_cache_page_write_begin(struct address_space *mapping,
3208 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003209{
Nick Piggineb2be182007-10-16 01:24:57 -07003210 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003211 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003212
Nick Piggin54566b22009-01-04 12:00:53 -08003213 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003214 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003215
Mel Gorman2457aec2014-06-04 16:10:31 -07003216 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003217 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003218 if (page)
3219 wait_for_stable_page(page);
3220
Nick Piggineb2be182007-10-16 01:24:57 -07003221 return page;
3222}
Nick Piggin54566b22009-01-04 12:00:53 -08003223EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003224
Al Viro3b93f912014-02-11 21:34:08 -05003225ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003226 struct iov_iter *i, loff_t pos)
3227{
3228 struct address_space *mapping = file->f_mapping;
3229 const struct address_space_operations *a_ops = mapping->a_ops;
3230 long status = 0;
3231 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003232 unsigned int flags = 0;
3233
Nick Pigginafddba42007-10-16 01:25:01 -07003234 do {
3235 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003236 unsigned long offset; /* Offset into pagecache page */
3237 unsigned long bytes; /* Bytes to write to page */
3238 size_t copied; /* Bytes copied from user */
3239 void *fsdata;
3240
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003241 offset = (pos & (PAGE_SIZE - 1));
3242 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003243 iov_iter_count(i));
3244
3245again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003246 /*
3247 * Bring in the user page that we will copy from _first_.
3248 * Otherwise there's a nasty deadlock on copying from the
3249 * same page as we're writing to, without it being marked
3250 * up-to-date.
3251 *
3252 * Not only is this an optimisation, but it is also required
3253 * to check that the address is actually valid, when atomic
3254 * usercopies are used, below.
3255 */
3256 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3257 status = -EFAULT;
3258 break;
3259 }
3260
Jan Kara296291c2015-10-22 13:32:21 -07003261 if (fatal_signal_pending(current)) {
3262 status = -EINTR;
3263 break;
3264 }
3265
Nick Piggin674b8922007-10-16 01:25:03 -07003266 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003267 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003268 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003269 break;
3270
anfei zhou931e80e2010-02-02 13:44:02 -08003271 if (mapping_writably_mapped(mapping))
3272 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003273
Nick Pigginafddba42007-10-16 01:25:01 -07003274 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003275 flush_dcache_page(page);
3276
3277 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3278 page, fsdata);
3279 if (unlikely(status < 0))
3280 break;
3281 copied = status;
3282
3283 cond_resched();
3284
Nick Piggin124d3b72008-02-02 15:01:17 +01003285 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003286 if (unlikely(copied == 0)) {
3287 /*
3288 * If we were unable to copy any data at all, we must
3289 * fall back to a single segment length write.
3290 *
3291 * If we didn't fallback here, we could livelock
3292 * because not all segments in the iov can be copied at
3293 * once without a pagefault.
3294 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003295 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003296 iov_iter_single_seg_count(i));
3297 goto again;
3298 }
Nick Pigginafddba42007-10-16 01:25:01 -07003299 pos += copied;
3300 written += copied;
3301
3302 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003303 } while (iov_iter_count(i));
3304
3305 return written ? written : status;
3306}
Al Viro3b93f912014-02-11 21:34:08 -05003307EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Jan Karae4dd9de2009-08-17 18:10:06 +02003309/**
Al Viro81742022014-04-03 03:17:43 -04003310 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003311 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003312 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003313 *
3314 * This function does all the work needed for actually writing data to a
3315 * file. It does all basic checks, removes SUID from the file, updates
3316 * modification times and calls proper subroutines depending on whether we
3317 * do direct IO or a standard buffered write.
3318 *
3319 * It expects i_mutex to be grabbed unless we work on a block device or similar
3320 * object which does not need locking at all.
3321 *
3322 * This function does *not* take care of syncing data in case of O_SYNC write.
3323 * A caller has to handle it. This is mainly due to the fact that we want to
3324 * avoid syncing under i_mutex.
Mike Rapoporta862f682019-03-05 15:48:42 -08003325 *
3326 * Return:
3327 * * number of bytes written, even for truncated writes
3328 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003329 */
Al Viro81742022014-04-03 03:17:43 -04003330ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003333 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003335 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003337 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003340 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003341 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (err)
3343 goto out;
3344
Josef Bacikc3b2da32012-03-26 09:59:21 -04003345 err = file_update_time(file);
3346 if (err)
3347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Al Viro2ba48ce2015-04-09 13:52:01 -04003349 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003350 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003351
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003352 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003353 /*
3354 * If the write stopped short of completing, fall back to
3355 * buffered writes. Some filesystems do this for writes to
3356 * holes, for example. For DAX files, a buffered write will
3357 * not succeed (even if it did, DAX does not handle dirty
3358 * page-cache pages correctly).
3359 */
Al Viro0b8def92015-04-07 10:22:53 -04003360 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003362
Al Viro0b8def92015-04-07 10:22:53 -04003363 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003364 /*
Al Viro3b93f912014-02-11 21:34:08 -05003365 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003366 * then we want to return the number of bytes which were
3367 * direct-written, or the error code if that was zero. Note
3368 * that this differs from normal direct-io semantics, which
3369 * will return -EFOO even if some bytes were written.
3370 */
Al Viro60bb4522014-08-08 12:39:16 -04003371 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003372 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003373 goto out;
3374 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003375 /*
3376 * We need to ensure that the page cache pages are written to
3377 * disk and invalidated to preserve the expected O_DIRECT
3378 * semantics.
3379 */
Al Viro3b93f912014-02-11 21:34:08 -05003380 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003381 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003382 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003383 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003384 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003385 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003386 pos >> PAGE_SHIFT,
3387 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003388 } else {
3389 /*
3390 * We don't know how much we wrote, so just return
3391 * the number of bytes which were direct-written
3392 */
3393 }
3394 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003395 written = generic_perform_write(file, from, iocb->ki_pos);
3396 if (likely(written > 0))
3397 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399out:
3400 current->backing_dev_info = NULL;
3401 return written ? written : err;
3402}
Al Viro81742022014-04-03 03:17:43 -04003403EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Jan Karae4dd9de2009-08-17 18:10:06 +02003405/**
Al Viro81742022014-04-03 03:17:43 -04003406 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003407 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003408 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003409 *
Al Viro81742022014-04-03 03:17:43 -04003410 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003411 * filesystems. It takes care of syncing the file in case of O_SYNC file
3412 * and acquires i_mutex as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003413 * Return:
3414 * * negative error code if no data has been written at all of
3415 * vfs_fsync_range() failed for a synchronous write
3416 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003417 */
Al Viro81742022014-04-03 03:17:43 -04003418ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
3420 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003421 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Al Viro59551022016-01-22 15:40:57 -05003424 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003425 ret = generic_write_checks(iocb, from);
3426 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003427 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003428 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Christoph Hellwige2592212016-04-07 08:52:01 -07003430 if (ret > 0)
3431 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return ret;
3433}
Al Viro81742022014-04-03 03:17:43 -04003434EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
David Howellscf9a2ae2006-08-29 19:05:54 +01003436/**
3437 * try_to_release_page() - release old fs-specific metadata on a page
3438 *
3439 * @page: the page which the kernel is trying to free
3440 * @gfp_mask: memory allocation flags (and I/O mode)
3441 *
3442 * The address_space is to try to release any data against the page
Mike Rapoporta862f682019-03-05 15:48:42 -08003443 * (presumably at page->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003444 *
David Howells266cf652009-04-03 16:42:36 +01003445 * This may also be called if PG_fscache is set on a page, indicating that the
3446 * page is known to the local caching routines.
3447 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003448 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003449 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003450 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003451 * Return: %1 if the release was successful, otherwise return zero.
David Howellscf9a2ae2006-08-29 19:05:54 +01003452 */
3453int try_to_release_page(struct page *page, gfp_t gfp_mask)
3454{
3455 struct address_space * const mapping = page->mapping;
3456
3457 BUG_ON(!PageLocked(page));
3458 if (PageWriteback(page))
3459 return 0;
3460
3461 if (mapping && mapping->a_ops->releasepage)
3462 return mapping->a_ops->releasepage(page, gfp_mask);
3463 return try_to_free_buffers(page);
3464}
3465
3466EXPORT_SYMBOL(try_to_release_page);