blob: 20f3b1f33f0e0f83486bba4df1c620f4ac8110f0 [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>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070016#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/mman.h>
23#include <linux/pagemap.h>
24#include <linux/file.h>
25#include <linux/uio.h>
26#include <linux/hash.h>
27#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070028#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pagevec.h>
30#include <linux/blkdev.h>
31#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080032#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070033#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
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>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070037#include <linux/rmap.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080038#include "internal.h"
39
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070040#define CREATE_TRACE_POINTS
41#include <trace/events/filemap.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * FIXME: remove all knowledge of the buffer layer from the core VM
45 */
Jan Kara148f9482009-08-17 19:52:36 +020046#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/mman.h>
49
50/*
51 * Shared mappings implemented 30.11.1994. It's not fully working yet,
52 * though.
53 *
54 * Shared mappings now work. 15.8.1995 Bruno.
55 *
56 * finished 'unifying' the page and buffer cache and SMP-threaded the
57 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
58 *
59 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
60 */
61
62/*
63 * Lock ordering:
64 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080065 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070067 * ->swap_lock (exclusive_swap_page, others)
68 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080070 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080071 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080074 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070075 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
77 *
78 * ->mmap_sem
79 * ->lock_page (access_process_vm)
80 *
Al Viroccad2362014-02-11 22:36:48 -050081 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070082 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060084 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110085 * sb_lock (fs/fs-writeback.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * ->mapping->tree_lock (__sync_single_inode)
87 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080088 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * ->anon_vma.lock (vma_adjust)
90 *
91 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070092 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070094 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070095 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * ->private_lock (try_to_unmap_one)
97 * ->tree_lock (try_to_unmap_one)
98 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -080099 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ->private_lock (page_remove_rmap->set_page_dirty)
101 * ->tree_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600102 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100103 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700104 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600105 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100106 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800109 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700110 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700113static void page_cache_tree_delete(struct address_space *mapping,
114 struct page *page, void *shadow)
115{
Johannes Weiner449dd692014-04-03 14:47:56 -0700116 struct radix_tree_node *node;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700117
Johannes Weiner449dd692014-04-03 14:47:56 -0700118 VM_BUG_ON(!PageLocked(page));
119
Matthew Wilcoxd604c322016-05-20 17:03:45 -0700120 node = radix_tree_replace_clear_tags(&mapping->page_tree, page->index,
121 shadow);
Johannes Weiner449dd692014-04-03 14:47:56 -0700122
123 if (shadow) {
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800124 mapping->nrexceptional++;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700125 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800126 * Make sure the nrexceptional update is committed before
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700127 * the nrpages update so that final truncate racing
128 * with reclaim does not see both counters 0 at the
129 * same time and miss a shadow entry.
130 */
131 smp_wmb();
Johannes Weiner449dd692014-04-03 14:47:56 -0700132 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700133 mapping->nrpages--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700134
Matthew Wilcoxd604c322016-05-20 17:03:45 -0700135 if (!node)
Johannes Weiner449dd692014-04-03 14:47:56 -0700136 return;
Johannes Weiner449dd692014-04-03 14:47:56 -0700137
Johannes Weiner449dd692014-04-03 14:47:56 -0700138 workingset_node_pages_dec(node);
139 if (shadow)
140 workingset_node_shadows_inc(node);
141 else
142 if (__radix_tree_delete_node(&mapping->page_tree, node))
143 return;
144
145 /*
Jan Karaac401cc2016-05-12 18:29:18 +0200146 * Track node that only contains shadow entries. DAX mappings contain
147 * no shadow entries and may contain other exceptional entries so skip
148 * those.
Johannes Weiner449dd692014-04-03 14:47:56 -0700149 *
150 * Avoid acquiring the list_lru lock if already tracked. The
151 * list_empty() test is safe as node->private_list is
152 * protected by mapping->tree_lock.
153 */
Jan Karaac401cc2016-05-12 18:29:18 +0200154 if (!dax_mapping(mapping) && !workingset_node_pages(node) &&
Johannes Weiner449dd692014-04-03 14:47:56 -0700155 list_empty(&node->private_list)) {
156 node->private_data = mapping;
157 list_lru_add(&workingset_shadow_nodes, &node->private_list);
158 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
Minchan Kime64a7822011-03-22 16:32:44 -0700162 * Delete a page from the page cache and free it. Caller has to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * sure the page is locked and that nobody else uses it - or that usage
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700164 * is safe. The caller must hold the mapping's tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Johannes Weiner62cccb82016-03-15 14:57:22 -0700166void __delete_from_page_cache(struct page *page, void *shadow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct address_space *mapping = page->mapping;
169
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -0700170 trace_mm_filemap_delete_from_page_cache(page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600171 /*
172 * if we're uptodate, flush out into the cleancache, otherwise
173 * invalidate any existing cleancache entries. We can't leave
174 * stale data around in the cleancache once our page is gone
175 */
176 if (PageUptodate(page) && PageMappedToDisk(page))
177 cleancache_put_page(page);
178 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400179 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600180
Hugh Dickins06b241f2016-03-09 14:08:07 -0800181 VM_BUG_ON_PAGE(page_mapped(page), page);
182 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
183 int mapcount;
184
185 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
186 current->comm, page_to_pfn(page));
187 dump_page(page, "still mapped when deleted");
188 dump_stack();
189 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
190
191 mapcount = page_mapcount(page);
192 if (mapping_exiting(mapping) &&
193 page_count(page) >= mapcount + 2) {
194 /*
195 * All vmas have already been torn down, so it's
196 * a good bet that actually the page is unmapped,
197 * and we'd prefer not to leak it: if we're wrong,
198 * some other bad page check should catch it later.
199 */
200 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700201 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800202 }
203 }
204
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700205 page_cache_tree_delete(mapping, page, shadow);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700208 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700209
Michal Hocko4165b9b2015-06-24 16:57:24 -0700210 /* hugetlb pages do not participate in page cache accounting. */
211 if (!PageHuge(page))
212 __dec_zone_page_state(page, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700213 if (PageSwapBacked(page))
214 __dec_zone_page_state(page, NR_SHMEM);
Linus Torvalds3a692792007-12-19 14:05:13 -0800215
216 /*
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700217 * At this point page must be either written or cleaned by truncate.
218 * Dirty page here signals a bug and loss of unwritten data.
Linus Torvalds3a692792007-12-19 14:05:13 -0800219 *
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700220 * This fixes dirty accounting after removing the page entirely but
221 * leaves PageDirty set: it has no effect for truncated page and
222 * anyway will be cleared before returning page into buddy allocator.
Linus Torvalds3a692792007-12-19 14:05:13 -0800223 */
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700224 if (WARN_ON_ONCE(PageDirty(page)))
Johannes Weiner62cccb82016-03-15 14:57:22 -0700225 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Minchan Kim702cfbf2011-03-22 16:32:43 -0700228/**
229 * delete_from_page_cache - delete page from page cache
230 * @page: the page which the kernel is trying to remove from page cache
231 *
232 * This must be called only on pages that have been verified to be in the page
233 * cache and locked. It will never put the page into the free list, the caller
234 * has a reference on the page.
235 */
236void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct address_space *mapping = page->mapping;
Greg Thelenc4843a72015-05-22 17:13:16 -0400239 unsigned long flags;
240
Linus Torvalds6072d132010-12-01 13:35:19 -0500241 void (*freepage)(struct page *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Matt Mackallcd7619d2005-05-01 08:59:01 -0700243 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds6072d132010-12-01 13:35:19 -0500245 freepage = mapping->a_ops->freepage;
Greg Thelenc4843a72015-05-22 17:13:16 -0400246
Greg Thelenc4843a72015-05-22 17:13:16 -0400247 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700248 __delete_from_page_cache(page, NULL);
Greg Thelenc4843a72015-05-22 17:13:16 -0400249 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500250
251 if (freepage)
252 freepage(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300253 put_page(page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700254}
255EXPORT_SYMBOL(delete_from_page_cache);
256
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700257static int filemap_check_errors(struct address_space *mapping)
258{
259 int ret = 0;
260 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700261 if (test_bit(AS_ENOSPC, &mapping->flags) &&
262 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700263 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700264 if (test_bit(AS_EIO, &mapping->flags) &&
265 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700266 ret = -EIO;
267 return ret;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700271 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700272 * @mapping: address space structure to write
273 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800274 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700275 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700277 * Start writeback against all of a mapping's dirty pages that lie
278 * within the byte offsets <start, end> inclusive.
279 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700281 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * these two operations is that if a dirty page/buffer is encountered, it must
283 * be waited upon, and not just skipped over.
284 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800285int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
286 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int ret;
289 struct writeback_control wbc = {
290 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800291 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700292 .range_start = start,
293 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 };
295
296 if (!mapping_cap_writeback_dirty(mapping))
297 return 0;
298
Tejun Heob16b1de2015-06-02 08:39:48 -0600299 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600301 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return ret;
303}
304
305static inline int __filemap_fdatawrite(struct address_space *mapping,
306 int sync_mode)
307{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700308 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311int filemap_fdatawrite(struct address_space *mapping)
312{
313 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
314}
315EXPORT_SYMBOL(filemap_fdatawrite);
316
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400317int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800318 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
321}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400322EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Randy Dunlap485bb992006-06-23 02:03:49 -0700324/**
325 * filemap_flush - mostly a non-blocking flush
326 * @mapping: target address_space
327 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * This is a mostly non-blocking flush. Not suitable for data-integrity
329 * purposes - I/O may not be started against all dirty pages.
330 */
331int filemap_flush(struct address_space *mapping)
332{
333 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
334}
335EXPORT_SYMBOL(filemap_flush);
336
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800337static int __filemap_fdatawait_range(struct address_space *mapping,
338 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300340 pgoff_t index = start_byte >> PAGE_SHIFT;
341 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct pagevec pvec;
343 int nr_pages;
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800344 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200346 if (end_byte < start_byte)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 pagevec_init(&pvec, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 while ((index <= end) &&
351 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
352 PAGECACHE_TAG_WRITEBACK,
353 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
354 unsigned i;
355
356 for (i = 0; i < nr_pages; i++) {
357 struct page *page = pvec.pages[i];
358
359 /* until radix tree lookup accepts end_index */
360 if (page->index > end)
361 continue;
362
363 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800364 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ret = -EIO;
366 }
367 pagevec_release(&pvec);
368 cond_resched();
369 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700370out:
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800371 return ret;
372}
373
374/**
375 * filemap_fdatawait_range - wait for writeback to complete
376 * @mapping: address space structure to wait for
377 * @start_byte: offset in bytes where the range starts
378 * @end_byte: offset in bytes where the range ends (inclusive)
379 *
380 * Walk the list of under-writeback pages of the given address space
381 * in the given range and wait for all of them. Check error status of
382 * the address space and return it.
383 *
384 * Since the error status of the address space is cleared by this function,
385 * callers are responsible for checking the return value and handling and/or
386 * reporting the error.
387 */
388int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
389 loff_t end_byte)
390{
391 int ret, ret2;
392
393 ret = __filemap_fdatawait_range(mapping, start_byte, end_byte);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700394 ret2 = filemap_check_errors(mapping);
395 if (!ret)
396 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return ret;
399}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200400EXPORT_SYMBOL(filemap_fdatawait_range);
401
402/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800403 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
404 * @mapping: address space structure to wait for
405 *
406 * Walk the list of under-writeback pages of the given address space
407 * and wait for all of them. Unlike filemap_fdatawait(), this function
408 * does not clear error status of the address space.
409 *
410 * Use this function if callers don't handle errors themselves. Expected
411 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
412 * fsfreeze(8)
413 */
414void filemap_fdatawait_keep_errors(struct address_space *mapping)
415{
416 loff_t i_size = i_size_read(mapping->host);
417
418 if (i_size == 0)
419 return;
420
421 __filemap_fdatawait_range(mapping, 0, i_size - 1);
422}
423
424/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700425 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700427 *
428 * Walk the list of under-writeback pages of the given address space
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800429 * and wait for all of them. Check error status of the address space
430 * and return it.
431 *
432 * Since the error status of the address space is cleared by this function,
433 * callers are responsible for checking the return value and handling and/or
434 * reporting the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
436int filemap_fdatawait(struct address_space *mapping)
437{
438 loff_t i_size = i_size_read(mapping->host);
439
440 if (i_size == 0)
441 return 0;
442
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200443 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445EXPORT_SYMBOL(filemap_fdatawait);
446
447int filemap_write_and_wait(struct address_space *mapping)
448{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800449 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800451 if ((!dax_mapping(mapping) && mapping->nrpages) ||
452 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800453 err = filemap_fdatawrite(mapping);
454 /*
455 * Even if the above returned error, the pages may be
456 * written partially (e.g. -ENOSPC), so we wait for it.
457 * But the -EIO is special case, it may indicate the worst
458 * thing (e.g. bug) happened, so we avoid waiting for it.
459 */
460 if (err != -EIO) {
461 int err2 = filemap_fdatawait(mapping);
462 if (!err)
463 err = err2;
464 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700465 } else {
466 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800470EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Randy Dunlap485bb992006-06-23 02:03:49 -0700472/**
473 * filemap_write_and_wait_range - write out & wait on a file range
474 * @mapping: the address_space for the pages
475 * @lstart: offset in bytes where the range starts
476 * @lend: offset in bytes where the range ends (inclusive)
477 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800478 * Write out and wait upon file offsets lstart->lend, inclusive.
479 *
480 * Note that `lend' is inclusive (describes the last byte to be written) so
481 * that this function can be used to write to the very end-of-file (end = -1).
482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483int filemap_write_and_wait_range(struct address_space *mapping,
484 loff_t lstart, loff_t lend)
485{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800486 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800488 if ((!dax_mapping(mapping) && mapping->nrpages) ||
489 (dax_mapping(mapping) && mapping->nrexceptional)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800490 err = __filemap_fdatawrite_range(mapping, lstart, lend,
491 WB_SYNC_ALL);
492 /* See comment of filemap_write_and_wait() */
493 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200494 int err2 = filemap_fdatawait_range(mapping,
495 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800496 if (!err)
497 err = err2;
498 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700499 } else {
500 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
Chris Masonf6995582009-04-15 13:22:37 -0400504EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Randy Dunlap485bb992006-06-23 02:03:49 -0700506/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700507 * replace_page_cache_page - replace a pagecache page with a new one
508 * @old: page to be replaced
509 * @new: page to replace with
510 * @gfp_mask: allocation mode
511 *
512 * This function replaces a page in the pagecache with a new one. On
513 * success it acquires the pagecache reference for the new page and
514 * drops it for the old page. Both the old and new pages must be
515 * locked. This function does not add the new page to the LRU, the
516 * caller must do that.
517 *
518 * The remove + add is atomic. The only way this function can fail is
519 * memory allocation failure.
520 */
521int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
522{
523 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700524
Sasha Levin309381fea2014-01-23 15:52:54 -0800525 VM_BUG_ON_PAGE(!PageLocked(old), old);
526 VM_BUG_ON_PAGE(!PageLocked(new), new);
527 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700528
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700529 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
530 if (!error) {
531 struct address_space *mapping = old->mapping;
532 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400533 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700534
535 pgoff_t offset = old->index;
536 freepage = mapping->a_ops->freepage;
537
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300538 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700539 new->mapping = mapping;
540 new->index = offset;
541
Greg Thelenc4843a72015-05-22 17:13:16 -0400542 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700543 __delete_from_page_cache(old, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700544 error = radix_tree_insert(&mapping->page_tree, offset, new);
545 BUG_ON(error);
546 mapping->nrpages++;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700547
548 /*
549 * hugetlb pages do not participate in page cache accounting.
550 */
551 if (!PageHuge(new))
552 __inc_zone_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700553 if (PageSwapBacked(new))
554 __inc_zone_page_state(new, NR_SHMEM);
Greg Thelenc4843a72015-05-22 17:13:16 -0400555 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700556 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700557 radix_tree_preload_end();
558 if (freepage)
559 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300560 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700561 }
562
563 return error;
564}
565EXPORT_SYMBOL_GPL(replace_page_cache_page);
566
Johannes Weiner0cd61442014-04-03 14:47:46 -0700567static int page_cache_tree_insert(struct address_space *mapping,
Johannes Weinera5289102014-04-03 14:47:51 -0700568 struct page *page, void **shadowp)
Johannes Weiner0cd61442014-04-03 14:47:46 -0700569{
Johannes Weiner449dd692014-04-03 14:47:56 -0700570 struct radix_tree_node *node;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700571 void **slot;
572 int error;
573
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700574 error = __radix_tree_create(&mapping->page_tree, page->index, 0,
Johannes Weiner449dd692014-04-03 14:47:56 -0700575 &node, &slot);
576 if (error)
577 return error;
578 if (*slot) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700579 void *p;
580
581 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
582 if (!radix_tree_exceptional_entry(p))
583 return -EEXIST;
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800584
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800585 mapping->nrexceptional--;
Jan Kara4f622932016-05-12 18:29:17 +0200586 if (!dax_mapping(mapping)) {
587 if (shadowp)
588 *shadowp = p;
589 if (node)
590 workingset_node_shadows_dec(node);
591 } else {
592 /* DAX can replace empty locked entry with a hole */
593 WARN_ON_ONCE(p !=
594 (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
595 RADIX_DAX_ENTRY_LOCK));
596 /* DAX accounts exceptional entries as normal pages */
597 if (node)
598 workingset_node_pages_dec(node);
Jan Karaac401cc2016-05-12 18:29:18 +0200599 /* Wakeup waiters for exceptional entry lock */
600 dax_wake_mapping_entry_waiter(mapping, page->index,
601 false);
Jan Kara4f622932016-05-12 18:29:17 +0200602 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700603 }
Johannes Weiner449dd692014-04-03 14:47:56 -0700604 radix_tree_replace_slot(slot, page);
605 mapping->nrpages++;
606 if (node) {
607 workingset_node_pages_inc(node);
608 /*
609 * Don't track node that contains actual pages.
610 *
611 * Avoid acquiring the list_lru lock if already
612 * untracked. The list_empty() test is safe as
613 * node->private_list is protected by
614 * mapping->tree_lock.
615 */
616 if (!list_empty(&node->private_list))
617 list_lru_del(&workingset_shadow_nodes,
618 &node->private_list);
619 }
620 return 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700621}
622
Johannes Weinera5289102014-04-03 14:47:51 -0700623static int __add_to_page_cache_locked(struct page *page,
624 struct address_space *mapping,
625 pgoff_t offset, gfp_t gfp_mask,
626 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Johannes Weiner00501b52014-08-08 14:19:20 -0700628 int huge = PageHuge(page);
629 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700630 int error;
631
Sasha Levin309381fea2014-01-23 15:52:54 -0800632 VM_BUG_ON_PAGE(!PageLocked(page), page);
633 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700634
Johannes Weiner00501b52014-08-08 14:19:20 -0700635 if (!huge) {
636 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800637 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700638 if (error)
639 return error;
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jan Kara5e4c0d972013-09-11 14:26:05 -0700642 error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700643 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700644 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800645 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700646 return error;
647 }
648
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300649 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700650 page->mapping = mapping;
651 page->index = offset;
652
653 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700654 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700655 radix_tree_preload_end();
656 if (unlikely(error))
657 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700658
659 /* hugetlb pages do not participate in page cache accounting. */
660 if (!huge)
661 __inc_zone_page_state(page, NR_FILE_PAGES);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700662 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700663 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800664 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700665 trace_mm_filemap_add_to_page_cache(page);
666 return 0;
667err_insert:
668 page->mapping = NULL;
669 /* Leave page->index set: truncation relies upon it */
670 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700671 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800672 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300673 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return error;
675}
Johannes Weinera5289102014-04-03 14:47:51 -0700676
677/**
678 * add_to_page_cache_locked - add a locked page to the pagecache
679 * @page: page to add
680 * @mapping: the page's address_space
681 * @offset: page index
682 * @gfp_mask: page allocation mode
683 *
684 * This function is used to add a page to the pagecache. It must be locked.
685 * This function does not add the page to the LRU. The caller must do that.
686 */
687int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
688 pgoff_t offset, gfp_t gfp_mask)
689{
690 return __add_to_page_cache_locked(page, mapping, offset,
691 gfp_mask, NULL);
692}
Nick Piggine2867812008-07-25 19:45:30 -0700693EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400696 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Johannes Weinera5289102014-04-03 14:47:51 -0700698 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700699 int ret;
700
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800701 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700702 ret = __add_to_page_cache_locked(page, mapping, offset,
703 gfp_mask, &shadow);
704 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800705 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700706 else {
707 /*
708 * The page might have been evicted from cache only
709 * recently, in which case it should be activated like
710 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700711 * The exception is pages getting rewritten; evicting other
712 * data from the working set, only to cache data that will
713 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700714 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700715 if (!(gfp_mask & __GFP_WRITE) &&
716 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700717 SetPageActive(page);
718 workingset_activation(page);
719 } else
720 ClearPageActive(page);
721 lru_cache_add(page);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return ret;
724}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300725EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Paul Jackson44110fe2006-03-24 03:16:04 -0800727#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700728struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800729{
Miao Xiec0ff7452010-05-24 14:32:08 -0700730 int n;
731 struct page *page;
732
Paul Jackson44110fe2006-03-24 03:16:04 -0800733 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700734 unsigned int cpuset_mems_cookie;
735 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700736 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700737 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700738 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700739 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700740
Miao Xiec0ff7452010-05-24 14:32:08 -0700741 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800742 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700743 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800744}
Nick Piggin2ae88142006-10-28 10:38:23 -0700745EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800746#endif
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/*
749 * In order to wait for pages to become available there must be
750 * waitqueues associated with pages. By using a hash table of
751 * waitqueues where the bucket discipline is to maintain all
752 * waiters on the same queue and wake all when any of the pages
753 * become available, and for the woken contexts to check to be
754 * sure the appropriate page became available, this saves space
755 * at a cost of "thundering herd" phenomena during rare hash
756 * collisions.
757 */
NeilBrowna4796e32014-09-24 11:28:32 +1000758wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 const struct zone *zone = page_zone(page);
761
762 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
763}
NeilBrowna4796e32014-09-24 11:28:32 +1000764EXPORT_SYMBOL(page_waitqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Harvey Harrison920c7a52008-02-04 22:29:26 -0800766void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
769
770 if (test_bit(bit_nr, &page->flags))
NeilBrown74316202014-07-07 15:16:04 +1000771 __wait_on_bit(page_waitqueue(page), &wait, bit_wait_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 TASK_UNINTERRUPTIBLE);
773}
774EXPORT_SYMBOL(wait_on_page_bit);
775
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700776int wait_on_page_bit_killable(struct page *page, int bit_nr)
777{
778 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
779
780 if (!test_bit(bit_nr, &page->flags))
781 return 0;
782
783 return __wait_on_bit(page_waitqueue(page), &wait,
NeilBrown74316202014-07-07 15:16:04 +1000784 bit_wait_io, TASK_KILLABLE);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700785}
786
NeilBrowncbbce822014-09-25 13:55:19 +1000787int wait_on_page_bit_killable_timeout(struct page *page,
788 int bit_nr, unsigned long timeout)
789{
790 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
791
792 wait.key.timeout = jiffies + timeout;
793 if (!test_bit(bit_nr, &page->flags))
794 return 0;
795 return __wait_on_bit(page_waitqueue(page), &wait,
796 bit_wait_io_timeout, TASK_KILLABLE);
797}
798EXPORT_SYMBOL_GPL(wait_on_page_bit_killable_timeout);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100801 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700802 * @page: Page defining the wait queue of interest
803 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100804 *
805 * Add an arbitrary @waiter to the wait queue for the nominated @page.
806 */
807void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
808{
809 wait_queue_head_t *q = page_waitqueue(page);
810 unsigned long flags;
811
812 spin_lock_irqsave(&q->lock, flags);
813 __add_wait_queue(q, waiter);
814 spin_unlock_irqrestore(&q->lock, flags);
815}
816EXPORT_SYMBOL_GPL(add_page_wait_queue);
817
818/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700819 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * @page: the page
821 *
822 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
823 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +0900824 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
826 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700827 * The mb is necessary to enforce ordering between the clear_bit and the read
828 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800830void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800832 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800833 VM_BUG_ON_PAGE(!PageLocked(page), page);
Nick Piggin8413ac92008-10-18 20:26:59 -0700834 clear_bit_unlock(PG_locked, &page->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100835 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 wake_up_page(page, PG_locked);
837}
838EXPORT_SYMBOL(unlock_page);
839
Randy Dunlap485bb992006-06-23 02:03:49 -0700840/**
841 * end_page_writeback - end writeback against a page
842 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844void end_page_writeback(struct page *page)
845{
Mel Gorman888cf2d2014-06-04 16:10:34 -0700846 /*
847 * TestClearPageReclaim could be used here but it is an atomic
848 * operation and overkill in this particular case. Failing to
849 * shuffle a page marked for immediate reclaim is too mild to
850 * justify taking an atomic operation penalty at the end of
851 * ever page writeback.
852 */
853 if (PageReclaim(page)) {
854 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700855 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -0700856 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700857
858 if (!test_clear_page_writeback(page))
859 BUG();
860
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100861 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 wake_up_page(page, PG_writeback);
863}
864EXPORT_SYMBOL(end_page_writeback);
865
Matthew Wilcox57d99842014-06-04 16:07:45 -0700866/*
867 * After completing I/O on a page, call this routine to update the page
868 * flags appropriately
869 */
870void page_endio(struct page *page, int rw, int err)
871{
872 if (rw == READ) {
873 if (!err) {
874 SetPageUptodate(page);
875 } else {
876 ClearPageUptodate(page);
877 SetPageError(page);
878 }
879 unlock_page(page);
880 } else { /* rw == WRITE */
881 if (err) {
882 SetPageError(page);
883 if (page->mapping)
884 mapping_set_error(page->mapping, err);
885 }
886 end_page_writeback(page);
887 }
888}
889EXPORT_SYMBOL_GPL(page_endio);
890
Randy Dunlap485bb992006-06-23 02:03:49 -0700891/**
892 * __lock_page - get a lock on the page, assuming we need to sleep to get it
893 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800895void __lock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800897 struct page *page_head = compound_head(page);
898 DEFINE_WAIT_BIT(wait, &page_head->flags, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800900 __wait_on_bit_lock(page_waitqueue(page_head), &wait, bit_wait_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 TASK_UNINTERRUPTIBLE);
902}
903EXPORT_SYMBOL(__lock_page);
904
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800905int __lock_page_killable(struct page *page)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500906{
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800907 struct page *page_head = compound_head(page);
908 DEFINE_WAIT_BIT(wait, &page_head->flags, PG_locked);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500909
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800910 return __wait_on_bit_lock(page_waitqueue(page_head), &wait,
NeilBrown74316202014-07-07 15:16:04 +1000911 bit_wait_io, TASK_KILLABLE);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500912}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300913EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500914
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700915/*
916 * Return values:
917 * 1 - page is locked; mmap_sem is still held.
918 * 0 - page is not locked.
919 * mmap_sem has been released (up_read()), unless flags had both
920 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
921 * which case mmap_sem is still held.
922 *
923 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
924 * with the page locked and the mmap_sem unperturbed.
925 */
Michel Lespinassed065bd82010-10-26 14:21:57 -0700926int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
927 unsigned int flags)
928{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700929 if (flags & FAULT_FLAG_ALLOW_RETRY) {
930 /*
931 * CAUTION! In this case, mmap_sem is not released
932 * even though return 0.
933 */
934 if (flags & FAULT_FLAG_RETRY_NOWAIT)
935 return 0;
936
937 up_read(&mm->mmap_sem);
938 if (flags & FAULT_FLAG_KILLABLE)
939 wait_on_page_locked_killable(page);
940 else
Gleb Natapov318b2752011-03-22 16:30:51 -0700941 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -0700942 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700943 } else {
944 if (flags & FAULT_FLAG_KILLABLE) {
945 int ret;
946
947 ret = __lock_page_killable(page);
948 if (ret) {
949 up_read(&mm->mmap_sem);
950 return 0;
951 }
952 } else
953 __lock_page(page);
954 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -0700955 }
956}
957
Randy Dunlap485bb992006-06-23 02:03:49 -0700958/**
Johannes Weinere7b563b2014-04-03 14:47:44 -0700959 * page_cache_next_hole - find the next hole (not-present entry)
960 * @mapping: mapping
961 * @index: index
962 * @max_scan: maximum range to search
963 *
964 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
965 * lowest indexed hole.
966 *
967 * Returns: the index of the hole if found, otherwise returns an index
968 * outside of the set specified (in which case 'return - index >=
969 * max_scan' will be true). In rare cases of index wrap-around, 0 will
970 * be returned.
971 *
972 * page_cache_next_hole may be called under rcu_read_lock. However,
973 * like radix_tree_gang_lookup, this will not atomically search a
974 * snapshot of the tree at a single point in time. For example, if a
975 * hole is created at index 5, then subsequently a hole is created at
976 * index 10, page_cache_next_hole covering both indexes may return 10
977 * if called under rcu_read_lock.
978 */
979pgoff_t page_cache_next_hole(struct address_space *mapping,
980 pgoff_t index, unsigned long max_scan)
981{
982 unsigned long i;
983
984 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700985 struct page *page;
986
987 page = radix_tree_lookup(&mapping->page_tree, index);
988 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -0700989 break;
990 index++;
991 if (index == 0)
992 break;
993 }
994
995 return index;
996}
997EXPORT_SYMBOL(page_cache_next_hole);
998
999/**
1000 * page_cache_prev_hole - find the prev hole (not-present entry)
1001 * @mapping: mapping
1002 * @index: index
1003 * @max_scan: maximum range to search
1004 *
1005 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1006 * the first hole.
1007 *
1008 * Returns: the index of the hole if found, otherwise returns an index
1009 * outside of the set specified (in which case 'index - return >=
1010 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1011 * will be returned.
1012 *
1013 * page_cache_prev_hole may be called under rcu_read_lock. However,
1014 * like radix_tree_gang_lookup, this will not atomically search a
1015 * snapshot of the tree at a single point in time. For example, if a
1016 * hole is created at index 10, then subsequently a hole is created at
1017 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1018 * called under rcu_read_lock.
1019 */
1020pgoff_t page_cache_prev_hole(struct address_space *mapping,
1021 pgoff_t index, unsigned long max_scan)
1022{
1023 unsigned long i;
1024
1025 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001026 struct page *page;
1027
1028 page = radix_tree_lookup(&mapping->page_tree, index);
1029 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001030 break;
1031 index--;
1032 if (index == ULONG_MAX)
1033 break;
1034 }
1035
1036 return index;
1037}
1038EXPORT_SYMBOL(page_cache_prev_hole);
1039
1040/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001041 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001042 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001043 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001044 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001045 * Looks up the page cache slot at @mapping & @offset. If there is a
1046 * page cache page, it is returned with an increased refcount.
1047 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001048 * If the slot holds a shadow entry of a previously evicted page, or a
1049 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001050 *
1051 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001053struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Nick Piggina60637c2008-07-25 19:45:31 -07001055 void **pagep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct page *page;
1057
Nick Piggina60637c2008-07-25 19:45:31 -07001058 rcu_read_lock();
1059repeat:
1060 page = NULL;
1061 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1062 if (pagep) {
1063 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001064 if (unlikely(!page))
1065 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001066 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001067 if (radix_tree_deref_retry(page))
1068 goto repeat;
1069 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001070 * A shadow entry of a recently evicted page,
1071 * or a swap entry from shmem/tmpfs. Return
1072 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001073 */
1074 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001075 }
Nick Piggina60637c2008-07-25 19:45:31 -07001076 if (!page_cache_get_speculative(page))
1077 goto repeat;
1078
1079 /*
1080 * Has the page moved?
1081 * This is part of the lockless pagecache protocol. See
1082 * include/linux/pagemap.h for details.
1083 */
1084 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001086 goto repeat;
1087 }
1088 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001089out:
Nick Piggina60637c2008-07-25 19:45:31 -07001090 rcu_read_unlock();
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return page;
1093}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001094EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Randy Dunlap485bb992006-06-23 02:03:49 -07001096/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001097 * find_lock_entry - locate, pin and lock a page cache entry
1098 * @mapping: the address_space to search
1099 * @offset: the page cache index
1100 *
1101 * Looks up the page cache slot at @mapping & @offset. If there is a
1102 * page cache page, it is returned locked and with an increased
1103 * refcount.
1104 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001105 * If the slot holds a shadow entry of a previously evicted page, or a
1106 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001107 *
1108 * Otherwise, %NULL is returned.
1109 *
1110 * find_lock_entry() may sleep.
1111 */
1112struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct page *page;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001117 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001118 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001119 lock_page(page);
1120 /* Has the page been truncated? */
1121 if (unlikely(page->mapping != mapping)) {
1122 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001123 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001124 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001126 VM_BUG_ON_PAGE(page->index != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return page;
1129}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001130EXPORT_SYMBOL(find_lock_entry);
1131
1132/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001133 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001134 * @mapping: the address_space to search
1135 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001136 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001137 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001138 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001139 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001140 *
Randy Dunlap75325182014-07-30 16:08:37 -07001141 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001142 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001143 * FGP_ACCESSED: the page will be marked accessed
1144 * FGP_LOCK: Page is return locked
1145 * FGP_CREAT: If page is not present then a new page is allocated using
Michal Hocko45f87de2014-12-29 20:30:35 +01001146 * @gfp_mask and added to the page cache and the VM's LRU
1147 * list. The page is returned locked and with an increased
1148 * refcount. Otherwise, %NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001149 *
1150 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1151 * if the GFP flags specified for FGP_CREAT are atomic.
1152 *
1153 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001154 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001155struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001156 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Nick Piggineb2be182007-10-16 01:24:57 -07001158 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001161 page = find_get_entry(mapping, offset);
1162 if (radix_tree_exceptional_entry(page))
1163 page = NULL;
1164 if (!page)
1165 goto no_page;
1166
1167 if (fgp_flags & FGP_LOCK) {
1168 if (fgp_flags & FGP_NOWAIT) {
1169 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001171 return NULL;
1172 }
1173 } else {
1174 lock_page(page);
1175 }
1176
1177 /* Has the page been truncated? */
1178 if (unlikely(page->mapping != mapping)) {
1179 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001180 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001181 goto repeat;
1182 }
1183 VM_BUG_ON_PAGE(page->index != offset, page);
1184 }
1185
1186 if (page && (fgp_flags & FGP_ACCESSED))
1187 mark_page_accessed(page);
1188
1189no_page:
1190 if (!page && (fgp_flags & FGP_CREAT)) {
1191 int err;
1192 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001193 gfp_mask |= __GFP_WRITE;
1194 if (fgp_flags & FGP_NOFS)
1195 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001196
Michal Hocko45f87de2014-12-29 20:30:35 +01001197 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001198 if (!page)
1199 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001200
1201 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1202 fgp_flags |= FGP_LOCK;
1203
Hugh Dickinseb39d612014-08-06 16:06:43 -07001204 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001205 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001206 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001207
Michal Hocko45f87de2014-12-29 20:30:35 +01001208 err = add_to_page_cache_lru(page, mapping, offset,
1209 gfp_mask & GFP_RECLAIM_MASK);
Nick Piggineb2be182007-10-16 01:24:57 -07001210 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001212 page = NULL;
1213 if (err == -EEXIST)
1214 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return page;
1219}
Mel Gorman2457aec2014-06-04 16:10:31 -07001220EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001223 * find_get_entries - gang pagecache lookup
1224 * @mapping: The address_space to search
1225 * @start: The starting page cache index
1226 * @nr_entries: The maximum number of entries
1227 * @entries: Where the resulting entries are placed
1228 * @indices: The cache indices corresponding to the entries in @entries
1229 *
1230 * find_get_entries() will search for and return a group of up to
1231 * @nr_entries entries in the mapping. The entries are placed at
1232 * @entries. find_get_entries() takes a reference against any actual
1233 * pages it returns.
1234 *
1235 * The search returns a group of mapping-contiguous page cache entries
1236 * with ascending indexes. There may be holes in the indices due to
1237 * not-present pages.
1238 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001239 * Any shadow entries of evicted pages, or swap entries from
1240 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001241 *
1242 * find_get_entries() returns the number of pages and shadow entries
1243 * which were found.
1244 */
1245unsigned find_get_entries(struct address_space *mapping,
1246 pgoff_t start, unsigned int nr_entries,
1247 struct page **entries, pgoff_t *indices)
1248{
1249 void **slot;
1250 unsigned int ret = 0;
1251 struct radix_tree_iter iter;
1252
1253 if (!nr_entries)
1254 return 0;
1255
1256 rcu_read_lock();
Johannes Weiner0cd61442014-04-03 14:47:46 -07001257 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1258 struct page *page;
1259repeat:
1260 page = radix_tree_deref_slot(slot);
1261 if (unlikely(!page))
1262 continue;
1263 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001264 if (radix_tree_deref_retry(page)) {
1265 slot = radix_tree_iter_retry(&iter);
1266 continue;
1267 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001268 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001269 * A shadow entry of a recently evicted page, a swap
1270 * entry from shmem/tmpfs or a DAX entry. Return it
1271 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001272 */
1273 goto export;
1274 }
1275 if (!page_cache_get_speculative(page))
1276 goto repeat;
1277
1278 /* Has the page moved? */
1279 if (unlikely(page != *slot)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001280 put_page(page);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001281 goto repeat;
1282 }
1283export:
1284 indices[ret] = iter.index;
1285 entries[ret] = page;
1286 if (++ret == nr_entries)
1287 break;
1288 }
1289 rcu_read_unlock();
1290 return ret;
1291}
1292
1293/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 * find_get_pages - gang pagecache lookup
1295 * @mapping: The address_space to search
1296 * @start: The starting page index
1297 * @nr_pages: The maximum number of pages
1298 * @pages: Where the resulting pages are placed
1299 *
1300 * find_get_pages() will search for and return a group of up to
1301 * @nr_pages pages in the mapping. The pages are placed at @pages.
1302 * find_get_pages() takes a reference against the returned pages.
1303 *
1304 * The search returns a group of mapping-contiguous pages with ascending
1305 * indexes. There may be holes in the indices due to not-present pages.
1306 *
1307 * find_get_pages() returns the number of pages which were found.
1308 */
1309unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1310 unsigned int nr_pages, struct page **pages)
1311{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001312 struct radix_tree_iter iter;
1313 void **slot;
1314 unsigned ret = 0;
1315
1316 if (unlikely(!nr_pages))
1317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Nick Piggina60637c2008-07-25 19:45:31 -07001319 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001320 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Nick Piggina60637c2008-07-25 19:45:31 -07001321 struct page *page;
1322repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001323 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001324 if (unlikely(!page))
1325 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001326
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001327 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001328 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001329 slot = radix_tree_iter_retry(&iter);
1330 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001331 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001332 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001333 * A shadow entry of a recently evicted page,
1334 * or a swap entry from shmem/tmpfs. Skip
1335 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001336 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001337 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001338 }
Nick Piggina60637c2008-07-25 19:45:31 -07001339
1340 if (!page_cache_get_speculative(page))
1341 goto repeat;
1342
1343 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001344 if (unlikely(page != *slot)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001345 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001346 goto repeat;
1347 }
1348
1349 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001350 if (++ret == nr_pages)
1351 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001352 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001353
Nick Piggina60637c2008-07-25 19:45:31 -07001354 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return ret;
1356}
1357
Jens Axboeebf43502006-04-27 08:46:01 +02001358/**
1359 * find_get_pages_contig - gang contiguous pagecache lookup
1360 * @mapping: The address_space to search
1361 * @index: The starting page index
1362 * @nr_pages: The maximum number of pages
1363 * @pages: Where the resulting pages are placed
1364 *
1365 * find_get_pages_contig() works exactly like find_get_pages(), except
1366 * that the returned number of pages are guaranteed to be contiguous.
1367 *
1368 * find_get_pages_contig() returns the number of pages which were found.
1369 */
1370unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1371 unsigned int nr_pages, struct page **pages)
1372{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001373 struct radix_tree_iter iter;
1374 void **slot;
1375 unsigned int ret = 0;
1376
1377 if (unlikely(!nr_pages))
1378 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001379
Nick Piggina60637c2008-07-25 19:45:31 -07001380 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001381 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Nick Piggina60637c2008-07-25 19:45:31 -07001382 struct page *page;
1383repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001384 page = radix_tree_deref_slot(slot);
1385 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001386 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001387 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001388
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001389 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001390 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001391 slot = radix_tree_iter_retry(&iter);
1392 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001393 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001394 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001395 * A shadow entry of a recently evicted page,
1396 * or a swap entry from shmem/tmpfs. Stop
1397 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001398 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001399 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001400 }
Nick Piggina60637c2008-07-25 19:45:31 -07001401
Nick Piggina60637c2008-07-25 19:45:31 -07001402 if (!page_cache_get_speculative(page))
1403 goto repeat;
1404
1405 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001406 if (unlikely(page != *slot)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001407 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001408 goto repeat;
1409 }
1410
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001411 /*
1412 * must check mapping and index after taking the ref.
1413 * otherwise we can get both false positives and false
1414 * negatives, which is just confusing to the caller.
1415 */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001416 if (page->mapping == NULL || page->index != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001417 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001418 break;
1419 }
1420
Nick Piggina60637c2008-07-25 19:45:31 -07001421 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001422 if (++ret == nr_pages)
1423 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001424 }
Nick Piggina60637c2008-07-25 19:45:31 -07001425 rcu_read_unlock();
1426 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001427}
David Howellsef71c152007-05-09 02:33:44 -07001428EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001429
Randy Dunlap485bb992006-06-23 02:03:49 -07001430/**
1431 * find_get_pages_tag - find and return pages that match @tag
1432 * @mapping: the address_space to search
1433 * @index: the starting page index
1434 * @tag: the tag index
1435 * @nr_pages: the maximum number of pages
1436 * @pages: where the resulting pages are placed
1437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001439 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
1441unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
1442 int tag, unsigned int nr_pages, struct page **pages)
1443{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001444 struct radix_tree_iter iter;
1445 void **slot;
1446 unsigned ret = 0;
1447
1448 if (unlikely(!nr_pages))
1449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Nick Piggina60637c2008-07-25 19:45:31 -07001451 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001452 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1453 &iter, *index, tag) {
Nick Piggina60637c2008-07-25 19:45:31 -07001454 struct page *page;
1455repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001456 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001457 if (unlikely(!page))
1458 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001459
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001460 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001461 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001462 slot = radix_tree_iter_retry(&iter);
1463 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001464 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001465 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001466 * A shadow entry of a recently evicted page.
1467 *
1468 * Those entries should never be tagged, but
1469 * this tree walk is lockless and the tags are
1470 * looked up in bulk, one radix tree node at a
1471 * time, so there is a sizable window for page
1472 * reclaim to evict a page we saw tagged.
1473 *
1474 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001475 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001476 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001477 }
Nick Piggina60637c2008-07-25 19:45:31 -07001478
1479 if (!page_cache_get_speculative(page))
1480 goto repeat;
1481
1482 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001483 if (unlikely(page != *slot)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001484 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001485 goto repeat;
1486 }
1487
1488 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001489 if (++ret == nr_pages)
1490 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001491 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001492
Nick Piggina60637c2008-07-25 19:45:31 -07001493 rcu_read_unlock();
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (ret)
1496 *index = pages[ret - 1]->index + 1;
Nick Piggina60637c2008-07-25 19:45:31 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return ret;
1499}
David Howellsef71c152007-05-09 02:33:44 -07001500EXPORT_SYMBOL(find_get_pages_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001502/**
1503 * find_get_entries_tag - find and return entries that match @tag
1504 * @mapping: the address_space to search
1505 * @start: the starting page cache index
1506 * @tag: the tag index
1507 * @nr_entries: the maximum number of entries
1508 * @entries: where the resulting entries are placed
1509 * @indices: the cache indices corresponding to the entries in @entries
1510 *
1511 * Like find_get_entries, except we only return entries which are tagged with
1512 * @tag.
1513 */
1514unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1515 int tag, unsigned int nr_entries,
1516 struct page **entries, pgoff_t *indices)
1517{
1518 void **slot;
1519 unsigned int ret = 0;
1520 struct radix_tree_iter iter;
1521
1522 if (!nr_entries)
1523 return 0;
1524
1525 rcu_read_lock();
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001526 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1527 &iter, start, tag) {
1528 struct page *page;
1529repeat:
1530 page = radix_tree_deref_slot(slot);
1531 if (unlikely(!page))
1532 continue;
1533 if (radix_tree_exception(page)) {
1534 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001535 slot = radix_tree_iter_retry(&iter);
1536 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001537 }
1538
1539 /*
1540 * A shadow entry of a recently evicted page, a swap
1541 * entry from shmem/tmpfs or a DAX entry. Return it
1542 * without attempting to raise page count.
1543 */
1544 goto export;
1545 }
1546 if (!page_cache_get_speculative(page))
1547 goto repeat;
1548
1549 /* Has the page moved? */
1550 if (unlikely(page != *slot)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001551 put_page(page);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001552 goto repeat;
1553 }
1554export:
1555 indices[ret] = iter.index;
1556 entries[ret] = page;
1557 if (++ret == nr_entries)
1558 break;
1559 }
1560 rcu_read_unlock();
1561 return ret;
1562}
1563EXPORT_SYMBOL(find_get_entries_tag);
1564
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001565/*
1566 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1567 * a _large_ part of the i/o request. Imagine the worst scenario:
1568 *
1569 * ---R__________________________________________B__________
1570 * ^ reading here ^ bad block(assume 4k)
1571 *
1572 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1573 * => failing the whole request => read(R) => read(R+1) =>
1574 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1575 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1576 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1577 *
1578 * It is going insane. Fix it by quickly scaling down the readahead size.
1579 */
1580static void shrink_readahead_size_eio(struct file *filp,
1581 struct file_ra_state *ra)
1582{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001583 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001584}
1585
Randy Dunlap485bb992006-06-23 02:03:49 -07001586/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001587 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001588 * @filp: the file to read
1589 * @ppos: current file position
Al Viro6e58e792014-02-03 17:07:03 -05001590 * @iter: data destination
1591 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001592 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001594 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *
1596 * This is really ugly. But the goto's actually try to clarify some
1597 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Al Viro6e58e792014-02-03 17:07:03 -05001599static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
1600 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001602 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001604 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001605 pgoff_t index;
1606 pgoff_t last_index;
1607 pgoff_t prev_index;
1608 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001609 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001610 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001612 index = *ppos >> PAGE_SHIFT;
1613 prev_index = ra->prev_pos >> PAGE_SHIFT;
1614 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1615 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1616 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 for (;;) {
1619 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001620 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001621 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 unsigned long nr, ret;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625find_page:
1626 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001627 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001628 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001629 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001630 index, last_index - index);
1631 page = find_get_page(mapping, index);
1632 if (unlikely(page == NULL))
1633 goto no_cached_page;
1634 }
1635 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001636 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001637 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001638 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001640 if (!PageUptodate(page)) {
Mel Gormanebded022016-03-15 14:55:39 -07001641 /*
1642 * See comment in do_read_cache_page on why
1643 * wait_on_page_locked is used to avoid unnecessarily
1644 * serialisations and why it's safe.
1645 */
1646 wait_on_page_locked_killable(page);
1647 if (PageUptodate(page))
1648 goto page_ok;
1649
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001650 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001651 !mapping->a_ops->is_partially_uptodate)
1652 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001653 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001654 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001655 /* Did it get truncated before we got the lock? */
1656 if (!page->mapping)
1657 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001658 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05001659 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001660 goto page_not_up_to_date_locked;
1661 unlock_page(page);
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001664 /*
1665 * i_size must be checked after we know the page is Uptodate.
1666 *
1667 * Checking i_size after the check allows us to calculate
1668 * the correct value for "nr", which means the zero-filled
1669 * part of the page is not copied back to userspace (unless
1670 * another truncate extends the file - this is desired though).
1671 */
1672
1673 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001674 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001675 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001676 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001677 goto out;
1678 }
1679
1680 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001681 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001682 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001683 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001684 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001685 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07001686 goto out;
1687 }
1688 }
1689 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* If users can be writing to this page using arbitrary
1692 * virtual addresses, take care about potential aliasing
1693 * before reading the page on the kernel side.
1694 */
1695 if (mapping_writably_mapped(mapping))
1696 flush_dcache_page(page);
1697
1698 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001699 * When a sequential read accesses a page several times,
1700 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
Jan Karaec0f1632007-05-06 14:49:25 -07001702 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 mark_page_accessed(page);
1704 prev_index = index;
1705
1706 /*
1707 * Ok, we have the page, and it's up-to-date, so
1708 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 */
Al Viro6e58e792014-02-03 17:07:03 -05001710
1711 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001713 index += offset >> PAGE_SHIFT;
1714 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001715 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001717 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001718 written += ret;
1719 if (!iov_iter_count(iter))
1720 goto out;
1721 if (ret < nr) {
1722 error = -EFAULT;
1723 goto out;
1724 }
1725 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727page_not_up_to_date:
1728 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001729 error = lock_page_killable(page);
1730 if (unlikely(error))
1731 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001733page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001734 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (!page->mapping) {
1736 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001737 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 continue;
1739 }
1740
1741 /* Did somebody else fill it already? */
1742 if (PageUptodate(page)) {
1743 unlock_page(page);
1744 goto page_ok;
1745 }
1746
1747readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001748 /*
1749 * A previous I/O error may have been due to temporary
1750 * failures, eg. multipath errors.
1751 * PG_error will be set again if readpage fails.
1752 */
1753 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* Start the actual read. The read will unlock the page. */
1755 error = mapping->a_ops->readpage(filp, page);
1756
Zach Brown994fc28c2005-12-15 14:28:17 -08001757 if (unlikely(error)) {
1758 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001759 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001760 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08001761 goto find_page;
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04001767 error = lock_page_killable(page);
1768 if (unlikely(error))
1769 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!PageUptodate(page)) {
1771 if (page->mapping == NULL) {
1772 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001773 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
1775 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001776 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto find_page;
1778 }
1779 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07001780 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04001781 error = -EIO;
1782 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 unlock_page(page);
1785 }
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto page_ok;
1788
1789readpage_error:
1790 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001791 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto out;
1793
1794no_cached_page:
1795 /*
1796 * Ok, it wasn't cached, so we need to create a new
1797 * page..
1798 */
Nick Piggineb2be182007-10-16 01:24:57 -07001799 page = page_cache_alloc_cold(mapping);
1800 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05001801 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07001802 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Michal Hocko6afdb852015-06-24 16:58:06 -07001804 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08001805 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001807 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05001808 if (error == -EEXIST) {
1809 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05001811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto out;
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto readpage;
1815 }
1816
1817out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07001818 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001819 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07001820 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001822 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07001823 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05001824 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Randy Dunlap485bb992006-06-23 02:03:49 -07001827/**
Al Viro6abd2322014-04-04 14:20:57 -04001828 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001829 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04001830 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07001831 *
Al Viro6abd2322014-04-04 14:20:57 -04001832 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * that can use the page cache directly.
1834 */
1835ssize_t
Al Viroed978a82014-03-05 22:53:04 -05001836generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Al Viroed978a82014-03-05 22:53:04 -05001838 struct file *file = iocb->ki_filp;
1839 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07001840 size_t count = iov_iter_count(iter);
1841
1842 if (!count)
1843 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Al Viro2ba48ce2015-04-09 13:52:01 -04001845 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viroed978a82014-03-05 22:53:04 -05001846 struct address_space *mapping = file->f_mapping;
1847 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07001848 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 size = i_size_read(inode);
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001851 retval = filemap_write_and_wait_range(mapping, iocb->ki_pos,
1852 iocb->ki_pos + count - 1);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001853 if (!retval) {
Al Viroed978a82014-03-05 22:53:04 -05001854 struct iov_iter data = *iter;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001855 retval = mapping->a_ops->direct_IO(iocb, &data);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001856 }
Al Viroed978a82014-03-05 22:53:04 -05001857
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001858 if (retval > 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001859 iocb->ki_pos += retval;
Al Viroed978a82014-03-05 22:53:04 -05001860 iov_iter_advance(iter, retval);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001861 }
Josef Bacik66f998f2010-05-23 11:00:54 -04001862
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001863 /*
1864 * Btrfs can have a short DIO read if we encounter
1865 * compressed extents, so if there was an error, or if
1866 * we've already read everything we wanted to, or if
1867 * there was a short read because we hit EOF, go ahead
1868 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08001869 * the rest of the read. Buffered reads will not work for
1870 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001871 */
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001872 if (retval < 0 || !iov_iter_count(iter) || iocb->ki_pos >= size ||
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08001873 IS_DAX(inode)) {
Al Viroed978a82014-03-05 22:53:04 -05001874 file_accessed(file);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001875 goto out;
Steven Whitehouse0e0bcae2006-09-27 14:45:07 -04001876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07001879 retval = do_generic_file_read(file, &iocb->ki_pos, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880out:
1881 return retval;
1882}
Al Viroed978a82014-03-05 22:53:04 -05001883EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001886/**
1887 * page_cache_read - adds requested page to the page cache if not already there
1888 * @file: file to read
1889 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08001890 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07001891 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * This adds the requested page to the page cache if it isn't already there,
1893 * and schedules an I/O to read in its contents from disk.
1894 */
Michal Hockoc20cd452016-01-14 15:20:12 -08001895static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07001898 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001899 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Zach Brown994fc28c2005-12-15 14:28:17 -08001901 do {
Michal Hockoc20cd452016-01-14 15:20:12 -08001902 page = __page_cache_alloc(gfp_mask|__GFP_COLD);
Zach Brown994fc28c2005-12-15 14:28:17 -08001903 if (!page)
1904 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Michal Hockoc20cd452016-01-14 15:20:12 -08001906 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask & GFP_KERNEL);
Zach Brown994fc28c2005-12-15 14:28:17 -08001907 if (ret == 0)
1908 ret = mapping->a_ops->readpage(file, page);
1909 else if (ret == -EEXIST)
1910 ret = 0; /* losing race to add is OK */
1911
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001912 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Zach Brown994fc28c2005-12-15 14:28:17 -08001914 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07001915
Zach Brown994fc28c2005-12-15 14:28:17 -08001916 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
1919#define MMAP_LOTSAMISS (100)
1920
Linus Torvaldsef00e082009-06-16 15:31:25 -07001921/*
1922 * Synchronous readahead happens when we don't even find
1923 * a page in the page cache at all.
1924 */
1925static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1926 struct file_ra_state *ra,
1927 struct file *file,
1928 pgoff_t offset)
1929{
Linus Torvaldsef00e082009-06-16 15:31:25 -07001930 struct address_space *mapping = file->f_mapping;
1931
1932 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07001933 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001934 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001935 if (!ra->ra_pages)
1936 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001937
Joe Perches64363aa2013-07-08 16:00:18 -07001938 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07001939 page_cache_sync_readahead(mapping, ra, file, offset,
1940 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001941 return;
1942 }
1943
Andi Kleen207d04b2011-05-24 17:12:29 -07001944 /* Avoid banging the cache line if not needed */
1945 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001946 ra->mmap_miss++;
1947
1948 /*
1949 * Do we miss much more than hit in this file? If so,
1950 * stop bothering with read-ahead. It will only hurt.
1951 */
1952 if (ra->mmap_miss > MMAP_LOTSAMISS)
1953 return;
1954
Wu Fengguangd30a1102009-06-16 15:31:30 -07001955 /*
1956 * mmap read-around
1957 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08001958 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
1959 ra->size = ra->ra_pages;
1960 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001961 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001962}
1963
1964/*
1965 * Asynchronous readahead happens when we find the page and PG_readahead,
1966 * so we want to possibly extend the readahead further..
1967 */
1968static void do_async_mmap_readahead(struct vm_area_struct *vma,
1969 struct file_ra_state *ra,
1970 struct file *file,
1971 struct page *page,
1972 pgoff_t offset)
1973{
1974 struct address_space *mapping = file->f_mapping;
1975
1976 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07001977 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001978 return;
1979 if (ra->mmap_miss > 0)
1980 ra->mmap_miss--;
1981 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07001982 page_cache_async_readahead(mapping, ra, file,
1983 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001984}
1985
Randy Dunlap485bb992006-06-23 02:03:49 -07001986/**
Nick Piggin54cb8822007-07-19 01:46:59 -07001987 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07001988 * @vma: vma in which the fault was taken
1989 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07001990 *
Nick Piggin54cb8822007-07-19 01:46:59 -07001991 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 * mapped memory region to read in file data during a page fault.
1993 *
1994 * The goto's are kind of ugly, but this streamlines the normal case of having
1995 * it in the page cache, and handles the special cases reasonably without
1996 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001997 *
1998 * vma->vm_mm->mmap_sem must be held on entry.
1999 *
2000 * If our return value has VM_FAULT_RETRY set, it's because
2001 * lock_page_or_retry() returned 0.
2002 * The mmap_sem has usually been released in this case.
2003 * See __lock_page_or_retry() for the exception.
2004 *
2005 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2006 * has not been released.
2007 *
2008 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Nick Piggind0217ac2007-07-19 01:47:03 -07002010int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07002013 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct address_space *mapping = file->f_mapping;
2015 struct file_ra_state *ra = &file->f_ra;
2016 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002017 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct page *page;
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07002019 loff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07002020 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002022 size = round_up(i_size_read(inode), PAGE_SIZE);
2023 if (offset >= size >> PAGE_SHIFT)
Linus Torvalds5307cc12007-10-31 09:19:46 -07002024 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002027 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002029 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002030 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002032 * We found the page, so try async readahead before
2033 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002035 do_async_mmap_readahead(vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002036 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002037 /* No page in the page cache at all */
2038 do_sync_mmap_readahead(vma, ra, file, offset);
2039 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07002040 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002041 ret = VM_FAULT_MAJOR;
2042retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002043 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (!page)
2045 goto no_cached_page;
2046 }
2047
Michel Lespinassed88c0922010-11-02 13:05:18 -07002048 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002049 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002050 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002051 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002052
2053 /* Did it get truncated? */
2054 if (unlikely(page->mapping != mapping)) {
2055 unlock_page(page);
2056 put_page(page);
2057 goto retry_find;
2058 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002059 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002062 * We have a locked page in the page cache, now we need to check
2063 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
Nick Piggind00806b2007-07-19 01:46:57 -07002065 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto page_not_uptodate;
2067
Linus Torvaldsef00e082009-06-16 15:31:25 -07002068 /*
2069 * Found the page and have a reference on it.
2070 * We must recheck i_size under page lock.
2071 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 size = round_up(i_size_read(inode), PAGE_SIZE);
2073 if (unlikely(offset >= size >> PAGE_SHIFT)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002074 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002076 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002077 }
2078
Nick Piggind0217ac2007-07-19 01:47:03 -07002079 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002080 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082no_cached_page:
2083 /*
2084 * We're only likely to ever get here if MADV_RANDOM is in
2085 * effect.
2086 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002087 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 /*
2090 * The page we want has now been added to the page cache.
2091 * In the unlikely event that someone removed it in the
2092 * meantime, we'll just come back here and read it again.
2093 */
2094 if (error >= 0)
2095 goto retry_find;
2096
2097 /*
2098 * An error return from page_cache_read can result if the
2099 * system is low on memory, or a problem occurs while trying
2100 * to schedule I/O.
2101 */
2102 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002103 return VM_FAULT_OOM;
2104 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * Umm, take care of errors if the page isn't up-to-date.
2109 * Try to re-read it _once_. We do this synchronously,
2110 * because there really aren't any performance issues here
2111 * and we need to check for errors.
2112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002114 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002115 if (!error) {
2116 wait_on_page_locked(page);
2117 if (!PageUptodate(page))
2118 error = -EIO;
2119 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002120 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002121
2122 if (!error || error == AOP_TRUNCATED_PAGE)
2123 goto retry_find;
2124
2125 /* Things didn't work out. Return zero to tell the mm layer so. */
2126 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002127 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002128}
2129EXPORT_SYMBOL(filemap_fault);
2130
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002131void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
2132{
2133 struct radix_tree_iter iter;
2134 void **slot;
2135 struct file *file = vma->vm_file;
2136 struct address_space *mapping = file->f_mapping;
2137 loff_t size;
2138 struct page *page;
2139 unsigned long address = (unsigned long) vmf->virtual_address;
2140 unsigned long addr;
2141 pte_t *pte;
2142
2143 rcu_read_lock();
2144 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, vmf->pgoff) {
2145 if (iter.index > vmf->max_pgoff)
2146 break;
2147repeat:
2148 page = radix_tree_deref_slot(slot);
2149 if (unlikely(!page))
2150 goto next;
2151 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002152 if (radix_tree_deref_retry(page)) {
2153 slot = radix_tree_iter_retry(&iter);
2154 continue;
2155 }
2156 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002157 }
2158
2159 if (!page_cache_get_speculative(page))
2160 goto repeat;
2161
2162 /* Has the page moved? */
2163 if (unlikely(page != *slot)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002164 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002165 goto repeat;
2166 }
2167
2168 if (!PageUptodate(page) ||
2169 PageReadahead(page) ||
2170 PageHWPoison(page))
2171 goto skip;
2172 if (!trylock_page(page))
2173 goto skip;
2174
2175 if (page->mapping != mapping || !PageUptodate(page))
2176 goto unlock;
2177
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002178 size = round_up(i_size_read(mapping->host), PAGE_SIZE);
2179 if (page->index >= size >> PAGE_SHIFT)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002180 goto unlock;
2181
2182 pte = vmf->pte + page->index - vmf->pgoff;
2183 if (!pte_none(*pte))
2184 goto unlock;
2185
2186 if (file->f_ra.mmap_miss > 0)
2187 file->f_ra.mmap_miss--;
2188 addr = address + (page->index - vmf->pgoff) * PAGE_SIZE;
Kirill A. Shutemov315d09b2016-06-24 14:49:45 -07002189 do_set_pte(vma, addr, page, pte, false, false);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002190 unlock_page(page);
2191 goto next;
2192unlock:
2193 unlock_page(page);
2194skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002196next:
2197 if (iter.index == vmf->max_pgoff)
2198 break;
2199 }
2200 rcu_read_unlock();
2201}
2202EXPORT_SYMBOL(filemap_map_pages);
2203
Jan Kara4fcf1c62012-06-12 16:20:29 +02002204int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2205{
2206 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002207 struct inode *inode = file_inode(vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002208 int ret = VM_FAULT_LOCKED;
2209
Jan Kara14da9202012-06-12 16:20:37 +02002210 sb_start_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002211 file_update_time(vma->vm_file);
2212 lock_page(page);
2213 if (page->mapping != inode->i_mapping) {
2214 unlock_page(page);
2215 ret = VM_FAULT_NOPAGE;
2216 goto out;
2217 }
Jan Kara14da9202012-06-12 16:20:37 +02002218 /*
2219 * We mark the page dirty already here so that when freeze is in
2220 * progress, we are guaranteed that writeback during freezing will
2221 * see the dirty page and writeprotect it again.
2222 */
2223 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002224 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002225out:
Jan Kara14da9202012-06-12 16:20:37 +02002226 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002227 return ret;
2228}
2229EXPORT_SYMBOL(filemap_page_mkwrite);
2230
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002231const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002232 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002233 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002234 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235};
2236
2237/* This is used for a general mmap of a disk file */
2238
2239int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2240{
2241 struct address_space *mapping = file->f_mapping;
2242
2243 if (!mapping->a_ops->readpage)
2244 return -ENOEXEC;
2245 file_accessed(file);
2246 vma->vm_ops = &generic_file_vm_ops;
2247 return 0;
2248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250/*
2251 * This is for filesystems which do not implement ->writepage.
2252 */
2253int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2254{
2255 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2256 return -EINVAL;
2257 return generic_file_mmap(file, vma);
2258}
2259#else
2260int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2261{
2262 return -ENOSYS;
2263}
2264int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2265{
2266 return -ENOSYS;
2267}
2268#endif /* CONFIG_MMU */
2269
2270EXPORT_SYMBOL(generic_file_mmap);
2271EXPORT_SYMBOL(generic_file_readonly_mmap);
2272
Sasha Levin67f9fd92014-04-03 14:48:18 -07002273static struct page *wait_on_page_read(struct page *page)
2274{
2275 if (!IS_ERR(page)) {
2276 wait_on_page_locked(page);
2277 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002278 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002279 page = ERR_PTR(-EIO);
2280 }
2281 }
2282 return page;
2283}
2284
Mel Gorman32b63522016-03-15 14:55:36 -07002285static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002286 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002287 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002288 void *data,
2289 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Nick Piggineb2be182007-10-16 01:24:57 -07002291 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int err;
2293repeat:
2294 page = find_get_page(mapping, index);
2295 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002296 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002297 if (!page)
2298 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002299 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002300 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002301 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002302 if (err == -EEXIST)
2303 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return ERR_PTR(err);
2306 }
Mel Gorman32b63522016-03-15 14:55:36 -07002307
2308filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 err = filler(data, page);
2310 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002311 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002312 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Mel Gorman32b63522016-03-15 14:55:36 -07002314
2315 page = wait_on_page_read(page);
2316 if (IS_ERR(page))
2317 return page;
2318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (PageUptodate(page))
2321 goto out;
2322
Mel Gormanebded022016-03-15 14:55:39 -07002323 /*
2324 * Page is not up to date and may be locked due one of the following
2325 * case a: Page is being filled and the page lock is held
2326 * case b: Read/write error clearing the page uptodate status
2327 * case c: Truncation in progress (page locked)
2328 * case d: Reclaim in progress
2329 *
2330 * Case a, the page will be up to date when the page is unlocked.
2331 * There is no need to serialise on the page lock here as the page
2332 * is pinned so the lock gives no additional protection. Even if the
2333 * the page is truncated, the data is still valid if PageUptodate as
2334 * it's a race vs truncate race.
2335 * Case b, the page will not be up to date
2336 * Case c, the page may be truncated but in itself, the data may still
2337 * be valid after IO completes as it's a read vs truncate race. The
2338 * operation must restart if the page is not uptodate on unlock but
2339 * otherwise serialising on page lock to stabilise the mapping gives
2340 * no additional guarantees to the caller as the page lock is
2341 * released before return.
2342 * Case d, similar to truncation. If reclaim holds the page lock, it
2343 * will be a race with remove_mapping that determines if the mapping
2344 * is valid on unlock but otherwise the data is valid and there is
2345 * no need to serialise with page lock.
2346 *
2347 * As the page lock gives no additional guarantee, we optimistically
2348 * wait on the page to be unlocked and check if it's up to date and
2349 * use the page if it is. Otherwise, the page lock is required to
2350 * distinguish between the different cases. The motivation is that we
2351 * avoid spurious serialisations and wakeups when multiple processes
2352 * wait on the same page for IO to complete.
2353 */
2354 wait_on_page_locked(page);
2355 if (PageUptodate(page))
2356 goto out;
2357
2358 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002360
2361 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (!page->mapping) {
2363 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002364 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002365 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
Mel Gormanebded022016-03-15 14:55:39 -07002367
2368 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (PageUptodate(page)) {
2370 unlock_page(page);
2371 goto out;
2372 }
Mel Gorman32b63522016-03-15 14:55:36 -07002373 goto filler;
2374
David Howellsc855ff32007-05-09 13:42:20 +01002375out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002376 mark_page_accessed(page);
2377 return page;
2378}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002379
2380/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002381 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002382 * @mapping: the page's address_space
2383 * @index: the page index
2384 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002385 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002386 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002387 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002388 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002389 *
2390 * If the page does not get brought uptodate, return -EIO.
2391 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002392struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002393 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002394 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002395 void *data)
2396{
2397 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2398}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002399EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002400
2401/**
2402 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2403 * @mapping: the page's address_space
2404 * @index: the page index
2405 * @gfp: the page allocator flags to use if allocating
2406 *
2407 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002408 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002409 *
2410 * If the page does not get brought uptodate, return -EIO.
2411 */
2412struct page *read_cache_page_gfp(struct address_space *mapping,
2413 pgoff_t index,
2414 gfp_t gfp)
2415{
2416 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2417
Sasha Levin67f9fd92014-04-03 14:48:18 -07002418 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002419}
2420EXPORT_SYMBOL(read_cache_page_gfp);
2421
Nick Piggin2f718ff2007-10-16 01:24:59 -07002422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 * Performs necessary checks before doing a write
2424 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002425 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 * Returns appropriate error code that caller should return or
2427 * zero in case that write should be allowed.
2428 */
Al Viro3309dd02015-04-09 12:55:47 -04002429inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Al Viro3309dd02015-04-09 12:55:47 -04002431 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002433 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002434 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Al Viro3309dd02015-04-09 12:55:47 -04002436 if (!iov_iter_count(from))
2437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Al Viro0fa6b002015-04-04 04:05:48 -04002439 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002440 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002441 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Al Viro3309dd02015-04-09 12:55:47 -04002443 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Al Viro0fa6b002015-04-04 04:05:48 -04002445 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002446 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002447 send_sig(SIGXFSZ, current, 0);
2448 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Al Viro3309dd02015-04-09 12:55:47 -04002450 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452
2453 /*
2454 * LFS rule
2455 */
Al Viro3309dd02015-04-09 12:55:47 -04002456 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002458 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002460 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
2463 /*
2464 * Are we about to exceed the fs block limit ?
2465 *
2466 * If we have written data it becomes a short write. If we have
2467 * exceeded without writing data we send a signal and return EFBIG.
2468 * Linus frestrict idea will clean these up nicely..
2469 */
Al Viro3309dd02015-04-09 12:55:47 -04002470 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2471 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Al Viro3309dd02015-04-09 12:55:47 -04002473 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2474 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476EXPORT_SYMBOL(generic_write_checks);
2477
Nick Pigginafddba42007-10-16 01:25:01 -07002478int pagecache_write_begin(struct file *file, struct address_space *mapping,
2479 loff_t pos, unsigned len, unsigned flags,
2480 struct page **pagep, void **fsdata)
2481{
2482 const struct address_space_operations *aops = mapping->a_ops;
2483
Nick Piggin4e02ed42008-10-29 14:00:55 -07002484 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002485 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002486}
2487EXPORT_SYMBOL(pagecache_write_begin);
2488
2489int pagecache_write_end(struct file *file, struct address_space *mapping,
2490 loff_t pos, unsigned len, unsigned copied,
2491 struct page *page, void *fsdata)
2492{
2493 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002494
Nick Piggin4e02ed42008-10-29 14:00:55 -07002495 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002496}
2497EXPORT_SYMBOL(pagecache_write_end);
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002500generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 struct file *file = iocb->ki_filp;
2503 struct address_space *mapping = file->f_mapping;
2504 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002505 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002507 size_t write_len;
2508 pgoff_t end;
Al Viro26978b82014-03-10 14:08:45 -04002509 struct iov_iter data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Al Viro0c949332014-03-22 06:51:37 -04002511 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002512 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002513
Nick Piggin48b47c52009-01-06 14:40:22 -08002514 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002515 if (written)
2516 goto out;
2517
2518 /*
2519 * After a write we want buffered reads to be sure to go to disk to get
2520 * the new data. We invalidate clean cached page from the region we're
2521 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002522 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002523 */
2524 if (mapping->nrpages) {
2525 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002526 pos >> PAGE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002527 /*
2528 * If a page can not be invalidated, return 0 to fall back
2529 * to buffered write.
2530 */
2531 if (written) {
2532 if (written == -EBUSY)
2533 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002534 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002535 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002536 }
2537
Al Viro26978b82014-03-10 14:08:45 -04002538 data = *from;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002539 written = mapping->a_ops->direct_IO(iocb, &data);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002540
2541 /*
2542 * Finally, try again to invalidate clean pages which might have been
2543 * cached by non-direct readahead, or faulted in by get_user_pages()
2544 * if the source of the write was an mmap'ed region of the file
2545 * we're writing. Either one is a pretty crazy thing to do,
2546 * so we don't support it 100%. If this invalidation
2547 * fails, tough, the write still worked...
2548 */
2549 if (mapping->nrpages) {
2550 invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002551 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002552 }
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002555 pos += written;
Al Virof8579f82014-03-03 22:03:20 -05002556 iov_iter_advance(from, written);
Namhyung Kim01166512010-10-26 14:21:58 -07002557 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2558 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 mark_inode_dirty(inode);
2560 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002561 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002563out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 return written;
2565}
2566EXPORT_SYMBOL(generic_file_direct_write);
2567
Nick Piggineb2be182007-10-16 01:24:57 -07002568/*
2569 * Find or create a page at the given pagecache position. Return the locked
2570 * page. This function is specifically for buffered writes.
2571 */
Nick Piggin54566b22009-01-04 12:00:53 -08002572struct page *grab_cache_page_write_begin(struct address_space *mapping,
2573 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002574{
Nick Piggineb2be182007-10-16 01:24:57 -07002575 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07002576 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002577
Nick Piggin54566b22009-01-04 12:00:53 -08002578 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002579 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002580
Mel Gorman2457aec2014-06-04 16:10:31 -07002581 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002582 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002583 if (page)
2584 wait_for_stable_page(page);
2585
Nick Piggineb2be182007-10-16 01:24:57 -07002586 return page;
2587}
Nick Piggin54566b22009-01-04 12:00:53 -08002588EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002589
Al Viro3b93f912014-02-11 21:34:08 -05002590ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07002591 struct iov_iter *i, loff_t pos)
2592{
2593 struct address_space *mapping = file->f_mapping;
2594 const struct address_space_operations *a_ops = mapping->a_ops;
2595 long status = 0;
2596 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002597 unsigned int flags = 0;
2598
2599 /*
2600 * Copies from kernel address space cannot fail (NFSD is a big user).
2601 */
Al Viro777eda22014-12-17 04:46:46 -05002602 if (!iter_is_iovec(i))
Nick Piggin674b8922007-10-16 01:25:03 -07002603 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002604
2605 do {
2606 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002607 unsigned long offset; /* Offset into pagecache page */
2608 unsigned long bytes; /* Bytes to write to page */
2609 size_t copied; /* Bytes copied from user */
2610 void *fsdata;
2611
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002612 offset = (pos & (PAGE_SIZE - 1));
2613 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002614 iov_iter_count(i));
2615
2616again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01002617 /*
2618 * Bring in the user page that we will copy from _first_.
2619 * Otherwise there's a nasty deadlock on copying from the
2620 * same page as we're writing to, without it being marked
2621 * up-to-date.
2622 *
2623 * Not only is this an optimisation, but it is also required
2624 * to check that the address is actually valid, when atomic
2625 * usercopies are used, below.
2626 */
2627 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2628 status = -EFAULT;
2629 break;
2630 }
2631
Jan Kara296291c2015-10-22 13:32:21 -07002632 if (fatal_signal_pending(current)) {
2633 status = -EINTR;
2634 break;
2635 }
2636
Nick Piggin674b8922007-10-16 01:25:03 -07002637 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002638 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07002639 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07002640 break;
2641
anfei zhou931e80e2010-02-02 13:44:02 -08002642 if (mapping_writably_mapped(mapping))
2643 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01002644
Nick Pigginafddba42007-10-16 01:25:01 -07002645 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07002646 flush_dcache_page(page);
2647
2648 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2649 page, fsdata);
2650 if (unlikely(status < 0))
2651 break;
2652 copied = status;
2653
2654 cond_resched();
2655
Nick Piggin124d3b72008-02-02 15:01:17 +01002656 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002657 if (unlikely(copied == 0)) {
2658 /*
2659 * If we were unable to copy any data at all, we must
2660 * fall back to a single segment length write.
2661 *
2662 * If we didn't fallback here, we could livelock
2663 * because not all segments in the iov can be copied at
2664 * once without a pagefault.
2665 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002666 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002667 iov_iter_single_seg_count(i));
2668 goto again;
2669 }
Nick Pigginafddba42007-10-16 01:25:01 -07002670 pos += copied;
2671 written += copied;
2672
2673 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07002674 } while (iov_iter_count(i));
2675
2676 return written ? written : status;
2677}
Al Viro3b93f912014-02-11 21:34:08 -05002678EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Jan Karae4dd9de2009-08-17 18:10:06 +02002680/**
Al Viro81742022014-04-03 03:17:43 -04002681 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002682 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04002683 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002684 *
2685 * This function does all the work needed for actually writing data to a
2686 * file. It does all basic checks, removes SUID from the file, updates
2687 * modification times and calls proper subroutines depending on whether we
2688 * do direct IO or a standard buffered write.
2689 *
2690 * It expects i_mutex to be grabbed unless we work on a block device or similar
2691 * object which does not need locking at all.
2692 *
2693 * This function does *not* take care of syncing data in case of O_SYNC write.
2694 * A caller has to handle it. This is mainly due to the fact that we want to
2695 * avoid syncing under i_mutex.
2696 */
Al Viro81742022014-04-03 03:17:43 -04002697ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002700 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05002702 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05002704 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01002707 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02002708 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (err)
2710 goto out;
2711
Josef Bacikc3b2da32012-03-26 09:59:21 -04002712 err = file_update_time(file);
2713 if (err)
2714 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Al Viro2ba48ce2015-04-09 13:52:01 -04002716 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04002717 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002718
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002719 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002720 /*
2721 * If the write stopped short of completing, fall back to
2722 * buffered writes. Some filesystems do this for writes to
2723 * holes, for example. For DAX files, a buffered write will
2724 * not succeed (even if it did, DAX does not handle dirty
2725 * page-cache pages correctly).
2726 */
Al Viro0b8def92015-04-07 10:22:53 -04002727 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05002729
Al Viro0b8def92015-04-07 10:22:53 -04002730 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002731 /*
Al Viro3b93f912014-02-11 21:34:08 -05002732 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002733 * then we want to return the number of bytes which were
2734 * direct-written, or the error code if that was zero. Note
2735 * that this differs from normal direct-io semantics, which
2736 * will return -EFOO even if some bytes were written.
2737 */
Al Viro60bb4522014-08-08 12:39:16 -04002738 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05002739 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002740 goto out;
2741 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002742 /*
2743 * We need to ensure that the page cache pages are written to
2744 * disk and invalidated to preserve the expected O_DIRECT
2745 * semantics.
2746 */
Al Viro3b93f912014-02-11 21:34:08 -05002747 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04002748 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002749 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04002750 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05002751 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002752 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002753 pos >> PAGE_SHIFT,
2754 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002755 } else {
2756 /*
2757 * We don't know how much we wrote, so just return
2758 * the number of bytes which were direct-written
2759 */
2760 }
2761 } else {
Al Viro0b8def92015-04-07 10:22:53 -04002762 written = generic_perform_write(file, from, iocb->ki_pos);
2763 if (likely(written > 0))
2764 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766out:
2767 current->backing_dev_info = NULL;
2768 return written ? written : err;
2769}
Al Viro81742022014-04-03 03:17:43 -04002770EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Jan Karae4dd9de2009-08-17 18:10:06 +02002772/**
Al Viro81742022014-04-03 03:17:43 -04002773 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002774 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04002775 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002776 *
Al Viro81742022014-04-03 03:17:43 -04002777 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02002778 * filesystems. It takes care of syncing the file in case of O_SYNC file
2779 * and acquires i_mutex as needed.
2780 */
Al Viro81742022014-04-03 03:17:43 -04002781ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02002784 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Al Viro59551022016-01-22 15:40:57 -05002787 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04002788 ret = generic_write_checks(iocb, from);
2789 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04002790 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05002791 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Christoph Hellwige2592212016-04-07 08:52:01 -07002793 if (ret > 0)
2794 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return ret;
2796}
Al Viro81742022014-04-03 03:17:43 -04002797EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
David Howellscf9a2ae2006-08-29 19:05:54 +01002799/**
2800 * try_to_release_page() - release old fs-specific metadata on a page
2801 *
2802 * @page: the page which the kernel is trying to free
2803 * @gfp_mask: memory allocation flags (and I/O mode)
2804 *
2805 * The address_space is to try to release any data against the page
2806 * (presumably at page->private). If the release was successful, return `1'.
2807 * Otherwise return zero.
2808 *
David Howells266cf652009-04-03 16:42:36 +01002809 * This may also be called if PG_fscache is set on a page, indicating that the
2810 * page is known to the local caching routines.
2811 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002812 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08002813 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01002814 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002815 */
2816int try_to_release_page(struct page *page, gfp_t gfp_mask)
2817{
2818 struct address_space * const mapping = page->mapping;
2819
2820 BUG_ON(!PageLocked(page));
2821 if (PageWriteback(page))
2822 return 0;
2823
2824 if (mapping && mapping->a_ops->releasepage)
2825 return mapping->a_ops->releasepage(page, gfp_mask);
2826 return try_to_free_buffers(page);
2827}
2828
2829EXPORT_SYMBOL(try_to_release_page);