blob: bbcbb44beeeae1290a14cf5f2342925c3438cf73 [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 Weiner9c29ace2018-10-26 15:06:08 -070039#include <linux/delayacct.h>
Johannes Weiner25f0c862018-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
Vinayak Menon3aee78b2016-05-20 16:58:41 -070053int want_old_faultaround_pte = 1;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * Shared mappings implemented 30.11.1994. It's not fully working yet,
57 * though.
58 *
59 * Shared mappings now work. 15.8.1995 Bruno.
60 *
61 * finished 'unifying' the page and buffer cache and SMP-threaded the
62 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
63 *
64 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
65 */
66
67/*
68 * Lock ordering:
69 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080070 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070072 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070073 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080075 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080076 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080079 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070080 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070081 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 * ->mmap_sem
84 * ->lock_page (access_process_vm)
85 *
Al Viroccad2362014-02-11 22:36:48 -050086 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070087 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060089 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110090 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070091 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080093 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ->anon_vma.lock (vma_adjust)
95 *
96 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070097 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070099 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -0700100 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700102 * ->i_pages lock (try_to_unmap_one)
Mel Gormana52633d2016-07-28 15:45:28 -0700103 * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
104 * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700106 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600107 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100108 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700109 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600110 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100111 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
113 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800114 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700115 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700118static int page_cache_tree_insert(struct address_space *mapping,
119 struct page *page, void **shadowp)
120{
121 struct radix_tree_node *node;
122 void **slot;
123 int error;
124
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700125 error = __radix_tree_create(&mapping->i_pages, page->index, 0,
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700126 &node, &slot);
127 if (error)
128 return error;
129 if (*slot) {
130 void *p;
131
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700132 p = radix_tree_deref_slot_protected(slot,
133 &mapping->i_pages.xa_lock);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700134 if (!radix_tree_exceptional_entry(p))
135 return -EEXIST;
136
137 mapping->nrexceptional--;
Ross Zwislerd01ad192017-09-06 16:18:47 -0700138 if (shadowp)
139 *shadowp = p;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700140 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700141 __radix_tree_replace(&mapping->i_pages, node, slot, page,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800142 workingset_lookup_update(mapping));
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700143 mapping->nrpages++;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700144 return 0;
145}
146
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700147static void page_cache_tree_delete(struct address_space *mapping,
148 struct page *page, void *shadow)
149{
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800150 int i, nr;
151
152 /* hugetlb pages are represented by one entry in the radix tree */
153 nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700154
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700155 VM_BUG_ON_PAGE(!PageLocked(page), page);
156 VM_BUG_ON_PAGE(PageTail(page), page);
157 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700158
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700159 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200160 struct radix_tree_node *node;
161 void **slot;
162
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700163 __radix_tree_lookup(&mapping->i_pages, page->index + i,
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200164 &node, &slot);
165
Johannes Weinerdbc446b2016-12-12 16:43:55 -0800166 VM_BUG_ON_PAGE(!node && nr != 1, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700167
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700168 radix_tree_clear_tags(&mapping->i_pages, node, slot);
169 __radix_tree_replace(&mapping->i_pages, node, slot, shadow,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800170 workingset_lookup_update(mapping));
Johannes Weiner449dd692014-04-03 14:47:56 -0700171 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200172
Jan Kara23006382017-11-15 17:37:26 -0800173 page->mapping = NULL;
174 /* Leave page->index set: truncation lookup relies upon it */
175
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200176 if (shadow) {
177 mapping->nrexceptional += nr;
178 /*
179 * Make sure the nrexceptional update is committed before
180 * the nrpages update so that final truncate racing
181 * with reclaim does not see both counters 0 at the
182 * same time and miss a shadow entry.
183 */
184 smp_wmb();
185 }
186 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700187}
188
Jan Kara5ecc4d82017-11-15 17:37:29 -0800189static void unaccount_page_cache_page(struct address_space *mapping,
190 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800192 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600194 /*
195 * if we're uptodate, flush out into the cleancache, otherwise
196 * invalidate any existing cleancache entries. We can't leave
197 * stale data around in the cleancache once our page is gone
198 */
Vinayak Menonf1571342015-07-13 13:24:55 +0530199 if (PageUptodate(page) && PageMappedToDisk(page)) {
200 count_vm_event(PGPGOUTCLEAN);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600201 cleancache_put_page(page);
Vinayak Menonf1571342015-07-13 13:24:55 +0530202 } else {
Dan Magenheimer31677602011-09-21 11:56:28 -0400203 cleancache_invalidate_page(mapping, page);
Vinayak Menonf1571342015-07-13 13:24:55 +0530204 }
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600205
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700206 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800207 VM_BUG_ON_PAGE(page_mapped(page), page);
208 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
209 int mapcount;
210
211 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
212 current->comm, page_to_pfn(page));
213 dump_page(page, "still mapped when deleted");
214 dump_stack();
215 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
216
217 mapcount = page_mapcount(page);
218 if (mapping_exiting(mapping) &&
219 page_count(page) >= mapcount + 2) {
220 /*
221 * All vmas have already been torn down, so it's
222 * a good bet that actually the page is unmapped,
223 * and we'd prefer not to leak it: if we're wrong,
224 * some other bad page check should catch it later.
225 */
226 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700227 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800228 }
229 }
230
Jan Kara76253fb2017-11-15 17:37:22 -0800231 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800232 if (PageHuge(page))
233 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800234
Jan Kara5ecc4d82017-11-15 17:37:29 -0800235 nr = hpage_nr_pages(page);
236
237 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
238 if (PageSwapBacked(page)) {
239 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
240 if (PageTransHuge(page))
241 __dec_node_page_state(page, NR_SHMEM_THPS);
242 } else {
243 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800244 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800245
246 /*
247 * At this point page must be either written or cleaned by
248 * truncate. Dirty page here signals a bug and loss of
249 * unwritten data.
250 *
251 * This fixes dirty accounting after removing the page entirely
252 * but leaves PageDirty set: it has no effect for truncated
253 * page and anyway will be cleared before returning page into
254 * buddy allocator.
255 */
256 if (WARN_ON_ONCE(PageDirty(page)))
257 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
258}
259
260/*
261 * Delete a page from the page cache and free it. Caller has to make
262 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700263 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800264 */
265void __delete_from_page_cache(struct page *page, void *shadow)
266{
267 struct address_space *mapping = page->mapping;
268
269 trace_mm_filemap_delete_from_page_cache(page);
270
271 unaccount_page_cache_page(mapping, page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700272 page_cache_tree_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Jan Kara59c66c52017-11-15 17:37:18 -0800275static void page_cache_free_page(struct address_space *mapping,
276 struct page *page)
277{
278 void (*freepage)(struct page *);
279
280 freepage = mapping->a_ops->freepage;
281 if (freepage)
282 freepage(page);
283
284 if (PageTransHuge(page) && !PageHuge(page)) {
285 page_ref_sub(page, HPAGE_PMD_NR);
286 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
287 } else {
288 put_page(page);
289 }
290}
291
Minchan Kim702cfbf2011-03-22 16:32:43 -0700292/**
293 * delete_from_page_cache - delete page from page cache
294 * @page: the page which the kernel is trying to remove from page cache
295 *
296 * This must be called only on pages that have been verified to be in the page
297 * cache and locked. It will never put the page into the free list, the caller
298 * has a reference on the page.
299 */
300void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700302 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400303 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Matt Mackallcd7619d2005-05-01 08:59:01 -0700305 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700306 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700307 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700308 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500309
Jan Kara59c66c52017-11-15 17:37:18 -0800310 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700311}
312EXPORT_SYMBOL(delete_from_page_cache);
313
Jan Karaaa65c292017-11-15 17:37:33 -0800314/*
315 * page_cache_tree_delete_batch - delete several pages from page cache
316 * @mapping: the mapping to which pages belong
317 * @pvec: pagevec with pages to delete
318 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700319 * The function walks over mapping->i_pages and removes pages passed in @pvec
320 * from the mapping. The function expects @pvec to be sorted by page index.
321 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800322 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700323 * @pvec and takes care to delete all corresponding tail pages from the
324 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800325 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700326 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800327 */
328static void
329page_cache_tree_delete_batch(struct address_space *mapping,
330 struct pagevec *pvec)
331{
332 struct radix_tree_iter iter;
333 void **slot;
334 int total_pages = 0;
335 int i = 0, tail_pages = 0;
336 struct page *page;
337 pgoff_t start;
338
339 start = pvec->pages[0]->index;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700340 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Jan Karaaa65c292017-11-15 17:37:33 -0800341 if (i >= pagevec_count(pvec) && !tail_pages)
342 break;
343 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700344 &mapping->i_pages.xa_lock);
Jan Karaaa65c292017-11-15 17:37:33 -0800345 if (radix_tree_exceptional_entry(page))
346 continue;
347 if (!tail_pages) {
348 /*
349 * Some page got inserted in our range? Skip it. We
350 * have our pages locked so they are protected from
351 * being removed.
352 */
353 if (page != pvec->pages[i])
354 continue;
355 WARN_ON_ONCE(!PageLocked(page));
356 if (PageTransHuge(page) && !PageHuge(page))
357 tail_pages = HPAGE_PMD_NR - 1;
358 page->mapping = NULL;
359 /*
360 * Leave page->index set: truncation lookup relies
361 * upon it
362 */
363 i++;
364 } else {
365 tail_pages--;
366 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700367 radix_tree_clear_tags(&mapping->i_pages, iter.node, slot);
368 __radix_tree_replace(&mapping->i_pages, iter.node, slot, NULL,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800369 workingset_lookup_update(mapping));
Jan Karaaa65c292017-11-15 17:37:33 -0800370 total_pages++;
371 }
372 mapping->nrpages -= total_pages;
373}
374
375void delete_from_page_cache_batch(struct address_space *mapping,
376 struct pagevec *pvec)
377{
378 int i;
379 unsigned long flags;
380
381 if (!pagevec_count(pvec))
382 return;
383
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700384 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800385 for (i = 0; i < pagevec_count(pvec); i++) {
386 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
387
388 unaccount_page_cache_page(mapping, pvec->pages[i]);
389 }
390 page_cache_tree_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700391 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800392
393 for (i = 0; i < pagevec_count(pvec); i++)
394 page_cache_free_page(mapping, pvec->pages[i]);
395}
396
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200397int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700398{
399 int ret = 0;
400 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700401 if (test_bit(AS_ENOSPC, &mapping->flags) &&
402 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700403 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700404 if (test_bit(AS_EIO, &mapping->flags) &&
405 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700406 ret = -EIO;
407 return ret;
408}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200409EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700410
Jeff Layton76341ca2017-07-06 07:02:22 -0400411static int filemap_check_and_keep_errors(struct address_space *mapping)
412{
413 /* Check for outstanding write errors */
414 if (test_bit(AS_EIO, &mapping->flags))
415 return -EIO;
416 if (test_bit(AS_ENOSPC, &mapping->flags))
417 return -ENOSPC;
418 return 0;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700422 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700423 * @mapping: address space structure to write
424 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800425 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700426 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700428 * Start writeback against all of a mapping's dirty pages that lie
429 * within the byte offsets <start, end> inclusive.
430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700432 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * these two operations is that if a dirty page/buffer is encountered, it must
434 * be waited upon, and not just skipped over.
435 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800436int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
437 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 int ret;
440 struct writeback_control wbc = {
441 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800442 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700443 .range_start = start,
444 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 };
446
447 if (!mapping_cap_writeback_dirty(mapping))
448 return 0;
449
Tejun Heob16b1de2015-06-02 08:39:48 -0600450 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600452 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return ret;
454}
455
456static inline int __filemap_fdatawrite(struct address_space *mapping,
457 int sync_mode)
458{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700459 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462int filemap_fdatawrite(struct address_space *mapping)
463{
464 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
465}
466EXPORT_SYMBOL(filemap_fdatawrite);
467
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400468int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800469 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
472}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400473EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Randy Dunlap485bb992006-06-23 02:03:49 -0700475/**
476 * filemap_flush - mostly a non-blocking flush
477 * @mapping: target address_space
478 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * This is a mostly non-blocking flush. Not suitable for data-integrity
480 * purposes - I/O may not be started against all dirty pages.
481 */
482int filemap_flush(struct address_space *mapping)
483{
484 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
485}
486EXPORT_SYMBOL(filemap_flush);
487
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500488/**
489 * filemap_range_has_page - check if a page exists in range.
490 * @mapping: address space within which to check
491 * @start_byte: offset in bytes where the range starts
492 * @end_byte: offset in bytes where the range ends (inclusive)
493 *
494 * Find at least one page in the range supplied, usually used to check if
495 * direct writing in this range will trigger a writeback.
496 */
497bool filemap_range_has_page(struct address_space *mapping,
498 loff_t start_byte, loff_t end_byte)
499{
500 pgoff_t index = start_byte >> PAGE_SHIFT;
501 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700502 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500503
504 if (end_byte < start_byte)
505 return false;
506
507 if (mapping->nrpages == 0)
508 return false;
509
Jan Karaf7b68042017-09-06 16:21:40 -0700510 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500511 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700512 put_page(page);
513 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500514}
515EXPORT_SYMBOL(filemap_range_has_page);
516
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400517static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800518 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 pgoff_t index = start_byte >> PAGE_SHIFT;
521 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct pagevec pvec;
523 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200525 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Mel Gorman86679822017-11-15 17:37:52 -0800528 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800529 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned i;
531
Jan Kara312e9d22017-11-15 17:35:05 -0800532 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800533 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800534 if (!nr_pages)
535 break;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 for (i = 0; i < nr_pages; i++) {
538 struct page *page = pvec.pages[i];
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400541 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 pagevec_release(&pvec);
544 cond_resched();
545 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800546}
547
548/**
549 * filemap_fdatawait_range - wait for writeback to complete
550 * @mapping: address space structure to wait for
551 * @start_byte: offset in bytes where the range starts
552 * @end_byte: offset in bytes where the range ends (inclusive)
553 *
554 * Walk the list of under-writeback pages of the given address space
555 * in the given range and wait for all of them. Check error status of
556 * the address space and return it.
557 *
558 * Since the error status of the address space is cleared by this function,
559 * callers are responsible for checking the return value and handling and/or
560 * reporting the error.
561 */
562int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
563 loff_t end_byte)
564{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400565 __filemap_fdatawait_range(mapping, start_byte, end_byte);
566 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200568EXPORT_SYMBOL(filemap_fdatawait_range);
569
570/**
Jeff Laytona823e452017-07-28 07:24:43 -0400571 * file_fdatawait_range - wait for writeback to complete
572 * @file: file pointing to address space structure to wait for
573 * @start_byte: offset in bytes where the range starts
574 * @end_byte: offset in bytes where the range ends (inclusive)
575 *
576 * Walk the list of under-writeback pages of the address space that file
577 * refers to, in the given range and wait for all of them. Check error
578 * status of the address space vs. the file->f_wb_err cursor and return it.
579 *
580 * Since the error status of the file is advanced by this function,
581 * callers are responsible for checking the return value and handling and/or
582 * reporting the error.
583 */
584int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
585{
586 struct address_space *mapping = file->f_mapping;
587
588 __filemap_fdatawait_range(mapping, start_byte, end_byte);
589 return file_check_and_advance_wb_err(file);
590}
591EXPORT_SYMBOL(file_fdatawait_range);
592
593/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800594 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
595 * @mapping: address space structure to wait for
596 *
597 * Walk the list of under-writeback pages of the given address space
598 * and wait for all of them. Unlike filemap_fdatawait(), this function
599 * does not clear error status of the address space.
600 *
601 * Use this function if callers don't handle errors themselves. Expected
602 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
603 * fsfreeze(8)
604 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400605int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800606{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400607 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400608 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800609}
Jeff Layton76341ca2017-07-06 07:02:22 -0400610EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800611
Jeff Layton9326c9b2017-07-26 10:21:11 -0400612static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400614 return (!dax_mapping(mapping) && mapping->nrpages) ||
615 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618int filemap_write_and_wait(struct address_space *mapping)
619{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800620 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Jeff Layton9326c9b2017-07-26 10:21:11 -0400622 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800623 err = filemap_fdatawrite(mapping);
624 /*
625 * Even if the above returned error, the pages may be
626 * written partially (e.g. -ENOSPC), so we wait for it.
627 * But the -EIO is special case, it may indicate the worst
628 * thing (e.g. bug) happened, so we avoid waiting for it.
629 */
630 if (err != -EIO) {
631 int err2 = filemap_fdatawait(mapping);
632 if (!err)
633 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400634 } else {
635 /* Clear any previously stored errors */
636 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800637 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700638 } else {
639 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800643EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Randy Dunlap485bb992006-06-23 02:03:49 -0700645/**
646 * filemap_write_and_wait_range - write out & wait on a file range
647 * @mapping: the address_space for the pages
648 * @lstart: offset in bytes where the range starts
649 * @lend: offset in bytes where the range ends (inclusive)
650 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800651 * Write out and wait upon file offsets lstart->lend, inclusive.
652 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300653 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800654 * that this function can be used to write to the very end-of-file (end = -1).
655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656int filemap_write_and_wait_range(struct address_space *mapping,
657 loff_t lstart, loff_t lend)
658{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800659 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jeff Layton9326c9b2017-07-26 10:21:11 -0400661 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800662 err = __filemap_fdatawrite_range(mapping, lstart, lend,
663 WB_SYNC_ALL);
664 /* See comment of filemap_write_and_wait() */
665 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200666 int err2 = filemap_fdatawait_range(mapping,
667 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800668 if (!err)
669 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400670 } else {
671 /* Clear any previously stored errors */
672 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800673 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700674 } else {
675 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
Chris Masonf6995582009-04-15 13:22:37 -0400679EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Jeff Layton5660e132017-07-06 07:02:25 -0400681void __filemap_set_wb_err(struct address_space *mapping, int err)
682{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400683 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400684
685 trace_filemap_set_wb_err(mapping, eseq);
686}
687EXPORT_SYMBOL(__filemap_set_wb_err);
688
689/**
690 * file_check_and_advance_wb_err - report wb error (if any) that was previously
691 * and advance wb_err to current one
692 * @file: struct file on which the error is being reported
693 *
694 * When userland calls fsync (or something like nfsd does the equivalent), we
695 * want to report any writeback errors that occurred since the last fsync (or
696 * since the file was opened if there haven't been any).
697 *
698 * Grab the wb_err from the mapping. If it matches what we have in the file,
699 * then just quickly return 0. The file is all caught up.
700 *
701 * If it doesn't match, then take the mapping value, set the "seen" flag in
702 * it and try to swap it into place. If it works, or another task beat us
703 * to it with the new value, then update the f_wb_err and return the error
704 * portion. The error at this point must be reported via proper channels
705 * (a'la fsync, or NFS COMMIT operation, etc.).
706 *
707 * While we handle mapping->wb_err with atomic operations, the f_wb_err
708 * value is protected by the f_lock since we must ensure that it reflects
709 * the latest value swapped in for this file descriptor.
710 */
711int file_check_and_advance_wb_err(struct file *file)
712{
713 int err = 0;
714 errseq_t old = READ_ONCE(file->f_wb_err);
715 struct address_space *mapping = file->f_mapping;
716
717 /* Locklessly handle the common case where nothing has changed */
718 if (errseq_check(&mapping->wb_err, old)) {
719 /* Something changed, must use slow path */
720 spin_lock(&file->f_lock);
721 old = file->f_wb_err;
722 err = errseq_check_and_advance(&mapping->wb_err,
723 &file->f_wb_err);
724 trace_file_check_and_advance_wb_err(file, old);
725 spin_unlock(&file->f_lock);
726 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700727
728 /*
729 * We're mostly using this function as a drop in replacement for
730 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
731 * that the legacy code would have had on these flags.
732 */
733 clear_bit(AS_EIO, &mapping->flags);
734 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400735 return err;
736}
737EXPORT_SYMBOL(file_check_and_advance_wb_err);
738
739/**
740 * file_write_and_wait_range - write out & wait on a file range
741 * @file: file pointing to address_space with pages
742 * @lstart: offset in bytes where the range starts
743 * @lend: offset in bytes where the range ends (inclusive)
744 *
745 * Write out and wait upon file offsets lstart->lend, inclusive.
746 *
747 * Note that @lend is inclusive (describes the last byte to be written) so
748 * that this function can be used to write to the very end-of-file (end = -1).
749 *
750 * After writing out and waiting on the data, we check and advance the
751 * f_wb_err cursor to the latest value, and return any errors detected there.
752 */
753int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
754{
755 int err = 0, err2;
756 struct address_space *mapping = file->f_mapping;
757
Jeff Layton9326c9b2017-07-26 10:21:11 -0400758 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400759 err = __filemap_fdatawrite_range(mapping, lstart, lend,
760 WB_SYNC_ALL);
761 /* See comment of filemap_write_and_wait() */
762 if (err != -EIO)
763 __filemap_fdatawait_range(mapping, lstart, lend);
764 }
765 err2 = file_check_and_advance_wb_err(file);
766 if (!err)
767 err = err2;
768 return err;
769}
770EXPORT_SYMBOL(file_write_and_wait_range);
771
Randy Dunlap485bb992006-06-23 02:03:49 -0700772/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700773 * replace_page_cache_page - replace a pagecache page with a new one
774 * @old: page to be replaced
775 * @new: page to replace with
776 * @gfp_mask: allocation mode
777 *
778 * This function replaces a page in the pagecache with a new one. On
779 * success it acquires the pagecache reference for the new page and
780 * drops it for the old page. Both the old and new pages must be
781 * locked. This function does not add the new page to the LRU, the
782 * caller must do that.
783 *
784 * The remove + add is atomic. The only way this function can fail is
785 * memory allocation failure.
786 */
787int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
788{
789 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700790
Sasha Levin309381fea2014-01-23 15:52:54 -0800791 VM_BUG_ON_PAGE(!PageLocked(old), old);
792 VM_BUG_ON_PAGE(!PageLocked(new), new);
793 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700794
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700795 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700796 if (!error) {
797 struct address_space *mapping = old->mapping;
798 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400799 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700800
801 pgoff_t offset = old->index;
802 freepage = mapping->a_ops->freepage;
803
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300804 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700805 new->mapping = mapping;
806 new->index = offset;
807
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700808 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700809 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700810 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700811 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700812
813 /*
814 * hugetlb pages do not participate in page cache accounting.
815 */
816 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700817 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700818 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700819 __inc_node_page_state(new, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700820 xa_unlock_irqrestore(&mapping->i_pages, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700821 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700822 radix_tree_preload_end();
823 if (freepage)
824 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300825 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700826 }
827
828 return error;
829}
830EXPORT_SYMBOL_GPL(replace_page_cache_page);
831
Johannes Weinera5289102014-04-03 14:47:51 -0700832static int __add_to_page_cache_locked(struct page *page,
833 struct address_space *mapping,
834 pgoff_t offset, gfp_t gfp_mask,
835 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Johannes Weiner00501b52014-08-08 14:19:20 -0700837 int huge = PageHuge(page);
838 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700839 int error;
840
Sasha Levin309381fea2014-01-23 15:52:54 -0800841 VM_BUG_ON_PAGE(!PageLocked(page), page);
842 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700843
Johannes Weiner00501b52014-08-08 14:19:20 -0700844 if (!huge) {
845 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800846 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700847 if (error)
848 return error;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700851 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700852 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700853 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800854 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700855 return error;
856 }
857
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300858 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700859 page->mapping = mapping;
860 page->index = offset;
861
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700862 xa_lock_irq(&mapping->i_pages);
Johannes Weinera5289102014-04-03 14:47:51 -0700863 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700864 radix_tree_preload_end();
865 if (unlikely(error))
866 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700867
868 /* hugetlb pages do not participate in page cache accounting. */
869 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700870 __inc_node_page_state(page, NR_FILE_PAGES);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700871 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700872 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800873 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700874 trace_mm_filemap_add_to_page_cache(page);
875 return 0;
876err_insert:
877 page->mapping = NULL;
878 /* Leave page->index set: truncation relies upon it */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700879 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700880 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800881 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300882 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return error;
884}
Johannes Weinera5289102014-04-03 14:47:51 -0700885
886/**
887 * add_to_page_cache_locked - add a locked page to the pagecache
888 * @page: page to add
889 * @mapping: the page's address_space
890 * @offset: page index
891 * @gfp_mask: page allocation mode
892 *
893 * This function is used to add a page to the pagecache. It must be locked.
894 * This function does not add the page to the LRU. The caller must do that.
895 */
896int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
897 pgoff_t offset, gfp_t gfp_mask)
898{
899 return __add_to_page_cache_locked(page, mapping, offset,
900 gfp_mask, NULL);
901}
Nick Piggine2867812008-07-25 19:45:30 -0700902EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400905 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Johannes Weinera5289102014-04-03 14:47:51 -0700907 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700908 int ret;
909
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800910 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700911 ret = __add_to_page_cache_locked(page, mapping, offset,
912 gfp_mask, &shadow);
913 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800914 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700915 else {
916 /*
917 * The page might have been evicted from cache only
918 * recently, in which case it should be activated like
919 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700920 * The exception is pages getting rewritten; evicting other
921 * data from the working set, only to cache data that will
922 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700923 */
Johannes Weiner494c0da2018-10-26 15:06:04 -0700924 WARN_ON_ONCE(PageActive(page));
925 if (!(gfp_mask & __GFP_WRITE) && shadow)
926 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700927 lru_cache_add(page);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return ret;
930}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300931EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Paul Jackson44110fe2006-03-24 03:16:04 -0800933#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700934struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800935{
Miao Xiec0ff7452010-05-24 14:32:08 -0700936 int n;
937 struct page *page;
938
Paul Jackson44110fe2006-03-24 03:16:04 -0800939 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700940 unsigned int cpuset_mems_cookie;
941 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700942 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700943 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700944 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700945 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700946
Miao Xiec0ff7452010-05-24 14:32:08 -0700947 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800948 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700949 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800950}
Nick Piggin2ae88142006-10-28 10:38:23 -0700951EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800952#endif
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/*
955 * In order to wait for pages to become available there must be
956 * waitqueues associated with pages. By using a hash table of
957 * waitqueues where the bucket discipline is to maintain all
958 * waiters on the same queue and wake all when any of the pages
959 * become available, and for the woken contexts to check to be
960 * sure the appropriate page became available, this saves space
961 * at a cost of "thundering herd" phenomena during rare hash
962 * collisions.
963 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000964#define PAGE_WAIT_TABLE_BITS 8
965#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
966static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
967
968static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Nicholas Piggin62906022016-12-25 13:00:30 +1000970 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
Nicholas Piggin62906022016-12-25 13:00:30 +1000972
973void __init pagecache_init(void)
974{
975 int i;
976
977 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
978 init_waitqueue_head(&page_wait_table[i]);
979
980 page_writeback_init();
981}
982
Linus Torvalds3510ca22017-08-27 13:55:12 -0700983/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000984struct wait_page_key {
985 struct page *page;
986 int bit_nr;
987 int page_match;
988};
989
990struct wait_page_queue {
991 struct page *page;
992 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200993 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000994};
995
Ingo Molnarac6424b2017-06-20 12:06:13 +0200996static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000997{
998 struct wait_page_key *key = arg;
999 struct wait_page_queue *wait_page
1000 = container_of(wait, struct wait_page_queue, wait);
1001
1002 if (wait_page->page != key->page)
1003 return 0;
1004 key->page_match = 1;
1005
1006 if (wait_page->bit_nr != key->bit_nr)
1007 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001008
1009 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +10001010 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001011 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001012
1013 return autoremove_wake_function(wait, mode, sync, key);
1014}
1015
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001016static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001017{
1018 wait_queue_head_t *q = page_waitqueue(page);
1019 struct wait_page_key key;
1020 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001021 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001022
1023 key.page = page;
1024 key.bit_nr = bit_nr;
1025 key.page_match = 0;
1026
Tim Chen11a19c72017-08-25 09:13:55 -07001027 bookmark.flags = 0;
1028 bookmark.private = NULL;
1029 bookmark.func = NULL;
1030 INIT_LIST_HEAD(&bookmark.entry);
1031
Nicholas Piggin62906022016-12-25 13:00:30 +10001032 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001033 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1034
1035 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1036 /*
1037 * Take a breather from holding the lock,
1038 * allow pages that finish wake up asynchronously
1039 * to acquire the lock and remove themselves
1040 * from wait queue
1041 */
1042 spin_unlock_irqrestore(&q->lock, flags);
1043 cpu_relax();
1044 spin_lock_irqsave(&q->lock, flags);
1045 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1046 }
1047
Nicholas Piggin62906022016-12-25 13:00:30 +10001048 /*
1049 * It is possible for other pages to have collided on the waitqueue
1050 * hash, so in that case check for a page match. That prevents a long-
1051 * term waiter
1052 *
1053 * It is still possible to miss a case here, when we woke page waiters
1054 * and removed them from the waitqueue, but there are still other
1055 * page waiters.
1056 */
1057 if (!waitqueue_active(q) || !key.page_match) {
1058 ClearPageWaiters(page);
1059 /*
1060 * It's possible to miss clearing Waiters here, when we woke
1061 * our page waiters, but the hashed waitqueue has waiters for
1062 * other pages on it.
1063 *
1064 * That's okay, it's a rare case. The next waker will clear it.
1065 */
1066 }
1067 spin_unlock_irqrestore(&q->lock, flags);
1068}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001069
1070static void wake_up_page(struct page *page, int bit)
1071{
1072 if (!PageWaiters(page))
1073 return;
1074 wake_up_page_bit(page, bit);
1075}
Nicholas Piggin62906022016-12-25 13:00:30 +10001076
1077static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1078 struct page *page, int bit_nr, int state, bool lock)
1079{
1080 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001081 wait_queue_entry_t *wait = &wait_page.wait;
Johannes Weiner9c29ace2018-10-26 15:06:08 -07001082 bool thrashing = false;
Johannes Weiner25f0c862018-10-26 15:06:27 -07001083 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001084 int ret = 0;
1085
Johannes Weiner25f0c862018-10-26 15:06:27 -07001086 if (bit_nr == PG_locked &&
Johannes Weiner9c29ace2018-10-26 15:06:08 -07001087 !PageUptodate(page) && PageWorkingset(page)) {
Johannes Weiner25f0c862018-10-26 15:06:27 -07001088 if (!PageSwapBacked(page))
1089 delayacct_thrashing_start();
1090 psi_memstall_enter(&pflags);
Johannes Weiner9c29ace2018-10-26 15:06:08 -07001091 thrashing = true;
1092 }
1093
Nicholas Piggin62906022016-12-25 13:00:30 +10001094 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001095 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001096 wait->func = wake_page_function;
1097 wait_page.page = page;
1098 wait_page.bit_nr = bit_nr;
1099
1100 for (;;) {
1101 spin_lock_irq(&q->lock);
1102
Ingo Molnar2055da92017-06-20 12:06:46 +02001103 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001104 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001105 SetPageWaiters(page);
1106 }
1107
1108 set_current_state(state);
1109
1110 spin_unlock_irq(&q->lock);
1111
1112 if (likely(test_bit(bit_nr, &page->flags))) {
1113 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001114 }
1115
1116 if (lock) {
1117 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1118 break;
1119 } else {
1120 if (!test_bit(bit_nr, &page->flags))
1121 break;
1122 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001123
1124 if (unlikely(signal_pending_state(state, current))) {
1125 ret = -EINTR;
1126 break;
1127 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001128 }
1129
1130 finish_wait(q, wait);
1131
Johannes Weiner25f0c862018-10-26 15:06:27 -07001132 if (thrashing) {
1133 if (!PageSwapBacked(page))
1134 delayacct_thrashing_end();
1135 psi_memstall_leave(&pflags);
1136 }
Johannes Weiner9c29ace2018-10-26 15:06:08 -07001137
Nicholas Piggin62906022016-12-25 13:00:30 +10001138 /*
1139 * A signal could leave PageWaiters set. Clearing it here if
1140 * !waitqueue_active would be possible (by open-coding finish_wait),
1141 * but still fail to catch it in the case of wait hash collision. We
1142 * already can fail to clear wait hash collision cases, so don't
1143 * bother with signals either.
1144 */
1145
1146 return ret;
1147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Harvey Harrison920c7a52008-02-04 22:29:26 -08001149void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Nicholas Piggin62906022016-12-25 13:00:30 +10001151 wait_queue_head_t *q = page_waitqueue(page);
1152 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154EXPORT_SYMBOL(wait_on_page_bit);
1155
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001156int wait_on_page_bit_killable(struct page *page, int bit_nr)
1157{
Nicholas Piggin62906022016-12-25 13:00:30 +10001158 wait_queue_head_t *q = page_waitqueue(page);
1159 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001160}
David Howells4343d002017-11-02 15:27:52 +00001161EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001164 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001165 * @page: Page defining the wait queue of interest
1166 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001167 *
1168 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1169 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001170void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001171{
1172 wait_queue_head_t *q = page_waitqueue(page);
1173 unsigned long flags;
1174
1175 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001176 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001177 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001178 spin_unlock_irqrestore(&q->lock, flags);
1179}
1180EXPORT_SYMBOL_GPL(add_page_wait_queue);
1181
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001182#ifndef clear_bit_unlock_is_negative_byte
1183
1184/*
1185 * PG_waiters is the high bit in the same byte as PG_lock.
1186 *
1187 * On x86 (and on many other architectures), we can clear PG_lock and
1188 * test the sign bit at the same time. But if the architecture does
1189 * not support that special operation, we just do this all by hand
1190 * instead.
1191 *
1192 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1193 * being cleared, but a memory barrier should be unneccssary since it is
1194 * in the same byte as PG_locked.
1195 */
1196static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1197{
1198 clear_bit_unlock(nr, mem);
1199 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001200 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001201}
1202
1203#endif
1204
David Howells385e1ca5f2009-04-03 16:42:39 +01001205/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001206 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * @page: the page
1208 *
1209 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1210 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001211 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1213 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001214 * Note that this depends on PG_waiters being the sign bit in the byte
1215 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1216 * clear the PG_locked bit and test PG_waiters at the same time fairly
1217 * portably (architectures that do LL/SC can test any bit, while x86 can
1218 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001220void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001222 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001223 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001224 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001225 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1226 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228EXPORT_SYMBOL(unlock_page);
1229
Randy Dunlap485bb992006-06-23 02:03:49 -07001230/**
1231 * end_page_writeback - end writeback against a page
1232 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
1234void end_page_writeback(struct page *page)
1235{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001236 /*
1237 * TestClearPageReclaim could be used here but it is an atomic
1238 * operation and overkill in this particular case. Failing to
1239 * shuffle a page marked for immediate reclaim is too mild to
1240 * justify taking an atomic operation penalty at the end of
1241 * ever page writeback.
1242 */
1243 if (PageReclaim(page)) {
1244 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001245 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001246 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001247
1248 if (!test_clear_page_writeback(page))
1249 BUG();
1250
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001251 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 wake_up_page(page, PG_writeback);
1253}
1254EXPORT_SYMBOL(end_page_writeback);
1255
Matthew Wilcox57d99842014-06-04 16:07:45 -07001256/*
1257 * After completing I/O on a page, call this routine to update the page
1258 * flags appropriately
1259 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001260void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001261{
Jens Axboec11f0c02016-08-05 08:11:04 -06001262 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001263 if (!err) {
1264 SetPageUptodate(page);
1265 } else {
1266 ClearPageUptodate(page);
1267 SetPageError(page);
1268 }
1269 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001270 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001271 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001272 struct address_space *mapping;
1273
Matthew Wilcox57d99842014-06-04 16:07:45 -07001274 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001275 mapping = page_mapping(page);
1276 if (mapping)
1277 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001278 }
1279 end_page_writeback(page);
1280 }
1281}
1282EXPORT_SYMBOL_GPL(page_endio);
1283
Randy Dunlap485bb992006-06-23 02:03:49 -07001284/**
1285 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001286 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001288void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Nicholas Piggin62906022016-12-25 13:00:30 +10001290 struct page *page = compound_head(__page);
1291 wait_queue_head_t *q = page_waitqueue(page);
1292 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294EXPORT_SYMBOL(__lock_page);
1295
Nicholas Piggin62906022016-12-25 13:00:30 +10001296int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001297{
Nicholas Piggin62906022016-12-25 13:00:30 +10001298 struct page *page = compound_head(__page);
1299 wait_queue_head_t *q = page_waitqueue(page);
1300 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001301}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001302EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001303
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001304/*
1305 * Return values:
1306 * 1 - page is locked; mmap_sem is still held.
1307 * 0 - page is not locked.
1308 * mmap_sem has been released (up_read()), unless flags had both
1309 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1310 * which case mmap_sem is still held.
1311 *
1312 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1313 * with the page locked and the mmap_sem unperturbed.
1314 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001315int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1316 unsigned int flags)
1317{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001318 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1319 /*
1320 * CAUTION! In this case, mmap_sem is not released
1321 * even though return 0.
1322 */
1323 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1324 return 0;
1325
1326 up_read(&mm->mmap_sem);
1327 if (flags & FAULT_FLAG_KILLABLE)
1328 wait_on_page_locked_killable(page);
1329 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001330 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001331 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001332 } else {
1333 if (flags & FAULT_FLAG_KILLABLE) {
1334 int ret;
1335
1336 ret = __lock_page_killable(page);
1337 if (ret) {
1338 up_read(&mm->mmap_sem);
1339 return 0;
1340 }
1341 } else
1342 __lock_page(page);
1343 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001344 }
1345}
1346
Randy Dunlap485bb992006-06-23 02:03:49 -07001347/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001348 * page_cache_next_hole - find the next hole (not-present entry)
1349 * @mapping: mapping
1350 * @index: index
1351 * @max_scan: maximum range to search
1352 *
1353 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1354 * lowest indexed hole.
1355 *
1356 * Returns: the index of the hole if found, otherwise returns an index
1357 * outside of the set specified (in which case 'return - index >=
1358 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1359 * be returned.
1360 *
1361 * page_cache_next_hole may be called under rcu_read_lock. However,
1362 * like radix_tree_gang_lookup, this will not atomically search a
1363 * snapshot of the tree at a single point in time. For example, if a
1364 * hole is created at index 5, then subsequently a hole is created at
1365 * index 10, page_cache_next_hole covering both indexes may return 10
1366 * if called under rcu_read_lock.
1367 */
1368pgoff_t page_cache_next_hole(struct address_space *mapping,
1369 pgoff_t index, unsigned long max_scan)
1370{
1371 unsigned long i;
1372
1373 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001374 struct page *page;
1375
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001376 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001377 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001378 break;
1379 index++;
1380 if (index == 0)
1381 break;
1382 }
1383
1384 return index;
1385}
1386EXPORT_SYMBOL(page_cache_next_hole);
1387
1388/**
1389 * page_cache_prev_hole - find the prev hole (not-present entry)
1390 * @mapping: mapping
1391 * @index: index
1392 * @max_scan: maximum range to search
1393 *
1394 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1395 * the first hole.
1396 *
1397 * Returns: the index of the hole if found, otherwise returns an index
1398 * outside of the set specified (in which case 'index - return >=
1399 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1400 * will be returned.
1401 *
1402 * page_cache_prev_hole may be called under rcu_read_lock. However,
1403 * like radix_tree_gang_lookup, this will not atomically search a
1404 * snapshot of the tree at a single point in time. For example, if a
1405 * hole is created at index 10, then subsequently a hole is created at
1406 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1407 * called under rcu_read_lock.
1408 */
1409pgoff_t page_cache_prev_hole(struct address_space *mapping,
1410 pgoff_t index, unsigned long max_scan)
1411{
1412 unsigned long i;
1413
1414 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001415 struct page *page;
1416
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001417 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001418 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001419 break;
1420 index--;
1421 if (index == ULONG_MAX)
1422 break;
1423 }
1424
1425 return index;
1426}
1427EXPORT_SYMBOL(page_cache_prev_hole);
1428
1429/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001430 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001431 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001432 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001433 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001434 * Looks up the page cache slot at @mapping & @offset. If there is a
1435 * page cache page, it is returned with an increased refcount.
1436 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001437 * If the slot holds a shadow entry of a previously evicted page, or a
1438 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001439 *
1440 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001442struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Nick Piggina60637c2008-07-25 19:45:31 -07001444 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001445 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Nick Piggina60637c2008-07-25 19:45:31 -07001447 rcu_read_lock();
1448repeat:
1449 page = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001450 pagep = radix_tree_lookup_slot(&mapping->i_pages, offset);
Nick Piggina60637c2008-07-25 19:45:31 -07001451 if (pagep) {
1452 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001453 if (unlikely(!page))
1454 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001455 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001456 if (radix_tree_deref_retry(page))
1457 goto repeat;
1458 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001459 * A shadow entry of a recently evicted page,
1460 * or a swap entry from shmem/tmpfs. Return
1461 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001462 */
1463 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001464 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001465
1466 head = compound_head(page);
1467 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001468 goto repeat;
1469
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001470 /* The page was split under us? */
1471 if (compound_head(page) != head) {
1472 put_page(head);
1473 goto repeat;
1474 }
1475
Nick Piggina60637c2008-07-25 19:45:31 -07001476 /*
1477 * Has the page moved?
1478 * This is part of the lockless pagecache protocol. See
1479 * include/linux/pagemap.h for details.
1480 */
1481 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001482 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001483 goto repeat;
1484 }
1485 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001486out:
Nick Piggina60637c2008-07-25 19:45:31 -07001487 rcu_read_unlock();
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return page;
1490}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001491EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Randy Dunlap485bb992006-06-23 02:03:49 -07001493/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001494 * find_lock_entry - locate, pin and lock a page cache entry
1495 * @mapping: the address_space to search
1496 * @offset: the page cache index
1497 *
1498 * Looks up the page cache slot at @mapping & @offset. If there is a
1499 * page cache page, it is returned locked and with an increased
1500 * refcount.
1501 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001502 * If the slot holds a shadow entry of a previously evicted page, or a
1503 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001504 *
1505 * Otherwise, %NULL is returned.
1506 *
1507 * find_lock_entry() may sleep.
1508 */
1509struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct page *page;
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001514 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001515 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001516 lock_page(page);
1517 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001518 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001519 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001520 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001521 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001523 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return page;
1526}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001527EXPORT_SYMBOL(find_lock_entry);
1528
1529/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001530 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001531 * @mapping: the address_space to search
1532 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001533 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001534 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001535 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001536 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001537 *
Randy Dunlap75325182014-07-30 16:08:37 -07001538 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001539 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001540 * @fgp_flags can be:
1541 *
1542 * - FGP_ACCESSED: the page will be marked accessed
1543 * - FGP_LOCK: Page is return locked
1544 * - FGP_CREAT: If page is not present then a new page is allocated using
1545 * @gfp_mask and added to the page cache and the VM's LRU
1546 * list. The page is returned locked and with an increased
Josef Bacik76b6ee82019-03-13 11:44:14 -07001547 * refcount.
1548 * - FGP_FOR_MMAP: Similar to FGP_CREAT, only we want to allow the caller to do
1549 * its own locking dance if the page is already in cache, or unlock the page
1550 * before returning if we had to add the page to pagecache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001551 *
1552 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1553 * if the GFP flags specified for FGP_CREAT are atomic.
1554 *
1555 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001556 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001557struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001558 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Nick Piggineb2be182007-10-16 01:24:57 -07001560 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001563 page = find_get_entry(mapping, offset);
1564 if (radix_tree_exceptional_entry(page))
1565 page = NULL;
1566 if (!page)
1567 goto no_page;
1568
1569 if (fgp_flags & FGP_LOCK) {
1570 if (fgp_flags & FGP_NOWAIT) {
1571 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001572 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001573 return NULL;
1574 }
1575 } else {
1576 lock_page(page);
1577 }
1578
1579 /* Has the page been truncated? */
1580 if (unlikely(page->mapping != mapping)) {
1581 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001582 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001583 goto repeat;
1584 }
1585 VM_BUG_ON_PAGE(page->index != offset, page);
1586 }
1587
1588 if (page && (fgp_flags & FGP_ACCESSED))
1589 mark_page_accessed(page);
1590
1591no_page:
1592 if (!page && (fgp_flags & FGP_CREAT)) {
1593 int err;
1594 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001595 gfp_mask |= __GFP_WRITE;
1596 if (fgp_flags & FGP_NOFS)
1597 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001598
Michal Hocko45f87de2014-12-29 20:30:35 +01001599 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001600 if (!page)
1601 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001602
Josef Bacik76b6ee82019-03-13 11:44:14 -07001603 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001604 fgp_flags |= FGP_LOCK;
1605
Hugh Dickinseb39d612014-08-06 16:06:43 -07001606 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001607 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001608 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001609
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001610 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001611 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001612 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001613 page = NULL;
1614 if (err == -EEXIST)
1615 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Josef Bacik76b6ee82019-03-13 11:44:14 -07001617
1618 /*
1619 * add_to_page_cache_lru locks the page, and for mmap we expect
1620 * an unlocked page.
1621 */
1622 if (page && (fgp_flags & FGP_FOR_MMAP))
1623 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return page;
1627}
Mel Gorman2457aec2014-06-04 16:10:31 -07001628EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001631 * find_get_entries - gang pagecache lookup
1632 * @mapping: The address_space to search
1633 * @start: The starting page cache index
1634 * @nr_entries: The maximum number of entries
1635 * @entries: Where the resulting entries are placed
1636 * @indices: The cache indices corresponding to the entries in @entries
1637 *
1638 * find_get_entries() will search for and return a group of up to
1639 * @nr_entries entries in the mapping. The entries are placed at
1640 * @entries. find_get_entries() takes a reference against any actual
1641 * pages it returns.
1642 *
1643 * The search returns a group of mapping-contiguous page cache entries
1644 * with ascending indexes. There may be holes in the indices due to
1645 * not-present pages.
1646 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001647 * Any shadow entries of evicted pages, or swap entries from
1648 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001649 *
1650 * find_get_entries() returns the number of pages and shadow entries
1651 * which were found.
1652 */
1653unsigned find_get_entries(struct address_space *mapping,
1654 pgoff_t start, unsigned int nr_entries,
1655 struct page **entries, pgoff_t *indices)
1656{
1657 void **slot;
1658 unsigned int ret = 0;
1659 struct radix_tree_iter iter;
1660
1661 if (!nr_entries)
1662 return 0;
1663
1664 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001665 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001666 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001667repeat:
1668 page = radix_tree_deref_slot(slot);
1669 if (unlikely(!page))
1670 continue;
1671 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001672 if (radix_tree_deref_retry(page)) {
1673 slot = radix_tree_iter_retry(&iter);
1674 continue;
1675 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001676 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001677 * A shadow entry of a recently evicted page, a swap
1678 * entry from shmem/tmpfs or a DAX entry. Return it
1679 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001680 */
1681 goto export;
1682 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001683
1684 head = compound_head(page);
1685 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001686 goto repeat;
1687
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001688 /* The page was split under us? */
1689 if (compound_head(page) != head) {
1690 put_page(head);
1691 goto repeat;
1692 }
1693
Johannes Weiner0cd61442014-04-03 14:47:46 -07001694 /* Has the page moved? */
1695 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001696 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001697 goto repeat;
1698 }
1699export:
1700 indices[ret] = iter.index;
1701 entries[ret] = page;
1702 if (++ret == nr_entries)
1703 break;
1704 }
1705 rcu_read_unlock();
1706 return ret;
1707}
1708
1709/**
Jan Karab947cee2017-09-06 16:21:21 -07001710 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 * @mapping: The address_space to search
1712 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001713 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * @nr_pages: The maximum number of pages
1715 * @pages: Where the resulting pages are placed
1716 *
Jan Karab947cee2017-09-06 16:21:21 -07001717 * find_get_pages_range() will search for and return a group of up to @nr_pages
1718 * pages in the mapping starting at index @start and up to index @end
1719 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1720 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *
1722 * The search returns a group of mapping-contiguous pages with ascending
1723 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001724 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
Jan Karab947cee2017-09-06 16:21:21 -07001726 * find_get_pages_range() returns the number of pages which were found. If this
1727 * number is smaller than @nr_pages, the end of specified range has been
1728 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 */
Jan Karab947cee2017-09-06 16:21:21 -07001730unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1731 pgoff_t end, unsigned int nr_pages,
1732 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001734 struct radix_tree_iter iter;
1735 void **slot;
1736 unsigned ret = 0;
1737
1738 if (unlikely(!nr_pages))
1739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Nick Piggina60637c2008-07-25 19:45:31 -07001741 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001742 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001743 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001744
1745 if (iter.index > end)
1746 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001747repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001748 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001749 if (unlikely(!page))
1750 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001751
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001752 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001753 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001754 slot = radix_tree_iter_retry(&iter);
1755 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001756 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001757 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001758 * A shadow entry of a recently evicted page,
1759 * or a swap entry from shmem/tmpfs. Skip
1760 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001761 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001762 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001763 }
Nick Piggina60637c2008-07-25 19:45:31 -07001764
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001765 head = compound_head(page);
1766 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001767 goto repeat;
1768
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001769 /* The page was split under us? */
1770 if (compound_head(page) != head) {
1771 put_page(head);
1772 goto repeat;
1773 }
1774
Nick Piggina60637c2008-07-25 19:45:31 -07001775 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001776 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001777 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001778 goto repeat;
1779 }
1780
1781 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001782 if (++ret == nr_pages) {
1783 *start = pages[ret - 1]->index + 1;
1784 goto out;
1785 }
Nick Piggina60637c2008-07-25 19:45:31 -07001786 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001787
Jan Karab947cee2017-09-06 16:21:21 -07001788 /*
1789 * We come here when there is no page beyond @end. We take care to not
1790 * overflow the index @start as it confuses some of the callers. This
1791 * breaks the iteration when there is page at index -1 but that is
1792 * already broken anyway.
1793 */
1794 if (end == (pgoff_t)-1)
1795 *start = (pgoff_t)-1;
1796 else
1797 *start = end + 1;
1798out:
Nick Piggina60637c2008-07-25 19:45:31 -07001799 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return ret;
1802}
1803
Jens Axboeebf43502006-04-27 08:46:01 +02001804/**
1805 * find_get_pages_contig - gang contiguous pagecache lookup
1806 * @mapping: The address_space to search
1807 * @index: The starting page index
1808 * @nr_pages: The maximum number of pages
1809 * @pages: Where the resulting pages are placed
1810 *
1811 * find_get_pages_contig() works exactly like find_get_pages(), except
1812 * that the returned number of pages are guaranteed to be contiguous.
1813 *
1814 * find_get_pages_contig() returns the number of pages which were found.
1815 */
1816unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1817 unsigned int nr_pages, struct page **pages)
1818{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001819 struct radix_tree_iter iter;
1820 void **slot;
1821 unsigned int ret = 0;
1822
1823 if (unlikely(!nr_pages))
1824 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001825
Nick Piggina60637c2008-07-25 19:45:31 -07001826 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001827 radix_tree_for_each_contig(slot, &mapping->i_pages, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001828 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001829repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001830 page = radix_tree_deref_slot(slot);
1831 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001832 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001833 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001834
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001835 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001836 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001837 slot = radix_tree_iter_retry(&iter);
1838 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001839 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001840 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001841 * A shadow entry of a recently evicted page,
1842 * or a swap entry from shmem/tmpfs. Stop
1843 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001844 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001845 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001846 }
Nick Piggina60637c2008-07-25 19:45:31 -07001847
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001848 head = compound_head(page);
1849 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001850 goto repeat;
1851
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001852 /* The page was split under us? */
1853 if (compound_head(page) != head) {
1854 put_page(head);
1855 goto repeat;
1856 }
1857
Nick Piggina60637c2008-07-25 19:45:31 -07001858 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001859 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001860 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001861 goto repeat;
1862 }
1863
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001864 /*
1865 * must check mapping and index after taking the ref.
1866 * otherwise we can get both false positives and false
1867 * negatives, which is just confusing to the caller.
1868 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001869 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001870 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001871 break;
1872 }
1873
Nick Piggina60637c2008-07-25 19:45:31 -07001874 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001875 if (++ret == nr_pages)
1876 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001877 }
Nick Piggina60637c2008-07-25 19:45:31 -07001878 rcu_read_unlock();
1879 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001880}
David Howellsef71c152007-05-09 02:33:44 -07001881EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001882
Randy Dunlap485bb992006-06-23 02:03:49 -07001883/**
Jan Kara72b045a2017-11-15 17:34:33 -08001884 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001885 * @mapping: the address_space to search
1886 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001887 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001888 * @tag: the tag index
1889 * @nr_pages: the maximum number of pages
1890 * @pages: where the resulting pages are placed
1891 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001893 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Jan Kara72b045a2017-11-15 17:34:33 -08001895unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1896 pgoff_t end, int tag, unsigned int nr_pages,
1897 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001899 struct radix_tree_iter iter;
1900 void **slot;
1901 unsigned ret = 0;
1902
1903 if (unlikely(!nr_pages))
1904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Nick Piggina60637c2008-07-25 19:45:31 -07001906 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001907 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001908 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001909
1910 if (iter.index > end)
1911 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001912repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001913 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001914 if (unlikely(!page))
1915 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001916
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001917 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001918 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001919 slot = radix_tree_iter_retry(&iter);
1920 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001921 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001922 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001923 * A shadow entry of a recently evicted page.
1924 *
1925 * Those entries should never be tagged, but
1926 * this tree walk is lockless and the tags are
1927 * looked up in bulk, one radix tree node at a
1928 * time, so there is a sizable window for page
1929 * reclaim to evict a page we saw tagged.
1930 *
1931 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001932 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001933 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001934 }
Nick Piggina60637c2008-07-25 19:45:31 -07001935
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001936 head = compound_head(page);
1937 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001938 goto repeat;
1939
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001940 /* The page was split under us? */
1941 if (compound_head(page) != head) {
1942 put_page(head);
1943 goto repeat;
1944 }
1945
Nick Piggina60637c2008-07-25 19:45:31 -07001946 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001947 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001948 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001949 goto repeat;
1950 }
1951
1952 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001953 if (++ret == nr_pages) {
1954 *index = pages[ret - 1]->index + 1;
1955 goto out;
1956 }
Nick Piggina60637c2008-07-25 19:45:31 -07001957 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001958
Jan Kara72b045a2017-11-15 17:34:33 -08001959 /*
1960 * We come here when we got at @end. We take care to not overflow the
1961 * index @index as it confuses some of the callers. This breaks the
1962 * iteration when there is page at index -1 but that is already broken
1963 * anyway.
1964 */
1965 if (end == (pgoff_t)-1)
1966 *index = (pgoff_t)-1;
1967 else
1968 *index = end + 1;
1969out:
Nick Piggina60637c2008-07-25 19:45:31 -07001970 rcu_read_unlock();
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return ret;
1973}
Jan Kara72b045a2017-11-15 17:34:33 -08001974EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001976/**
1977 * find_get_entries_tag - find and return entries that match @tag
1978 * @mapping: the address_space to search
1979 * @start: the starting page cache index
1980 * @tag: the tag index
1981 * @nr_entries: the maximum number of entries
1982 * @entries: where the resulting entries are placed
1983 * @indices: the cache indices corresponding to the entries in @entries
1984 *
1985 * Like find_get_entries, except we only return entries which are tagged with
1986 * @tag.
1987 */
1988unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1989 int tag, unsigned int nr_entries,
1990 struct page **entries, pgoff_t *indices)
1991{
1992 void **slot;
1993 unsigned int ret = 0;
1994 struct radix_tree_iter iter;
1995
1996 if (!nr_entries)
1997 return 0;
1998
1999 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002000 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002001 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002002repeat:
2003 page = radix_tree_deref_slot(slot);
2004 if (unlikely(!page))
2005 continue;
2006 if (radix_tree_exception(page)) {
2007 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002008 slot = radix_tree_iter_retry(&iter);
2009 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002010 }
2011
2012 /*
2013 * A shadow entry of a recently evicted page, a swap
2014 * entry from shmem/tmpfs or a DAX entry. Return it
2015 * without attempting to raise page count.
2016 */
2017 goto export;
2018 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002019
2020 head = compound_head(page);
2021 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002022 goto repeat;
2023
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002024 /* The page was split under us? */
2025 if (compound_head(page) != head) {
2026 put_page(head);
2027 goto repeat;
2028 }
2029
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002030 /* Has the page moved? */
2031 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002032 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002033 goto repeat;
2034 }
2035export:
2036 indices[ret] = iter.index;
2037 entries[ret] = page;
2038 if (++ret == nr_entries)
2039 break;
2040 }
2041 rcu_read_unlock();
2042 return ret;
2043}
2044EXPORT_SYMBOL(find_get_entries_tag);
2045
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002046/*
2047 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2048 * a _large_ part of the i/o request. Imagine the worst scenario:
2049 *
2050 * ---R__________________________________________B__________
2051 * ^ reading here ^ bad block(assume 4k)
2052 *
2053 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2054 * => failing the whole request => read(R) => read(R+1) =>
2055 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2056 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2057 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2058 *
2059 * It is going insane. Fix it by quickly scaling down the readahead size.
2060 */
2061static void shrink_readahead_size_eio(struct file *filp,
2062 struct file_ra_state *ra)
2063{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002064 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002065}
2066
Randy Dunlap485bb992006-06-23 02:03:49 -07002067/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002068 * generic_file_buffered_read - generic file read routine
2069 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002070 * @iter: data destination
2071 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002074 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 *
2076 * This is really ugly. But the goto's actually try to clarify some
2077 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002079static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002080 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002082 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002083 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002085 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002086 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002087 pgoff_t index;
2088 pgoff_t last_index;
2089 pgoff_t prev_index;
2090 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002091 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002092 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Wei Fangc2a97372016-10-07 17:01:52 -07002094 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002095 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002096 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2097
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002098 index = *ppos >> PAGE_SHIFT;
2099 prev_index = ra->prev_pos >> PAGE_SHIFT;
2100 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2101 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2102 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 for (;;) {
2105 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002106 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002107 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 unsigned long nr, ret;
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002112 if (fatal_signal_pending(current)) {
2113 error = -EINTR;
2114 goto out;
2115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002118 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002119 if (iocb->ki_flags & IOCB_NOWAIT)
2120 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002121 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002122 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002123 index, last_index - index);
2124 page = find_get_page(mapping, index);
2125 if (unlikely(page == NULL))
2126 goto no_cached_page;
2127 }
2128 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002129 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002130 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002131 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002133 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002134 if (iocb->ki_flags & IOCB_NOWAIT) {
2135 put_page(page);
2136 goto would_block;
2137 }
2138
Mel Gormanebded022016-03-15 14:55:39 -07002139 /*
2140 * See comment in do_read_cache_page on why
2141 * wait_on_page_locked is used to avoid unnecessarily
2142 * serialisations and why it's safe.
2143 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002144 error = wait_on_page_locked_killable(page);
2145 if (unlikely(error))
2146 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002147 if (PageUptodate(page))
2148 goto page_ok;
2149
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002150 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002151 !mapping->a_ops->is_partially_uptodate)
2152 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002153 /* pipes can't handle partially uptodate pages */
2154 if (unlikely(iter->type & ITER_PIPE))
2155 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002156 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002157 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002158 /* Did it get truncated before we got the lock? */
2159 if (!page->mapping)
2160 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002161 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002162 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002163 goto page_not_up_to_date_locked;
2164 unlock_page(page);
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002167 /*
2168 * i_size must be checked after we know the page is Uptodate.
2169 *
2170 * Checking i_size after the check allows us to calculate
2171 * the correct value for "nr", which means the zero-filled
2172 * part of the page is not copied back to userspace (unless
2173 * another truncate extends the file - this is desired though).
2174 */
2175
2176 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002177 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002178 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002180 goto out;
2181 }
2182
2183 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002184 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002185 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002186 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002187 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002188 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002189 goto out;
2190 }
2191 }
2192 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 /* If users can be writing to this page using arbitrary
2195 * virtual addresses, take care about potential aliasing
2196 * before reading the page on the kernel side.
2197 */
2198 if (mapping_writably_mapped(mapping))
2199 flush_dcache_page(page);
2200
2201 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002202 * When a sequential read accesses a page several times,
2203 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 */
Jan Karaec0f1632007-05-06 14:49:25 -07002205 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 mark_page_accessed(page);
2207 prev_index = index;
2208
2209 /*
2210 * Ok, we have the page, and it's up-to-date, so
2211 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 */
Al Viro6e58e792014-02-03 17:07:03 -05002213
2214 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002216 index += offset >> PAGE_SHIFT;
2217 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002218 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002220 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002221 written += ret;
2222 if (!iov_iter_count(iter))
2223 goto out;
2224 if (ret < nr) {
2225 error = -EFAULT;
2226 goto out;
2227 }
2228 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230page_not_up_to_date:
2231 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002232 error = lock_page_killable(page);
2233 if (unlikely(error))
2234 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002236page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002237 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (!page->mapping) {
2239 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002240 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 continue;
2242 }
2243
2244 /* Did somebody else fill it already? */
2245 if (PageUptodate(page)) {
2246 unlock_page(page);
2247 goto page_ok;
2248 }
2249
2250readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002251 /*
2252 * A previous I/O error may have been due to temporary
2253 * failures, eg. multipath errors.
2254 * PG_error will be set again if readpage fails.
2255 */
2256 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 /* Start the actual read. The read will unlock the page. */
2258 error = mapping->a_ops->readpage(filp, page);
2259
Zach Brown994fc28c2005-12-15 14:28:17 -08002260 if (unlikely(error)) {
2261 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002262 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002263 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002264 goto find_page;
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002270 error = lock_page_killable(page);
2271 if (unlikely(error))
2272 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (!PageUptodate(page)) {
2274 if (page->mapping == NULL) {
2275 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002276 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 */
2278 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002279 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto find_page;
2281 }
2282 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002283 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002284 error = -EIO;
2285 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287 unlock_page(page);
2288 }
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto page_ok;
2291
2292readpage_error:
2293 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002294 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto out;
2296
2297no_cached_page:
2298 /*
2299 * Ok, it wasn't cached, so we need to create a new
2300 * page..
2301 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002302 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002303 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002304 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002305 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002307 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002308 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002310 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002311 if (error == -EEXIST) {
2312 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 goto out;
2316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto readpage;
2318 }
2319
Milosz Tanski3239d832017-08-29 16:13:19 +02002320would_block:
2321 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002323 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002324 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002325 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002327 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002328 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002329 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
Randy Dunlap485bb992006-06-23 02:03:49 -07002332/**
Al Viro6abd2322014-04-04 14:20:57 -04002333 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002334 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002335 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002336 *
Al Viro6abd2322014-04-04 14:20:57 -04002337 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 * that can use the page cache directly.
2339 */
2340ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002341generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002343 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002344 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002345
2346 if (!count)
2347 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Al Viro2ba48ce2015-04-09 13:52:01 -04002349 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002350 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002351 struct address_space *mapping = file->f_mapping;
2352 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002353 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002356 if (iocb->ki_flags & IOCB_NOWAIT) {
2357 if (filemap_range_has_page(mapping, iocb->ki_pos,
2358 iocb->ki_pos + count - 1))
2359 return -EAGAIN;
2360 } else {
2361 retval = filemap_write_and_wait_range(mapping,
2362 iocb->ki_pos,
2363 iocb->ki_pos + count - 1);
2364 if (retval < 0)
2365 goto out;
2366 }
Al Viroed978a82014-03-05 22:53:04 -05002367
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002368 file_accessed(file);
2369
Al Viro5ecda132017-04-13 14:13:36 -04002370 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002371 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002372 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002373 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002374 }
Al Viro5b47d592017-05-08 13:54:47 -04002375 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002376
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002377 /*
2378 * Btrfs can have a short DIO read if we encounter
2379 * compressed extents, so if there was an error, or if
2380 * we've already read everything we wanted to, or if
2381 * there was a short read because we hit EOF, go ahead
2382 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002383 * the rest of the read. Buffered reads will not work for
2384 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002385 */
Al Viro5ecda132017-04-13 14:13:36 -04002386 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002387 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002391 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392out:
2393 return retval;
2394}
Al Viroed978a82014-03-05 22:53:04 -05002395EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398#define MMAP_LOTSAMISS (100)
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002399static struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
2400 struct file *fpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002402 int flags = vmf->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002404 if (fpin)
2405 return fpin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002407 /*
2408 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
2409 * anything, so we only pin the file and drop the mmap_sem if only
2410 * FAULT_FLAG_ALLOW_RETRY is set.
2411 */
2412 if ((flags & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT)) ==
2413 FAULT_FLAG_ALLOW_RETRY) {
2414 fpin = get_file(vmf->vma->vm_file);
2415 up_read(&vmf->vma->vm_mm->mmap_sem);
2416 }
2417 return fpin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Linus Torvaldsef00e082009-06-16 15:31:25 -07002420/*
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002421 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_sem
2422 * @vmf - the vm_fault for this fault.
2423 * @page - the page to lock.
2424 * @fpin - the pointer to the file we may pin (or is already pinned).
2425 *
2426 * This works similar to lock_page_or_retry in that it can drop the mmap_sem.
2427 * It differs in that it actually returns the page locked if it returns 1 and 0
2428 * if it couldn't lock the page. If we did have to drop the mmap_sem then fpin
2429 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002430 */
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002431static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2432 struct file **fpin)
2433{
2434 if (trylock_page(page))
2435 return 1;
2436
Linus Torvalds1d1d2f02019-03-15 11:26:07 -07002437 /*
2438 * NOTE! This will make us return with VM_FAULT_RETRY, but with
2439 * the mmap_sem still held. That's how FAULT_FLAG_RETRY_NOWAIT
2440 * is supposed to work. We have way too many special cases..
2441 */
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002442 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2443 return 0;
2444
2445 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2446 if (vmf->flags & FAULT_FLAG_KILLABLE) {
2447 if (__lock_page_killable(page)) {
2448 /*
2449 * We didn't have the right flags to drop the mmap_sem,
2450 * but all fault_handlers only check for fatal signals
2451 * if we return VM_FAULT_RETRY, so we need to drop the
2452 * mmap_sem here and return 0 if we don't have a fpin.
2453 */
2454 if (*fpin == NULL)
2455 up_read(&vmf->vma->vm_mm->mmap_sem);
2456 return 0;
2457 }
2458 } else
2459 __lock_page(page);
2460 return 1;
2461}
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Linus Torvaldsef00e082009-06-16 15:31:25 -07002464/*
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002465 * Synchronous readahead happens when we don't even find a page in the page
2466 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2467 * to drop the mmap sem we return the file that was pinned in order for us to do
2468 * that. If we didn't pin a file then we return NULL. The file that is
2469 * returned needs to be fput()'ed when we're done with it.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002470 */
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002471static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002472{
Josef Bacik0cb18b72019-03-13 11:44:18 -07002473 struct file *file = vmf->vma->vm_file;
2474 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002475 struct address_space *mapping = file->f_mapping;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002476 struct file *fpin = NULL;
Josef Bacik0cb18b72019-03-13 11:44:18 -07002477 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002478
2479 /* If we don't want any read-ahead, don't bother */
Vinayak Menon0947fbf2018-12-11 18:03:55 +05302480 if (vmf->vma_flags & VM_RAND_READ)
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002481 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002482 if (!ra->ra_pages)
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002483 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002484
Vinayak Menon0947fbf2018-12-11 18:03:55 +05302485 if (vmf->vma_flags & VM_SEQ_READ) {
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002486 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002487 page_cache_sync_readahead(mapping, ra, file, offset,
2488 ra->ra_pages);
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002489 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002490 }
2491
Andi Kleen207d04b2011-05-24 17:12:29 -07002492 /* Avoid banging the cache line if not needed */
2493 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002494 ra->mmap_miss++;
2495
2496 /*
2497 * Do we miss much more than hit in this file? If so,
2498 * stop bothering with read-ahead. It will only hurt.
2499 */
2500 if (ra->mmap_miss > MMAP_LOTSAMISS)
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002501 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002502
Wu Fengguangd30a1102009-06-16 15:31:30 -07002503 /*
2504 * mmap read-around
2505 */
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002506 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002507 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2508 ra->size = ra->ra_pages;
2509 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002510 ra_submit(ra, mapping, file);
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002511 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002512}
2513
2514/*
2515 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002516 * so we want to possibly extend the readahead further. We return the file that
2517 * was pinned if we have to drop the mmap_sem in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002518 */
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002519static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
2520 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002521{
Josef Bacik0cb18b72019-03-13 11:44:18 -07002522 struct file *file = vmf->vma->vm_file;
2523 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002524 struct address_space *mapping = file->f_mapping;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002525 struct file *fpin = NULL;
Josef Bacik0cb18b72019-03-13 11:44:18 -07002526 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002527
2528 /* If we don't want any read-ahead, don't bother */
Vinayak Menon0947fbf2018-12-11 18:03:55 +05302529 if (vmf->vma_flags & VM_RAND_READ)
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002530 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002531 if (ra->mmap_miss > 0)
2532 ra->mmap_miss--;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002533 if (PageReadahead(page)) {
2534 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002535 page_cache_async_readahead(mapping, ra, file,
2536 page, offset, ra->ra_pages);
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002537 }
2538 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002539}
2540
Randy Dunlap485bb992006-06-23 02:03:49 -07002541/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002542 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002543 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002544 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002545 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 * mapped memory region to read in file data during a page fault.
2547 *
2548 * The goto's are kind of ugly, but this streamlines the normal case of having
2549 * it in the page cache, and handles the special cases reasonably without
2550 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002551 *
Vinayak Menon0947fbf2018-12-11 18:03:55 +05302552 * vma->vm_mm->mmap_sem must be held on entry (except FAULT_FLAG_SPECULATIVE).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002553 *
2554 * If our return value has VM_FAULT_RETRY set, it's because
2555 * lock_page_or_retry() returned 0.
2556 * The mmap_sem has usually been released in this case.
2557 * See __lock_page_or_retry() for the exception.
2558 *
2559 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2560 * has not been released.
2561 *
2562 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002564vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002567 struct file *file = vmf->vma->vm_file;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002568 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct address_space *mapping = file->f_mapping;
2570 struct file_ra_state *ra = &file->f_ra;
2571 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002572 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002573 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002575 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002577 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2578 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002579 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002582 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002584 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002585 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002587 * We found the page, so try async readahead before
2588 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002590 fpin = do_async_mmap_readahead(vmf, page);
Shaohua Li45cac652012-10-08 16:32:19 -07002591 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002592 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002593 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002594 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002595 ret = VM_FAULT_MAJOR;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002596 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002597retry_find:
Josef Bacik76b6ee82019-03-13 11:44:14 -07002598 page = pagecache_get_page(mapping, offset,
2599 FGP_CREAT|FGP_FOR_MMAP,
2600 vmf->gfp_mask);
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002601 if (!page) {
2602 if (fpin)
2603 goto out_retry;
Josef Bacik76b6ee82019-03-13 11:44:14 -07002604 return VM_FAULT_OOM;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002608 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
2609 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07002610
2611 /* Did it get truncated? */
2612 if (unlikely(page->mapping != mapping)) {
2613 unlock_page(page);
2614 put_page(page);
2615 goto retry_find;
2616 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002617 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002620 * We have a locked page in the page cache, now we need to check
2621 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 */
Nick Piggind00806b2007-07-19 01:46:57 -07002623 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 goto page_not_uptodate;
2625
Linus Torvaldsef00e082009-06-16 15:31:25 -07002626 /*
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002627 * We've made it this far and we had to drop our mmap_sem, now is the
2628 * time to return to the upper layer and have it re-find the vma and
2629 * redo the fault.
2630 */
2631 if (fpin) {
2632 unlock_page(page);
2633 goto out_retry;
2634 }
2635
2636 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002637 * Found the page and have a reference on it.
2638 * We must recheck i_size under page lock.
2639 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002640 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2641 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002642 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002643 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002644 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002645 }
2646
Nick Piggind0217ac2007-07-19 01:47:03 -07002647 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002648 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /*
2652 * Umm, take care of errors if the page isn't up-to-date.
2653 * Try to re-read it _once_. We do this synchronously,
2654 * because there really aren't any performance issues here
2655 * and we need to check for errors.
2656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 ClearPageError(page);
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002658 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Zach Brown994fc28c2005-12-15 14:28:17 -08002659 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002660 if (!error) {
2661 wait_on_page_locked(page);
2662 if (!PageUptodate(page))
2663 error = -EIO;
2664 }
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002665 if (fpin)
2666 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002667 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002668
2669 if (!error || error == AOP_TRUNCATED_PAGE)
2670 goto retry_find;
2671
2672 /* Things didn't work out. Return zero to tell the mm layer so. */
2673 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002674 return VM_FAULT_SIGBUS;
Josef Bacik9f5fa1f2019-03-13 11:44:22 -07002675
2676out_retry:
2677 /*
2678 * We dropped the mmap_sem, we need to return to the fault handler to
2679 * re-find the vma and come back and find our hopefully still populated
2680 * page.
2681 */
2682 if (page)
2683 put_page(page);
2684 if (fpin)
2685 fput(fpin);
2686 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07002687}
2688EXPORT_SYMBOL(filemap_fault);
2689
Jan Kara82b0f8c2016-12-14 15:06:58 -08002690void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002691 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002692{
2693 struct radix_tree_iter iter;
2694 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002695 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002696 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002697 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002698 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002699 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002700
2701 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002702 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start_pgoff) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002703 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002704 break;
2705repeat:
2706 page = radix_tree_deref_slot(slot);
2707 if (unlikely(!page))
2708 goto next;
2709 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002710 if (radix_tree_deref_retry(page)) {
2711 slot = radix_tree_iter_retry(&iter);
2712 continue;
2713 }
2714 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002715 }
2716
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002717 head = compound_head(page);
2718 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002719 goto repeat;
2720
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002721 /* The page was split under us? */
2722 if (compound_head(page) != head) {
2723 put_page(head);
2724 goto repeat;
2725 }
2726
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002727 /* Has the page moved? */
2728 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002729 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002730 goto repeat;
2731 }
2732
2733 if (!PageUptodate(page) ||
2734 PageReadahead(page) ||
2735 PageHWPoison(page))
2736 goto skip;
2737 if (!trylock_page(page))
2738 goto skip;
2739
2740 if (page->mapping != mapping || !PageUptodate(page))
2741 goto unlock;
2742
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002743 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2744 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002745 goto unlock;
2746
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002747 if (file->f_ra.mmap_miss > 0)
2748 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002749
Jan Kara82b0f8c2016-12-14 15:06:58 -08002750 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2751 if (vmf->pte)
2752 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002753 last_pgoff = iter.index;
Vinayak Menon3aee78b2016-05-20 16:58:41 -07002754
2755 if (want_old_faultaround_pte) {
2756 if (iter.index == vmf->pgoff)
2757 vmf->flags &= ~FAULT_FLAG_PREFAULT_OLD;
2758 else
2759 vmf->flags |= FAULT_FLAG_PREFAULT_OLD;
2760 }
2761
Jan Kara82b0f8c2016-12-14 15:06:58 -08002762 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002763 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002764 unlock_page(page);
2765 goto next;
2766unlock:
2767 unlock_page(page);
2768skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002769 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002770next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002771 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002772 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002773 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002774 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002775 break;
2776 }
2777 rcu_read_unlock();
2778}
2779EXPORT_SYMBOL(filemap_map_pages);
2780
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002781vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002782{
2783 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002784 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002785 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002786
Jan Kara14da9202012-06-12 16:20:37 +02002787 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002788 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002789 lock_page(page);
2790 if (page->mapping != inode->i_mapping) {
2791 unlock_page(page);
2792 ret = VM_FAULT_NOPAGE;
2793 goto out;
2794 }
Jan Kara14da9202012-06-12 16:20:37 +02002795 /*
2796 * We mark the page dirty already here so that when freeze is in
2797 * progress, we are guaranteed that writeback during freezing will
2798 * see the dirty page and writeprotect it again.
2799 */
2800 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002801 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002802out:
Jan Kara14da9202012-06-12 16:20:37 +02002803 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002804 return ret;
2805}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002806
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002807const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002808 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002809 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002810 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811};
2812
2813/* This is used for a general mmap of a disk file */
2814
2815int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2816{
2817 struct address_space *mapping = file->f_mapping;
2818
2819 if (!mapping->a_ops->readpage)
2820 return -ENOEXEC;
2821 file_accessed(file);
2822 vma->vm_ops = &generic_file_vm_ops;
2823 return 0;
2824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826/*
2827 * This is for filesystems which do not implement ->writepage.
2828 */
2829int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2830{
2831 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2832 return -EINVAL;
2833 return generic_file_mmap(file, vma);
2834}
2835#else
Arnd Bergmann45397222018-04-13 15:35:27 -07002836int filemap_page_mkwrite(struct vm_fault *vmf)
2837{
2838 return -ENOSYS;
2839}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2841{
2842 return -ENOSYS;
2843}
2844int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2845{
2846 return -ENOSYS;
2847}
2848#endif /* CONFIG_MMU */
2849
Arnd Bergmann45397222018-04-13 15:35:27 -07002850EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851EXPORT_SYMBOL(generic_file_mmap);
2852EXPORT_SYMBOL(generic_file_readonly_mmap);
2853
Sasha Levin67f9fd92014-04-03 14:48:18 -07002854static struct page *wait_on_page_read(struct page *page)
2855{
2856 if (!IS_ERR(page)) {
2857 wait_on_page_locked(page);
2858 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002859 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002860 page = ERR_PTR(-EIO);
2861 }
2862 }
2863 return page;
2864}
2865
Mel Gorman32b63522016-03-15 14:55:36 -07002866static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002867 pgoff_t index,
Sami Tolvanen2aa218a2018-04-25 16:08:11 -07002868 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002869 void *data,
2870 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Nick Piggineb2be182007-10-16 01:24:57 -07002872 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 int err;
2874repeat:
2875 page = find_get_page(mapping, index);
2876 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002877 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002878 if (!page)
2879 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002880 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002881 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002882 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002883 if (err == -EEXIST)
2884 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return ERR_PTR(err);
2887 }
Mel Gorman32b63522016-03-15 14:55:36 -07002888
2889filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 err = filler(data, page);
2891 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002892 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002893 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Mel Gorman32b63522016-03-15 14:55:36 -07002895
2896 page = wait_on_page_read(page);
2897 if (IS_ERR(page))
2898 return page;
2899 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (PageUptodate(page))
2902 goto out;
2903
Mel Gormanebded022016-03-15 14:55:39 -07002904 /*
2905 * Page is not up to date and may be locked due one of the following
2906 * case a: Page is being filled and the page lock is held
2907 * case b: Read/write error clearing the page uptodate status
2908 * case c: Truncation in progress (page locked)
2909 * case d: Reclaim in progress
2910 *
2911 * Case a, the page will be up to date when the page is unlocked.
2912 * There is no need to serialise on the page lock here as the page
2913 * is pinned so the lock gives no additional protection. Even if the
2914 * the page is truncated, the data is still valid if PageUptodate as
2915 * it's a race vs truncate race.
2916 * Case b, the page will not be up to date
2917 * Case c, the page may be truncated but in itself, the data may still
2918 * be valid after IO completes as it's a read vs truncate race. The
2919 * operation must restart if the page is not uptodate on unlock but
2920 * otherwise serialising on page lock to stabilise the mapping gives
2921 * no additional guarantees to the caller as the page lock is
2922 * released before return.
2923 * Case d, similar to truncation. If reclaim holds the page lock, it
2924 * will be a race with remove_mapping that determines if the mapping
2925 * is valid on unlock but otherwise the data is valid and there is
2926 * no need to serialise with page lock.
2927 *
2928 * As the page lock gives no additional guarantee, we optimistically
2929 * wait on the page to be unlocked and check if it's up to date and
2930 * use the page if it is. Otherwise, the page lock is required to
2931 * distinguish between the different cases. The motivation is that we
2932 * avoid spurious serialisations and wakeups when multiple processes
2933 * wait on the same page for IO to complete.
2934 */
2935 wait_on_page_locked(page);
2936 if (PageUptodate(page))
2937 goto out;
2938
2939 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002941
2942 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (!page->mapping) {
2944 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002945 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002946 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Mel Gormanebded022016-03-15 14:55:39 -07002948
2949 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (PageUptodate(page)) {
2951 unlock_page(page);
2952 goto out;
2953 }
Mel Gorman32b63522016-03-15 14:55:36 -07002954 goto filler;
2955
David Howellsc855ff32007-05-09 13:42:20 +01002956out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002957 mark_page_accessed(page);
2958 return page;
2959}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002960
2961/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002962 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002963 * @mapping: the page's address_space
2964 * @index: the page index
2965 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002966 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002967 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002968 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002969 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002970 *
2971 * If the page does not get brought uptodate, return -EIO.
2972 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002973struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002974 pgoff_t index,
Sami Tolvanen2aa218a2018-04-25 16:08:11 -07002975 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002976 void *data)
2977{
2978 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2979}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002980EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002981
2982/**
2983 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2984 * @mapping: the page's address_space
2985 * @index: the page index
2986 * @gfp: the page allocator flags to use if allocating
2987 *
2988 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002989 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002990 *
2991 * If the page does not get brought uptodate, return -EIO.
2992 */
2993struct page *read_cache_page_gfp(struct address_space *mapping,
2994 pgoff_t index,
2995 gfp_t gfp)
2996{
Sami Tolvanen2aa218a2018-04-25 16:08:11 -07002997 filler_t *filler = mapping->a_ops->readpage;
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002998
Sasha Levin67f9fd92014-04-03 14:48:18 -07002999 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003000}
3001EXPORT_SYMBOL(read_cache_page_gfp);
3002
Nick Piggin2f718ff2007-10-16 01:24:59 -07003003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 * Performs necessary checks before doing a write
3005 *
Randy Dunlap485bb992006-06-23 02:03:49 -07003006 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 * Returns appropriate error code that caller should return or
3008 * zero in case that write should be allowed.
3009 */
Al Viro3309dd02015-04-09 12:55:47 -04003010inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Al Viro3309dd02015-04-09 12:55:47 -04003012 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08003014 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04003015 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Al Viro3309dd02015-04-09 12:55:47 -04003017 if (!iov_iter_count(from))
3018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Al Viro0fa6b002015-04-04 04:05:48 -04003020 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04003021 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04003022 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Al Viro3309dd02015-04-09 12:55:47 -04003024 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003026 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
3027 return -EINVAL;
3028
Al Viro0fa6b002015-04-04 04:05:48 -04003029 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04003030 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04003031 send_sig(SIGXFSZ, current, 0);
3032 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Al Viro3309dd02015-04-09 12:55:47 -04003034 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 }
3036
3037 /*
3038 * LFS rule
3039 */
Al Viro3309dd02015-04-09 12:55:47 -04003040 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04003042 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04003044 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046
3047 /*
3048 * Are we about to exceed the fs block limit ?
3049 *
3050 * If we have written data it becomes a short write. If we have
3051 * exceeded without writing data we send a signal and return EFBIG.
3052 * Linus frestrict idea will clean these up nicely..
3053 */
Al Viro3309dd02015-04-09 12:55:47 -04003054 if (unlikely(pos >= inode->i_sb->s_maxbytes))
3055 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Al Viro3309dd02015-04-09 12:55:47 -04003057 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
3058 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060EXPORT_SYMBOL(generic_write_checks);
3061
Nick Pigginafddba42007-10-16 01:25:01 -07003062int pagecache_write_begin(struct file *file, struct address_space *mapping,
3063 loff_t pos, unsigned len, unsigned flags,
3064 struct page **pagep, void **fsdata)
3065{
3066 const struct address_space_operations *aops = mapping->a_ops;
3067
Nick Piggin4e02ed42008-10-29 14:00:55 -07003068 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003069 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003070}
3071EXPORT_SYMBOL(pagecache_write_begin);
3072
3073int pagecache_write_end(struct file *file, struct address_space *mapping,
3074 loff_t pos, unsigned len, unsigned copied,
3075 struct page *page, void *fsdata)
3076{
3077 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003078
Nick Piggin4e02ed42008-10-29 14:00:55 -07003079 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003080}
3081EXPORT_SYMBOL(pagecache_write_end);
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003084generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 struct file *file = iocb->ki_filp;
3087 struct address_space *mapping = file->f_mapping;
3088 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003089 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003091 size_t write_len;
3092 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Al Viro0c949332014-03-22 06:51:37 -04003094 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003095 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003096
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003097 if (iocb->ki_flags & IOCB_NOWAIT) {
3098 /* If there are pages to writeback, return */
3099 if (filemap_range_has_page(inode->i_mapping, pos,
3100 pos + iov_iter_count(from)))
3101 return -EAGAIN;
3102 } else {
3103 written = filemap_write_and_wait_range(mapping, pos,
3104 pos + write_len - 1);
3105 if (written)
3106 goto out;
3107 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003108
3109 /*
3110 * After a write we want buffered reads to be sure to go to disk to get
3111 * the new data. We invalidate clean cached page from the region we're
3112 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003113 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003114 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003115 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003116 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003117 /*
3118 * If a page can not be invalidated, return 0 to fall back
3119 * to buffered write.
3120 */
3121 if (written) {
3122 if (written == -EBUSY)
3123 return 0;
3124 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003125 }
3126
Al Viro639a93a52017-04-13 14:10:15 -04003127 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003128
3129 /*
3130 * Finally, try again to invalidate clean pages which might have been
3131 * cached by non-direct readahead, or faulted in by get_user_pages()
3132 * if the source of the write was an mmap'ed region of the file
3133 * we're writing. Either one is a pretty crazy thing to do,
3134 * so we don't support it 100%. If this invalidation
3135 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003136 *
3137 * Most of the time we do not need this since dio_complete() will do
3138 * the invalidation for us. However there are some file systems that
3139 * do not end up with dio_complete() being called, so let's not break
3140 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003141 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003142 if (mapping->nrpages)
3143 invalidate_inode_pages2_range(mapping,
3144 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003147 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003148 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003149 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3150 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 mark_inode_dirty(inode);
3152 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003153 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
Al Viro639a93a52017-04-13 14:10:15 -04003155 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003156out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return written;
3158}
3159EXPORT_SYMBOL(generic_file_direct_write);
3160
Nick Piggineb2be182007-10-16 01:24:57 -07003161/*
3162 * Find or create a page at the given pagecache position. Return the locked
3163 * page. This function is specifically for buffered writes.
3164 */
Nick Piggin54566b22009-01-04 12:00:53 -08003165struct page *grab_cache_page_write_begin(struct address_space *mapping,
3166 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003167{
Nick Piggineb2be182007-10-16 01:24:57 -07003168 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003169 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003170
Nick Piggin54566b22009-01-04 12:00:53 -08003171 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003172 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003173
Mel Gorman2457aec2014-06-04 16:10:31 -07003174 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003175 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003176 if (page)
3177 wait_for_stable_page(page);
3178
Nick Piggineb2be182007-10-16 01:24:57 -07003179 return page;
3180}
Nick Piggin54566b22009-01-04 12:00:53 -08003181EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003182
Al Viro3b93f912014-02-11 21:34:08 -05003183ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003184 struct iov_iter *i, loff_t pos)
3185{
3186 struct address_space *mapping = file->f_mapping;
3187 const struct address_space_operations *a_ops = mapping->a_ops;
3188 long status = 0;
3189 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003190 unsigned int flags = 0;
3191
Nick Pigginafddba42007-10-16 01:25:01 -07003192 do {
3193 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003194 unsigned long offset; /* Offset into pagecache page */
3195 unsigned long bytes; /* Bytes to write to page */
3196 size_t copied; /* Bytes copied from user */
3197 void *fsdata;
3198
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003199 offset = (pos & (PAGE_SIZE - 1));
3200 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003201 iov_iter_count(i));
3202
3203again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003204 /*
3205 * Bring in the user page that we will copy from _first_.
3206 * Otherwise there's a nasty deadlock on copying from the
3207 * same page as we're writing to, without it being marked
3208 * up-to-date.
3209 *
3210 * Not only is this an optimisation, but it is also required
3211 * to check that the address is actually valid, when atomic
3212 * usercopies are used, below.
3213 */
3214 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3215 status = -EFAULT;
3216 break;
3217 }
3218
Jan Kara296291c2015-10-22 13:32:21 -07003219 if (fatal_signal_pending(current)) {
3220 status = -EINTR;
3221 break;
3222 }
3223
Nick Piggin674b8922007-10-16 01:25:03 -07003224 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003225 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003226 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003227 break;
3228
anfei zhou931e80e2010-02-02 13:44:02 -08003229 if (mapping_writably_mapped(mapping))
3230 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003231
Nick Pigginafddba42007-10-16 01:25:01 -07003232 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003233 flush_dcache_page(page);
3234
3235 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3236 page, fsdata);
3237 if (unlikely(status < 0))
3238 break;
3239 copied = status;
3240
3241 cond_resched();
3242
Nick Piggin124d3b72008-02-02 15:01:17 +01003243 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003244 if (unlikely(copied == 0)) {
3245 /*
3246 * If we were unable to copy any data at all, we must
3247 * fall back to a single segment length write.
3248 *
3249 * If we didn't fallback here, we could livelock
3250 * because not all segments in the iov can be copied at
3251 * once without a pagefault.
3252 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003253 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003254 iov_iter_single_seg_count(i));
3255 goto again;
3256 }
Nick Pigginafddba42007-10-16 01:25:01 -07003257 pos += copied;
3258 written += copied;
3259
3260 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003261 } while (iov_iter_count(i));
3262
3263 return written ? written : status;
3264}
Al Viro3b93f912014-02-11 21:34:08 -05003265EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Jan Karae4dd9de2009-08-17 18:10:06 +02003267/**
Al Viro81742022014-04-03 03:17:43 -04003268 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003269 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003270 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003271 *
3272 * This function does all the work needed for actually writing data to a
3273 * file. It does all basic checks, removes SUID from the file, updates
3274 * modification times and calls proper subroutines depending on whether we
3275 * do direct IO or a standard buffered write.
3276 *
3277 * It expects i_mutex to be grabbed unless we work on a block device or similar
3278 * object which does not need locking at all.
3279 *
3280 * This function does *not* take care of syncing data in case of O_SYNC write.
3281 * A caller has to handle it. This is mainly due to the fact that we want to
3282 * avoid syncing under i_mutex.
3283 */
Al Viro81742022014-04-03 03:17:43 -04003284ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
3286 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003287 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003289 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003291 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003294 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003295 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (err)
3297 goto out;
3298
Josef Bacikc3b2da32012-03-26 09:59:21 -04003299 err = file_update_time(file);
3300 if (err)
3301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Al Viro2ba48ce2015-04-09 13:52:01 -04003303 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003304 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003305
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003306 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003307 /*
3308 * If the write stopped short of completing, fall back to
3309 * buffered writes. Some filesystems do this for writes to
3310 * holes, for example. For DAX files, a buffered write will
3311 * not succeed (even if it did, DAX does not handle dirty
3312 * page-cache pages correctly).
3313 */
Al Viro0b8def92015-04-07 10:22:53 -04003314 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003316
Al Viro0b8def92015-04-07 10:22:53 -04003317 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003318 /*
Al Viro3b93f912014-02-11 21:34:08 -05003319 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003320 * then we want to return the number of bytes which were
3321 * direct-written, or the error code if that was zero. Note
3322 * that this differs from normal direct-io semantics, which
3323 * will return -EFOO even if some bytes were written.
3324 */
Al Viro60bb4522014-08-08 12:39:16 -04003325 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003326 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003327 goto out;
3328 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003329 /*
3330 * We need to ensure that the page cache pages are written to
3331 * disk and invalidated to preserve the expected O_DIRECT
3332 * semantics.
3333 */
Al Viro3b93f912014-02-11 21:34:08 -05003334 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003335 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003336 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003337 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003338 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003339 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003340 pos >> PAGE_SHIFT,
3341 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003342 } else {
3343 /*
3344 * We don't know how much we wrote, so just return
3345 * the number of bytes which were direct-written
3346 */
3347 }
3348 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003349 written = generic_perform_write(file, from, iocb->ki_pos);
3350 if (likely(written > 0))
3351 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353out:
3354 current->backing_dev_info = NULL;
3355 return written ? written : err;
3356}
Al Viro81742022014-04-03 03:17:43 -04003357EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Jan Karae4dd9de2009-08-17 18:10:06 +02003359/**
Al Viro81742022014-04-03 03:17:43 -04003360 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003361 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003362 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003363 *
Al Viro81742022014-04-03 03:17:43 -04003364 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003365 * filesystems. It takes care of syncing the file in case of O_SYNC file
3366 * and acquires i_mutex as needed.
3367 */
Al Viro81742022014-04-03 03:17:43 -04003368ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003371 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Al Viro59551022016-01-22 15:40:57 -05003374 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003375 ret = generic_write_checks(iocb, from);
3376 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003377 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003378 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Christoph Hellwige2592212016-04-07 08:52:01 -07003380 if (ret > 0)
3381 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return ret;
3383}
Al Viro81742022014-04-03 03:17:43 -04003384EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
David Howellscf9a2ae2006-08-29 19:05:54 +01003386/**
3387 * try_to_release_page() - release old fs-specific metadata on a page
3388 *
3389 * @page: the page which the kernel is trying to free
3390 * @gfp_mask: memory allocation flags (and I/O mode)
3391 *
3392 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003393 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003394 * Otherwise return zero.
3395 *
David Howells266cf652009-04-03 16:42:36 +01003396 * This may also be called if PG_fscache is set on a page, indicating that the
3397 * page is known to the local caching routines.
3398 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003399 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003400 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003401 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003402 */
3403int try_to_release_page(struct page *page, gfp_t gfp_mask)
3404{
3405 struct address_space * const mapping = page->mapping;
3406
3407 BUG_ON(!PageLocked(page));
3408 if (PageWriteback(page))
3409 return 0;
3410
3411 if (mapping && mapping->a_ops->releasepage)
3412 return mapping->a_ops->releasepage(page, gfp_mask);
3413 return try_to_free_buffers(page);
3414}
3415
3416EXPORT_SYMBOL(try_to_release_page);