blob: 714d3d0f60f55000a60889921c3ea8ca9bab0242 [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
51/*
52 * Shared mappings implemented 30.11.1994. It's not fully working yet,
53 * though.
54 *
55 * Shared mappings now work. 15.8.1995 Bruno.
56 *
57 * finished 'unifying' the page and buffer cache and SMP-threaded the
58 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
59 *
60 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
61 */
62
63/*
64 * Lock ordering:
65 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080066 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070068 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070069 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080071 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080072 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080075 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070076 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070077 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 * ->mmap_sem
80 * ->lock_page (access_process_vm)
81 *
Al Viroccad2362014-02-11 22:36:48 -050082 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070083 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060085 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110086 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070087 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080089 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * ->anon_vma.lock (vma_adjust)
91 *
92 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070093 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070095 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070096 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070098 * ->i_pages lock (try_to_unmap_one)
Mel Gormana52633d2016-07-28 15:45:28 -070099 * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
100 * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700102 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600103 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100104 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700105 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600106 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100107 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
109 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800110 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700111 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700114static int page_cache_tree_insert(struct address_space *mapping,
115 struct page *page, void **shadowp)
116{
117 struct radix_tree_node *node;
118 void **slot;
119 int error;
120
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700121 error = __radix_tree_create(&mapping->i_pages, page->index, 0,
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700122 &node, &slot);
123 if (error)
124 return error;
125 if (*slot) {
126 void *p;
127
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700128 p = radix_tree_deref_slot_protected(slot,
129 &mapping->i_pages.xa_lock);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400130 if (!xa_is_value(p))
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700131 return -EEXIST;
132
133 mapping->nrexceptional--;
Ross Zwislerd01ad192017-09-06 16:18:47 -0700134 if (shadowp)
135 *shadowp = p;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700136 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700137 __radix_tree_replace(&mapping->i_pages, node, slot, page,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800138 workingset_lookup_update(mapping));
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700139 mapping->nrpages++;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700140 return 0;
141}
142
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700143static void page_cache_tree_delete(struct address_space *mapping,
144 struct page *page, void *shadow)
145{
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800146 int i, nr;
147
148 /* hugetlb pages are represented by one entry in the radix tree */
149 nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700150
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700151 VM_BUG_ON_PAGE(!PageLocked(page), page);
152 VM_BUG_ON_PAGE(PageTail(page), page);
153 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700154
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700155 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200156 struct radix_tree_node *node;
157 void **slot;
158
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700159 __radix_tree_lookup(&mapping->i_pages, page->index + i,
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200160 &node, &slot);
161
Johannes Weinerdbc446b2016-12-12 16:43:55 -0800162 VM_BUG_ON_PAGE(!node && nr != 1, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700163
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700164 radix_tree_clear_tags(&mapping->i_pages, node, slot);
165 __radix_tree_replace(&mapping->i_pages, node, slot, shadow,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800166 workingset_lookup_update(mapping));
Johannes Weiner449dd692014-04-03 14:47:56 -0700167 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200168
Jan Kara23006382017-11-15 17:37:26 -0800169 page->mapping = NULL;
170 /* Leave page->index set: truncation lookup relies upon it */
171
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200172 if (shadow) {
173 mapping->nrexceptional += nr;
174 /*
175 * Make sure the nrexceptional update is committed before
176 * the nrpages update so that final truncate racing
177 * with reclaim does not see both counters 0 at the
178 * same time and miss a shadow entry.
179 */
180 smp_wmb();
181 }
182 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700183}
184
Jan Kara5ecc4d82017-11-15 17:37:29 -0800185static void unaccount_page_cache_page(struct address_space *mapping,
186 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800188 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600190 /*
191 * if we're uptodate, flush out into the cleancache, otherwise
192 * invalidate any existing cleancache entries. We can't leave
193 * stale data around in the cleancache once our page is gone
194 */
195 if (PageUptodate(page) && PageMappedToDisk(page))
196 cleancache_put_page(page);
197 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400198 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600199
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700200 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800201 VM_BUG_ON_PAGE(page_mapped(page), page);
202 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
203 int mapcount;
204
205 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
206 current->comm, page_to_pfn(page));
207 dump_page(page, "still mapped when deleted");
208 dump_stack();
209 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
210
211 mapcount = page_mapcount(page);
212 if (mapping_exiting(mapping) &&
213 page_count(page) >= mapcount + 2) {
214 /*
215 * All vmas have already been torn down, so it's
216 * a good bet that actually the page is unmapped,
217 * and we'd prefer not to leak it: if we're wrong,
218 * some other bad page check should catch it later.
219 */
220 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700221 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800222 }
223 }
224
Jan Kara76253fb2017-11-15 17:37:22 -0800225 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800226 if (PageHuge(page))
227 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800228
Jan Kara5ecc4d82017-11-15 17:37:29 -0800229 nr = hpage_nr_pages(page);
230
231 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
232 if (PageSwapBacked(page)) {
233 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
234 if (PageTransHuge(page))
235 __dec_node_page_state(page, NR_SHMEM_THPS);
236 } else {
237 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800238 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800239
240 /*
241 * At this point page must be either written or cleaned by
242 * truncate. Dirty page here signals a bug and loss of
243 * unwritten data.
244 *
245 * This fixes dirty accounting after removing the page entirely
246 * but leaves PageDirty set: it has no effect for truncated
247 * page and anyway will be cleared before returning page into
248 * buddy allocator.
249 */
250 if (WARN_ON_ONCE(PageDirty(page)))
251 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
252}
253
254/*
255 * Delete a page from the page cache and free it. Caller has to make
256 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700257 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800258 */
259void __delete_from_page_cache(struct page *page, void *shadow)
260{
261 struct address_space *mapping = page->mapping;
262
263 trace_mm_filemap_delete_from_page_cache(page);
264
265 unaccount_page_cache_page(mapping, page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700266 page_cache_tree_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Jan Kara59c66c52017-11-15 17:37:18 -0800269static void page_cache_free_page(struct address_space *mapping,
270 struct page *page)
271{
272 void (*freepage)(struct page *);
273
274 freepage = mapping->a_ops->freepage;
275 if (freepage)
276 freepage(page);
277
278 if (PageTransHuge(page) && !PageHuge(page)) {
279 page_ref_sub(page, HPAGE_PMD_NR);
280 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
281 } else {
282 put_page(page);
283 }
284}
285
Minchan Kim702cfbf2011-03-22 16:32:43 -0700286/**
287 * delete_from_page_cache - delete page from page cache
288 * @page: the page which the kernel is trying to remove from page cache
289 *
290 * This must be called only on pages that have been verified to be in the page
291 * cache and locked. It will never put the page into the free list, the caller
292 * has a reference on the page.
293 */
294void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700296 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400297 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Matt Mackallcd7619d2005-05-01 08:59:01 -0700299 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700300 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700301 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700302 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500303
Jan Kara59c66c52017-11-15 17:37:18 -0800304 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700305}
306EXPORT_SYMBOL(delete_from_page_cache);
307
Jan Karaaa65c292017-11-15 17:37:33 -0800308/*
309 * page_cache_tree_delete_batch - delete several pages from page cache
310 * @mapping: the mapping to which pages belong
311 * @pvec: pagevec with pages to delete
312 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700313 * The function walks over mapping->i_pages and removes pages passed in @pvec
314 * from the mapping. The function expects @pvec to be sorted by page index.
315 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800316 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700317 * @pvec and takes care to delete all corresponding tail pages from the
318 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800319 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700320 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800321 */
322static void
323page_cache_tree_delete_batch(struct address_space *mapping,
324 struct pagevec *pvec)
325{
326 struct radix_tree_iter iter;
327 void **slot;
328 int total_pages = 0;
329 int i = 0, tail_pages = 0;
330 struct page *page;
331 pgoff_t start;
332
333 start = pvec->pages[0]->index;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700334 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Jan Karaaa65c292017-11-15 17:37:33 -0800335 if (i >= pagevec_count(pvec) && !tail_pages)
336 break;
337 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700338 &mapping->i_pages.xa_lock);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400339 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800340 continue;
341 if (!tail_pages) {
342 /*
343 * Some page got inserted in our range? Skip it. We
344 * have our pages locked so they are protected from
345 * being removed.
346 */
347 if (page != pvec->pages[i])
348 continue;
349 WARN_ON_ONCE(!PageLocked(page));
350 if (PageTransHuge(page) && !PageHuge(page))
351 tail_pages = HPAGE_PMD_NR - 1;
352 page->mapping = NULL;
353 /*
354 * Leave page->index set: truncation lookup relies
355 * upon it
356 */
357 i++;
358 } else {
359 tail_pages--;
360 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700361 radix_tree_clear_tags(&mapping->i_pages, iter.node, slot);
362 __radix_tree_replace(&mapping->i_pages, iter.node, slot, NULL,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800363 workingset_lookup_update(mapping));
Jan Karaaa65c292017-11-15 17:37:33 -0800364 total_pages++;
365 }
366 mapping->nrpages -= total_pages;
367}
368
369void delete_from_page_cache_batch(struct address_space *mapping,
370 struct pagevec *pvec)
371{
372 int i;
373 unsigned long flags;
374
375 if (!pagevec_count(pvec))
376 return;
377
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700378 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800379 for (i = 0; i < pagevec_count(pvec); i++) {
380 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
381
382 unaccount_page_cache_page(mapping, pvec->pages[i]);
383 }
384 page_cache_tree_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700385 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800386
387 for (i = 0; i < pagevec_count(pvec); i++)
388 page_cache_free_page(mapping, pvec->pages[i]);
389}
390
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200391int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700392{
393 int ret = 0;
394 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700395 if (test_bit(AS_ENOSPC, &mapping->flags) &&
396 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700397 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700398 if (test_bit(AS_EIO, &mapping->flags) &&
399 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700400 ret = -EIO;
401 return ret;
402}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200403EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700404
Jeff Layton76341ca2017-07-06 07:02:22 -0400405static int filemap_check_and_keep_errors(struct address_space *mapping)
406{
407 /* Check for outstanding write errors */
408 if (test_bit(AS_EIO, &mapping->flags))
409 return -EIO;
410 if (test_bit(AS_ENOSPC, &mapping->flags))
411 return -ENOSPC;
412 return 0;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700416 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700417 * @mapping: address space structure to write
418 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800419 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700420 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700422 * Start writeback against all of a mapping's dirty pages that lie
423 * within the byte offsets <start, end> inclusive.
424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700426 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * these two operations is that if a dirty page/buffer is encountered, it must
428 * be waited upon, and not just skipped over.
429 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800430int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
431 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int ret;
434 struct writeback_control wbc = {
435 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800436 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700437 .range_start = start,
438 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 };
440
441 if (!mapping_cap_writeback_dirty(mapping))
442 return 0;
443
Tejun Heob16b1de2015-06-02 08:39:48 -0600444 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600446 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ret;
448}
449
450static inline int __filemap_fdatawrite(struct address_space *mapping,
451 int sync_mode)
452{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700453 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456int filemap_fdatawrite(struct address_space *mapping)
457{
458 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
459}
460EXPORT_SYMBOL(filemap_fdatawrite);
461
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400462int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800463 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
466}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400467EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Randy Dunlap485bb992006-06-23 02:03:49 -0700469/**
470 * filemap_flush - mostly a non-blocking flush
471 * @mapping: target address_space
472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * This is a mostly non-blocking flush. Not suitable for data-integrity
474 * purposes - I/O may not be started against all dirty pages.
475 */
476int filemap_flush(struct address_space *mapping)
477{
478 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
479}
480EXPORT_SYMBOL(filemap_flush);
481
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500482/**
483 * filemap_range_has_page - check if a page exists in range.
484 * @mapping: address space within which to check
485 * @start_byte: offset in bytes where the range starts
486 * @end_byte: offset in bytes where the range ends (inclusive)
487 *
488 * Find at least one page in the range supplied, usually used to check if
489 * direct writing in this range will trigger a writeback.
490 */
491bool filemap_range_has_page(struct address_space *mapping,
492 loff_t start_byte, loff_t end_byte)
493{
494 pgoff_t index = start_byte >> PAGE_SHIFT;
495 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700496 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500497
498 if (end_byte < start_byte)
499 return false;
500
501 if (mapping->nrpages == 0)
502 return false;
503
Jan Karaf7b68042017-09-06 16:21:40 -0700504 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500505 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700506 put_page(page);
507 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500508}
509EXPORT_SYMBOL(filemap_range_has_page);
510
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400511static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800512 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300514 pgoff_t index = start_byte >> PAGE_SHIFT;
515 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct pagevec pvec;
517 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200519 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400520 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Mel Gorman86679822017-11-15 17:37:52 -0800522 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800523 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned i;
525
Jan Kara312e9d22017-11-15 17:35:05 -0800526 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800527 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800528 if (!nr_pages)
529 break;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 for (i = 0; i < nr_pages; i++) {
532 struct page *page = pvec.pages[i];
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400535 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 pagevec_release(&pvec);
538 cond_resched();
539 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800540}
541
542/**
543 * filemap_fdatawait_range - wait for writeback to complete
544 * @mapping: address space structure to wait for
545 * @start_byte: offset in bytes where the range starts
546 * @end_byte: offset in bytes where the range ends (inclusive)
547 *
548 * Walk the list of under-writeback pages of the given address space
549 * in the given range and wait for all of them. Check error status of
550 * the address space and return it.
551 *
552 * Since the error status of the address space is cleared by this function,
553 * callers are responsible for checking the return value and handling and/or
554 * reporting the error.
555 */
556int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
557 loff_t end_byte)
558{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400559 __filemap_fdatawait_range(mapping, start_byte, end_byte);
560 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200562EXPORT_SYMBOL(filemap_fdatawait_range);
563
564/**
Jeff Laytona823e452017-07-28 07:24:43 -0400565 * file_fdatawait_range - wait for writeback to complete
566 * @file: file pointing to address space structure to wait for
567 * @start_byte: offset in bytes where the range starts
568 * @end_byte: offset in bytes where the range ends (inclusive)
569 *
570 * Walk the list of under-writeback pages of the address space that file
571 * refers to, in the given range and wait for all of them. Check error
572 * status of the address space vs. the file->f_wb_err cursor and return it.
573 *
574 * Since the error status of the file is advanced by this function,
575 * callers are responsible for checking the return value and handling and/or
576 * reporting the error.
577 */
578int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
579{
580 struct address_space *mapping = file->f_mapping;
581
582 __filemap_fdatawait_range(mapping, start_byte, end_byte);
583 return file_check_and_advance_wb_err(file);
584}
585EXPORT_SYMBOL(file_fdatawait_range);
586
587/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800588 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
589 * @mapping: address space structure to wait for
590 *
591 * Walk the list of under-writeback pages of the given address space
592 * and wait for all of them. Unlike filemap_fdatawait(), this function
593 * does not clear error status of the address space.
594 *
595 * Use this function if callers don't handle errors themselves. Expected
596 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
597 * fsfreeze(8)
598 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400599int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800600{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400601 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400602 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800603}
Jeff Layton76341ca2017-07-06 07:02:22 -0400604EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800605
Jeff Layton9326c9b2017-07-26 10:21:11 -0400606static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400608 return (!dax_mapping(mapping) && mapping->nrpages) ||
609 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612int filemap_write_and_wait(struct address_space *mapping)
613{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800614 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Jeff Layton9326c9b2017-07-26 10:21:11 -0400616 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800617 err = filemap_fdatawrite(mapping);
618 /*
619 * Even if the above returned error, the pages may be
620 * written partially (e.g. -ENOSPC), so we wait for it.
621 * But the -EIO is special case, it may indicate the worst
622 * thing (e.g. bug) happened, so we avoid waiting for it.
623 */
624 if (err != -EIO) {
625 int err2 = filemap_fdatawait(mapping);
626 if (!err)
627 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400628 } else {
629 /* Clear any previously stored errors */
630 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800631 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700632 } else {
633 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800637EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Randy Dunlap485bb992006-06-23 02:03:49 -0700639/**
640 * filemap_write_and_wait_range - write out & wait on a file range
641 * @mapping: the address_space for the pages
642 * @lstart: offset in bytes where the range starts
643 * @lend: offset in bytes where the range ends (inclusive)
644 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800645 * Write out and wait upon file offsets lstart->lend, inclusive.
646 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300647 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800648 * that this function can be used to write to the very end-of-file (end = -1).
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650int filemap_write_and_wait_range(struct address_space *mapping,
651 loff_t lstart, loff_t lend)
652{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800653 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Jeff Layton9326c9b2017-07-26 10:21:11 -0400655 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800656 err = __filemap_fdatawrite_range(mapping, lstart, lend,
657 WB_SYNC_ALL);
658 /* See comment of filemap_write_and_wait() */
659 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200660 int err2 = filemap_fdatawait_range(mapping,
661 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800662 if (!err)
663 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400664 } else {
665 /* Clear any previously stored errors */
666 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800667 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700668 } else {
669 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
Chris Masonf6995582009-04-15 13:22:37 -0400673EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jeff Layton5660e132017-07-06 07:02:25 -0400675void __filemap_set_wb_err(struct address_space *mapping, int err)
676{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400677 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400678
679 trace_filemap_set_wb_err(mapping, eseq);
680}
681EXPORT_SYMBOL(__filemap_set_wb_err);
682
683/**
684 * file_check_and_advance_wb_err - report wb error (if any) that was previously
685 * and advance wb_err to current one
686 * @file: struct file on which the error is being reported
687 *
688 * When userland calls fsync (or something like nfsd does the equivalent), we
689 * want to report any writeback errors that occurred since the last fsync (or
690 * since the file was opened if there haven't been any).
691 *
692 * Grab the wb_err from the mapping. If it matches what we have in the file,
693 * then just quickly return 0. The file is all caught up.
694 *
695 * If it doesn't match, then take the mapping value, set the "seen" flag in
696 * it and try to swap it into place. If it works, or another task beat us
697 * to it with the new value, then update the f_wb_err and return the error
698 * portion. The error at this point must be reported via proper channels
699 * (a'la fsync, or NFS COMMIT operation, etc.).
700 *
701 * While we handle mapping->wb_err with atomic operations, the f_wb_err
702 * value is protected by the f_lock since we must ensure that it reflects
703 * the latest value swapped in for this file descriptor.
704 */
705int file_check_and_advance_wb_err(struct file *file)
706{
707 int err = 0;
708 errseq_t old = READ_ONCE(file->f_wb_err);
709 struct address_space *mapping = file->f_mapping;
710
711 /* Locklessly handle the common case where nothing has changed */
712 if (errseq_check(&mapping->wb_err, old)) {
713 /* Something changed, must use slow path */
714 spin_lock(&file->f_lock);
715 old = file->f_wb_err;
716 err = errseq_check_and_advance(&mapping->wb_err,
717 &file->f_wb_err);
718 trace_file_check_and_advance_wb_err(file, old);
719 spin_unlock(&file->f_lock);
720 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700721
722 /*
723 * We're mostly using this function as a drop in replacement for
724 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
725 * that the legacy code would have had on these flags.
726 */
727 clear_bit(AS_EIO, &mapping->flags);
728 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400729 return err;
730}
731EXPORT_SYMBOL(file_check_and_advance_wb_err);
732
733/**
734 * file_write_and_wait_range - write out & wait on a file range
735 * @file: file pointing to address_space with pages
736 * @lstart: offset in bytes where the range starts
737 * @lend: offset in bytes where the range ends (inclusive)
738 *
739 * Write out and wait upon file offsets lstart->lend, inclusive.
740 *
741 * Note that @lend is inclusive (describes the last byte to be written) so
742 * that this function can be used to write to the very end-of-file (end = -1).
743 *
744 * After writing out and waiting on the data, we check and advance the
745 * f_wb_err cursor to the latest value, and return any errors detected there.
746 */
747int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
748{
749 int err = 0, err2;
750 struct address_space *mapping = file->f_mapping;
751
Jeff Layton9326c9b2017-07-26 10:21:11 -0400752 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400753 err = __filemap_fdatawrite_range(mapping, lstart, lend,
754 WB_SYNC_ALL);
755 /* See comment of filemap_write_and_wait() */
756 if (err != -EIO)
757 __filemap_fdatawait_range(mapping, lstart, lend);
758 }
759 err2 = file_check_and_advance_wb_err(file);
760 if (!err)
761 err = err2;
762 return err;
763}
764EXPORT_SYMBOL(file_write_and_wait_range);
765
Randy Dunlap485bb992006-06-23 02:03:49 -0700766/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700767 * replace_page_cache_page - replace a pagecache page with a new one
768 * @old: page to be replaced
769 * @new: page to replace with
770 * @gfp_mask: allocation mode
771 *
772 * This function replaces a page in the pagecache with a new one. On
773 * success it acquires the pagecache reference for the new page and
774 * drops it for the old page. Both the old and new pages must be
775 * locked. This function does not add the new page to the LRU, the
776 * caller must do that.
777 *
778 * The remove + add is atomic. The only way this function can fail is
779 * memory allocation failure.
780 */
781int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
782{
783 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700784
Sasha Levin309381fea2014-01-23 15:52:54 -0800785 VM_BUG_ON_PAGE(!PageLocked(old), old);
786 VM_BUG_ON_PAGE(!PageLocked(new), new);
787 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700788
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700789 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700790 if (!error) {
791 struct address_space *mapping = old->mapping;
792 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400793 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700794
795 pgoff_t offset = old->index;
796 freepage = mapping->a_ops->freepage;
797
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300798 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700799 new->mapping = mapping;
800 new->index = offset;
801
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700802 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700803 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700804 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700805 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700806
807 /*
808 * hugetlb pages do not participate in page cache accounting.
809 */
810 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700811 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700812 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700813 __inc_node_page_state(new, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700814 xa_unlock_irqrestore(&mapping->i_pages, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700815 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700816 radix_tree_preload_end();
817 if (freepage)
818 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300819 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700820 }
821
822 return error;
823}
824EXPORT_SYMBOL_GPL(replace_page_cache_page);
825
Johannes Weinera5289102014-04-03 14:47:51 -0700826static int __add_to_page_cache_locked(struct page *page,
827 struct address_space *mapping,
828 pgoff_t offset, gfp_t gfp_mask,
829 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Johannes Weiner00501b52014-08-08 14:19:20 -0700831 int huge = PageHuge(page);
832 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700833 int error;
834
Sasha Levin309381fea2014-01-23 15:52:54 -0800835 VM_BUG_ON_PAGE(!PageLocked(page), page);
836 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700837
Johannes Weiner00501b52014-08-08 14:19:20 -0700838 if (!huge) {
839 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800840 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700841 if (error)
842 return error;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700845 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700846 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700847 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800848 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700849 return error;
850 }
851
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300852 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700853 page->mapping = mapping;
854 page->index = offset;
855
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700856 xa_lock_irq(&mapping->i_pages);
Johannes Weinera5289102014-04-03 14:47:51 -0700857 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700858 radix_tree_preload_end();
859 if (unlikely(error))
860 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700861
862 /* hugetlb pages do not participate in page cache accounting. */
863 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700864 __inc_node_page_state(page, NR_FILE_PAGES);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700865 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700866 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800867 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700868 trace_mm_filemap_add_to_page_cache(page);
869 return 0;
870err_insert:
871 page->mapping = NULL;
872 /* Leave page->index set: truncation relies upon it */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700873 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700874 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800875 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300876 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return error;
878}
Johannes Weinera5289102014-04-03 14:47:51 -0700879
880/**
881 * add_to_page_cache_locked - add a locked page to the pagecache
882 * @page: page to add
883 * @mapping: the page's address_space
884 * @offset: page index
885 * @gfp_mask: page allocation mode
886 *
887 * This function is used to add a page to the pagecache. It must be locked.
888 * This function does not add the page to the LRU. The caller must do that.
889 */
890int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
891 pgoff_t offset, gfp_t gfp_mask)
892{
893 return __add_to_page_cache_locked(page, mapping, offset,
894 gfp_mask, NULL);
895}
Nick Piggine2867812008-07-25 19:45:30 -0700896EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400899 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Johannes Weinera5289102014-04-03 14:47:51 -0700901 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700902 int ret;
903
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800904 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700905 ret = __add_to_page_cache_locked(page, mapping, offset,
906 gfp_mask, &shadow);
907 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800908 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700909 else {
910 /*
911 * The page might have been evicted from cache only
912 * recently, in which case it should be activated like
913 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700914 * The exception is pages getting rewritten; evicting other
915 * data from the working set, only to cache data that will
916 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700917 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700918 if (!(gfp_mask & __GFP_WRITE) &&
919 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700920 SetPageActive(page);
921 workingset_activation(page);
922 } else
923 ClearPageActive(page);
924 lru_cache_add(page);
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return ret;
927}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300928EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Paul Jackson44110fe2006-03-24 03:16:04 -0800930#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700931struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800932{
Miao Xiec0ff7452010-05-24 14:32:08 -0700933 int n;
934 struct page *page;
935
Paul Jackson44110fe2006-03-24 03:16:04 -0800936 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700937 unsigned int cpuset_mems_cookie;
938 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700939 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700940 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700941 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700942 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700943
Miao Xiec0ff7452010-05-24 14:32:08 -0700944 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800945 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700946 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800947}
Nick Piggin2ae88142006-10-28 10:38:23 -0700948EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800949#endif
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * In order to wait for pages to become available there must be
953 * waitqueues associated with pages. By using a hash table of
954 * waitqueues where the bucket discipline is to maintain all
955 * waiters on the same queue and wake all when any of the pages
956 * become available, and for the woken contexts to check to be
957 * sure the appropriate page became available, this saves space
958 * at a cost of "thundering herd" phenomena during rare hash
959 * collisions.
960 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000961#define PAGE_WAIT_TABLE_BITS 8
962#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
963static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
964
965static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Nicholas Piggin62906022016-12-25 13:00:30 +1000967 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
Nicholas Piggin62906022016-12-25 13:00:30 +1000969
970void __init pagecache_init(void)
971{
972 int i;
973
974 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
975 init_waitqueue_head(&page_wait_table[i]);
976
977 page_writeback_init();
978}
979
Linus Torvalds3510ca22017-08-27 13:55:12 -0700980/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000981struct wait_page_key {
982 struct page *page;
983 int bit_nr;
984 int page_match;
985};
986
987struct wait_page_queue {
988 struct page *page;
989 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200990 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000991};
992
Ingo Molnarac6424b2017-06-20 12:06:13 +0200993static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000994{
995 struct wait_page_key *key = arg;
996 struct wait_page_queue *wait_page
997 = container_of(wait, struct wait_page_queue, wait);
998
999 if (wait_page->page != key->page)
1000 return 0;
1001 key->page_match = 1;
1002
1003 if (wait_page->bit_nr != key->bit_nr)
1004 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001005
1006 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +10001007 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001008 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001009
1010 return autoremove_wake_function(wait, mode, sync, key);
1011}
1012
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001013static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001014{
1015 wait_queue_head_t *q = page_waitqueue(page);
1016 struct wait_page_key key;
1017 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001018 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001019
1020 key.page = page;
1021 key.bit_nr = bit_nr;
1022 key.page_match = 0;
1023
Tim Chen11a19c72017-08-25 09:13:55 -07001024 bookmark.flags = 0;
1025 bookmark.private = NULL;
1026 bookmark.func = NULL;
1027 INIT_LIST_HEAD(&bookmark.entry);
1028
Nicholas Piggin62906022016-12-25 13:00:30 +10001029 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001030 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1031
1032 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1033 /*
1034 * Take a breather from holding the lock,
1035 * allow pages that finish wake up asynchronously
1036 * to acquire the lock and remove themselves
1037 * from wait queue
1038 */
1039 spin_unlock_irqrestore(&q->lock, flags);
1040 cpu_relax();
1041 spin_lock_irqsave(&q->lock, flags);
1042 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1043 }
1044
Nicholas Piggin62906022016-12-25 13:00:30 +10001045 /*
1046 * It is possible for other pages to have collided on the waitqueue
1047 * hash, so in that case check for a page match. That prevents a long-
1048 * term waiter
1049 *
1050 * It is still possible to miss a case here, when we woke page waiters
1051 * and removed them from the waitqueue, but there are still other
1052 * page waiters.
1053 */
1054 if (!waitqueue_active(q) || !key.page_match) {
1055 ClearPageWaiters(page);
1056 /*
1057 * It's possible to miss clearing Waiters here, when we woke
1058 * our page waiters, but the hashed waitqueue has waiters for
1059 * other pages on it.
1060 *
1061 * That's okay, it's a rare case. The next waker will clear it.
1062 */
1063 }
1064 spin_unlock_irqrestore(&q->lock, flags);
1065}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001066
1067static void wake_up_page(struct page *page, int bit)
1068{
1069 if (!PageWaiters(page))
1070 return;
1071 wake_up_page_bit(page, bit);
1072}
Nicholas Piggin62906022016-12-25 13:00:30 +10001073
1074static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1075 struct page *page, int bit_nr, int state, bool lock)
1076{
1077 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001078 wait_queue_entry_t *wait = &wait_page.wait;
Nicholas Piggin62906022016-12-25 13:00:30 +10001079 int ret = 0;
1080
1081 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001082 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001083 wait->func = wake_page_function;
1084 wait_page.page = page;
1085 wait_page.bit_nr = bit_nr;
1086
1087 for (;;) {
1088 spin_lock_irq(&q->lock);
1089
Ingo Molnar2055da92017-06-20 12:06:46 +02001090 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001091 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001092 SetPageWaiters(page);
1093 }
1094
1095 set_current_state(state);
1096
1097 spin_unlock_irq(&q->lock);
1098
1099 if (likely(test_bit(bit_nr, &page->flags))) {
1100 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001101 }
1102
1103 if (lock) {
1104 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1105 break;
1106 } else {
1107 if (!test_bit(bit_nr, &page->flags))
1108 break;
1109 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001110
1111 if (unlikely(signal_pending_state(state, current))) {
1112 ret = -EINTR;
1113 break;
1114 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001115 }
1116
1117 finish_wait(q, wait);
1118
1119 /*
1120 * A signal could leave PageWaiters set. Clearing it here if
1121 * !waitqueue_active would be possible (by open-coding finish_wait),
1122 * but still fail to catch it in the case of wait hash collision. We
1123 * already can fail to clear wait hash collision cases, so don't
1124 * bother with signals either.
1125 */
1126
1127 return ret;
1128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Harvey Harrison920c7a52008-02-04 22:29:26 -08001130void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Nicholas Piggin62906022016-12-25 13:00:30 +10001132 wait_queue_head_t *q = page_waitqueue(page);
1133 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135EXPORT_SYMBOL(wait_on_page_bit);
1136
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001137int wait_on_page_bit_killable(struct page *page, int bit_nr)
1138{
Nicholas Piggin62906022016-12-25 13:00:30 +10001139 wait_queue_head_t *q = page_waitqueue(page);
1140 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001141}
David Howells4343d002017-11-02 15:27:52 +00001142EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001145 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001146 * @page: Page defining the wait queue of interest
1147 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001148 *
1149 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1150 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001151void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001152{
1153 wait_queue_head_t *q = page_waitqueue(page);
1154 unsigned long flags;
1155
1156 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001157 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001158 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001159 spin_unlock_irqrestore(&q->lock, flags);
1160}
1161EXPORT_SYMBOL_GPL(add_page_wait_queue);
1162
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001163#ifndef clear_bit_unlock_is_negative_byte
1164
1165/*
1166 * PG_waiters is the high bit in the same byte as PG_lock.
1167 *
1168 * On x86 (and on many other architectures), we can clear PG_lock and
1169 * test the sign bit at the same time. But if the architecture does
1170 * not support that special operation, we just do this all by hand
1171 * instead.
1172 *
1173 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1174 * being cleared, but a memory barrier should be unneccssary since it is
1175 * in the same byte as PG_locked.
1176 */
1177static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1178{
1179 clear_bit_unlock(nr, mem);
1180 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001181 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001182}
1183
1184#endif
1185
David Howells385e1ca5f2009-04-03 16:42:39 +01001186/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001187 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * @page: the page
1189 *
1190 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1191 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001192 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1194 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001195 * Note that this depends on PG_waiters being the sign bit in the byte
1196 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1197 * clear the PG_locked bit and test PG_waiters at the same time fairly
1198 * portably (architectures that do LL/SC can test any bit, while x86 can
1199 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001201void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001203 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001204 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001205 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001206 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1207 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209EXPORT_SYMBOL(unlock_page);
1210
Randy Dunlap485bb992006-06-23 02:03:49 -07001211/**
1212 * end_page_writeback - end writeback against a page
1213 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
1215void end_page_writeback(struct page *page)
1216{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001217 /*
1218 * TestClearPageReclaim could be used here but it is an atomic
1219 * operation and overkill in this particular case. Failing to
1220 * shuffle a page marked for immediate reclaim is too mild to
1221 * justify taking an atomic operation penalty at the end of
1222 * ever page writeback.
1223 */
1224 if (PageReclaim(page)) {
1225 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001226 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001227 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001228
1229 if (!test_clear_page_writeback(page))
1230 BUG();
1231
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001232 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 wake_up_page(page, PG_writeback);
1234}
1235EXPORT_SYMBOL(end_page_writeback);
1236
Matthew Wilcox57d99842014-06-04 16:07:45 -07001237/*
1238 * After completing I/O on a page, call this routine to update the page
1239 * flags appropriately
1240 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001241void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001242{
Jens Axboec11f0c02016-08-05 08:11:04 -06001243 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001244 if (!err) {
1245 SetPageUptodate(page);
1246 } else {
1247 ClearPageUptodate(page);
1248 SetPageError(page);
1249 }
1250 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001251 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001252 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001253 struct address_space *mapping;
1254
Matthew Wilcox57d99842014-06-04 16:07:45 -07001255 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001256 mapping = page_mapping(page);
1257 if (mapping)
1258 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001259 }
1260 end_page_writeback(page);
1261 }
1262}
1263EXPORT_SYMBOL_GPL(page_endio);
1264
Randy Dunlap485bb992006-06-23 02:03:49 -07001265/**
1266 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001267 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001269void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Nicholas Piggin62906022016-12-25 13:00:30 +10001271 struct page *page = compound_head(__page);
1272 wait_queue_head_t *q = page_waitqueue(page);
1273 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275EXPORT_SYMBOL(__lock_page);
1276
Nicholas Piggin62906022016-12-25 13:00:30 +10001277int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001278{
Nicholas Piggin62906022016-12-25 13:00:30 +10001279 struct page *page = compound_head(__page);
1280 wait_queue_head_t *q = page_waitqueue(page);
1281 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001282}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001283EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001284
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001285/*
1286 * Return values:
1287 * 1 - page is locked; mmap_sem is still held.
1288 * 0 - page is not locked.
1289 * mmap_sem has been released (up_read()), unless flags had both
1290 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1291 * which case mmap_sem is still held.
1292 *
1293 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1294 * with the page locked and the mmap_sem unperturbed.
1295 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001296int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1297 unsigned int flags)
1298{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001299 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1300 /*
1301 * CAUTION! In this case, mmap_sem is not released
1302 * even though return 0.
1303 */
1304 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1305 return 0;
1306
1307 up_read(&mm->mmap_sem);
1308 if (flags & FAULT_FLAG_KILLABLE)
1309 wait_on_page_locked_killable(page);
1310 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001311 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001312 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001313 } else {
1314 if (flags & FAULT_FLAG_KILLABLE) {
1315 int ret;
1316
1317 ret = __lock_page_killable(page);
1318 if (ret) {
1319 up_read(&mm->mmap_sem);
1320 return 0;
1321 }
1322 } else
1323 __lock_page(page);
1324 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001325 }
1326}
1327
Randy Dunlap485bb992006-06-23 02:03:49 -07001328/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001329 * page_cache_next_miss() - Find the next gap in the page cache.
1330 * @mapping: Mapping.
1331 * @index: Index.
1332 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001333 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001334 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1335 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001336 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001337 * This function may be called under the rcu_read_lock. However, this will
1338 * not atomically search a snapshot of the cache at a single point in time.
1339 * For example, if a gap is created at index 5, then subsequently a gap is
1340 * created at index 10, page_cache_next_miss covering both indices may
1341 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001342 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001343 * Return: The index of the gap if found, otherwise an index outside the
1344 * range specified (in which case 'return - index >= max_scan' will be true).
1345 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001346 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001347pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001348 pgoff_t index, unsigned long max_scan)
1349{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001350 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001351
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001352 while (max_scan--) {
1353 void *entry = xas_next(&xas);
1354 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001355 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001356 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001357 break;
1358 }
1359
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001360 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001361}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001362EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001363
1364/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001365 * page_cache_prev_miss() - Find the next gap in the page cache.
1366 * @mapping: Mapping.
1367 * @index: Index.
1368 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001369 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001370 * Search the range [max(index - max_scan + 1, 0), index] for the
1371 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001372 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001373 * This function may be called under the rcu_read_lock. However, this will
1374 * not atomically search a snapshot of the cache at a single point in time.
1375 * For example, if a gap is created at index 10, then subsequently a gap is
1376 * created at index 5, page_cache_prev_miss() covering both indices may
1377 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001378 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001379 * Return: The index of the gap if found, otherwise an index outside the
1380 * range specified (in which case 'index - return >= max_scan' will be true).
1381 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001382 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001383pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001384 pgoff_t index, unsigned long max_scan)
1385{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001386 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001387
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001388 while (max_scan--) {
1389 void *entry = xas_prev(&xas);
1390 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001391 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001392 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001393 break;
1394 }
1395
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001396 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001397}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001398EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001399
1400/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001401 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001402 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001403 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001404 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001405 * Looks up the page cache slot at @mapping & @offset. If there is a
1406 * page cache page, it is returned with an increased refcount.
1407 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001408 * If the slot holds a shadow entry of a previously evicted page, or a
1409 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001410 *
1411 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001413struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Nick Piggina60637c2008-07-25 19:45:31 -07001415 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001416 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Nick Piggina60637c2008-07-25 19:45:31 -07001418 rcu_read_lock();
1419repeat:
1420 page = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001421 pagep = radix_tree_lookup_slot(&mapping->i_pages, offset);
Nick Piggina60637c2008-07-25 19:45:31 -07001422 if (pagep) {
1423 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001424 if (unlikely(!page))
1425 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001426 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001427 if (radix_tree_deref_retry(page))
1428 goto repeat;
1429 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001430 * A shadow entry of a recently evicted page,
1431 * or a swap entry from shmem/tmpfs. Return
1432 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001433 */
1434 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001435 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001436
1437 head = compound_head(page);
1438 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001439 goto repeat;
1440
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001441 /* The page was split under us? */
1442 if (compound_head(page) != head) {
1443 put_page(head);
1444 goto repeat;
1445 }
1446
Nick Piggina60637c2008-07-25 19:45:31 -07001447 /*
1448 * Has the page moved?
1449 * This is part of the lockless pagecache protocol. See
1450 * include/linux/pagemap.h for details.
1451 */
1452 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001453 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001454 goto repeat;
1455 }
1456 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001457out:
Nick Piggina60637c2008-07-25 19:45:31 -07001458 rcu_read_unlock();
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return page;
1461}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001462EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Randy Dunlap485bb992006-06-23 02:03:49 -07001464/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001465 * find_lock_entry - locate, pin and lock a page cache entry
1466 * @mapping: the address_space to search
1467 * @offset: the page cache index
1468 *
1469 * Looks up the page cache slot at @mapping & @offset. If there is a
1470 * page cache page, it is returned locked and with an increased
1471 * refcount.
1472 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001473 * If the slot holds a shadow entry of a previously evicted page, or a
1474 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001475 *
1476 * Otherwise, %NULL is returned.
1477 *
1478 * find_lock_entry() may sleep.
1479 */
1480struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct page *page;
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001485 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001486 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001487 lock_page(page);
1488 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001489 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001490 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001491 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001492 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001494 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return page;
1497}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001498EXPORT_SYMBOL(find_lock_entry);
1499
1500/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001501 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001502 * @mapping: the address_space to search
1503 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001504 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001505 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001506 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001507 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001508 *
Randy Dunlap75325182014-07-30 16:08:37 -07001509 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001510 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001511 * @fgp_flags can be:
1512 *
1513 * - FGP_ACCESSED: the page will be marked accessed
1514 * - FGP_LOCK: Page is return locked
1515 * - FGP_CREAT: If page is not present then a new page is allocated using
1516 * @gfp_mask and added to the page cache and the VM's LRU
1517 * list. The page is returned locked and with an increased
1518 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001519 *
1520 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1521 * if the GFP flags specified for FGP_CREAT are atomic.
1522 *
1523 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001524 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001525struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001526 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Nick Piggineb2be182007-10-16 01:24:57 -07001528 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001531 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001532 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001533 page = NULL;
1534 if (!page)
1535 goto no_page;
1536
1537 if (fgp_flags & FGP_LOCK) {
1538 if (fgp_flags & FGP_NOWAIT) {
1539 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001540 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001541 return NULL;
1542 }
1543 } else {
1544 lock_page(page);
1545 }
1546
1547 /* Has the page been truncated? */
1548 if (unlikely(page->mapping != mapping)) {
1549 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001550 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001551 goto repeat;
1552 }
1553 VM_BUG_ON_PAGE(page->index != offset, page);
1554 }
1555
1556 if (page && (fgp_flags & FGP_ACCESSED))
1557 mark_page_accessed(page);
1558
1559no_page:
1560 if (!page && (fgp_flags & FGP_CREAT)) {
1561 int err;
1562 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001563 gfp_mask |= __GFP_WRITE;
1564 if (fgp_flags & FGP_NOFS)
1565 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001566
Michal Hocko45f87de2014-12-29 20:30:35 +01001567 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001568 if (!page)
1569 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001570
1571 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1572 fgp_flags |= FGP_LOCK;
1573
Hugh Dickinseb39d612014-08-06 16:06:43 -07001574 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001575 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001576 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001577
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001578 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001579 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001580 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001581 page = NULL;
1582 if (err == -EEXIST)
1583 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return page;
1588}
Mel Gorman2457aec2014-06-04 16:10:31 -07001589EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001592 * find_get_entries - gang pagecache lookup
1593 * @mapping: The address_space to search
1594 * @start: The starting page cache index
1595 * @nr_entries: The maximum number of entries
1596 * @entries: Where the resulting entries are placed
1597 * @indices: The cache indices corresponding to the entries in @entries
1598 *
1599 * find_get_entries() will search for and return a group of up to
1600 * @nr_entries entries in the mapping. The entries are placed at
1601 * @entries. find_get_entries() takes a reference against any actual
1602 * pages it returns.
1603 *
1604 * The search returns a group of mapping-contiguous page cache entries
1605 * with ascending indexes. There may be holes in the indices due to
1606 * not-present pages.
1607 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001608 * Any shadow entries of evicted pages, or swap entries from
1609 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001610 *
1611 * find_get_entries() returns the number of pages and shadow entries
1612 * which were found.
1613 */
1614unsigned find_get_entries(struct address_space *mapping,
1615 pgoff_t start, unsigned int nr_entries,
1616 struct page **entries, pgoff_t *indices)
1617{
1618 void **slot;
1619 unsigned int ret = 0;
1620 struct radix_tree_iter iter;
1621
1622 if (!nr_entries)
1623 return 0;
1624
1625 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001626 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001627 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001628repeat:
1629 page = radix_tree_deref_slot(slot);
1630 if (unlikely(!page))
1631 continue;
1632 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001633 if (radix_tree_deref_retry(page)) {
1634 slot = radix_tree_iter_retry(&iter);
1635 continue;
1636 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001637 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001638 * A shadow entry of a recently evicted page, a swap
1639 * entry from shmem/tmpfs or a DAX entry. Return it
1640 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001641 */
1642 goto export;
1643 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001644
1645 head = compound_head(page);
1646 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001647 goto repeat;
1648
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001649 /* The page was split under us? */
1650 if (compound_head(page) != head) {
1651 put_page(head);
1652 goto repeat;
1653 }
1654
Johannes Weiner0cd61442014-04-03 14:47:46 -07001655 /* Has the page moved? */
1656 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001657 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001658 goto repeat;
1659 }
1660export:
1661 indices[ret] = iter.index;
1662 entries[ret] = page;
1663 if (++ret == nr_entries)
1664 break;
1665 }
1666 rcu_read_unlock();
1667 return ret;
1668}
1669
1670/**
Jan Karab947cee2017-09-06 16:21:21 -07001671 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * @mapping: The address_space to search
1673 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001674 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * @nr_pages: The maximum number of pages
1676 * @pages: Where the resulting pages are placed
1677 *
Jan Karab947cee2017-09-06 16:21:21 -07001678 * find_get_pages_range() will search for and return a group of up to @nr_pages
1679 * pages in the mapping starting at index @start and up to index @end
1680 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1681 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 *
1683 * The search returns a group of mapping-contiguous pages with ascending
1684 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001685 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *
Jan Karab947cee2017-09-06 16:21:21 -07001687 * find_get_pages_range() returns the number of pages which were found. If this
1688 * number is smaller than @nr_pages, the end of specified range has been
1689 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 */
Jan Karab947cee2017-09-06 16:21:21 -07001691unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1692 pgoff_t end, unsigned int nr_pages,
1693 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001695 struct radix_tree_iter iter;
1696 void **slot;
1697 unsigned ret = 0;
1698
1699 if (unlikely(!nr_pages))
1700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Nick Piggina60637c2008-07-25 19:45:31 -07001702 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001703 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001704 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001705
1706 if (iter.index > end)
1707 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001708repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001709 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001710 if (unlikely(!page))
1711 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001712
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001713 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001714 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001715 slot = radix_tree_iter_retry(&iter);
1716 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001717 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001718 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001719 * A shadow entry of a recently evicted page,
1720 * or a swap entry from shmem/tmpfs. Skip
1721 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001722 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001723 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001724 }
Nick Piggina60637c2008-07-25 19:45:31 -07001725
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001726 head = compound_head(page);
1727 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001728 goto repeat;
1729
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001730 /* The page was split under us? */
1731 if (compound_head(page) != head) {
1732 put_page(head);
1733 goto repeat;
1734 }
1735
Nick Piggina60637c2008-07-25 19:45:31 -07001736 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001737 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001738 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001739 goto repeat;
1740 }
1741
1742 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001743 if (++ret == nr_pages) {
1744 *start = pages[ret - 1]->index + 1;
1745 goto out;
1746 }
Nick Piggina60637c2008-07-25 19:45:31 -07001747 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001748
Jan Karab947cee2017-09-06 16:21:21 -07001749 /*
1750 * We come here when there is no page beyond @end. We take care to not
1751 * overflow the index @start as it confuses some of the callers. This
1752 * breaks the iteration when there is page at index -1 but that is
1753 * already broken anyway.
1754 */
1755 if (end == (pgoff_t)-1)
1756 *start = (pgoff_t)-1;
1757 else
1758 *start = end + 1;
1759out:
Nick Piggina60637c2008-07-25 19:45:31 -07001760 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return ret;
1763}
1764
Jens Axboeebf43502006-04-27 08:46:01 +02001765/**
1766 * find_get_pages_contig - gang contiguous pagecache lookup
1767 * @mapping: The address_space to search
1768 * @index: The starting page index
1769 * @nr_pages: The maximum number of pages
1770 * @pages: Where the resulting pages are placed
1771 *
1772 * find_get_pages_contig() works exactly like find_get_pages(), except
1773 * that the returned number of pages are guaranteed to be contiguous.
1774 *
1775 * find_get_pages_contig() returns the number of pages which were found.
1776 */
1777unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1778 unsigned int nr_pages, struct page **pages)
1779{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001780 struct radix_tree_iter iter;
1781 void **slot;
1782 unsigned int ret = 0;
1783
1784 if (unlikely(!nr_pages))
1785 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001786
Nick Piggina60637c2008-07-25 19:45:31 -07001787 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001788 radix_tree_for_each_contig(slot, &mapping->i_pages, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001789 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001790repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001791 page = radix_tree_deref_slot(slot);
1792 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001793 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001794 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001795
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001796 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001797 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001798 slot = radix_tree_iter_retry(&iter);
1799 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001800 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001801 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001802 * A shadow entry of a recently evicted page,
1803 * or a swap entry from shmem/tmpfs. Stop
1804 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001805 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001806 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001807 }
Nick Piggina60637c2008-07-25 19:45:31 -07001808
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001809 head = compound_head(page);
1810 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001811 goto repeat;
1812
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001813 /* The page was split under us? */
1814 if (compound_head(page) != head) {
1815 put_page(head);
1816 goto repeat;
1817 }
1818
Nick Piggina60637c2008-07-25 19:45:31 -07001819 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001820 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001821 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001822 goto repeat;
1823 }
1824
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001825 /*
1826 * must check mapping and index after taking the ref.
1827 * otherwise we can get both false positives and false
1828 * negatives, which is just confusing to the caller.
1829 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001830 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001831 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001832 break;
1833 }
1834
Nick Piggina60637c2008-07-25 19:45:31 -07001835 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001836 if (++ret == nr_pages)
1837 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001838 }
Nick Piggina60637c2008-07-25 19:45:31 -07001839 rcu_read_unlock();
1840 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001841}
David Howellsef71c152007-05-09 02:33:44 -07001842EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001843
Randy Dunlap485bb992006-06-23 02:03:49 -07001844/**
Jan Kara72b045a2017-11-15 17:34:33 -08001845 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001846 * @mapping: the address_space to search
1847 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001848 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001849 * @tag: the tag index
1850 * @nr_pages: the maximum number of pages
1851 * @pages: where the resulting pages are placed
1852 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001854 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
Jan Kara72b045a2017-11-15 17:34:33 -08001856unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1857 pgoff_t end, int tag, unsigned int nr_pages,
1858 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001860 struct radix_tree_iter iter;
1861 void **slot;
1862 unsigned ret = 0;
1863
1864 if (unlikely(!nr_pages))
1865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Nick Piggina60637c2008-07-25 19:45:31 -07001867 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001868 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001869 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001870
1871 if (iter.index > end)
1872 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001873repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001874 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001875 if (unlikely(!page))
1876 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001877
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001878 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001879 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001880 slot = radix_tree_iter_retry(&iter);
1881 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001882 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001883 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001884 * A shadow entry of a recently evicted page.
1885 *
1886 * Those entries should never be tagged, but
1887 * this tree walk is lockless and the tags are
1888 * looked up in bulk, one radix tree node at a
1889 * time, so there is a sizable window for page
1890 * reclaim to evict a page we saw tagged.
1891 *
1892 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001893 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001894 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001895 }
Nick Piggina60637c2008-07-25 19:45:31 -07001896
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001897 head = compound_head(page);
1898 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001899 goto repeat;
1900
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001901 /* The page was split under us? */
1902 if (compound_head(page) != head) {
1903 put_page(head);
1904 goto repeat;
1905 }
1906
Nick Piggina60637c2008-07-25 19:45:31 -07001907 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001908 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001909 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001910 goto repeat;
1911 }
1912
1913 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001914 if (++ret == nr_pages) {
1915 *index = pages[ret - 1]->index + 1;
1916 goto out;
1917 }
Nick Piggina60637c2008-07-25 19:45:31 -07001918 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001919
Jan Kara72b045a2017-11-15 17:34:33 -08001920 /*
1921 * We come here when we got at @end. We take care to not overflow the
1922 * index @index as it confuses some of the callers. This breaks the
1923 * iteration when there is page at index -1 but that is already broken
1924 * anyway.
1925 */
1926 if (end == (pgoff_t)-1)
1927 *index = (pgoff_t)-1;
1928 else
1929 *index = end + 1;
1930out:
Nick Piggina60637c2008-07-25 19:45:31 -07001931 rcu_read_unlock();
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return ret;
1934}
Jan Kara72b045a2017-11-15 17:34:33 -08001935EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001937/**
1938 * find_get_entries_tag - find and return entries that match @tag
1939 * @mapping: the address_space to search
1940 * @start: the starting page cache index
1941 * @tag: the tag index
1942 * @nr_entries: the maximum number of entries
1943 * @entries: where the resulting entries are placed
1944 * @indices: the cache indices corresponding to the entries in @entries
1945 *
1946 * Like find_get_entries, except we only return entries which are tagged with
1947 * @tag.
1948 */
1949unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1950 int tag, unsigned int nr_entries,
1951 struct page **entries, pgoff_t *indices)
1952{
1953 void **slot;
1954 unsigned int ret = 0;
1955 struct radix_tree_iter iter;
1956
1957 if (!nr_entries)
1958 return 0;
1959
1960 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001961 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001962 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001963repeat:
1964 page = radix_tree_deref_slot(slot);
1965 if (unlikely(!page))
1966 continue;
1967 if (radix_tree_exception(page)) {
1968 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001969 slot = radix_tree_iter_retry(&iter);
1970 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001971 }
1972
1973 /*
1974 * A shadow entry of a recently evicted page, a swap
1975 * entry from shmem/tmpfs or a DAX entry. Return it
1976 * without attempting to raise page count.
1977 */
1978 goto export;
1979 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001980
1981 head = compound_head(page);
1982 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001983 goto repeat;
1984
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001985 /* The page was split under us? */
1986 if (compound_head(page) != head) {
1987 put_page(head);
1988 goto repeat;
1989 }
1990
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001991 /* Has the page moved? */
1992 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001993 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001994 goto repeat;
1995 }
1996export:
1997 indices[ret] = iter.index;
1998 entries[ret] = page;
1999 if (++ret == nr_entries)
2000 break;
2001 }
2002 rcu_read_unlock();
2003 return ret;
2004}
2005EXPORT_SYMBOL(find_get_entries_tag);
2006
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002007/*
2008 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2009 * a _large_ part of the i/o request. Imagine the worst scenario:
2010 *
2011 * ---R__________________________________________B__________
2012 * ^ reading here ^ bad block(assume 4k)
2013 *
2014 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2015 * => failing the whole request => read(R) => read(R+1) =>
2016 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2017 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2018 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2019 *
2020 * It is going insane. Fix it by quickly scaling down the readahead size.
2021 */
2022static void shrink_readahead_size_eio(struct file *filp,
2023 struct file_ra_state *ra)
2024{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002025 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002026}
2027
Randy Dunlap485bb992006-06-23 02:03:49 -07002028/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002029 * generic_file_buffered_read - generic file read routine
2030 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002031 * @iter: data destination
2032 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002033 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002035 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *
2037 * This is really ugly. But the goto's actually try to clarify some
2038 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002040static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002041 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002043 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002044 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002046 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002047 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002048 pgoff_t index;
2049 pgoff_t last_index;
2050 pgoff_t prev_index;
2051 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002052 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002053 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Wei Fangc2a97372016-10-07 17:01:52 -07002055 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002056 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002057 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002059 index = *ppos >> PAGE_SHIFT;
2060 prev_index = ra->prev_pos >> PAGE_SHIFT;
2061 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2062 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2063 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 for (;;) {
2066 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002067 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002068 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 unsigned long nr, ret;
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002073 if (fatal_signal_pending(current)) {
2074 error = -EINTR;
2075 goto out;
2076 }
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002079 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002080 if (iocb->ki_flags & IOCB_NOWAIT)
2081 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002082 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002083 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002084 index, last_index - index);
2085 page = find_get_page(mapping, index);
2086 if (unlikely(page == NULL))
2087 goto no_cached_page;
2088 }
2089 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002090 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002091 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002092 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002094 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002095 if (iocb->ki_flags & IOCB_NOWAIT) {
2096 put_page(page);
2097 goto would_block;
2098 }
2099
Mel Gormanebded022016-03-15 14:55:39 -07002100 /*
2101 * See comment in do_read_cache_page on why
2102 * wait_on_page_locked is used to avoid unnecessarily
2103 * serialisations and why it's safe.
2104 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002105 error = wait_on_page_locked_killable(page);
2106 if (unlikely(error))
2107 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002108 if (PageUptodate(page))
2109 goto page_ok;
2110
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002112 !mapping->a_ops->is_partially_uptodate)
2113 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002114 /* pipes can't handle partially uptodate pages */
2115 if (unlikely(iter->type & ITER_PIPE))
2116 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002117 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002118 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002119 /* Did it get truncated before we got the lock? */
2120 if (!page->mapping)
2121 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002122 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002123 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002124 goto page_not_up_to_date_locked;
2125 unlock_page(page);
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002128 /*
2129 * i_size must be checked after we know the page is Uptodate.
2130 *
2131 * Checking i_size after the check allows us to calculate
2132 * the correct value for "nr", which means the zero-filled
2133 * part of the page is not copied back to userspace (unless
2134 * another truncate extends the file - this is desired though).
2135 */
2136
2137 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002138 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002139 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002141 goto out;
2142 }
2143
2144 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002146 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002148 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002149 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002150 goto out;
2151 }
2152 }
2153 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /* If users can be writing to this page using arbitrary
2156 * virtual addresses, take care about potential aliasing
2157 * before reading the page on the kernel side.
2158 */
2159 if (mapping_writably_mapped(mapping))
2160 flush_dcache_page(page);
2161
2162 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002163 * When a sequential read accesses a page several times,
2164 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 */
Jan Karaec0f1632007-05-06 14:49:25 -07002166 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 mark_page_accessed(page);
2168 prev_index = index;
2169
2170 /*
2171 * Ok, we have the page, and it's up-to-date, so
2172 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Al Viro6e58e792014-02-03 17:07:03 -05002174
2175 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002177 index += offset >> PAGE_SHIFT;
2178 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002179 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002181 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002182 written += ret;
2183 if (!iov_iter_count(iter))
2184 goto out;
2185 if (ret < nr) {
2186 error = -EFAULT;
2187 goto out;
2188 }
2189 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191page_not_up_to_date:
2192 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002193 error = lock_page_killable(page);
2194 if (unlikely(error))
2195 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002197page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002198 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!page->mapping) {
2200 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002201 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 continue;
2203 }
2204
2205 /* Did somebody else fill it already? */
2206 if (PageUptodate(page)) {
2207 unlock_page(page);
2208 goto page_ok;
2209 }
2210
2211readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002212 /*
2213 * A previous I/O error may have been due to temporary
2214 * failures, eg. multipath errors.
2215 * PG_error will be set again if readpage fails.
2216 */
2217 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* Start the actual read. The read will unlock the page. */
2219 error = mapping->a_ops->readpage(filp, page);
2220
Zach Brown994fc28c2005-12-15 14:28:17 -08002221 if (unlikely(error)) {
2222 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002223 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002224 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002225 goto find_page;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002231 error = lock_page_killable(page);
2232 if (unlikely(error))
2233 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (!PageUptodate(page)) {
2235 if (page->mapping == NULL) {
2236 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002237 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 */
2239 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002240 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto find_page;
2242 }
2243 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002244 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002245 error = -EIO;
2246 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 unlock_page(page);
2249 }
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto page_ok;
2252
2253readpage_error:
2254 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002255 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto out;
2257
2258no_cached_page:
2259 /*
2260 * Ok, it wasn't cached, so we need to create a new
2261 * page..
2262 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002263 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002264 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002265 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002268 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002269 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002271 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002272 if (error == -EEXIST) {
2273 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto out;
2277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto readpage;
2279 }
2280
Milosz Tanski3239d832017-08-29 16:13:19 +02002281would_block:
2282 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002284 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002285 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002286 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002288 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002289 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002290 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Randy Dunlap485bb992006-06-23 02:03:49 -07002293/**
Al Viro6abd2322014-04-04 14:20:57 -04002294 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002295 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002296 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002297 *
Al Viro6abd2322014-04-04 14:20:57 -04002298 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * that can use the page cache directly.
2300 */
2301ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002302generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002304 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002305 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002306
2307 if (!count)
2308 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Al Viro2ba48ce2015-04-09 13:52:01 -04002310 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002311 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002312 struct address_space *mapping = file->f_mapping;
2313 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002314 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002317 if (iocb->ki_flags & IOCB_NOWAIT) {
2318 if (filemap_range_has_page(mapping, iocb->ki_pos,
2319 iocb->ki_pos + count - 1))
2320 return -EAGAIN;
2321 } else {
2322 retval = filemap_write_and_wait_range(mapping,
2323 iocb->ki_pos,
2324 iocb->ki_pos + count - 1);
2325 if (retval < 0)
2326 goto out;
2327 }
Al Viroed978a82014-03-05 22:53:04 -05002328
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002329 file_accessed(file);
2330
Al Viro5ecda132017-04-13 14:13:36 -04002331 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002332 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002333 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002334 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002335 }
Al Viro5b47d592017-05-08 13:54:47 -04002336 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002337
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002338 /*
2339 * Btrfs can have a short DIO read if we encounter
2340 * compressed extents, so if there was an error, or if
2341 * we've already read everything we wanted to, or if
2342 * there was a short read because we hit EOF, go ahead
2343 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002344 * the rest of the read. Buffered reads will not work for
2345 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002346 */
Al Viro5ecda132017-04-13 14:13:36 -04002347 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002348 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002349 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
2351
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002352 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353out:
2354 return retval;
2355}
Al Viroed978a82014-03-05 22:53:04 -05002356EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002359/**
2360 * page_cache_read - adds requested page to the page cache if not already there
2361 * @file: file to read
2362 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002363 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002364 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 * This adds the requested page to the page cache if it isn't already there,
2366 * and schedules an I/O to read in its contents from disk.
2367 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002368static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002371 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002372 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Zach Brown994fc28c2005-12-15 14:28:17 -08002374 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002375 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002376 if (!page)
2377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002379 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002380 if (ret == 0)
2381 ret = mapping->a_ops->readpage(file, page);
2382 else if (ret == -EEXIST)
2383 ret = 0; /* losing race to add is OK */
2384
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002385 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Zach Brown994fc28c2005-12-15 14:28:17 -08002387 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002388
Zach Brown994fc28c2005-12-15 14:28:17 -08002389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
2392#define MMAP_LOTSAMISS (100)
2393
Linus Torvaldsef00e082009-06-16 15:31:25 -07002394/*
2395 * Synchronous readahead happens when we don't even find
2396 * a page in the page cache at all.
2397 */
2398static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2399 struct file_ra_state *ra,
2400 struct file *file,
2401 pgoff_t offset)
2402{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002403 struct address_space *mapping = file->f_mapping;
2404
2405 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002406 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002407 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002408 if (!ra->ra_pages)
2409 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002410
Joe Perches64363aa2013-07-08 16:00:18 -07002411 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002412 page_cache_sync_readahead(mapping, ra, file, offset,
2413 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002414 return;
2415 }
2416
Andi Kleen207d04b2011-05-24 17:12:29 -07002417 /* Avoid banging the cache line if not needed */
2418 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002419 ra->mmap_miss++;
2420
2421 /*
2422 * Do we miss much more than hit in this file? If so,
2423 * stop bothering with read-ahead. It will only hurt.
2424 */
2425 if (ra->mmap_miss > MMAP_LOTSAMISS)
2426 return;
2427
Wu Fengguangd30a1102009-06-16 15:31:30 -07002428 /*
2429 * mmap read-around
2430 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002431 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2432 ra->size = ra->ra_pages;
2433 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002434 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002435}
2436
2437/*
2438 * Asynchronous readahead happens when we find the page and PG_readahead,
2439 * so we want to possibly extend the readahead further..
2440 */
2441static void do_async_mmap_readahead(struct vm_area_struct *vma,
2442 struct file_ra_state *ra,
2443 struct file *file,
2444 struct page *page,
2445 pgoff_t offset)
2446{
2447 struct address_space *mapping = file->f_mapping;
2448
2449 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002450 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002451 return;
2452 if (ra->mmap_miss > 0)
2453 ra->mmap_miss--;
2454 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002455 page_cache_async_readahead(mapping, ra, file,
2456 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002457}
2458
Randy Dunlap485bb992006-06-23 02:03:49 -07002459/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002460 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002461 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002462 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002463 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 * mapped memory region to read in file data during a page fault.
2465 *
2466 * The goto's are kind of ugly, but this streamlines the normal case of having
2467 * it in the page cache, and handles the special cases reasonably without
2468 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002469 *
2470 * vma->vm_mm->mmap_sem must be held on entry.
2471 *
2472 * If our return value has VM_FAULT_RETRY set, it's because
2473 * lock_page_or_retry() returned 0.
2474 * The mmap_sem has usually been released in this case.
2475 * See __lock_page_or_retry() for the exception.
2476 *
2477 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2478 * has not been released.
2479 *
2480 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002482vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002485 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 struct address_space *mapping = file->f_mapping;
2487 struct file_ra_state *ra = &file->f_ra;
2488 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002489 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002490 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002492 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002494 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2495 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002496 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002499 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002501 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002502 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002504 * We found the page, so try async readahead before
2505 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Dave Jiang11bac802017-02-24 14:56:41 -08002507 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002508 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002509 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002510 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002511 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002512 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002513 ret = VM_FAULT_MAJOR;
2514retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002515 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (!page)
2517 goto no_cached_page;
2518 }
2519
Dave Jiang11bac802017-02-24 14:56:41 -08002520 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002521 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002522 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002523 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002524
2525 /* Did it get truncated? */
2526 if (unlikely(page->mapping != mapping)) {
2527 unlock_page(page);
2528 put_page(page);
2529 goto retry_find;
2530 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002531 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002534 * We have a locked page in the page cache, now we need to check
2535 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 */
Nick Piggind00806b2007-07-19 01:46:57 -07002537 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 goto page_not_uptodate;
2539
Linus Torvaldsef00e082009-06-16 15:31:25 -07002540 /*
2541 * Found the page and have a reference on it.
2542 * We must recheck i_size under page lock.
2543 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002544 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2545 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002546 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002547 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002548 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002549 }
2550
Nick Piggind0217ac2007-07-19 01:47:03 -07002551 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002552 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554no_cached_page:
2555 /*
2556 * We're only likely to ever get here if MADV_RANDOM is in
2557 * effect.
2558 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002559 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 /*
2562 * The page we want has now been added to the page cache.
2563 * In the unlikely event that someone removed it in the
2564 * meantime, we'll just come back here and read it again.
2565 */
2566 if (error >= 0)
2567 goto retry_find;
2568
2569 /*
2570 * An error return from page_cache_read can result if the
2571 * system is low on memory, or a problem occurs while trying
2572 * to schedule I/O.
2573 */
2574 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002575 return VM_FAULT_OOM;
2576 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 /*
2580 * Umm, take care of errors if the page isn't up-to-date.
2581 * Try to re-read it _once_. We do this synchronously,
2582 * because there really aren't any performance issues here
2583 * and we need to check for errors.
2584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002586 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002587 if (!error) {
2588 wait_on_page_locked(page);
2589 if (!PageUptodate(page))
2590 error = -EIO;
2591 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002592 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002593
2594 if (!error || error == AOP_TRUNCATED_PAGE)
2595 goto retry_find;
2596
2597 /* Things didn't work out. Return zero to tell the mm layer so. */
2598 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002599 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002600}
2601EXPORT_SYMBOL(filemap_fault);
2602
Jan Kara82b0f8c2016-12-14 15:06:58 -08002603void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002604 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002605{
2606 struct radix_tree_iter iter;
2607 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002608 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002609 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002610 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002611 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002612 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002613
2614 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002615 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start_pgoff) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002616 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002617 break;
2618repeat:
2619 page = radix_tree_deref_slot(slot);
2620 if (unlikely(!page))
2621 goto next;
2622 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002623 if (radix_tree_deref_retry(page)) {
2624 slot = radix_tree_iter_retry(&iter);
2625 continue;
2626 }
2627 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002628 }
2629
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002630 head = compound_head(page);
2631 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002632 goto repeat;
2633
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002634 /* The page was split under us? */
2635 if (compound_head(page) != head) {
2636 put_page(head);
2637 goto repeat;
2638 }
2639
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002640 /* Has the page moved? */
2641 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002642 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002643 goto repeat;
2644 }
2645
2646 if (!PageUptodate(page) ||
2647 PageReadahead(page) ||
2648 PageHWPoison(page))
2649 goto skip;
2650 if (!trylock_page(page))
2651 goto skip;
2652
2653 if (page->mapping != mapping || !PageUptodate(page))
2654 goto unlock;
2655
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002656 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2657 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002658 goto unlock;
2659
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002660 if (file->f_ra.mmap_miss > 0)
2661 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002662
Jan Kara82b0f8c2016-12-14 15:06:58 -08002663 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2664 if (vmf->pte)
2665 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002666 last_pgoff = iter.index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002667 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002668 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002669 unlock_page(page);
2670 goto next;
2671unlock:
2672 unlock_page(page);
2673skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002674 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002675next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002676 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002677 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002678 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002679 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002680 break;
2681 }
2682 rcu_read_unlock();
2683}
2684EXPORT_SYMBOL(filemap_map_pages);
2685
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002686vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002687{
2688 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002689 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002690 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002691
Jan Kara14da9202012-06-12 16:20:37 +02002692 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002693 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002694 lock_page(page);
2695 if (page->mapping != inode->i_mapping) {
2696 unlock_page(page);
2697 ret = VM_FAULT_NOPAGE;
2698 goto out;
2699 }
Jan Kara14da9202012-06-12 16:20:37 +02002700 /*
2701 * We mark the page dirty already here so that when freeze is in
2702 * progress, we are guaranteed that writeback during freezing will
2703 * see the dirty page and writeprotect it again.
2704 */
2705 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002706 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002707out:
Jan Kara14da9202012-06-12 16:20:37 +02002708 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002709 return ret;
2710}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002711
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002712const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002713 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002714 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002715 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716};
2717
2718/* This is used for a general mmap of a disk file */
2719
2720int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2721{
2722 struct address_space *mapping = file->f_mapping;
2723
2724 if (!mapping->a_ops->readpage)
2725 return -ENOEXEC;
2726 file_accessed(file);
2727 vma->vm_ops = &generic_file_vm_ops;
2728 return 0;
2729}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731/*
2732 * This is for filesystems which do not implement ->writepage.
2733 */
2734int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2735{
2736 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2737 return -EINVAL;
2738 return generic_file_mmap(file, vma);
2739}
2740#else
Arnd Bergmann45397222018-04-13 15:35:27 -07002741int filemap_page_mkwrite(struct vm_fault *vmf)
2742{
2743 return -ENOSYS;
2744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2746{
2747 return -ENOSYS;
2748}
2749int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2750{
2751 return -ENOSYS;
2752}
2753#endif /* CONFIG_MMU */
2754
Arnd Bergmann45397222018-04-13 15:35:27 -07002755EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756EXPORT_SYMBOL(generic_file_mmap);
2757EXPORT_SYMBOL(generic_file_readonly_mmap);
2758
Sasha Levin67f9fd92014-04-03 14:48:18 -07002759static struct page *wait_on_page_read(struct page *page)
2760{
2761 if (!IS_ERR(page)) {
2762 wait_on_page_locked(page);
2763 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002764 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002765 page = ERR_PTR(-EIO);
2766 }
2767 }
2768 return page;
2769}
2770
Mel Gorman32b63522016-03-15 14:55:36 -07002771static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002772 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002773 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002774 void *data,
2775 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Nick Piggineb2be182007-10-16 01:24:57 -07002777 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 int err;
2779repeat:
2780 page = find_get_page(mapping, index);
2781 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002782 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002783 if (!page)
2784 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002785 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002786 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002787 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002788 if (err == -EEXIST)
2789 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return ERR_PTR(err);
2792 }
Mel Gorman32b63522016-03-15 14:55:36 -07002793
2794filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 err = filler(data, page);
2796 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002797 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002798 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Mel Gorman32b63522016-03-15 14:55:36 -07002800
2801 page = wait_on_page_read(page);
2802 if (IS_ERR(page))
2803 return page;
2804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (PageUptodate(page))
2807 goto out;
2808
Mel Gormanebded022016-03-15 14:55:39 -07002809 /*
2810 * Page is not up to date and may be locked due one of the following
2811 * case a: Page is being filled and the page lock is held
2812 * case b: Read/write error clearing the page uptodate status
2813 * case c: Truncation in progress (page locked)
2814 * case d: Reclaim in progress
2815 *
2816 * Case a, the page will be up to date when the page is unlocked.
2817 * There is no need to serialise on the page lock here as the page
2818 * is pinned so the lock gives no additional protection. Even if the
2819 * the page is truncated, the data is still valid if PageUptodate as
2820 * it's a race vs truncate race.
2821 * Case b, the page will not be up to date
2822 * Case c, the page may be truncated but in itself, the data may still
2823 * be valid after IO completes as it's a read vs truncate race. The
2824 * operation must restart if the page is not uptodate on unlock but
2825 * otherwise serialising on page lock to stabilise the mapping gives
2826 * no additional guarantees to the caller as the page lock is
2827 * released before return.
2828 * Case d, similar to truncation. If reclaim holds the page lock, it
2829 * will be a race with remove_mapping that determines if the mapping
2830 * is valid on unlock but otherwise the data is valid and there is
2831 * no need to serialise with page lock.
2832 *
2833 * As the page lock gives no additional guarantee, we optimistically
2834 * wait on the page to be unlocked and check if it's up to date and
2835 * use the page if it is. Otherwise, the page lock is required to
2836 * distinguish between the different cases. The motivation is that we
2837 * avoid spurious serialisations and wakeups when multiple processes
2838 * wait on the same page for IO to complete.
2839 */
2840 wait_on_page_locked(page);
2841 if (PageUptodate(page))
2842 goto out;
2843
2844 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002846
2847 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (!page->mapping) {
2849 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002850 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002851 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Mel Gormanebded022016-03-15 14:55:39 -07002853
2854 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (PageUptodate(page)) {
2856 unlock_page(page);
2857 goto out;
2858 }
Mel Gorman32b63522016-03-15 14:55:36 -07002859 goto filler;
2860
David Howellsc855ff32007-05-09 13:42:20 +01002861out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002862 mark_page_accessed(page);
2863 return page;
2864}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002865
2866/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002867 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002868 * @mapping: the page's address_space
2869 * @index: the page index
2870 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002871 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002872 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002873 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002874 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002875 *
2876 * If the page does not get brought uptodate, return -EIO.
2877 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002878struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002879 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002880 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002881 void *data)
2882{
2883 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2884}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002885EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002886
2887/**
2888 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2889 * @mapping: the page's address_space
2890 * @index: the page index
2891 * @gfp: the page allocator flags to use if allocating
2892 *
2893 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002894 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002895 *
2896 * If the page does not get brought uptodate, return -EIO.
2897 */
2898struct page *read_cache_page_gfp(struct address_space *mapping,
2899 pgoff_t index,
2900 gfp_t gfp)
2901{
2902 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2903
Sasha Levin67f9fd92014-04-03 14:48:18 -07002904 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002905}
2906EXPORT_SYMBOL(read_cache_page_gfp);
2907
Nick Piggin2f718ff2007-10-16 01:24:59 -07002908/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 * Performs necessary checks before doing a write
2910 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002911 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 * Returns appropriate error code that caller should return or
2913 * zero in case that write should be allowed.
2914 */
Al Viro3309dd02015-04-09 12:55:47 -04002915inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Al Viro3309dd02015-04-09 12:55:47 -04002917 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002919 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002920 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Al Viro3309dd02015-04-09 12:55:47 -04002922 if (!iov_iter_count(from))
2923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Al Viro0fa6b002015-04-04 04:05:48 -04002925 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002926 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002927 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Al Viro3309dd02015-04-09 12:55:47 -04002929 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002931 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2932 return -EINVAL;
2933
Al Viro0fa6b002015-04-04 04:05:48 -04002934 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002935 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002936 send_sig(SIGXFSZ, current, 0);
2937 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
Al Viro3309dd02015-04-09 12:55:47 -04002939 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941
2942 /*
2943 * LFS rule
2944 */
Al Viro3309dd02015-04-09 12:55:47 -04002945 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002947 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002949 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 }
2951
2952 /*
2953 * Are we about to exceed the fs block limit ?
2954 *
2955 * If we have written data it becomes a short write. If we have
2956 * exceeded without writing data we send a signal and return EFBIG.
2957 * Linus frestrict idea will clean these up nicely..
2958 */
Al Viro3309dd02015-04-09 12:55:47 -04002959 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2960 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Al Viro3309dd02015-04-09 12:55:47 -04002962 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2963 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965EXPORT_SYMBOL(generic_write_checks);
2966
Nick Pigginafddba42007-10-16 01:25:01 -07002967int pagecache_write_begin(struct file *file, struct address_space *mapping,
2968 loff_t pos, unsigned len, unsigned flags,
2969 struct page **pagep, void **fsdata)
2970{
2971 const struct address_space_operations *aops = mapping->a_ops;
2972
Nick Piggin4e02ed42008-10-29 14:00:55 -07002973 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002974 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002975}
2976EXPORT_SYMBOL(pagecache_write_begin);
2977
2978int pagecache_write_end(struct file *file, struct address_space *mapping,
2979 loff_t pos, unsigned len, unsigned copied,
2980 struct page *page, void *fsdata)
2981{
2982 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002983
Nick Piggin4e02ed42008-10-29 14:00:55 -07002984 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002985}
2986EXPORT_SYMBOL(pagecache_write_end);
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002989generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 struct file *file = iocb->ki_filp;
2992 struct address_space *mapping = file->f_mapping;
2993 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002994 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002996 size_t write_len;
2997 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Al Viro0c949332014-03-22 06:51:37 -04002999 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003000 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003001
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003002 if (iocb->ki_flags & IOCB_NOWAIT) {
3003 /* If there are pages to writeback, return */
3004 if (filemap_range_has_page(inode->i_mapping, pos,
3005 pos + iov_iter_count(from)))
3006 return -EAGAIN;
3007 } else {
3008 written = filemap_write_and_wait_range(mapping, pos,
3009 pos + write_len - 1);
3010 if (written)
3011 goto out;
3012 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003013
3014 /*
3015 * After a write we want buffered reads to be sure to go to disk to get
3016 * the new data. We invalidate clean cached page from the region we're
3017 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003018 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003019 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003020 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003021 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003022 /*
3023 * If a page can not be invalidated, return 0 to fall back
3024 * to buffered write.
3025 */
3026 if (written) {
3027 if (written == -EBUSY)
3028 return 0;
3029 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003030 }
3031
Al Viro639a93a52017-04-13 14:10:15 -04003032 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003033
3034 /*
3035 * Finally, try again to invalidate clean pages which might have been
3036 * cached by non-direct readahead, or faulted in by get_user_pages()
3037 * if the source of the write was an mmap'ed region of the file
3038 * we're writing. Either one is a pretty crazy thing to do,
3039 * so we don't support it 100%. If this invalidation
3040 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003041 *
3042 * Most of the time we do not need this since dio_complete() will do
3043 * the invalidation for us. However there are some file systems that
3044 * do not end up with dio_complete() being called, so let's not break
3045 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003046 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003047 if (mapping->nrpages)
3048 invalidate_inode_pages2_range(mapping,
3049 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003052 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003053 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003054 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3055 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 mark_inode_dirty(inode);
3057 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003058 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
Al Viro639a93a52017-04-13 14:10:15 -04003060 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003061out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return written;
3063}
3064EXPORT_SYMBOL(generic_file_direct_write);
3065
Nick Piggineb2be182007-10-16 01:24:57 -07003066/*
3067 * Find or create a page at the given pagecache position. Return the locked
3068 * page. This function is specifically for buffered writes.
3069 */
Nick Piggin54566b22009-01-04 12:00:53 -08003070struct page *grab_cache_page_write_begin(struct address_space *mapping,
3071 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003072{
Nick Piggineb2be182007-10-16 01:24:57 -07003073 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003074 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003075
Nick Piggin54566b22009-01-04 12:00:53 -08003076 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003077 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003078
Mel Gorman2457aec2014-06-04 16:10:31 -07003079 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003080 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003081 if (page)
3082 wait_for_stable_page(page);
3083
Nick Piggineb2be182007-10-16 01:24:57 -07003084 return page;
3085}
Nick Piggin54566b22009-01-04 12:00:53 -08003086EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003087
Al Viro3b93f912014-02-11 21:34:08 -05003088ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003089 struct iov_iter *i, loff_t pos)
3090{
3091 struct address_space *mapping = file->f_mapping;
3092 const struct address_space_operations *a_ops = mapping->a_ops;
3093 long status = 0;
3094 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003095 unsigned int flags = 0;
3096
Nick Pigginafddba42007-10-16 01:25:01 -07003097 do {
3098 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003099 unsigned long offset; /* Offset into pagecache page */
3100 unsigned long bytes; /* Bytes to write to page */
3101 size_t copied; /* Bytes copied from user */
3102 void *fsdata;
3103
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003104 offset = (pos & (PAGE_SIZE - 1));
3105 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003106 iov_iter_count(i));
3107
3108again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003109 /*
3110 * Bring in the user page that we will copy from _first_.
3111 * Otherwise there's a nasty deadlock on copying from the
3112 * same page as we're writing to, without it being marked
3113 * up-to-date.
3114 *
3115 * Not only is this an optimisation, but it is also required
3116 * to check that the address is actually valid, when atomic
3117 * usercopies are used, below.
3118 */
3119 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3120 status = -EFAULT;
3121 break;
3122 }
3123
Jan Kara296291c2015-10-22 13:32:21 -07003124 if (fatal_signal_pending(current)) {
3125 status = -EINTR;
3126 break;
3127 }
3128
Nick Piggin674b8922007-10-16 01:25:03 -07003129 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003130 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003131 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003132 break;
3133
anfei zhou931e80e2010-02-02 13:44:02 -08003134 if (mapping_writably_mapped(mapping))
3135 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003136
Nick Pigginafddba42007-10-16 01:25:01 -07003137 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003138 flush_dcache_page(page);
3139
3140 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3141 page, fsdata);
3142 if (unlikely(status < 0))
3143 break;
3144 copied = status;
3145
3146 cond_resched();
3147
Nick Piggin124d3b72008-02-02 15:01:17 +01003148 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003149 if (unlikely(copied == 0)) {
3150 /*
3151 * If we were unable to copy any data at all, we must
3152 * fall back to a single segment length write.
3153 *
3154 * If we didn't fallback here, we could livelock
3155 * because not all segments in the iov can be copied at
3156 * once without a pagefault.
3157 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003158 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003159 iov_iter_single_seg_count(i));
3160 goto again;
3161 }
Nick Pigginafddba42007-10-16 01:25:01 -07003162 pos += copied;
3163 written += copied;
3164
3165 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003166 } while (iov_iter_count(i));
3167
3168 return written ? written : status;
3169}
Al Viro3b93f912014-02-11 21:34:08 -05003170EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Jan Karae4dd9de2009-08-17 18:10:06 +02003172/**
Al Viro81742022014-04-03 03:17:43 -04003173 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003174 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003175 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003176 *
3177 * This function does all the work needed for actually writing data to a
3178 * file. It does all basic checks, removes SUID from the file, updates
3179 * modification times and calls proper subroutines depending on whether we
3180 * do direct IO or a standard buffered write.
3181 *
3182 * It expects i_mutex to be grabbed unless we work on a block device or similar
3183 * object which does not need locking at all.
3184 *
3185 * This function does *not* take care of syncing data in case of O_SYNC write.
3186 * A caller has to handle it. This is mainly due to the fact that we want to
3187 * avoid syncing under i_mutex.
3188 */
Al Viro81742022014-04-03 03:17:43 -04003189ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003192 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003194 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003196 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003199 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003200 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 if (err)
3202 goto out;
3203
Josef Bacikc3b2da32012-03-26 09:59:21 -04003204 err = file_update_time(file);
3205 if (err)
3206 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Al Viro2ba48ce2015-04-09 13:52:01 -04003208 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003209 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003210
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003211 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003212 /*
3213 * If the write stopped short of completing, fall back to
3214 * buffered writes. Some filesystems do this for writes to
3215 * holes, for example. For DAX files, a buffered write will
3216 * not succeed (even if it did, DAX does not handle dirty
3217 * page-cache pages correctly).
3218 */
Al Viro0b8def92015-04-07 10:22:53 -04003219 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003221
Al Viro0b8def92015-04-07 10:22:53 -04003222 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003223 /*
Al Viro3b93f912014-02-11 21:34:08 -05003224 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003225 * then we want to return the number of bytes which were
3226 * direct-written, or the error code if that was zero. Note
3227 * that this differs from normal direct-io semantics, which
3228 * will return -EFOO even if some bytes were written.
3229 */
Al Viro60bb4522014-08-08 12:39:16 -04003230 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003231 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003232 goto out;
3233 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003234 /*
3235 * We need to ensure that the page cache pages are written to
3236 * disk and invalidated to preserve the expected O_DIRECT
3237 * semantics.
3238 */
Al Viro3b93f912014-02-11 21:34:08 -05003239 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003240 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003241 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003242 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003243 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003244 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003245 pos >> PAGE_SHIFT,
3246 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003247 } else {
3248 /*
3249 * We don't know how much we wrote, so just return
3250 * the number of bytes which were direct-written
3251 */
3252 }
3253 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003254 written = generic_perform_write(file, from, iocb->ki_pos);
3255 if (likely(written > 0))
3256 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258out:
3259 current->backing_dev_info = NULL;
3260 return written ? written : err;
3261}
Al Viro81742022014-04-03 03:17:43 -04003262EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Jan Karae4dd9de2009-08-17 18:10:06 +02003264/**
Al Viro81742022014-04-03 03:17:43 -04003265 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003266 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003267 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003268 *
Al Viro81742022014-04-03 03:17:43 -04003269 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003270 * filesystems. It takes care of syncing the file in case of O_SYNC file
3271 * and acquires i_mutex as needed.
3272 */
Al Viro81742022014-04-03 03:17:43 -04003273ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003276 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Al Viro59551022016-01-22 15:40:57 -05003279 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003280 ret = generic_write_checks(iocb, from);
3281 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003282 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003283 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Christoph Hellwige2592212016-04-07 08:52:01 -07003285 if (ret > 0)
3286 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return ret;
3288}
Al Viro81742022014-04-03 03:17:43 -04003289EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
David Howellscf9a2ae2006-08-29 19:05:54 +01003291/**
3292 * try_to_release_page() - release old fs-specific metadata on a page
3293 *
3294 * @page: the page which the kernel is trying to free
3295 * @gfp_mask: memory allocation flags (and I/O mode)
3296 *
3297 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003298 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003299 * Otherwise return zero.
3300 *
David Howells266cf652009-04-03 16:42:36 +01003301 * This may also be called if PG_fscache is set on a page, indicating that the
3302 * page is known to the local caching routines.
3303 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003304 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003305 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003306 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003307 */
3308int try_to_release_page(struct page *page, gfp_t gfp_mask)
3309{
3310 struct address_space * const mapping = page->mapping;
3311
3312 BUG_ON(!PageLocked(page));
3313 if (PageWriteback(page))
3314 return 0;
3315
3316 if (mapping && mapping->a_ops->releasepage)
3317 return mapping->a_ops->releasepage(page, gfp_mask);
3318 return try_to_free_buffers(page);
3319}
3320
3321EXPORT_SYMBOL(try_to_release_page);