blob: 1544865fa64a7fcfe50af82399ce8e357a4955e2 [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>
14#include <linux/fs.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070015#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mm.h>
20#include <linux/swap.h>
21#include <linux/mman.h>
22#include <linux/pagemap.h>
23#include <linux/file.h>
24#include <linux/uio.h>
25#include <linux/hash.h>
26#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070027#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pagevec.h>
29#include <linux/blkdev.h>
30#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080031#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070032#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
Johannes Weiner00501b52014-08-08 14:19:20 -070033#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080034#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060035#include <linux/cleancache.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070036#include <linux/rmap.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080037#include "internal.h"
38
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070039#define CREATE_TRACE_POINTS
40#include <trace/events/filemap.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * FIXME: remove all knowledge of the buffer layer from the core VM
44 */
Jan Kara148f9482009-08-17 19:52:36 +020045#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/mman.h>
48
49/*
50 * Shared mappings implemented 30.11.1994. It's not fully working yet,
51 * though.
52 *
53 * Shared mappings now work. 15.8.1995 Bruno.
54 *
55 * finished 'unifying' the page and buffer cache and SMP-threaded the
56 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
57 *
58 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
59 */
60
61/*
62 * Lock ordering:
63 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080064 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070066 * ->swap_lock (exclusive_swap_page, others)
67 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080069 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080070 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
72 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080073 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070074 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
76 *
77 * ->mmap_sem
78 * ->lock_page (access_process_vm)
79 *
Al Viroccad2362014-02-11 22:36:48 -050080 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070081 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060083 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110084 * sb_lock (fs/fs-writeback.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * ->mapping->tree_lock (__sync_single_inode)
86 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080087 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * ->anon_vma.lock (vma_adjust)
89 *
90 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070091 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070093 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070094 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * ->private_lock (try_to_unmap_one)
96 * ->tree_lock (try_to_unmap_one)
97 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -080098 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * ->private_lock (page_remove_rmap->set_page_dirty)
100 * ->tree_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600101 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100102 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Greg Thelenc4843a72015-05-22 17:13:16 -0400103 * ->memcg->move_lock (page_remove_rmap->mem_cgroup_begin_page_stat)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600104 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100105 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
107 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800108 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700109 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700112static void page_cache_tree_delete(struct address_space *mapping,
113 struct page *page, void *shadow)
114{
Johannes Weiner449dd692014-04-03 14:47:56 -0700115 struct radix_tree_node *node;
116 unsigned long index;
117 unsigned int offset;
118 unsigned int tag;
119 void **slot;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700120
Johannes Weiner449dd692014-04-03 14:47:56 -0700121 VM_BUG_ON(!PageLocked(page));
122
123 __radix_tree_lookup(&mapping->page_tree, page->index, &node, &slot);
124
125 if (shadow) {
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700126 mapping->nrshadows++;
127 /*
128 * Make sure the nrshadows update is committed before
129 * the nrpages update so that final truncate racing
130 * with reclaim does not see both counters 0 at the
131 * same time and miss a shadow entry.
132 */
133 smp_wmb();
Johannes Weiner449dd692014-04-03 14:47:56 -0700134 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700135 mapping->nrpages--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700136
137 if (!node) {
138 /* Clear direct pointer tags in root node */
139 mapping->page_tree.gfp_mask &= __GFP_BITS_MASK;
140 radix_tree_replace_slot(slot, shadow);
141 return;
142 }
143
144 /* Clear tree tags for the removed page */
145 index = page->index;
146 offset = index & RADIX_TREE_MAP_MASK;
147 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
148 if (test_bit(offset, node->tags[tag]))
149 radix_tree_tag_clear(&mapping->page_tree, index, tag);
150 }
151
152 /* Delete page, swap shadow entry */
153 radix_tree_replace_slot(slot, shadow);
154 workingset_node_pages_dec(node);
155 if (shadow)
156 workingset_node_shadows_inc(node);
157 else
158 if (__radix_tree_delete_node(&mapping->page_tree, node))
159 return;
160
161 /*
162 * Track node that only contains shadow entries.
163 *
164 * Avoid acquiring the list_lru lock if already tracked. The
165 * list_empty() test is safe as node->private_list is
166 * protected by mapping->tree_lock.
167 */
168 if (!workingset_node_pages(node) &&
169 list_empty(&node->private_list)) {
170 node->private_data = mapping;
171 list_lru_add(&workingset_shadow_nodes, &node->private_list);
172 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Minchan Kime64a7822011-03-22 16:32:44 -0700176 * Delete a page from the page cache and free it. Caller has to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * sure the page is locked and that nobody else uses it - or that usage
Greg Thelenc4843a72015-05-22 17:13:16 -0400178 * is safe. The caller must hold the mapping's tree_lock and
179 * mem_cgroup_begin_page_stat().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Greg Thelenc4843a72015-05-22 17:13:16 -0400181void __delete_from_page_cache(struct page *page, void *shadow,
182 struct mem_cgroup *memcg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct address_space *mapping = page->mapping;
185
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -0700186 trace_mm_filemap_delete_from_page_cache(page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600187 /*
188 * if we're uptodate, flush out into the cleancache, otherwise
189 * invalidate any existing cleancache entries. We can't leave
190 * stale data around in the cleancache once our page is gone
191 */
192 if (PageUptodate(page) && PageMappedToDisk(page))
193 cleancache_put_page(page);
194 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400195 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600196
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700197 page_cache_tree_delete(mapping, page, shadow);
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700200 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700201
Michal Hocko4165b9b2015-06-24 16:57:24 -0700202 /* hugetlb pages do not participate in page cache accounting. */
203 if (!PageHuge(page))
204 __dec_zone_page_state(page, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700205 if (PageSwapBacked(page))
206 __dec_zone_page_state(page, NR_SHMEM);
Nick Piggin45426812007-07-15 23:38:12 -0700207 BUG_ON(page_mapped(page));
Linus Torvalds3a692792007-12-19 14:05:13 -0800208
209 /*
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700210 * At this point page must be either written or cleaned by truncate.
211 * Dirty page here signals a bug and loss of unwritten data.
Linus Torvalds3a692792007-12-19 14:05:13 -0800212 *
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700213 * This fixes dirty accounting after removing the page entirely but
214 * leaves PageDirty set: it has no effect for truncated page and
215 * anyway will be cleared before returning page into buddy allocator.
Linus Torvalds3a692792007-12-19 14:05:13 -0800216 */
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700217 if (WARN_ON_ONCE(PageDirty(page)))
Tejun Heo682aa8e2015-05-28 14:50:53 -0400218 account_page_cleaned(page, mapping, memcg,
219 inode_to_wb(mapping->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Minchan Kim702cfbf2011-03-22 16:32:43 -0700222/**
223 * delete_from_page_cache - delete page from page cache
224 * @page: the page which the kernel is trying to remove from page cache
225 *
226 * This must be called only on pages that have been verified to be in the page
227 * cache and locked. It will never put the page into the free list, the caller
228 * has a reference on the page.
229 */
230void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct address_space *mapping = page->mapping;
Greg Thelenc4843a72015-05-22 17:13:16 -0400233 struct mem_cgroup *memcg;
234 unsigned long flags;
235
Linus Torvalds6072d132010-12-01 13:35:19 -0500236 void (*freepage)(struct page *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Matt Mackallcd7619d2005-05-01 08:59:01 -0700238 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds6072d132010-12-01 13:35:19 -0500240 freepage = mapping->a_ops->freepage;
Greg Thelenc4843a72015-05-22 17:13:16 -0400241
242 memcg = mem_cgroup_begin_page_stat(page);
243 spin_lock_irqsave(&mapping->tree_lock, flags);
244 __delete_from_page_cache(page, NULL, memcg);
245 spin_unlock_irqrestore(&mapping->tree_lock, flags);
246 mem_cgroup_end_page_stat(memcg);
Linus Torvalds6072d132010-12-01 13:35:19 -0500247
248 if (freepage)
249 freepage(page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700250 page_cache_release(page);
251}
252EXPORT_SYMBOL(delete_from_page_cache);
253
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700254static int filemap_check_errors(struct address_space *mapping)
255{
256 int ret = 0;
257 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700258 if (test_bit(AS_ENOSPC, &mapping->flags) &&
259 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700260 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700261 if (test_bit(AS_EIO, &mapping->flags) &&
262 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700263 ret = -EIO;
264 return ret;
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700268 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700269 * @mapping: address space structure to write
270 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800271 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700272 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700274 * Start writeback against all of a mapping's dirty pages that lie
275 * within the byte offsets <start, end> inclusive.
276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700278 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * these two operations is that if a dirty page/buffer is encountered, it must
280 * be waited upon, and not just skipped over.
281 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800282int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
283 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int ret;
286 struct writeback_control wbc = {
287 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800288 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700289 .range_start = start,
290 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 };
292
293 if (!mapping_cap_writeback_dirty(mapping))
294 return 0;
295
Tejun Heob16b1de2015-06-02 08:39:48 -0600296 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600298 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return ret;
300}
301
302static inline int __filemap_fdatawrite(struct address_space *mapping,
303 int sync_mode)
304{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700305 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308int filemap_fdatawrite(struct address_space *mapping)
309{
310 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
311}
312EXPORT_SYMBOL(filemap_fdatawrite);
313
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400314int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800315 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
318}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400319EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Randy Dunlap485bb992006-06-23 02:03:49 -0700321/**
322 * filemap_flush - mostly a non-blocking flush
323 * @mapping: target address_space
324 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * This is a mostly non-blocking flush. Not suitable for data-integrity
326 * purposes - I/O may not be started against all dirty pages.
327 */
328int filemap_flush(struct address_space *mapping)
329{
330 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
331}
332EXPORT_SYMBOL(filemap_flush);
333
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800334static int __filemap_fdatawait_range(struct address_space *mapping,
335 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200337 pgoff_t index = start_byte >> PAGE_CACHE_SHIFT;
338 pgoff_t end = end_byte >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct pagevec pvec;
340 int nr_pages;
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800341 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200343 if (end_byte < start_byte)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 pagevec_init(&pvec, 0);
Jan Kara26778b82017-11-15 17:35:05 -0800347 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned i;
349
Jan Kara26778b82017-11-15 17:35:05 -0800350 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
351 end, PAGECACHE_TAG_WRITEBACK, PAGEVEC_SIZE);
352 if (!nr_pages)
353 break;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 for (i = 0; i < nr_pages; i++) {
356 struct page *page = pvec.pages[i];
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800359 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ret = -EIO;
361 }
362 pagevec_release(&pvec);
363 cond_resched();
364 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700365out:
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800366 return ret;
367}
368
369/**
370 * filemap_fdatawait_range - wait for writeback to complete
371 * @mapping: address space structure to wait for
372 * @start_byte: offset in bytes where the range starts
373 * @end_byte: offset in bytes where the range ends (inclusive)
374 *
375 * Walk the list of under-writeback pages of the given address space
376 * in the given range and wait for all of them. Check error status of
377 * the address space and return it.
378 *
379 * Since the error status of the address space is cleared by this function,
380 * callers are responsible for checking the return value and handling and/or
381 * reporting the error.
382 */
383int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
384 loff_t end_byte)
385{
386 int ret, ret2;
387
388 ret = __filemap_fdatawait_range(mapping, start_byte, end_byte);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700389 ret2 = filemap_check_errors(mapping);
390 if (!ret)
391 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return ret;
394}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200395EXPORT_SYMBOL(filemap_fdatawait_range);
396
397/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800398 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
399 * @mapping: address space structure to wait for
400 *
401 * Walk the list of under-writeback pages of the given address space
402 * and wait for all of them. Unlike filemap_fdatawait(), this function
403 * does not clear error status of the address space.
404 *
405 * Use this function if callers don't handle errors themselves. Expected
406 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
407 * fsfreeze(8)
408 */
409void filemap_fdatawait_keep_errors(struct address_space *mapping)
410{
411 loff_t i_size = i_size_read(mapping->host);
412
413 if (i_size == 0)
414 return;
415
416 __filemap_fdatawait_range(mapping, 0, i_size - 1);
417}
418
419/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700420 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700422 *
423 * Walk the list of under-writeback pages of the given address space
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800424 * and wait for all of them. Check error status of the address space
425 * and return it.
426 *
427 * Since the error status of the address space is cleared by this function,
428 * callers are responsible for checking the return value and handling and/or
429 * reporting the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
431int filemap_fdatawait(struct address_space *mapping)
432{
433 loff_t i_size = i_size_read(mapping->host);
434
435 if (i_size == 0)
436 return 0;
437
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200438 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440EXPORT_SYMBOL(filemap_fdatawait);
441
442int filemap_write_and_wait(struct address_space *mapping)
443{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800444 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800447 err = filemap_fdatawrite(mapping);
448 /*
449 * Even if the above returned error, the pages may be
450 * written partially (e.g. -ENOSPC), so we wait for it.
451 * But the -EIO is special case, it may indicate the worst
452 * thing (e.g. bug) happened, so we avoid waiting for it.
453 */
454 if (err != -EIO) {
455 int err2 = filemap_fdatawait(mapping);
456 if (!err)
457 err = err2;
458 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700459 } else {
460 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800464EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Randy Dunlap485bb992006-06-23 02:03:49 -0700466/**
467 * filemap_write_and_wait_range - write out & wait on a file range
468 * @mapping: the address_space for the pages
469 * @lstart: offset in bytes where the range starts
470 * @lend: offset in bytes where the range ends (inclusive)
471 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800472 * Write out and wait upon file offsets lstart->lend, inclusive.
473 *
474 * Note that `lend' is inclusive (describes the last byte to be written) so
475 * that this function can be used to write to the very end-of-file (end = -1).
476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477int filemap_write_and_wait_range(struct address_space *mapping,
478 loff_t lstart, loff_t lend)
479{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800480 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800483 err = __filemap_fdatawrite_range(mapping, lstart, lend,
484 WB_SYNC_ALL);
485 /* See comment of filemap_write_and_wait() */
486 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200487 int err2 = filemap_fdatawait_range(mapping,
488 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800489 if (!err)
490 err = err2;
491 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700492 } else {
493 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Chris Masonf6995582009-04-15 13:22:37 -0400497EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Randy Dunlap485bb992006-06-23 02:03:49 -0700499/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700500 * replace_page_cache_page - replace a pagecache page with a new one
501 * @old: page to be replaced
502 * @new: page to replace with
503 * @gfp_mask: allocation mode
504 *
505 * This function replaces a page in the pagecache with a new one. On
506 * success it acquires the pagecache reference for the new page and
507 * drops it for the old page. Both the old and new pages must be
508 * locked. This function does not add the new page to the LRU, the
509 * caller must do that.
510 *
511 * The remove + add is atomic. The only way this function can fail is
512 * memory allocation failure.
513 */
514int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
515{
516 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700517
Sasha Levin309381fea2014-01-23 15:52:54 -0800518 VM_BUG_ON_PAGE(!PageLocked(old), old);
519 VM_BUG_ON_PAGE(!PageLocked(new), new);
520 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700521
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700522 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
523 if (!error) {
524 struct address_space *mapping = old->mapping;
525 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400526 struct mem_cgroup *memcg;
527 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700528
529 pgoff_t offset = old->index;
530 freepage = mapping->a_ops->freepage;
531
532 page_cache_get(new);
533 new->mapping = mapping;
534 new->index = offset;
535
Greg Thelenc4843a72015-05-22 17:13:16 -0400536 memcg = mem_cgroup_begin_page_stat(old);
537 spin_lock_irqsave(&mapping->tree_lock, flags);
538 __delete_from_page_cache(old, NULL, memcg);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700539 error = radix_tree_insert(&mapping->page_tree, offset, new);
540 BUG_ON(error);
541 mapping->nrpages++;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700542
543 /*
544 * hugetlb pages do not participate in page cache accounting.
545 */
546 if (!PageHuge(new))
547 __inc_zone_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700548 if (PageSwapBacked(new))
549 __inc_zone_page_state(new, NR_SHMEM);
Greg Thelenc4843a72015-05-22 17:13:16 -0400550 spin_unlock_irqrestore(&mapping->tree_lock, flags);
551 mem_cgroup_end_page_stat(memcg);
Hugh Dickins45637ba2015-11-05 18:49:40 -0800552 mem_cgroup_replace_page(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700553 radix_tree_preload_end();
554 if (freepage)
555 freepage(old);
556 page_cache_release(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700557 }
558
559 return error;
560}
561EXPORT_SYMBOL_GPL(replace_page_cache_page);
562
Johannes Weiner0cd61442014-04-03 14:47:46 -0700563static int page_cache_tree_insert(struct address_space *mapping,
Johannes Weinera5289102014-04-03 14:47:51 -0700564 struct page *page, void **shadowp)
Johannes Weiner0cd61442014-04-03 14:47:46 -0700565{
Johannes Weiner449dd692014-04-03 14:47:56 -0700566 struct radix_tree_node *node;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700567 void **slot;
568 int error;
569
Johannes Weiner449dd692014-04-03 14:47:56 -0700570 error = __radix_tree_create(&mapping->page_tree, page->index,
571 &node, &slot);
572 if (error)
573 return error;
574 if (*slot) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700575 void *p;
576
577 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
578 if (!radix_tree_exceptional_entry(p))
579 return -EEXIST;
Johannes Weinera5289102014-04-03 14:47:51 -0700580 if (shadowp)
581 *shadowp = p;
Johannes Weiner449dd692014-04-03 14:47:56 -0700582 mapping->nrshadows--;
583 if (node)
584 workingset_node_shadows_dec(node);
Johannes Weiner0cd61442014-04-03 14:47:46 -0700585 }
Johannes Weiner449dd692014-04-03 14:47:56 -0700586 radix_tree_replace_slot(slot, page);
587 mapping->nrpages++;
588 if (node) {
589 workingset_node_pages_inc(node);
590 /*
591 * Don't track node that contains actual pages.
592 *
593 * Avoid acquiring the list_lru lock if already
594 * untracked. The list_empty() test is safe as
595 * node->private_list is protected by
596 * mapping->tree_lock.
597 */
598 if (!list_empty(&node->private_list))
599 list_lru_del(&workingset_shadow_nodes,
600 &node->private_list);
601 }
602 return 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700603}
604
Johannes Weinera5289102014-04-03 14:47:51 -0700605static int __add_to_page_cache_locked(struct page *page,
606 struct address_space *mapping,
607 pgoff_t offset, gfp_t gfp_mask,
608 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Johannes Weiner00501b52014-08-08 14:19:20 -0700610 int huge = PageHuge(page);
611 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700612 int error;
613
Sasha Levin309381fea2014-01-23 15:52:54 -0800614 VM_BUG_ON_PAGE(!PageLocked(page), page);
615 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700616
Johannes Weiner00501b52014-08-08 14:19:20 -0700617 if (!huge) {
618 error = mem_cgroup_try_charge(page, current->mm,
619 gfp_mask, &memcg);
620 if (error)
621 return error;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jan Kara5e4c0d972013-09-11 14:26:05 -0700624 error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700625 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700626 if (!huge)
627 mem_cgroup_cancel_charge(page, memcg);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700628 return error;
629 }
630
631 page_cache_get(page);
632 page->mapping = mapping;
633 page->index = offset;
634
635 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700636 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700637 radix_tree_preload_end();
638 if (unlikely(error))
639 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700640
641 /* hugetlb pages do not participate in page cache accounting. */
642 if (!huge)
643 __inc_zone_page_state(page, NR_FILE_PAGES);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700644 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700645 if (!huge)
646 mem_cgroup_commit_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700647 trace_mm_filemap_add_to_page_cache(page);
648 return 0;
649err_insert:
650 page->mapping = NULL;
651 /* Leave page->index set: truncation relies upon it */
652 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700653 if (!huge)
654 mem_cgroup_cancel_charge(page, memcg);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700655 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return error;
657}
Johannes Weinera5289102014-04-03 14:47:51 -0700658
659/**
660 * add_to_page_cache_locked - add a locked page to the pagecache
661 * @page: page to add
662 * @mapping: the page's address_space
663 * @offset: page index
664 * @gfp_mask: page allocation mode
665 *
666 * This function is used to add a page to the pagecache. It must be locked.
667 * This function does not add the page to the LRU. The caller must do that.
668 */
669int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
670 pgoff_t offset, gfp_t gfp_mask)
671{
672 return __add_to_page_cache_locked(page, mapping, offset,
673 gfp_mask, NULL);
674}
Nick Piggine2867812008-07-25 19:45:30 -0700675EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400678 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Johannes Weinera5289102014-04-03 14:47:51 -0700680 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700681 int ret;
682
Johannes Weinera5289102014-04-03 14:47:51 -0700683 __set_page_locked(page);
684 ret = __add_to_page_cache_locked(page, mapping, offset,
685 gfp_mask, &shadow);
686 if (unlikely(ret))
687 __clear_page_locked(page);
688 else {
689 /*
690 * The page might have been evicted from cache only
691 * recently, in which case it should be activated like
692 * any other repeatedly accessed page.
693 */
694 if (shadow && workingset_refault(shadow)) {
695 SetPageActive(page);
696 workingset_activation(page);
697 } else
698 ClearPageActive(page);
699 lru_cache_add(page);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return ret;
702}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300703EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Paul Jackson44110fe2006-03-24 03:16:04 -0800705#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700706struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800707{
Miao Xiec0ff7452010-05-24 14:32:08 -0700708 int n;
709 struct page *page;
710
Paul Jackson44110fe2006-03-24 03:16:04 -0800711 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700712 unsigned int cpuset_mems_cookie;
713 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700714 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700715 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700716 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700717 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700718
Miao Xiec0ff7452010-05-24 14:32:08 -0700719 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800720 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700721 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800722}
Nick Piggin2ae88142006-10-28 10:38:23 -0700723EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800724#endif
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*
727 * In order to wait for pages to become available there must be
728 * waitqueues associated with pages. By using a hash table of
729 * waitqueues where the bucket discipline is to maintain all
730 * waiters on the same queue and wake all when any of the pages
731 * become available, and for the woken contexts to check to be
732 * sure the appropriate page became available, this saves space
733 * at a cost of "thundering herd" phenomena during rare hash
734 * collisions.
735 */
NeilBrowna4796e32014-09-24 11:28:32 +1000736wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 const struct zone *zone = page_zone(page);
739
740 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
741}
NeilBrowna4796e32014-09-24 11:28:32 +1000742EXPORT_SYMBOL(page_waitqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Harvey Harrison920c7a52008-02-04 22:29:26 -0800744void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
747
748 if (test_bit(bit_nr, &page->flags))
NeilBrown74316202014-07-07 15:16:04 +1000749 __wait_on_bit(page_waitqueue(page), &wait, bit_wait_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 TASK_UNINTERRUPTIBLE);
751}
752EXPORT_SYMBOL(wait_on_page_bit);
753
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700754int wait_on_page_bit_killable(struct page *page, int bit_nr)
755{
756 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
757
758 if (!test_bit(bit_nr, &page->flags))
759 return 0;
760
761 return __wait_on_bit(page_waitqueue(page), &wait,
NeilBrown74316202014-07-07 15:16:04 +1000762 bit_wait_io, TASK_KILLABLE);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700763}
764
NeilBrowncbbce822014-09-25 13:55:19 +1000765int wait_on_page_bit_killable_timeout(struct page *page,
766 int bit_nr, unsigned long timeout)
767{
768 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
769
770 wait.key.timeout = jiffies + timeout;
771 if (!test_bit(bit_nr, &page->flags))
772 return 0;
773 return __wait_on_bit(page_waitqueue(page), &wait,
774 bit_wait_io_timeout, TASK_KILLABLE);
775}
776EXPORT_SYMBOL_GPL(wait_on_page_bit_killable_timeout);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100779 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700780 * @page: Page defining the wait queue of interest
781 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100782 *
783 * Add an arbitrary @waiter to the wait queue for the nominated @page.
784 */
785void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
786{
787 wait_queue_head_t *q = page_waitqueue(page);
788 unsigned long flags;
789
790 spin_lock_irqsave(&q->lock, flags);
791 __add_wait_queue(q, waiter);
792 spin_unlock_irqrestore(&q->lock, flags);
793}
794EXPORT_SYMBOL_GPL(add_page_wait_queue);
795
796/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700797 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * @page: the page
799 *
800 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
801 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +0900802 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
804 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700805 * The mb is necessary to enforce ordering between the clear_bit and the read
806 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800808void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Sasha Levin309381fea2014-01-23 15:52:54 -0800810 VM_BUG_ON_PAGE(!PageLocked(page), page);
Nick Piggin8413ac92008-10-18 20:26:59 -0700811 clear_bit_unlock(PG_locked, &page->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100812 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 wake_up_page(page, PG_locked);
814}
815EXPORT_SYMBOL(unlock_page);
816
Randy Dunlap485bb992006-06-23 02:03:49 -0700817/**
818 * end_page_writeback - end writeback against a page
819 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
821void end_page_writeback(struct page *page)
822{
Mel Gorman888cf2d2014-06-04 16:10:34 -0700823 /*
824 * TestClearPageReclaim could be used here but it is an atomic
825 * operation and overkill in this particular case. Failing to
826 * shuffle a page marked for immediate reclaim is too mild to
827 * justify taking an atomic operation penalty at the end of
828 * ever page writeback.
829 */
830 if (PageReclaim(page)) {
831 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700832 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -0700833 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700834
835 if (!test_clear_page_writeback(page))
836 BUG();
837
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100838 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 wake_up_page(page, PG_writeback);
840}
841EXPORT_SYMBOL(end_page_writeback);
842
Matthew Wilcox57d99842014-06-04 16:07:45 -0700843/*
844 * After completing I/O on a page, call this routine to update the page
845 * flags appropriately
846 */
847void page_endio(struct page *page, int rw, int err)
848{
849 if (rw == READ) {
850 if (!err) {
851 SetPageUptodate(page);
852 } else {
853 ClearPageUptodate(page);
854 SetPageError(page);
855 }
856 unlock_page(page);
857 } else { /* rw == WRITE */
858 if (err) {
859 SetPageError(page);
860 if (page->mapping)
861 mapping_set_error(page->mapping, err);
862 }
863 end_page_writeback(page);
864 }
865}
866EXPORT_SYMBOL_GPL(page_endio);
867
Randy Dunlap485bb992006-06-23 02:03:49 -0700868/**
869 * __lock_page - get a lock on the page, assuming we need to sleep to get it
870 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800872void __lock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
875
NeilBrown74316202014-07-07 15:16:04 +1000876 __wait_on_bit_lock(page_waitqueue(page), &wait, bit_wait_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 TASK_UNINTERRUPTIBLE);
878}
879EXPORT_SYMBOL(__lock_page);
880
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800881int __lock_page_killable(struct page *page)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500882{
883 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
884
885 return __wait_on_bit_lock(page_waitqueue(page), &wait,
NeilBrown74316202014-07-07 15:16:04 +1000886 bit_wait_io, TASK_KILLABLE);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500887}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300888EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500889
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700890/*
891 * Return values:
892 * 1 - page is locked; mmap_sem is still held.
893 * 0 - page is not locked.
894 * mmap_sem has been released (up_read()), unless flags had both
895 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
896 * which case mmap_sem is still held.
897 *
898 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
899 * with the page locked and the mmap_sem unperturbed.
900 */
Michel Lespinassed065bd82010-10-26 14:21:57 -0700901int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
902 unsigned int flags)
903{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700904 if (flags & FAULT_FLAG_ALLOW_RETRY) {
905 /*
906 * CAUTION! In this case, mmap_sem is not released
907 * even though return 0.
908 */
909 if (flags & FAULT_FLAG_RETRY_NOWAIT)
910 return 0;
911
912 up_read(&mm->mmap_sem);
913 if (flags & FAULT_FLAG_KILLABLE)
914 wait_on_page_locked_killable(page);
915 else
Gleb Natapov318b2752011-03-22 16:30:51 -0700916 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -0700917 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700918 } else {
919 if (flags & FAULT_FLAG_KILLABLE) {
920 int ret;
921
922 ret = __lock_page_killable(page);
923 if (ret) {
924 up_read(&mm->mmap_sem);
925 return 0;
926 }
927 } else
928 __lock_page(page);
929 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -0700930 }
931}
932
Randy Dunlap485bb992006-06-23 02:03:49 -0700933/**
Johannes Weinere7b563b2014-04-03 14:47:44 -0700934 * page_cache_next_hole - find the next hole (not-present entry)
935 * @mapping: mapping
936 * @index: index
937 * @max_scan: maximum range to search
938 *
939 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
940 * lowest indexed hole.
941 *
942 * Returns: the index of the hole if found, otherwise returns an index
943 * outside of the set specified (in which case 'return - index >=
944 * max_scan' will be true). In rare cases of index wrap-around, 0 will
945 * be returned.
946 *
947 * page_cache_next_hole may be called under rcu_read_lock. However,
948 * like radix_tree_gang_lookup, this will not atomically search a
949 * snapshot of the tree at a single point in time. For example, if a
950 * hole is created at index 5, then subsequently a hole is created at
951 * index 10, page_cache_next_hole covering both indexes may return 10
952 * if called under rcu_read_lock.
953 */
954pgoff_t page_cache_next_hole(struct address_space *mapping,
955 pgoff_t index, unsigned long max_scan)
956{
957 unsigned long i;
958
959 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700960 struct page *page;
961
962 page = radix_tree_lookup(&mapping->page_tree, index);
963 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -0700964 break;
965 index++;
966 if (index == 0)
967 break;
968 }
969
970 return index;
971}
972EXPORT_SYMBOL(page_cache_next_hole);
973
974/**
975 * page_cache_prev_hole - find the prev hole (not-present entry)
976 * @mapping: mapping
977 * @index: index
978 * @max_scan: maximum range to search
979 *
980 * Search backwards in the range [max(index-max_scan+1, 0), index] for
981 * the first hole.
982 *
983 * Returns: the index of the hole if found, otherwise returns an index
984 * outside of the set specified (in which case 'index - return >=
985 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
986 * will be returned.
987 *
988 * page_cache_prev_hole may be called under rcu_read_lock. However,
989 * like radix_tree_gang_lookup, this will not atomically search a
990 * snapshot of the tree at a single point in time. For example, if a
991 * hole is created at index 10, then subsequently a hole is created at
992 * index 5, page_cache_prev_hole covering both indexes may return 5 if
993 * called under rcu_read_lock.
994 */
995pgoff_t page_cache_prev_hole(struct address_space *mapping,
996 pgoff_t index, unsigned long max_scan)
997{
998 unsigned long i;
999
1000 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001001 struct page *page;
1002
1003 page = radix_tree_lookup(&mapping->page_tree, index);
1004 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001005 break;
1006 index--;
1007 if (index == ULONG_MAX)
1008 break;
1009 }
1010
1011 return index;
1012}
1013EXPORT_SYMBOL(page_cache_prev_hole);
1014
1015/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001016 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001017 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001018 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001019 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001020 * Looks up the page cache slot at @mapping & @offset. If there is a
1021 * page cache page, it is returned with an increased refcount.
1022 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001023 * If the slot holds a shadow entry of a previously evicted page, or a
1024 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001025 *
1026 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001028struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Nick Piggina60637c2008-07-25 19:45:31 -07001030 void **pagep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct page *page;
1032
Nick Piggina60637c2008-07-25 19:45:31 -07001033 rcu_read_lock();
1034repeat:
1035 page = NULL;
1036 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1037 if (pagep) {
1038 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001039 if (unlikely(!page))
1040 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001041 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001042 if (radix_tree_deref_retry(page))
1043 goto repeat;
1044 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001045 * A shadow entry of a recently evicted page,
1046 * or a swap entry from shmem/tmpfs. Return
1047 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001048 */
1049 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001050 }
Nick Piggina60637c2008-07-25 19:45:31 -07001051 if (!page_cache_get_speculative(page))
1052 goto repeat;
1053
1054 /*
1055 * Has the page moved?
1056 * This is part of the lockless pagecache protocol. See
1057 * include/linux/pagemap.h for details.
1058 */
1059 if (unlikely(page != *pagep)) {
1060 page_cache_release(page);
1061 goto repeat;
1062 }
1063 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001064out:
Nick Piggina60637c2008-07-25 19:45:31 -07001065 rcu_read_unlock();
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return page;
1068}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001069EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Randy Dunlap485bb992006-06-23 02:03:49 -07001071/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001072 * find_lock_entry - locate, pin and lock a page cache entry
1073 * @mapping: the address_space to search
1074 * @offset: the page cache index
1075 *
1076 * Looks up the page cache slot at @mapping & @offset. If there is a
1077 * page cache page, it is returned locked and with an increased
1078 * refcount.
1079 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001080 * If the slot holds a shadow entry of a previously evicted page, or a
1081 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001082 *
1083 * Otherwise, %NULL is returned.
1084 *
1085 * find_lock_entry() may sleep.
1086 */
1087struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct page *page;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001092 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001093 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001094 lock_page(page);
1095 /* Has the page been truncated? */
1096 if (unlikely(page->mapping != mapping)) {
1097 unlock_page(page);
1098 page_cache_release(page);
1099 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001101 VM_BUG_ON_PAGE(page->index != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return page;
1104}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001105EXPORT_SYMBOL(find_lock_entry);
1106
1107/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001108 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001109 * @mapping: the address_space to search
1110 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001111 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001112 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001113 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001114 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001115 *
Randy Dunlap75325182014-07-30 16:08:37 -07001116 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001117 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001118 * FGP_ACCESSED: the page will be marked accessed
1119 * FGP_LOCK: Page is return locked
1120 * FGP_CREAT: If page is not present then a new page is allocated using
Michal Hocko45f87de2014-12-29 20:30:35 +01001121 * @gfp_mask and added to the page cache and the VM's LRU
1122 * list. The page is returned locked and with an increased
1123 * refcount. Otherwise, %NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001124 *
1125 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1126 * if the GFP flags specified for FGP_CREAT are atomic.
1127 *
1128 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001129 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001130struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001131 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Nick Piggineb2be182007-10-16 01:24:57 -07001133 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001136 page = find_get_entry(mapping, offset);
1137 if (radix_tree_exceptional_entry(page))
1138 page = NULL;
1139 if (!page)
1140 goto no_page;
1141
1142 if (fgp_flags & FGP_LOCK) {
1143 if (fgp_flags & FGP_NOWAIT) {
1144 if (!trylock_page(page)) {
1145 page_cache_release(page);
1146 return NULL;
1147 }
1148 } else {
1149 lock_page(page);
1150 }
1151
1152 /* Has the page been truncated? */
1153 if (unlikely(page->mapping != mapping)) {
1154 unlock_page(page);
1155 page_cache_release(page);
1156 goto repeat;
1157 }
1158 VM_BUG_ON_PAGE(page->index != offset, page);
1159 }
1160
1161 if (page && (fgp_flags & FGP_ACCESSED))
1162 mark_page_accessed(page);
1163
1164no_page:
1165 if (!page && (fgp_flags & FGP_CREAT)) {
1166 int err;
1167 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001168 gfp_mask |= __GFP_WRITE;
1169 if (fgp_flags & FGP_NOFS)
1170 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001171
Michal Hocko45f87de2014-12-29 20:30:35 +01001172 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001173 if (!page)
1174 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001175
1176 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1177 fgp_flags |= FGP_LOCK;
1178
Hugh Dickinseb39d612014-08-06 16:06:43 -07001179 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001180 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001181 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001182
Michal Hocko45f87de2014-12-29 20:30:35 +01001183 err = add_to_page_cache_lru(page, mapping, offset,
1184 gfp_mask & GFP_RECLAIM_MASK);
Nick Piggineb2be182007-10-16 01:24:57 -07001185 if (unlikely(err)) {
1186 page_cache_release(page);
1187 page = NULL;
1188 if (err == -EEXIST)
1189 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return page;
1194}
Mel Gorman2457aec2014-06-04 16:10:31 -07001195EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001198 * find_get_entries - gang pagecache lookup
1199 * @mapping: The address_space to search
1200 * @start: The starting page cache index
1201 * @nr_entries: The maximum number of entries
1202 * @entries: Where the resulting entries are placed
1203 * @indices: The cache indices corresponding to the entries in @entries
1204 *
1205 * find_get_entries() will search for and return a group of up to
1206 * @nr_entries entries in the mapping. The entries are placed at
1207 * @entries. find_get_entries() takes a reference against any actual
1208 * pages it returns.
1209 *
1210 * The search returns a group of mapping-contiguous page cache entries
1211 * with ascending indexes. There may be holes in the indices due to
1212 * not-present pages.
1213 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001214 * Any shadow entries of evicted pages, or swap entries from
1215 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001216 *
1217 * find_get_entries() returns the number of pages and shadow entries
1218 * which were found.
1219 */
1220unsigned find_get_entries(struct address_space *mapping,
1221 pgoff_t start, unsigned int nr_entries,
1222 struct page **entries, pgoff_t *indices)
1223{
1224 void **slot;
1225 unsigned int ret = 0;
1226 struct radix_tree_iter iter;
1227
1228 if (!nr_entries)
1229 return 0;
1230
1231 rcu_read_lock();
1232restart:
1233 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1234 struct page *page;
1235repeat:
1236 page = radix_tree_deref_slot(slot);
1237 if (unlikely(!page))
1238 continue;
1239 if (radix_tree_exception(page)) {
1240 if (radix_tree_deref_retry(page))
1241 goto restart;
1242 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001243 * A shadow entry of a recently evicted page,
1244 * or a swap entry from shmem/tmpfs. Return
1245 * it without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001246 */
1247 goto export;
1248 }
1249 if (!page_cache_get_speculative(page))
1250 goto repeat;
1251
1252 /* Has the page moved? */
1253 if (unlikely(page != *slot)) {
1254 page_cache_release(page);
1255 goto repeat;
1256 }
1257export:
1258 indices[ret] = iter.index;
1259 entries[ret] = page;
1260 if (++ret == nr_entries)
1261 break;
1262 }
1263 rcu_read_unlock();
1264 return ret;
1265}
1266
1267/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * find_get_pages - gang pagecache lookup
1269 * @mapping: The address_space to search
1270 * @start: The starting page index
1271 * @nr_pages: The maximum number of pages
1272 * @pages: Where the resulting pages are placed
1273 *
1274 * find_get_pages() will search for and return a group of up to
1275 * @nr_pages pages in the mapping. The pages are placed at @pages.
1276 * find_get_pages() takes a reference against the returned pages.
1277 *
1278 * The search returns a group of mapping-contiguous pages with ascending
1279 * indexes. There may be holes in the indices due to not-present pages.
1280 *
1281 * find_get_pages() returns the number of pages which were found.
1282 */
1283unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1284 unsigned int nr_pages, struct page **pages)
1285{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001286 struct radix_tree_iter iter;
1287 void **slot;
1288 unsigned ret = 0;
1289
1290 if (unlikely(!nr_pages))
1291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Nick Piggina60637c2008-07-25 19:45:31 -07001293 rcu_read_lock();
1294restart:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001295 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Nick Piggina60637c2008-07-25 19:45:31 -07001296 struct page *page;
1297repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001298 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001299 if (unlikely(!page))
1300 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001301
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001302 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001303 if (radix_tree_deref_retry(page)) {
1304 /*
1305 * Transient condition which can only trigger
1306 * when entry at index 0 moves out of or back
1307 * to root: none yet gotten, safe to restart.
1308 */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001309 WARN_ON(iter.index);
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001310 goto restart;
1311 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001312 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001313 * A shadow entry of a recently evicted page,
1314 * or a swap entry from shmem/tmpfs. Skip
1315 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001316 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001317 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001318 }
Nick Piggina60637c2008-07-25 19:45:31 -07001319
1320 if (!page_cache_get_speculative(page))
1321 goto repeat;
1322
1323 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001324 if (unlikely(page != *slot)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001325 page_cache_release(page);
1326 goto repeat;
1327 }
1328
1329 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001330 if (++ret == nr_pages)
1331 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001332 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001333
Nick Piggina60637c2008-07-25 19:45:31 -07001334 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return ret;
1336}
1337
Jens Axboeebf43502006-04-27 08:46:01 +02001338/**
1339 * find_get_pages_contig - gang contiguous pagecache lookup
1340 * @mapping: The address_space to search
1341 * @index: The starting page index
1342 * @nr_pages: The maximum number of pages
1343 * @pages: Where the resulting pages are placed
1344 *
1345 * find_get_pages_contig() works exactly like find_get_pages(), except
1346 * that the returned number of pages are guaranteed to be contiguous.
1347 *
1348 * find_get_pages_contig() returns the number of pages which were found.
1349 */
1350unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1351 unsigned int nr_pages, struct page **pages)
1352{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001353 struct radix_tree_iter iter;
1354 void **slot;
1355 unsigned int ret = 0;
1356
1357 if (unlikely(!nr_pages))
1358 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001359
Nick Piggina60637c2008-07-25 19:45:31 -07001360 rcu_read_lock();
1361restart:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001362 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Nick Piggina60637c2008-07-25 19:45:31 -07001363 struct page *page;
1364repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001365 page = radix_tree_deref_slot(slot);
1366 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001367 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001368 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001369
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001370 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001371 if (radix_tree_deref_retry(page)) {
1372 /*
1373 * Transient condition which can only trigger
1374 * when entry at index 0 moves out of or back
1375 * to root: none yet gotten, safe to restart.
1376 */
1377 goto restart;
1378 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001379 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001380 * A shadow entry of a recently evicted page,
1381 * or a swap entry from shmem/tmpfs. Stop
1382 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001383 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001384 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001385 }
Nick Piggina60637c2008-07-25 19:45:31 -07001386
Nick Piggina60637c2008-07-25 19:45:31 -07001387 if (!page_cache_get_speculative(page))
1388 goto repeat;
1389
1390 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001391 if (unlikely(page != *slot)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001392 page_cache_release(page);
1393 goto repeat;
1394 }
1395
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001396 /*
1397 * must check mapping and index after taking the ref.
1398 * otherwise we can get both false positives and false
1399 * negatives, which is just confusing to the caller.
1400 */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001401 if (page->mapping == NULL || page->index != iter.index) {
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001402 page_cache_release(page);
1403 break;
1404 }
1405
Nick Piggina60637c2008-07-25 19:45:31 -07001406 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001407 if (++ret == nr_pages)
1408 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001409 }
Nick Piggina60637c2008-07-25 19:45:31 -07001410 rcu_read_unlock();
1411 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001412}
David Howellsef71c152007-05-09 02:33:44 -07001413EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001414
Randy Dunlap485bb992006-06-23 02:03:49 -07001415/**
Jan Karabf9510b2017-11-15 17:34:33 -08001416 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001417 * @mapping: the address_space to search
1418 * @index: the starting page index
Jan Karabf9510b2017-11-15 17:34:33 -08001419 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001420 * @tag: the tag index
1421 * @nr_pages: the maximum number of pages
1422 * @pages: where the resulting pages are placed
1423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001425 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Jan Karabf9510b2017-11-15 17:34:33 -08001427unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1428 pgoff_t end, int tag, unsigned int nr_pages,
1429 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001431 struct radix_tree_iter iter;
1432 void **slot;
1433 unsigned ret = 0;
1434
1435 if (unlikely(!nr_pages))
1436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Nick Piggina60637c2008-07-25 19:45:31 -07001438 rcu_read_lock();
1439restart:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001440 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1441 &iter, *index, tag) {
Nick Piggina60637c2008-07-25 19:45:31 -07001442 struct page *page;
Jan Karabf9510b2017-11-15 17:34:33 -08001443
1444 if (iter.index > end)
1445 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001446repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001447 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001448 if (unlikely(!page))
1449 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001450
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001451 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001452 if (radix_tree_deref_retry(page)) {
1453 /*
1454 * Transient condition which can only trigger
1455 * when entry at index 0 moves out of or back
1456 * to root: none yet gotten, safe to restart.
1457 */
1458 goto restart;
1459 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001460 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001461 * A shadow entry of a recently evicted page.
1462 *
1463 * Those entries should never be tagged, but
1464 * this tree walk is lockless and the tags are
1465 * looked up in bulk, one radix tree node at a
1466 * time, so there is a sizable window for page
1467 * reclaim to evict a page we saw tagged.
1468 *
1469 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001470 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001471 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001472 }
Nick Piggina60637c2008-07-25 19:45:31 -07001473
1474 if (!page_cache_get_speculative(page))
1475 goto repeat;
1476
1477 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001478 if (unlikely(page != *slot)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001479 page_cache_release(page);
1480 goto repeat;
1481 }
1482
1483 pages[ret] = page;
Jan Karabf9510b2017-11-15 17:34:33 -08001484 if (++ret == nr_pages) {
1485 *index = pages[ret - 1]->index + 1;
1486 goto out;
1487 }
Nick Piggina60637c2008-07-25 19:45:31 -07001488 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001489
Jan Karabf9510b2017-11-15 17:34:33 -08001490 /*
1491 * We come here when we got at @end. We take care to not overflow the
1492 * index @index as it confuses some of the callers. This breaks the
1493 * iteration when there is page at index -1 but that is already broken
1494 * anyway.
1495 */
1496 if (end == (pgoff_t)-1)
1497 *index = (pgoff_t)-1;
1498 else
1499 *index = end + 1;
1500out:
Nick Piggina60637c2008-07-25 19:45:31 -07001501 rcu_read_unlock();
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return ret;
1504}
Jan Karabf9510b2017-11-15 17:34:33 -08001505EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001507/*
1508 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1509 * a _large_ part of the i/o request. Imagine the worst scenario:
1510 *
1511 * ---R__________________________________________B__________
1512 * ^ reading here ^ bad block(assume 4k)
1513 *
1514 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1515 * => failing the whole request => read(R) => read(R+1) =>
1516 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1517 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1518 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1519 *
1520 * It is going insane. Fix it by quickly scaling down the readahead size.
1521 */
1522static void shrink_readahead_size_eio(struct file *filp,
1523 struct file_ra_state *ra)
1524{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001525 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001526}
1527
Randy Dunlap485bb992006-06-23 02:03:49 -07001528/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001529 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001530 * @filp: the file to read
1531 * @ppos: current file position
Al Viro6e58e792014-02-03 17:07:03 -05001532 * @iter: data destination
1533 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001534 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001536 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
1538 * This is really ugly. But the goto's actually try to clarify some
1539 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
Al Viro6e58e792014-02-03 17:07:03 -05001541static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
1542 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001544 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001546 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001547 pgoff_t index;
1548 pgoff_t last_index;
1549 pgoff_t prev_index;
1550 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001551 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001552 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 index = *ppos >> PAGE_CACHE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07001555 prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
1556 prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
Al Viro6e58e792014-02-03 17:07:03 -05001557 last_index = (*ppos + iter->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 offset = *ppos & ~PAGE_CACHE_MASK;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 for (;;) {
1561 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001562 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001563 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 unsigned long nr, ret;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567find_page:
1568 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001569 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001570 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001571 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001572 index, last_index - index);
1573 page = find_get_page(mapping, index);
1574 if (unlikely(page == NULL))
1575 goto no_cached_page;
1576 }
1577 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001578 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001579 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001580 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001582 if (!PageUptodate(page)) {
1583 if (inode->i_blkbits == PAGE_CACHE_SHIFT ||
1584 !mapping->a_ops->is_partially_uptodate)
1585 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001586 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001587 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001588 /* Did it get truncated before we got the lock? */
1589 if (!page->mapping)
1590 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001591 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05001592 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001593 goto page_not_up_to_date_locked;
1594 unlock_page(page);
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001597 /*
1598 * i_size must be checked after we know the page is Uptodate.
1599 *
1600 * Checking i_size after the check allows us to calculate
1601 * the correct value for "nr", which means the zero-filled
1602 * part of the page is not copied back to userspace (unless
1603 * another truncate extends the file - this is desired though).
1604 */
1605
1606 isize = i_size_read(inode);
1607 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1608 if (unlikely(!isize || index > end_index)) {
1609 page_cache_release(page);
1610 goto out;
1611 }
1612
1613 /* nr is the maximum number of bytes to copy from this page */
1614 nr = PAGE_CACHE_SIZE;
1615 if (index == end_index) {
1616 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1617 if (nr <= offset) {
1618 page_cache_release(page);
1619 goto out;
1620 }
1621 }
1622 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /* If users can be writing to this page using arbitrary
1625 * virtual addresses, take care about potential aliasing
1626 * before reading the page on the kernel side.
1627 */
1628 if (mapping_writably_mapped(mapping))
1629 flush_dcache_page(page);
1630
1631 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001632 * When a sequential read accesses a page several times,
1633 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
Jan Karaec0f1632007-05-06 14:49:25 -07001635 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 mark_page_accessed(page);
1637 prev_index = index;
1638
1639 /*
1640 * Ok, we have the page, and it's up-to-date, so
1641 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Al Viro6e58e792014-02-03 17:07:03 -05001643
1644 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 offset += ret;
1646 index += offset >> PAGE_CACHE_SHIFT;
1647 offset &= ~PAGE_CACHE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001648 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 page_cache_release(page);
Al Viro6e58e792014-02-03 17:07:03 -05001651 written += ret;
1652 if (!iov_iter_count(iter))
1653 goto out;
1654 if (ret < nr) {
1655 error = -EFAULT;
1656 goto out;
1657 }
1658 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660page_not_up_to_date:
1661 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001662 error = lock_page_killable(page);
1663 if (unlikely(error))
1664 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001666page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001667 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (!page->mapping) {
1669 unlock_page(page);
1670 page_cache_release(page);
1671 continue;
1672 }
1673
1674 /* Did somebody else fill it already? */
1675 if (PageUptodate(page)) {
1676 unlock_page(page);
1677 goto page_ok;
1678 }
1679
1680readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001681 /*
1682 * A previous I/O error may have been due to temporary
1683 * failures, eg. multipath errors.
1684 * PG_error will be set again if readpage fails.
1685 */
1686 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* Start the actual read. The read will unlock the page. */
1688 error = mapping->a_ops->readpage(filp, page);
1689
Zach Brown994fc28c2005-12-15 14:28:17 -08001690 if (unlikely(error)) {
1691 if (error == AOP_TRUNCATED_PAGE) {
1692 page_cache_release(page);
Al Viro6e58e792014-02-03 17:07:03 -05001693 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08001694 goto find_page;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04001700 error = lock_page_killable(page);
1701 if (unlikely(error))
1702 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (!PageUptodate(page)) {
1704 if (page->mapping == NULL) {
1705 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001706 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
1708 unlock_page(page);
1709 page_cache_release(page);
1710 goto find_page;
1711 }
1712 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07001713 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04001714 error = -EIO;
1715 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 unlock_page(page);
1718 }
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto page_ok;
1721
1722readpage_error:
1723 /* UHHUH! A synchronous read error occurred. Report it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 page_cache_release(page);
1725 goto out;
1726
1727no_cached_page:
1728 /*
1729 * Ok, it wasn't cached, so we need to create a new
1730 * page..
1731 */
Nick Piggineb2be182007-10-16 01:24:57 -07001732 page = page_cache_alloc_cold(mapping);
1733 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05001734 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07001735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Michal Hocko6afdb852015-06-24 16:58:06 -07001737 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08001738 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (error) {
Nick Piggineb2be182007-10-16 01:24:57 -07001740 page_cache_release(page);
Al Viro6e58e792014-02-03 17:07:03 -05001741 if (error == -EEXIST) {
1742 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05001744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto out;
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto readpage;
1748 }
1749
1750out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07001751 ra->prev_pos = prev_index;
1752 ra->prev_pos <<= PAGE_CACHE_SHIFT;
1753 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Fengguang Wuf4e6b492007-10-16 01:24:33 -07001755 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07001756 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05001757 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Randy Dunlap485bb992006-06-23 02:03:49 -07001760/**
Al Viro6abd2322014-04-04 14:20:57 -04001761 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001762 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04001763 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07001764 *
Al Viro6abd2322014-04-04 14:20:57 -04001765 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 * that can use the page cache directly.
1767 */
1768ssize_t
Al Viroed978a82014-03-05 22:53:04 -05001769generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Al Viroed978a82014-03-05 22:53:04 -05001771 struct file *file = iocb->ki_filp;
1772 ssize_t retval = 0;
Badari Pulavarty543ade12006-09-30 23:28:48 -07001773 loff_t *ppos = &iocb->ki_pos;
Al Viroed978a82014-03-05 22:53:04 -05001774 loff_t pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Al Viro2ba48ce2015-04-09 13:52:01 -04001776 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viroed978a82014-03-05 22:53:04 -05001777 struct address_space *mapping = file->f_mapping;
1778 struct inode *inode = mapping->host;
1779 size_t count = iov_iter_count(iter);
Badari Pulavarty543ade12006-09-30 23:28:48 -07001780 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (!count)
1783 goto out; /* skip atime */
1784 size = i_size_read(inode);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001785 retval = filemap_write_and_wait_range(mapping, pos,
Al Viroed978a82014-03-05 22:53:04 -05001786 pos + count - 1);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001787 if (!retval) {
Al Viroed978a82014-03-05 22:53:04 -05001788 struct iov_iter data = *iter;
Omar Sandoval22c61862015-03-16 04:33:53 -07001789 retval = mapping->a_ops->direct_IO(iocb, &data, pos);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001790 }
Al Viroed978a82014-03-05 22:53:04 -05001791
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001792 if (retval > 0) {
1793 *ppos = pos + retval;
Al Viroed978a82014-03-05 22:53:04 -05001794 iov_iter_advance(iter, retval);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001795 }
Josef Bacik66f998f2010-05-23 11:00:54 -04001796
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001797 /*
1798 * Btrfs can have a short DIO read if we encounter
1799 * compressed extents, so if there was an error, or if
1800 * we've already read everything we wanted to, or if
1801 * there was a short read because we hit EOF, go ahead
1802 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08001803 * the rest of the read. Buffered reads will not work for
1804 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001805 */
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08001806 if (retval < 0 || !iov_iter_count(iter) || *ppos >= size ||
1807 IS_DAX(inode)) {
Al Viroed978a82014-03-05 22:53:04 -05001808 file_accessed(file);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001809 goto out;
Steven Whitehouse0e0bcae2006-09-27 14:45:07 -04001810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Al Viroed978a82014-03-05 22:53:04 -05001813 retval = do_generic_file_read(file, ppos, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814out:
1815 return retval;
1816}
Al Viroed978a82014-03-05 22:53:04 -05001817EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001820/**
1821 * page_cache_read - adds requested page to the page cache if not already there
1822 * @file: file to read
1823 * @offset: page index
1824 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * This adds the requested page to the page cache if it isn't already there,
1826 * and schedules an I/O to read in its contents from disk.
1827 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001828static int page_cache_read(struct file *file, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07001831 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001832 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Zach Brown994fc28c2005-12-15 14:28:17 -08001834 do {
1835 page = page_cache_alloc_cold(mapping);
1836 if (!page)
1837 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Michal Hocko6afdb852015-06-24 16:58:06 -07001839 ret = add_to_page_cache_lru(page, mapping, offset,
Michal Hockoc62d2552015-11-06 16:28:49 -08001840 mapping_gfp_constraint(mapping, GFP_KERNEL));
Zach Brown994fc28c2005-12-15 14:28:17 -08001841 if (ret == 0)
1842 ret = mapping->a_ops->readpage(file, page);
1843 else if (ret == -EEXIST)
1844 ret = 0; /* losing race to add is OK */
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Zach Brown994fc28c2005-12-15 14:28:17 -08001848 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07001849
Zach Brown994fc28c2005-12-15 14:28:17 -08001850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853#define MMAP_LOTSAMISS (100)
1854
Linus Torvaldsef00e082009-06-16 15:31:25 -07001855/*
1856 * Synchronous readahead happens when we don't even find
1857 * a page in the page cache at all.
1858 */
1859static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1860 struct file_ra_state *ra,
1861 struct file *file,
1862 pgoff_t offset)
1863{
Linus Torvaldsef00e082009-06-16 15:31:25 -07001864 struct address_space *mapping = file->f_mapping;
1865
1866 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07001867 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001868 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001869 if (!ra->ra_pages)
1870 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001871
Joe Perches64363aa2013-07-08 16:00:18 -07001872 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07001873 page_cache_sync_readahead(mapping, ra, file, offset,
1874 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001875 return;
1876 }
1877
Andi Kleen207d04b2011-05-24 17:12:29 -07001878 /* Avoid banging the cache line if not needed */
1879 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001880 ra->mmap_miss++;
1881
1882 /*
1883 * Do we miss much more than hit in this file? If so,
1884 * stop bothering with read-ahead. It will only hurt.
1885 */
1886 if (ra->mmap_miss > MMAP_LOTSAMISS)
1887 return;
1888
Wu Fengguangd30a1102009-06-16 15:31:30 -07001889 /*
1890 * mmap read-around
1891 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08001892 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
1893 ra->size = ra->ra_pages;
1894 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001895 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001896}
1897
1898/*
1899 * Asynchronous readahead happens when we find the page and PG_readahead,
1900 * so we want to possibly extend the readahead further..
1901 */
1902static void do_async_mmap_readahead(struct vm_area_struct *vma,
1903 struct file_ra_state *ra,
1904 struct file *file,
1905 struct page *page,
1906 pgoff_t offset)
1907{
1908 struct address_space *mapping = file->f_mapping;
1909
1910 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07001911 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001912 return;
1913 if (ra->mmap_miss > 0)
1914 ra->mmap_miss--;
1915 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07001916 page_cache_async_readahead(mapping, ra, file,
1917 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001918}
1919
Randy Dunlap485bb992006-06-23 02:03:49 -07001920/**
Nick Piggin54cb8822007-07-19 01:46:59 -07001921 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07001922 * @vma: vma in which the fault was taken
1923 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07001924 *
Nick Piggin54cb8822007-07-19 01:46:59 -07001925 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 * mapped memory region to read in file data during a page fault.
1927 *
1928 * The goto's are kind of ugly, but this streamlines the normal case of having
1929 * it in the page cache, and handles the special cases reasonably without
1930 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001931 *
1932 * vma->vm_mm->mmap_sem must be held on entry.
1933 *
1934 * If our return value has VM_FAULT_RETRY set, it's because
1935 * lock_page_or_retry() returned 0.
1936 * The mmap_sem has usually been released in this case.
1937 * See __lock_page_or_retry() for the exception.
1938 *
1939 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
1940 * has not been released.
1941 *
1942 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001944int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07001947 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct address_space *mapping = file->f_mapping;
1949 struct file_ra_state *ra = &file->f_ra;
1950 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001951 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct page *page;
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07001953 loff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07001954 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07001956 size = round_up(i_size_read(inode), PAGE_CACHE_SIZE);
1957 if (offset >= size >> PAGE_CACHE_SHIFT)
Linus Torvalds5307cc12007-10-31 09:19:46 -07001958 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001961 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001963 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07001964 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07001966 * We found the page, so try async readahead before
1967 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001969 do_async_mmap_readahead(vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07001970 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07001971 /* No page in the page cache at all */
1972 do_sync_mmap_readahead(vma, ra, file, offset);
1973 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07001974 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001975 ret = VM_FAULT_MAJOR;
1976retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07001977 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (!page)
1979 goto no_cached_page;
1980 }
1981
Michel Lespinassed88c0922010-11-02 13:05:18 -07001982 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
1983 page_cache_release(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001984 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07001985 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07001986
1987 /* Did it get truncated? */
1988 if (unlikely(page->mapping != mapping)) {
1989 unlock_page(page);
1990 put_page(page);
1991 goto retry_find;
1992 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001993 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /*
Nick Piggind00806b2007-07-19 01:46:57 -07001996 * We have a locked page in the page cache, now we need to check
1997 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 */
Nick Piggind00806b2007-07-19 01:46:57 -07001999 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 goto page_not_uptodate;
2001
Linus Torvaldsef00e082009-06-16 15:31:25 -07002002 /*
2003 * Found the page and have a reference on it.
2004 * We must recheck i_size under page lock.
2005 */
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07002006 size = round_up(i_size_read(inode), PAGE_CACHE_SIZE);
2007 if (unlikely(offset >= size >> PAGE_CACHE_SHIFT)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002008 unlock_page(page);
Yan Zheng745ad482007-10-08 10:08:37 -07002009 page_cache_release(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002010 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002011 }
2012
Nick Piggind0217ac2007-07-19 01:47:03 -07002013 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002014 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016no_cached_page:
2017 /*
2018 * We're only likely to ever get here if MADV_RANDOM is in
2019 * effect.
2020 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002021 error = page_cache_read(file, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 /*
2024 * The page we want has now been added to the page cache.
2025 * In the unlikely event that someone removed it in the
2026 * meantime, we'll just come back here and read it again.
2027 */
2028 if (error >= 0)
2029 goto retry_find;
2030
2031 /*
2032 * An error return from page_cache_read can result if the
2033 * system is low on memory, or a problem occurs while trying
2034 * to schedule I/O.
2035 */
2036 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002037 return VM_FAULT_OOM;
2038 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * Umm, take care of errors if the page isn't up-to-date.
2043 * Try to re-read it _once_. We do this synchronously,
2044 * because there really aren't any performance issues here
2045 * and we need to check for errors.
2046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002048 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002049 if (!error) {
2050 wait_on_page_locked(page);
2051 if (!PageUptodate(page))
2052 error = -EIO;
2053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 page_cache_release(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002055
2056 if (!error || error == AOP_TRUNCATED_PAGE)
2057 goto retry_find;
2058
2059 /* Things didn't work out. Return zero to tell the mm layer so. */
2060 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002061 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002062}
2063EXPORT_SYMBOL(filemap_fault);
2064
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002065void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
2066{
2067 struct radix_tree_iter iter;
2068 void **slot;
2069 struct file *file = vma->vm_file;
2070 struct address_space *mapping = file->f_mapping;
2071 loff_t size;
2072 struct page *page;
2073 unsigned long address = (unsigned long) vmf->virtual_address;
2074 unsigned long addr;
2075 pte_t *pte;
2076
2077 rcu_read_lock();
2078 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, vmf->pgoff) {
2079 if (iter.index > vmf->max_pgoff)
2080 break;
2081repeat:
2082 page = radix_tree_deref_slot(slot);
2083 if (unlikely(!page))
2084 goto next;
2085 if (radix_tree_exception(page)) {
2086 if (radix_tree_deref_retry(page))
2087 break;
2088 else
2089 goto next;
2090 }
2091
2092 if (!page_cache_get_speculative(page))
2093 goto repeat;
2094
2095 /* Has the page moved? */
2096 if (unlikely(page != *slot)) {
2097 page_cache_release(page);
2098 goto repeat;
2099 }
2100
2101 if (!PageUptodate(page) ||
2102 PageReadahead(page) ||
2103 PageHWPoison(page))
2104 goto skip;
2105 if (!trylock_page(page))
2106 goto skip;
2107
2108 if (page->mapping != mapping || !PageUptodate(page))
2109 goto unlock;
2110
Kirill A. Shutemov99e3e532014-04-07 15:37:21 -07002111 size = round_up(i_size_read(mapping->host), PAGE_CACHE_SIZE);
2112 if (page->index >= size >> PAGE_CACHE_SHIFT)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002113 goto unlock;
2114
2115 pte = vmf->pte + page->index - vmf->pgoff;
2116 if (!pte_none(*pte))
2117 goto unlock;
2118
2119 if (file->f_ra.mmap_miss > 0)
2120 file->f_ra.mmap_miss--;
2121 addr = address + (page->index - vmf->pgoff) * PAGE_SIZE;
2122 do_set_pte(vma, addr, page, pte, false, false);
2123 unlock_page(page);
2124 goto next;
2125unlock:
2126 unlock_page(page);
2127skip:
2128 page_cache_release(page);
2129next:
2130 if (iter.index == vmf->max_pgoff)
2131 break;
2132 }
2133 rcu_read_unlock();
2134}
2135EXPORT_SYMBOL(filemap_map_pages);
2136
Jan Kara4fcf1c62012-06-12 16:20:29 +02002137int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2138{
2139 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002140 struct inode *inode = file_inode(vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002141 int ret = VM_FAULT_LOCKED;
2142
Jan Kara14da9202012-06-12 16:20:37 +02002143 sb_start_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002144 file_update_time(vma->vm_file);
2145 lock_page(page);
2146 if (page->mapping != inode->i_mapping) {
2147 unlock_page(page);
2148 ret = VM_FAULT_NOPAGE;
2149 goto out;
2150 }
Jan Kara14da9202012-06-12 16:20:37 +02002151 /*
2152 * We mark the page dirty already here so that when freeze is in
2153 * progress, we are guaranteed that writeback during freezing will
2154 * see the dirty page and writeprotect it again.
2155 */
2156 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002157 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002158out:
Jan Kara14da9202012-06-12 16:20:37 +02002159 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002160 return ret;
2161}
2162EXPORT_SYMBOL(filemap_page_mkwrite);
2163
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002164const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002165 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002166 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002167 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
2170/* This is used for a general mmap of a disk file */
2171
2172int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2173{
2174 struct address_space *mapping = file->f_mapping;
2175
2176 if (!mapping->a_ops->readpage)
2177 return -ENOEXEC;
2178 file_accessed(file);
2179 vma->vm_ops = &generic_file_vm_ops;
2180 return 0;
2181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183/*
2184 * This is for filesystems which do not implement ->writepage.
2185 */
2186int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2187{
2188 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2189 return -EINVAL;
2190 return generic_file_mmap(file, vma);
2191}
2192#else
2193int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2194{
2195 return -ENOSYS;
2196}
2197int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2198{
2199 return -ENOSYS;
2200}
2201#endif /* CONFIG_MMU */
2202
2203EXPORT_SYMBOL(generic_file_mmap);
2204EXPORT_SYMBOL(generic_file_readonly_mmap);
2205
Sasha Levin67f9fd92014-04-03 14:48:18 -07002206static struct page *wait_on_page_read(struct page *page)
2207{
2208 if (!IS_ERR(page)) {
2209 wait_on_page_locked(page);
2210 if (!PageUptodate(page)) {
2211 page_cache_release(page);
2212 page = ERR_PTR(-EIO);
2213 }
2214 }
2215 return page;
2216}
2217
Nick Piggin6fe69002007-05-06 14:49:04 -07002218static struct page *__read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002219 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002220 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002221 void *data,
2222 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Nick Piggineb2be182007-10-16 01:24:57 -07002224 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 int err;
2226repeat:
2227 page = find_get_page(mapping, index);
2228 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002229 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002230 if (!page)
2231 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002232 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002233 if (unlikely(err)) {
2234 page_cache_release(page);
2235 if (err == -EEXIST)
2236 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return ERR_PTR(err);
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 err = filler(data, page);
2241 if (err < 0) {
2242 page_cache_release(page);
2243 page = ERR_PTR(err);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002244 } else {
2245 page = wait_on_page_read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return page;
2249}
2250
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002251static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002252 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002253 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002254 void *data,
2255 gfp_t gfp)
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 struct page *page;
2259 int err;
2260
2261retry:
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002262 page = __read_cache_page(mapping, index, filler, data, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (IS_ERR(page))
David Howellsc855ff32007-05-09 13:42:20 +01002264 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (PageUptodate(page))
2266 goto out;
2267
2268 lock_page(page);
2269 if (!page->mapping) {
2270 unlock_page(page);
2271 page_cache_release(page);
2272 goto retry;
2273 }
2274 if (PageUptodate(page)) {
2275 unlock_page(page);
2276 goto out;
2277 }
2278 err = filler(data, page);
2279 if (err < 0) {
2280 page_cache_release(page);
David Howellsc855ff32007-05-09 13:42:20 +01002281 return ERR_PTR(err);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002282 } else {
2283 page = wait_on_page_read(page);
2284 if (IS_ERR(page))
2285 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
David Howellsc855ff32007-05-09 13:42:20 +01002287out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002288 mark_page_accessed(page);
2289 return page;
2290}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002291
2292/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002293 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002294 * @mapping: the page's address_space
2295 * @index: the page index
2296 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002297 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002298 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002299 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002300 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002301 *
2302 * If the page does not get brought uptodate, return -EIO.
2303 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002304struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002305 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002306 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002307 void *data)
2308{
2309 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2310}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002311EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002312
2313/**
2314 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2315 * @mapping: the page's address_space
2316 * @index: the page index
2317 * @gfp: the page allocator flags to use if allocating
2318 *
2319 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002320 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002321 *
2322 * If the page does not get brought uptodate, return -EIO.
2323 */
2324struct page *read_cache_page_gfp(struct address_space *mapping,
2325 pgoff_t index,
2326 gfp_t gfp)
2327{
2328 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2329
Sasha Levin67f9fd92014-04-03 14:48:18 -07002330 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002331}
2332EXPORT_SYMBOL(read_cache_page_gfp);
2333
Nick Piggin2f718ff2007-10-16 01:24:59 -07002334/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 * Performs necessary checks before doing a write
2336 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002337 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 * Returns appropriate error code that caller should return or
2339 * zero in case that write should be allowed.
2340 */
Al Viro3309dd02015-04-09 12:55:47 -04002341inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Al Viro3309dd02015-04-09 12:55:47 -04002343 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002345 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002346 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Al Viro3309dd02015-04-09 12:55:47 -04002348 if (!iov_iter_count(from))
2349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Al Viro0fa6b002015-04-04 04:05:48 -04002351 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002352 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002353 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Al Viro3309dd02015-04-09 12:55:47 -04002355 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Al Viro0fa6b002015-04-04 04:05:48 -04002357 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002358 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002359 send_sig(SIGXFSZ, current, 0);
2360 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
Al Viro3309dd02015-04-09 12:55:47 -04002362 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364
2365 /*
2366 * LFS rule
2367 */
Al Viro3309dd02015-04-09 12:55:47 -04002368 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002370 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002372 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
2375 /*
2376 * Are we about to exceed the fs block limit ?
2377 *
2378 * If we have written data it becomes a short write. If we have
2379 * exceeded without writing data we send a signal and return EFBIG.
2380 * Linus frestrict idea will clean these up nicely..
2381 */
Al Viro3309dd02015-04-09 12:55:47 -04002382 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2383 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Al Viro3309dd02015-04-09 12:55:47 -04002385 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2386 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388EXPORT_SYMBOL(generic_write_checks);
2389
Nick Pigginafddba42007-10-16 01:25:01 -07002390int pagecache_write_begin(struct file *file, struct address_space *mapping,
2391 loff_t pos, unsigned len, unsigned flags,
2392 struct page **pagep, void **fsdata)
2393{
2394 const struct address_space_operations *aops = mapping->a_ops;
2395
Nick Piggin4e02ed42008-10-29 14:00:55 -07002396 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002397 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002398}
2399EXPORT_SYMBOL(pagecache_write_begin);
2400
2401int pagecache_write_end(struct file *file, struct address_space *mapping,
2402 loff_t pos, unsigned len, unsigned copied,
2403 struct page *page, void *fsdata)
2404{
2405 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002406
Nick Piggin4e02ed42008-10-29 14:00:55 -07002407 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002408}
2409EXPORT_SYMBOL(pagecache_write_end);
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411ssize_t
Al Viro0c949332014-03-22 06:51:37 -04002412generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 struct file *file = iocb->ki_filp;
2415 struct address_space *mapping = file->f_mapping;
2416 struct inode *inode = mapping->host;
2417 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002418 size_t write_len;
2419 pgoff_t end;
Al Viro26978b82014-03-10 14:08:45 -04002420 struct iov_iter data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Al Viro0c949332014-03-22 06:51:37 -04002422 write_len = iov_iter_count(from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002423 end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002424
Nick Piggin48b47c52009-01-06 14:40:22 -08002425 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002426 if (written)
2427 goto out;
2428
2429 /*
2430 * After a write we want buffered reads to be sure to go to disk to get
2431 * the new data. We invalidate clean cached page from the region we're
2432 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002433 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002434 */
2435 if (mapping->nrpages) {
2436 written = invalidate_inode_pages2_range(mapping,
2437 pos >> PAGE_CACHE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002438 /*
2439 * If a page can not be invalidated, return 0 to fall back
2440 * to buffered write.
2441 */
2442 if (written) {
2443 if (written == -EBUSY)
2444 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002445 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002446 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002447 }
2448
Al Viro26978b82014-03-10 14:08:45 -04002449 data = *from;
Omar Sandoval22c61862015-03-16 04:33:53 -07002450 written = mapping->a_ops->direct_IO(iocb, &data, pos);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002451
2452 /*
2453 * Finally, try again to invalidate clean pages which might have been
2454 * cached by non-direct readahead, or faulted in by get_user_pages()
2455 * if the source of the write was an mmap'ed region of the file
2456 * we're writing. Either one is a pretty crazy thing to do,
2457 * so we don't support it 100%. If this invalidation
2458 * fails, tough, the write still worked...
2459 */
2460 if (mapping->nrpages) {
2461 invalidate_inode_pages2_range(mapping,
2462 pos >> PAGE_CACHE_SHIFT, end);
2463 }
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002466 pos += written;
Al Virof8579f82014-03-03 22:03:20 -05002467 iov_iter_advance(from, written);
Namhyung Kim01166512010-10-26 14:21:58 -07002468 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2469 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 mark_inode_dirty(inode);
2471 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002472 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002474out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 return written;
2476}
2477EXPORT_SYMBOL(generic_file_direct_write);
2478
Nick Piggineb2be182007-10-16 01:24:57 -07002479/*
2480 * Find or create a page at the given pagecache position. Return the locked
2481 * page. This function is specifically for buffered writes.
2482 */
Nick Piggin54566b22009-01-04 12:00:53 -08002483struct page *grab_cache_page_write_begin(struct address_space *mapping,
2484 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002485{
Nick Piggineb2be182007-10-16 01:24:57 -07002486 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07002487 int fgp_flags = FGP_LOCK|FGP_ACCESSED|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002488
Nick Piggin54566b22009-01-04 12:00:53 -08002489 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002490 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002491
Mel Gorman2457aec2014-06-04 16:10:31 -07002492 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002493 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002494 if (page)
2495 wait_for_stable_page(page);
2496
Nick Piggineb2be182007-10-16 01:24:57 -07002497 return page;
2498}
Nick Piggin54566b22009-01-04 12:00:53 -08002499EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002500
Al Viro3b93f912014-02-11 21:34:08 -05002501ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07002502 struct iov_iter *i, loff_t pos)
2503{
2504 struct address_space *mapping = file->f_mapping;
2505 const struct address_space_operations *a_ops = mapping->a_ops;
2506 long status = 0;
2507 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002508 unsigned int flags = 0;
2509
2510 /*
2511 * Copies from kernel address space cannot fail (NFSD is a big user).
2512 */
Al Viro777eda22014-12-17 04:46:46 -05002513 if (!iter_is_iovec(i))
Nick Piggin674b8922007-10-16 01:25:03 -07002514 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002515
2516 do {
2517 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002518 unsigned long offset; /* Offset into pagecache page */
2519 unsigned long bytes; /* Bytes to write to page */
2520 size_t copied; /* Bytes copied from user */
2521 void *fsdata;
2522
2523 offset = (pos & (PAGE_CACHE_SIZE - 1));
Nick Pigginafddba42007-10-16 01:25:01 -07002524 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2525 iov_iter_count(i));
2526
2527again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01002528 /*
2529 * Bring in the user page that we will copy from _first_.
2530 * Otherwise there's a nasty deadlock on copying from the
2531 * same page as we're writing to, without it being marked
2532 * up-to-date.
2533 *
2534 * Not only is this an optimisation, but it is also required
2535 * to check that the address is actually valid, when atomic
2536 * usercopies are used, below.
2537 */
2538 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2539 status = -EFAULT;
2540 break;
2541 }
2542
Jan Kara296291c2015-10-22 13:32:21 -07002543 if (fatal_signal_pending(current)) {
2544 status = -EINTR;
2545 break;
2546 }
2547
Nick Piggin674b8922007-10-16 01:25:03 -07002548 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002549 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07002550 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07002551 break;
2552
anfei zhou931e80e2010-02-02 13:44:02 -08002553 if (mapping_writably_mapped(mapping))
2554 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01002555
Nick Pigginafddba42007-10-16 01:25:01 -07002556 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07002557 flush_dcache_page(page);
2558
2559 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2560 page, fsdata);
2561 if (unlikely(status < 0))
2562 break;
2563 copied = status;
2564
2565 cond_resched();
2566
Nick Piggin124d3b72008-02-02 15:01:17 +01002567 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002568 if (unlikely(copied == 0)) {
2569 /*
2570 * If we were unable to copy any data at all, we must
2571 * fall back to a single segment length write.
2572 *
2573 * If we didn't fallback here, we could livelock
2574 * because not all segments in the iov can be copied at
2575 * once without a pagefault.
2576 */
2577 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2578 iov_iter_single_seg_count(i));
2579 goto again;
2580 }
Nick Pigginafddba42007-10-16 01:25:01 -07002581 pos += copied;
2582 written += copied;
2583
2584 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07002585 } while (iov_iter_count(i));
2586
2587 return written ? written : status;
2588}
Al Viro3b93f912014-02-11 21:34:08 -05002589EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Jan Karae4dd9de2009-08-17 18:10:06 +02002591/**
Al Viro81742022014-04-03 03:17:43 -04002592 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002593 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04002594 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002595 *
2596 * This function does all the work needed for actually writing data to a
2597 * file. It does all basic checks, removes SUID from the file, updates
2598 * modification times and calls proper subroutines depending on whether we
2599 * do direct IO or a standard buffered write.
2600 *
2601 * It expects i_mutex to be grabbed unless we work on a block device or similar
2602 * object which does not need locking at all.
2603 *
2604 * This function does *not* take care of syncing data in case of O_SYNC write.
2605 * A caller has to handle it. This is mainly due to the fact that we want to
2606 * avoid syncing under i_mutex.
2607 */
Al Viro81742022014-04-03 03:17:43 -04002608ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
2610 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002611 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05002613 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05002615 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01002618 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02002619 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (err)
2621 goto out;
2622
Josef Bacikc3b2da32012-03-26 09:59:21 -04002623 err = file_update_time(file);
2624 if (err)
2625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Al Viro2ba48ce2015-04-09 13:52:01 -04002627 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04002628 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002629
Al Viro0b8def92015-04-07 10:22:53 -04002630 written = generic_file_direct_write(iocb, from, iocb->ki_pos);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002631 /*
2632 * If the write stopped short of completing, fall back to
2633 * buffered writes. Some filesystems do this for writes to
2634 * holes, for example. For DAX files, a buffered write will
2635 * not succeed (even if it did, DAX does not handle dirty
2636 * page-cache pages correctly).
2637 */
Al Viro0b8def92015-04-07 10:22:53 -04002638 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05002640
Al Viro0b8def92015-04-07 10:22:53 -04002641 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002642 /*
Al Viro3b93f912014-02-11 21:34:08 -05002643 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002644 * then we want to return the number of bytes which were
2645 * direct-written, or the error code if that was zero. Note
2646 * that this differs from normal direct-io semantics, which
2647 * will return -EFOO even if some bytes were written.
2648 */
Al Viro60bb4522014-08-08 12:39:16 -04002649 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05002650 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002651 goto out;
2652 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002653 /*
2654 * We need to ensure that the page cache pages are written to
2655 * disk and invalidated to preserve the expected O_DIRECT
2656 * semantics.
2657 */
Al Viro3b93f912014-02-11 21:34:08 -05002658 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04002659 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002660 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04002661 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05002662 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002663 invalidate_mapping_pages(mapping,
2664 pos >> PAGE_CACHE_SHIFT,
2665 endbyte >> PAGE_CACHE_SHIFT);
2666 } else {
2667 /*
2668 * We don't know how much we wrote, so just return
2669 * the number of bytes which were direct-written
2670 */
2671 }
2672 } else {
Al Viro0b8def92015-04-07 10:22:53 -04002673 written = generic_perform_write(file, from, iocb->ki_pos);
2674 if (likely(written > 0))
2675 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677out:
2678 current->backing_dev_info = NULL;
2679 return written ? written : err;
2680}
Al Viro81742022014-04-03 03:17:43 -04002681EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Jan Karae4dd9de2009-08-17 18:10:06 +02002683/**
Al Viro81742022014-04-03 03:17:43 -04002684 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02002685 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04002686 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02002687 *
Al Viro81742022014-04-03 03:17:43 -04002688 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02002689 * filesystems. It takes care of syncing the file in case of O_SYNC file
2690 * and acquires i_mutex as needed.
2691 */
Al Viro81742022014-04-03 03:17:43 -04002692ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02002695 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002698 mutex_lock(&inode->i_mutex);
Al Viro3309dd02015-04-09 12:55:47 -04002699 ret = generic_write_checks(iocb, from);
2700 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04002701 ret = __generic_file_write_iter(iocb, from);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002702 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Christoph Hellwig02afc27f2013-09-04 15:04:40 +02002704 if (ret > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 ssize_t err;
2706
Al Virod311d792014-02-09 15:18:09 -05002707 err = generic_write_sync(file, iocb->ki_pos - ret, ret);
2708 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 ret = err;
2710 }
2711 return ret;
2712}
Al Viro81742022014-04-03 03:17:43 -04002713EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
David Howellscf9a2ae2006-08-29 19:05:54 +01002715/**
2716 * try_to_release_page() - release old fs-specific metadata on a page
2717 *
2718 * @page: the page which the kernel is trying to free
2719 * @gfp_mask: memory allocation flags (and I/O mode)
2720 *
2721 * The address_space is to try to release any data against the page
2722 * (presumably at page->private). If the release was successful, return `1'.
2723 * Otherwise return zero.
2724 *
David Howells266cf652009-04-03 16:42:36 +01002725 * This may also be called if PG_fscache is set on a page, indicating that the
2726 * page is known to the local caching routines.
2727 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002728 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08002729 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01002730 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002731 */
2732int try_to_release_page(struct page *page, gfp_t gfp_mask)
2733{
2734 struct address_space * const mapping = page->mapping;
2735
2736 BUG_ON(!PageLocked(page));
2737 if (PageWriteback(page))
2738 return 0;
2739
2740 if (mapping && mapping->a_ops->releasepage)
2741 return mapping->a_ops->releasepage(page, gfp_mask);
2742 return try_to_free_buffers(page);
2743}
2744
2745EXPORT_SYMBOL(try_to_release_page);