blob: 41586009fa4228c91a9988ff1c90a763709b039a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/filemap.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
11 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080014#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070017#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/mman.h>
24#include <linux/pagemap.h>
25#include <linux/file.h>
26#include <linux/uio.h>
27#include <linux/hash.h>
28#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070029#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/pagevec.h>
31#include <linux/blkdev.h>
32#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080033#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070034#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060036#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080037#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070038#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070039#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070040#include <linux/psi.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080041#include "internal.h"
42
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070043#define CREATE_TRACE_POINTS
44#include <trace/events/filemap.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * FIXME: remove all knowledge of the buffer layer from the core VM
48 */
Jan Kara148f9482009-08-17 19:52:36 +020049#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/mman.h>
52
53/*
54 * Shared mappings implemented 30.11.1994. It's not fully working yet,
55 * though.
56 *
57 * Shared mappings now work. 15.8.1995 Bruno.
58 *
59 * finished 'unifying' the page and buffer cache and SMP-threaded the
60 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
61 *
62 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
63 */
64
65/*
66 * Lock ordering:
67 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080068 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070070 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070071 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080073 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080074 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080077 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070078 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070079 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
81 * ->mmap_sem
82 * ->lock_page (access_process_vm)
83 *
Al Viroccad2362014-02-11 22:36:48 -050084 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070085 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060087 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110088 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070089 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080091 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ->anon_vma.lock (vma_adjust)
93 *
94 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070095 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070097 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070098 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700100 * ->i_pages lock (try_to_unmap_one)
Mel Gormana52633d2016-07-28 15:45:28 -0700101 * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
102 * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700104 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600105 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100106 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700107 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600108 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100109 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
111 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800112 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700113 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700116static int page_cache_tree_insert(struct address_space *mapping,
117 struct page *page, void **shadowp)
118{
119 struct radix_tree_node *node;
120 void **slot;
121 int error;
122
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700123 error = __radix_tree_create(&mapping->i_pages, page->index, 0,
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700124 &node, &slot);
125 if (error)
126 return error;
127 if (*slot) {
128 void *p;
129
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700130 p = radix_tree_deref_slot_protected(slot,
131 &mapping->i_pages.xa_lock);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700132 if (!radix_tree_exceptional_entry(p))
133 return -EEXIST;
134
135 mapping->nrexceptional--;
Ross Zwislerd01ad192017-09-06 16:18:47 -0700136 if (shadowp)
137 *shadowp = p;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700138 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700139 __radix_tree_replace(&mapping->i_pages, node, slot, page,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800140 workingset_lookup_update(mapping));
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700141 mapping->nrpages++;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700142 return 0;
143}
144
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700145static void page_cache_tree_delete(struct address_space *mapping,
146 struct page *page, void *shadow)
147{
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800148 int i, nr;
149
150 /* hugetlb pages are represented by one entry in the radix tree */
151 nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700152
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700153 VM_BUG_ON_PAGE(!PageLocked(page), page);
154 VM_BUG_ON_PAGE(PageTail(page), page);
155 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700156
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700157 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200158 struct radix_tree_node *node;
159 void **slot;
160
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700161 __radix_tree_lookup(&mapping->i_pages, page->index + i,
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200162 &node, &slot);
163
Johannes Weinerdbc446b2016-12-12 16:43:55 -0800164 VM_BUG_ON_PAGE(!node && nr != 1, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700165
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700166 radix_tree_clear_tags(&mapping->i_pages, node, slot);
167 __radix_tree_replace(&mapping->i_pages, node, slot, shadow,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800168 workingset_lookup_update(mapping));
Johannes Weiner449dd692014-04-03 14:47:56 -0700169 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200170
Jan Kara23006382017-11-15 17:37:26 -0800171 page->mapping = NULL;
172 /* Leave page->index set: truncation lookup relies upon it */
173
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200174 if (shadow) {
175 mapping->nrexceptional += nr;
176 /*
177 * Make sure the nrexceptional update is committed before
178 * the nrpages update so that final truncate racing
179 * with reclaim does not see both counters 0 at the
180 * same time and miss a shadow entry.
181 */
182 smp_wmb();
183 }
184 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700185}
186
Jan Kara5ecc4d82017-11-15 17:37:29 -0800187static void unaccount_page_cache_page(struct address_space *mapping,
188 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800190 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600192 /*
193 * if we're uptodate, flush out into the cleancache, otherwise
194 * invalidate any existing cleancache entries. We can't leave
195 * stale data around in the cleancache once our page is gone
196 */
197 if (PageUptodate(page) && PageMappedToDisk(page))
198 cleancache_put_page(page);
199 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400200 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600201
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700202 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800203 VM_BUG_ON_PAGE(page_mapped(page), page);
204 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
205 int mapcount;
206
207 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
208 current->comm, page_to_pfn(page));
209 dump_page(page, "still mapped when deleted");
210 dump_stack();
211 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
212
213 mapcount = page_mapcount(page);
214 if (mapping_exiting(mapping) &&
215 page_count(page) >= mapcount + 2) {
216 /*
217 * All vmas have already been torn down, so it's
218 * a good bet that actually the page is unmapped,
219 * and we'd prefer not to leak it: if we're wrong,
220 * some other bad page check should catch it later.
221 */
222 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700223 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800224 }
225 }
226
Jan Kara76253fb2017-11-15 17:37:22 -0800227 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800228 if (PageHuge(page))
229 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800230
Jan Kara5ecc4d82017-11-15 17:37:29 -0800231 nr = hpage_nr_pages(page);
232
233 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
234 if (PageSwapBacked(page)) {
235 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
236 if (PageTransHuge(page))
237 __dec_node_page_state(page, NR_SHMEM_THPS);
238 } else {
239 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800240 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800241
242 /*
243 * At this point page must be either written or cleaned by
244 * truncate. Dirty page here signals a bug and loss of
245 * unwritten data.
246 *
247 * This fixes dirty accounting after removing the page entirely
248 * but leaves PageDirty set: it has no effect for truncated
249 * page and anyway will be cleared before returning page into
250 * buddy allocator.
251 */
252 if (WARN_ON_ONCE(PageDirty(page)))
253 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
254}
255
256/*
257 * Delete a page from the page cache and free it. Caller has to make
258 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700259 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800260 */
261void __delete_from_page_cache(struct page *page, void *shadow)
262{
263 struct address_space *mapping = page->mapping;
264
265 trace_mm_filemap_delete_from_page_cache(page);
266
267 unaccount_page_cache_page(mapping, page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700268 page_cache_tree_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Jan Kara59c66c52017-11-15 17:37:18 -0800271static void page_cache_free_page(struct address_space *mapping,
272 struct page *page)
273{
274 void (*freepage)(struct page *);
275
276 freepage = mapping->a_ops->freepage;
277 if (freepage)
278 freepage(page);
279
280 if (PageTransHuge(page) && !PageHuge(page)) {
281 page_ref_sub(page, HPAGE_PMD_NR);
282 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
283 } else {
284 put_page(page);
285 }
286}
287
Minchan Kim702cfbf2011-03-22 16:32:43 -0700288/**
289 * delete_from_page_cache - delete page from page cache
290 * @page: the page which the kernel is trying to remove from page cache
291 *
292 * This must be called only on pages that have been verified to be in the page
293 * cache and locked. It will never put the page into the free list, the caller
294 * has a reference on the page.
295 */
296void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700298 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400299 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Matt Mackallcd7619d2005-05-01 08:59:01 -0700301 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700302 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700303 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700304 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500305
Jan Kara59c66c52017-11-15 17:37:18 -0800306 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700307}
308EXPORT_SYMBOL(delete_from_page_cache);
309
Jan Karaaa65c292017-11-15 17:37:33 -0800310/*
311 * page_cache_tree_delete_batch - delete several pages from page cache
312 * @mapping: the mapping to which pages belong
313 * @pvec: pagevec with pages to delete
314 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700315 * The function walks over mapping->i_pages and removes pages passed in @pvec
316 * from the mapping. The function expects @pvec to be sorted by page index.
317 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800318 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700319 * @pvec and takes care to delete all corresponding tail pages from the
320 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800321 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700322 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800323 */
324static void
325page_cache_tree_delete_batch(struct address_space *mapping,
326 struct pagevec *pvec)
327{
328 struct radix_tree_iter iter;
329 void **slot;
330 int total_pages = 0;
331 int i = 0, tail_pages = 0;
332 struct page *page;
333 pgoff_t start;
334
335 start = pvec->pages[0]->index;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700336 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Jan Karaaa65c292017-11-15 17:37:33 -0800337 if (i >= pagevec_count(pvec) && !tail_pages)
338 break;
339 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700340 &mapping->i_pages.xa_lock);
Jan Karaaa65c292017-11-15 17:37:33 -0800341 if (radix_tree_exceptional_entry(page))
342 continue;
343 if (!tail_pages) {
344 /*
345 * Some page got inserted in our range? Skip it. We
346 * have our pages locked so they are protected from
347 * being removed.
348 */
349 if (page != pvec->pages[i])
350 continue;
351 WARN_ON_ONCE(!PageLocked(page));
352 if (PageTransHuge(page) && !PageHuge(page))
353 tail_pages = HPAGE_PMD_NR - 1;
354 page->mapping = NULL;
355 /*
356 * Leave page->index set: truncation lookup relies
357 * upon it
358 */
359 i++;
360 } else {
361 tail_pages--;
362 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700363 radix_tree_clear_tags(&mapping->i_pages, iter.node, slot);
364 __radix_tree_replace(&mapping->i_pages, iter.node, slot, NULL,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800365 workingset_lookup_update(mapping));
Jan Karaaa65c292017-11-15 17:37:33 -0800366 total_pages++;
367 }
368 mapping->nrpages -= total_pages;
369}
370
371void delete_from_page_cache_batch(struct address_space *mapping,
372 struct pagevec *pvec)
373{
374 int i;
375 unsigned long flags;
376
377 if (!pagevec_count(pvec))
378 return;
379
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700380 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800381 for (i = 0; i < pagevec_count(pvec); i++) {
382 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
383
384 unaccount_page_cache_page(mapping, pvec->pages[i]);
385 }
386 page_cache_tree_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700387 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800388
389 for (i = 0; i < pagevec_count(pvec); i++)
390 page_cache_free_page(mapping, pvec->pages[i]);
391}
392
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200393int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700394{
395 int ret = 0;
396 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700397 if (test_bit(AS_ENOSPC, &mapping->flags) &&
398 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700399 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700400 if (test_bit(AS_EIO, &mapping->flags) &&
401 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700402 ret = -EIO;
403 return ret;
404}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200405EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700406
Jeff Layton76341ca2017-07-06 07:02:22 -0400407static int filemap_check_and_keep_errors(struct address_space *mapping)
408{
409 /* Check for outstanding write errors */
410 if (test_bit(AS_EIO, &mapping->flags))
411 return -EIO;
412 if (test_bit(AS_ENOSPC, &mapping->flags))
413 return -ENOSPC;
414 return 0;
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700418 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700419 * @mapping: address space structure to write
420 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800421 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700422 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700424 * Start writeback against all of a mapping's dirty pages that lie
425 * within the byte offsets <start, end> inclusive.
426 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700428 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * these two operations is that if a dirty page/buffer is encountered, it must
430 * be waited upon, and not just skipped over.
431 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800432int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
433 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 int ret;
436 struct writeback_control wbc = {
437 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800438 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700439 .range_start = start,
440 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 };
442
443 if (!mapping_cap_writeback_dirty(mapping))
444 return 0;
445
Tejun Heob16b1de2015-06-02 08:39:48 -0600446 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600448 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return ret;
450}
451
452static inline int __filemap_fdatawrite(struct address_space *mapping,
453 int sync_mode)
454{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700455 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458int filemap_fdatawrite(struct address_space *mapping)
459{
460 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
461}
462EXPORT_SYMBOL(filemap_fdatawrite);
463
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400464int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800465 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
468}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400469EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Randy Dunlap485bb992006-06-23 02:03:49 -0700471/**
472 * filemap_flush - mostly a non-blocking flush
473 * @mapping: target address_space
474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * This is a mostly non-blocking flush. Not suitable for data-integrity
476 * purposes - I/O may not be started against all dirty pages.
477 */
478int filemap_flush(struct address_space *mapping)
479{
480 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
481}
482EXPORT_SYMBOL(filemap_flush);
483
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500484/**
485 * filemap_range_has_page - check if a page exists in range.
486 * @mapping: address space within which to check
487 * @start_byte: offset in bytes where the range starts
488 * @end_byte: offset in bytes where the range ends (inclusive)
489 *
490 * Find at least one page in the range supplied, usually used to check if
491 * direct writing in this range will trigger a writeback.
492 */
493bool filemap_range_has_page(struct address_space *mapping,
494 loff_t start_byte, loff_t end_byte)
495{
496 pgoff_t index = start_byte >> PAGE_SHIFT;
497 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700498 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500499
500 if (end_byte < start_byte)
501 return false;
502
503 if (mapping->nrpages == 0)
504 return false;
505
Jan Karaf7b68042017-09-06 16:21:40 -0700506 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500507 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700508 put_page(page);
509 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500510}
511EXPORT_SYMBOL(filemap_range_has_page);
512
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400513static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800514 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300516 pgoff_t index = start_byte >> PAGE_SHIFT;
517 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct pagevec pvec;
519 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200521 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400522 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Mel Gorman86679822017-11-15 17:37:52 -0800524 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800525 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned i;
527
Jan Kara312e9d22017-11-15 17:35:05 -0800528 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800529 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800530 if (!nr_pages)
531 break;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 for (i = 0; i < nr_pages; i++) {
534 struct page *page = pvec.pages[i];
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400537 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 pagevec_release(&pvec);
540 cond_resched();
541 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800542}
543
544/**
545 * filemap_fdatawait_range - wait for writeback to complete
546 * @mapping: address space structure to wait for
547 * @start_byte: offset in bytes where the range starts
548 * @end_byte: offset in bytes where the range ends (inclusive)
549 *
550 * Walk the list of under-writeback pages of the given address space
551 * in the given range and wait for all of them. Check error status of
552 * the address space and return it.
553 *
554 * Since the error status of the address space is cleared by this function,
555 * callers are responsible for checking the return value and handling and/or
556 * reporting the error.
557 */
558int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
559 loff_t end_byte)
560{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400561 __filemap_fdatawait_range(mapping, start_byte, end_byte);
562 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200564EXPORT_SYMBOL(filemap_fdatawait_range);
565
566/**
Jeff Laytona823e452017-07-28 07:24:43 -0400567 * file_fdatawait_range - wait for writeback to complete
568 * @file: file pointing to address space structure to wait for
569 * @start_byte: offset in bytes where the range starts
570 * @end_byte: offset in bytes where the range ends (inclusive)
571 *
572 * Walk the list of under-writeback pages of the address space that file
573 * refers to, in the given range and wait for all of them. Check error
574 * status of the address space vs. the file->f_wb_err cursor and return it.
575 *
576 * Since the error status of the file is advanced by this function,
577 * callers are responsible for checking the return value and handling and/or
578 * reporting the error.
579 */
580int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
581{
582 struct address_space *mapping = file->f_mapping;
583
584 __filemap_fdatawait_range(mapping, start_byte, end_byte);
585 return file_check_and_advance_wb_err(file);
586}
587EXPORT_SYMBOL(file_fdatawait_range);
588
589/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800590 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
591 * @mapping: address space structure to wait for
592 *
593 * Walk the list of under-writeback pages of the given address space
594 * and wait for all of them. Unlike filemap_fdatawait(), this function
595 * does not clear error status of the address space.
596 *
597 * Use this function if callers don't handle errors themselves. Expected
598 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
599 * fsfreeze(8)
600 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400601int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800602{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400603 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400604 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800605}
Jeff Layton76341ca2017-07-06 07:02:22 -0400606EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800607
Jeff Layton9326c9b2017-07-26 10:21:11 -0400608static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400610 return (!dax_mapping(mapping) && mapping->nrpages) ||
611 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614int filemap_write_and_wait(struct address_space *mapping)
615{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800616 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Jeff Layton9326c9b2017-07-26 10:21:11 -0400618 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800619 err = filemap_fdatawrite(mapping);
620 /*
621 * Even if the above returned error, the pages may be
622 * written partially (e.g. -ENOSPC), so we wait for it.
623 * But the -EIO is special case, it may indicate the worst
624 * thing (e.g. bug) happened, so we avoid waiting for it.
625 */
626 if (err != -EIO) {
627 int err2 = filemap_fdatawait(mapping);
628 if (!err)
629 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400630 } else {
631 /* Clear any previously stored errors */
632 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800633 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700634 } else {
635 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800637 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800639EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Randy Dunlap485bb992006-06-23 02:03:49 -0700641/**
642 * filemap_write_and_wait_range - write out & wait on a file range
643 * @mapping: the address_space for the pages
644 * @lstart: offset in bytes where the range starts
645 * @lend: offset in bytes where the range ends (inclusive)
646 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800647 * Write out and wait upon file offsets lstart->lend, inclusive.
648 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300649 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800650 * that this function can be used to write to the very end-of-file (end = -1).
651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652int filemap_write_and_wait_range(struct address_space *mapping,
653 loff_t lstart, loff_t lend)
654{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800655 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jeff Layton9326c9b2017-07-26 10:21:11 -0400657 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800658 err = __filemap_fdatawrite_range(mapping, lstart, lend,
659 WB_SYNC_ALL);
660 /* See comment of filemap_write_and_wait() */
661 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200662 int err2 = filemap_fdatawait_range(mapping,
663 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800664 if (!err)
665 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400666 } else {
667 /* Clear any previously stored errors */
668 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800669 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700670 } else {
671 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
Chris Masonf6995582009-04-15 13:22:37 -0400675EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jeff Layton5660e132017-07-06 07:02:25 -0400677void __filemap_set_wb_err(struct address_space *mapping, int err)
678{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400679 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400680
681 trace_filemap_set_wb_err(mapping, eseq);
682}
683EXPORT_SYMBOL(__filemap_set_wb_err);
684
685/**
686 * file_check_and_advance_wb_err - report wb error (if any) that was previously
687 * and advance wb_err to current one
688 * @file: struct file on which the error is being reported
689 *
690 * When userland calls fsync (or something like nfsd does the equivalent), we
691 * want to report any writeback errors that occurred since the last fsync (or
692 * since the file was opened if there haven't been any).
693 *
694 * Grab the wb_err from the mapping. If it matches what we have in the file,
695 * then just quickly return 0. The file is all caught up.
696 *
697 * If it doesn't match, then take the mapping value, set the "seen" flag in
698 * it and try to swap it into place. If it works, or another task beat us
699 * to it with the new value, then update the f_wb_err and return the error
700 * portion. The error at this point must be reported via proper channels
701 * (a'la fsync, or NFS COMMIT operation, etc.).
702 *
703 * While we handle mapping->wb_err with atomic operations, the f_wb_err
704 * value is protected by the f_lock since we must ensure that it reflects
705 * the latest value swapped in for this file descriptor.
706 */
707int file_check_and_advance_wb_err(struct file *file)
708{
709 int err = 0;
710 errseq_t old = READ_ONCE(file->f_wb_err);
711 struct address_space *mapping = file->f_mapping;
712
713 /* Locklessly handle the common case where nothing has changed */
714 if (errseq_check(&mapping->wb_err, old)) {
715 /* Something changed, must use slow path */
716 spin_lock(&file->f_lock);
717 old = file->f_wb_err;
718 err = errseq_check_and_advance(&mapping->wb_err,
719 &file->f_wb_err);
720 trace_file_check_and_advance_wb_err(file, old);
721 spin_unlock(&file->f_lock);
722 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700723
724 /*
725 * We're mostly using this function as a drop in replacement for
726 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
727 * that the legacy code would have had on these flags.
728 */
729 clear_bit(AS_EIO, &mapping->flags);
730 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400731 return err;
732}
733EXPORT_SYMBOL(file_check_and_advance_wb_err);
734
735/**
736 * file_write_and_wait_range - write out & wait on a file range
737 * @file: file pointing to address_space with pages
738 * @lstart: offset in bytes where the range starts
739 * @lend: offset in bytes where the range ends (inclusive)
740 *
741 * Write out and wait upon file offsets lstart->lend, inclusive.
742 *
743 * Note that @lend is inclusive (describes the last byte to be written) so
744 * that this function can be used to write to the very end-of-file (end = -1).
745 *
746 * After writing out and waiting on the data, we check and advance the
747 * f_wb_err cursor to the latest value, and return any errors detected there.
748 */
749int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
750{
751 int err = 0, err2;
752 struct address_space *mapping = file->f_mapping;
753
Jeff Layton9326c9b2017-07-26 10:21:11 -0400754 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400755 err = __filemap_fdatawrite_range(mapping, lstart, lend,
756 WB_SYNC_ALL);
757 /* See comment of filemap_write_and_wait() */
758 if (err != -EIO)
759 __filemap_fdatawait_range(mapping, lstart, lend);
760 }
761 err2 = file_check_and_advance_wb_err(file);
762 if (!err)
763 err = err2;
764 return err;
765}
766EXPORT_SYMBOL(file_write_and_wait_range);
767
Randy Dunlap485bb992006-06-23 02:03:49 -0700768/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700769 * replace_page_cache_page - replace a pagecache page with a new one
770 * @old: page to be replaced
771 * @new: page to replace with
772 * @gfp_mask: allocation mode
773 *
774 * This function replaces a page in the pagecache with a new one. On
775 * success it acquires the pagecache reference for the new page and
776 * drops it for the old page. Both the old and new pages must be
777 * locked. This function does not add the new page to the LRU, the
778 * caller must do that.
779 *
780 * The remove + add is atomic. The only way this function can fail is
781 * memory allocation failure.
782 */
783int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
784{
785 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700786
Sasha Levin309381fea2014-01-23 15:52:54 -0800787 VM_BUG_ON_PAGE(!PageLocked(old), old);
788 VM_BUG_ON_PAGE(!PageLocked(new), new);
789 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700790
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700791 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700792 if (!error) {
793 struct address_space *mapping = old->mapping;
794 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400795 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700796
797 pgoff_t offset = old->index;
798 freepage = mapping->a_ops->freepage;
799
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300800 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700801 new->mapping = mapping;
802 new->index = offset;
803
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700804 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700805 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700806 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700807 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700808
809 /*
810 * hugetlb pages do not participate in page cache accounting.
811 */
812 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700813 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700814 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700815 __inc_node_page_state(new, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700816 xa_unlock_irqrestore(&mapping->i_pages, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700817 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700818 radix_tree_preload_end();
819 if (freepage)
820 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300821 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700822 }
823
824 return error;
825}
826EXPORT_SYMBOL_GPL(replace_page_cache_page);
827
Johannes Weinera5289102014-04-03 14:47:51 -0700828static int __add_to_page_cache_locked(struct page *page,
829 struct address_space *mapping,
830 pgoff_t offset, gfp_t gfp_mask,
831 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Johannes Weiner00501b52014-08-08 14:19:20 -0700833 int huge = PageHuge(page);
834 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700835 int error;
836
Sasha Levin309381fea2014-01-23 15:52:54 -0800837 VM_BUG_ON_PAGE(!PageLocked(page), page);
838 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700839
Johannes Weiner00501b52014-08-08 14:19:20 -0700840 if (!huge) {
841 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800842 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700843 if (error)
844 return error;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700847 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700848 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700849 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800850 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700851 return error;
852 }
853
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300854 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700855 page->mapping = mapping;
856 page->index = offset;
857
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700858 xa_lock_irq(&mapping->i_pages);
Johannes Weinera5289102014-04-03 14:47:51 -0700859 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700860 radix_tree_preload_end();
861 if (unlikely(error))
862 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700863
864 /* hugetlb pages do not participate in page cache accounting. */
865 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700866 __inc_node_page_state(page, NR_FILE_PAGES);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700867 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700868 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800869 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700870 trace_mm_filemap_add_to_page_cache(page);
871 return 0;
872err_insert:
873 page->mapping = NULL;
874 /* Leave page->index set: truncation relies upon it */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700875 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700876 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800877 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300878 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return error;
880}
Johannes Weinera5289102014-04-03 14:47:51 -0700881
882/**
883 * add_to_page_cache_locked - add a locked page to the pagecache
884 * @page: page to add
885 * @mapping: the page's address_space
886 * @offset: page index
887 * @gfp_mask: page allocation mode
888 *
889 * This function is used to add a page to the pagecache. It must be locked.
890 * This function does not add the page to the LRU. The caller must do that.
891 */
892int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
893 pgoff_t offset, gfp_t gfp_mask)
894{
895 return __add_to_page_cache_locked(page, mapping, offset,
896 gfp_mask, NULL);
897}
Nick Piggine2867812008-07-25 19:45:30 -0700898EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400901 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Johannes Weinera5289102014-04-03 14:47:51 -0700903 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700904 int ret;
905
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800906 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700907 ret = __add_to_page_cache_locked(page, mapping, offset,
908 gfp_mask, &shadow);
909 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800910 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700911 else {
912 /*
913 * The page might have been evicted from cache only
914 * recently, in which case it should be activated like
915 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700916 * The exception is pages getting rewritten; evicting other
917 * data from the working set, only to cache data that will
918 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700919 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700920 WARN_ON_ONCE(PageActive(page));
921 if (!(gfp_mask & __GFP_WRITE) && shadow)
922 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700923 lru_cache_add(page);
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return ret;
926}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300927EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Paul Jackson44110fe2006-03-24 03:16:04 -0800929#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700930struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800931{
Miao Xiec0ff7452010-05-24 14:32:08 -0700932 int n;
933 struct page *page;
934
Paul Jackson44110fe2006-03-24 03:16:04 -0800935 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700936 unsigned int cpuset_mems_cookie;
937 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700938 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700939 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700940 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700941 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700942
Miao Xiec0ff7452010-05-24 14:32:08 -0700943 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800944 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700945 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800946}
Nick Piggin2ae88142006-10-28 10:38:23 -0700947EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800948#endif
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/*
951 * In order to wait for pages to become available there must be
952 * waitqueues associated with pages. By using a hash table of
953 * waitqueues where the bucket discipline is to maintain all
954 * waiters on the same queue and wake all when any of the pages
955 * become available, and for the woken contexts to check to be
956 * sure the appropriate page became available, this saves space
957 * at a cost of "thundering herd" phenomena during rare hash
958 * collisions.
959 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000960#define PAGE_WAIT_TABLE_BITS 8
961#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
962static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
963
964static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Nicholas Piggin62906022016-12-25 13:00:30 +1000966 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
Nicholas Piggin62906022016-12-25 13:00:30 +1000968
969void __init pagecache_init(void)
970{
971 int i;
972
973 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
974 init_waitqueue_head(&page_wait_table[i]);
975
976 page_writeback_init();
977}
978
Linus Torvalds3510ca22017-08-27 13:55:12 -0700979/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000980struct wait_page_key {
981 struct page *page;
982 int bit_nr;
983 int page_match;
984};
985
986struct wait_page_queue {
987 struct page *page;
988 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200989 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000990};
991
Ingo Molnarac6424b2017-06-20 12:06:13 +0200992static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000993{
994 struct wait_page_key *key = arg;
995 struct wait_page_queue *wait_page
996 = container_of(wait, struct wait_page_queue, wait);
997
998 if (wait_page->page != key->page)
999 return 0;
1000 key->page_match = 1;
1001
1002 if (wait_page->bit_nr != key->bit_nr)
1003 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001004
1005 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +10001006 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001007 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001008
1009 return autoremove_wake_function(wait, mode, sync, key);
1010}
1011
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001012static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001013{
1014 wait_queue_head_t *q = page_waitqueue(page);
1015 struct wait_page_key key;
1016 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001017 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001018
1019 key.page = page;
1020 key.bit_nr = bit_nr;
1021 key.page_match = 0;
1022
Tim Chen11a19c72017-08-25 09:13:55 -07001023 bookmark.flags = 0;
1024 bookmark.private = NULL;
1025 bookmark.func = NULL;
1026 INIT_LIST_HEAD(&bookmark.entry);
1027
Nicholas Piggin62906022016-12-25 13:00:30 +10001028 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001029 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1030
1031 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1032 /*
1033 * Take a breather from holding the lock,
1034 * allow pages that finish wake up asynchronously
1035 * to acquire the lock and remove themselves
1036 * from wait queue
1037 */
1038 spin_unlock_irqrestore(&q->lock, flags);
1039 cpu_relax();
1040 spin_lock_irqsave(&q->lock, flags);
1041 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1042 }
1043
Nicholas Piggin62906022016-12-25 13:00:30 +10001044 /*
1045 * It is possible for other pages to have collided on the waitqueue
1046 * hash, so in that case check for a page match. That prevents a long-
1047 * term waiter
1048 *
1049 * It is still possible to miss a case here, when we woke page waiters
1050 * and removed them from the waitqueue, but there are still other
1051 * page waiters.
1052 */
1053 if (!waitqueue_active(q) || !key.page_match) {
1054 ClearPageWaiters(page);
1055 /*
1056 * It's possible to miss clearing Waiters here, when we woke
1057 * our page waiters, but the hashed waitqueue has waiters for
1058 * other pages on it.
1059 *
1060 * That's okay, it's a rare case. The next waker will clear it.
1061 */
1062 }
1063 spin_unlock_irqrestore(&q->lock, flags);
1064}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001065
1066static void wake_up_page(struct page *page, int bit)
1067{
1068 if (!PageWaiters(page))
1069 return;
1070 wake_up_page_bit(page, bit);
1071}
Nicholas Piggin62906022016-12-25 13:00:30 +10001072
1073static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1074 struct page *page, int bit_nr, int state, bool lock)
1075{
1076 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001077 wait_queue_entry_t *wait = &wait_page.wait;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001078 bool thrashing = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001079 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001080 int ret = 0;
1081
Johannes Weinereb414682018-10-26 15:06:27 -07001082 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001083 !PageUptodate(page) && PageWorkingset(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001084 if (!PageSwapBacked(page))
1085 delayacct_thrashing_start();
1086 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001087 thrashing = true;
1088 }
1089
Nicholas Piggin62906022016-12-25 13:00:30 +10001090 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001091 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001092 wait->func = wake_page_function;
1093 wait_page.page = page;
1094 wait_page.bit_nr = bit_nr;
1095
1096 for (;;) {
1097 spin_lock_irq(&q->lock);
1098
Ingo Molnar2055da92017-06-20 12:06:46 +02001099 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001100 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001101 SetPageWaiters(page);
1102 }
1103
1104 set_current_state(state);
1105
1106 spin_unlock_irq(&q->lock);
1107
1108 if (likely(test_bit(bit_nr, &page->flags))) {
1109 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001110 }
1111
1112 if (lock) {
1113 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1114 break;
1115 } else {
1116 if (!test_bit(bit_nr, &page->flags))
1117 break;
1118 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001119
1120 if (unlikely(signal_pending_state(state, current))) {
1121 ret = -EINTR;
1122 break;
1123 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001124 }
1125
1126 finish_wait(q, wait);
1127
Johannes Weinereb414682018-10-26 15:06:27 -07001128 if (thrashing) {
1129 if (!PageSwapBacked(page))
1130 delayacct_thrashing_end();
1131 psi_memstall_leave(&pflags);
1132 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001133
Nicholas Piggin62906022016-12-25 13:00:30 +10001134 /*
1135 * A signal could leave PageWaiters set. Clearing it here if
1136 * !waitqueue_active would be possible (by open-coding finish_wait),
1137 * but still fail to catch it in the case of wait hash collision. We
1138 * already can fail to clear wait hash collision cases, so don't
1139 * bother with signals either.
1140 */
1141
1142 return ret;
1143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Harvey Harrison920c7a52008-02-04 22:29:26 -08001145void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Nicholas Piggin62906022016-12-25 13:00:30 +10001147 wait_queue_head_t *q = page_waitqueue(page);
1148 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150EXPORT_SYMBOL(wait_on_page_bit);
1151
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001152int wait_on_page_bit_killable(struct page *page, int bit_nr)
1153{
Nicholas Piggin62906022016-12-25 13:00:30 +10001154 wait_queue_head_t *q = page_waitqueue(page);
1155 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001156}
David Howells4343d002017-11-02 15:27:52 +00001157EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001160 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001161 * @page: Page defining the wait queue of interest
1162 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001163 *
1164 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1165 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001166void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001167{
1168 wait_queue_head_t *q = page_waitqueue(page);
1169 unsigned long flags;
1170
1171 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001172 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001173 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001174 spin_unlock_irqrestore(&q->lock, flags);
1175}
1176EXPORT_SYMBOL_GPL(add_page_wait_queue);
1177
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001178#ifndef clear_bit_unlock_is_negative_byte
1179
1180/*
1181 * PG_waiters is the high bit in the same byte as PG_lock.
1182 *
1183 * On x86 (and on many other architectures), we can clear PG_lock and
1184 * test the sign bit at the same time. But if the architecture does
1185 * not support that special operation, we just do this all by hand
1186 * instead.
1187 *
1188 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1189 * being cleared, but a memory barrier should be unneccssary since it is
1190 * in the same byte as PG_locked.
1191 */
1192static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1193{
1194 clear_bit_unlock(nr, mem);
1195 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001196 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001197}
1198
1199#endif
1200
David Howells385e1ca5f2009-04-03 16:42:39 +01001201/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001202 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * @page: the page
1204 *
1205 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1206 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001207 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1209 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001210 * Note that this depends on PG_waiters being the sign bit in the byte
1211 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1212 * clear the PG_locked bit and test PG_waiters at the same time fairly
1213 * portably (architectures that do LL/SC can test any bit, while x86 can
1214 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001216void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001218 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001219 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001220 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001221 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1222 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224EXPORT_SYMBOL(unlock_page);
1225
Randy Dunlap485bb992006-06-23 02:03:49 -07001226/**
1227 * end_page_writeback - end writeback against a page
1228 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
1230void end_page_writeback(struct page *page)
1231{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001232 /*
1233 * TestClearPageReclaim could be used here but it is an atomic
1234 * operation and overkill in this particular case. Failing to
1235 * shuffle a page marked for immediate reclaim is too mild to
1236 * justify taking an atomic operation penalty at the end of
1237 * ever page writeback.
1238 */
1239 if (PageReclaim(page)) {
1240 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001241 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001242 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001243
1244 if (!test_clear_page_writeback(page))
1245 BUG();
1246
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001247 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 wake_up_page(page, PG_writeback);
1249}
1250EXPORT_SYMBOL(end_page_writeback);
1251
Matthew Wilcox57d99842014-06-04 16:07:45 -07001252/*
1253 * After completing I/O on a page, call this routine to update the page
1254 * flags appropriately
1255 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001256void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001257{
Jens Axboec11f0c02016-08-05 08:11:04 -06001258 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001259 if (!err) {
1260 SetPageUptodate(page);
1261 } else {
1262 ClearPageUptodate(page);
1263 SetPageError(page);
1264 }
1265 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001266 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001267 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001268 struct address_space *mapping;
1269
Matthew Wilcox57d99842014-06-04 16:07:45 -07001270 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001271 mapping = page_mapping(page);
1272 if (mapping)
1273 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001274 }
1275 end_page_writeback(page);
1276 }
1277}
1278EXPORT_SYMBOL_GPL(page_endio);
1279
Randy Dunlap485bb992006-06-23 02:03:49 -07001280/**
1281 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001282 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001284void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Nicholas Piggin62906022016-12-25 13:00:30 +10001286 struct page *page = compound_head(__page);
1287 wait_queue_head_t *q = page_waitqueue(page);
1288 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290EXPORT_SYMBOL(__lock_page);
1291
Nicholas Piggin62906022016-12-25 13:00:30 +10001292int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001293{
Nicholas Piggin62906022016-12-25 13:00:30 +10001294 struct page *page = compound_head(__page);
1295 wait_queue_head_t *q = page_waitqueue(page);
1296 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001297}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001298EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001299
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001300/*
1301 * Return values:
1302 * 1 - page is locked; mmap_sem is still held.
1303 * 0 - page is not locked.
1304 * mmap_sem has been released (up_read()), unless flags had both
1305 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1306 * which case mmap_sem is still held.
1307 *
1308 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1309 * with the page locked and the mmap_sem unperturbed.
1310 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001311int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1312 unsigned int flags)
1313{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001314 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1315 /*
1316 * CAUTION! In this case, mmap_sem is not released
1317 * even though return 0.
1318 */
1319 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1320 return 0;
1321
1322 up_read(&mm->mmap_sem);
1323 if (flags & FAULT_FLAG_KILLABLE)
1324 wait_on_page_locked_killable(page);
1325 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001326 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001327 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001328 } else {
1329 if (flags & FAULT_FLAG_KILLABLE) {
1330 int ret;
1331
1332 ret = __lock_page_killable(page);
1333 if (ret) {
1334 up_read(&mm->mmap_sem);
1335 return 0;
1336 }
1337 } else
1338 __lock_page(page);
1339 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001340 }
1341}
1342
Randy Dunlap485bb992006-06-23 02:03:49 -07001343/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001344 * page_cache_next_hole - find the next hole (not-present entry)
1345 * @mapping: mapping
1346 * @index: index
1347 * @max_scan: maximum range to search
1348 *
1349 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1350 * lowest indexed hole.
1351 *
1352 * Returns: the index of the hole if found, otherwise returns an index
1353 * outside of the set specified (in which case 'return - index >=
1354 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1355 * be returned.
1356 *
1357 * page_cache_next_hole may be called under rcu_read_lock. However,
1358 * like radix_tree_gang_lookup, this will not atomically search a
1359 * snapshot of the tree at a single point in time. For example, if a
1360 * hole is created at index 5, then subsequently a hole is created at
1361 * index 10, page_cache_next_hole covering both indexes may return 10
1362 * if called under rcu_read_lock.
1363 */
1364pgoff_t page_cache_next_hole(struct address_space *mapping,
1365 pgoff_t index, unsigned long max_scan)
1366{
1367 unsigned long i;
1368
1369 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001370 struct page *page;
1371
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001372 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001373 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001374 break;
1375 index++;
1376 if (index == 0)
1377 break;
1378 }
1379
1380 return index;
1381}
1382EXPORT_SYMBOL(page_cache_next_hole);
1383
1384/**
1385 * page_cache_prev_hole - find the prev hole (not-present entry)
1386 * @mapping: mapping
1387 * @index: index
1388 * @max_scan: maximum range to search
1389 *
1390 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1391 * the first hole.
1392 *
1393 * Returns: the index of the hole if found, otherwise returns an index
1394 * outside of the set specified (in which case 'index - return >=
1395 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1396 * will be returned.
1397 *
1398 * page_cache_prev_hole may be called under rcu_read_lock. However,
1399 * like radix_tree_gang_lookup, this will not atomically search a
1400 * snapshot of the tree at a single point in time. For example, if a
1401 * hole is created at index 10, then subsequently a hole is created at
1402 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1403 * called under rcu_read_lock.
1404 */
1405pgoff_t page_cache_prev_hole(struct address_space *mapping,
1406 pgoff_t index, unsigned long max_scan)
1407{
1408 unsigned long i;
1409
1410 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001411 struct page *page;
1412
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001413 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001414 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001415 break;
1416 index--;
1417 if (index == ULONG_MAX)
1418 break;
1419 }
1420
1421 return index;
1422}
1423EXPORT_SYMBOL(page_cache_prev_hole);
1424
1425/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001426 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001427 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001428 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001429 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001430 * Looks up the page cache slot at @mapping & @offset. If there is a
1431 * page cache page, it is returned with an increased refcount.
1432 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001433 * If the slot holds a shadow entry of a previously evicted page, or a
1434 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001435 *
1436 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001438struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Nick Piggina60637c2008-07-25 19:45:31 -07001440 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001441 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Nick Piggina60637c2008-07-25 19:45:31 -07001443 rcu_read_lock();
1444repeat:
1445 page = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001446 pagep = radix_tree_lookup_slot(&mapping->i_pages, offset);
Nick Piggina60637c2008-07-25 19:45:31 -07001447 if (pagep) {
1448 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001449 if (unlikely(!page))
1450 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001451 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001452 if (radix_tree_deref_retry(page))
1453 goto repeat;
1454 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001455 * A shadow entry of a recently evicted page,
1456 * or a swap entry from shmem/tmpfs. Return
1457 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001458 */
1459 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001460 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001461
1462 head = compound_head(page);
1463 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001464 goto repeat;
1465
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001466 /* The page was split under us? */
1467 if (compound_head(page) != head) {
1468 put_page(head);
1469 goto repeat;
1470 }
1471
Nick Piggina60637c2008-07-25 19:45:31 -07001472 /*
1473 * Has the page moved?
1474 * This is part of the lockless pagecache protocol. See
1475 * include/linux/pagemap.h for details.
1476 */
1477 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001478 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001479 goto repeat;
1480 }
1481 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001482out:
Nick Piggina60637c2008-07-25 19:45:31 -07001483 rcu_read_unlock();
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return page;
1486}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001487EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Randy Dunlap485bb992006-06-23 02:03:49 -07001489/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001490 * find_lock_entry - locate, pin and lock a page cache entry
1491 * @mapping: the address_space to search
1492 * @offset: the page cache index
1493 *
1494 * Looks up the page cache slot at @mapping & @offset. If there is a
1495 * page cache page, it is returned locked and with an increased
1496 * refcount.
1497 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001498 * If the slot holds a shadow entry of a previously evicted page, or a
1499 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001500 *
1501 * Otherwise, %NULL is returned.
1502 *
1503 * find_lock_entry() may sleep.
1504 */
1505struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct page *page;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001510 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001511 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001512 lock_page(page);
1513 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001514 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001515 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001516 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001517 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001519 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return page;
1522}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001523EXPORT_SYMBOL(find_lock_entry);
1524
1525/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001526 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001527 * @mapping: the address_space to search
1528 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001529 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001530 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001531 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001532 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001533 *
Randy Dunlap75325182014-07-30 16:08:37 -07001534 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001535 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001536 * @fgp_flags can be:
1537 *
1538 * - FGP_ACCESSED: the page will be marked accessed
1539 * - FGP_LOCK: Page is return locked
1540 * - FGP_CREAT: If page is not present then a new page is allocated using
1541 * @gfp_mask and added to the page cache and the VM's LRU
1542 * list. The page is returned locked and with an increased
1543 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001544 *
1545 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1546 * if the GFP flags specified for FGP_CREAT are atomic.
1547 *
1548 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001549 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001550struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001551 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Nick Piggineb2be182007-10-16 01:24:57 -07001553 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001556 page = find_get_entry(mapping, offset);
1557 if (radix_tree_exceptional_entry(page))
1558 page = NULL;
1559 if (!page)
1560 goto no_page;
1561
1562 if (fgp_flags & FGP_LOCK) {
1563 if (fgp_flags & FGP_NOWAIT) {
1564 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001565 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001566 return NULL;
1567 }
1568 } else {
1569 lock_page(page);
1570 }
1571
1572 /* Has the page been truncated? */
1573 if (unlikely(page->mapping != mapping)) {
1574 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001575 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001576 goto repeat;
1577 }
1578 VM_BUG_ON_PAGE(page->index != offset, page);
1579 }
1580
1581 if (page && (fgp_flags & FGP_ACCESSED))
1582 mark_page_accessed(page);
1583
1584no_page:
1585 if (!page && (fgp_flags & FGP_CREAT)) {
1586 int err;
1587 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001588 gfp_mask |= __GFP_WRITE;
1589 if (fgp_flags & FGP_NOFS)
1590 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001591
Michal Hocko45f87de2014-12-29 20:30:35 +01001592 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001593 if (!page)
1594 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001595
1596 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1597 fgp_flags |= FGP_LOCK;
1598
Hugh Dickinseb39d612014-08-06 16:06:43 -07001599 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001600 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001601 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001602
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001603 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001604 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001605 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001606 page = NULL;
1607 if (err == -EEXIST)
1608 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return page;
1613}
Mel Gorman2457aec2014-06-04 16:10:31 -07001614EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001617 * find_get_entries - gang pagecache lookup
1618 * @mapping: The address_space to search
1619 * @start: The starting page cache index
1620 * @nr_entries: The maximum number of entries
1621 * @entries: Where the resulting entries are placed
1622 * @indices: The cache indices corresponding to the entries in @entries
1623 *
1624 * find_get_entries() will search for and return a group of up to
1625 * @nr_entries entries in the mapping. The entries are placed at
1626 * @entries. find_get_entries() takes a reference against any actual
1627 * pages it returns.
1628 *
1629 * The search returns a group of mapping-contiguous page cache entries
1630 * with ascending indexes. There may be holes in the indices due to
1631 * not-present pages.
1632 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001633 * Any shadow entries of evicted pages, or swap entries from
1634 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001635 *
1636 * find_get_entries() returns the number of pages and shadow entries
1637 * which were found.
1638 */
1639unsigned find_get_entries(struct address_space *mapping,
1640 pgoff_t start, unsigned int nr_entries,
1641 struct page **entries, pgoff_t *indices)
1642{
1643 void **slot;
1644 unsigned int ret = 0;
1645 struct radix_tree_iter iter;
1646
1647 if (!nr_entries)
1648 return 0;
1649
1650 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001651 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001652 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001653repeat:
1654 page = radix_tree_deref_slot(slot);
1655 if (unlikely(!page))
1656 continue;
1657 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001658 if (radix_tree_deref_retry(page)) {
1659 slot = radix_tree_iter_retry(&iter);
1660 continue;
1661 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001662 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001663 * A shadow entry of a recently evicted page, a swap
1664 * entry from shmem/tmpfs or a DAX entry. Return it
1665 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001666 */
1667 goto export;
1668 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001669
1670 head = compound_head(page);
1671 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001672 goto repeat;
1673
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001674 /* The page was split under us? */
1675 if (compound_head(page) != head) {
1676 put_page(head);
1677 goto repeat;
1678 }
1679
Johannes Weiner0cd61442014-04-03 14:47:46 -07001680 /* Has the page moved? */
1681 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001682 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001683 goto repeat;
1684 }
1685export:
1686 indices[ret] = iter.index;
1687 entries[ret] = page;
1688 if (++ret == nr_entries)
1689 break;
1690 }
1691 rcu_read_unlock();
1692 return ret;
1693}
1694
1695/**
Jan Karab947cee2017-09-06 16:21:21 -07001696 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 * @mapping: The address_space to search
1698 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001699 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 * @nr_pages: The maximum number of pages
1701 * @pages: Where the resulting pages are placed
1702 *
Jan Karab947cee2017-09-06 16:21:21 -07001703 * find_get_pages_range() will search for and return a group of up to @nr_pages
1704 * pages in the mapping starting at index @start and up to index @end
1705 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1706 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 *
1708 * The search returns a group of mapping-contiguous pages with ascending
1709 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001710 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *
Jan Karab947cee2017-09-06 16:21:21 -07001712 * find_get_pages_range() returns the number of pages which were found. If this
1713 * number is smaller than @nr_pages, the end of specified range has been
1714 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
Jan Karab947cee2017-09-06 16:21:21 -07001716unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1717 pgoff_t end, unsigned int nr_pages,
1718 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001720 struct radix_tree_iter iter;
1721 void **slot;
1722 unsigned ret = 0;
1723
1724 if (unlikely(!nr_pages))
1725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Nick Piggina60637c2008-07-25 19:45:31 -07001727 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001728 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001729 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001730
1731 if (iter.index > end)
1732 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001733repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001734 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001735 if (unlikely(!page))
1736 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001737
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001738 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001739 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001740 slot = radix_tree_iter_retry(&iter);
1741 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001742 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001743 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001744 * A shadow entry of a recently evicted page,
1745 * or a swap entry from shmem/tmpfs. Skip
1746 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001747 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001748 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001749 }
Nick Piggina60637c2008-07-25 19:45:31 -07001750
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001751 head = compound_head(page);
1752 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001753 goto repeat;
1754
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001755 /* The page was split under us? */
1756 if (compound_head(page) != head) {
1757 put_page(head);
1758 goto repeat;
1759 }
1760
Nick Piggina60637c2008-07-25 19:45:31 -07001761 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001762 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001763 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001764 goto repeat;
1765 }
1766
1767 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001768 if (++ret == nr_pages) {
1769 *start = pages[ret - 1]->index + 1;
1770 goto out;
1771 }
Nick Piggina60637c2008-07-25 19:45:31 -07001772 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001773
Jan Karab947cee2017-09-06 16:21:21 -07001774 /*
1775 * We come here when there is no page beyond @end. We take care to not
1776 * overflow the index @start as it confuses some of the callers. This
1777 * breaks the iteration when there is page at index -1 but that is
1778 * already broken anyway.
1779 */
1780 if (end == (pgoff_t)-1)
1781 *start = (pgoff_t)-1;
1782 else
1783 *start = end + 1;
1784out:
Nick Piggina60637c2008-07-25 19:45:31 -07001785 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return ret;
1788}
1789
Jens Axboeebf43502006-04-27 08:46:01 +02001790/**
1791 * find_get_pages_contig - gang contiguous pagecache lookup
1792 * @mapping: The address_space to search
1793 * @index: The starting page index
1794 * @nr_pages: The maximum number of pages
1795 * @pages: Where the resulting pages are placed
1796 *
1797 * find_get_pages_contig() works exactly like find_get_pages(), except
1798 * that the returned number of pages are guaranteed to be contiguous.
1799 *
1800 * find_get_pages_contig() returns the number of pages which were found.
1801 */
1802unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1803 unsigned int nr_pages, struct page **pages)
1804{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001805 struct radix_tree_iter iter;
1806 void **slot;
1807 unsigned int ret = 0;
1808
1809 if (unlikely(!nr_pages))
1810 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001811
Nick Piggina60637c2008-07-25 19:45:31 -07001812 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001813 radix_tree_for_each_contig(slot, &mapping->i_pages, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001814 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001815repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001816 page = radix_tree_deref_slot(slot);
1817 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001818 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001819 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001820
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001821 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001822 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001823 slot = radix_tree_iter_retry(&iter);
1824 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001825 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001826 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001827 * A shadow entry of a recently evicted page,
1828 * or a swap entry from shmem/tmpfs. Stop
1829 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001830 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001831 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001832 }
Nick Piggina60637c2008-07-25 19:45:31 -07001833
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001834 head = compound_head(page);
1835 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001836 goto repeat;
1837
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001838 /* The page was split under us? */
1839 if (compound_head(page) != head) {
1840 put_page(head);
1841 goto repeat;
1842 }
1843
Nick Piggina60637c2008-07-25 19:45:31 -07001844 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001845 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001846 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001847 goto repeat;
1848 }
1849
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001850 /*
1851 * must check mapping and index after taking the ref.
1852 * otherwise we can get both false positives and false
1853 * negatives, which is just confusing to the caller.
1854 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001855 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001856 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001857 break;
1858 }
1859
Nick Piggina60637c2008-07-25 19:45:31 -07001860 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001861 if (++ret == nr_pages)
1862 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001863 }
Nick Piggina60637c2008-07-25 19:45:31 -07001864 rcu_read_unlock();
1865 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001866}
David Howellsef71c152007-05-09 02:33:44 -07001867EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001868
Randy Dunlap485bb992006-06-23 02:03:49 -07001869/**
Jan Kara72b045a2017-11-15 17:34:33 -08001870 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001871 * @mapping: the address_space to search
1872 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001873 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001874 * @tag: the tag index
1875 * @nr_pages: the maximum number of pages
1876 * @pages: where the resulting pages are placed
1877 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001879 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
Jan Kara72b045a2017-11-15 17:34:33 -08001881unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1882 pgoff_t end, int tag, unsigned int nr_pages,
1883 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001885 struct radix_tree_iter iter;
1886 void **slot;
1887 unsigned ret = 0;
1888
1889 if (unlikely(!nr_pages))
1890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Nick Piggina60637c2008-07-25 19:45:31 -07001892 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001893 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001894 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001895
1896 if (iter.index > end)
1897 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001898repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001899 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001900 if (unlikely(!page))
1901 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001902
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001903 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001904 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001905 slot = radix_tree_iter_retry(&iter);
1906 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001907 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001908 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001909 * A shadow entry of a recently evicted page.
1910 *
1911 * Those entries should never be tagged, but
1912 * this tree walk is lockless and the tags are
1913 * looked up in bulk, one radix tree node at a
1914 * time, so there is a sizable window for page
1915 * reclaim to evict a page we saw tagged.
1916 *
1917 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001918 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001919 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001920 }
Nick Piggina60637c2008-07-25 19:45:31 -07001921
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001922 head = compound_head(page);
1923 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001924 goto repeat;
1925
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001926 /* The page was split under us? */
1927 if (compound_head(page) != head) {
1928 put_page(head);
1929 goto repeat;
1930 }
1931
Nick Piggina60637c2008-07-25 19:45:31 -07001932 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001933 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001934 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001935 goto repeat;
1936 }
1937
1938 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001939 if (++ret == nr_pages) {
1940 *index = pages[ret - 1]->index + 1;
1941 goto out;
1942 }
Nick Piggina60637c2008-07-25 19:45:31 -07001943 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001944
Jan Kara72b045a2017-11-15 17:34:33 -08001945 /*
1946 * We come here when we got at @end. We take care to not overflow the
1947 * index @index as it confuses some of the callers. This breaks the
1948 * iteration when there is page at index -1 but that is already broken
1949 * anyway.
1950 */
1951 if (end == (pgoff_t)-1)
1952 *index = (pgoff_t)-1;
1953 else
1954 *index = end + 1;
1955out:
Nick Piggina60637c2008-07-25 19:45:31 -07001956 rcu_read_unlock();
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return ret;
1959}
Jan Kara72b045a2017-11-15 17:34:33 -08001960EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001962/**
1963 * find_get_entries_tag - find and return entries that match @tag
1964 * @mapping: the address_space to search
1965 * @start: the starting page cache index
1966 * @tag: the tag index
1967 * @nr_entries: the maximum number of entries
1968 * @entries: where the resulting entries are placed
1969 * @indices: the cache indices corresponding to the entries in @entries
1970 *
1971 * Like find_get_entries, except we only return entries which are tagged with
1972 * @tag.
1973 */
1974unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1975 int tag, unsigned int nr_entries,
1976 struct page **entries, pgoff_t *indices)
1977{
1978 void **slot;
1979 unsigned int ret = 0;
1980 struct radix_tree_iter iter;
1981
1982 if (!nr_entries)
1983 return 0;
1984
1985 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001986 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001987 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001988repeat:
1989 page = radix_tree_deref_slot(slot);
1990 if (unlikely(!page))
1991 continue;
1992 if (radix_tree_exception(page)) {
1993 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001994 slot = radix_tree_iter_retry(&iter);
1995 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001996 }
1997
1998 /*
1999 * A shadow entry of a recently evicted page, a swap
2000 * entry from shmem/tmpfs or a DAX entry. Return it
2001 * without attempting to raise page count.
2002 */
2003 goto export;
2004 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002005
2006 head = compound_head(page);
2007 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002008 goto repeat;
2009
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002010 /* The page was split under us? */
2011 if (compound_head(page) != head) {
2012 put_page(head);
2013 goto repeat;
2014 }
2015
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002016 /* Has the page moved? */
2017 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002018 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002019 goto repeat;
2020 }
2021export:
2022 indices[ret] = iter.index;
2023 entries[ret] = page;
2024 if (++ret == nr_entries)
2025 break;
2026 }
2027 rcu_read_unlock();
2028 return ret;
2029}
2030EXPORT_SYMBOL(find_get_entries_tag);
2031
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002032/*
2033 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2034 * a _large_ part of the i/o request. Imagine the worst scenario:
2035 *
2036 * ---R__________________________________________B__________
2037 * ^ reading here ^ bad block(assume 4k)
2038 *
2039 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2040 * => failing the whole request => read(R) => read(R+1) =>
2041 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2042 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2043 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2044 *
2045 * It is going insane. Fix it by quickly scaling down the readahead size.
2046 */
2047static void shrink_readahead_size_eio(struct file *filp,
2048 struct file_ra_state *ra)
2049{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002050 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002051}
2052
Randy Dunlap485bb992006-06-23 02:03:49 -07002053/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002054 * generic_file_buffered_read - generic file read routine
2055 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002056 * @iter: data destination
2057 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002060 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 *
2062 * This is really ugly. But the goto's actually try to clarify some
2063 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002065static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002066 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002068 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002069 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002071 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002072 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002073 pgoff_t index;
2074 pgoff_t last_index;
2075 pgoff_t prev_index;
2076 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002077 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002078 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Wei Fangc2a97372016-10-07 17:01:52 -07002080 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002081 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002082 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2083
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002084 index = *ppos >> PAGE_SHIFT;
2085 prev_index = ra->prev_pos >> PAGE_SHIFT;
2086 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2087 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2088 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 for (;;) {
2091 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002092 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002093 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 unsigned long nr, ret;
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002098 if (fatal_signal_pending(current)) {
2099 error = -EINTR;
2100 goto out;
2101 }
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002104 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002105 if (iocb->ki_flags & IOCB_NOWAIT)
2106 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002107 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002108 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002109 index, last_index - index);
2110 page = find_get_page(mapping, index);
2111 if (unlikely(page == NULL))
2112 goto no_cached_page;
2113 }
2114 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002115 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002116 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002117 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002119 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002120 if (iocb->ki_flags & IOCB_NOWAIT) {
2121 put_page(page);
2122 goto would_block;
2123 }
2124
Mel Gormanebded022016-03-15 14:55:39 -07002125 /*
2126 * See comment in do_read_cache_page on why
2127 * wait_on_page_locked is used to avoid unnecessarily
2128 * serialisations and why it's safe.
2129 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002130 error = wait_on_page_locked_killable(page);
2131 if (unlikely(error))
2132 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002133 if (PageUptodate(page))
2134 goto page_ok;
2135
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002137 !mapping->a_ops->is_partially_uptodate)
2138 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002139 /* pipes can't handle partially uptodate pages */
2140 if (unlikely(iter->type & ITER_PIPE))
2141 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002142 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002143 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002144 /* Did it get truncated before we got the lock? */
2145 if (!page->mapping)
2146 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002147 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002148 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002149 goto page_not_up_to_date_locked;
2150 unlock_page(page);
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002153 /*
2154 * i_size must be checked after we know the page is Uptodate.
2155 *
2156 * Checking i_size after the check allows us to calculate
2157 * the correct value for "nr", which means the zero-filled
2158 * part of the page is not copied back to userspace (unless
2159 * another truncate extends the file - this is desired though).
2160 */
2161
2162 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002163 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002164 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002165 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002166 goto out;
2167 }
2168
2169 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002170 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002171 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002172 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002173 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002174 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002175 goto out;
2176 }
2177 }
2178 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 /* If users can be writing to this page using arbitrary
2181 * virtual addresses, take care about potential aliasing
2182 * before reading the page on the kernel side.
2183 */
2184 if (mapping_writably_mapped(mapping))
2185 flush_dcache_page(page);
2186
2187 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002188 * When a sequential read accesses a page several times,
2189 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 */
Jan Karaec0f1632007-05-06 14:49:25 -07002191 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 mark_page_accessed(page);
2193 prev_index = index;
2194
2195 /*
2196 * Ok, we have the page, and it's up-to-date, so
2197 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 */
Al Viro6e58e792014-02-03 17:07:03 -05002199
2200 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002202 index += offset >> PAGE_SHIFT;
2203 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002204 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002206 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002207 written += ret;
2208 if (!iov_iter_count(iter))
2209 goto out;
2210 if (ret < nr) {
2211 error = -EFAULT;
2212 goto out;
2213 }
2214 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216page_not_up_to_date:
2217 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002218 error = lock_page_killable(page);
2219 if (unlikely(error))
2220 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002222page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002223 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (!page->mapping) {
2225 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002226 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 continue;
2228 }
2229
2230 /* Did somebody else fill it already? */
2231 if (PageUptodate(page)) {
2232 unlock_page(page);
2233 goto page_ok;
2234 }
2235
2236readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002237 /*
2238 * A previous I/O error may have been due to temporary
2239 * failures, eg. multipath errors.
2240 * PG_error will be set again if readpage fails.
2241 */
2242 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /* Start the actual read. The read will unlock the page. */
2244 error = mapping->a_ops->readpage(filp, page);
2245
Zach Brown994fc28c2005-12-15 14:28:17 -08002246 if (unlikely(error)) {
2247 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002248 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002249 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002250 goto find_page;
2251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002256 error = lock_page_killable(page);
2257 if (unlikely(error))
2258 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (!PageUptodate(page)) {
2260 if (page->mapping == NULL) {
2261 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002262 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
2264 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002265 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto find_page;
2267 }
2268 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002269 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002270 error = -EIO;
2271 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273 unlock_page(page);
2274 }
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto page_ok;
2277
2278readpage_error:
2279 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002280 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 goto out;
2282
2283no_cached_page:
2284 /*
2285 * Ok, it wasn't cached, so we need to create a new
2286 * page..
2287 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002288 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002289 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002290 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002293 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002294 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002296 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002297 if (error == -EEXIST) {
2298 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto out;
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto readpage;
2304 }
2305
Milosz Tanski3239d832017-08-29 16:13:19 +02002306would_block:
2307 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002309 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002310 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002311 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002313 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002314 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002315 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Randy Dunlap485bb992006-06-23 02:03:49 -07002318/**
Al Viro6abd2322014-04-04 14:20:57 -04002319 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002320 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002321 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002322 *
Al Viro6abd2322014-04-04 14:20:57 -04002323 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 * that can use the page cache directly.
2325 */
2326ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002327generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002329 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002330 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002331
2332 if (!count)
2333 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Al Viro2ba48ce2015-04-09 13:52:01 -04002335 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002336 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002337 struct address_space *mapping = file->f_mapping;
2338 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002339 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002342 if (iocb->ki_flags & IOCB_NOWAIT) {
2343 if (filemap_range_has_page(mapping, iocb->ki_pos,
2344 iocb->ki_pos + count - 1))
2345 return -EAGAIN;
2346 } else {
2347 retval = filemap_write_and_wait_range(mapping,
2348 iocb->ki_pos,
2349 iocb->ki_pos + count - 1);
2350 if (retval < 0)
2351 goto out;
2352 }
Al Viroed978a82014-03-05 22:53:04 -05002353
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002354 file_accessed(file);
2355
Al Viro5ecda132017-04-13 14:13:36 -04002356 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002357 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002358 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002359 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002360 }
Al Viro5b47d592017-05-08 13:54:47 -04002361 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002362
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002363 /*
2364 * Btrfs can have a short DIO read if we encounter
2365 * compressed extents, so if there was an error, or if
2366 * we've already read everything we wanted to, or if
2367 * there was a short read because we hit EOF, go ahead
2368 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002369 * the rest of the read. Buffered reads will not work for
2370 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002371 */
Al Viro5ecda132017-04-13 14:13:36 -04002372 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002373 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002377 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378out:
2379 return retval;
2380}
Al Viroed978a82014-03-05 22:53:04 -05002381EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002384/**
2385 * page_cache_read - adds requested page to the page cache if not already there
2386 * @file: file to read
2387 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002388 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * This adds the requested page to the page cache if it isn't already there,
2391 * and schedules an I/O to read in its contents from disk.
2392 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002393static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002396 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Zach Brown994fc28c2005-12-15 14:28:17 -08002399 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002400 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002401 if (!page)
2402 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002404 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002405 if (ret == 0)
2406 ret = mapping->a_ops->readpage(file, page);
2407 else if (ret == -EEXIST)
2408 ret = 0; /* losing race to add is OK */
2409
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002410 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Zach Brown994fc28c2005-12-15 14:28:17 -08002412 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002413
Zach Brown994fc28c2005-12-15 14:28:17 -08002414 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417#define MMAP_LOTSAMISS (100)
2418
Linus Torvaldsef00e082009-06-16 15:31:25 -07002419/*
2420 * Synchronous readahead happens when we don't even find
2421 * a page in the page cache at all.
2422 */
2423static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2424 struct file_ra_state *ra,
2425 struct file *file,
2426 pgoff_t offset)
2427{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002428 struct address_space *mapping = file->f_mapping;
2429
2430 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002431 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002432 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002433 if (!ra->ra_pages)
2434 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002435
Joe Perches64363aa2013-07-08 16:00:18 -07002436 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002437 page_cache_sync_readahead(mapping, ra, file, offset,
2438 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002439 return;
2440 }
2441
Andi Kleen207d04b2011-05-24 17:12:29 -07002442 /* Avoid banging the cache line if not needed */
2443 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002444 ra->mmap_miss++;
2445
2446 /*
2447 * Do we miss much more than hit in this file? If so,
2448 * stop bothering with read-ahead. It will only hurt.
2449 */
2450 if (ra->mmap_miss > MMAP_LOTSAMISS)
2451 return;
2452
Wu Fengguangd30a1102009-06-16 15:31:30 -07002453 /*
2454 * mmap read-around
2455 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002456 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2457 ra->size = ra->ra_pages;
2458 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002459 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002460}
2461
2462/*
2463 * Asynchronous readahead happens when we find the page and PG_readahead,
2464 * so we want to possibly extend the readahead further..
2465 */
2466static void do_async_mmap_readahead(struct vm_area_struct *vma,
2467 struct file_ra_state *ra,
2468 struct file *file,
2469 struct page *page,
2470 pgoff_t offset)
2471{
2472 struct address_space *mapping = file->f_mapping;
2473
2474 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002475 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002476 return;
2477 if (ra->mmap_miss > 0)
2478 ra->mmap_miss--;
2479 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002480 page_cache_async_readahead(mapping, ra, file,
2481 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002482}
2483
Randy Dunlap485bb992006-06-23 02:03:49 -07002484/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002485 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002486 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002487 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002488 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 * mapped memory region to read in file data during a page fault.
2490 *
2491 * The goto's are kind of ugly, but this streamlines the normal case of having
2492 * it in the page cache, and handles the special cases reasonably without
2493 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002494 *
2495 * vma->vm_mm->mmap_sem must be held on entry.
2496 *
2497 * If our return value has VM_FAULT_RETRY set, it's because
2498 * lock_page_or_retry() returned 0.
2499 * The mmap_sem has usually been released in this case.
2500 * See __lock_page_or_retry() for the exception.
2501 *
2502 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2503 * has not been released.
2504 *
2505 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002507vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
2509 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002510 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct address_space *mapping = file->f_mapping;
2512 struct file_ra_state *ra = &file->f_ra;
2513 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002514 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002515 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002517 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002519 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2520 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002521 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002524 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002526 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002527 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002529 * We found the page, so try async readahead before
2530 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 */
Dave Jiang11bac802017-02-24 14:56:41 -08002532 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002533 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002534 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002535 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002536 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002537 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002538 ret = VM_FAULT_MAJOR;
2539retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002540 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (!page)
2542 goto no_cached_page;
2543 }
2544
Dave Jiang11bac802017-02-24 14:56:41 -08002545 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002546 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002547 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002548 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002549
2550 /* Did it get truncated? */
2551 if (unlikely(page->mapping != mapping)) {
2552 unlock_page(page);
2553 put_page(page);
2554 goto retry_find;
2555 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002556 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002559 * We have a locked page in the page cache, now we need to check
2560 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 */
Nick Piggind00806b2007-07-19 01:46:57 -07002562 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 goto page_not_uptodate;
2564
Linus Torvaldsef00e082009-06-16 15:31:25 -07002565 /*
2566 * Found the page and have a reference on it.
2567 * We must recheck i_size under page lock.
2568 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002569 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2570 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002571 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002572 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002573 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002574 }
2575
Nick Piggind0217ac2007-07-19 01:47:03 -07002576 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002577 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579no_cached_page:
2580 /*
2581 * We're only likely to ever get here if MADV_RANDOM is in
2582 * effect.
2583 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002584 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 /*
2587 * The page we want has now been added to the page cache.
2588 * In the unlikely event that someone removed it in the
2589 * meantime, we'll just come back here and read it again.
2590 */
2591 if (error >= 0)
2592 goto retry_find;
2593
2594 /*
2595 * An error return from page_cache_read can result if the
2596 * system is low on memory, or a problem occurs while trying
2597 * to schedule I/O.
2598 */
2599 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002600 return VM_FAULT_OOM;
2601 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /*
2605 * Umm, take care of errors if the page isn't up-to-date.
2606 * Try to re-read it _once_. We do this synchronously,
2607 * because there really aren't any performance issues here
2608 * and we need to check for errors.
2609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002611 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002612 if (!error) {
2613 wait_on_page_locked(page);
2614 if (!PageUptodate(page))
2615 error = -EIO;
2616 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002617 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002618
2619 if (!error || error == AOP_TRUNCATED_PAGE)
2620 goto retry_find;
2621
2622 /* Things didn't work out. Return zero to tell the mm layer so. */
2623 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002624 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002625}
2626EXPORT_SYMBOL(filemap_fault);
2627
Jan Kara82b0f8c2016-12-14 15:06:58 -08002628void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002629 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002630{
2631 struct radix_tree_iter iter;
2632 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002633 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002634 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002635 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002636 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002637 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002638
2639 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002640 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start_pgoff) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002641 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002642 break;
2643repeat:
2644 page = radix_tree_deref_slot(slot);
2645 if (unlikely(!page))
2646 goto next;
2647 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002648 if (radix_tree_deref_retry(page)) {
2649 slot = radix_tree_iter_retry(&iter);
2650 continue;
2651 }
2652 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002653 }
2654
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002655 head = compound_head(page);
2656 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002657 goto repeat;
2658
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002659 /* The page was split under us? */
2660 if (compound_head(page) != head) {
2661 put_page(head);
2662 goto repeat;
2663 }
2664
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002665 /* Has the page moved? */
2666 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002667 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002668 goto repeat;
2669 }
2670
2671 if (!PageUptodate(page) ||
2672 PageReadahead(page) ||
2673 PageHWPoison(page))
2674 goto skip;
2675 if (!trylock_page(page))
2676 goto skip;
2677
2678 if (page->mapping != mapping || !PageUptodate(page))
2679 goto unlock;
2680
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002681 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2682 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002683 goto unlock;
2684
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002685 if (file->f_ra.mmap_miss > 0)
2686 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002687
Jan Kara82b0f8c2016-12-14 15:06:58 -08002688 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2689 if (vmf->pte)
2690 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002691 last_pgoff = iter.index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002692 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002693 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002694 unlock_page(page);
2695 goto next;
2696unlock:
2697 unlock_page(page);
2698skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002699 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002700next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002701 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002702 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002703 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002704 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002705 break;
2706 }
2707 rcu_read_unlock();
2708}
2709EXPORT_SYMBOL(filemap_map_pages);
2710
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002711vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002712{
2713 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002714 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002715 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002716
Jan Kara14da9202012-06-12 16:20:37 +02002717 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002718 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002719 lock_page(page);
2720 if (page->mapping != inode->i_mapping) {
2721 unlock_page(page);
2722 ret = VM_FAULT_NOPAGE;
2723 goto out;
2724 }
Jan Kara14da9202012-06-12 16:20:37 +02002725 /*
2726 * We mark the page dirty already here so that when freeze is in
2727 * progress, we are guaranteed that writeback during freezing will
2728 * see the dirty page and writeprotect it again.
2729 */
2730 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002731 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002732out:
Jan Kara14da9202012-06-12 16:20:37 +02002733 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002734 return ret;
2735}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002736
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002737const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002738 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002739 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002740 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741};
2742
2743/* This is used for a general mmap of a disk file */
2744
2745int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2746{
2747 struct address_space *mapping = file->f_mapping;
2748
2749 if (!mapping->a_ops->readpage)
2750 return -ENOEXEC;
2751 file_accessed(file);
2752 vma->vm_ops = &generic_file_vm_ops;
2753 return 0;
2754}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756/*
2757 * This is for filesystems which do not implement ->writepage.
2758 */
2759int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2760{
2761 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2762 return -EINVAL;
2763 return generic_file_mmap(file, vma);
2764}
2765#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002766vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann45397222018-04-13 15:35:27 -07002767{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002768 return VM_FAULT_SIGBUS;
Arnd Bergmann45397222018-04-13 15:35:27 -07002769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2771{
2772 return -ENOSYS;
2773}
2774int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2775{
2776 return -ENOSYS;
2777}
2778#endif /* CONFIG_MMU */
2779
Arnd Bergmann45397222018-04-13 15:35:27 -07002780EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781EXPORT_SYMBOL(generic_file_mmap);
2782EXPORT_SYMBOL(generic_file_readonly_mmap);
2783
Sasha Levin67f9fd92014-04-03 14:48:18 -07002784static struct page *wait_on_page_read(struct page *page)
2785{
2786 if (!IS_ERR(page)) {
2787 wait_on_page_locked(page);
2788 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002789 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002790 page = ERR_PTR(-EIO);
2791 }
2792 }
2793 return page;
2794}
2795
Mel Gorman32b63522016-03-15 14:55:36 -07002796static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002797 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002798 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002799 void *data,
2800 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Nick Piggineb2be182007-10-16 01:24:57 -07002802 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 int err;
2804repeat:
2805 page = find_get_page(mapping, index);
2806 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002807 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002808 if (!page)
2809 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002810 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002811 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002812 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002813 if (err == -EEXIST)
2814 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return ERR_PTR(err);
2817 }
Mel Gorman32b63522016-03-15 14:55:36 -07002818
2819filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 err = filler(data, page);
2821 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002822 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002823 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
Mel Gorman32b63522016-03-15 14:55:36 -07002825
2826 page = wait_on_page_read(page);
2827 if (IS_ERR(page))
2828 return page;
2829 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (PageUptodate(page))
2832 goto out;
2833
Mel Gormanebded022016-03-15 14:55:39 -07002834 /*
2835 * Page is not up to date and may be locked due one of the following
2836 * case a: Page is being filled and the page lock is held
2837 * case b: Read/write error clearing the page uptodate status
2838 * case c: Truncation in progress (page locked)
2839 * case d: Reclaim in progress
2840 *
2841 * Case a, the page will be up to date when the page is unlocked.
2842 * There is no need to serialise on the page lock here as the page
2843 * is pinned so the lock gives no additional protection. Even if the
2844 * the page is truncated, the data is still valid if PageUptodate as
2845 * it's a race vs truncate race.
2846 * Case b, the page will not be up to date
2847 * Case c, the page may be truncated but in itself, the data may still
2848 * be valid after IO completes as it's a read vs truncate race. The
2849 * operation must restart if the page is not uptodate on unlock but
2850 * otherwise serialising on page lock to stabilise the mapping gives
2851 * no additional guarantees to the caller as the page lock is
2852 * released before return.
2853 * Case d, similar to truncation. If reclaim holds the page lock, it
2854 * will be a race with remove_mapping that determines if the mapping
2855 * is valid on unlock but otherwise the data is valid and there is
2856 * no need to serialise with page lock.
2857 *
2858 * As the page lock gives no additional guarantee, we optimistically
2859 * wait on the page to be unlocked and check if it's up to date and
2860 * use the page if it is. Otherwise, the page lock is required to
2861 * distinguish between the different cases. The motivation is that we
2862 * avoid spurious serialisations and wakeups when multiple processes
2863 * wait on the same page for IO to complete.
2864 */
2865 wait_on_page_locked(page);
2866 if (PageUptodate(page))
2867 goto out;
2868
2869 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002871
2872 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (!page->mapping) {
2874 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002875 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002876 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
Mel Gormanebded022016-03-15 14:55:39 -07002878
2879 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (PageUptodate(page)) {
2881 unlock_page(page);
2882 goto out;
2883 }
Mel Gorman32b63522016-03-15 14:55:36 -07002884 goto filler;
2885
David Howellsc855ff32007-05-09 13:42:20 +01002886out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002887 mark_page_accessed(page);
2888 return page;
2889}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002890
2891/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002892 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002893 * @mapping: the page's address_space
2894 * @index: the page index
2895 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002896 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002897 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002898 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002899 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002900 *
2901 * If the page does not get brought uptodate, return -EIO.
2902 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002903struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002904 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002905 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002906 void *data)
2907{
2908 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2909}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002910EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002911
2912/**
2913 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2914 * @mapping: the page's address_space
2915 * @index: the page index
2916 * @gfp: the page allocator flags to use if allocating
2917 *
2918 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002919 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002920 *
2921 * If the page does not get brought uptodate, return -EIO.
2922 */
2923struct page *read_cache_page_gfp(struct address_space *mapping,
2924 pgoff_t index,
2925 gfp_t gfp)
2926{
2927 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2928
Sasha Levin67f9fd92014-04-03 14:48:18 -07002929 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002930}
2931EXPORT_SYMBOL(read_cache_page_gfp);
2932
Nick Piggin2f718ff2007-10-16 01:24:59 -07002933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * Performs necessary checks before doing a write
2935 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002936 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 * Returns appropriate error code that caller should return or
2938 * zero in case that write should be allowed.
2939 */
Al Viro3309dd02015-04-09 12:55:47 -04002940inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Al Viro3309dd02015-04-09 12:55:47 -04002942 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002944 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002945 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Al Viro3309dd02015-04-09 12:55:47 -04002947 if (!iov_iter_count(from))
2948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Al Viro0fa6b002015-04-04 04:05:48 -04002950 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002951 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002952 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Al Viro3309dd02015-04-09 12:55:47 -04002954 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002956 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2957 return -EINVAL;
2958
Al Viro0fa6b002015-04-04 04:05:48 -04002959 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002960 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002961 send_sig(SIGXFSZ, current, 0);
2962 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
Al Viro3309dd02015-04-09 12:55:47 -04002964 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
2966
2967 /*
2968 * LFS rule
2969 */
Al Viro3309dd02015-04-09 12:55:47 -04002970 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002972 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002974 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976
2977 /*
2978 * Are we about to exceed the fs block limit ?
2979 *
2980 * If we have written data it becomes a short write. If we have
2981 * exceeded without writing data we send a signal and return EFBIG.
2982 * Linus frestrict idea will clean these up nicely..
2983 */
Al Viro3309dd02015-04-09 12:55:47 -04002984 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2985 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Al Viro3309dd02015-04-09 12:55:47 -04002987 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2988 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990EXPORT_SYMBOL(generic_write_checks);
2991
Nick Pigginafddba42007-10-16 01:25:01 -07002992int pagecache_write_begin(struct file *file, struct address_space *mapping,
2993 loff_t pos, unsigned len, unsigned flags,
2994 struct page **pagep, void **fsdata)
2995{
2996 const struct address_space_operations *aops = mapping->a_ops;
2997
Nick Piggin4e02ed42008-10-29 14:00:55 -07002998 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002999 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003000}
3001EXPORT_SYMBOL(pagecache_write_begin);
3002
3003int pagecache_write_end(struct file *file, struct address_space *mapping,
3004 loff_t pos, unsigned len, unsigned copied,
3005 struct page *page, void *fsdata)
3006{
3007 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003008
Nick Piggin4e02ed42008-10-29 14:00:55 -07003009 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003010}
3011EXPORT_SYMBOL(pagecache_write_end);
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003014generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 struct file *file = iocb->ki_filp;
3017 struct address_space *mapping = file->f_mapping;
3018 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003019 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003021 size_t write_len;
3022 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Al Viro0c949332014-03-22 06:51:37 -04003024 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003025 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003026
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003027 if (iocb->ki_flags & IOCB_NOWAIT) {
3028 /* If there are pages to writeback, return */
3029 if (filemap_range_has_page(inode->i_mapping, pos,
3030 pos + iov_iter_count(from)))
3031 return -EAGAIN;
3032 } else {
3033 written = filemap_write_and_wait_range(mapping, pos,
3034 pos + write_len - 1);
3035 if (written)
3036 goto out;
3037 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003038
3039 /*
3040 * After a write we want buffered reads to be sure to go to disk to get
3041 * the new data. We invalidate clean cached page from the region we're
3042 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003043 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003044 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003045 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003046 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003047 /*
3048 * If a page can not be invalidated, return 0 to fall back
3049 * to buffered write.
3050 */
3051 if (written) {
3052 if (written == -EBUSY)
3053 return 0;
3054 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003055 }
3056
Al Viro639a93a52017-04-13 14:10:15 -04003057 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003058
3059 /*
3060 * Finally, try again to invalidate clean pages which might have been
3061 * cached by non-direct readahead, or faulted in by get_user_pages()
3062 * if the source of the write was an mmap'ed region of the file
3063 * we're writing. Either one is a pretty crazy thing to do,
3064 * so we don't support it 100%. If this invalidation
3065 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003066 *
3067 * Most of the time we do not need this since dio_complete() will do
3068 * the invalidation for us. However there are some file systems that
3069 * do not end up with dio_complete() being called, so let's not break
3070 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003071 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003072 if (mapping->nrpages)
3073 invalidate_inode_pages2_range(mapping,
3074 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003077 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003078 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003079 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3080 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 mark_inode_dirty(inode);
3082 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003083 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
Al Viro639a93a52017-04-13 14:10:15 -04003085 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003086out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return written;
3088}
3089EXPORT_SYMBOL(generic_file_direct_write);
3090
Nick Piggineb2be182007-10-16 01:24:57 -07003091/*
3092 * Find or create a page at the given pagecache position. Return the locked
3093 * page. This function is specifically for buffered writes.
3094 */
Nick Piggin54566b22009-01-04 12:00:53 -08003095struct page *grab_cache_page_write_begin(struct address_space *mapping,
3096 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003097{
Nick Piggineb2be182007-10-16 01:24:57 -07003098 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003099 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003100
Nick Piggin54566b22009-01-04 12:00:53 -08003101 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003102 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003103
Mel Gorman2457aec2014-06-04 16:10:31 -07003104 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003105 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003106 if (page)
3107 wait_for_stable_page(page);
3108
Nick Piggineb2be182007-10-16 01:24:57 -07003109 return page;
3110}
Nick Piggin54566b22009-01-04 12:00:53 -08003111EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003112
Al Viro3b93f912014-02-11 21:34:08 -05003113ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003114 struct iov_iter *i, loff_t pos)
3115{
3116 struct address_space *mapping = file->f_mapping;
3117 const struct address_space_operations *a_ops = mapping->a_ops;
3118 long status = 0;
3119 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003120 unsigned int flags = 0;
3121
Nick Pigginafddba42007-10-16 01:25:01 -07003122 do {
3123 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003124 unsigned long offset; /* Offset into pagecache page */
3125 unsigned long bytes; /* Bytes to write to page */
3126 size_t copied; /* Bytes copied from user */
3127 void *fsdata;
3128
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003129 offset = (pos & (PAGE_SIZE - 1));
3130 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003131 iov_iter_count(i));
3132
3133again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003134 /*
3135 * Bring in the user page that we will copy from _first_.
3136 * Otherwise there's a nasty deadlock on copying from the
3137 * same page as we're writing to, without it being marked
3138 * up-to-date.
3139 *
3140 * Not only is this an optimisation, but it is also required
3141 * to check that the address is actually valid, when atomic
3142 * usercopies are used, below.
3143 */
3144 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3145 status = -EFAULT;
3146 break;
3147 }
3148
Jan Kara296291c2015-10-22 13:32:21 -07003149 if (fatal_signal_pending(current)) {
3150 status = -EINTR;
3151 break;
3152 }
3153
Nick Piggin674b8922007-10-16 01:25:03 -07003154 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003155 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003156 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003157 break;
3158
anfei zhou931e80e2010-02-02 13:44:02 -08003159 if (mapping_writably_mapped(mapping))
3160 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003161
Nick Pigginafddba42007-10-16 01:25:01 -07003162 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003163 flush_dcache_page(page);
3164
3165 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3166 page, fsdata);
3167 if (unlikely(status < 0))
3168 break;
3169 copied = status;
3170
3171 cond_resched();
3172
Nick Piggin124d3b72008-02-02 15:01:17 +01003173 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003174 if (unlikely(copied == 0)) {
3175 /*
3176 * If we were unable to copy any data at all, we must
3177 * fall back to a single segment length write.
3178 *
3179 * If we didn't fallback here, we could livelock
3180 * because not all segments in the iov can be copied at
3181 * once without a pagefault.
3182 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003183 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003184 iov_iter_single_seg_count(i));
3185 goto again;
3186 }
Nick Pigginafddba42007-10-16 01:25:01 -07003187 pos += copied;
3188 written += copied;
3189
3190 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003191 } while (iov_iter_count(i));
3192
3193 return written ? written : status;
3194}
Al Viro3b93f912014-02-11 21:34:08 -05003195EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Jan Karae4dd9de2009-08-17 18:10:06 +02003197/**
Al Viro81742022014-04-03 03:17:43 -04003198 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003199 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003200 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003201 *
3202 * This function does all the work needed for actually writing data to a
3203 * file. It does all basic checks, removes SUID from the file, updates
3204 * modification times and calls proper subroutines depending on whether we
3205 * do direct IO or a standard buffered write.
3206 *
3207 * It expects i_mutex to be grabbed unless we work on a block device or similar
3208 * object which does not need locking at all.
3209 *
3210 * This function does *not* take care of syncing data in case of O_SYNC write.
3211 * A caller has to handle it. This is mainly due to the fact that we want to
3212 * avoid syncing under i_mutex.
3213 */
Al Viro81742022014-04-03 03:17:43 -04003214ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003217 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003219 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003221 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003224 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003225 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (err)
3227 goto out;
3228
Josef Bacikc3b2da32012-03-26 09:59:21 -04003229 err = file_update_time(file);
3230 if (err)
3231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Al Viro2ba48ce2015-04-09 13:52:01 -04003233 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003234 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003235
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003236 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003237 /*
3238 * If the write stopped short of completing, fall back to
3239 * buffered writes. Some filesystems do this for writes to
3240 * holes, for example. For DAX files, a buffered write will
3241 * not succeed (even if it did, DAX does not handle dirty
3242 * page-cache pages correctly).
3243 */
Al Viro0b8def92015-04-07 10:22:53 -04003244 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003246
Al Viro0b8def92015-04-07 10:22:53 -04003247 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003248 /*
Al Viro3b93f912014-02-11 21:34:08 -05003249 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003250 * then we want to return the number of bytes which were
3251 * direct-written, or the error code if that was zero. Note
3252 * that this differs from normal direct-io semantics, which
3253 * will return -EFOO even if some bytes were written.
3254 */
Al Viro60bb4522014-08-08 12:39:16 -04003255 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003256 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003257 goto out;
3258 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003259 /*
3260 * We need to ensure that the page cache pages are written to
3261 * disk and invalidated to preserve the expected O_DIRECT
3262 * semantics.
3263 */
Al Viro3b93f912014-02-11 21:34:08 -05003264 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003265 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003266 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003267 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003268 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003269 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003270 pos >> PAGE_SHIFT,
3271 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003272 } else {
3273 /*
3274 * We don't know how much we wrote, so just return
3275 * the number of bytes which were direct-written
3276 */
3277 }
3278 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003279 written = generic_perform_write(file, from, iocb->ki_pos);
3280 if (likely(written > 0))
3281 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283out:
3284 current->backing_dev_info = NULL;
3285 return written ? written : err;
3286}
Al Viro81742022014-04-03 03:17:43 -04003287EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Jan Karae4dd9de2009-08-17 18:10:06 +02003289/**
Al Viro81742022014-04-03 03:17:43 -04003290 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003291 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003292 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003293 *
Al Viro81742022014-04-03 03:17:43 -04003294 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003295 * filesystems. It takes care of syncing the file in case of O_SYNC file
3296 * and acquires i_mutex as needed.
3297 */
Al Viro81742022014-04-03 03:17:43 -04003298ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003301 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Al Viro59551022016-01-22 15:40:57 -05003304 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003305 ret = generic_write_checks(iocb, from);
3306 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003307 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003308 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Christoph Hellwige2592212016-04-07 08:52:01 -07003310 if (ret > 0)
3311 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 return ret;
3313}
Al Viro81742022014-04-03 03:17:43 -04003314EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
David Howellscf9a2ae2006-08-29 19:05:54 +01003316/**
3317 * try_to_release_page() - release old fs-specific metadata on a page
3318 *
3319 * @page: the page which the kernel is trying to free
3320 * @gfp_mask: memory allocation flags (and I/O mode)
3321 *
3322 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003323 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003324 * Otherwise return zero.
3325 *
David Howells266cf652009-04-03 16:42:36 +01003326 * This may also be called if PG_fscache is set on a page, indicating that the
3327 * page is known to the local caching routines.
3328 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003329 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003330 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003331 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003332 */
3333int try_to_release_page(struct page *page, gfp_t gfp_mask)
3334{
3335 struct address_space * const mapping = page->mapping;
3336
3337 BUG_ON(!PageLocked(page));
3338 if (PageWriteback(page))
3339 return 0;
3340
3341 if (mapping && mapping->a_ops->releasepage)
3342 return mapping->a_ops->releasepage(page, gfp_mask);
3343 return try_to_free_buffers(page);
3344}
3345
3346EXPORT_SYMBOL(try_to_release_page);