blob: 8cec52968e834fb5c01bb7b81b162894ed389129 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/filemap.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
11 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080014#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070017#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/mman.h>
24#include <linux/pagemap.h>
25#include <linux/file.h>
26#include <linux/uio.h>
27#include <linux/hash.h>
28#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070029#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/pagevec.h>
31#include <linux/blkdev.h>
32#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080033#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070034#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060036#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080037#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070038#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070039#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070040#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080041#include "internal.h"
42
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070043#define CREATE_TRACE_POINTS
44#include <trace/events/filemap.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * FIXME: remove all knowledge of the buffer layer from the core VM
48 */
Jan Kara148f9482009-08-17 19:52:36 +020049#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/mman.h>
52
53/*
54 * Shared mappings implemented 30.11.1994. It's not fully working yet,
55 * though.
56 *
57 * Shared mappings now work. 15.8.1995 Bruno.
58 *
59 * finished 'unifying' the page and buffer cache and SMP-threaded the
60 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
61 *
62 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
63 */
64
65/*
66 * Lock ordering:
67 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080068 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070070 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070071 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080073 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080074 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080077 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070078 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070079 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
81 * ->mmap_sem
82 * ->lock_page (access_process_vm)
83 *
Al Viroccad2362014-02-11 22:36:48 -050084 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070085 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060087 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110088 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070089 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080091 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ->anon_vma.lock (vma_adjust)
93 *
94 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070095 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070097 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070098 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700100 * ->i_pages lock (try_to_unmap_one)
Mel Gormana52633d2016-07-28 15:45:28 -0700101 * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
102 * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700104 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600105 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100106 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700107 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600108 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100109 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
111 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800112 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700113 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500116static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700117 struct page *page, void *shadow)
118{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500119 XA_STATE(xas, &mapping->i_pages, page->index);
120 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800121
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500122 mapping_set_update(&xas, mapping);
123
124 /* hugetlb pages are represented by a single entry in the xarray */
125 if (!PageHuge(page)) {
126 xas_set_order(&xas, page->index, compound_order(page));
127 nr = 1U << compound_order(page);
128 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700129
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700130 VM_BUG_ON_PAGE(!PageLocked(page), page);
131 VM_BUG_ON_PAGE(PageTail(page), page);
132 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700133
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500134 xas_store(&xas, shadow);
135 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200136
Jan Kara23006382017-11-15 17:37:26 -0800137 page->mapping = NULL;
138 /* Leave page->index set: truncation lookup relies upon it */
139
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200140 if (shadow) {
141 mapping->nrexceptional += nr;
142 /*
143 * Make sure the nrexceptional update is committed before
144 * the nrpages update so that final truncate racing
145 * with reclaim does not see both counters 0 at the
146 * same time and miss a shadow entry.
147 */
148 smp_wmb();
149 }
150 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700151}
152
Jan Kara5ecc4d82017-11-15 17:37:29 -0800153static void unaccount_page_cache_page(struct address_space *mapping,
154 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800156 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600158 /*
159 * if we're uptodate, flush out into the cleancache, otherwise
160 * invalidate any existing cleancache entries. We can't leave
161 * stale data around in the cleancache once our page is gone
162 */
163 if (PageUptodate(page) && PageMappedToDisk(page))
164 cleancache_put_page(page);
165 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400166 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600167
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700168 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800169 VM_BUG_ON_PAGE(page_mapped(page), page);
170 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
171 int mapcount;
172
173 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
174 current->comm, page_to_pfn(page));
175 dump_page(page, "still mapped when deleted");
176 dump_stack();
177 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
178
179 mapcount = page_mapcount(page);
180 if (mapping_exiting(mapping) &&
181 page_count(page) >= mapcount + 2) {
182 /*
183 * All vmas have already been torn down, so it's
184 * a good bet that actually the page is unmapped,
185 * and we'd prefer not to leak it: if we're wrong,
186 * some other bad page check should catch it later.
187 */
188 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700189 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800190 }
191 }
192
Jan Kara76253fb2017-11-15 17:37:22 -0800193 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800194 if (PageHuge(page))
195 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800196
Jan Kara5ecc4d82017-11-15 17:37:29 -0800197 nr = hpage_nr_pages(page);
198
199 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
200 if (PageSwapBacked(page)) {
201 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
202 if (PageTransHuge(page))
203 __dec_node_page_state(page, NR_SHMEM_THPS);
204 } else {
205 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800206 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800207
208 /*
209 * At this point page must be either written or cleaned by
210 * truncate. Dirty page here signals a bug and loss of
211 * unwritten data.
212 *
213 * This fixes dirty accounting after removing the page entirely
214 * but leaves PageDirty set: it has no effect for truncated
215 * page and anyway will be cleared before returning page into
216 * buddy allocator.
217 */
218 if (WARN_ON_ONCE(PageDirty(page)))
219 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
220}
221
222/*
223 * Delete a page from the page cache and free it. Caller has to make
224 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700225 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800226 */
227void __delete_from_page_cache(struct page *page, void *shadow)
228{
229 struct address_space *mapping = page->mapping;
230
231 trace_mm_filemap_delete_from_page_cache(page);
232
233 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500234 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Jan Kara59c66c52017-11-15 17:37:18 -0800237static void page_cache_free_page(struct address_space *mapping,
238 struct page *page)
239{
240 void (*freepage)(struct page *);
241
242 freepage = mapping->a_ops->freepage;
243 if (freepage)
244 freepage(page);
245
246 if (PageTransHuge(page) && !PageHuge(page)) {
247 page_ref_sub(page, HPAGE_PMD_NR);
248 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
249 } else {
250 put_page(page);
251 }
252}
253
Minchan Kim702cfbf2011-03-22 16:32:43 -0700254/**
255 * delete_from_page_cache - delete page from page cache
256 * @page: the page which the kernel is trying to remove from page cache
257 *
258 * This must be called only on pages that have been verified to be in the page
259 * cache and locked. It will never put the page into the free list, the caller
260 * has a reference on the page.
261 */
262void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700264 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400265 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Matt Mackallcd7619d2005-05-01 08:59:01 -0700267 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700268 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700269 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700270 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500271
Jan Kara59c66c52017-11-15 17:37:18 -0800272 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700273}
274EXPORT_SYMBOL(delete_from_page_cache);
275
Jan Karaaa65c292017-11-15 17:37:33 -0800276/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500277 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800278 * @mapping: the mapping to which pages belong
279 * @pvec: pagevec with pages to delete
280 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700281 * The function walks over mapping->i_pages and removes pages passed in @pvec
282 * from the mapping. The function expects @pvec to be sorted by page index.
283 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800284 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700285 * @pvec and takes care to delete all corresponding tail pages from the
286 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800287 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700288 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800289 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500290static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800291 struct pagevec *pvec)
292{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500293 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800294 int total_pages = 0;
295 int i = 0, tail_pages = 0;
296 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800297
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500298 mapping_set_update(&xas, mapping);
299 xas_for_each(&xas, page, ULONG_MAX) {
Jan Karaaa65c292017-11-15 17:37:33 -0800300 if (i >= pagevec_count(pvec) && !tail_pages)
301 break;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400302 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800303 continue;
304 if (!tail_pages) {
305 /*
306 * Some page got inserted in our range? Skip it. We
307 * have our pages locked so they are protected from
308 * being removed.
309 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500310 if (page != pvec->pages[i]) {
311 VM_BUG_ON_PAGE(page->index >
312 pvec->pages[i]->index, page);
Jan Karaaa65c292017-11-15 17:37:33 -0800313 continue;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500314 }
Jan Karaaa65c292017-11-15 17:37:33 -0800315 WARN_ON_ONCE(!PageLocked(page));
316 if (PageTransHuge(page) && !PageHuge(page))
317 tail_pages = HPAGE_PMD_NR - 1;
318 page->mapping = NULL;
319 /*
320 * Leave page->index set: truncation lookup relies
321 * upon it
322 */
323 i++;
324 } else {
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500325 VM_BUG_ON_PAGE(page->index + HPAGE_PMD_NR - tail_pages
326 != pvec->pages[i]->index, page);
Jan Karaaa65c292017-11-15 17:37:33 -0800327 tail_pages--;
328 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500329 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800330 total_pages++;
331 }
332 mapping->nrpages -= total_pages;
333}
334
335void delete_from_page_cache_batch(struct address_space *mapping,
336 struct pagevec *pvec)
337{
338 int i;
339 unsigned long flags;
340
341 if (!pagevec_count(pvec))
342 return;
343
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700344 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800345 for (i = 0; i < pagevec_count(pvec); i++) {
346 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
347
348 unaccount_page_cache_page(mapping, pvec->pages[i]);
349 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500350 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700351 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800352
353 for (i = 0; i < pagevec_count(pvec); i++)
354 page_cache_free_page(mapping, pvec->pages[i]);
355}
356
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200357int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700358{
359 int ret = 0;
360 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700361 if (test_bit(AS_ENOSPC, &mapping->flags) &&
362 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700363 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700364 if (test_bit(AS_EIO, &mapping->flags) &&
365 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700366 ret = -EIO;
367 return ret;
368}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200369EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700370
Jeff Layton76341ca2017-07-06 07:02:22 -0400371static int filemap_check_and_keep_errors(struct address_space *mapping)
372{
373 /* Check for outstanding write errors */
374 if (test_bit(AS_EIO, &mapping->flags))
375 return -EIO;
376 if (test_bit(AS_ENOSPC, &mapping->flags))
377 return -ENOSPC;
378 return 0;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700382 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700383 * @mapping: address space structure to write
384 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800385 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700386 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700388 * Start writeback against all of a mapping's dirty pages that lie
389 * within the byte offsets <start, end> inclusive.
390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700392 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * these two operations is that if a dirty page/buffer is encountered, it must
394 * be waited upon, and not just skipped over.
395 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800396int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
397 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 int ret;
400 struct writeback_control wbc = {
401 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800402 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700403 .range_start = start,
404 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 };
406
407 if (!mapping_cap_writeback_dirty(mapping))
408 return 0;
409
Tejun Heob16b1de2015-06-02 08:39:48 -0600410 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600412 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return ret;
414}
415
416static inline int __filemap_fdatawrite(struct address_space *mapping,
417 int sync_mode)
418{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700419 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422int filemap_fdatawrite(struct address_space *mapping)
423{
424 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
425}
426EXPORT_SYMBOL(filemap_fdatawrite);
427
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400428int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800429 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
432}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400433EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Randy Dunlap485bb992006-06-23 02:03:49 -0700435/**
436 * filemap_flush - mostly a non-blocking flush
437 * @mapping: target address_space
438 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * This is a mostly non-blocking flush. Not suitable for data-integrity
440 * purposes - I/O may not be started against all dirty pages.
441 */
442int filemap_flush(struct address_space *mapping)
443{
444 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
445}
446EXPORT_SYMBOL(filemap_flush);
447
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500448/**
449 * filemap_range_has_page - check if a page exists in range.
450 * @mapping: address space within which to check
451 * @start_byte: offset in bytes where the range starts
452 * @end_byte: offset in bytes where the range ends (inclusive)
453 *
454 * Find at least one page in the range supplied, usually used to check if
455 * direct writing in this range will trigger a writeback.
456 */
457bool filemap_range_has_page(struct address_space *mapping,
458 loff_t start_byte, loff_t end_byte)
459{
Jan Karaf7b68042017-09-06 16:21:40 -0700460 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500461 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
462 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500463
464 if (end_byte < start_byte)
465 return false;
466
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500467 rcu_read_lock();
468 for (;;) {
469 page = xas_find(&xas, max);
470 if (xas_retry(&xas, page))
471 continue;
472 /* Shadow entries don't count */
473 if (xa_is_value(page))
474 continue;
475 /*
476 * We don't need to try to pin this page; we're about to
477 * release the RCU lock anyway. It is enough to know that
478 * there was a page here recently.
479 */
480 break;
481 }
482 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500483
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500484 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500485}
486EXPORT_SYMBOL(filemap_range_has_page);
487
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400488static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800489 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300491 pgoff_t index = start_byte >> PAGE_SHIFT;
492 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct pagevec pvec;
494 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200496 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Mel Gorman86679822017-11-15 17:37:52 -0800499 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800500 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned i;
502
Jan Kara312e9d22017-11-15 17:35:05 -0800503 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800504 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800505 if (!nr_pages)
506 break;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 for (i = 0; i < nr_pages; i++) {
509 struct page *page = pvec.pages[i];
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400512 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 pagevec_release(&pvec);
515 cond_resched();
516 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800517}
518
519/**
520 * filemap_fdatawait_range - wait for writeback to complete
521 * @mapping: address space structure to wait for
522 * @start_byte: offset in bytes where the range starts
523 * @end_byte: offset in bytes where the range ends (inclusive)
524 *
525 * Walk the list of under-writeback pages of the given address space
526 * in the given range and wait for all of them. Check error status of
527 * the address space and return it.
528 *
529 * Since the error status of the address space is cleared by this function,
530 * callers are responsible for checking the return value and handling and/or
531 * reporting the error.
532 */
533int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
534 loff_t end_byte)
535{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400536 __filemap_fdatawait_range(mapping, start_byte, end_byte);
537 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200539EXPORT_SYMBOL(filemap_fdatawait_range);
540
541/**
Jeff Laytona823e452017-07-28 07:24:43 -0400542 * file_fdatawait_range - wait for writeback to complete
543 * @file: file pointing to address space structure to wait for
544 * @start_byte: offset in bytes where the range starts
545 * @end_byte: offset in bytes where the range ends (inclusive)
546 *
547 * Walk the list of under-writeback pages of the address space that file
548 * refers to, in the given range and wait for all of them. Check error
549 * status of the address space vs. the file->f_wb_err cursor and return it.
550 *
551 * Since the error status of the file is advanced by this function,
552 * callers are responsible for checking the return value and handling and/or
553 * reporting the error.
554 */
555int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
556{
557 struct address_space *mapping = file->f_mapping;
558
559 __filemap_fdatawait_range(mapping, start_byte, end_byte);
560 return file_check_and_advance_wb_err(file);
561}
562EXPORT_SYMBOL(file_fdatawait_range);
563
564/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800565 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
566 * @mapping: address space structure to wait for
567 *
568 * Walk the list of under-writeback pages of the given address space
569 * and wait for all of them. Unlike filemap_fdatawait(), this function
570 * does not clear error status of the address space.
571 *
572 * Use this function if callers don't handle errors themselves. Expected
573 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
574 * fsfreeze(8)
575 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400576int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800577{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400578 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400579 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800580}
Jeff Layton76341ca2017-07-06 07:02:22 -0400581EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800582
Jeff Layton9326c9b2017-07-26 10:21:11 -0400583static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400585 return (!dax_mapping(mapping) && mapping->nrpages) ||
586 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589int filemap_write_and_wait(struct address_space *mapping)
590{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800591 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Jeff Layton9326c9b2017-07-26 10:21:11 -0400593 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800594 err = filemap_fdatawrite(mapping);
595 /*
596 * Even if the above returned error, the pages may be
597 * written partially (e.g. -ENOSPC), so we wait for it.
598 * But the -EIO is special case, it may indicate the worst
599 * thing (e.g. bug) happened, so we avoid waiting for it.
600 */
601 if (err != -EIO) {
602 int err2 = filemap_fdatawait(mapping);
603 if (!err)
604 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400605 } else {
606 /* Clear any previously stored errors */
607 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800608 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700609 } else {
610 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800614EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Randy Dunlap485bb992006-06-23 02:03:49 -0700616/**
617 * filemap_write_and_wait_range - write out & wait on a file range
618 * @mapping: the address_space for the pages
619 * @lstart: offset in bytes where the range starts
620 * @lend: offset in bytes where the range ends (inclusive)
621 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800622 * Write out and wait upon file offsets lstart->lend, inclusive.
623 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300624 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800625 * that this function can be used to write to the very end-of-file (end = -1).
626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627int filemap_write_and_wait_range(struct address_space *mapping,
628 loff_t lstart, loff_t lend)
629{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800630 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jeff Layton9326c9b2017-07-26 10:21:11 -0400632 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800633 err = __filemap_fdatawrite_range(mapping, lstart, lend,
634 WB_SYNC_ALL);
635 /* See comment of filemap_write_and_wait() */
636 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200637 int err2 = filemap_fdatawait_range(mapping,
638 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800639 if (!err)
640 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400641 } else {
642 /* Clear any previously stored errors */
643 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800644 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700645 } else {
646 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Chris Masonf6995582009-04-15 13:22:37 -0400650EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jeff Layton5660e132017-07-06 07:02:25 -0400652void __filemap_set_wb_err(struct address_space *mapping, int err)
653{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400654 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400655
656 trace_filemap_set_wb_err(mapping, eseq);
657}
658EXPORT_SYMBOL(__filemap_set_wb_err);
659
660/**
661 * file_check_and_advance_wb_err - report wb error (if any) that was previously
662 * and advance wb_err to current one
663 * @file: struct file on which the error is being reported
664 *
665 * When userland calls fsync (or something like nfsd does the equivalent), we
666 * want to report any writeback errors that occurred since the last fsync (or
667 * since the file was opened if there haven't been any).
668 *
669 * Grab the wb_err from the mapping. If it matches what we have in the file,
670 * then just quickly return 0. The file is all caught up.
671 *
672 * If it doesn't match, then take the mapping value, set the "seen" flag in
673 * it and try to swap it into place. If it works, or another task beat us
674 * to it with the new value, then update the f_wb_err and return the error
675 * portion. The error at this point must be reported via proper channels
676 * (a'la fsync, or NFS COMMIT operation, etc.).
677 *
678 * While we handle mapping->wb_err with atomic operations, the f_wb_err
679 * value is protected by the f_lock since we must ensure that it reflects
680 * the latest value swapped in for this file descriptor.
681 */
682int file_check_and_advance_wb_err(struct file *file)
683{
684 int err = 0;
685 errseq_t old = READ_ONCE(file->f_wb_err);
686 struct address_space *mapping = file->f_mapping;
687
688 /* Locklessly handle the common case where nothing has changed */
689 if (errseq_check(&mapping->wb_err, old)) {
690 /* Something changed, must use slow path */
691 spin_lock(&file->f_lock);
692 old = file->f_wb_err;
693 err = errseq_check_and_advance(&mapping->wb_err,
694 &file->f_wb_err);
695 trace_file_check_and_advance_wb_err(file, old);
696 spin_unlock(&file->f_lock);
697 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700698
699 /*
700 * We're mostly using this function as a drop in replacement for
701 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
702 * that the legacy code would have had on these flags.
703 */
704 clear_bit(AS_EIO, &mapping->flags);
705 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400706 return err;
707}
708EXPORT_SYMBOL(file_check_and_advance_wb_err);
709
710/**
711 * file_write_and_wait_range - write out & wait on a file range
712 * @file: file pointing to address_space with pages
713 * @lstart: offset in bytes where the range starts
714 * @lend: offset in bytes where the range ends (inclusive)
715 *
716 * Write out and wait upon file offsets lstart->lend, inclusive.
717 *
718 * Note that @lend is inclusive (describes the last byte to be written) so
719 * that this function can be used to write to the very end-of-file (end = -1).
720 *
721 * After writing out and waiting on the data, we check and advance the
722 * f_wb_err cursor to the latest value, and return any errors detected there.
723 */
724int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
725{
726 int err = 0, err2;
727 struct address_space *mapping = file->f_mapping;
728
Jeff Layton9326c9b2017-07-26 10:21:11 -0400729 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400730 err = __filemap_fdatawrite_range(mapping, lstart, lend,
731 WB_SYNC_ALL);
732 /* See comment of filemap_write_and_wait() */
733 if (err != -EIO)
734 __filemap_fdatawait_range(mapping, lstart, lend);
735 }
736 err2 = file_check_and_advance_wb_err(file);
737 if (!err)
738 err = err2;
739 return err;
740}
741EXPORT_SYMBOL(file_write_and_wait_range);
742
Randy Dunlap485bb992006-06-23 02:03:49 -0700743/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700744 * replace_page_cache_page - replace a pagecache page with a new one
745 * @old: page to be replaced
746 * @new: page to replace with
747 * @gfp_mask: allocation mode
748 *
749 * This function replaces a page in the pagecache with a new one. On
750 * success it acquires the pagecache reference for the new page and
751 * drops it for the old page. Both the old and new pages must be
752 * locked. This function does not add the new page to the LRU, the
753 * caller must do that.
754 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500755 * The remove + add is atomic. This function cannot fail.
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700756 */
757int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
758{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500759 struct address_space *mapping = old->mapping;
760 void (*freepage)(struct page *) = mapping->a_ops->freepage;
761 pgoff_t offset = old->index;
762 XA_STATE(xas, &mapping->i_pages, offset);
763 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700764
Sasha Levin309381fea2014-01-23 15:52:54 -0800765 VM_BUG_ON_PAGE(!PageLocked(old), old);
766 VM_BUG_ON_PAGE(!PageLocked(new), new);
767 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700768
Matthew Wilcox74d60952017-11-17 10:01:45 -0500769 get_page(new);
770 new->mapping = mapping;
771 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700772
Matthew Wilcox74d60952017-11-17 10:01:45 -0500773 xas_lock_irqsave(&xas, flags);
774 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700775
Matthew Wilcox74d60952017-11-17 10:01:45 -0500776 old->mapping = NULL;
777 /* hugetlb pages do not participate in page cache accounting. */
778 if (!PageHuge(old))
779 __dec_node_page_state(new, NR_FILE_PAGES);
780 if (!PageHuge(new))
781 __inc_node_page_state(new, NR_FILE_PAGES);
782 if (PageSwapBacked(old))
783 __dec_node_page_state(new, NR_SHMEM);
784 if (PageSwapBacked(new))
785 __inc_node_page_state(new, NR_SHMEM);
786 xas_unlock_irqrestore(&xas, flags);
787 mem_cgroup_migrate(old, new);
788 if (freepage)
789 freepage(old);
790 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700791
Matthew Wilcox74d60952017-11-17 10:01:45 -0500792 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700793}
794EXPORT_SYMBOL_GPL(replace_page_cache_page);
795
Johannes Weinera5289102014-04-03 14:47:51 -0700796static int __add_to_page_cache_locked(struct page *page,
797 struct address_space *mapping,
798 pgoff_t offset, gfp_t gfp_mask,
799 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500801 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700802 int huge = PageHuge(page);
803 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700804 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500805 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700806
Sasha Levin309381fea2014-01-23 15:52:54 -0800807 VM_BUG_ON_PAGE(!PageLocked(page), page);
808 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500809 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700810
Johannes Weiner00501b52014-08-08 14:19:20 -0700811 if (!huge) {
812 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800813 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700814 if (error)
815 return error;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300818 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700819 page->mapping = mapping;
820 page->index = offset;
821
Matthew Wilcox74d60952017-11-17 10:01:45 -0500822 do {
823 xas_lock_irq(&xas);
824 old = xas_load(&xas);
825 if (old && !xa_is_value(old))
826 xas_set_err(&xas, -EEXIST);
827 xas_store(&xas, page);
828 if (xas_error(&xas))
829 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700830
Matthew Wilcox74d60952017-11-17 10:01:45 -0500831 if (xa_is_value(old)) {
832 mapping->nrexceptional--;
833 if (shadowp)
834 *shadowp = old;
835 }
836 mapping->nrpages++;
837
838 /* hugetlb pages do not participate in page cache accounting */
839 if (!huge)
840 __inc_node_page_state(page, NR_FILE_PAGES);
841unlock:
842 xas_unlock_irq(&xas);
843 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
844
845 if (xas_error(&xas))
846 goto error;
847
Johannes Weiner00501b52014-08-08 14:19:20 -0700848 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800849 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700850 trace_mm_filemap_add_to_page_cache(page);
851 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500852error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700853 page->mapping = NULL;
854 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700855 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800856 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300857 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500858 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
Johannes Weinera5289102014-04-03 14:47:51 -0700860
861/**
862 * add_to_page_cache_locked - add a locked page to the pagecache
863 * @page: page to add
864 * @mapping: the page's address_space
865 * @offset: page index
866 * @gfp_mask: page allocation mode
867 *
868 * This function is used to add a page to the pagecache. It must be locked.
869 * This function does not add the page to the LRU. The caller must do that.
870 */
871int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
872 pgoff_t offset, gfp_t gfp_mask)
873{
874 return __add_to_page_cache_locked(page, mapping, offset,
875 gfp_mask, NULL);
876}
Nick Piggine2867812008-07-25 19:45:30 -0700877EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400880 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Johannes Weinera5289102014-04-03 14:47:51 -0700882 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700883 int ret;
884
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800885 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700886 ret = __add_to_page_cache_locked(page, mapping, offset,
887 gfp_mask, &shadow);
888 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800889 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700890 else {
891 /*
892 * The page might have been evicted from cache only
893 * recently, in which case it should be activated like
894 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700895 * The exception is pages getting rewritten; evicting other
896 * data from the working set, only to cache data that will
897 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700898 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700899 WARN_ON_ONCE(PageActive(page));
900 if (!(gfp_mask & __GFP_WRITE) && shadow)
901 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700902 lru_cache_add(page);
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return ret;
905}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300906EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Paul Jackson44110fe2006-03-24 03:16:04 -0800908#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700909struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800910{
Miao Xiec0ff7452010-05-24 14:32:08 -0700911 int n;
912 struct page *page;
913
Paul Jackson44110fe2006-03-24 03:16:04 -0800914 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700915 unsigned int cpuset_mems_cookie;
916 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700917 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700918 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700919 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700920 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700921
Miao Xiec0ff7452010-05-24 14:32:08 -0700922 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800923 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700924 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800925}
Nick Piggin2ae88142006-10-28 10:38:23 -0700926EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800927#endif
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
930 * In order to wait for pages to become available there must be
931 * waitqueues associated with pages. By using a hash table of
932 * waitqueues where the bucket discipline is to maintain all
933 * waiters on the same queue and wake all when any of the pages
934 * become available, and for the woken contexts to check to be
935 * sure the appropriate page became available, this saves space
936 * at a cost of "thundering herd" phenomena during rare hash
937 * collisions.
938 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000939#define PAGE_WAIT_TABLE_BITS 8
940#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
941static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
942
943static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Nicholas Piggin62906022016-12-25 13:00:30 +1000945 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
Nicholas Piggin62906022016-12-25 13:00:30 +1000947
948void __init pagecache_init(void)
949{
950 int i;
951
952 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
953 init_waitqueue_head(&page_wait_table[i]);
954
955 page_writeback_init();
956}
957
Linus Torvalds3510ca22017-08-27 13:55:12 -0700958/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000959struct wait_page_key {
960 struct page *page;
961 int bit_nr;
962 int page_match;
963};
964
965struct wait_page_queue {
966 struct page *page;
967 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200968 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000969};
970
Ingo Molnarac6424b2017-06-20 12:06:13 +0200971static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000972{
973 struct wait_page_key *key = arg;
974 struct wait_page_queue *wait_page
975 = container_of(wait, struct wait_page_queue, wait);
976
977 if (wait_page->page != key->page)
978 return 0;
979 key->page_match = 1;
980
981 if (wait_page->bit_nr != key->bit_nr)
982 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -0700983
Hugh Dickins9a1ea432018-12-28 00:36:14 -0800984 /*
985 * Stop walking if it's locked.
986 * Is this safe if put_and_wait_on_page_locked() is in use?
987 * Yes: the waker must hold a reference to this page, and if PG_locked
988 * has now already been set by another task, that task must also hold
989 * a reference to the *same usage* of this page; so there is no need
990 * to walk on to wake even the put_and_wait_on_page_locked() callers.
991 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000992 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -0700993 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +1000994
995 return autoremove_wake_function(wait, mode, sync, key);
996}
997
Nicholas Piggin74d81bf2017-02-22 15:44:41 -0800998static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +1000999{
1000 wait_queue_head_t *q = page_waitqueue(page);
1001 struct wait_page_key key;
1002 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001003 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001004
1005 key.page = page;
1006 key.bit_nr = bit_nr;
1007 key.page_match = 0;
1008
Tim Chen11a19c72017-08-25 09:13:55 -07001009 bookmark.flags = 0;
1010 bookmark.private = NULL;
1011 bookmark.func = NULL;
1012 INIT_LIST_HEAD(&bookmark.entry);
1013
Nicholas Piggin62906022016-12-25 13:00:30 +10001014 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001015 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1016
1017 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1018 /*
1019 * Take a breather from holding the lock,
1020 * allow pages that finish wake up asynchronously
1021 * to acquire the lock and remove themselves
1022 * from wait queue
1023 */
1024 spin_unlock_irqrestore(&q->lock, flags);
1025 cpu_relax();
1026 spin_lock_irqsave(&q->lock, flags);
1027 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1028 }
1029
Nicholas Piggin62906022016-12-25 13:00:30 +10001030 /*
1031 * It is possible for other pages to have collided on the waitqueue
1032 * hash, so in that case check for a page match. That prevents a long-
1033 * term waiter
1034 *
1035 * It is still possible to miss a case here, when we woke page waiters
1036 * and removed them from the waitqueue, but there are still other
1037 * page waiters.
1038 */
1039 if (!waitqueue_active(q) || !key.page_match) {
1040 ClearPageWaiters(page);
1041 /*
1042 * It's possible to miss clearing Waiters here, when we woke
1043 * our page waiters, but the hashed waitqueue has waiters for
1044 * other pages on it.
1045 *
1046 * That's okay, it's a rare case. The next waker will clear it.
1047 */
1048 }
1049 spin_unlock_irqrestore(&q->lock, flags);
1050}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001051
1052static void wake_up_page(struct page *page, int bit)
1053{
1054 if (!PageWaiters(page))
1055 return;
1056 wake_up_page_bit(page, bit);
1057}
Nicholas Piggin62906022016-12-25 13:00:30 +10001058
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001059/*
1060 * A choice of three behaviors for wait_on_page_bit_common():
1061 */
1062enum behavior {
1063 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
1064 * __lock_page() waiting on then setting PG_locked.
1065 */
1066 SHARED, /* Hold ref to page and check the bit when woken, like
1067 * wait_on_page_writeback() waiting on PG_writeback.
1068 */
1069 DROP, /* Drop ref to page before wait, no check when woken,
1070 * like put_and_wait_on_page_locked() on PG_locked.
1071 */
1072};
1073
Nicholas Piggin62906022016-12-25 13:00:30 +10001074static inline int wait_on_page_bit_common(wait_queue_head_t *q,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001075 struct page *page, int bit_nr, int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001076{
1077 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001078 wait_queue_entry_t *wait = &wait_page.wait;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001079 bool bit_is_set;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001080 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001081 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001082 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001083 int ret = 0;
1084
Johannes Weinereb414682018-10-26 15:06:27 -07001085 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001086 !PageUptodate(page) && PageWorkingset(page)) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001087 if (!PageSwapBacked(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001088 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001089 delayacct = true;
1090 }
Johannes Weinereb414682018-10-26 15:06:27 -07001091 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001092 thrashing = true;
1093 }
1094
Nicholas Piggin62906022016-12-25 13:00:30 +10001095 init_wait(wait);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001096 wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001097 wait->func = wake_page_function;
1098 wait_page.page = page;
1099 wait_page.bit_nr = bit_nr;
1100
1101 for (;;) {
1102 spin_lock_irq(&q->lock);
1103
Ingo Molnar2055da92017-06-20 12:06:46 +02001104 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001105 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001106 SetPageWaiters(page);
1107 }
1108
1109 set_current_state(state);
1110
1111 spin_unlock_irq(&q->lock);
1112
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001113 bit_is_set = test_bit(bit_nr, &page->flags);
1114 if (behavior == DROP)
1115 put_page(page);
Nicholas Piggin62906022016-12-25 13:00:30 +10001116
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001117 if (likely(bit_is_set))
1118 io_schedule();
1119
1120 if (behavior == EXCLUSIVE) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001121 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1122 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001123 } else if (behavior == SHARED) {
Nicholas Piggin62906022016-12-25 13:00:30 +10001124 if (!test_bit(bit_nr, &page->flags))
1125 break;
1126 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001127
1128 if (unlikely(signal_pending_state(state, current))) {
1129 ret = -EINTR;
1130 break;
1131 }
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001132
1133 if (behavior == DROP) {
1134 /*
1135 * We can no longer safely access page->flags:
1136 * even if CONFIG_MEMORY_HOTREMOVE is not enabled,
1137 * there is a risk of waiting forever on a page reused
1138 * for something that keeps it locked indefinitely.
1139 * But best check for -EINTR above before breaking.
1140 */
1141 break;
1142 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001143 }
1144
1145 finish_wait(q, wait);
1146
Johannes Weinereb414682018-10-26 15:06:27 -07001147 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001148 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001149 delayacct_thrashing_end();
1150 psi_memstall_leave(&pflags);
1151 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001152
Nicholas Piggin62906022016-12-25 13:00:30 +10001153 /*
1154 * A signal could leave PageWaiters set. Clearing it here if
1155 * !waitqueue_active would be possible (by open-coding finish_wait),
1156 * but still fail to catch it in the case of wait hash collision. We
1157 * already can fail to clear wait hash collision cases, so don't
1158 * bother with signals either.
1159 */
1160
1161 return ret;
1162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Harvey Harrison920c7a52008-02-04 22:29:26 -08001164void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Nicholas Piggin62906022016-12-25 13:00:30 +10001166 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001167 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169EXPORT_SYMBOL(wait_on_page_bit);
1170
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001171int wait_on_page_bit_killable(struct page *page, int bit_nr)
1172{
Nicholas Piggin62906022016-12-25 13:00:30 +10001173 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001174 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001175}
David Howells4343d002017-11-02 15:27:52 +00001176EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/**
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001179 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1180 * @page: The page to wait for.
1181 *
1182 * The caller should hold a reference on @page. They expect the page to
1183 * become unlocked relatively soon, but do not wish to hold up migration
1184 * (for example) by holding the reference while waiting for the page to
1185 * come unlocked. After this function returns, the caller should not
1186 * dereference @page.
1187 */
1188void put_and_wait_on_page_locked(struct page *page)
1189{
1190 wait_queue_head_t *q;
1191
1192 page = compound_head(page);
1193 q = page_waitqueue(page);
1194 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP);
1195}
1196
1197/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001198 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001199 * @page: Page defining the wait queue of interest
1200 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001201 *
1202 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1203 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001204void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001205{
1206 wait_queue_head_t *q = page_waitqueue(page);
1207 unsigned long flags;
1208
1209 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001210 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001211 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001212 spin_unlock_irqrestore(&q->lock, flags);
1213}
1214EXPORT_SYMBOL_GPL(add_page_wait_queue);
1215
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001216#ifndef clear_bit_unlock_is_negative_byte
1217
1218/*
1219 * PG_waiters is the high bit in the same byte as PG_lock.
1220 *
1221 * On x86 (and on many other architectures), we can clear PG_lock and
1222 * test the sign bit at the same time. But if the architecture does
1223 * not support that special operation, we just do this all by hand
1224 * instead.
1225 *
1226 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1227 * being cleared, but a memory barrier should be unneccssary since it is
1228 * in the same byte as PG_locked.
1229 */
1230static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1231{
1232 clear_bit_unlock(nr, mem);
1233 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001234 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001235}
1236
1237#endif
1238
David Howells385e1ca5f2009-04-03 16:42:39 +01001239/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001240 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * @page: the page
1242 *
1243 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1244 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001245 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1247 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001248 * Note that this depends on PG_waiters being the sign bit in the byte
1249 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1250 * clear the PG_locked bit and test PG_waiters at the same time fairly
1251 * portably (architectures that do LL/SC can test any bit, while x86 can
1252 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001254void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001256 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001257 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001258 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001259 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1260 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262EXPORT_SYMBOL(unlock_page);
1263
Randy Dunlap485bb992006-06-23 02:03:49 -07001264/**
1265 * end_page_writeback - end writeback against a page
1266 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
1268void end_page_writeback(struct page *page)
1269{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001270 /*
1271 * TestClearPageReclaim could be used here but it is an atomic
1272 * operation and overkill in this particular case. Failing to
1273 * shuffle a page marked for immediate reclaim is too mild to
1274 * justify taking an atomic operation penalty at the end of
1275 * ever page writeback.
1276 */
1277 if (PageReclaim(page)) {
1278 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001279 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001280 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001281
1282 if (!test_clear_page_writeback(page))
1283 BUG();
1284
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001285 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 wake_up_page(page, PG_writeback);
1287}
1288EXPORT_SYMBOL(end_page_writeback);
1289
Matthew Wilcox57d99842014-06-04 16:07:45 -07001290/*
1291 * After completing I/O on a page, call this routine to update the page
1292 * flags appropriately
1293 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001294void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001295{
Jens Axboec11f0c02016-08-05 08:11:04 -06001296 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001297 if (!err) {
1298 SetPageUptodate(page);
1299 } else {
1300 ClearPageUptodate(page);
1301 SetPageError(page);
1302 }
1303 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001304 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001305 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001306 struct address_space *mapping;
1307
Matthew Wilcox57d99842014-06-04 16:07:45 -07001308 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001309 mapping = page_mapping(page);
1310 if (mapping)
1311 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001312 }
1313 end_page_writeback(page);
1314 }
1315}
1316EXPORT_SYMBOL_GPL(page_endio);
1317
Randy Dunlap485bb992006-06-23 02:03:49 -07001318/**
1319 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001320 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001322void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Nicholas Piggin62906022016-12-25 13:00:30 +10001324 struct page *page = compound_head(__page);
1325 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001326 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE,
1327 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329EXPORT_SYMBOL(__lock_page);
1330
Nicholas Piggin62906022016-12-25 13:00:30 +10001331int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001332{
Nicholas Piggin62906022016-12-25 13:00:30 +10001333 struct page *page = compound_head(__page);
1334 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001335 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE,
1336 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001337}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001338EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001339
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001340/*
1341 * Return values:
1342 * 1 - page is locked; mmap_sem is still held.
1343 * 0 - page is not locked.
1344 * mmap_sem has been released (up_read()), unless flags had both
1345 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1346 * which case mmap_sem is still held.
1347 *
1348 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1349 * with the page locked and the mmap_sem unperturbed.
1350 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001351int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1352 unsigned int flags)
1353{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001354 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1355 /*
1356 * CAUTION! In this case, mmap_sem is not released
1357 * even though return 0.
1358 */
1359 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1360 return 0;
1361
1362 up_read(&mm->mmap_sem);
1363 if (flags & FAULT_FLAG_KILLABLE)
1364 wait_on_page_locked_killable(page);
1365 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001366 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001367 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001368 } else {
1369 if (flags & FAULT_FLAG_KILLABLE) {
1370 int ret;
1371
1372 ret = __lock_page_killable(page);
1373 if (ret) {
1374 up_read(&mm->mmap_sem);
1375 return 0;
1376 }
1377 } else
1378 __lock_page(page);
1379 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001380 }
1381}
1382
Randy Dunlap485bb992006-06-23 02:03:49 -07001383/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001384 * page_cache_next_miss() - Find the next gap in the page cache.
1385 * @mapping: Mapping.
1386 * @index: Index.
1387 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001388 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001389 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1390 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001391 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001392 * This function may be called under the rcu_read_lock. However, this will
1393 * not atomically search a snapshot of the cache at a single point in time.
1394 * For example, if a gap is created at index 5, then subsequently a gap is
1395 * created at index 10, page_cache_next_miss covering both indices may
1396 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001397 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001398 * Return: The index of the gap if found, otherwise an index outside the
1399 * range specified (in which case 'return - index >= max_scan' will be true).
1400 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001401 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001402pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001403 pgoff_t index, unsigned long max_scan)
1404{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001405 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001406
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001407 while (max_scan--) {
1408 void *entry = xas_next(&xas);
1409 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001410 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001411 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001412 break;
1413 }
1414
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001415 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001416}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001417EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001418
1419/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001420 * page_cache_prev_miss() - Find the next gap in the page cache.
1421 * @mapping: Mapping.
1422 * @index: Index.
1423 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001424 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001425 * Search the range [max(index - max_scan + 1, 0), index] for the
1426 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001427 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001428 * This function may be called under the rcu_read_lock. However, this will
1429 * not atomically search a snapshot of the cache at a single point in time.
1430 * For example, if a gap is created at index 10, then subsequently a gap is
1431 * created at index 5, page_cache_prev_miss() covering both indices may
1432 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001433 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001434 * Return: The index of the gap if found, otherwise an index outside the
1435 * range specified (in which case 'index - return >= max_scan' will be true).
1436 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001437 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001438pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001439 pgoff_t index, unsigned long max_scan)
1440{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001441 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001442
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001443 while (max_scan--) {
1444 void *entry = xas_prev(&xas);
1445 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001446 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001447 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001448 break;
1449 }
1450
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001451 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001452}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001453EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001454
1455/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001456 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001457 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001458 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001459 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001460 * Looks up the page cache slot at @mapping & @offset. If there is a
1461 * page cache page, it is returned with an increased refcount.
1462 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001463 * If the slot holds a shadow entry of a previously evicted page, or a
1464 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001465 *
1466 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001468struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001470 XA_STATE(xas, &mapping->i_pages, offset);
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001471 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Nick Piggina60637c2008-07-25 19:45:31 -07001473 rcu_read_lock();
1474repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001475 xas_reset(&xas);
1476 page = xas_load(&xas);
1477 if (xas_retry(&xas, page))
1478 goto repeat;
1479 /*
1480 * A shadow entry of a recently evicted page, or a swap entry from
1481 * shmem/tmpfs. Return it without attempting to raise page count.
1482 */
1483 if (!page || xa_is_value(page))
1484 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001485
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001486 head = compound_head(page);
1487 if (!page_cache_get_speculative(head))
1488 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001489
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001490 /* The page was split under us? */
1491 if (compound_head(page) != head) {
1492 put_page(head);
1493 goto repeat;
1494 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001495
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001496 /*
1497 * Has the page moved?
1498 * This is part of the lockless pagecache protocol. See
1499 * include/linux/pagemap.h for details.
1500 */
1501 if (unlikely(page != xas_reload(&xas))) {
1502 put_page(head);
1503 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001504 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001505out:
Nick Piggina60637c2008-07-25 19:45:31 -07001506 rcu_read_unlock();
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return page;
1509}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001510EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Randy Dunlap485bb992006-06-23 02:03:49 -07001512/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001513 * find_lock_entry - locate, pin and lock a page cache entry
1514 * @mapping: the address_space to search
1515 * @offset: the page cache index
1516 *
1517 * Looks up the page cache slot at @mapping & @offset. If there is a
1518 * page cache page, it is returned locked and with an increased
1519 * refcount.
1520 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001521 * If the slot holds a shadow entry of a previously evicted page, or a
1522 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001523 *
1524 * Otherwise, %NULL is returned.
1525 *
1526 * find_lock_entry() may sleep.
1527 */
1528struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct page *page;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001533 page = find_get_entry(mapping, offset);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001534 if (page && !xa_is_value(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001535 lock_page(page);
1536 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001537 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001538 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001539 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001540 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001542 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return page;
1545}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001546EXPORT_SYMBOL(find_lock_entry);
1547
1548/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001549 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001550 * @mapping: the address_space to search
1551 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001552 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001553 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001554 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001555 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001556 *
Randy Dunlap75325182014-07-30 16:08:37 -07001557 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001558 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001559 * @fgp_flags can be:
1560 *
1561 * - FGP_ACCESSED: the page will be marked accessed
1562 * - FGP_LOCK: Page is return locked
1563 * - FGP_CREAT: If page is not present then a new page is allocated using
1564 * @gfp_mask and added to the page cache and the VM's LRU
1565 * list. The page is returned locked and with an increased
1566 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001567 *
1568 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1569 * if the GFP flags specified for FGP_CREAT are atomic.
1570 *
1571 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001572 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001573struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001574 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Nick Piggineb2be182007-10-16 01:24:57 -07001576 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001579 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001580 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001581 page = NULL;
1582 if (!page)
1583 goto no_page;
1584
1585 if (fgp_flags & FGP_LOCK) {
1586 if (fgp_flags & FGP_NOWAIT) {
1587 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001588 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001589 return NULL;
1590 }
1591 } else {
1592 lock_page(page);
1593 }
1594
1595 /* Has the page been truncated? */
1596 if (unlikely(page->mapping != mapping)) {
1597 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001598 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001599 goto repeat;
1600 }
1601 VM_BUG_ON_PAGE(page->index != offset, page);
1602 }
1603
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001604 if (fgp_flags & FGP_ACCESSED)
Mel Gorman2457aec2014-06-04 16:10:31 -07001605 mark_page_accessed(page);
1606
1607no_page:
1608 if (!page && (fgp_flags & FGP_CREAT)) {
1609 int err;
1610 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001611 gfp_mask |= __GFP_WRITE;
1612 if (fgp_flags & FGP_NOFS)
1613 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001614
Michal Hocko45f87de2014-12-29 20:30:35 +01001615 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001616 if (!page)
1617 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001618
1619 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1620 fgp_flags |= FGP_LOCK;
1621
Hugh Dickinseb39d612014-08-06 16:06:43 -07001622 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001623 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001624 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001625
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001626 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001627 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001628 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001629 page = NULL;
1630 if (err == -EEXIST)
1631 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return page;
1636}
Mel Gorman2457aec2014-06-04 16:10:31 -07001637EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001640 * find_get_entries - gang pagecache lookup
1641 * @mapping: The address_space to search
1642 * @start: The starting page cache index
1643 * @nr_entries: The maximum number of entries
1644 * @entries: Where the resulting entries are placed
1645 * @indices: The cache indices corresponding to the entries in @entries
1646 *
1647 * find_get_entries() will search for and return a group of up to
1648 * @nr_entries entries in the mapping. The entries are placed at
1649 * @entries. find_get_entries() takes a reference against any actual
1650 * pages it returns.
1651 *
1652 * The search returns a group of mapping-contiguous page cache entries
1653 * with ascending indexes. There may be holes in the indices due to
1654 * not-present pages.
1655 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001656 * Any shadow entries of evicted pages, or swap entries from
1657 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001658 *
1659 * find_get_entries() returns the number of pages and shadow entries
1660 * which were found.
1661 */
1662unsigned find_get_entries(struct address_space *mapping,
1663 pgoff_t start, unsigned int nr_entries,
1664 struct page **entries, pgoff_t *indices)
1665{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001666 XA_STATE(xas, &mapping->i_pages, start);
1667 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001668 unsigned int ret = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001669
1670 if (!nr_entries)
1671 return 0;
1672
1673 rcu_read_lock();
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001674 xas_for_each(&xas, page, ULONG_MAX) {
1675 struct page *head;
1676 if (xas_retry(&xas, page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001677 continue;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001678 /*
1679 * A shadow entry of a recently evicted page, a swap
1680 * entry from shmem/tmpfs or a DAX entry. Return it
1681 * without attempting to raise page count.
1682 */
1683 if (xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001684 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001685
1686 head = compound_head(page);
1687 if (!page_cache_get_speculative(head))
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001688 goto retry;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001689
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001690 /* The page was split under us? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001691 if (compound_head(page) != head)
1692 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001693
Johannes Weiner0cd61442014-04-03 14:47:46 -07001694 /* Has the page moved? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001695 if (unlikely(page != xas_reload(&xas)))
1696 goto put_page;
1697
Johannes Weiner0cd61442014-04-03 14:47:46 -07001698export:
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001699 indices[ret] = xas.xa_index;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001700 entries[ret] = page;
1701 if (++ret == nr_entries)
1702 break;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001703 continue;
1704put_page:
1705 put_page(head);
1706retry:
1707 xas_reset(&xas);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001708 }
1709 rcu_read_unlock();
1710 return ret;
1711}
1712
1713/**
Jan Karab947cee2017-09-06 16:21:21 -07001714 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * @mapping: The address_space to search
1716 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001717 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * @nr_pages: The maximum number of pages
1719 * @pages: Where the resulting pages are placed
1720 *
Jan Karab947cee2017-09-06 16:21:21 -07001721 * find_get_pages_range() will search for and return a group of up to @nr_pages
1722 * pages in the mapping starting at index @start and up to index @end
1723 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1724 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
1726 * The search returns a group of mapping-contiguous pages with ascending
1727 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001728 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
Jan Karab947cee2017-09-06 16:21:21 -07001730 * find_get_pages_range() returns the number of pages which were found. If this
1731 * number is smaller than @nr_pages, the end of specified range has been
1732 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Jan Karab947cee2017-09-06 16:21:21 -07001734unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1735 pgoff_t end, unsigned int nr_pages,
1736 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001738 XA_STATE(xas, &mapping->i_pages, *start);
1739 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001740 unsigned ret = 0;
1741
1742 if (unlikely(!nr_pages))
1743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Nick Piggina60637c2008-07-25 19:45:31 -07001745 rcu_read_lock();
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001746 xas_for_each(&xas, page, end) {
1747 struct page *head;
1748 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001749 continue;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001750 /* Skip over shadow, swap and DAX entries */
1751 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001752 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001753
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001754 head = compound_head(page);
1755 if (!page_cache_get_speculative(head))
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001756 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001757
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001758 /* The page was split under us? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001759 if (compound_head(page) != head)
1760 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001761
Nick Piggina60637c2008-07-25 19:45:31 -07001762 /* Has the page moved? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001763 if (unlikely(page != xas_reload(&xas)))
1764 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001765
1766 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001767 if (++ret == nr_pages) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001768 *start = page->index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07001769 goto out;
1770 }
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001771 continue;
1772put_page:
1773 put_page(head);
1774retry:
1775 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001776 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001777
Jan Karab947cee2017-09-06 16:21:21 -07001778 /*
1779 * We come here when there is no page beyond @end. We take care to not
1780 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001781 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07001782 * already broken anyway.
1783 */
1784 if (end == (pgoff_t)-1)
1785 *start = (pgoff_t)-1;
1786 else
1787 *start = end + 1;
1788out:
Nick Piggina60637c2008-07-25 19:45:31 -07001789 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return ret;
1792}
1793
Jens Axboeebf43502006-04-27 08:46:01 +02001794/**
1795 * find_get_pages_contig - gang contiguous pagecache lookup
1796 * @mapping: The address_space to search
1797 * @index: The starting page index
1798 * @nr_pages: The maximum number of pages
1799 * @pages: Where the resulting pages are placed
1800 *
1801 * find_get_pages_contig() works exactly like find_get_pages(), except
1802 * that the returned number of pages are guaranteed to be contiguous.
1803 *
1804 * find_get_pages_contig() returns the number of pages which were found.
1805 */
1806unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1807 unsigned int nr_pages, struct page **pages)
1808{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001809 XA_STATE(xas, &mapping->i_pages, index);
1810 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001811 unsigned int ret = 0;
1812
1813 if (unlikely(!nr_pages))
1814 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001815
Nick Piggina60637c2008-07-25 19:45:31 -07001816 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001817 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1818 struct page *head;
1819 if (xas_retry(&xas, page))
1820 continue;
1821 /*
1822 * If the entry has been swapped out, we can stop looking.
1823 * No current caller is looking for DAX entries.
1824 */
1825 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001826 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001827
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001828 head = compound_head(page);
1829 if (!page_cache_get_speculative(head))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001830 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001831
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001832 /* The page was split under us? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001833 if (compound_head(page) != head)
1834 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001835
Nick Piggina60637c2008-07-25 19:45:31 -07001836 /* Has the page moved? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001837 if (unlikely(page != xas_reload(&xas)))
1838 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001839
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001840 /*
1841 * must check mapping and index after taking the ref.
1842 * otherwise we can get both false positives and false
1843 * negatives, which is just confusing to the caller.
1844 */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001845 if (!page->mapping || page_to_pgoff(page) != xas.xa_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001846 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001847 break;
1848 }
1849
Nick Piggina60637c2008-07-25 19:45:31 -07001850 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001851 if (++ret == nr_pages)
1852 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001853 continue;
1854put_page:
1855 put_page(head);
1856retry:
1857 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02001858 }
Nick Piggina60637c2008-07-25 19:45:31 -07001859 rcu_read_unlock();
1860 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001861}
David Howellsef71c152007-05-09 02:33:44 -07001862EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001863
Randy Dunlap485bb992006-06-23 02:03:49 -07001864/**
Jan Kara72b045a2017-11-15 17:34:33 -08001865 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001866 * @mapping: the address_space to search
1867 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001868 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001869 * @tag: the tag index
1870 * @nr_pages: the maximum number of pages
1871 * @pages: where the resulting pages are placed
1872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001874 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
Jan Kara72b045a2017-11-15 17:34:33 -08001876unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001877 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08001878 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001880 XA_STATE(xas, &mapping->i_pages, *index);
1881 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001882 unsigned ret = 0;
1883
1884 if (unlikely(!nr_pages))
1885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Nick Piggina60637c2008-07-25 19:45:31 -07001887 rcu_read_lock();
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001888 xas_for_each_marked(&xas, page, end, tag) {
1889 struct page *head;
1890 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001891 continue;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001892 /*
1893 * Shadow entries should never be tagged, but this iteration
1894 * is lockless so there is a window for page reclaim to evict
1895 * a page we saw tagged. Skip over it.
1896 */
1897 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07001898 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001899
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001900 head = compound_head(page);
1901 if (!page_cache_get_speculative(head))
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001902 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001903
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001904 /* The page was split under us? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001905 if (compound_head(page) != head)
1906 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001907
Nick Piggina60637c2008-07-25 19:45:31 -07001908 /* Has the page moved? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001909 if (unlikely(page != xas_reload(&xas)))
1910 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001911
1912 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001913 if (++ret == nr_pages) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001914 *index = page->index + 1;
Jan Kara72b045a2017-11-15 17:34:33 -08001915 goto out;
1916 }
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001917 continue;
1918put_page:
1919 put_page(head);
1920retry:
1921 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001922 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001923
Jan Kara72b045a2017-11-15 17:34:33 -08001924 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001925 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08001926 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001927 * iteration when there is a page at index -1 but that is already
1928 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08001929 */
1930 if (end == (pgoff_t)-1)
1931 *index = (pgoff_t)-1;
1932 else
1933 *index = end + 1;
1934out:
Nick Piggina60637c2008-07-25 19:45:31 -07001935 rcu_read_unlock();
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return ret;
1938}
Jan Kara72b045a2017-11-15 17:34:33 -08001939EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001941/**
1942 * find_get_entries_tag - find and return entries that match @tag
1943 * @mapping: the address_space to search
1944 * @start: the starting page cache index
1945 * @tag: the tag index
1946 * @nr_entries: the maximum number of entries
1947 * @entries: where the resulting entries are placed
1948 * @indices: the cache indices corresponding to the entries in @entries
1949 *
1950 * Like find_get_entries, except we only return entries which are tagged with
1951 * @tag.
1952 */
1953unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001954 xa_mark_t tag, unsigned int nr_entries,
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001955 struct page **entries, pgoff_t *indices)
1956{
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001957 XA_STATE(xas, &mapping->i_pages, start);
1958 struct page *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001959 unsigned int ret = 0;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001960
1961 if (!nr_entries)
1962 return 0;
1963
1964 rcu_read_lock();
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001965 xas_for_each_marked(&xas, page, ULONG_MAX, tag) {
1966 struct page *head;
1967 if (xas_retry(&xas, page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001968 continue;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001969 /*
1970 * A shadow entry of a recently evicted page, a swap
1971 * entry from shmem/tmpfs or a DAX entry. Return it
1972 * without attempting to raise page count.
1973 */
1974 if (xa_is_value(page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001975 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001976
1977 head = compound_head(page);
1978 if (!page_cache_get_speculative(head))
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001979 goto retry;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001980
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001981 /* The page was split under us? */
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001982 if (compound_head(page) != head)
1983 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001984
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001985 /* Has the page moved? */
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001986 if (unlikely(page != xas_reload(&xas)))
1987 goto put_page;
1988
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:
1996 put_page(head);
1997retry:
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002038static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002039 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002041 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002042 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002044 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002045 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002046 pgoff_t index;
2047 pgoff_t last_index;
2048 pgoff_t prev_index;
2049 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002050 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002051 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Wei Fangc2a97372016-10-07 17:01:52 -07002053 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002054 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002055 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2056
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002057 index = *ppos >> PAGE_SHIFT;
2058 prev_index = ra->prev_pos >> PAGE_SHIFT;
2059 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2060 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2061 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 for (;;) {
2064 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002065 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002066 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 unsigned long nr, ret;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002071 if (fatal_signal_pending(current)) {
2072 error = -EINTR;
2073 goto out;
2074 }
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002077 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002078 if (iocb->ki_flags & IOCB_NOWAIT)
2079 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002080 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002081 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002082 index, last_index - index);
2083 page = find_get_page(mapping, index);
2084 if (unlikely(page == NULL))
2085 goto no_cached_page;
2086 }
2087 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002088 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002089 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002090 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002092 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002093 if (iocb->ki_flags & IOCB_NOWAIT) {
2094 put_page(page);
2095 goto would_block;
2096 }
2097
Mel Gormanebded022016-03-15 14:55:39 -07002098 /*
2099 * See comment in do_read_cache_page on why
2100 * wait_on_page_locked is used to avoid unnecessarily
2101 * serialisations and why it's safe.
2102 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002103 error = wait_on_page_locked_killable(page);
2104 if (unlikely(error))
2105 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002106 if (PageUptodate(page))
2107 goto page_ok;
2108
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002109 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002110 !mapping->a_ops->is_partially_uptodate)
2111 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002112 /* pipes can't handle partially uptodate pages */
David Howells00e23702018-10-22 13:07:28 +01002113 if (unlikely(iov_iter_is_pipe(iter)))
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002114 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002115 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002116 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002117 /* Did it get truncated before we got the lock? */
2118 if (!page->mapping)
2119 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002120 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002121 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002122 goto page_not_up_to_date_locked;
2123 unlock_page(page);
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002126 /*
2127 * i_size must be checked after we know the page is Uptodate.
2128 *
2129 * Checking i_size after the check allows us to calculate
2130 * the correct value for "nr", which means the zero-filled
2131 * part of the page is not copied back to userspace (unless
2132 * another truncate extends the file - this is desired though).
2133 */
2134
2135 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002137 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002138 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002139 goto out;
2140 }
2141
2142 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002143 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002144 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002146 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002148 goto out;
2149 }
2150 }
2151 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 /* If users can be writing to this page using arbitrary
2154 * virtual addresses, take care about potential aliasing
2155 * before reading the page on the kernel side.
2156 */
2157 if (mapping_writably_mapped(mapping))
2158 flush_dcache_page(page);
2159
2160 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002161 * When a sequential read accesses a page several times,
2162 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 */
Jan Karaec0f1632007-05-06 14:49:25 -07002164 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 mark_page_accessed(page);
2166 prev_index = index;
2167
2168 /*
2169 * Ok, we have the page, and it's up-to-date, so
2170 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 */
Al Viro6e58e792014-02-03 17:07:03 -05002172
2173 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002175 index += offset >> PAGE_SHIFT;
2176 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002177 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002180 written += ret;
2181 if (!iov_iter_count(iter))
2182 goto out;
2183 if (ret < nr) {
2184 error = -EFAULT;
2185 goto out;
2186 }
2187 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189page_not_up_to_date:
2190 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002191 error = lock_page_killable(page);
2192 if (unlikely(error))
2193 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002195page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002196 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (!page->mapping) {
2198 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002199 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 continue;
2201 }
2202
2203 /* Did somebody else fill it already? */
2204 if (PageUptodate(page)) {
2205 unlock_page(page);
2206 goto page_ok;
2207 }
2208
2209readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002210 /*
2211 * A previous I/O error may have been due to temporary
2212 * failures, eg. multipath errors.
2213 * PG_error will be set again if readpage fails.
2214 */
2215 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /* Start the actual read. The read will unlock the page. */
2217 error = mapping->a_ops->readpage(filp, page);
2218
Zach Brown994fc28c2005-12-15 14:28:17 -08002219 if (unlikely(error)) {
2220 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002221 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002222 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002223 goto find_page;
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002229 error = lock_page_killable(page);
2230 if (unlikely(error))
2231 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (!PageUptodate(page)) {
2233 if (page->mapping == NULL) {
2234 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002235 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 */
2237 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002238 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto find_page;
2240 }
2241 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002242 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002243 error = -EIO;
2244 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 unlock_page(page);
2247 }
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 goto page_ok;
2250
2251readpage_error:
2252 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002253 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto out;
2255
2256no_cached_page:
2257 /*
2258 * Ok, it wasn't cached, so we need to create a new
2259 * page..
2260 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002261 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002262 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002263 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002266 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002267 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002269 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002270 if (error == -EEXIST) {
2271 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto out;
2275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto readpage;
2277 }
2278
Milosz Tanski3239d832017-08-29 16:13:19 +02002279would_block:
2280 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002282 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002283 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002284 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002286 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002287 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002288 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Randy Dunlap485bb992006-06-23 02:03:49 -07002291/**
Al Viro6abd2322014-04-04 14:20:57 -04002292 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002293 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002294 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002295 *
Al Viro6abd2322014-04-04 14:20:57 -04002296 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * that can use the page cache directly.
2298 */
2299ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002300generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002302 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002303 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002304
2305 if (!count)
2306 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Al Viro2ba48ce2015-04-09 13:52:01 -04002308 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002309 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002310 struct address_space *mapping = file->f_mapping;
2311 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002312 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002315 if (iocb->ki_flags & IOCB_NOWAIT) {
2316 if (filemap_range_has_page(mapping, iocb->ki_pos,
2317 iocb->ki_pos + count - 1))
2318 return -EAGAIN;
2319 } else {
2320 retval = filemap_write_and_wait_range(mapping,
2321 iocb->ki_pos,
2322 iocb->ki_pos + count - 1);
2323 if (retval < 0)
2324 goto out;
2325 }
Al Viroed978a82014-03-05 22:53:04 -05002326
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002327 file_accessed(file);
2328
Al Viro5ecda132017-04-13 14:13:36 -04002329 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002330 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002331 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002332 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002333 }
Al Viro5b47d592017-05-08 13:54:47 -04002334 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002335
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002336 /*
2337 * Btrfs can have a short DIO read if we encounter
2338 * compressed extents, so if there was an error, or if
2339 * we've already read everything we wanted to, or if
2340 * there was a short read because we hit EOF, go ahead
2341 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002342 * the rest of the read. Buffered reads will not work for
2343 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002344 */
Al Viro5ecda132017-04-13 14:13:36 -04002345 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002346 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002350 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351out:
2352 return retval;
2353}
Al Viroed978a82014-03-05 22:53:04 -05002354EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002357/**
2358 * page_cache_read - adds requested page to the page cache if not already there
2359 * @file: file to read
2360 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002361 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002362 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 * This adds the requested page to the page cache if it isn't already there,
2364 * and schedules an I/O to read in its contents from disk.
2365 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002366static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
2368 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002369 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002370 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Zach Brown994fc28c2005-12-15 14:28:17 -08002372 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002373 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002374 if (!page)
2375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002377 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002378 if (ret == 0)
2379 ret = mapping->a_ops->readpage(file, page);
2380 else if (ret == -EEXIST)
2381 ret = 0; /* losing race to add is OK */
2382
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002383 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Zach Brown994fc28c2005-12-15 14:28:17 -08002385 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002386
Zach Brown994fc28c2005-12-15 14:28:17 -08002387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
2390#define MMAP_LOTSAMISS (100)
2391
Linus Torvaldsef00e082009-06-16 15:31:25 -07002392/*
2393 * Synchronous readahead happens when we don't even find
2394 * a page in the page cache at all.
2395 */
2396static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2397 struct file_ra_state *ra,
2398 struct file *file,
2399 pgoff_t offset)
2400{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002401 struct address_space *mapping = file->f_mapping;
2402
2403 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002404 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002405 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002406 if (!ra->ra_pages)
2407 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002408
Joe Perches64363aa2013-07-08 16:00:18 -07002409 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002410 page_cache_sync_readahead(mapping, ra, file, offset,
2411 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002412 return;
2413 }
2414
Andi Kleen207d04b2011-05-24 17:12:29 -07002415 /* Avoid banging the cache line if not needed */
2416 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002417 ra->mmap_miss++;
2418
2419 /*
2420 * Do we miss much more than hit in this file? If so,
2421 * stop bothering with read-ahead. It will only hurt.
2422 */
2423 if (ra->mmap_miss > MMAP_LOTSAMISS)
2424 return;
2425
Wu Fengguangd30a1102009-06-16 15:31:30 -07002426 /*
2427 * mmap read-around
2428 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002429 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2430 ra->size = ra->ra_pages;
2431 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002432 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002433}
2434
2435/*
2436 * Asynchronous readahead happens when we find the page and PG_readahead,
2437 * so we want to possibly extend the readahead further..
2438 */
2439static void do_async_mmap_readahead(struct vm_area_struct *vma,
2440 struct file_ra_state *ra,
2441 struct file *file,
2442 struct page *page,
2443 pgoff_t offset)
2444{
2445 struct address_space *mapping = file->f_mapping;
2446
2447 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002448 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002449 return;
2450 if (ra->mmap_miss > 0)
2451 ra->mmap_miss--;
2452 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002453 page_cache_async_readahead(mapping, ra, file,
2454 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002455}
2456
Randy Dunlap485bb992006-06-23 02:03:49 -07002457/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002458 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002459 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002460 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002461 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 * mapped memory region to read in file data during a page fault.
2463 *
2464 * The goto's are kind of ugly, but this streamlines the normal case of having
2465 * it in the page cache, and handles the special cases reasonably without
2466 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002467 *
2468 * vma->vm_mm->mmap_sem must be held on entry.
2469 *
2470 * If our return value has VM_FAULT_RETRY set, it's because
2471 * lock_page_or_retry() returned 0.
2472 * The mmap_sem has usually been released in this case.
2473 * See __lock_page_or_retry() for the exception.
2474 *
2475 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2476 * has not been released.
2477 *
2478 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002480vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002483 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct address_space *mapping = file->f_mapping;
2485 struct file_ra_state *ra = &file->f_ra;
2486 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002487 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002488 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002490 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002492 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2493 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002494 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002497 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002499 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002500 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002502 * We found the page, so try async readahead before
2503 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
Dave Jiang11bac802017-02-24 14:56:41 -08002505 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002506 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002507 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002508 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002509 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002510 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002511 ret = VM_FAULT_MAJOR;
2512retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002513 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (!page)
2515 goto no_cached_page;
2516 }
2517
Dave Jiang11bac802017-02-24 14:56:41 -08002518 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002519 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002520 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002521 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002522
2523 /* Did it get truncated? */
2524 if (unlikely(page->mapping != mapping)) {
2525 unlock_page(page);
2526 put_page(page);
2527 goto retry_find;
2528 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002529 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002532 * We have a locked page in the page cache, now we need to check
2533 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 */
Nick Piggind00806b2007-07-19 01:46:57 -07002535 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 goto page_not_uptodate;
2537
Linus Torvaldsef00e082009-06-16 15:31:25 -07002538 /*
2539 * Found the page and have a reference on it.
2540 * We must recheck i_size under page lock.
2541 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002542 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2543 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002544 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002545 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002546 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002547 }
2548
Nick Piggind0217ac2007-07-19 01:47:03 -07002549 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002550 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552no_cached_page:
2553 /*
2554 * We're only likely to ever get here if MADV_RANDOM is in
2555 * effect.
2556 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002557 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /*
2560 * The page we want has now been added to the page cache.
2561 * In the unlikely event that someone removed it in the
2562 * meantime, we'll just come back here and read it again.
2563 */
2564 if (error >= 0)
2565 goto retry_find;
2566
2567 /*
2568 * An error return from page_cache_read can result if the
2569 * system is low on memory, or a problem occurs while trying
2570 * to schedule I/O.
2571 */
Souptick Joarder3c051322018-10-26 15:08:47 -07002572 return vmf_error(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /*
2576 * Umm, take care of errors if the page isn't up-to-date.
2577 * Try to re-read it _once_. We do this synchronously,
2578 * because there really aren't any performance issues here
2579 * and we need to check for errors.
2580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002582 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002583 if (!error) {
2584 wait_on_page_locked(page);
2585 if (!PageUptodate(page))
2586 error = -EIO;
2587 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002588 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002589
2590 if (!error || error == AOP_TRUNCATED_PAGE)
2591 goto retry_find;
2592
2593 /* Things didn't work out. Return zero to tell the mm layer so. */
2594 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002595 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002596}
2597EXPORT_SYMBOL(filemap_fault);
2598
Jan Kara82b0f8c2016-12-14 15:06:58 -08002599void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002600 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002601{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002602 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002603 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002604 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002605 unsigned long max_idx;
Matthew Wilcox070e8072018-05-17 00:08:30 -04002606 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002607 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002608
2609 rcu_read_lock();
Matthew Wilcox070e8072018-05-17 00:08:30 -04002610 xas_for_each(&xas, page, end_pgoff) {
2611 if (xas_retry(&xas, page))
2612 continue;
2613 if (xa_is_value(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002614 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002615
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002616 head = compound_head(page);
2617 if (!page_cache_get_speculative(head))
Matthew Wilcox070e8072018-05-17 00:08:30 -04002618 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002619
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002620 /* The page was split under us? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002621 if (compound_head(page) != head)
2622 goto skip;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002623
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002624 /* Has the page moved? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002625 if (unlikely(page != xas_reload(&xas)))
2626 goto skip;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002627
2628 if (!PageUptodate(page) ||
2629 PageReadahead(page) ||
2630 PageHWPoison(page))
2631 goto skip;
2632 if (!trylock_page(page))
2633 goto skip;
2634
2635 if (page->mapping != mapping || !PageUptodate(page))
2636 goto unlock;
2637
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002638 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2639 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002640 goto unlock;
2641
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002642 if (file->f_ra.mmap_miss > 0)
2643 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002644
Matthew Wilcox070e8072018-05-17 00:08:30 -04002645 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002646 if (vmf->pte)
Matthew Wilcox070e8072018-05-17 00:08:30 -04002647 vmf->pte += xas.xa_index - last_pgoff;
2648 last_pgoff = xas.xa_index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002649 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002650 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002651 unlock_page(page);
2652 goto next;
2653unlock:
2654 unlock_page(page);
2655skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002656 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002657next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002658 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002659 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002660 break;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002661 }
2662 rcu_read_unlock();
2663}
2664EXPORT_SYMBOL(filemap_map_pages);
2665
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002666vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002667{
2668 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002669 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002670 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002671
Jan Kara14da9202012-06-12 16:20:37 +02002672 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002673 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002674 lock_page(page);
2675 if (page->mapping != inode->i_mapping) {
2676 unlock_page(page);
2677 ret = VM_FAULT_NOPAGE;
2678 goto out;
2679 }
Jan Kara14da9202012-06-12 16:20:37 +02002680 /*
2681 * We mark the page dirty already here so that when freeze is in
2682 * progress, we are guaranteed that writeback during freezing will
2683 * see the dirty page and writeprotect it again.
2684 */
2685 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002686 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002687out:
Jan Kara14da9202012-06-12 16:20:37 +02002688 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002689 return ret;
2690}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002691
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002692const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002693 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002694 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002695 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696};
2697
2698/* This is used for a general mmap of a disk file */
2699
2700int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2701{
2702 struct address_space *mapping = file->f_mapping;
2703
2704 if (!mapping->a_ops->readpage)
2705 return -ENOEXEC;
2706 file_accessed(file);
2707 vma->vm_ops = &generic_file_vm_ops;
2708 return 0;
2709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711/*
2712 * This is for filesystems which do not implement ->writepage.
2713 */
2714int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2715{
2716 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2717 return -EINVAL;
2718 return generic_file_mmap(file, vma);
2719}
2720#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002721vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann45397222018-04-13 15:35:27 -07002722{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002723 return VM_FAULT_SIGBUS;
Arnd Bergmann45397222018-04-13 15:35:27 -07002724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2726{
2727 return -ENOSYS;
2728}
2729int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2730{
2731 return -ENOSYS;
2732}
2733#endif /* CONFIG_MMU */
2734
Arnd Bergmann45397222018-04-13 15:35:27 -07002735EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736EXPORT_SYMBOL(generic_file_mmap);
2737EXPORT_SYMBOL(generic_file_readonly_mmap);
2738
Sasha Levin67f9fd92014-04-03 14:48:18 -07002739static struct page *wait_on_page_read(struct page *page)
2740{
2741 if (!IS_ERR(page)) {
2742 wait_on_page_locked(page);
2743 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002744 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002745 page = ERR_PTR(-EIO);
2746 }
2747 }
2748 return page;
2749}
2750
Mel Gorman32b63522016-03-15 14:55:36 -07002751static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002752 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002753 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002754 void *data,
2755 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Nick Piggineb2be182007-10-16 01:24:57 -07002757 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 int err;
2759repeat:
2760 page = find_get_page(mapping, index);
2761 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002762 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002763 if (!page)
2764 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002765 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002766 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002767 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002768 if (err == -EEXIST)
2769 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05002770 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return ERR_PTR(err);
2772 }
Mel Gorman32b63522016-03-15 14:55:36 -07002773
2774filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 err = filler(data, page);
2776 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002777 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002778 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Mel Gorman32b63522016-03-15 14:55:36 -07002780
2781 page = wait_on_page_read(page);
2782 if (IS_ERR(page))
2783 return page;
2784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (PageUptodate(page))
2787 goto out;
2788
Mel Gormanebded022016-03-15 14:55:39 -07002789 /*
2790 * Page is not up to date and may be locked due one of the following
2791 * case a: Page is being filled and the page lock is held
2792 * case b: Read/write error clearing the page uptodate status
2793 * case c: Truncation in progress (page locked)
2794 * case d: Reclaim in progress
2795 *
2796 * Case a, the page will be up to date when the page is unlocked.
2797 * There is no need to serialise on the page lock here as the page
2798 * is pinned so the lock gives no additional protection. Even if the
2799 * the page is truncated, the data is still valid if PageUptodate as
2800 * it's a race vs truncate race.
2801 * Case b, the page will not be up to date
2802 * Case c, the page may be truncated but in itself, the data may still
2803 * be valid after IO completes as it's a read vs truncate race. The
2804 * operation must restart if the page is not uptodate on unlock but
2805 * otherwise serialising on page lock to stabilise the mapping gives
2806 * no additional guarantees to the caller as the page lock is
2807 * released before return.
2808 * Case d, similar to truncation. If reclaim holds the page lock, it
2809 * will be a race with remove_mapping that determines if the mapping
2810 * is valid on unlock but otherwise the data is valid and there is
2811 * no need to serialise with page lock.
2812 *
2813 * As the page lock gives no additional guarantee, we optimistically
2814 * wait on the page to be unlocked and check if it's up to date and
2815 * use the page if it is. Otherwise, the page lock is required to
2816 * distinguish between the different cases. The motivation is that we
2817 * avoid spurious serialisations and wakeups when multiple processes
2818 * wait on the same page for IO to complete.
2819 */
2820 wait_on_page_locked(page);
2821 if (PageUptodate(page))
2822 goto out;
2823
2824 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002826
2827 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!page->mapping) {
2829 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002830 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002831 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Mel Gormanebded022016-03-15 14:55:39 -07002833
2834 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (PageUptodate(page)) {
2836 unlock_page(page);
2837 goto out;
2838 }
Mel Gorman32b63522016-03-15 14:55:36 -07002839 goto filler;
2840
David Howellsc855ff32007-05-09 13:42:20 +01002841out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002842 mark_page_accessed(page);
2843 return page;
2844}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002845
2846/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002847 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002848 * @mapping: the page's address_space
2849 * @index: the page index
2850 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002851 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002852 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002853 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002854 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002855 *
2856 * If the page does not get brought uptodate, return -EIO.
2857 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002858struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002859 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002860 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002861 void *data)
2862{
2863 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2864}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002865EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002866
2867/**
2868 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2869 * @mapping: the page's address_space
2870 * @index: the page index
2871 * @gfp: the page allocator flags to use if allocating
2872 *
2873 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002874 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002875 *
2876 * If the page does not get brought uptodate, return -EIO.
2877 */
2878struct page *read_cache_page_gfp(struct address_space *mapping,
2879 pgoff_t index,
2880 gfp_t gfp)
2881{
2882 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2883
Sasha Levin67f9fd92014-04-03 14:48:18 -07002884 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002885}
2886EXPORT_SYMBOL(read_cache_page_gfp);
2887
Nick Piggin2f718ff2007-10-16 01:24:59 -07002888/*
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002889 * Don't operate on ranges the page cache doesn't support, and don't exceed the
2890 * LFS limits. If pos is under the limit it becomes a short access. If it
2891 * exceeds the limit we return -EFBIG.
2892 */
2893static int generic_access_check_limits(struct file *file, loff_t pos,
2894 loff_t *count)
2895{
2896 struct inode *inode = file->f_mapping->host;
2897 loff_t max_size = inode->i_sb->s_maxbytes;
2898
2899 if (!(file->f_flags & O_LARGEFILE))
2900 max_size = MAX_NON_LFS;
2901
2902 if (unlikely(pos >= max_size))
2903 return -EFBIG;
2904 *count = min(*count, max_size - pos);
2905 return 0;
2906}
2907
2908static int generic_write_check_limits(struct file *file, loff_t pos,
2909 loff_t *count)
2910{
2911 loff_t limit = rlimit(RLIMIT_FSIZE);
2912
2913 if (limit != RLIM_INFINITY) {
2914 if (pos >= limit) {
2915 send_sig(SIGXFSZ, current, 0);
2916 return -EFBIG;
2917 }
2918 *count = min(*count, limit - pos);
2919 }
2920
2921 return generic_access_check_limits(file, pos, count);
2922}
2923
2924/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 * Performs necessary checks before doing a write
2926 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002927 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 * Returns appropriate error code that caller should return or
2929 * zero in case that write should be allowed.
2930 */
Al Viro3309dd02015-04-09 12:55:47 -04002931inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Al Viro3309dd02015-04-09 12:55:47 -04002933 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct inode *inode = file->f_mapping->host;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002935 loff_t count;
2936 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Al Viro3309dd02015-04-09 12:55:47 -04002938 if (!iov_iter_count(from))
2939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Al Viro0fa6b002015-04-04 04:05:48 -04002941 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002942 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002943 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002945 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2946 return -EINVAL;
2947
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002948 count = iov_iter_count(from);
2949 ret = generic_write_check_limits(file, iocb->ki_pos, &count);
2950 if (ret)
2951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002953 iov_iter_truncate(from, count);
Al Viro3309dd02015-04-09 12:55:47 -04002954 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956EXPORT_SYMBOL(generic_write_checks);
2957
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002958/*
2959 * Performs necessary checks before doing a clone.
2960 *
2961 * Can adjust amount of bytes to clone.
2962 * Returns appropriate error code that caller should return or
2963 * zero in case the clone should be allowed.
2964 */
2965int generic_remap_checks(struct file *file_in, loff_t pos_in,
2966 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002967 loff_t *req_count, unsigned int remap_flags)
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002968{
2969 struct inode *inode_in = file_in->f_mapping->host;
2970 struct inode *inode_out = file_out->f_mapping->host;
2971 uint64_t count = *req_count;
2972 uint64_t bcount;
2973 loff_t size_in, size_out;
2974 loff_t bs = inode_out->i_sb->s_blocksize;
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002975 int ret;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002976
2977 /* The start of both ranges must be aligned to an fs block. */
2978 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
2979 return -EINVAL;
2980
2981 /* Ensure offsets don't wrap. */
2982 if (pos_in + count < pos_in || pos_out + count < pos_out)
2983 return -EINVAL;
2984
2985 size_in = i_size_read(inode_in);
2986 size_out = i_size_read(inode_out);
2987
2988 /* Dedupe requires both ranges to be within EOF. */
Darrick J. Wong3d281932018-10-30 10:41:34 +11002989 if ((remap_flags & REMAP_FILE_DEDUP) &&
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002990 (pos_in >= size_in || pos_in + count > size_in ||
2991 pos_out >= size_out || pos_out + count > size_out))
2992 return -EINVAL;
2993
2994 /* Ensure the infile range is within the infile. */
2995 if (pos_in >= size_in)
2996 return -EINVAL;
2997 count = min(count, size_in - (uint64_t)pos_in);
2998
Darrick J. Wong9fd91a90cb2018-10-30 10:40:46 +11002999 ret = generic_access_check_limits(file_in, pos_in, &count);
3000 if (ret)
3001 return ret;
3002
3003 ret = generic_write_check_limits(file_out, pos_out, &count);
3004 if (ret)
3005 return ret;
3006
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003007 /*
3008 * If the user wanted us to link to the infile's EOF, round up to the
3009 * next block boundary for this check.
3010 *
3011 * Otherwise, make sure the count is also block-aligned, having
3012 * already confirmed the starting offsets' block alignment.
3013 */
3014 if (pos_in + count == size_in) {
3015 bcount = ALIGN(size_in, bs) - pos_in;
3016 } else {
3017 if (!IS_ALIGNED(count, bs))
Darrick J. Wongeca36542018-10-30 10:42:10 +11003018 count = ALIGN_DOWN(count, bs);
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003019 bcount = count;
3020 }
3021
3022 /* Don't allow overlapped cloning within the same file. */
3023 if (inode_in == inode_out &&
3024 pos_out + bcount > pos_in &&
3025 pos_out < pos_in + bcount)
3026 return -EINVAL;
3027
Darrick J. Wongeca36542018-10-30 10:42:10 +11003028 /*
3029 * We shortened the request but the caller can't deal with that, so
3030 * bounce the request back to userspace.
3031 */
3032 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003033 return -EINVAL;
3034
Darrick J. Wongeca36542018-10-30 10:42:10 +11003035 *req_count = count;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11003036 return 0;
3037}
3038
Nick Pigginafddba42007-10-16 01:25:01 -07003039int pagecache_write_begin(struct file *file, struct address_space *mapping,
3040 loff_t pos, unsigned len, unsigned flags,
3041 struct page **pagep, void **fsdata)
3042{
3043 const struct address_space_operations *aops = mapping->a_ops;
3044
Nick Piggin4e02ed42008-10-29 14:00:55 -07003045 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003046 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003047}
3048EXPORT_SYMBOL(pagecache_write_begin);
3049
3050int pagecache_write_end(struct file *file, struct address_space *mapping,
3051 loff_t pos, unsigned len, unsigned copied,
3052 struct page *page, void *fsdata)
3053{
3054 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003055
Nick Piggin4e02ed42008-10-29 14:00:55 -07003056 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003057}
3058EXPORT_SYMBOL(pagecache_write_end);
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003061generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 struct file *file = iocb->ki_filp;
3064 struct address_space *mapping = file->f_mapping;
3065 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003066 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003068 size_t write_len;
3069 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Al Viro0c949332014-03-22 06:51:37 -04003071 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003072 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003073
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003074 if (iocb->ki_flags & IOCB_NOWAIT) {
3075 /* If there are pages to writeback, return */
3076 if (filemap_range_has_page(inode->i_mapping, pos,
haiqing.shq3cb7b122018-10-26 15:07:22 -07003077 pos + write_len))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003078 return -EAGAIN;
3079 } else {
3080 written = filemap_write_and_wait_range(mapping, pos,
3081 pos + write_len - 1);
3082 if (written)
3083 goto out;
3084 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003085
3086 /*
3087 * After a write we want buffered reads to be sure to go to disk to get
3088 * the new data. We invalidate clean cached page from the region we're
3089 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003090 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003091 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003092 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003093 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003094 /*
3095 * If a page can not be invalidated, return 0 to fall back
3096 * to buffered write.
3097 */
3098 if (written) {
3099 if (written == -EBUSY)
3100 return 0;
3101 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003102 }
3103
Al Viro639a93a52017-04-13 14:10:15 -04003104 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003105
3106 /*
3107 * Finally, try again to invalidate clean pages which might have been
3108 * cached by non-direct readahead, or faulted in by get_user_pages()
3109 * if the source of the write was an mmap'ed region of the file
3110 * we're writing. Either one is a pretty crazy thing to do,
3111 * so we don't support it 100%. If this invalidation
3112 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003113 *
3114 * Most of the time we do not need this since dio_complete() will do
3115 * the invalidation for us. However there are some file systems that
3116 * do not end up with dio_complete() being called, so let's not break
3117 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003118 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003119 if (mapping->nrpages)
3120 invalidate_inode_pages2_range(mapping,
3121 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003124 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003125 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003126 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3127 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 mark_inode_dirty(inode);
3129 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003130 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
Al Viro639a93a52017-04-13 14:10:15 -04003132 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003133out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return written;
3135}
3136EXPORT_SYMBOL(generic_file_direct_write);
3137
Nick Piggineb2be182007-10-16 01:24:57 -07003138/*
3139 * Find or create a page at the given pagecache position. Return the locked
3140 * page. This function is specifically for buffered writes.
3141 */
Nick Piggin54566b22009-01-04 12:00:53 -08003142struct page *grab_cache_page_write_begin(struct address_space *mapping,
3143 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003144{
Nick Piggineb2be182007-10-16 01:24:57 -07003145 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003146 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003147
Nick Piggin54566b22009-01-04 12:00:53 -08003148 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003149 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003150
Mel Gorman2457aec2014-06-04 16:10:31 -07003151 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003152 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003153 if (page)
3154 wait_for_stable_page(page);
3155
Nick Piggineb2be182007-10-16 01:24:57 -07003156 return page;
3157}
Nick Piggin54566b22009-01-04 12:00:53 -08003158EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003159
Al Viro3b93f912014-02-11 21:34:08 -05003160ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003161 struct iov_iter *i, loff_t pos)
3162{
3163 struct address_space *mapping = file->f_mapping;
3164 const struct address_space_operations *a_ops = mapping->a_ops;
3165 long status = 0;
3166 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003167 unsigned int flags = 0;
3168
Nick Pigginafddba42007-10-16 01:25:01 -07003169 do {
3170 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003171 unsigned long offset; /* Offset into pagecache page */
3172 unsigned long bytes; /* Bytes to write to page */
3173 size_t copied; /* Bytes copied from user */
3174 void *fsdata;
3175
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003176 offset = (pos & (PAGE_SIZE - 1));
3177 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003178 iov_iter_count(i));
3179
3180again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003181 /*
3182 * Bring in the user page that we will copy from _first_.
3183 * Otherwise there's a nasty deadlock on copying from the
3184 * same page as we're writing to, without it being marked
3185 * up-to-date.
3186 *
3187 * Not only is this an optimisation, but it is also required
3188 * to check that the address is actually valid, when atomic
3189 * usercopies are used, below.
3190 */
3191 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3192 status = -EFAULT;
3193 break;
3194 }
3195
Jan Kara296291c2015-10-22 13:32:21 -07003196 if (fatal_signal_pending(current)) {
3197 status = -EINTR;
3198 break;
3199 }
3200
Nick Piggin674b8922007-10-16 01:25:03 -07003201 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003202 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003203 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003204 break;
3205
anfei zhou931e80e2010-02-02 13:44:02 -08003206 if (mapping_writably_mapped(mapping))
3207 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003208
Nick Pigginafddba42007-10-16 01:25:01 -07003209 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003210 flush_dcache_page(page);
3211
3212 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3213 page, fsdata);
3214 if (unlikely(status < 0))
3215 break;
3216 copied = status;
3217
3218 cond_resched();
3219
Nick Piggin124d3b72008-02-02 15:01:17 +01003220 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003221 if (unlikely(copied == 0)) {
3222 /*
3223 * If we were unable to copy any data at all, we must
3224 * fall back to a single segment length write.
3225 *
3226 * If we didn't fallback here, we could livelock
3227 * because not all segments in the iov can be copied at
3228 * once without a pagefault.
3229 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003230 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003231 iov_iter_single_seg_count(i));
3232 goto again;
3233 }
Nick Pigginafddba42007-10-16 01:25:01 -07003234 pos += copied;
3235 written += copied;
3236
3237 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003238 } while (iov_iter_count(i));
3239
3240 return written ? written : status;
3241}
Al Viro3b93f912014-02-11 21:34:08 -05003242EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Jan Karae4dd9de2009-08-17 18:10:06 +02003244/**
Al Viro81742022014-04-03 03:17:43 -04003245 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003246 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003247 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003248 *
3249 * This function does all the work needed for actually writing data to a
3250 * file. It does all basic checks, removes SUID from the file, updates
3251 * modification times and calls proper subroutines depending on whether we
3252 * do direct IO or a standard buffered write.
3253 *
3254 * It expects i_mutex to be grabbed unless we work on a block device or similar
3255 * object which does not need locking at all.
3256 *
3257 * This function does *not* take care of syncing data in case of O_SYNC write.
3258 * A caller has to handle it. This is mainly due to the fact that we want to
3259 * avoid syncing under i_mutex.
3260 */
Al Viro81742022014-04-03 03:17:43 -04003261ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003264 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003266 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003268 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003271 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003272 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (err)
3274 goto out;
3275
Josef Bacikc3b2da32012-03-26 09:59:21 -04003276 err = file_update_time(file);
3277 if (err)
3278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Al Viro2ba48ce2015-04-09 13:52:01 -04003280 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003281 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003282
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003283 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003284 /*
3285 * If the write stopped short of completing, fall back to
3286 * buffered writes. Some filesystems do this for writes to
3287 * holes, for example. For DAX files, a buffered write will
3288 * not succeed (even if it did, DAX does not handle dirty
3289 * page-cache pages correctly).
3290 */
Al Viro0b8def92015-04-07 10:22:53 -04003291 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003293
Al Viro0b8def92015-04-07 10:22:53 -04003294 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003295 /*
Al Viro3b93f912014-02-11 21:34:08 -05003296 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003297 * then we want to return the number of bytes which were
3298 * direct-written, or the error code if that was zero. Note
3299 * that this differs from normal direct-io semantics, which
3300 * will return -EFOO even if some bytes were written.
3301 */
Al Viro60bb4522014-08-08 12:39:16 -04003302 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003303 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003304 goto out;
3305 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003306 /*
3307 * We need to ensure that the page cache pages are written to
3308 * disk and invalidated to preserve the expected O_DIRECT
3309 * semantics.
3310 */
Al Viro3b93f912014-02-11 21:34:08 -05003311 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003312 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003313 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003314 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003315 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003316 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003317 pos >> PAGE_SHIFT,
3318 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003319 } else {
3320 /*
3321 * We don't know how much we wrote, so just return
3322 * the number of bytes which were direct-written
3323 */
3324 }
3325 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003326 written = generic_perform_write(file, from, iocb->ki_pos);
3327 if (likely(written > 0))
3328 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330out:
3331 current->backing_dev_info = NULL;
3332 return written ? written : err;
3333}
Al Viro81742022014-04-03 03:17:43 -04003334EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Jan Karae4dd9de2009-08-17 18:10:06 +02003336/**
Al Viro81742022014-04-03 03:17:43 -04003337 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003338 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003339 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003340 *
Al Viro81742022014-04-03 03:17:43 -04003341 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003342 * filesystems. It takes care of syncing the file in case of O_SYNC file
3343 * and acquires i_mutex as needed.
3344 */
Al Viro81742022014-04-03 03:17:43 -04003345ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003348 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Al Viro59551022016-01-22 15:40:57 -05003351 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003352 ret = generic_write_checks(iocb, from);
3353 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003354 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003355 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Christoph Hellwige2592212016-04-07 08:52:01 -07003357 if (ret > 0)
3358 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return ret;
3360}
Al Viro81742022014-04-03 03:17:43 -04003361EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
David Howellscf9a2ae2006-08-29 19:05:54 +01003363/**
3364 * try_to_release_page() - release old fs-specific metadata on a page
3365 *
3366 * @page: the page which the kernel is trying to free
3367 * @gfp_mask: memory allocation flags (and I/O mode)
3368 *
3369 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003370 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003371 * Otherwise return zero.
3372 *
David Howells266cf652009-04-03 16:42:36 +01003373 * This may also be called if PG_fscache is set on a page, indicating that the
3374 * page is known to the local caching routines.
3375 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003376 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003377 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003378 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003379 */
3380int try_to_release_page(struct page *page, gfp_t gfp_mask)
3381{
3382 struct address_space * const mapping = page->mapping;
3383
3384 BUG_ON(!PageLocked(page));
3385 if (PageWriteback(page))
3386 return 0;
3387
3388 if (mapping && mapping->a_ops->releasepage)
3389 return mapping->a_ops->releasepage(page, gfp_mask);
3390 return try_to_free_buffers(page);
3391}
3392
3393EXPORT_SYMBOL(try_to_release_page);