blob: b63caebd13675ea3d1fd10271cae7eb717061f88 [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 Weiner91b0abe2014-04-03 14:47:49 -0700114static void page_cache_tree_delete(struct address_space *mapping,
115 struct page *page, void *shadow)
116{
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800117 int i, nr;
118
119 /* hugetlb pages are represented by one entry in the radix tree */
120 nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700121
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700122 VM_BUG_ON_PAGE(!PageLocked(page), page);
123 VM_BUG_ON_PAGE(PageTail(page), page);
124 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700125
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700126 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200127 struct radix_tree_node *node;
128 void **slot;
129
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700130 __radix_tree_lookup(&mapping->i_pages, page->index + i,
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200131 &node, &slot);
132
Johannes Weinerdbc446b2016-12-12 16:43:55 -0800133 VM_BUG_ON_PAGE(!node && nr != 1, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700134
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700135 radix_tree_clear_tags(&mapping->i_pages, node, slot);
136 __radix_tree_replace(&mapping->i_pages, node, slot, shadow,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800137 workingset_lookup_update(mapping));
Johannes Weiner449dd692014-04-03 14:47:56 -0700138 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200139
Jan Kara23006382017-11-15 17:37:26 -0800140 page->mapping = NULL;
141 /* Leave page->index set: truncation lookup relies upon it */
142
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200143 if (shadow) {
144 mapping->nrexceptional += nr;
145 /*
146 * Make sure the nrexceptional update is committed before
147 * the nrpages update so that final truncate racing
148 * with reclaim does not see both counters 0 at the
149 * same time and miss a shadow entry.
150 */
151 smp_wmb();
152 }
153 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700154}
155
Jan Kara5ecc4d82017-11-15 17:37:29 -0800156static void unaccount_page_cache_page(struct address_space *mapping,
157 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800159 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600161 /*
162 * if we're uptodate, flush out into the cleancache, otherwise
163 * invalidate any existing cleancache entries. We can't leave
164 * stale data around in the cleancache once our page is gone
165 */
166 if (PageUptodate(page) && PageMappedToDisk(page))
167 cleancache_put_page(page);
168 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400169 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600170
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700171 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800172 VM_BUG_ON_PAGE(page_mapped(page), page);
173 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
174 int mapcount;
175
176 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
177 current->comm, page_to_pfn(page));
178 dump_page(page, "still mapped when deleted");
179 dump_stack();
180 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
181
182 mapcount = page_mapcount(page);
183 if (mapping_exiting(mapping) &&
184 page_count(page) >= mapcount + 2) {
185 /*
186 * All vmas have already been torn down, so it's
187 * a good bet that actually the page is unmapped,
188 * and we'd prefer not to leak it: if we're wrong,
189 * some other bad page check should catch it later.
190 */
191 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700192 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800193 }
194 }
195
Jan Kara76253fb2017-11-15 17:37:22 -0800196 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800197 if (PageHuge(page))
198 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800199
Jan Kara5ecc4d82017-11-15 17:37:29 -0800200 nr = hpage_nr_pages(page);
201
202 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
203 if (PageSwapBacked(page)) {
204 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
205 if (PageTransHuge(page))
206 __dec_node_page_state(page, NR_SHMEM_THPS);
207 } else {
208 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800209 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800210
211 /*
212 * At this point page must be either written or cleaned by
213 * truncate. Dirty page here signals a bug and loss of
214 * unwritten data.
215 *
216 * This fixes dirty accounting after removing the page entirely
217 * but leaves PageDirty set: it has no effect for truncated
218 * page and anyway will be cleared before returning page into
219 * buddy allocator.
220 */
221 if (WARN_ON_ONCE(PageDirty(page)))
222 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
223}
224
225/*
226 * Delete a page from the page cache and free it. Caller has to make
227 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700228 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800229 */
230void __delete_from_page_cache(struct page *page, void *shadow)
231{
232 struct address_space *mapping = page->mapping;
233
234 trace_mm_filemap_delete_from_page_cache(page);
235
236 unaccount_page_cache_page(mapping, page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700237 page_cache_tree_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Jan Kara59c66c52017-11-15 17:37:18 -0800240static void page_cache_free_page(struct address_space *mapping,
241 struct page *page)
242{
243 void (*freepage)(struct page *);
244
245 freepage = mapping->a_ops->freepage;
246 if (freepage)
247 freepage(page);
248
249 if (PageTransHuge(page) && !PageHuge(page)) {
250 page_ref_sub(page, HPAGE_PMD_NR);
251 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
252 } else {
253 put_page(page);
254 }
255}
256
Minchan Kim702cfbf2011-03-22 16:32:43 -0700257/**
258 * delete_from_page_cache - delete page from page cache
259 * @page: the page which the kernel is trying to remove from page cache
260 *
261 * This must be called only on pages that have been verified to be in the page
262 * cache and locked. It will never put the page into the free list, the caller
263 * has a reference on the page.
264 */
265void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700267 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400268 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Matt Mackallcd7619d2005-05-01 08:59:01 -0700270 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700271 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700272 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700273 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500274
Jan Kara59c66c52017-11-15 17:37:18 -0800275 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700276}
277EXPORT_SYMBOL(delete_from_page_cache);
278
Jan Karaaa65c292017-11-15 17:37:33 -0800279/*
280 * page_cache_tree_delete_batch - delete several pages from page cache
281 * @mapping: the mapping to which pages belong
282 * @pvec: pagevec with pages to delete
283 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700284 * The function walks over mapping->i_pages and removes pages passed in @pvec
285 * from the mapping. The function expects @pvec to be sorted by page index.
286 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800287 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700288 * @pvec and takes care to delete all corresponding tail pages from the
289 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800290 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700291 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800292 */
293static void
294page_cache_tree_delete_batch(struct address_space *mapping,
295 struct pagevec *pvec)
296{
297 struct radix_tree_iter iter;
298 void **slot;
299 int total_pages = 0;
300 int i = 0, tail_pages = 0;
301 struct page *page;
302 pgoff_t start;
303
304 start = pvec->pages[0]->index;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700305 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Jan Karaaa65c292017-11-15 17:37:33 -0800306 if (i >= pagevec_count(pvec) && !tail_pages)
307 break;
308 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700309 &mapping->i_pages.xa_lock);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400310 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800311 continue;
312 if (!tail_pages) {
313 /*
314 * Some page got inserted in our range? Skip it. We
315 * have our pages locked so they are protected from
316 * being removed.
317 */
318 if (page != pvec->pages[i])
319 continue;
320 WARN_ON_ONCE(!PageLocked(page));
321 if (PageTransHuge(page) && !PageHuge(page))
322 tail_pages = HPAGE_PMD_NR - 1;
323 page->mapping = NULL;
324 /*
325 * Leave page->index set: truncation lookup relies
326 * upon it
327 */
328 i++;
329 } else {
330 tail_pages--;
331 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700332 radix_tree_clear_tags(&mapping->i_pages, iter.node, slot);
333 __radix_tree_replace(&mapping->i_pages, iter.node, slot, NULL,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800334 workingset_lookup_update(mapping));
Jan Karaaa65c292017-11-15 17:37:33 -0800335 total_pages++;
336 }
337 mapping->nrpages -= total_pages;
338}
339
340void delete_from_page_cache_batch(struct address_space *mapping,
341 struct pagevec *pvec)
342{
343 int i;
344 unsigned long flags;
345
346 if (!pagevec_count(pvec))
347 return;
348
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700349 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800350 for (i = 0; i < pagevec_count(pvec); i++) {
351 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
352
353 unaccount_page_cache_page(mapping, pvec->pages[i]);
354 }
355 page_cache_tree_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700356 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800357
358 for (i = 0; i < pagevec_count(pvec); i++)
359 page_cache_free_page(mapping, pvec->pages[i]);
360}
361
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200362int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700363{
364 int ret = 0;
365 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700366 if (test_bit(AS_ENOSPC, &mapping->flags) &&
367 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700368 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700369 if (test_bit(AS_EIO, &mapping->flags) &&
370 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700371 ret = -EIO;
372 return ret;
373}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200374EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700375
Jeff Layton76341ca2017-07-06 07:02:22 -0400376static int filemap_check_and_keep_errors(struct address_space *mapping)
377{
378 /* Check for outstanding write errors */
379 if (test_bit(AS_EIO, &mapping->flags))
380 return -EIO;
381 if (test_bit(AS_ENOSPC, &mapping->flags))
382 return -ENOSPC;
383 return 0;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700387 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700388 * @mapping: address space structure to write
389 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800390 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700391 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700393 * Start writeback against all of a mapping's dirty pages that lie
394 * within the byte offsets <start, end> inclusive.
395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700397 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * these two operations is that if a dirty page/buffer is encountered, it must
399 * be waited upon, and not just skipped over.
400 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800401int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
402 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 int ret;
405 struct writeback_control wbc = {
406 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800407 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700408 .range_start = start,
409 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 };
411
412 if (!mapping_cap_writeback_dirty(mapping))
413 return 0;
414
Tejun Heob16b1de2015-06-02 08:39:48 -0600415 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600417 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return ret;
419}
420
421static inline int __filemap_fdatawrite(struct address_space *mapping,
422 int sync_mode)
423{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700424 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427int filemap_fdatawrite(struct address_space *mapping)
428{
429 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
430}
431EXPORT_SYMBOL(filemap_fdatawrite);
432
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400433int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800434 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
437}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400438EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Randy Dunlap485bb992006-06-23 02:03:49 -0700440/**
441 * filemap_flush - mostly a non-blocking flush
442 * @mapping: target address_space
443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * This is a mostly non-blocking flush. Not suitable for data-integrity
445 * purposes - I/O may not be started against all dirty pages.
446 */
447int filemap_flush(struct address_space *mapping)
448{
449 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
450}
451EXPORT_SYMBOL(filemap_flush);
452
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500453/**
454 * filemap_range_has_page - check if a page exists in range.
455 * @mapping: address space within which to check
456 * @start_byte: offset in bytes where the range starts
457 * @end_byte: offset in bytes where the range ends (inclusive)
458 *
459 * Find at least one page in the range supplied, usually used to check if
460 * direct writing in this range will trigger a writeback.
461 */
462bool filemap_range_has_page(struct address_space *mapping,
463 loff_t start_byte, loff_t end_byte)
464{
465 pgoff_t index = start_byte >> PAGE_SHIFT;
466 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700467 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500468
469 if (end_byte < start_byte)
470 return false;
471
472 if (mapping->nrpages == 0)
473 return false;
474
Jan Karaf7b68042017-09-06 16:21:40 -0700475 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500476 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700477 put_page(page);
478 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500479}
480EXPORT_SYMBOL(filemap_range_has_page);
481
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400482static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800483 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300485 pgoff_t index = start_byte >> PAGE_SHIFT;
486 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct pagevec pvec;
488 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200490 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400491 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Mel Gorman86679822017-11-15 17:37:52 -0800493 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800494 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unsigned i;
496
Jan Kara312e9d22017-11-15 17:35:05 -0800497 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800498 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800499 if (!nr_pages)
500 break;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 for (i = 0; i < nr_pages; i++) {
503 struct page *page = pvec.pages[i];
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400506 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 pagevec_release(&pvec);
509 cond_resched();
510 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800511}
512
513/**
514 * filemap_fdatawait_range - wait for writeback to complete
515 * @mapping: address space structure to wait for
516 * @start_byte: offset in bytes where the range starts
517 * @end_byte: offset in bytes where the range ends (inclusive)
518 *
519 * Walk the list of under-writeback pages of the given address space
520 * in the given range and wait for all of them. Check error status of
521 * the address space and return it.
522 *
523 * Since the error status of the address space is cleared by this function,
524 * callers are responsible for checking the return value and handling and/or
525 * reporting the error.
526 */
527int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
528 loff_t end_byte)
529{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400530 __filemap_fdatawait_range(mapping, start_byte, end_byte);
531 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200533EXPORT_SYMBOL(filemap_fdatawait_range);
534
535/**
Jeff Laytona823e452017-07-28 07:24:43 -0400536 * file_fdatawait_range - wait for writeback to complete
537 * @file: file pointing to address space structure to wait for
538 * @start_byte: offset in bytes where the range starts
539 * @end_byte: offset in bytes where the range ends (inclusive)
540 *
541 * Walk the list of under-writeback pages of the address space that file
542 * refers to, in the given range and wait for all of them. Check error
543 * status of the address space vs. the file->f_wb_err cursor and return it.
544 *
545 * Since the error status of the file is advanced by this function,
546 * callers are responsible for checking the return value and handling and/or
547 * reporting the error.
548 */
549int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
550{
551 struct address_space *mapping = file->f_mapping;
552
553 __filemap_fdatawait_range(mapping, start_byte, end_byte);
554 return file_check_and_advance_wb_err(file);
555}
556EXPORT_SYMBOL(file_fdatawait_range);
557
558/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800559 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
560 * @mapping: address space structure to wait for
561 *
562 * Walk the list of under-writeback pages of the given address space
563 * and wait for all of them. Unlike filemap_fdatawait(), this function
564 * does not clear error status of the address space.
565 *
566 * Use this function if callers don't handle errors themselves. Expected
567 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
568 * fsfreeze(8)
569 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400570int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800571{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400572 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400573 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800574}
Jeff Layton76341ca2017-07-06 07:02:22 -0400575EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800576
Jeff Layton9326c9b2017-07-26 10:21:11 -0400577static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400579 return (!dax_mapping(mapping) && mapping->nrpages) ||
580 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583int filemap_write_and_wait(struct address_space *mapping)
584{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800585 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jeff Layton9326c9b2017-07-26 10:21:11 -0400587 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800588 err = filemap_fdatawrite(mapping);
589 /*
590 * Even if the above returned error, the pages may be
591 * written partially (e.g. -ENOSPC), so we wait for it.
592 * But the -EIO is special case, it may indicate the worst
593 * thing (e.g. bug) happened, so we avoid waiting for it.
594 */
595 if (err != -EIO) {
596 int err2 = filemap_fdatawait(mapping);
597 if (!err)
598 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400599 } else {
600 /* Clear any previously stored errors */
601 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800602 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700603 } else {
604 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800608EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Randy Dunlap485bb992006-06-23 02:03:49 -0700610/**
611 * filemap_write_and_wait_range - write out & wait on a file range
612 * @mapping: the address_space for the pages
613 * @lstart: offset in bytes where the range starts
614 * @lend: offset in bytes where the range ends (inclusive)
615 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800616 * Write out and wait upon file offsets lstart->lend, inclusive.
617 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300618 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800619 * that this function can be used to write to the very end-of-file (end = -1).
620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621int filemap_write_and_wait_range(struct address_space *mapping,
622 loff_t lstart, loff_t lend)
623{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800624 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Jeff Layton9326c9b2017-07-26 10:21:11 -0400626 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800627 err = __filemap_fdatawrite_range(mapping, lstart, lend,
628 WB_SYNC_ALL);
629 /* See comment of filemap_write_and_wait() */
630 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200631 int err2 = filemap_fdatawait_range(mapping,
632 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800633 if (!err)
634 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400635 } else {
636 /* Clear any previously stored errors */
637 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800638 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700639 } else {
640 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
Chris Masonf6995582009-04-15 13:22:37 -0400644EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jeff Layton5660e132017-07-06 07:02:25 -0400646void __filemap_set_wb_err(struct address_space *mapping, int err)
647{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400648 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400649
650 trace_filemap_set_wb_err(mapping, eseq);
651}
652EXPORT_SYMBOL(__filemap_set_wb_err);
653
654/**
655 * file_check_and_advance_wb_err - report wb error (if any) that was previously
656 * and advance wb_err to current one
657 * @file: struct file on which the error is being reported
658 *
659 * When userland calls fsync (or something like nfsd does the equivalent), we
660 * want to report any writeback errors that occurred since the last fsync (or
661 * since the file was opened if there haven't been any).
662 *
663 * Grab the wb_err from the mapping. If it matches what we have in the file,
664 * then just quickly return 0. The file is all caught up.
665 *
666 * If it doesn't match, then take the mapping value, set the "seen" flag in
667 * it and try to swap it into place. If it works, or another task beat us
668 * to it with the new value, then update the f_wb_err and return the error
669 * portion. The error at this point must be reported via proper channels
670 * (a'la fsync, or NFS COMMIT operation, etc.).
671 *
672 * While we handle mapping->wb_err with atomic operations, the f_wb_err
673 * value is protected by the f_lock since we must ensure that it reflects
674 * the latest value swapped in for this file descriptor.
675 */
676int file_check_and_advance_wb_err(struct file *file)
677{
678 int err = 0;
679 errseq_t old = READ_ONCE(file->f_wb_err);
680 struct address_space *mapping = file->f_mapping;
681
682 /* Locklessly handle the common case where nothing has changed */
683 if (errseq_check(&mapping->wb_err, old)) {
684 /* Something changed, must use slow path */
685 spin_lock(&file->f_lock);
686 old = file->f_wb_err;
687 err = errseq_check_and_advance(&mapping->wb_err,
688 &file->f_wb_err);
689 trace_file_check_and_advance_wb_err(file, old);
690 spin_unlock(&file->f_lock);
691 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700692
693 /*
694 * We're mostly using this function as a drop in replacement for
695 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
696 * that the legacy code would have had on these flags.
697 */
698 clear_bit(AS_EIO, &mapping->flags);
699 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400700 return err;
701}
702EXPORT_SYMBOL(file_check_and_advance_wb_err);
703
704/**
705 * file_write_and_wait_range - write out & wait on a file range
706 * @file: file pointing to address_space with pages
707 * @lstart: offset in bytes where the range starts
708 * @lend: offset in bytes where the range ends (inclusive)
709 *
710 * Write out and wait upon file offsets lstart->lend, inclusive.
711 *
712 * Note that @lend is inclusive (describes the last byte to be written) so
713 * that this function can be used to write to the very end-of-file (end = -1).
714 *
715 * After writing out and waiting on the data, we check and advance the
716 * f_wb_err cursor to the latest value, and return any errors detected there.
717 */
718int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
719{
720 int err = 0, err2;
721 struct address_space *mapping = file->f_mapping;
722
Jeff Layton9326c9b2017-07-26 10:21:11 -0400723 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400724 err = __filemap_fdatawrite_range(mapping, lstart, lend,
725 WB_SYNC_ALL);
726 /* See comment of filemap_write_and_wait() */
727 if (err != -EIO)
728 __filemap_fdatawait_range(mapping, lstart, lend);
729 }
730 err2 = file_check_and_advance_wb_err(file);
731 if (!err)
732 err = err2;
733 return err;
734}
735EXPORT_SYMBOL(file_write_and_wait_range);
736
Randy Dunlap485bb992006-06-23 02:03:49 -0700737/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700738 * replace_page_cache_page - replace a pagecache page with a new one
739 * @old: page to be replaced
740 * @new: page to replace with
741 * @gfp_mask: allocation mode
742 *
743 * This function replaces a page in the pagecache with a new one. On
744 * success it acquires the pagecache reference for the new page and
745 * drops it for the old page. Both the old and new pages must be
746 * locked. This function does not add the new page to the LRU, the
747 * caller must do that.
748 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500749 * The remove + add is atomic. This function cannot fail.
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700750 */
751int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
752{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500753 struct address_space *mapping = old->mapping;
754 void (*freepage)(struct page *) = mapping->a_ops->freepage;
755 pgoff_t offset = old->index;
756 XA_STATE(xas, &mapping->i_pages, offset);
757 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700758
Sasha Levin309381fea2014-01-23 15:52:54 -0800759 VM_BUG_ON_PAGE(!PageLocked(old), old);
760 VM_BUG_ON_PAGE(!PageLocked(new), new);
761 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700762
Matthew Wilcox74d60952017-11-17 10:01:45 -0500763 get_page(new);
764 new->mapping = mapping;
765 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700766
Matthew Wilcox74d60952017-11-17 10:01:45 -0500767 xas_lock_irqsave(&xas, flags);
768 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700769
Matthew Wilcox74d60952017-11-17 10:01:45 -0500770 old->mapping = NULL;
771 /* hugetlb pages do not participate in page cache accounting. */
772 if (!PageHuge(old))
773 __dec_node_page_state(new, NR_FILE_PAGES);
774 if (!PageHuge(new))
775 __inc_node_page_state(new, NR_FILE_PAGES);
776 if (PageSwapBacked(old))
777 __dec_node_page_state(new, NR_SHMEM);
778 if (PageSwapBacked(new))
779 __inc_node_page_state(new, NR_SHMEM);
780 xas_unlock_irqrestore(&xas, flags);
781 mem_cgroup_migrate(old, new);
782 if (freepage)
783 freepage(old);
784 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700785
Matthew Wilcox74d60952017-11-17 10:01:45 -0500786 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700787}
788EXPORT_SYMBOL_GPL(replace_page_cache_page);
789
Johannes Weinera5289102014-04-03 14:47:51 -0700790static int __add_to_page_cache_locked(struct page *page,
791 struct address_space *mapping,
792 pgoff_t offset, gfp_t gfp_mask,
793 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500795 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700796 int huge = PageHuge(page);
797 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700798 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500799 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700800
Sasha Levin309381fea2014-01-23 15:52:54 -0800801 VM_BUG_ON_PAGE(!PageLocked(page), page);
802 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500803 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700804
Johannes Weiner00501b52014-08-08 14:19:20 -0700805 if (!huge) {
806 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800807 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700808 if (error)
809 return error;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300812 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700813 page->mapping = mapping;
814 page->index = offset;
815
Matthew Wilcox74d60952017-11-17 10:01:45 -0500816 do {
817 xas_lock_irq(&xas);
818 old = xas_load(&xas);
819 if (old && !xa_is_value(old))
820 xas_set_err(&xas, -EEXIST);
821 xas_store(&xas, page);
822 if (xas_error(&xas))
823 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700824
Matthew Wilcox74d60952017-11-17 10:01:45 -0500825 if (xa_is_value(old)) {
826 mapping->nrexceptional--;
827 if (shadowp)
828 *shadowp = old;
829 }
830 mapping->nrpages++;
831
832 /* hugetlb pages do not participate in page cache accounting */
833 if (!huge)
834 __inc_node_page_state(page, NR_FILE_PAGES);
835unlock:
836 xas_unlock_irq(&xas);
837 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
838
839 if (xas_error(&xas))
840 goto error;
841
Johannes Weiner00501b52014-08-08 14:19:20 -0700842 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800843 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700844 trace_mm_filemap_add_to_page_cache(page);
845 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500846error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700847 page->mapping = NULL;
848 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700849 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800850 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300851 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500852 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
Johannes Weinera5289102014-04-03 14:47:51 -0700854
855/**
856 * add_to_page_cache_locked - add a locked page to the pagecache
857 * @page: page to add
858 * @mapping: the page's address_space
859 * @offset: page index
860 * @gfp_mask: page allocation mode
861 *
862 * This function is used to add a page to the pagecache. It must be locked.
863 * This function does not add the page to the LRU. The caller must do that.
864 */
865int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
866 pgoff_t offset, gfp_t gfp_mask)
867{
868 return __add_to_page_cache_locked(page, mapping, offset,
869 gfp_mask, NULL);
870}
Nick Piggine2867812008-07-25 19:45:30 -0700871EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400874 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Johannes Weinera5289102014-04-03 14:47:51 -0700876 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700877 int ret;
878
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800879 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700880 ret = __add_to_page_cache_locked(page, mapping, offset,
881 gfp_mask, &shadow);
882 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800883 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700884 else {
885 /*
886 * The page might have been evicted from cache only
887 * recently, in which case it should be activated like
888 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700889 * The exception is pages getting rewritten; evicting other
890 * data from the working set, only to cache data that will
891 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700892 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700893 if (!(gfp_mask & __GFP_WRITE) &&
894 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700895 SetPageActive(page);
896 workingset_activation(page);
897 } else
898 ClearPageActive(page);
899 lru_cache_add(page);
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ret;
902}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300903EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Paul Jackson44110fe2006-03-24 03:16:04 -0800905#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700906struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800907{
Miao Xiec0ff7452010-05-24 14:32:08 -0700908 int n;
909 struct page *page;
910
Paul Jackson44110fe2006-03-24 03:16:04 -0800911 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700912 unsigned int cpuset_mems_cookie;
913 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700914 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700915 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700916 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700917 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700918
Miao Xiec0ff7452010-05-24 14:32:08 -0700919 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800920 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700921 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800922}
Nick Piggin2ae88142006-10-28 10:38:23 -0700923EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800924#endif
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
927 * In order to wait for pages to become available there must be
928 * waitqueues associated with pages. By using a hash table of
929 * waitqueues where the bucket discipline is to maintain all
930 * waiters on the same queue and wake all when any of the pages
931 * become available, and for the woken contexts to check to be
932 * sure the appropriate page became available, this saves space
933 * at a cost of "thundering herd" phenomena during rare hash
934 * collisions.
935 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000936#define PAGE_WAIT_TABLE_BITS 8
937#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
938static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
939
940static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Nicholas Piggin62906022016-12-25 13:00:30 +1000942 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
Nicholas Piggin62906022016-12-25 13:00:30 +1000944
945void __init pagecache_init(void)
946{
947 int i;
948
949 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
950 init_waitqueue_head(&page_wait_table[i]);
951
952 page_writeback_init();
953}
954
Linus Torvalds3510ca22017-08-27 13:55:12 -0700955/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000956struct wait_page_key {
957 struct page *page;
958 int bit_nr;
959 int page_match;
960};
961
962struct wait_page_queue {
963 struct page *page;
964 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200965 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000966};
967
Ingo Molnarac6424b2017-06-20 12:06:13 +0200968static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000969{
970 struct wait_page_key *key = arg;
971 struct wait_page_queue *wait_page
972 = container_of(wait, struct wait_page_queue, wait);
973
974 if (wait_page->page != key->page)
975 return 0;
976 key->page_match = 1;
977
978 if (wait_page->bit_nr != key->bit_nr)
979 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -0700980
981 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +1000982 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -0700983 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +1000984
985 return autoremove_wake_function(wait, mode, sync, key);
986}
987
Nicholas Piggin74d81bf2017-02-22 15:44:41 -0800988static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +1000989{
990 wait_queue_head_t *q = page_waitqueue(page);
991 struct wait_page_key key;
992 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -0700993 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +1000994
995 key.page = page;
996 key.bit_nr = bit_nr;
997 key.page_match = 0;
998
Tim Chen11a19c72017-08-25 09:13:55 -0700999 bookmark.flags = 0;
1000 bookmark.private = NULL;
1001 bookmark.func = NULL;
1002 INIT_LIST_HEAD(&bookmark.entry);
1003
Nicholas Piggin62906022016-12-25 13:00:30 +10001004 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001005 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1006
1007 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1008 /*
1009 * Take a breather from holding the lock,
1010 * allow pages that finish wake up asynchronously
1011 * to acquire the lock and remove themselves
1012 * from wait queue
1013 */
1014 spin_unlock_irqrestore(&q->lock, flags);
1015 cpu_relax();
1016 spin_lock_irqsave(&q->lock, flags);
1017 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1018 }
1019
Nicholas Piggin62906022016-12-25 13:00:30 +10001020 /*
1021 * It is possible for other pages to have collided on the waitqueue
1022 * hash, so in that case check for a page match. That prevents a long-
1023 * term waiter
1024 *
1025 * It is still possible to miss a case here, when we woke page waiters
1026 * and removed them from the waitqueue, but there are still other
1027 * page waiters.
1028 */
1029 if (!waitqueue_active(q) || !key.page_match) {
1030 ClearPageWaiters(page);
1031 /*
1032 * It's possible to miss clearing Waiters here, when we woke
1033 * our page waiters, but the hashed waitqueue has waiters for
1034 * other pages on it.
1035 *
1036 * That's okay, it's a rare case. The next waker will clear it.
1037 */
1038 }
1039 spin_unlock_irqrestore(&q->lock, flags);
1040}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001041
1042static void wake_up_page(struct page *page, int bit)
1043{
1044 if (!PageWaiters(page))
1045 return;
1046 wake_up_page_bit(page, bit);
1047}
Nicholas Piggin62906022016-12-25 13:00:30 +10001048
1049static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1050 struct page *page, int bit_nr, int state, bool lock)
1051{
1052 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001053 wait_queue_entry_t *wait = &wait_page.wait;
Nicholas Piggin62906022016-12-25 13:00:30 +10001054 int ret = 0;
1055
1056 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001057 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001058 wait->func = wake_page_function;
1059 wait_page.page = page;
1060 wait_page.bit_nr = bit_nr;
1061
1062 for (;;) {
1063 spin_lock_irq(&q->lock);
1064
Ingo Molnar2055da92017-06-20 12:06:46 +02001065 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001066 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001067 SetPageWaiters(page);
1068 }
1069
1070 set_current_state(state);
1071
1072 spin_unlock_irq(&q->lock);
1073
1074 if (likely(test_bit(bit_nr, &page->flags))) {
1075 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001076 }
1077
1078 if (lock) {
1079 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1080 break;
1081 } else {
1082 if (!test_bit(bit_nr, &page->flags))
1083 break;
1084 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001085
1086 if (unlikely(signal_pending_state(state, current))) {
1087 ret = -EINTR;
1088 break;
1089 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001090 }
1091
1092 finish_wait(q, wait);
1093
1094 /*
1095 * A signal could leave PageWaiters set. Clearing it here if
1096 * !waitqueue_active would be possible (by open-coding finish_wait),
1097 * but still fail to catch it in the case of wait hash collision. We
1098 * already can fail to clear wait hash collision cases, so don't
1099 * bother with signals either.
1100 */
1101
1102 return ret;
1103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Harvey Harrison920c7a52008-02-04 22:29:26 -08001105void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Nicholas Piggin62906022016-12-25 13:00:30 +10001107 wait_queue_head_t *q = page_waitqueue(page);
1108 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110EXPORT_SYMBOL(wait_on_page_bit);
1111
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001112int wait_on_page_bit_killable(struct page *page, int bit_nr)
1113{
Nicholas Piggin62906022016-12-25 13:00:30 +10001114 wait_queue_head_t *q = page_waitqueue(page);
1115 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001116}
David Howells4343d002017-11-02 15:27:52 +00001117EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001120 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001121 * @page: Page defining the wait queue of interest
1122 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001123 *
1124 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1125 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001126void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001127{
1128 wait_queue_head_t *q = page_waitqueue(page);
1129 unsigned long flags;
1130
1131 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001132 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001133 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001134 spin_unlock_irqrestore(&q->lock, flags);
1135}
1136EXPORT_SYMBOL_GPL(add_page_wait_queue);
1137
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001138#ifndef clear_bit_unlock_is_negative_byte
1139
1140/*
1141 * PG_waiters is the high bit in the same byte as PG_lock.
1142 *
1143 * On x86 (and on many other architectures), we can clear PG_lock and
1144 * test the sign bit at the same time. But if the architecture does
1145 * not support that special operation, we just do this all by hand
1146 * instead.
1147 *
1148 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1149 * being cleared, but a memory barrier should be unneccssary since it is
1150 * in the same byte as PG_locked.
1151 */
1152static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1153{
1154 clear_bit_unlock(nr, mem);
1155 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001156 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001157}
1158
1159#endif
1160
David Howells385e1ca5f2009-04-03 16:42:39 +01001161/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001162 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * @page: the page
1164 *
1165 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1166 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001167 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1169 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001170 * Note that this depends on PG_waiters being the sign bit in the byte
1171 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1172 * clear the PG_locked bit and test PG_waiters at the same time fairly
1173 * portably (architectures that do LL/SC can test any bit, while x86 can
1174 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001176void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001178 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001179 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001180 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001181 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1182 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184EXPORT_SYMBOL(unlock_page);
1185
Randy Dunlap485bb992006-06-23 02:03:49 -07001186/**
1187 * end_page_writeback - end writeback against a page
1188 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190void end_page_writeback(struct page *page)
1191{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001192 /*
1193 * TestClearPageReclaim could be used here but it is an atomic
1194 * operation and overkill in this particular case. Failing to
1195 * shuffle a page marked for immediate reclaim is too mild to
1196 * justify taking an atomic operation penalty at the end of
1197 * ever page writeback.
1198 */
1199 if (PageReclaim(page)) {
1200 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001201 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001202 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001203
1204 if (!test_clear_page_writeback(page))
1205 BUG();
1206
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001207 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 wake_up_page(page, PG_writeback);
1209}
1210EXPORT_SYMBOL(end_page_writeback);
1211
Matthew Wilcox57d99842014-06-04 16:07:45 -07001212/*
1213 * After completing I/O on a page, call this routine to update the page
1214 * flags appropriately
1215 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001216void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001217{
Jens Axboec11f0c02016-08-05 08:11:04 -06001218 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001219 if (!err) {
1220 SetPageUptodate(page);
1221 } else {
1222 ClearPageUptodate(page);
1223 SetPageError(page);
1224 }
1225 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001226 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001227 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001228 struct address_space *mapping;
1229
Matthew Wilcox57d99842014-06-04 16:07:45 -07001230 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001231 mapping = page_mapping(page);
1232 if (mapping)
1233 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001234 }
1235 end_page_writeback(page);
1236 }
1237}
1238EXPORT_SYMBOL_GPL(page_endio);
1239
Randy Dunlap485bb992006-06-23 02:03:49 -07001240/**
1241 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001242 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001244void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Nicholas Piggin62906022016-12-25 13:00:30 +10001246 struct page *page = compound_head(__page);
1247 wait_queue_head_t *q = page_waitqueue(page);
1248 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250EXPORT_SYMBOL(__lock_page);
1251
Nicholas Piggin62906022016-12-25 13:00:30 +10001252int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001253{
Nicholas Piggin62906022016-12-25 13:00:30 +10001254 struct page *page = compound_head(__page);
1255 wait_queue_head_t *q = page_waitqueue(page);
1256 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001257}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001258EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001259
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001260/*
1261 * Return values:
1262 * 1 - page is locked; mmap_sem is still held.
1263 * 0 - page is not locked.
1264 * mmap_sem has been released (up_read()), unless flags had both
1265 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1266 * which case mmap_sem is still held.
1267 *
1268 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1269 * with the page locked and the mmap_sem unperturbed.
1270 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001271int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1272 unsigned int flags)
1273{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001274 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1275 /*
1276 * CAUTION! In this case, mmap_sem is not released
1277 * even though return 0.
1278 */
1279 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1280 return 0;
1281
1282 up_read(&mm->mmap_sem);
1283 if (flags & FAULT_FLAG_KILLABLE)
1284 wait_on_page_locked_killable(page);
1285 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001286 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001287 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001288 } else {
1289 if (flags & FAULT_FLAG_KILLABLE) {
1290 int ret;
1291
1292 ret = __lock_page_killable(page);
1293 if (ret) {
1294 up_read(&mm->mmap_sem);
1295 return 0;
1296 }
1297 } else
1298 __lock_page(page);
1299 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001300 }
1301}
1302
Randy Dunlap485bb992006-06-23 02:03:49 -07001303/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001304 * page_cache_next_miss() - Find the next gap in the page cache.
1305 * @mapping: Mapping.
1306 * @index: Index.
1307 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001308 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001309 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1310 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001311 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001312 * This function may be called under the rcu_read_lock. However, this will
1313 * not atomically search a snapshot of the cache at a single point in time.
1314 * For example, if a gap is created at index 5, then subsequently a gap is
1315 * created at index 10, page_cache_next_miss covering both indices may
1316 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001317 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001318 * Return: The index of the gap if found, otherwise an index outside the
1319 * range specified (in which case 'return - index >= max_scan' will be true).
1320 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001321 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001322pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001323 pgoff_t index, unsigned long max_scan)
1324{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001325 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001326
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001327 while (max_scan--) {
1328 void *entry = xas_next(&xas);
1329 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001330 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001331 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001332 break;
1333 }
1334
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001335 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001336}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001337EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001338
1339/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001340 * page_cache_prev_miss() - Find the next gap in the page cache.
1341 * @mapping: Mapping.
1342 * @index: Index.
1343 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001344 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001345 * Search the range [max(index - max_scan + 1, 0), index] for the
1346 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001347 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001348 * This function may be called under the rcu_read_lock. However, this will
1349 * not atomically search a snapshot of the cache at a single point in time.
1350 * For example, if a gap is created at index 10, then subsequently a gap is
1351 * created at index 5, page_cache_prev_miss() covering both indices may
1352 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001353 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001354 * Return: The index of the gap if found, otherwise an index outside the
1355 * range specified (in which case 'index - return >= max_scan' will be true).
1356 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001357 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001358pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001359 pgoff_t index, unsigned long max_scan)
1360{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001361 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001362
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001363 while (max_scan--) {
1364 void *entry = xas_prev(&xas);
1365 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001366 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001367 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001368 break;
1369 }
1370
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001371 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001372}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001373EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001374
1375/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001376 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001377 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001378 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001379 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001380 * Looks up the page cache slot at @mapping & @offset. If there is a
1381 * page cache page, it is returned with an increased refcount.
1382 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001383 * If the slot holds a shadow entry of a previously evicted page, or a
1384 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001385 *
1386 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001388struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Nick Piggina60637c2008-07-25 19:45:31 -07001390 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001391 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Nick Piggina60637c2008-07-25 19:45:31 -07001393 rcu_read_lock();
1394repeat:
1395 page = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001396 pagep = radix_tree_lookup_slot(&mapping->i_pages, offset);
Nick Piggina60637c2008-07-25 19:45:31 -07001397 if (pagep) {
1398 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001399 if (unlikely(!page))
1400 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001401 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001402 if (radix_tree_deref_retry(page))
1403 goto repeat;
1404 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001405 * A shadow entry of a recently evicted page,
1406 * or a swap entry from shmem/tmpfs. Return
1407 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001408 */
1409 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001410 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001411
1412 head = compound_head(page);
1413 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001414 goto repeat;
1415
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001416 /* The page was split under us? */
1417 if (compound_head(page) != head) {
1418 put_page(head);
1419 goto repeat;
1420 }
1421
Nick Piggina60637c2008-07-25 19:45:31 -07001422 /*
1423 * Has the page moved?
1424 * This is part of the lockless pagecache protocol. See
1425 * include/linux/pagemap.h for details.
1426 */
1427 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001428 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001429 goto repeat;
1430 }
1431 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001432out:
Nick Piggina60637c2008-07-25 19:45:31 -07001433 rcu_read_unlock();
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return page;
1436}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001437EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Randy Dunlap485bb992006-06-23 02:03:49 -07001439/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001440 * find_lock_entry - locate, pin and lock a page cache entry
1441 * @mapping: the address_space to search
1442 * @offset: the page cache index
1443 *
1444 * Looks up the page cache slot at @mapping & @offset. If there is a
1445 * page cache page, it is returned locked and with an increased
1446 * refcount.
1447 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001448 * If the slot holds a shadow entry of a previously evicted page, or a
1449 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001450 *
1451 * Otherwise, %NULL is returned.
1452 *
1453 * find_lock_entry() may sleep.
1454 */
1455struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct page *page;
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001460 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001461 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001462 lock_page(page);
1463 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001464 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001465 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001466 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001467 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001469 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return page;
1472}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001473EXPORT_SYMBOL(find_lock_entry);
1474
1475/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001476 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001477 * @mapping: the address_space to search
1478 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001479 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001480 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001481 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001482 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001483 *
Randy Dunlap75325182014-07-30 16:08:37 -07001484 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001485 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001486 * @fgp_flags can be:
1487 *
1488 * - FGP_ACCESSED: the page will be marked accessed
1489 * - FGP_LOCK: Page is return locked
1490 * - FGP_CREAT: If page is not present then a new page is allocated using
1491 * @gfp_mask and added to the page cache and the VM's LRU
1492 * list. The page is returned locked and with an increased
1493 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001494 *
1495 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1496 * if the GFP flags specified for FGP_CREAT are atomic.
1497 *
1498 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001499 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001500struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001501 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Nick Piggineb2be182007-10-16 01:24:57 -07001503 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001506 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001507 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001508 page = NULL;
1509 if (!page)
1510 goto no_page;
1511
1512 if (fgp_flags & FGP_LOCK) {
1513 if (fgp_flags & FGP_NOWAIT) {
1514 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001515 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001516 return NULL;
1517 }
1518 } else {
1519 lock_page(page);
1520 }
1521
1522 /* Has the page been truncated? */
1523 if (unlikely(page->mapping != mapping)) {
1524 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001525 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001526 goto repeat;
1527 }
1528 VM_BUG_ON_PAGE(page->index != offset, page);
1529 }
1530
1531 if (page && (fgp_flags & FGP_ACCESSED))
1532 mark_page_accessed(page);
1533
1534no_page:
1535 if (!page && (fgp_flags & FGP_CREAT)) {
1536 int err;
1537 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001538 gfp_mask |= __GFP_WRITE;
1539 if (fgp_flags & FGP_NOFS)
1540 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001541
Michal Hocko45f87de2014-12-29 20:30:35 +01001542 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001543 if (!page)
1544 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001545
1546 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1547 fgp_flags |= FGP_LOCK;
1548
Hugh Dickinseb39d612014-08-06 16:06:43 -07001549 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001550 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001551 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001552
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001553 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001554 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001555 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001556 page = NULL;
1557 if (err == -EEXIST)
1558 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return page;
1563}
Mel Gorman2457aec2014-06-04 16:10:31 -07001564EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001567 * find_get_entries - gang pagecache lookup
1568 * @mapping: The address_space to search
1569 * @start: The starting page cache index
1570 * @nr_entries: The maximum number of entries
1571 * @entries: Where the resulting entries are placed
1572 * @indices: The cache indices corresponding to the entries in @entries
1573 *
1574 * find_get_entries() will search for and return a group of up to
1575 * @nr_entries entries in the mapping. The entries are placed at
1576 * @entries. find_get_entries() takes a reference against any actual
1577 * pages it returns.
1578 *
1579 * The search returns a group of mapping-contiguous page cache entries
1580 * with ascending indexes. There may be holes in the indices due to
1581 * not-present pages.
1582 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001583 * Any shadow entries of evicted pages, or swap entries from
1584 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001585 *
1586 * find_get_entries() returns the number of pages and shadow entries
1587 * which were found.
1588 */
1589unsigned find_get_entries(struct address_space *mapping,
1590 pgoff_t start, unsigned int nr_entries,
1591 struct page **entries, pgoff_t *indices)
1592{
1593 void **slot;
1594 unsigned int ret = 0;
1595 struct radix_tree_iter iter;
1596
1597 if (!nr_entries)
1598 return 0;
1599
1600 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001601 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001602 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001603repeat:
1604 page = radix_tree_deref_slot(slot);
1605 if (unlikely(!page))
1606 continue;
1607 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001608 if (radix_tree_deref_retry(page)) {
1609 slot = radix_tree_iter_retry(&iter);
1610 continue;
1611 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001612 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001613 * A shadow entry of a recently evicted page, a swap
1614 * entry from shmem/tmpfs or a DAX entry. Return it
1615 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001616 */
1617 goto export;
1618 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001619
1620 head = compound_head(page);
1621 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001622 goto repeat;
1623
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001624 /* The page was split under us? */
1625 if (compound_head(page) != head) {
1626 put_page(head);
1627 goto repeat;
1628 }
1629
Johannes Weiner0cd61442014-04-03 14:47:46 -07001630 /* Has the page moved? */
1631 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001632 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001633 goto repeat;
1634 }
1635export:
1636 indices[ret] = iter.index;
1637 entries[ret] = page;
1638 if (++ret == nr_entries)
1639 break;
1640 }
1641 rcu_read_unlock();
1642 return ret;
1643}
1644
1645/**
Jan Karab947cee2017-09-06 16:21:21 -07001646 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * @mapping: The address_space to search
1648 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001649 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * @nr_pages: The maximum number of pages
1651 * @pages: Where the resulting pages are placed
1652 *
Jan Karab947cee2017-09-06 16:21:21 -07001653 * find_get_pages_range() will search for and return a group of up to @nr_pages
1654 * pages in the mapping starting at index @start and up to index @end
1655 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1656 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 *
1658 * The search returns a group of mapping-contiguous pages with ascending
1659 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001660 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 *
Jan Karab947cee2017-09-06 16:21:21 -07001662 * find_get_pages_range() returns the number of pages which were found. If this
1663 * number is smaller than @nr_pages, the end of specified range has been
1664 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
Jan Karab947cee2017-09-06 16:21:21 -07001666unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1667 pgoff_t end, unsigned int nr_pages,
1668 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001670 struct radix_tree_iter iter;
1671 void **slot;
1672 unsigned ret = 0;
1673
1674 if (unlikely(!nr_pages))
1675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Nick Piggina60637c2008-07-25 19:45:31 -07001677 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001678 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001679 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001680
1681 if (iter.index > end)
1682 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001683repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001684 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001685 if (unlikely(!page))
1686 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001687
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001688 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001689 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001690 slot = radix_tree_iter_retry(&iter);
1691 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001692 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001693 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001694 * A shadow entry of a recently evicted page,
1695 * or a swap entry from shmem/tmpfs. Skip
1696 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001697 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001698 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001699 }
Nick Piggina60637c2008-07-25 19:45:31 -07001700
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001701 head = compound_head(page);
1702 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001703 goto repeat;
1704
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001705 /* The page was split under us? */
1706 if (compound_head(page) != head) {
1707 put_page(head);
1708 goto repeat;
1709 }
1710
Nick Piggina60637c2008-07-25 19:45:31 -07001711 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001712 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001713 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001714 goto repeat;
1715 }
1716
1717 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001718 if (++ret == nr_pages) {
1719 *start = pages[ret - 1]->index + 1;
1720 goto out;
1721 }
Nick Piggina60637c2008-07-25 19:45:31 -07001722 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001723
Jan Karab947cee2017-09-06 16:21:21 -07001724 /*
1725 * We come here when there is no page beyond @end. We take care to not
1726 * overflow the index @start as it confuses some of the callers. This
1727 * breaks the iteration when there is page at index -1 but that is
1728 * already broken anyway.
1729 */
1730 if (end == (pgoff_t)-1)
1731 *start = (pgoff_t)-1;
1732 else
1733 *start = end + 1;
1734out:
Nick Piggina60637c2008-07-25 19:45:31 -07001735 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return ret;
1738}
1739
Jens Axboeebf43502006-04-27 08:46:01 +02001740/**
1741 * find_get_pages_contig - gang contiguous pagecache lookup
1742 * @mapping: The address_space to search
1743 * @index: The starting page index
1744 * @nr_pages: The maximum number of pages
1745 * @pages: Where the resulting pages are placed
1746 *
1747 * find_get_pages_contig() works exactly like find_get_pages(), except
1748 * that the returned number of pages are guaranteed to be contiguous.
1749 *
1750 * find_get_pages_contig() returns the number of pages which were found.
1751 */
1752unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1753 unsigned int nr_pages, struct page **pages)
1754{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001755 struct radix_tree_iter iter;
1756 void **slot;
1757 unsigned int ret = 0;
1758
1759 if (unlikely(!nr_pages))
1760 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001761
Nick Piggina60637c2008-07-25 19:45:31 -07001762 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001763 radix_tree_for_each_contig(slot, &mapping->i_pages, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001764 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001765repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001766 page = radix_tree_deref_slot(slot);
1767 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001768 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001769 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001770
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001771 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001772 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001773 slot = radix_tree_iter_retry(&iter);
1774 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001775 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001776 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001777 * A shadow entry of a recently evicted page,
1778 * or a swap entry from shmem/tmpfs. Stop
1779 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001780 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001781 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001782 }
Nick Piggina60637c2008-07-25 19:45:31 -07001783
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001784 head = compound_head(page);
1785 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001786 goto repeat;
1787
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001788 /* The page was split under us? */
1789 if (compound_head(page) != head) {
1790 put_page(head);
1791 goto repeat;
1792 }
1793
Nick Piggina60637c2008-07-25 19:45:31 -07001794 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001795 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001796 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001797 goto repeat;
1798 }
1799
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001800 /*
1801 * must check mapping and index after taking the ref.
1802 * otherwise we can get both false positives and false
1803 * negatives, which is just confusing to the caller.
1804 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001805 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001806 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001807 break;
1808 }
1809
Nick Piggina60637c2008-07-25 19:45:31 -07001810 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001811 if (++ret == nr_pages)
1812 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001813 }
Nick Piggina60637c2008-07-25 19:45:31 -07001814 rcu_read_unlock();
1815 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001816}
David Howellsef71c152007-05-09 02:33:44 -07001817EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001818
Randy Dunlap485bb992006-06-23 02:03:49 -07001819/**
Jan Kara72b045a2017-11-15 17:34:33 -08001820 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001821 * @mapping: the address_space to search
1822 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001823 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001824 * @tag: the tag index
1825 * @nr_pages: the maximum number of pages
1826 * @pages: where the resulting pages are placed
1827 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001829 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Jan Kara72b045a2017-11-15 17:34:33 -08001831unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1832 pgoff_t end, int tag, unsigned int nr_pages,
1833 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001835 struct radix_tree_iter iter;
1836 void **slot;
1837 unsigned ret = 0;
1838
1839 if (unlikely(!nr_pages))
1840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Nick Piggina60637c2008-07-25 19:45:31 -07001842 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001843 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001844 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001845
1846 if (iter.index > end)
1847 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001848repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001849 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001850 if (unlikely(!page))
1851 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001852
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001853 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001854 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001855 slot = radix_tree_iter_retry(&iter);
1856 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001857 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001858 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001859 * A shadow entry of a recently evicted page.
1860 *
1861 * Those entries should never be tagged, but
1862 * this tree walk is lockless and the tags are
1863 * looked up in bulk, one radix tree node at a
1864 * time, so there is a sizable window for page
1865 * reclaim to evict a page we saw tagged.
1866 *
1867 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001868 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001869 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001870 }
Nick Piggina60637c2008-07-25 19:45:31 -07001871
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001872 head = compound_head(page);
1873 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001874 goto repeat;
1875
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001876 /* The page was split under us? */
1877 if (compound_head(page) != head) {
1878 put_page(head);
1879 goto repeat;
1880 }
1881
Nick Piggina60637c2008-07-25 19:45:31 -07001882 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001883 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001884 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001885 goto repeat;
1886 }
1887
1888 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001889 if (++ret == nr_pages) {
1890 *index = pages[ret - 1]->index + 1;
1891 goto out;
1892 }
Nick Piggina60637c2008-07-25 19:45:31 -07001893 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001894
Jan Kara72b045a2017-11-15 17:34:33 -08001895 /*
1896 * We come here when we got at @end. We take care to not overflow the
1897 * index @index as it confuses some of the callers. This breaks the
1898 * iteration when there is page at index -1 but that is already broken
1899 * anyway.
1900 */
1901 if (end == (pgoff_t)-1)
1902 *index = (pgoff_t)-1;
1903 else
1904 *index = end + 1;
1905out:
Nick Piggina60637c2008-07-25 19:45:31 -07001906 rcu_read_unlock();
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return ret;
1909}
Jan Kara72b045a2017-11-15 17:34:33 -08001910EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001912/**
1913 * find_get_entries_tag - find and return entries that match @tag
1914 * @mapping: the address_space to search
1915 * @start: the starting page cache index
1916 * @tag: the tag index
1917 * @nr_entries: the maximum number of entries
1918 * @entries: where the resulting entries are placed
1919 * @indices: the cache indices corresponding to the entries in @entries
1920 *
1921 * Like find_get_entries, except we only return entries which are tagged with
1922 * @tag.
1923 */
1924unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1925 int tag, unsigned int nr_entries,
1926 struct page **entries, pgoff_t *indices)
1927{
1928 void **slot;
1929 unsigned int ret = 0;
1930 struct radix_tree_iter iter;
1931
1932 if (!nr_entries)
1933 return 0;
1934
1935 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001936 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001937 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001938repeat:
1939 page = radix_tree_deref_slot(slot);
1940 if (unlikely(!page))
1941 continue;
1942 if (radix_tree_exception(page)) {
1943 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001944 slot = radix_tree_iter_retry(&iter);
1945 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001946 }
1947
1948 /*
1949 * A shadow entry of a recently evicted page, a swap
1950 * entry from shmem/tmpfs or a DAX entry. Return it
1951 * without attempting to raise page count.
1952 */
1953 goto export;
1954 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001955
1956 head = compound_head(page);
1957 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001958 goto repeat;
1959
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001960 /* The page was split under us? */
1961 if (compound_head(page) != head) {
1962 put_page(head);
1963 goto repeat;
1964 }
1965
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001966 /* Has the page moved? */
1967 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001968 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001969 goto repeat;
1970 }
1971export:
1972 indices[ret] = iter.index;
1973 entries[ret] = page;
1974 if (++ret == nr_entries)
1975 break;
1976 }
1977 rcu_read_unlock();
1978 return ret;
1979}
1980EXPORT_SYMBOL(find_get_entries_tag);
1981
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001982/*
1983 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1984 * a _large_ part of the i/o request. Imagine the worst scenario:
1985 *
1986 * ---R__________________________________________B__________
1987 * ^ reading here ^ bad block(assume 4k)
1988 *
1989 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1990 * => failing the whole request => read(R) => read(R+1) =>
1991 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1992 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1993 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1994 *
1995 * It is going insane. Fix it by quickly scaling down the readahead size.
1996 */
1997static void shrink_readahead_size_eio(struct file *filp,
1998 struct file_ra_state *ra)
1999{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002000 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002001}
2002
Randy Dunlap485bb992006-06-23 02:03:49 -07002003/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002004 * generic_file_buffered_read - generic file read routine
2005 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002006 * @iter: data destination
2007 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002010 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 *
2012 * This is really ugly. But the goto's actually try to clarify some
2013 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002015static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002016 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002018 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002019 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002021 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002022 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002023 pgoff_t index;
2024 pgoff_t last_index;
2025 pgoff_t prev_index;
2026 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002027 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002028 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Wei Fangc2a97372016-10-07 17:01:52 -07002030 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002031 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002032 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2033
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002034 index = *ppos >> PAGE_SHIFT;
2035 prev_index = ra->prev_pos >> PAGE_SHIFT;
2036 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2037 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2038 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 for (;;) {
2041 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002042 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002043 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 unsigned long nr, ret;
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002048 if (fatal_signal_pending(current)) {
2049 error = -EINTR;
2050 goto out;
2051 }
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002054 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002055 if (iocb->ki_flags & IOCB_NOWAIT)
2056 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002057 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002058 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002059 index, last_index - index);
2060 page = find_get_page(mapping, index);
2061 if (unlikely(page == NULL))
2062 goto no_cached_page;
2063 }
2064 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002065 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002066 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002067 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002069 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002070 if (iocb->ki_flags & IOCB_NOWAIT) {
2071 put_page(page);
2072 goto would_block;
2073 }
2074
Mel Gormanebded022016-03-15 14:55:39 -07002075 /*
2076 * See comment in do_read_cache_page on why
2077 * wait_on_page_locked is used to avoid unnecessarily
2078 * serialisations and why it's safe.
2079 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002080 error = wait_on_page_locked_killable(page);
2081 if (unlikely(error))
2082 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002083 if (PageUptodate(page))
2084 goto page_ok;
2085
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002086 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002087 !mapping->a_ops->is_partially_uptodate)
2088 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002089 /* pipes can't handle partially uptodate pages */
2090 if (unlikely(iter->type & ITER_PIPE))
2091 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002092 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002093 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002094 /* Did it get truncated before we got the lock? */
2095 if (!page->mapping)
2096 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002097 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002098 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002099 goto page_not_up_to_date_locked;
2100 unlock_page(page);
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002103 /*
2104 * i_size must be checked after we know the page is Uptodate.
2105 *
2106 * Checking i_size after the check allows us to calculate
2107 * the correct value for "nr", which means the zero-filled
2108 * part of the page is not copied back to userspace (unless
2109 * another truncate extends the file - this is desired though).
2110 */
2111
2112 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002113 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002114 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002115 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002116 goto out;
2117 }
2118
2119 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002120 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002121 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002122 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002123 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002124 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002125 goto out;
2126 }
2127 }
2128 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* If users can be writing to this page using arbitrary
2131 * virtual addresses, take care about potential aliasing
2132 * before reading the page on the kernel side.
2133 */
2134 if (mapping_writably_mapped(mapping))
2135 flush_dcache_page(page);
2136
2137 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002138 * When a sequential read accesses a page several times,
2139 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 */
Jan Karaec0f1632007-05-06 14:49:25 -07002141 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 mark_page_accessed(page);
2143 prev_index = index;
2144
2145 /*
2146 * Ok, we have the page, and it's up-to-date, so
2147 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Al Viro6e58e792014-02-03 17:07:03 -05002149
2150 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002152 index += offset >> PAGE_SHIFT;
2153 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002154 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002156 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002157 written += ret;
2158 if (!iov_iter_count(iter))
2159 goto out;
2160 if (ret < nr) {
2161 error = -EFAULT;
2162 goto out;
2163 }
2164 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166page_not_up_to_date:
2167 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002168 error = lock_page_killable(page);
2169 if (unlikely(error))
2170 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002172page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002173 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!page->mapping) {
2175 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002176 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 continue;
2178 }
2179
2180 /* Did somebody else fill it already? */
2181 if (PageUptodate(page)) {
2182 unlock_page(page);
2183 goto page_ok;
2184 }
2185
2186readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002187 /*
2188 * A previous I/O error may have been due to temporary
2189 * failures, eg. multipath errors.
2190 * PG_error will be set again if readpage fails.
2191 */
2192 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* Start the actual read. The read will unlock the page. */
2194 error = mapping->a_ops->readpage(filp, page);
2195
Zach Brown994fc28c2005-12-15 14:28:17 -08002196 if (unlikely(error)) {
2197 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002198 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002199 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002200 goto find_page;
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002206 error = lock_page_killable(page);
2207 if (unlikely(error))
2208 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (!PageUptodate(page)) {
2210 if (page->mapping == NULL) {
2211 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002212 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 */
2214 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002215 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 goto find_page;
2217 }
2218 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002219 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002220 error = -EIO;
2221 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223 unlock_page(page);
2224 }
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 goto page_ok;
2227
2228readpage_error:
2229 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002230 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto out;
2232
2233no_cached_page:
2234 /*
2235 * Ok, it wasn't cached, so we need to create a new
2236 * page..
2237 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002238 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002239 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002240 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002243 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002244 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002246 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002247 if (error == -EEXIST) {
2248 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto out;
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 goto readpage;
2254 }
2255
Milosz Tanski3239d832017-08-29 16:13:19 +02002256would_block:
2257 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002259 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002260 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002261 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002263 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002264 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002265 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Randy Dunlap485bb992006-06-23 02:03:49 -07002268/**
Al Viro6abd2322014-04-04 14:20:57 -04002269 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002270 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002271 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002272 *
Al Viro6abd2322014-04-04 14:20:57 -04002273 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 * that can use the page cache directly.
2275 */
2276ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002277generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002279 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002280 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002281
2282 if (!count)
2283 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Al Viro2ba48ce2015-04-09 13:52:01 -04002285 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002286 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002287 struct address_space *mapping = file->f_mapping;
2288 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002289 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002292 if (iocb->ki_flags & IOCB_NOWAIT) {
2293 if (filemap_range_has_page(mapping, iocb->ki_pos,
2294 iocb->ki_pos + count - 1))
2295 return -EAGAIN;
2296 } else {
2297 retval = filemap_write_and_wait_range(mapping,
2298 iocb->ki_pos,
2299 iocb->ki_pos + count - 1);
2300 if (retval < 0)
2301 goto out;
2302 }
Al Viroed978a82014-03-05 22:53:04 -05002303
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002304 file_accessed(file);
2305
Al Viro5ecda132017-04-13 14:13:36 -04002306 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002307 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002308 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002309 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002310 }
Al Viro5b47d592017-05-08 13:54:47 -04002311 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002312
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002313 /*
2314 * Btrfs can have a short DIO read if we encounter
2315 * compressed extents, so if there was an error, or if
2316 * we've already read everything we wanted to, or if
2317 * there was a short read because we hit EOF, go ahead
2318 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002319 * the rest of the read. Buffered reads will not work for
2320 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002321 */
Al Viro5ecda132017-04-13 14:13:36 -04002322 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002323 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002324 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002327 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328out:
2329 return retval;
2330}
Al Viroed978a82014-03-05 22:53:04 -05002331EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002334/**
2335 * page_cache_read - adds requested page to the page cache if not already there
2336 * @file: file to read
2337 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002338 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002339 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 * This adds the requested page to the page cache if it isn't already there,
2341 * and schedules an I/O to read in its contents from disk.
2342 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002343static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002346 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002347 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Zach Brown994fc28c2005-12-15 14:28:17 -08002349 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002350 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002351 if (!page)
2352 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002354 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002355 if (ret == 0)
2356 ret = mapping->a_ops->readpage(file, page);
2357 else if (ret == -EEXIST)
2358 ret = 0; /* losing race to add is OK */
2359
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002360 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Zach Brown994fc28c2005-12-15 14:28:17 -08002362 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002363
Zach Brown994fc28c2005-12-15 14:28:17 -08002364 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
2367#define MMAP_LOTSAMISS (100)
2368
Linus Torvaldsef00e082009-06-16 15:31:25 -07002369/*
2370 * Synchronous readahead happens when we don't even find
2371 * a page in the page cache at all.
2372 */
2373static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2374 struct file_ra_state *ra,
2375 struct file *file,
2376 pgoff_t offset)
2377{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002378 struct address_space *mapping = file->f_mapping;
2379
2380 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002381 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002382 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002383 if (!ra->ra_pages)
2384 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002385
Joe Perches64363aa2013-07-08 16:00:18 -07002386 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002387 page_cache_sync_readahead(mapping, ra, file, offset,
2388 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002389 return;
2390 }
2391
Andi Kleen207d04b2011-05-24 17:12:29 -07002392 /* Avoid banging the cache line if not needed */
2393 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002394 ra->mmap_miss++;
2395
2396 /*
2397 * Do we miss much more than hit in this file? If so,
2398 * stop bothering with read-ahead. It will only hurt.
2399 */
2400 if (ra->mmap_miss > MMAP_LOTSAMISS)
2401 return;
2402
Wu Fengguangd30a1102009-06-16 15:31:30 -07002403 /*
2404 * mmap read-around
2405 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002406 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2407 ra->size = ra->ra_pages;
2408 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002409 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002410}
2411
2412/*
2413 * Asynchronous readahead happens when we find the page and PG_readahead,
2414 * so we want to possibly extend the readahead further..
2415 */
2416static void do_async_mmap_readahead(struct vm_area_struct *vma,
2417 struct file_ra_state *ra,
2418 struct file *file,
2419 struct page *page,
2420 pgoff_t offset)
2421{
2422 struct address_space *mapping = file->f_mapping;
2423
2424 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002425 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002426 return;
2427 if (ra->mmap_miss > 0)
2428 ra->mmap_miss--;
2429 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002430 page_cache_async_readahead(mapping, ra, file,
2431 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002432}
2433
Randy Dunlap485bb992006-06-23 02:03:49 -07002434/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002435 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002436 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002437 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002438 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 * mapped memory region to read in file data during a page fault.
2440 *
2441 * The goto's are kind of ugly, but this streamlines the normal case of having
2442 * it in the page cache, and handles the special cases reasonably without
2443 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002444 *
2445 * vma->vm_mm->mmap_sem must be held on entry.
2446 *
2447 * If our return value has VM_FAULT_RETRY set, it's because
2448 * lock_page_or_retry() returned 0.
2449 * The mmap_sem has usually been released in this case.
2450 * See __lock_page_or_retry() for the exception.
2451 *
2452 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2453 * has not been released.
2454 *
2455 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002457vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002460 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct address_space *mapping = file->f_mapping;
2462 struct file_ra_state *ra = &file->f_ra;
2463 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002464 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002465 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002467 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002469 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2470 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002471 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002474 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002476 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002477 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002479 * We found the page, so try async readahead before
2480 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 */
Dave Jiang11bac802017-02-24 14:56:41 -08002482 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002483 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002484 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002485 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002486 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002487 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002488 ret = VM_FAULT_MAJOR;
2489retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002490 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (!page)
2492 goto no_cached_page;
2493 }
2494
Dave Jiang11bac802017-02-24 14:56:41 -08002495 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002496 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002497 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002498 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002499
2500 /* Did it get truncated? */
2501 if (unlikely(page->mapping != mapping)) {
2502 unlock_page(page);
2503 put_page(page);
2504 goto retry_find;
2505 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002506 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002509 * We have a locked page in the page cache, now we need to check
2510 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
Nick Piggind00806b2007-07-19 01:46:57 -07002512 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 goto page_not_uptodate;
2514
Linus Torvaldsef00e082009-06-16 15:31:25 -07002515 /*
2516 * Found the page and have a reference on it.
2517 * We must recheck i_size under page lock.
2518 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002519 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2520 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002521 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002522 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002523 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002524 }
2525
Nick Piggind0217ac2007-07-19 01:47:03 -07002526 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002527 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529no_cached_page:
2530 /*
2531 * We're only likely to ever get here if MADV_RANDOM is in
2532 * effect.
2533 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002534 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /*
2537 * The page we want has now been added to the page cache.
2538 * In the unlikely event that someone removed it in the
2539 * meantime, we'll just come back here and read it again.
2540 */
2541 if (error >= 0)
2542 goto retry_find;
2543
2544 /*
2545 * An error return from page_cache_read can result if the
2546 * system is low on memory, or a problem occurs while trying
2547 * to schedule I/O.
2548 */
2549 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002550 return VM_FAULT_OOM;
2551 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /*
2555 * Umm, take care of errors if the page isn't up-to-date.
2556 * Try to re-read it _once_. We do this synchronously,
2557 * because there really aren't any performance issues here
2558 * and we need to check for errors.
2559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002561 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002562 if (!error) {
2563 wait_on_page_locked(page);
2564 if (!PageUptodate(page))
2565 error = -EIO;
2566 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002567 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002568
2569 if (!error || error == AOP_TRUNCATED_PAGE)
2570 goto retry_find;
2571
2572 /* Things didn't work out. Return zero to tell the mm layer so. */
2573 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002574 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002575}
2576EXPORT_SYMBOL(filemap_fault);
2577
Jan Kara82b0f8c2016-12-14 15:06:58 -08002578void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002579 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002580{
2581 struct radix_tree_iter iter;
2582 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002583 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002584 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002585 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002586 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002587 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002588
2589 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002590 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start_pgoff) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002591 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002592 break;
2593repeat:
2594 page = radix_tree_deref_slot(slot);
2595 if (unlikely(!page))
2596 goto next;
2597 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002598 if (radix_tree_deref_retry(page)) {
2599 slot = radix_tree_iter_retry(&iter);
2600 continue;
2601 }
2602 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002603 }
2604
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002605 head = compound_head(page);
2606 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002607 goto repeat;
2608
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002609 /* The page was split under us? */
2610 if (compound_head(page) != head) {
2611 put_page(head);
2612 goto repeat;
2613 }
2614
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002615 /* Has the page moved? */
2616 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002617 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002618 goto repeat;
2619 }
2620
2621 if (!PageUptodate(page) ||
2622 PageReadahead(page) ||
2623 PageHWPoison(page))
2624 goto skip;
2625 if (!trylock_page(page))
2626 goto skip;
2627
2628 if (page->mapping != mapping || !PageUptodate(page))
2629 goto unlock;
2630
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002631 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2632 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002633 goto unlock;
2634
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002635 if (file->f_ra.mmap_miss > 0)
2636 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002637
Jan Kara82b0f8c2016-12-14 15:06:58 -08002638 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2639 if (vmf->pte)
2640 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002641 last_pgoff = iter.index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002642 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002643 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002644 unlock_page(page);
2645 goto next;
2646unlock:
2647 unlock_page(page);
2648skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002649 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002650next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002651 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002652 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002653 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002654 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002655 break;
2656 }
2657 rcu_read_unlock();
2658}
2659EXPORT_SYMBOL(filemap_map_pages);
2660
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002661vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002662{
2663 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002664 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002665 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002666
Jan Kara14da9202012-06-12 16:20:37 +02002667 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002668 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002669 lock_page(page);
2670 if (page->mapping != inode->i_mapping) {
2671 unlock_page(page);
2672 ret = VM_FAULT_NOPAGE;
2673 goto out;
2674 }
Jan Kara14da9202012-06-12 16:20:37 +02002675 /*
2676 * We mark the page dirty already here so that when freeze is in
2677 * progress, we are guaranteed that writeback during freezing will
2678 * see the dirty page and writeprotect it again.
2679 */
2680 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002681 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002682out:
Jan Kara14da9202012-06-12 16:20:37 +02002683 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002684 return ret;
2685}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002686
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002687const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002688 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002689 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002690 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691};
2692
2693/* This is used for a general mmap of a disk file */
2694
2695int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2696{
2697 struct address_space *mapping = file->f_mapping;
2698
2699 if (!mapping->a_ops->readpage)
2700 return -ENOEXEC;
2701 file_accessed(file);
2702 vma->vm_ops = &generic_file_vm_ops;
2703 return 0;
2704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706/*
2707 * This is for filesystems which do not implement ->writepage.
2708 */
2709int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2710{
2711 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2712 return -EINVAL;
2713 return generic_file_mmap(file, vma);
2714}
2715#else
Arnd Bergmann453972282018-04-13 15:35:27 -07002716int filemap_page_mkwrite(struct vm_fault *vmf)
2717{
2718 return -ENOSYS;
2719}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2721{
2722 return -ENOSYS;
2723}
2724int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2725{
2726 return -ENOSYS;
2727}
2728#endif /* CONFIG_MMU */
2729
Arnd Bergmann453972282018-04-13 15:35:27 -07002730EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731EXPORT_SYMBOL(generic_file_mmap);
2732EXPORT_SYMBOL(generic_file_readonly_mmap);
2733
Sasha Levin67f9fd92014-04-03 14:48:18 -07002734static struct page *wait_on_page_read(struct page *page)
2735{
2736 if (!IS_ERR(page)) {
2737 wait_on_page_locked(page);
2738 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002739 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002740 page = ERR_PTR(-EIO);
2741 }
2742 }
2743 return page;
2744}
2745
Mel Gorman32b63522016-03-15 14:55:36 -07002746static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002747 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002748 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002749 void *data,
2750 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Nick Piggineb2be182007-10-16 01:24:57 -07002752 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 int err;
2754repeat:
2755 page = find_get_page(mapping, index);
2756 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002757 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002758 if (!page)
2759 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002760 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002761 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002762 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002763 if (err == -EEXIST)
2764 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return ERR_PTR(err);
2767 }
Mel Gorman32b63522016-03-15 14:55:36 -07002768
2769filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 err = filler(data, page);
2771 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002772 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002773 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
Mel Gorman32b63522016-03-15 14:55:36 -07002775
2776 page = wait_on_page_read(page);
2777 if (IS_ERR(page))
2778 return page;
2779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (PageUptodate(page))
2782 goto out;
2783
Mel Gormanebded022016-03-15 14:55:39 -07002784 /*
2785 * Page is not up to date and may be locked due one of the following
2786 * case a: Page is being filled and the page lock is held
2787 * case b: Read/write error clearing the page uptodate status
2788 * case c: Truncation in progress (page locked)
2789 * case d: Reclaim in progress
2790 *
2791 * Case a, the page will be up to date when the page is unlocked.
2792 * There is no need to serialise on the page lock here as the page
2793 * is pinned so the lock gives no additional protection. Even if the
2794 * the page is truncated, the data is still valid if PageUptodate as
2795 * it's a race vs truncate race.
2796 * Case b, the page will not be up to date
2797 * Case c, the page may be truncated but in itself, the data may still
2798 * be valid after IO completes as it's a read vs truncate race. The
2799 * operation must restart if the page is not uptodate on unlock but
2800 * otherwise serialising on page lock to stabilise the mapping gives
2801 * no additional guarantees to the caller as the page lock is
2802 * released before return.
2803 * Case d, similar to truncation. If reclaim holds the page lock, it
2804 * will be a race with remove_mapping that determines if the mapping
2805 * is valid on unlock but otherwise the data is valid and there is
2806 * no need to serialise with page lock.
2807 *
2808 * As the page lock gives no additional guarantee, we optimistically
2809 * wait on the page to be unlocked and check if it's up to date and
2810 * use the page if it is. Otherwise, the page lock is required to
2811 * distinguish between the different cases. The motivation is that we
2812 * avoid spurious serialisations and wakeups when multiple processes
2813 * wait on the same page for IO to complete.
2814 */
2815 wait_on_page_locked(page);
2816 if (PageUptodate(page))
2817 goto out;
2818
2819 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002821
2822 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (!page->mapping) {
2824 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002825 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002826 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
Mel Gormanebded022016-03-15 14:55:39 -07002828
2829 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (PageUptodate(page)) {
2831 unlock_page(page);
2832 goto out;
2833 }
Mel Gorman32b63522016-03-15 14:55:36 -07002834 goto filler;
2835
David Howellsc855ff32007-05-09 13:42:20 +01002836out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002837 mark_page_accessed(page);
2838 return page;
2839}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002840
2841/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002842 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002843 * @mapping: the page's address_space
2844 * @index: the page index
2845 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002846 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002847 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002848 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002849 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002850 *
2851 * If the page does not get brought uptodate, return -EIO.
2852 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002853struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002854 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002855 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002856 void *data)
2857{
2858 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2859}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002860EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002861
2862/**
2863 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2864 * @mapping: the page's address_space
2865 * @index: the page index
2866 * @gfp: the page allocator flags to use if allocating
2867 *
2868 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002869 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002870 *
2871 * If the page does not get brought uptodate, return -EIO.
2872 */
2873struct page *read_cache_page_gfp(struct address_space *mapping,
2874 pgoff_t index,
2875 gfp_t gfp)
2876{
2877 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2878
Sasha Levin67f9fd92014-04-03 14:48:18 -07002879 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002880}
2881EXPORT_SYMBOL(read_cache_page_gfp);
2882
Nick Piggin2f718ff2007-10-16 01:24:59 -07002883/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * Performs necessary checks before doing a write
2885 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002886 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 * Returns appropriate error code that caller should return or
2888 * zero in case that write should be allowed.
2889 */
Al Viro3309dd02015-04-09 12:55:47 -04002890inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Al Viro3309dd02015-04-09 12:55:47 -04002892 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002894 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002895 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Al Viro3309dd02015-04-09 12:55:47 -04002897 if (!iov_iter_count(from))
2898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Al Viro0fa6b002015-04-04 04:05:48 -04002900 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002901 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002902 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Al Viro3309dd02015-04-09 12:55:47 -04002904 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002906 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2907 return -EINVAL;
2908
Al Viro0fa6b002015-04-04 04:05:48 -04002909 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002910 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002911 send_sig(SIGXFSZ, current, 0);
2912 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
Al Viro3309dd02015-04-09 12:55:47 -04002914 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916
2917 /*
2918 * LFS rule
2919 */
Al Viro3309dd02015-04-09 12:55:47 -04002920 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002922 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002924 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926
2927 /*
2928 * Are we about to exceed the fs block limit ?
2929 *
2930 * If we have written data it becomes a short write. If we have
2931 * exceeded without writing data we send a signal and return EFBIG.
2932 * Linus frestrict idea will clean these up nicely..
2933 */
Al Viro3309dd02015-04-09 12:55:47 -04002934 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2935 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Al Viro3309dd02015-04-09 12:55:47 -04002937 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2938 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940EXPORT_SYMBOL(generic_write_checks);
2941
Nick Pigginafddba42007-10-16 01:25:01 -07002942int pagecache_write_begin(struct file *file, struct address_space *mapping,
2943 loff_t pos, unsigned len, unsigned flags,
2944 struct page **pagep, void **fsdata)
2945{
2946 const struct address_space_operations *aops = mapping->a_ops;
2947
Nick Piggin4e02ed42008-10-29 14:00:55 -07002948 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002949 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002950}
2951EXPORT_SYMBOL(pagecache_write_begin);
2952
2953int pagecache_write_end(struct file *file, struct address_space *mapping,
2954 loff_t pos, unsigned len, unsigned copied,
2955 struct page *page, void *fsdata)
2956{
2957 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002958
Nick Piggin4e02ed42008-10-29 14:00:55 -07002959 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002960}
2961EXPORT_SYMBOL(pagecache_write_end);
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002964generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 struct file *file = iocb->ki_filp;
2967 struct address_space *mapping = file->f_mapping;
2968 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002969 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002971 size_t write_len;
2972 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Al Viro0c949332014-03-22 06:51:37 -04002974 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002975 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002976
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002977 if (iocb->ki_flags & IOCB_NOWAIT) {
2978 /* If there are pages to writeback, return */
2979 if (filemap_range_has_page(inode->i_mapping, pos,
2980 pos + iov_iter_count(from)))
2981 return -EAGAIN;
2982 } else {
2983 written = filemap_write_and_wait_range(mapping, pos,
2984 pos + write_len - 1);
2985 if (written)
2986 goto out;
2987 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002988
2989 /*
2990 * After a write we want buffered reads to be sure to go to disk to get
2991 * the new data. We invalidate clean cached page from the region we're
2992 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002993 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002994 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07002995 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002996 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07002997 /*
2998 * If a page can not be invalidated, return 0 to fall back
2999 * to buffered write.
3000 */
3001 if (written) {
3002 if (written == -EBUSY)
3003 return 0;
3004 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003005 }
3006
Al Viro639a93a52017-04-13 14:10:15 -04003007 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003008
3009 /*
3010 * Finally, try again to invalidate clean pages which might have been
3011 * cached by non-direct readahead, or faulted in by get_user_pages()
3012 * if the source of the write was an mmap'ed region of the file
3013 * we're writing. Either one is a pretty crazy thing to do,
3014 * so we don't support it 100%. If this invalidation
3015 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003016 *
3017 * Most of the time we do not need this since dio_complete() will do
3018 * the invalidation for us. However there are some file systems that
3019 * do not end up with dio_complete() being called, so let's not break
3020 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003021 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003022 if (mapping->nrpages)
3023 invalidate_inode_pages2_range(mapping,
3024 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003027 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003028 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003029 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3030 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 mark_inode_dirty(inode);
3032 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003033 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
Al Viro639a93a52017-04-13 14:10:15 -04003035 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003036out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return written;
3038}
3039EXPORT_SYMBOL(generic_file_direct_write);
3040
Nick Piggineb2be182007-10-16 01:24:57 -07003041/*
3042 * Find or create a page at the given pagecache position. Return the locked
3043 * page. This function is specifically for buffered writes.
3044 */
Nick Piggin54566b22009-01-04 12:00:53 -08003045struct page *grab_cache_page_write_begin(struct address_space *mapping,
3046 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003047{
Nick Piggineb2be182007-10-16 01:24:57 -07003048 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003049 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003050
Nick Piggin54566b22009-01-04 12:00:53 -08003051 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003052 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003053
Mel Gorman2457aec2014-06-04 16:10:31 -07003054 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003055 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003056 if (page)
3057 wait_for_stable_page(page);
3058
Nick Piggineb2be182007-10-16 01:24:57 -07003059 return page;
3060}
Nick Piggin54566b22009-01-04 12:00:53 -08003061EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003062
Al Viro3b93f912014-02-11 21:34:08 -05003063ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003064 struct iov_iter *i, loff_t pos)
3065{
3066 struct address_space *mapping = file->f_mapping;
3067 const struct address_space_operations *a_ops = mapping->a_ops;
3068 long status = 0;
3069 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003070 unsigned int flags = 0;
3071
Nick Pigginafddba42007-10-16 01:25:01 -07003072 do {
3073 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003074 unsigned long offset; /* Offset into pagecache page */
3075 unsigned long bytes; /* Bytes to write to page */
3076 size_t copied; /* Bytes copied from user */
3077 void *fsdata;
3078
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003079 offset = (pos & (PAGE_SIZE - 1));
3080 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003081 iov_iter_count(i));
3082
3083again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003084 /*
3085 * Bring in the user page that we will copy from _first_.
3086 * Otherwise there's a nasty deadlock on copying from the
3087 * same page as we're writing to, without it being marked
3088 * up-to-date.
3089 *
3090 * Not only is this an optimisation, but it is also required
3091 * to check that the address is actually valid, when atomic
3092 * usercopies are used, below.
3093 */
3094 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3095 status = -EFAULT;
3096 break;
3097 }
3098
Jan Kara296291c2015-10-22 13:32:21 -07003099 if (fatal_signal_pending(current)) {
3100 status = -EINTR;
3101 break;
3102 }
3103
Nick Piggin674b8922007-10-16 01:25:03 -07003104 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003105 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003106 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003107 break;
3108
anfei zhou931e80e2010-02-02 13:44:02 -08003109 if (mapping_writably_mapped(mapping))
3110 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003111
Nick Pigginafddba42007-10-16 01:25:01 -07003112 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003113 flush_dcache_page(page);
3114
3115 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3116 page, fsdata);
3117 if (unlikely(status < 0))
3118 break;
3119 copied = status;
3120
3121 cond_resched();
3122
Nick Piggin124d3b72008-02-02 15:01:17 +01003123 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003124 if (unlikely(copied == 0)) {
3125 /*
3126 * If we were unable to copy any data at all, we must
3127 * fall back to a single segment length write.
3128 *
3129 * If we didn't fallback here, we could livelock
3130 * because not all segments in the iov can be copied at
3131 * once without a pagefault.
3132 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003133 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003134 iov_iter_single_seg_count(i));
3135 goto again;
3136 }
Nick Pigginafddba42007-10-16 01:25:01 -07003137 pos += copied;
3138 written += copied;
3139
3140 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003141 } while (iov_iter_count(i));
3142
3143 return written ? written : status;
3144}
Al Viro3b93f912014-02-11 21:34:08 -05003145EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Jan Karae4dd9de2009-08-17 18:10:06 +02003147/**
Al Viro81742022014-04-03 03:17:43 -04003148 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003149 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003150 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003151 *
3152 * This function does all the work needed for actually writing data to a
3153 * file. It does all basic checks, removes SUID from the file, updates
3154 * modification times and calls proper subroutines depending on whether we
3155 * do direct IO or a standard buffered write.
3156 *
3157 * It expects i_mutex to be grabbed unless we work on a block device or similar
3158 * object which does not need locking at all.
3159 *
3160 * This function does *not* take care of syncing data in case of O_SYNC write.
3161 * A caller has to handle it. This is mainly due to the fact that we want to
3162 * avoid syncing under i_mutex.
3163 */
Al Viro81742022014-04-03 03:17:43 -04003164ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
3166 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003167 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003169 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003171 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003174 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003175 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (err)
3177 goto out;
3178
Josef Bacikc3b2da32012-03-26 09:59:21 -04003179 err = file_update_time(file);
3180 if (err)
3181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Al Viro2ba48ce2015-04-09 13:52:01 -04003183 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003184 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003185
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003186 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003187 /*
3188 * If the write stopped short of completing, fall back to
3189 * buffered writes. Some filesystems do this for writes to
3190 * holes, for example. For DAX files, a buffered write will
3191 * not succeed (even if it did, DAX does not handle dirty
3192 * page-cache pages correctly).
3193 */
Al Viro0b8def92015-04-07 10:22:53 -04003194 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003196
Al Viro0b8def92015-04-07 10:22:53 -04003197 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003198 /*
Al Viro3b93f912014-02-11 21:34:08 -05003199 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003200 * then we want to return the number of bytes which were
3201 * direct-written, or the error code if that was zero. Note
3202 * that this differs from normal direct-io semantics, which
3203 * will return -EFOO even if some bytes were written.
3204 */
Al Viro60bb4522014-08-08 12:39:16 -04003205 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003206 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003207 goto out;
3208 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003209 /*
3210 * We need to ensure that the page cache pages are written to
3211 * disk and invalidated to preserve the expected O_DIRECT
3212 * semantics.
3213 */
Al Viro3b93f912014-02-11 21:34:08 -05003214 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003215 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003216 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003217 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003218 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003219 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003220 pos >> PAGE_SHIFT,
3221 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003222 } else {
3223 /*
3224 * We don't know how much we wrote, so just return
3225 * the number of bytes which were direct-written
3226 */
3227 }
3228 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003229 written = generic_perform_write(file, from, iocb->ki_pos);
3230 if (likely(written > 0))
3231 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233out:
3234 current->backing_dev_info = NULL;
3235 return written ? written : err;
3236}
Al Viro81742022014-04-03 03:17:43 -04003237EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Jan Karae4dd9de2009-08-17 18:10:06 +02003239/**
Al Viro81742022014-04-03 03:17:43 -04003240 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003241 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003242 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003243 *
Al Viro81742022014-04-03 03:17:43 -04003244 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003245 * filesystems. It takes care of syncing the file in case of O_SYNC file
3246 * and acquires i_mutex as needed.
3247 */
Al Viro81742022014-04-03 03:17:43 -04003248ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
3250 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003251 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Al Viro59551022016-01-22 15:40:57 -05003254 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003255 ret = generic_write_checks(iocb, from);
3256 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003257 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003258 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Christoph Hellwige2592212016-04-07 08:52:01 -07003260 if (ret > 0)
3261 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return ret;
3263}
Al Viro81742022014-04-03 03:17:43 -04003264EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
David Howellscf9a2ae2006-08-29 19:05:54 +01003266/**
3267 * try_to_release_page() - release old fs-specific metadata on a page
3268 *
3269 * @page: the page which the kernel is trying to free
3270 * @gfp_mask: memory allocation flags (and I/O mode)
3271 *
3272 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003273 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003274 * Otherwise return zero.
3275 *
David Howells266cf652009-04-03 16:42:36 +01003276 * This may also be called if PG_fscache is set on a page, indicating that the
3277 * page is known to the local caching routines.
3278 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003279 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003280 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003281 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003282 */
3283int try_to_release_page(struct page *page, gfp_t gfp_mask)
3284{
3285 struct address_space * const mapping = page->mapping;
3286
3287 BUG_ON(!PageLocked(page));
3288 if (PageWriteback(page))
3289 return 0;
3290
3291 if (mapping && mapping->a_ops->releasepage)
3292 return mapping->a_ops->releasepage(page, gfp_mask);
3293 return try_to_free_buffers(page);
3294}
3295
3296EXPORT_SYMBOL(try_to_release_page);