blob: 843429e2065a4a5c7b6252e3d37c7f446972f063 [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>
Nick Piggin0f8053a2006-03-22 00:08:33 -080039#include "internal.h"
40
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070041#define CREATE_TRACE_POINTS
42#include <trace/events/filemap.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * FIXME: remove all knowledge of the buffer layer from the core VM
46 */
Jan Kara148f9482009-08-17 19:52:36 +020047#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/mman.h>
50
Vinayak Menon3aee78b2016-05-20 16:58:41 -070051int want_old_faultaround_pte = 1;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
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 */
Vinayak Menonf1571342015-07-13 13:24:55 +0530197 if (PageUptodate(page) && PageMappedToDisk(page)) {
198 count_vm_event(PGPGOUTCLEAN);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600199 cleancache_put_page(page);
Vinayak Menonf1571342015-07-13 13:24:55 +0530200 } else {
Dan Magenheimer31677602011-09-21 11:56:28 -0400201 cleancache_invalidate_page(mapping, page);
Vinayak Menonf1571342015-07-13 13:24:55 +0530202 }
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600203
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700204 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800205 VM_BUG_ON_PAGE(page_mapped(page), page);
206 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
207 int mapcount;
208
209 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
210 current->comm, page_to_pfn(page));
211 dump_page(page, "still mapped when deleted");
212 dump_stack();
213 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
214
215 mapcount = page_mapcount(page);
216 if (mapping_exiting(mapping) &&
217 page_count(page) >= mapcount + 2) {
218 /*
219 * All vmas have already been torn down, so it's
220 * a good bet that actually the page is unmapped,
221 * and we'd prefer not to leak it: if we're wrong,
222 * some other bad page check should catch it later.
223 */
224 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700225 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800226 }
227 }
228
Jan Kara76253fb2017-11-15 17:37:22 -0800229 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800230 if (PageHuge(page))
231 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800232
Jan Kara5ecc4d82017-11-15 17:37:29 -0800233 nr = hpage_nr_pages(page);
234
235 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
236 if (PageSwapBacked(page)) {
237 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
238 if (PageTransHuge(page))
239 __dec_node_page_state(page, NR_SHMEM_THPS);
240 } else {
241 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800242 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800243
244 /*
245 * At this point page must be either written or cleaned by
246 * truncate. Dirty page here signals a bug and loss of
247 * unwritten data.
248 *
249 * This fixes dirty accounting after removing the page entirely
250 * but leaves PageDirty set: it has no effect for truncated
251 * page and anyway will be cleared before returning page into
252 * buddy allocator.
253 */
254 if (WARN_ON_ONCE(PageDirty(page)))
255 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
256}
257
258/*
259 * Delete a page from the page cache and free it. Caller has to make
260 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700261 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800262 */
263void __delete_from_page_cache(struct page *page, void *shadow)
264{
265 struct address_space *mapping = page->mapping;
266
267 trace_mm_filemap_delete_from_page_cache(page);
268
269 unaccount_page_cache_page(mapping, page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700270 page_cache_tree_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Jan Kara59c66c52017-11-15 17:37:18 -0800273static void page_cache_free_page(struct address_space *mapping,
274 struct page *page)
275{
276 void (*freepage)(struct page *);
277
278 freepage = mapping->a_ops->freepage;
279 if (freepage)
280 freepage(page);
281
282 if (PageTransHuge(page) && !PageHuge(page)) {
283 page_ref_sub(page, HPAGE_PMD_NR);
284 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
285 } else {
286 put_page(page);
287 }
288}
289
Minchan Kim702cfbf2011-03-22 16:32:43 -0700290/**
291 * delete_from_page_cache - delete page from page cache
292 * @page: the page which the kernel is trying to remove from page cache
293 *
294 * This must be called only on pages that have been verified to be in the page
295 * cache and locked. It will never put the page into the free list, the caller
296 * has a reference on the page.
297 */
298void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700300 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400301 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Matt Mackallcd7619d2005-05-01 08:59:01 -0700303 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700304 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700305 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700306 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500307
Jan Kara59c66c52017-11-15 17:37:18 -0800308 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700309}
310EXPORT_SYMBOL(delete_from_page_cache);
311
Jan Karaaa65c292017-11-15 17:37:33 -0800312/*
313 * page_cache_tree_delete_batch - delete several pages from page cache
314 * @mapping: the mapping to which pages belong
315 * @pvec: pagevec with pages to delete
316 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700317 * The function walks over mapping->i_pages and removes pages passed in @pvec
318 * from the mapping. The function expects @pvec to be sorted by page index.
319 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800320 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700321 * @pvec and takes care to delete all corresponding tail pages from the
322 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800323 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700324 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800325 */
326static void
327page_cache_tree_delete_batch(struct address_space *mapping,
328 struct pagevec *pvec)
329{
330 struct radix_tree_iter iter;
331 void **slot;
332 int total_pages = 0;
333 int i = 0, tail_pages = 0;
334 struct page *page;
335 pgoff_t start;
336
337 start = pvec->pages[0]->index;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700338 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Jan Karaaa65c292017-11-15 17:37:33 -0800339 if (i >= pagevec_count(pvec) && !tail_pages)
340 break;
341 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700342 &mapping->i_pages.xa_lock);
Jan Karaaa65c292017-11-15 17:37:33 -0800343 if (radix_tree_exceptional_entry(page))
344 continue;
345 if (!tail_pages) {
346 /*
347 * Some page got inserted in our range? Skip it. We
348 * have our pages locked so they are protected from
349 * being removed.
350 */
351 if (page != pvec->pages[i])
352 continue;
353 WARN_ON_ONCE(!PageLocked(page));
354 if (PageTransHuge(page) && !PageHuge(page))
355 tail_pages = HPAGE_PMD_NR - 1;
356 page->mapping = NULL;
357 /*
358 * Leave page->index set: truncation lookup relies
359 * upon it
360 */
361 i++;
362 } else {
363 tail_pages--;
364 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700365 radix_tree_clear_tags(&mapping->i_pages, iter.node, slot);
366 __radix_tree_replace(&mapping->i_pages, iter.node, slot, NULL,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800367 workingset_lookup_update(mapping));
Jan Karaaa65c292017-11-15 17:37:33 -0800368 total_pages++;
369 }
370 mapping->nrpages -= total_pages;
371}
372
373void delete_from_page_cache_batch(struct address_space *mapping,
374 struct pagevec *pvec)
375{
376 int i;
377 unsigned long flags;
378
379 if (!pagevec_count(pvec))
380 return;
381
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700382 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800383 for (i = 0; i < pagevec_count(pvec); i++) {
384 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
385
386 unaccount_page_cache_page(mapping, pvec->pages[i]);
387 }
388 page_cache_tree_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700389 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800390
391 for (i = 0; i < pagevec_count(pvec); i++)
392 page_cache_free_page(mapping, pvec->pages[i]);
393}
394
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200395int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700396{
397 int ret = 0;
398 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700399 if (test_bit(AS_ENOSPC, &mapping->flags) &&
400 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700401 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700402 if (test_bit(AS_EIO, &mapping->flags) &&
403 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700404 ret = -EIO;
405 return ret;
406}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200407EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700408
Jeff Layton76341ca2017-07-06 07:02:22 -0400409static int filemap_check_and_keep_errors(struct address_space *mapping)
410{
411 /* Check for outstanding write errors */
412 if (test_bit(AS_EIO, &mapping->flags))
413 return -EIO;
414 if (test_bit(AS_ENOSPC, &mapping->flags))
415 return -ENOSPC;
416 return 0;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700420 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700421 * @mapping: address space structure to write
422 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800423 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700424 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700426 * Start writeback against all of a mapping's dirty pages that lie
427 * within the byte offsets <start, end> inclusive.
428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700430 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * these two operations is that if a dirty page/buffer is encountered, it must
432 * be waited upon, and not just skipped over.
433 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800434int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
435 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 int ret;
438 struct writeback_control wbc = {
439 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800440 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700441 .range_start = start,
442 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 };
444
445 if (!mapping_cap_writeback_dirty(mapping))
446 return 0;
447
Tejun Heob16b1de2015-06-02 08:39:48 -0600448 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600450 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ret;
452}
453
454static inline int __filemap_fdatawrite(struct address_space *mapping,
455 int sync_mode)
456{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700457 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460int filemap_fdatawrite(struct address_space *mapping)
461{
462 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
463}
464EXPORT_SYMBOL(filemap_fdatawrite);
465
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400466int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800467 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
470}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400471EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Randy Dunlap485bb992006-06-23 02:03:49 -0700473/**
474 * filemap_flush - mostly a non-blocking flush
475 * @mapping: target address_space
476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * This is a mostly non-blocking flush. Not suitable for data-integrity
478 * purposes - I/O may not be started against all dirty pages.
479 */
480int filemap_flush(struct address_space *mapping)
481{
482 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
483}
484EXPORT_SYMBOL(filemap_flush);
485
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500486/**
487 * filemap_range_has_page - check if a page exists in range.
488 * @mapping: address space within which to check
489 * @start_byte: offset in bytes where the range starts
490 * @end_byte: offset in bytes where the range ends (inclusive)
491 *
492 * Find at least one page in the range supplied, usually used to check if
493 * direct writing in this range will trigger a writeback.
494 */
495bool filemap_range_has_page(struct address_space *mapping,
496 loff_t start_byte, loff_t end_byte)
497{
498 pgoff_t index = start_byte >> PAGE_SHIFT;
499 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700500 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500501
502 if (end_byte < start_byte)
503 return false;
504
505 if (mapping->nrpages == 0)
506 return false;
507
Jan Karaf7b68042017-09-06 16:21:40 -0700508 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500509 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700510 put_page(page);
511 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500512}
513EXPORT_SYMBOL(filemap_range_has_page);
514
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400515static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800516 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 pgoff_t index = start_byte >> PAGE_SHIFT;
519 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct pagevec pvec;
521 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200523 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Mel Gorman86679822017-11-15 17:37:52 -0800526 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800527 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unsigned i;
529
Jan Kara312e9d22017-11-15 17:35:05 -0800530 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800531 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800532 if (!nr_pages)
533 break;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 for (i = 0; i < nr_pages; i++) {
536 struct page *page = pvec.pages[i];
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400539 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 pagevec_release(&pvec);
542 cond_resched();
543 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800544}
545
546/**
547 * filemap_fdatawait_range - wait for writeback to complete
548 * @mapping: address space structure to wait for
549 * @start_byte: offset in bytes where the range starts
550 * @end_byte: offset in bytes where the range ends (inclusive)
551 *
552 * Walk the list of under-writeback pages of the given address space
553 * in the given range and wait for all of them. Check error status of
554 * the address space and return it.
555 *
556 * Since the error status of the address space is cleared by this function,
557 * callers are responsible for checking the return value and handling and/or
558 * reporting the error.
559 */
560int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
561 loff_t end_byte)
562{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400563 __filemap_fdatawait_range(mapping, start_byte, end_byte);
564 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200566EXPORT_SYMBOL(filemap_fdatawait_range);
567
568/**
Jeff Laytona823e452017-07-28 07:24:43 -0400569 * file_fdatawait_range - wait for writeback to complete
570 * @file: file pointing to address space structure to wait for
571 * @start_byte: offset in bytes where the range starts
572 * @end_byte: offset in bytes where the range ends (inclusive)
573 *
574 * Walk the list of under-writeback pages of the address space that file
575 * refers to, in the given range and wait for all of them. Check error
576 * status of the address space vs. the file->f_wb_err cursor and return it.
577 *
578 * Since the error status of the file is advanced by this function,
579 * callers are responsible for checking the return value and handling and/or
580 * reporting the error.
581 */
582int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
583{
584 struct address_space *mapping = file->f_mapping;
585
586 __filemap_fdatawait_range(mapping, start_byte, end_byte);
587 return file_check_and_advance_wb_err(file);
588}
589EXPORT_SYMBOL(file_fdatawait_range);
590
591/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800592 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
593 * @mapping: address space structure to wait for
594 *
595 * Walk the list of under-writeback pages of the given address space
596 * and wait for all of them. Unlike filemap_fdatawait(), this function
597 * does not clear error status of the address space.
598 *
599 * Use this function if callers don't handle errors themselves. Expected
600 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
601 * fsfreeze(8)
602 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400603int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800604{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400605 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400606 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800607}
Jeff Layton76341ca2017-07-06 07:02:22 -0400608EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800609
Jeff Layton9326c9b2017-07-26 10:21:11 -0400610static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400612 return (!dax_mapping(mapping) && mapping->nrpages) ||
613 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616int filemap_write_and_wait(struct address_space *mapping)
617{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800618 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Jeff Layton9326c9b2017-07-26 10:21:11 -0400620 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800621 err = filemap_fdatawrite(mapping);
622 /*
623 * Even if the above returned error, the pages may be
624 * written partially (e.g. -ENOSPC), so we wait for it.
625 * But the -EIO is special case, it may indicate the worst
626 * thing (e.g. bug) happened, so we avoid waiting for it.
627 */
628 if (err != -EIO) {
629 int err2 = filemap_fdatawait(mapping);
630 if (!err)
631 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400632 } else {
633 /* Clear any previously stored errors */
634 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800635 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700636 } else {
637 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800641EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Randy Dunlap485bb992006-06-23 02:03:49 -0700643/**
644 * filemap_write_and_wait_range - write out & wait on a file range
645 * @mapping: the address_space for the pages
646 * @lstart: offset in bytes where the range starts
647 * @lend: offset in bytes where the range ends (inclusive)
648 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800649 * Write out and wait upon file offsets lstart->lend, inclusive.
650 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300651 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800652 * that this function can be used to write to the very end-of-file (end = -1).
653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654int filemap_write_and_wait_range(struct address_space *mapping,
655 loff_t lstart, loff_t lend)
656{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800657 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Jeff Layton9326c9b2017-07-26 10:21:11 -0400659 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800660 err = __filemap_fdatawrite_range(mapping, lstart, lend,
661 WB_SYNC_ALL);
662 /* See comment of filemap_write_and_wait() */
663 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200664 int err2 = filemap_fdatawait_range(mapping,
665 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800666 if (!err)
667 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400668 } else {
669 /* Clear any previously stored errors */
670 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800671 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700672 } else {
673 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
Chris Masonf6995582009-04-15 13:22:37 -0400677EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jeff Layton5660e132017-07-06 07:02:25 -0400679void __filemap_set_wb_err(struct address_space *mapping, int err)
680{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400681 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400682
683 trace_filemap_set_wb_err(mapping, eseq);
684}
685EXPORT_SYMBOL(__filemap_set_wb_err);
686
687/**
688 * file_check_and_advance_wb_err - report wb error (if any) that was previously
689 * and advance wb_err to current one
690 * @file: struct file on which the error is being reported
691 *
692 * When userland calls fsync (or something like nfsd does the equivalent), we
693 * want to report any writeback errors that occurred since the last fsync (or
694 * since the file was opened if there haven't been any).
695 *
696 * Grab the wb_err from the mapping. If it matches what we have in the file,
697 * then just quickly return 0. The file is all caught up.
698 *
699 * If it doesn't match, then take the mapping value, set the "seen" flag in
700 * it and try to swap it into place. If it works, or another task beat us
701 * to it with the new value, then update the f_wb_err and return the error
702 * portion. The error at this point must be reported via proper channels
703 * (a'la fsync, or NFS COMMIT operation, etc.).
704 *
705 * While we handle mapping->wb_err with atomic operations, the f_wb_err
706 * value is protected by the f_lock since we must ensure that it reflects
707 * the latest value swapped in for this file descriptor.
708 */
709int file_check_and_advance_wb_err(struct file *file)
710{
711 int err = 0;
712 errseq_t old = READ_ONCE(file->f_wb_err);
713 struct address_space *mapping = file->f_mapping;
714
715 /* Locklessly handle the common case where nothing has changed */
716 if (errseq_check(&mapping->wb_err, old)) {
717 /* Something changed, must use slow path */
718 spin_lock(&file->f_lock);
719 old = file->f_wb_err;
720 err = errseq_check_and_advance(&mapping->wb_err,
721 &file->f_wb_err);
722 trace_file_check_and_advance_wb_err(file, old);
723 spin_unlock(&file->f_lock);
724 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700725
726 /*
727 * We're mostly using this function as a drop in replacement for
728 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
729 * that the legacy code would have had on these flags.
730 */
731 clear_bit(AS_EIO, &mapping->flags);
732 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400733 return err;
734}
735EXPORT_SYMBOL(file_check_and_advance_wb_err);
736
737/**
738 * file_write_and_wait_range - write out & wait on a file range
739 * @file: file pointing to address_space with pages
740 * @lstart: offset in bytes where the range starts
741 * @lend: offset in bytes where the range ends (inclusive)
742 *
743 * Write out and wait upon file offsets lstart->lend, inclusive.
744 *
745 * Note that @lend is inclusive (describes the last byte to be written) so
746 * that this function can be used to write to the very end-of-file (end = -1).
747 *
748 * After writing out and waiting on the data, we check and advance the
749 * f_wb_err cursor to the latest value, and return any errors detected there.
750 */
751int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
752{
753 int err = 0, err2;
754 struct address_space *mapping = file->f_mapping;
755
Jeff Layton9326c9b2017-07-26 10:21:11 -0400756 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400757 err = __filemap_fdatawrite_range(mapping, lstart, lend,
758 WB_SYNC_ALL);
759 /* See comment of filemap_write_and_wait() */
760 if (err != -EIO)
761 __filemap_fdatawait_range(mapping, lstart, lend);
762 }
763 err2 = file_check_and_advance_wb_err(file);
764 if (!err)
765 err = err2;
766 return err;
767}
768EXPORT_SYMBOL(file_write_and_wait_range);
769
Randy Dunlap485bb992006-06-23 02:03:49 -0700770/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700771 * replace_page_cache_page - replace a pagecache page with a new one
772 * @old: page to be replaced
773 * @new: page to replace with
774 * @gfp_mask: allocation mode
775 *
776 * This function replaces a page in the pagecache with a new one. On
777 * success it acquires the pagecache reference for the new page and
778 * drops it for the old page. Both the old and new pages must be
779 * locked. This function does not add the new page to the LRU, the
780 * caller must do that.
781 *
782 * The remove + add is atomic. The only way this function can fail is
783 * memory allocation failure.
784 */
785int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
786{
787 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700788
Sasha Levin309381fea2014-01-23 15:52:54 -0800789 VM_BUG_ON_PAGE(!PageLocked(old), old);
790 VM_BUG_ON_PAGE(!PageLocked(new), new);
791 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700792
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700793 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700794 if (!error) {
795 struct address_space *mapping = old->mapping;
796 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400797 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700798
799 pgoff_t offset = old->index;
800 freepage = mapping->a_ops->freepage;
801
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300802 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700803 new->mapping = mapping;
804 new->index = offset;
805
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700806 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700807 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700808 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700809 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700810
811 /*
812 * hugetlb pages do not participate in page cache accounting.
813 */
814 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700815 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700816 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700817 __inc_node_page_state(new, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700818 xa_unlock_irqrestore(&mapping->i_pages, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700819 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700820 radix_tree_preload_end();
821 if (freepage)
822 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300823 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700824 }
825
826 return error;
827}
828EXPORT_SYMBOL_GPL(replace_page_cache_page);
829
Johannes Weinera5289102014-04-03 14:47:51 -0700830static int __add_to_page_cache_locked(struct page *page,
831 struct address_space *mapping,
832 pgoff_t offset, gfp_t gfp_mask,
833 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Johannes Weiner00501b52014-08-08 14:19:20 -0700835 int huge = PageHuge(page);
836 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700837 int error;
838
Sasha Levin309381fea2014-01-23 15:52:54 -0800839 VM_BUG_ON_PAGE(!PageLocked(page), page);
840 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700841
Johannes Weiner00501b52014-08-08 14:19:20 -0700842 if (!huge) {
843 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800844 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700845 if (error)
846 return error;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700849 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700850 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700851 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800852 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700853 return error;
854 }
855
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300856 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700857 page->mapping = mapping;
858 page->index = offset;
859
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700860 xa_lock_irq(&mapping->i_pages);
Johannes Weinera5289102014-04-03 14:47:51 -0700861 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700862 radix_tree_preload_end();
863 if (unlikely(error))
864 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700865
866 /* hugetlb pages do not participate in page cache accounting. */
867 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700868 __inc_node_page_state(page, NR_FILE_PAGES);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700869 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700870 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800871 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700872 trace_mm_filemap_add_to_page_cache(page);
873 return 0;
874err_insert:
875 page->mapping = NULL;
876 /* Leave page->index set: truncation relies upon it */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700877 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700878 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800879 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300880 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return error;
882}
Johannes Weinera5289102014-04-03 14:47:51 -0700883
884/**
885 * add_to_page_cache_locked - add a locked page to the pagecache
886 * @page: page to add
887 * @mapping: the page's address_space
888 * @offset: page index
889 * @gfp_mask: page allocation mode
890 *
891 * This function is used to add a page to the pagecache. It must be locked.
892 * This function does not add the page to the LRU. The caller must do that.
893 */
894int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
895 pgoff_t offset, gfp_t gfp_mask)
896{
897 return __add_to_page_cache_locked(page, mapping, offset,
898 gfp_mask, NULL);
899}
Nick Piggine2867812008-07-25 19:45:30 -0700900EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400903 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Johannes Weinera5289102014-04-03 14:47:51 -0700905 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700906 int ret;
907
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800908 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700909 ret = __add_to_page_cache_locked(page, mapping, offset,
910 gfp_mask, &shadow);
911 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800912 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700913 else {
914 /*
915 * The page might have been evicted from cache only
916 * recently, in which case it should be activated like
917 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700918 * The exception is pages getting rewritten; evicting other
919 * data from the working set, only to cache data that will
920 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700921 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700922 if (!(gfp_mask & __GFP_WRITE) &&
923 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700924 SetPageActive(page);
925 workingset_activation(page);
926 } else
927 ClearPageActive(page);
928 lru_cache_add(page);
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return ret;
931}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300932EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Paul Jackson44110fe2006-03-24 03:16:04 -0800934#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700935struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800936{
Miao Xiec0ff7452010-05-24 14:32:08 -0700937 int n;
938 struct page *page;
939
Paul Jackson44110fe2006-03-24 03:16:04 -0800940 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700941 unsigned int cpuset_mems_cookie;
942 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700943 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700944 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700945 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700946 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700947
Miao Xiec0ff7452010-05-24 14:32:08 -0700948 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800949 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700950 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800951}
Nick Piggin2ae88142006-10-28 10:38:23 -0700952EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800953#endif
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/*
956 * In order to wait for pages to become available there must be
957 * waitqueues associated with pages. By using a hash table of
958 * waitqueues where the bucket discipline is to maintain all
959 * waiters on the same queue and wake all when any of the pages
960 * become available, and for the woken contexts to check to be
961 * sure the appropriate page became available, this saves space
962 * at a cost of "thundering herd" phenomena during rare hash
963 * collisions.
964 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000965#define PAGE_WAIT_TABLE_BITS 8
966#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
967static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
968
969static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Nicholas Piggin62906022016-12-25 13:00:30 +1000971 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Nicholas Piggin62906022016-12-25 13:00:30 +1000973
974void __init pagecache_init(void)
975{
976 int i;
977
978 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
979 init_waitqueue_head(&page_wait_table[i]);
980
981 page_writeback_init();
982}
983
Linus Torvalds3510ca22017-08-27 13:55:12 -0700984/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000985struct wait_page_key {
986 struct page *page;
987 int bit_nr;
988 int page_match;
989};
990
991struct wait_page_queue {
992 struct page *page;
993 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200994 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000995};
996
Ingo Molnarac6424b2017-06-20 12:06:13 +0200997static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000998{
999 struct wait_page_key *key = arg;
1000 struct wait_page_queue *wait_page
1001 = container_of(wait, struct wait_page_queue, wait);
1002
1003 if (wait_page->page != key->page)
1004 return 0;
1005 key->page_match = 1;
1006
1007 if (wait_page->bit_nr != key->bit_nr)
1008 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001009
1010 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +10001011 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001012 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001013
1014 return autoremove_wake_function(wait, mode, sync, key);
1015}
1016
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001017static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001018{
1019 wait_queue_head_t *q = page_waitqueue(page);
1020 struct wait_page_key key;
1021 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001022 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001023
1024 key.page = page;
1025 key.bit_nr = bit_nr;
1026 key.page_match = 0;
1027
Tim Chen11a19c72017-08-25 09:13:55 -07001028 bookmark.flags = 0;
1029 bookmark.private = NULL;
1030 bookmark.func = NULL;
1031 INIT_LIST_HEAD(&bookmark.entry);
1032
Nicholas Piggin62906022016-12-25 13:00:30 +10001033 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001034 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1035
1036 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1037 /*
1038 * Take a breather from holding the lock,
1039 * allow pages that finish wake up asynchronously
1040 * to acquire the lock and remove themselves
1041 * from wait queue
1042 */
1043 spin_unlock_irqrestore(&q->lock, flags);
1044 cpu_relax();
1045 spin_lock_irqsave(&q->lock, flags);
1046 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1047 }
1048
Nicholas Piggin62906022016-12-25 13:00:30 +10001049 /*
1050 * It is possible for other pages to have collided on the waitqueue
1051 * hash, so in that case check for a page match. That prevents a long-
1052 * term waiter
1053 *
1054 * It is still possible to miss a case here, when we woke page waiters
1055 * and removed them from the waitqueue, but there are still other
1056 * page waiters.
1057 */
1058 if (!waitqueue_active(q) || !key.page_match) {
1059 ClearPageWaiters(page);
1060 /*
1061 * It's possible to miss clearing Waiters here, when we woke
1062 * our page waiters, but the hashed waitqueue has waiters for
1063 * other pages on it.
1064 *
1065 * That's okay, it's a rare case. The next waker will clear it.
1066 */
1067 }
1068 spin_unlock_irqrestore(&q->lock, flags);
1069}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001070
1071static void wake_up_page(struct page *page, int bit)
1072{
1073 if (!PageWaiters(page))
1074 return;
1075 wake_up_page_bit(page, bit);
1076}
Nicholas Piggin62906022016-12-25 13:00:30 +10001077
1078static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1079 struct page *page, int bit_nr, int state, bool lock)
1080{
1081 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001082 wait_queue_entry_t *wait = &wait_page.wait;
Nicholas Piggin62906022016-12-25 13:00:30 +10001083 int ret = 0;
1084
1085 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001086 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001087 wait->func = wake_page_function;
1088 wait_page.page = page;
1089 wait_page.bit_nr = bit_nr;
1090
1091 for (;;) {
1092 spin_lock_irq(&q->lock);
1093
Ingo Molnar2055da92017-06-20 12:06:46 +02001094 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001095 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001096 SetPageWaiters(page);
1097 }
1098
1099 set_current_state(state);
1100
1101 spin_unlock_irq(&q->lock);
1102
1103 if (likely(test_bit(bit_nr, &page->flags))) {
1104 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001105 }
1106
1107 if (lock) {
1108 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1109 break;
1110 } else {
1111 if (!test_bit(bit_nr, &page->flags))
1112 break;
1113 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001114
1115 if (unlikely(signal_pending_state(state, current))) {
1116 ret = -EINTR;
1117 break;
1118 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001119 }
1120
1121 finish_wait(q, wait);
1122
1123 /*
1124 * A signal could leave PageWaiters set. Clearing it here if
1125 * !waitqueue_active would be possible (by open-coding finish_wait),
1126 * but still fail to catch it in the case of wait hash collision. We
1127 * already can fail to clear wait hash collision cases, so don't
1128 * bother with signals either.
1129 */
1130
1131 return ret;
1132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Harvey Harrison920c7a52008-02-04 22:29:26 -08001134void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Nicholas Piggin62906022016-12-25 13:00:30 +10001136 wait_queue_head_t *q = page_waitqueue(page);
1137 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139EXPORT_SYMBOL(wait_on_page_bit);
1140
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001141int wait_on_page_bit_killable(struct page *page, int bit_nr)
1142{
Nicholas Piggin62906022016-12-25 13:00:30 +10001143 wait_queue_head_t *q = page_waitqueue(page);
1144 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001145}
David Howells4343d002017-11-02 15:27:52 +00001146EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001149 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001150 * @page: Page defining the wait queue of interest
1151 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001152 *
1153 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1154 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001155void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001156{
1157 wait_queue_head_t *q = page_waitqueue(page);
1158 unsigned long flags;
1159
1160 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001161 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001162 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001163 spin_unlock_irqrestore(&q->lock, flags);
1164}
1165EXPORT_SYMBOL_GPL(add_page_wait_queue);
1166
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001167#ifndef clear_bit_unlock_is_negative_byte
1168
1169/*
1170 * PG_waiters is the high bit in the same byte as PG_lock.
1171 *
1172 * On x86 (and on many other architectures), we can clear PG_lock and
1173 * test the sign bit at the same time. But if the architecture does
1174 * not support that special operation, we just do this all by hand
1175 * instead.
1176 *
1177 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1178 * being cleared, but a memory barrier should be unneccssary since it is
1179 * in the same byte as PG_locked.
1180 */
1181static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1182{
1183 clear_bit_unlock(nr, mem);
1184 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001185 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001186}
1187
1188#endif
1189
David Howells385e1ca5f2009-04-03 16:42:39 +01001190/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001191 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 * @page: the page
1193 *
1194 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1195 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001196 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1198 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001199 * Note that this depends on PG_waiters being the sign bit in the byte
1200 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1201 * clear the PG_locked bit and test PG_waiters at the same time fairly
1202 * portably (architectures that do LL/SC can test any bit, while x86 can
1203 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001205void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001207 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001208 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001209 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001210 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1211 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213EXPORT_SYMBOL(unlock_page);
1214
Randy Dunlap485bb992006-06-23 02:03:49 -07001215/**
1216 * end_page_writeback - end writeback against a page
1217 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
1219void end_page_writeback(struct page *page)
1220{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001221 /*
1222 * TestClearPageReclaim could be used here but it is an atomic
1223 * operation and overkill in this particular case. Failing to
1224 * shuffle a page marked for immediate reclaim is too mild to
1225 * justify taking an atomic operation penalty at the end of
1226 * ever page writeback.
1227 */
1228 if (PageReclaim(page)) {
1229 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001230 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001231 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001232
1233 if (!test_clear_page_writeback(page))
1234 BUG();
1235
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001236 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 wake_up_page(page, PG_writeback);
1238}
1239EXPORT_SYMBOL(end_page_writeback);
1240
Matthew Wilcox57d99842014-06-04 16:07:45 -07001241/*
1242 * After completing I/O on a page, call this routine to update the page
1243 * flags appropriately
1244 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001245void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001246{
Jens Axboec11f0c02016-08-05 08:11:04 -06001247 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001248 if (!err) {
1249 SetPageUptodate(page);
1250 } else {
1251 ClearPageUptodate(page);
1252 SetPageError(page);
1253 }
1254 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001255 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001256 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001257 struct address_space *mapping;
1258
Matthew Wilcox57d99842014-06-04 16:07:45 -07001259 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001260 mapping = page_mapping(page);
1261 if (mapping)
1262 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001263 }
1264 end_page_writeback(page);
1265 }
1266}
1267EXPORT_SYMBOL_GPL(page_endio);
1268
Randy Dunlap485bb992006-06-23 02:03:49 -07001269/**
1270 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001271 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001273void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Nicholas Piggin62906022016-12-25 13:00:30 +10001275 struct page *page = compound_head(__page);
1276 wait_queue_head_t *q = page_waitqueue(page);
1277 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279EXPORT_SYMBOL(__lock_page);
1280
Nicholas Piggin62906022016-12-25 13:00:30 +10001281int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001282{
Nicholas Piggin62906022016-12-25 13:00:30 +10001283 struct page *page = compound_head(__page);
1284 wait_queue_head_t *q = page_waitqueue(page);
1285 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001286}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001287EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001288
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001289/*
1290 * Return values:
1291 * 1 - page is locked; mmap_sem is still held.
1292 * 0 - page is not locked.
1293 * mmap_sem has been released (up_read()), unless flags had both
1294 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1295 * which case mmap_sem is still held.
1296 *
1297 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1298 * with the page locked and the mmap_sem unperturbed.
1299 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001300int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1301 unsigned int flags)
1302{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001303 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1304 /*
1305 * CAUTION! In this case, mmap_sem is not released
1306 * even though return 0.
1307 */
1308 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1309 return 0;
1310
1311 up_read(&mm->mmap_sem);
1312 if (flags & FAULT_FLAG_KILLABLE)
1313 wait_on_page_locked_killable(page);
1314 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001315 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001316 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001317 } else {
1318 if (flags & FAULT_FLAG_KILLABLE) {
1319 int ret;
1320
1321 ret = __lock_page_killable(page);
1322 if (ret) {
1323 up_read(&mm->mmap_sem);
1324 return 0;
1325 }
1326 } else
1327 __lock_page(page);
1328 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001329 }
1330}
1331
Randy Dunlap485bb992006-06-23 02:03:49 -07001332/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001333 * page_cache_next_hole - find the next hole (not-present entry)
1334 * @mapping: mapping
1335 * @index: index
1336 * @max_scan: maximum range to search
1337 *
1338 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1339 * lowest indexed hole.
1340 *
1341 * Returns: the index of the hole if found, otherwise returns an index
1342 * outside of the set specified (in which case 'return - index >=
1343 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1344 * be returned.
1345 *
1346 * page_cache_next_hole may be called under rcu_read_lock. However,
1347 * like radix_tree_gang_lookup, this will not atomically search a
1348 * snapshot of the tree at a single point in time. For example, if a
1349 * hole is created at index 5, then subsequently a hole is created at
1350 * index 10, page_cache_next_hole covering both indexes may return 10
1351 * if called under rcu_read_lock.
1352 */
1353pgoff_t page_cache_next_hole(struct address_space *mapping,
1354 pgoff_t index, unsigned long max_scan)
1355{
1356 unsigned long i;
1357
1358 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001359 struct page *page;
1360
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001361 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001362 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001363 break;
1364 index++;
1365 if (index == 0)
1366 break;
1367 }
1368
1369 return index;
1370}
1371EXPORT_SYMBOL(page_cache_next_hole);
1372
1373/**
1374 * page_cache_prev_hole - find the prev hole (not-present entry)
1375 * @mapping: mapping
1376 * @index: index
1377 * @max_scan: maximum range to search
1378 *
1379 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1380 * the first hole.
1381 *
1382 * Returns: the index of the hole if found, otherwise returns an index
1383 * outside of the set specified (in which case 'index - return >=
1384 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1385 * will be returned.
1386 *
1387 * page_cache_prev_hole may be called under rcu_read_lock. However,
1388 * like radix_tree_gang_lookup, this will not atomically search a
1389 * snapshot of the tree at a single point in time. For example, if a
1390 * hole is created at index 10, then subsequently a hole is created at
1391 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1392 * called under rcu_read_lock.
1393 */
1394pgoff_t page_cache_prev_hole(struct address_space *mapping,
1395 pgoff_t index, unsigned long max_scan)
1396{
1397 unsigned long i;
1398
1399 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001400 struct page *page;
1401
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001402 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001403 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001404 break;
1405 index--;
1406 if (index == ULONG_MAX)
1407 break;
1408 }
1409
1410 return index;
1411}
1412EXPORT_SYMBOL(page_cache_prev_hole);
1413
1414/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001415 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001416 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001417 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001418 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001419 * Looks up the page cache slot at @mapping & @offset. If there is a
1420 * page cache page, it is returned with an increased refcount.
1421 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001422 * If the slot holds a shadow entry of a previously evicted page, or a
1423 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001424 *
1425 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001427struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Nick Piggina60637c2008-07-25 19:45:31 -07001429 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001430 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Nick Piggina60637c2008-07-25 19:45:31 -07001432 rcu_read_lock();
1433repeat:
1434 page = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001435 pagep = radix_tree_lookup_slot(&mapping->i_pages, offset);
Nick Piggina60637c2008-07-25 19:45:31 -07001436 if (pagep) {
1437 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001438 if (unlikely(!page))
1439 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001440 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001441 if (radix_tree_deref_retry(page))
1442 goto repeat;
1443 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001444 * A shadow entry of a recently evicted page,
1445 * or a swap entry from shmem/tmpfs. Return
1446 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001447 */
1448 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001449 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001450
1451 head = compound_head(page);
1452 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001453 goto repeat;
1454
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001455 /* The page was split under us? */
1456 if (compound_head(page) != head) {
1457 put_page(head);
1458 goto repeat;
1459 }
1460
Nick Piggina60637c2008-07-25 19:45:31 -07001461 /*
1462 * Has the page moved?
1463 * This is part of the lockless pagecache protocol. See
1464 * include/linux/pagemap.h for details.
1465 */
1466 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001467 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001468 goto repeat;
1469 }
1470 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001471out:
Nick Piggina60637c2008-07-25 19:45:31 -07001472 rcu_read_unlock();
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return page;
1475}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001476EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Randy Dunlap485bb992006-06-23 02:03:49 -07001478/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001479 * find_lock_entry - locate, pin and lock a page cache entry
1480 * @mapping: the address_space to search
1481 * @offset: the page cache index
1482 *
1483 * Looks up the page cache slot at @mapping & @offset. If there is a
1484 * page cache page, it is returned locked and with an increased
1485 * refcount.
1486 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001487 * If the slot holds a shadow entry of a previously evicted page, or a
1488 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001489 *
1490 * Otherwise, %NULL is returned.
1491 *
1492 * find_lock_entry() may sleep.
1493 */
1494struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct page *page;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001499 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001500 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001501 lock_page(page);
1502 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001503 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001504 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001505 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001506 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001508 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return page;
1511}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001512EXPORT_SYMBOL(find_lock_entry);
1513
1514/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001515 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001516 * @mapping: the address_space to search
1517 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001518 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001519 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001520 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001521 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001522 *
Randy Dunlap75325182014-07-30 16:08:37 -07001523 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001524 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001525 * @fgp_flags can be:
1526 *
1527 * - FGP_ACCESSED: the page will be marked accessed
1528 * - FGP_LOCK: Page is return locked
1529 * - FGP_CREAT: If page is not present then a new page is allocated using
1530 * @gfp_mask and added to the page cache and the VM's LRU
1531 * list. The page is returned locked and with an increased
1532 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001533 *
1534 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1535 * if the GFP flags specified for FGP_CREAT are atomic.
1536 *
1537 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001538 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001539struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001540 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Nick Piggineb2be182007-10-16 01:24:57 -07001542 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001545 page = find_get_entry(mapping, offset);
1546 if (radix_tree_exceptional_entry(page))
1547 page = NULL;
1548 if (!page)
1549 goto no_page;
1550
1551 if (fgp_flags & FGP_LOCK) {
1552 if (fgp_flags & FGP_NOWAIT) {
1553 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001554 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001555 return NULL;
1556 }
1557 } else {
1558 lock_page(page);
1559 }
1560
1561 /* Has the page been truncated? */
1562 if (unlikely(page->mapping != mapping)) {
1563 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001564 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001565 goto repeat;
1566 }
1567 VM_BUG_ON_PAGE(page->index != offset, page);
1568 }
1569
1570 if (page && (fgp_flags & FGP_ACCESSED))
1571 mark_page_accessed(page);
1572
1573no_page:
1574 if (!page && (fgp_flags & FGP_CREAT)) {
1575 int err;
1576 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001577 gfp_mask |= __GFP_WRITE;
1578 if (fgp_flags & FGP_NOFS)
1579 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001580
Michal Hocko45f87de2014-12-29 20:30:35 +01001581 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001582 if (!page)
1583 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001584
1585 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1586 fgp_flags |= FGP_LOCK;
1587
Hugh Dickinseb39d612014-08-06 16:06:43 -07001588 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001589 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001590 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001591
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001592 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001593 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001594 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001595 page = NULL;
1596 if (err == -EEXIST)
1597 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return page;
1602}
Mel Gorman2457aec2014-06-04 16:10:31 -07001603EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001606 * find_get_entries - gang pagecache lookup
1607 * @mapping: The address_space to search
1608 * @start: The starting page cache index
1609 * @nr_entries: The maximum number of entries
1610 * @entries: Where the resulting entries are placed
1611 * @indices: The cache indices corresponding to the entries in @entries
1612 *
1613 * find_get_entries() will search for and return a group of up to
1614 * @nr_entries entries in the mapping. The entries are placed at
1615 * @entries. find_get_entries() takes a reference against any actual
1616 * pages it returns.
1617 *
1618 * The search returns a group of mapping-contiguous page cache entries
1619 * with ascending indexes. There may be holes in the indices due to
1620 * not-present pages.
1621 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001622 * Any shadow entries of evicted pages, or swap entries from
1623 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001624 *
1625 * find_get_entries() returns the number of pages and shadow entries
1626 * which were found.
1627 */
1628unsigned find_get_entries(struct address_space *mapping,
1629 pgoff_t start, unsigned int nr_entries,
1630 struct page **entries, pgoff_t *indices)
1631{
1632 void **slot;
1633 unsigned int ret = 0;
1634 struct radix_tree_iter iter;
1635
1636 if (!nr_entries)
1637 return 0;
1638
1639 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001640 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001641 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001642repeat:
1643 page = radix_tree_deref_slot(slot);
1644 if (unlikely(!page))
1645 continue;
1646 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001647 if (radix_tree_deref_retry(page)) {
1648 slot = radix_tree_iter_retry(&iter);
1649 continue;
1650 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001651 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001652 * A shadow entry of a recently evicted page, a swap
1653 * entry from shmem/tmpfs or a DAX entry. Return it
1654 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001655 */
1656 goto export;
1657 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001658
1659 head = compound_head(page);
1660 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001661 goto repeat;
1662
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001663 /* The page was split under us? */
1664 if (compound_head(page) != head) {
1665 put_page(head);
1666 goto repeat;
1667 }
1668
Johannes Weiner0cd61442014-04-03 14:47:46 -07001669 /* Has the page moved? */
1670 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001671 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001672 goto repeat;
1673 }
1674export:
1675 indices[ret] = iter.index;
1676 entries[ret] = page;
1677 if (++ret == nr_entries)
1678 break;
1679 }
1680 rcu_read_unlock();
1681 return ret;
1682}
1683
1684/**
Jan Karab947cee2017-09-06 16:21:21 -07001685 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * @mapping: The address_space to search
1687 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001688 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * @nr_pages: The maximum number of pages
1690 * @pages: Where the resulting pages are placed
1691 *
Jan Karab947cee2017-09-06 16:21:21 -07001692 * find_get_pages_range() will search for and return a group of up to @nr_pages
1693 * pages in the mapping starting at index @start and up to index @end
1694 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1695 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 *
1697 * The search returns a group of mapping-contiguous pages with ascending
1698 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001699 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *
Jan Karab947cee2017-09-06 16:21:21 -07001701 * find_get_pages_range() returns the number of pages which were found. If this
1702 * number is smaller than @nr_pages, the end of specified range has been
1703 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Jan Karab947cee2017-09-06 16:21:21 -07001705unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1706 pgoff_t end, unsigned int nr_pages,
1707 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001709 struct radix_tree_iter iter;
1710 void **slot;
1711 unsigned ret = 0;
1712
1713 if (unlikely(!nr_pages))
1714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Nick Piggina60637c2008-07-25 19:45:31 -07001716 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001717 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001718 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001719
1720 if (iter.index > end)
1721 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001722repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001723 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001724 if (unlikely(!page))
1725 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001726
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001727 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001728 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001729 slot = radix_tree_iter_retry(&iter);
1730 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001731 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001732 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001733 * A shadow entry of a recently evicted page,
1734 * or a swap entry from shmem/tmpfs. Skip
1735 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001736 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001737 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001738 }
Nick Piggina60637c2008-07-25 19:45:31 -07001739
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001740 head = compound_head(page);
1741 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001742 goto repeat;
1743
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001744 /* The page was split under us? */
1745 if (compound_head(page) != head) {
1746 put_page(head);
1747 goto repeat;
1748 }
1749
Nick Piggina60637c2008-07-25 19:45:31 -07001750 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001751 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001752 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001753 goto repeat;
1754 }
1755
1756 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001757 if (++ret == nr_pages) {
1758 *start = pages[ret - 1]->index + 1;
1759 goto out;
1760 }
Nick Piggina60637c2008-07-25 19:45:31 -07001761 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001762
Jan Karab947cee2017-09-06 16:21:21 -07001763 /*
1764 * We come here when there is no page beyond @end. We take care to not
1765 * overflow the index @start as it confuses some of the callers. This
1766 * breaks the iteration when there is page at index -1 but that is
1767 * already broken anyway.
1768 */
1769 if (end == (pgoff_t)-1)
1770 *start = (pgoff_t)-1;
1771 else
1772 *start = end + 1;
1773out:
Nick Piggina60637c2008-07-25 19:45:31 -07001774 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return ret;
1777}
1778
Jens Axboeebf43502006-04-27 08:46:01 +02001779/**
1780 * find_get_pages_contig - gang contiguous pagecache lookup
1781 * @mapping: The address_space to search
1782 * @index: The starting page index
1783 * @nr_pages: The maximum number of pages
1784 * @pages: Where the resulting pages are placed
1785 *
1786 * find_get_pages_contig() works exactly like find_get_pages(), except
1787 * that the returned number of pages are guaranteed to be contiguous.
1788 *
1789 * find_get_pages_contig() returns the number of pages which were found.
1790 */
1791unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1792 unsigned int nr_pages, struct page **pages)
1793{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001794 struct radix_tree_iter iter;
1795 void **slot;
1796 unsigned int ret = 0;
1797
1798 if (unlikely(!nr_pages))
1799 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001800
Nick Piggina60637c2008-07-25 19:45:31 -07001801 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001802 radix_tree_for_each_contig(slot, &mapping->i_pages, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001803 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001804repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001805 page = radix_tree_deref_slot(slot);
1806 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001807 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001808 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001809
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001810 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001811 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001812 slot = radix_tree_iter_retry(&iter);
1813 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001814 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001815 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001816 * A shadow entry of a recently evicted page,
1817 * or a swap entry from shmem/tmpfs. Stop
1818 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001819 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001820 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001821 }
Nick Piggina60637c2008-07-25 19:45:31 -07001822
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001823 head = compound_head(page);
1824 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001825 goto repeat;
1826
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001827 /* The page was split under us? */
1828 if (compound_head(page) != head) {
1829 put_page(head);
1830 goto repeat;
1831 }
1832
Nick Piggina60637c2008-07-25 19:45:31 -07001833 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001834 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001835 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001836 goto repeat;
1837 }
1838
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001839 /*
1840 * must check mapping and index after taking the ref.
1841 * otherwise we can get both false positives and false
1842 * negatives, which is just confusing to the caller.
1843 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001844 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001845 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001846 break;
1847 }
1848
Nick Piggina60637c2008-07-25 19:45:31 -07001849 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001850 if (++ret == nr_pages)
1851 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001852 }
Nick Piggina60637c2008-07-25 19:45:31 -07001853 rcu_read_unlock();
1854 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001855}
David Howellsef71c152007-05-09 02:33:44 -07001856EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001857
Randy Dunlap485bb992006-06-23 02:03:49 -07001858/**
Jan Kara72b045a2017-11-15 17:34:33 -08001859 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001860 * @mapping: the address_space to search
1861 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001862 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001863 * @tag: the tag index
1864 * @nr_pages: the maximum number of pages
1865 * @pages: where the resulting pages are placed
1866 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001868 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 */
Jan Kara72b045a2017-11-15 17:34:33 -08001870unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1871 pgoff_t end, int tag, unsigned int nr_pages,
1872 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001874 struct radix_tree_iter iter;
1875 void **slot;
1876 unsigned ret = 0;
1877
1878 if (unlikely(!nr_pages))
1879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Nick Piggina60637c2008-07-25 19:45:31 -07001881 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001882 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001883 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001884
1885 if (iter.index > end)
1886 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001887repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001888 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001889 if (unlikely(!page))
1890 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001891
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001892 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001893 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001894 slot = radix_tree_iter_retry(&iter);
1895 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001896 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001897 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001898 * A shadow entry of a recently evicted page.
1899 *
1900 * Those entries should never be tagged, but
1901 * this tree walk is lockless and the tags are
1902 * looked up in bulk, one radix tree node at a
1903 * time, so there is a sizable window for page
1904 * reclaim to evict a page we saw tagged.
1905 *
1906 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001907 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001908 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001909 }
Nick Piggina60637c2008-07-25 19:45:31 -07001910
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001911 head = compound_head(page);
1912 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001913 goto repeat;
1914
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001915 /* The page was split under us? */
1916 if (compound_head(page) != head) {
1917 put_page(head);
1918 goto repeat;
1919 }
1920
Nick Piggina60637c2008-07-25 19:45:31 -07001921 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001922 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001923 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001924 goto repeat;
1925 }
1926
1927 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001928 if (++ret == nr_pages) {
1929 *index = pages[ret - 1]->index + 1;
1930 goto out;
1931 }
Nick Piggina60637c2008-07-25 19:45:31 -07001932 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001933
Jan Kara72b045a2017-11-15 17:34:33 -08001934 /*
1935 * We come here when we got at @end. We take care to not overflow the
1936 * index @index as it confuses some of the callers. This breaks the
1937 * iteration when there is page at index -1 but that is already broken
1938 * anyway.
1939 */
1940 if (end == (pgoff_t)-1)
1941 *index = (pgoff_t)-1;
1942 else
1943 *index = end + 1;
1944out:
Nick Piggina60637c2008-07-25 19:45:31 -07001945 rcu_read_unlock();
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return ret;
1948}
Jan Kara72b045a2017-11-15 17:34:33 -08001949EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001951/**
1952 * find_get_entries_tag - find and return entries that match @tag
1953 * @mapping: the address_space to search
1954 * @start: the starting page cache index
1955 * @tag: the tag index
1956 * @nr_entries: the maximum number of entries
1957 * @entries: where the resulting entries are placed
1958 * @indices: the cache indices corresponding to the entries in @entries
1959 *
1960 * Like find_get_entries, except we only return entries which are tagged with
1961 * @tag.
1962 */
1963unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1964 int tag, unsigned int nr_entries,
1965 struct page **entries, pgoff_t *indices)
1966{
1967 void **slot;
1968 unsigned int ret = 0;
1969 struct radix_tree_iter iter;
1970
1971 if (!nr_entries)
1972 return 0;
1973
1974 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001975 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001976 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001977repeat:
1978 page = radix_tree_deref_slot(slot);
1979 if (unlikely(!page))
1980 continue;
1981 if (radix_tree_exception(page)) {
1982 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001983 slot = radix_tree_iter_retry(&iter);
1984 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001985 }
1986
1987 /*
1988 * A shadow entry of a recently evicted page, a swap
1989 * entry from shmem/tmpfs or a DAX entry. Return it
1990 * without attempting to raise page count.
1991 */
1992 goto export;
1993 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001994
1995 head = compound_head(page);
1996 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001997 goto repeat;
1998
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001999 /* The page was split under us? */
2000 if (compound_head(page) != head) {
2001 put_page(head);
2002 goto repeat;
2003 }
2004
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002005 /* Has the page moved? */
2006 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002007 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002008 goto repeat;
2009 }
2010export:
2011 indices[ret] = iter.index;
2012 entries[ret] = page;
2013 if (++ret == nr_entries)
2014 break;
2015 }
2016 rcu_read_unlock();
2017 return ret;
2018}
2019EXPORT_SYMBOL(find_get_entries_tag);
2020
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002021/*
2022 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2023 * a _large_ part of the i/o request. Imagine the worst scenario:
2024 *
2025 * ---R__________________________________________B__________
2026 * ^ reading here ^ bad block(assume 4k)
2027 *
2028 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2029 * => failing the whole request => read(R) => read(R+1) =>
2030 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2031 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2032 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2033 *
2034 * It is going insane. Fix it by quickly scaling down the readahead size.
2035 */
2036static void shrink_readahead_size_eio(struct file *filp,
2037 struct file_ra_state *ra)
2038{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002039 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002040}
2041
Randy Dunlap485bb992006-06-23 02:03:49 -07002042/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002043 * generic_file_buffered_read - generic file read routine
2044 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002045 * @iter: data destination
2046 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002047 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002049 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 *
2051 * This is really ugly. But the goto's actually try to clarify some
2052 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002054static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002055 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002057 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002058 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002060 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002061 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002062 pgoff_t index;
2063 pgoff_t last_index;
2064 pgoff_t prev_index;
2065 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002066 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002067 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Wei Fangc2a97372016-10-07 17:01:52 -07002069 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002070 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002071 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2072
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 index = *ppos >> PAGE_SHIFT;
2074 prev_index = ra->prev_pos >> PAGE_SHIFT;
2075 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2076 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2077 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 for (;;) {
2080 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002081 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002082 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 unsigned long nr, ret;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002087 if (fatal_signal_pending(current)) {
2088 error = -EINTR;
2089 goto out;
2090 }
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002093 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002094 if (iocb->ki_flags & IOCB_NOWAIT)
2095 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002096 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002097 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002098 index, last_index - index);
2099 page = find_get_page(mapping, index);
2100 if (unlikely(page == NULL))
2101 goto no_cached_page;
2102 }
2103 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002104 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002105 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002106 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002108 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002109 if (iocb->ki_flags & IOCB_NOWAIT) {
2110 put_page(page);
2111 goto would_block;
2112 }
2113
Mel Gormanebded022016-03-15 14:55:39 -07002114 /*
2115 * See comment in do_read_cache_page on why
2116 * wait_on_page_locked is used to avoid unnecessarily
2117 * serialisations and why it's safe.
2118 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002119 error = wait_on_page_locked_killable(page);
2120 if (unlikely(error))
2121 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002122 if (PageUptodate(page))
2123 goto page_ok;
2124
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002126 !mapping->a_ops->is_partially_uptodate)
2127 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002128 /* pipes can't handle partially uptodate pages */
2129 if (unlikely(iter->type & ITER_PIPE))
2130 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002131 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002132 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002133 /* Did it get truncated before we got the lock? */
2134 if (!page->mapping)
2135 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002136 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002137 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002138 goto page_not_up_to_date_locked;
2139 unlock_page(page);
2140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002142 /*
2143 * i_size must be checked after we know the page is Uptodate.
2144 *
2145 * Checking i_size after the check allows us to calculate
2146 * the correct value for "nr", which means the zero-filled
2147 * part of the page is not copied back to userspace (unless
2148 * another truncate extends the file - this is desired though).
2149 */
2150
2151 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002152 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002153 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002154 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002155 goto out;
2156 }
2157
2158 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002159 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002160 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002161 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002162 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002163 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002164 goto out;
2165 }
2166 }
2167 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 /* If users can be writing to this page using arbitrary
2170 * virtual addresses, take care about potential aliasing
2171 * before reading the page on the kernel side.
2172 */
2173 if (mapping_writably_mapped(mapping))
2174 flush_dcache_page(page);
2175
2176 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002177 * When a sequential read accesses a page several times,
2178 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 */
Jan Karaec0f1632007-05-06 14:49:25 -07002180 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 mark_page_accessed(page);
2182 prev_index = index;
2183
2184 /*
2185 * Ok, we have the page, and it's up-to-date, so
2186 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 */
Al Viro6e58e792014-02-03 17:07:03 -05002188
2189 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002191 index += offset >> PAGE_SHIFT;
2192 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002193 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002196 written += ret;
2197 if (!iov_iter_count(iter))
2198 goto out;
2199 if (ret < nr) {
2200 error = -EFAULT;
2201 goto out;
2202 }
2203 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205page_not_up_to_date:
2206 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002207 error = lock_page_killable(page);
2208 if (unlikely(error))
2209 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002211page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002212 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (!page->mapping) {
2214 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002215 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 continue;
2217 }
2218
2219 /* Did somebody else fill it already? */
2220 if (PageUptodate(page)) {
2221 unlock_page(page);
2222 goto page_ok;
2223 }
2224
2225readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002226 /*
2227 * A previous I/O error may have been due to temporary
2228 * failures, eg. multipath errors.
2229 * PG_error will be set again if readpage fails.
2230 */
2231 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* Start the actual read. The read will unlock the page. */
2233 error = mapping->a_ops->readpage(filp, page);
2234
Zach Brown994fc28c2005-12-15 14:28:17 -08002235 if (unlikely(error)) {
2236 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002237 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002238 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002239 goto find_page;
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002245 error = lock_page_killable(page);
2246 if (unlikely(error))
2247 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!PageUptodate(page)) {
2249 if (page->mapping == NULL) {
2250 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002251 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 */
2253 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002254 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto find_page;
2256 }
2257 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002258 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002259 error = -EIO;
2260 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262 unlock_page(page);
2263 }
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto page_ok;
2266
2267readpage_error:
2268 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002269 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 goto out;
2271
2272no_cached_page:
2273 /*
2274 * Ok, it wasn't cached, so we need to create a new
2275 * page..
2276 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002277 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002278 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002279 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002280 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002282 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002283 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002285 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002286 if (error == -EEXIST) {
2287 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto out;
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto readpage;
2293 }
2294
Milosz Tanski3239d832017-08-29 16:13:19 +02002295would_block:
2296 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002298 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002299 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002300 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002302 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002303 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002304 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Randy Dunlap485bb992006-06-23 02:03:49 -07002307/**
Al Viro6abd2322014-04-04 14:20:57 -04002308 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002309 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002310 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002311 *
Al Viro6abd2322014-04-04 14:20:57 -04002312 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 * that can use the page cache directly.
2314 */
2315ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002316generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002318 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002319 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002320
2321 if (!count)
2322 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Al Viro2ba48ce2015-04-09 13:52:01 -04002324 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002325 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002326 struct address_space *mapping = file->f_mapping;
2327 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002328 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002331 if (iocb->ki_flags & IOCB_NOWAIT) {
2332 if (filemap_range_has_page(mapping, iocb->ki_pos,
2333 iocb->ki_pos + count - 1))
2334 return -EAGAIN;
2335 } else {
2336 retval = filemap_write_and_wait_range(mapping,
2337 iocb->ki_pos,
2338 iocb->ki_pos + count - 1);
2339 if (retval < 0)
2340 goto out;
2341 }
Al Viroed978a82014-03-05 22:53:04 -05002342
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002343 file_accessed(file);
2344
Al Viro5ecda132017-04-13 14:13:36 -04002345 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002346 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002347 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002348 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002349 }
Al Viro5b47d592017-05-08 13:54:47 -04002350 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002351
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002352 /*
2353 * Btrfs can have a short DIO read if we encounter
2354 * compressed extents, so if there was an error, or if
2355 * we've already read everything we wanted to, or if
2356 * there was a short read because we hit EOF, go ahead
2357 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002358 * the rest of the read. Buffered reads will not work for
2359 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002360 */
Al Viro5ecda132017-04-13 14:13:36 -04002361 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002362 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002366 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367out:
2368 return retval;
2369}
Al Viroed978a82014-03-05 22:53:04 -05002370EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002373/**
2374 * page_cache_read - adds requested page to the page cache if not already there
2375 * @file: file to read
2376 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002377 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002378 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 * This adds the requested page to the page cache if it isn't already there,
2380 * and schedules an I/O to read in its contents from disk.
2381 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002382static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002385 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002386 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Zach Brown994fc28c2005-12-15 14:28:17 -08002388 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002389 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002390 if (!page)
2391 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002393 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002394 if (ret == 0)
2395 ret = mapping->a_ops->readpage(file, page);
2396 else if (ret == -EEXIST)
2397 ret = 0; /* losing race to add is OK */
2398
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002399 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Zach Brown994fc28c2005-12-15 14:28:17 -08002401 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002402
Zach Brown994fc28c2005-12-15 14:28:17 -08002403 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406#define MMAP_LOTSAMISS (100)
2407
Linus Torvaldsef00e082009-06-16 15:31:25 -07002408/*
2409 * Synchronous readahead happens when we don't even find
2410 * a page in the page cache at all.
2411 */
2412static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2413 struct file_ra_state *ra,
2414 struct file *file,
2415 pgoff_t offset)
2416{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002417 struct address_space *mapping = file->f_mapping;
2418
2419 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002420 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002421 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002422 if (!ra->ra_pages)
2423 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002424
Joe Perches64363aa2013-07-08 16:00:18 -07002425 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002426 page_cache_sync_readahead(mapping, ra, file, offset,
2427 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002428 return;
2429 }
2430
Andi Kleen207d04b2011-05-24 17:12:29 -07002431 /* Avoid banging the cache line if not needed */
2432 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002433 ra->mmap_miss++;
2434
2435 /*
2436 * Do we miss much more than hit in this file? If so,
2437 * stop bothering with read-ahead. It will only hurt.
2438 */
2439 if (ra->mmap_miss > MMAP_LOTSAMISS)
2440 return;
2441
Wu Fengguangd30a1102009-06-16 15:31:30 -07002442 /*
2443 * mmap read-around
2444 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002445 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2446 ra->size = ra->ra_pages;
2447 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002448 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002449}
2450
2451/*
2452 * Asynchronous readahead happens when we find the page and PG_readahead,
2453 * so we want to possibly extend the readahead further..
2454 */
2455static void do_async_mmap_readahead(struct vm_area_struct *vma,
2456 struct file_ra_state *ra,
2457 struct file *file,
2458 struct page *page,
2459 pgoff_t offset)
2460{
2461 struct address_space *mapping = file->f_mapping;
2462
2463 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002464 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002465 return;
2466 if (ra->mmap_miss > 0)
2467 ra->mmap_miss--;
2468 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002469 page_cache_async_readahead(mapping, ra, file,
2470 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002471}
2472
Randy Dunlap485bb992006-06-23 02:03:49 -07002473/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002474 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002475 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002476 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002477 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 * mapped memory region to read in file data during a page fault.
2479 *
2480 * The goto's are kind of ugly, but this streamlines the normal case of having
2481 * it in the page cache, and handles the special cases reasonably without
2482 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002483 *
2484 * vma->vm_mm->mmap_sem must be held on entry.
2485 *
2486 * If our return value has VM_FAULT_RETRY set, it's because
2487 * lock_page_or_retry() returned 0.
2488 * The mmap_sem has usually been released in this case.
2489 * See __lock_page_or_retry() for the exception.
2490 *
2491 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2492 * has not been released.
2493 *
2494 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002496vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002499 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct address_space *mapping = file->f_mapping;
2501 struct file_ra_state *ra = &file->f_ra;
2502 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002503 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002504 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002506 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002508 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2509 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002510 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002513 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002515 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002516 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002518 * We found the page, so try async readahead before
2519 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 */
Dave Jiang11bac802017-02-24 14:56:41 -08002521 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002522 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002523 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002524 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002525 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002526 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002527 ret = VM_FAULT_MAJOR;
2528retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002529 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (!page)
2531 goto no_cached_page;
2532 }
2533
Dave Jiang11bac802017-02-24 14:56:41 -08002534 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002535 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002536 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002537 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002538
2539 /* Did it get truncated? */
2540 if (unlikely(page->mapping != mapping)) {
2541 unlock_page(page);
2542 put_page(page);
2543 goto retry_find;
2544 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002545 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002548 * We have a locked page in the page cache, now we need to check
2549 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 */
Nick Piggind00806b2007-07-19 01:46:57 -07002551 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 goto page_not_uptodate;
2553
Linus Torvaldsef00e082009-06-16 15:31:25 -07002554 /*
2555 * Found the page and have a reference on it.
2556 * We must recheck i_size under page lock.
2557 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002558 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2559 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002560 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002561 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002562 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002563 }
2564
Nick Piggind0217ac2007-07-19 01:47:03 -07002565 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002566 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568no_cached_page:
2569 /*
2570 * We're only likely to ever get here if MADV_RANDOM is in
2571 * effect.
2572 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002573 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /*
2576 * The page we want has now been added to the page cache.
2577 * In the unlikely event that someone removed it in the
2578 * meantime, we'll just come back here and read it again.
2579 */
2580 if (error >= 0)
2581 goto retry_find;
2582
2583 /*
2584 * An error return from page_cache_read can result if the
2585 * system is low on memory, or a problem occurs while trying
2586 * to schedule I/O.
2587 */
2588 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002589 return VM_FAULT_OOM;
2590 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 /*
2594 * Umm, take care of errors if the page isn't up-to-date.
2595 * Try to re-read it _once_. We do this synchronously,
2596 * because there really aren't any performance issues here
2597 * and we need to check for errors.
2598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002600 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002601 if (!error) {
2602 wait_on_page_locked(page);
2603 if (!PageUptodate(page))
2604 error = -EIO;
2605 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002606 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002607
2608 if (!error || error == AOP_TRUNCATED_PAGE)
2609 goto retry_find;
2610
2611 /* Things didn't work out. Return zero to tell the mm layer so. */
2612 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002613 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002614}
2615EXPORT_SYMBOL(filemap_fault);
2616
Jan Kara82b0f8c2016-12-14 15:06:58 -08002617void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002618 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002619{
2620 struct radix_tree_iter iter;
2621 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002622 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002623 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002624 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002625 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002626 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002627
2628 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002629 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start_pgoff) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002630 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002631 break;
2632repeat:
2633 page = radix_tree_deref_slot(slot);
2634 if (unlikely(!page))
2635 goto next;
2636 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002637 if (radix_tree_deref_retry(page)) {
2638 slot = radix_tree_iter_retry(&iter);
2639 continue;
2640 }
2641 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002642 }
2643
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002644 head = compound_head(page);
2645 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002646 goto repeat;
2647
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002648 /* The page was split under us? */
2649 if (compound_head(page) != head) {
2650 put_page(head);
2651 goto repeat;
2652 }
2653
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002654 /* Has the page moved? */
2655 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002656 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002657 goto repeat;
2658 }
2659
2660 if (!PageUptodate(page) ||
2661 PageReadahead(page) ||
2662 PageHWPoison(page))
2663 goto skip;
2664 if (!trylock_page(page))
2665 goto skip;
2666
2667 if (page->mapping != mapping || !PageUptodate(page))
2668 goto unlock;
2669
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002670 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2671 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002672 goto unlock;
2673
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002674 if (file->f_ra.mmap_miss > 0)
2675 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002676
Jan Kara82b0f8c2016-12-14 15:06:58 -08002677 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2678 if (vmf->pte)
2679 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002680 last_pgoff = iter.index;
Vinayak Menon3aee78b2016-05-20 16:58:41 -07002681
2682 if (want_old_faultaround_pte) {
2683 if (iter.index == vmf->pgoff)
2684 vmf->flags &= ~FAULT_FLAG_PREFAULT_OLD;
2685 else
2686 vmf->flags |= FAULT_FLAG_PREFAULT_OLD;
2687 }
2688
Jan Kara82b0f8c2016-12-14 15:06:58 -08002689 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002690 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002691 unlock_page(page);
2692 goto next;
2693unlock:
2694 unlock_page(page);
2695skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002696 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002697next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002698 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002699 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002700 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002701 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002702 break;
2703 }
2704 rcu_read_unlock();
2705}
2706EXPORT_SYMBOL(filemap_map_pages);
2707
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002708vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002709{
2710 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002711 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002712 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002713
Jan Kara14da9202012-06-12 16:20:37 +02002714 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002715 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002716 lock_page(page);
2717 if (page->mapping != inode->i_mapping) {
2718 unlock_page(page);
2719 ret = VM_FAULT_NOPAGE;
2720 goto out;
2721 }
Jan Kara14da9202012-06-12 16:20:37 +02002722 /*
2723 * We mark the page dirty already here so that when freeze is in
2724 * progress, we are guaranteed that writeback during freezing will
2725 * see the dirty page and writeprotect it again.
2726 */
2727 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002728 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002729out:
Jan Kara14da9202012-06-12 16:20:37 +02002730 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002731 return ret;
2732}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002733
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002734const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002735 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002736 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002737 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738};
2739
2740/* This is used for a general mmap of a disk file */
2741
2742int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2743{
2744 struct address_space *mapping = file->f_mapping;
2745
2746 if (!mapping->a_ops->readpage)
2747 return -ENOEXEC;
2748 file_accessed(file);
2749 vma->vm_ops = &generic_file_vm_ops;
2750 return 0;
2751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753/*
2754 * This is for filesystems which do not implement ->writepage.
2755 */
2756int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2757{
2758 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2759 return -EINVAL;
2760 return generic_file_mmap(file, vma);
2761}
2762#else
Arnd Bergmann45397222018-04-13 15:35:27 -07002763int filemap_page_mkwrite(struct vm_fault *vmf)
2764{
2765 return -ENOSYS;
2766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2768{
2769 return -ENOSYS;
2770}
2771int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2772{
2773 return -ENOSYS;
2774}
2775#endif /* CONFIG_MMU */
2776
Arnd Bergmann45397222018-04-13 15:35:27 -07002777EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778EXPORT_SYMBOL(generic_file_mmap);
2779EXPORT_SYMBOL(generic_file_readonly_mmap);
2780
Sasha Levin67f9fd92014-04-03 14:48:18 -07002781static struct page *wait_on_page_read(struct page *page)
2782{
2783 if (!IS_ERR(page)) {
2784 wait_on_page_locked(page);
2785 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002786 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002787 page = ERR_PTR(-EIO);
2788 }
2789 }
2790 return page;
2791}
2792
Mel Gorman32b63522016-03-15 14:55:36 -07002793static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002794 pgoff_t index,
Sami Tolvanen2aa218a2018-04-25 16:08:11 -07002795 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002796 void *data,
2797 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Nick Piggineb2be182007-10-16 01:24:57 -07002799 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 int err;
2801repeat:
2802 page = find_get_page(mapping, index);
2803 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002804 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002805 if (!page)
2806 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002807 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002808 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002809 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002810 if (err == -EEXIST)
2811 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return ERR_PTR(err);
2814 }
Mel Gorman32b63522016-03-15 14:55:36 -07002815
2816filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 err = filler(data, page);
2818 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002819 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002820 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
Mel Gorman32b63522016-03-15 14:55:36 -07002822
2823 page = wait_on_page_read(page);
2824 if (IS_ERR(page))
2825 return page;
2826 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (PageUptodate(page))
2829 goto out;
2830
Mel Gormanebded022016-03-15 14:55:39 -07002831 /*
2832 * Page is not up to date and may be locked due one of the following
2833 * case a: Page is being filled and the page lock is held
2834 * case b: Read/write error clearing the page uptodate status
2835 * case c: Truncation in progress (page locked)
2836 * case d: Reclaim in progress
2837 *
2838 * Case a, the page will be up to date when the page is unlocked.
2839 * There is no need to serialise on the page lock here as the page
2840 * is pinned so the lock gives no additional protection. Even if the
2841 * the page is truncated, the data is still valid if PageUptodate as
2842 * it's a race vs truncate race.
2843 * Case b, the page will not be up to date
2844 * Case c, the page may be truncated but in itself, the data may still
2845 * be valid after IO completes as it's a read vs truncate race. The
2846 * operation must restart if the page is not uptodate on unlock but
2847 * otherwise serialising on page lock to stabilise the mapping gives
2848 * no additional guarantees to the caller as the page lock is
2849 * released before return.
2850 * Case d, similar to truncation. If reclaim holds the page lock, it
2851 * will be a race with remove_mapping that determines if the mapping
2852 * is valid on unlock but otherwise the data is valid and there is
2853 * no need to serialise with page lock.
2854 *
2855 * As the page lock gives no additional guarantee, we optimistically
2856 * wait on the page to be unlocked and check if it's up to date and
2857 * use the page if it is. Otherwise, the page lock is required to
2858 * distinguish between the different cases. The motivation is that we
2859 * avoid spurious serialisations and wakeups when multiple processes
2860 * wait on the same page for IO to complete.
2861 */
2862 wait_on_page_locked(page);
2863 if (PageUptodate(page))
2864 goto out;
2865
2866 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002868
2869 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (!page->mapping) {
2871 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002872 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002873 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Mel Gormanebded022016-03-15 14:55:39 -07002875
2876 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (PageUptodate(page)) {
2878 unlock_page(page);
2879 goto out;
2880 }
Mel Gorman32b63522016-03-15 14:55:36 -07002881 goto filler;
2882
David Howellsc855ff32007-05-09 13:42:20 +01002883out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002884 mark_page_accessed(page);
2885 return page;
2886}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002887
2888/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002889 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002890 * @mapping: the page's address_space
2891 * @index: the page index
2892 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002893 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002894 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002895 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002896 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002897 *
2898 * If the page does not get brought uptodate, return -EIO.
2899 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002900struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002901 pgoff_t index,
Sami Tolvanen2aa218a2018-04-25 16:08:11 -07002902 int (*filler)(struct file *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002903 void *data)
2904{
2905 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2906}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002907EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002908
2909/**
2910 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2911 * @mapping: the page's address_space
2912 * @index: the page index
2913 * @gfp: the page allocator flags to use if allocating
2914 *
2915 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002916 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002917 *
2918 * If the page does not get brought uptodate, return -EIO.
2919 */
2920struct page *read_cache_page_gfp(struct address_space *mapping,
2921 pgoff_t index,
2922 gfp_t gfp)
2923{
Sami Tolvanen2aa218a2018-04-25 16:08:11 -07002924 filler_t *filler = mapping->a_ops->readpage;
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002925
Sasha Levin67f9fd92014-04-03 14:48:18 -07002926 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002927}
2928EXPORT_SYMBOL(read_cache_page_gfp);
2929
Nick Piggin2f718ff2007-10-16 01:24:59 -07002930/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 * Performs necessary checks before doing a write
2932 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002933 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * Returns appropriate error code that caller should return or
2935 * zero in case that write should be allowed.
2936 */
Al Viro3309dd02015-04-09 12:55:47 -04002937inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Al Viro3309dd02015-04-09 12:55:47 -04002939 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002941 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002942 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Al Viro3309dd02015-04-09 12:55:47 -04002944 if (!iov_iter_count(from))
2945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Al Viro0fa6b002015-04-04 04:05:48 -04002947 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002948 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002949 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Al Viro3309dd02015-04-09 12:55:47 -04002951 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002953 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2954 return -EINVAL;
2955
Al Viro0fa6b002015-04-04 04:05:48 -04002956 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002957 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002958 send_sig(SIGXFSZ, current, 0);
2959 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
Al Viro3309dd02015-04-09 12:55:47 -04002961 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963
2964 /*
2965 * LFS rule
2966 */
Al Viro3309dd02015-04-09 12:55:47 -04002967 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002969 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002971 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973
2974 /*
2975 * Are we about to exceed the fs block limit ?
2976 *
2977 * If we have written data it becomes a short write. If we have
2978 * exceeded without writing data we send a signal and return EFBIG.
2979 * Linus frestrict idea will clean these up nicely..
2980 */
Al Viro3309dd02015-04-09 12:55:47 -04002981 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2982 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Al Viro3309dd02015-04-09 12:55:47 -04002984 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2985 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987EXPORT_SYMBOL(generic_write_checks);
2988
Nick Pigginafddba42007-10-16 01:25:01 -07002989int pagecache_write_begin(struct file *file, struct address_space *mapping,
2990 loff_t pos, unsigned len, unsigned flags,
2991 struct page **pagep, void **fsdata)
2992{
2993 const struct address_space_operations *aops = mapping->a_ops;
2994
Nick Piggin4e02ed42008-10-29 14:00:55 -07002995 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002996 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002997}
2998EXPORT_SYMBOL(pagecache_write_begin);
2999
3000int pagecache_write_end(struct file *file, struct address_space *mapping,
3001 loff_t pos, unsigned len, unsigned copied,
3002 struct page *page, void *fsdata)
3003{
3004 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003005
Nick Piggin4e02ed42008-10-29 14:00:55 -07003006 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003007}
3008EXPORT_SYMBOL(pagecache_write_end);
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003011generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 struct file *file = iocb->ki_filp;
3014 struct address_space *mapping = file->f_mapping;
3015 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003016 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003018 size_t write_len;
3019 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Al Viro0c949332014-03-22 06:51:37 -04003021 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003022 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003023
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003024 if (iocb->ki_flags & IOCB_NOWAIT) {
3025 /* If there are pages to writeback, return */
3026 if (filemap_range_has_page(inode->i_mapping, pos,
3027 pos + iov_iter_count(from)))
3028 return -EAGAIN;
3029 } else {
3030 written = filemap_write_and_wait_range(mapping, pos,
3031 pos + write_len - 1);
3032 if (written)
3033 goto out;
3034 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003035
3036 /*
3037 * After a write we want buffered reads to be sure to go to disk to get
3038 * the new data. We invalidate clean cached page from the region we're
3039 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003040 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003041 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003042 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003043 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003044 /*
3045 * If a page can not be invalidated, return 0 to fall back
3046 * to buffered write.
3047 */
3048 if (written) {
3049 if (written == -EBUSY)
3050 return 0;
3051 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003052 }
3053
Al Viro639a93a52017-04-13 14:10:15 -04003054 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003055
3056 /*
3057 * Finally, try again to invalidate clean pages which might have been
3058 * cached by non-direct readahead, or faulted in by get_user_pages()
3059 * if the source of the write was an mmap'ed region of the file
3060 * we're writing. Either one is a pretty crazy thing to do,
3061 * so we don't support it 100%. If this invalidation
3062 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003063 *
3064 * Most of the time we do not need this since dio_complete() will do
3065 * the invalidation for us. However there are some file systems that
3066 * do not end up with dio_complete() being called, so let's not break
3067 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003068 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003069 if (mapping->nrpages)
3070 invalidate_inode_pages2_range(mapping,
3071 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003074 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003075 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003076 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3077 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 mark_inode_dirty(inode);
3079 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003080 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Al Viro639a93a52017-04-13 14:10:15 -04003082 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003083out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return written;
3085}
3086EXPORT_SYMBOL(generic_file_direct_write);
3087
Nick Piggineb2be182007-10-16 01:24:57 -07003088/*
3089 * Find or create a page at the given pagecache position. Return the locked
3090 * page. This function is specifically for buffered writes.
3091 */
Nick Piggin54566b22009-01-04 12:00:53 -08003092struct page *grab_cache_page_write_begin(struct address_space *mapping,
3093 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003094{
Nick Piggineb2be182007-10-16 01:24:57 -07003095 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003096 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003097
Nick Piggin54566b22009-01-04 12:00:53 -08003098 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003099 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003100
Mel Gorman2457aec2014-06-04 16:10:31 -07003101 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003102 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003103 if (page)
3104 wait_for_stable_page(page);
3105
Nick Piggineb2be182007-10-16 01:24:57 -07003106 return page;
3107}
Nick Piggin54566b22009-01-04 12:00:53 -08003108EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003109
Al Viro3b93f912014-02-11 21:34:08 -05003110ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003111 struct iov_iter *i, loff_t pos)
3112{
3113 struct address_space *mapping = file->f_mapping;
3114 const struct address_space_operations *a_ops = mapping->a_ops;
3115 long status = 0;
3116 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003117 unsigned int flags = 0;
3118
Nick Pigginafddba42007-10-16 01:25:01 -07003119 do {
3120 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003121 unsigned long offset; /* Offset into pagecache page */
3122 unsigned long bytes; /* Bytes to write to page */
3123 size_t copied; /* Bytes copied from user */
3124 void *fsdata;
3125
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003126 offset = (pos & (PAGE_SIZE - 1));
3127 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003128 iov_iter_count(i));
3129
3130again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003131 /*
3132 * Bring in the user page that we will copy from _first_.
3133 * Otherwise there's a nasty deadlock on copying from the
3134 * same page as we're writing to, without it being marked
3135 * up-to-date.
3136 *
3137 * Not only is this an optimisation, but it is also required
3138 * to check that the address is actually valid, when atomic
3139 * usercopies are used, below.
3140 */
3141 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3142 status = -EFAULT;
3143 break;
3144 }
3145
Jan Kara296291c2015-10-22 13:32:21 -07003146 if (fatal_signal_pending(current)) {
3147 status = -EINTR;
3148 break;
3149 }
3150
Nick Piggin674b8922007-10-16 01:25:03 -07003151 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003152 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003153 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003154 break;
3155
anfei zhou931e80e2010-02-02 13:44:02 -08003156 if (mapping_writably_mapped(mapping))
3157 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003158
Nick Pigginafddba42007-10-16 01:25:01 -07003159 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003160 flush_dcache_page(page);
3161
3162 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3163 page, fsdata);
3164 if (unlikely(status < 0))
3165 break;
3166 copied = status;
3167
3168 cond_resched();
3169
Nick Piggin124d3b72008-02-02 15:01:17 +01003170 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003171 if (unlikely(copied == 0)) {
3172 /*
3173 * If we were unable to copy any data at all, we must
3174 * fall back to a single segment length write.
3175 *
3176 * If we didn't fallback here, we could livelock
3177 * because not all segments in the iov can be copied at
3178 * once without a pagefault.
3179 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003180 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003181 iov_iter_single_seg_count(i));
3182 goto again;
3183 }
Nick Pigginafddba42007-10-16 01:25:01 -07003184 pos += copied;
3185 written += copied;
3186
3187 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003188 } while (iov_iter_count(i));
3189
3190 return written ? written : status;
3191}
Al Viro3b93f912014-02-11 21:34:08 -05003192EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Jan Karae4dd9de2009-08-17 18:10:06 +02003194/**
Al Viro81742022014-04-03 03:17:43 -04003195 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003196 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003197 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003198 *
3199 * This function does all the work needed for actually writing data to a
3200 * file. It does all basic checks, removes SUID from the file, updates
3201 * modification times and calls proper subroutines depending on whether we
3202 * do direct IO or a standard buffered write.
3203 *
3204 * It expects i_mutex to be grabbed unless we work on a block device or similar
3205 * object which does not need locking at all.
3206 *
3207 * This function does *not* take care of syncing data in case of O_SYNC write.
3208 * A caller has to handle it. This is mainly due to the fact that we want to
3209 * avoid syncing under i_mutex.
3210 */
Al Viro81742022014-04-03 03:17:43 -04003211ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003214 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003216 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003218 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003221 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003222 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (err)
3224 goto out;
3225
Josef Bacikc3b2da32012-03-26 09:59:21 -04003226 err = file_update_time(file);
3227 if (err)
3228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Al Viro2ba48ce2015-04-09 13:52:01 -04003230 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003231 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003232
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003233 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003234 /*
3235 * If the write stopped short of completing, fall back to
3236 * buffered writes. Some filesystems do this for writes to
3237 * holes, for example. For DAX files, a buffered write will
3238 * not succeed (even if it did, DAX does not handle dirty
3239 * page-cache pages correctly).
3240 */
Al Viro0b8def92015-04-07 10:22:53 -04003241 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003243
Al Viro0b8def92015-04-07 10:22:53 -04003244 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003245 /*
Al Viro3b93f912014-02-11 21:34:08 -05003246 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003247 * then we want to return the number of bytes which were
3248 * direct-written, or the error code if that was zero. Note
3249 * that this differs from normal direct-io semantics, which
3250 * will return -EFOO even if some bytes were written.
3251 */
Al Viro60bb4522014-08-08 12:39:16 -04003252 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003253 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003254 goto out;
3255 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003256 /*
3257 * We need to ensure that the page cache pages are written to
3258 * disk and invalidated to preserve the expected O_DIRECT
3259 * semantics.
3260 */
Al Viro3b93f912014-02-11 21:34:08 -05003261 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003262 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003263 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003264 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003265 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003266 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003267 pos >> PAGE_SHIFT,
3268 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003269 } else {
3270 /*
3271 * We don't know how much we wrote, so just return
3272 * the number of bytes which were direct-written
3273 */
3274 }
3275 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003276 written = generic_perform_write(file, from, iocb->ki_pos);
3277 if (likely(written > 0))
3278 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280out:
3281 current->backing_dev_info = NULL;
3282 return written ? written : err;
3283}
Al Viro81742022014-04-03 03:17:43 -04003284EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Jan Karae4dd9de2009-08-17 18:10:06 +02003286/**
Al Viro81742022014-04-03 03:17:43 -04003287 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003288 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003289 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003290 *
Al Viro81742022014-04-03 03:17:43 -04003291 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003292 * filesystems. It takes care of syncing the file in case of O_SYNC file
3293 * and acquires i_mutex as needed.
3294 */
Al Viro81742022014-04-03 03:17:43 -04003295ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003298 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Al Viro59551022016-01-22 15:40:57 -05003301 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003302 ret = generic_write_checks(iocb, from);
3303 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003304 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003305 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Christoph Hellwige2592212016-04-07 08:52:01 -07003307 if (ret > 0)
3308 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return ret;
3310}
Al Viro81742022014-04-03 03:17:43 -04003311EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
David Howellscf9a2ae2006-08-29 19:05:54 +01003313/**
3314 * try_to_release_page() - release old fs-specific metadata on a page
3315 *
3316 * @page: the page which the kernel is trying to free
3317 * @gfp_mask: memory allocation flags (and I/O mode)
3318 *
3319 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003320 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003321 * Otherwise return zero.
3322 *
David Howells266cf652009-04-03 16:42:36 +01003323 * This may also be called if PG_fscache is set on a page, indicating that the
3324 * page is known to the local caching routines.
3325 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003326 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003327 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003328 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003329 */
3330int try_to_release_page(struct page *page, gfp_t gfp_mask)
3331{
3332 struct address_space * const mapping = page->mapping;
3333
3334 BUG_ON(!PageLocked(page));
3335 if (PageWriteback(page))
3336 return 0;
3337
3338 if (mapping && mapping->a_ops->releasepage)
3339 return mapping->a_ops->releasepage(page, gfp_mask);
3340 return try_to_free_buffers(page);
3341}
3342
3343EXPORT_SYMBOL(try_to_release_page);