blob: d6df3bacb0fbe38bbacb6a81b3399391e4455ba3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/aio.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/mman.h>
23#include <linux/pagemap.h>
24#include <linux/file.h>
25#include <linux/uio.h>
26#include <linux/hash.h>
27#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070028#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pagevec.h>
30#include <linux/blkdev.h>
31#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080032#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070033#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080034#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060035#include <linux/cleancache.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080036#include "internal.h"
37
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070038#define CREATE_TRACE_POINTS
39#include <trace/events/filemap.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * FIXME: remove all knowledge of the buffer layer from the core VM
43 */
Jan Kara148f9482009-08-17 19:52:36 +020044#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/mman.h>
47
48/*
49 * Shared mappings implemented 30.11.1994. It's not fully working yet,
50 * though.
51 *
52 * Shared mappings now work. 15.8.1995 Bruno.
53 *
54 * finished 'unifying' the page and buffer cache and SMP-threaded the
55 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
56 *
57 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
58 */
59
60/*
61 * Lock ordering:
62 *
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070063 * ->i_mmap_mutex (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070065 * ->swap_lock (exclusive_swap_page, others)
66 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080068 * ->i_mutex
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070069 * ->i_mmap_mutex (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * ->mmap_sem
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070072 * ->i_mmap_mutex
Hugh Dickinsb8072f02005-10-29 18:16:41 -070073 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
75 *
76 * ->mmap_sem
77 * ->lock_page (access_process_vm)
78 *
Nick Piggin82591e62006-10-19 23:29:10 -070079 * ->i_mutex (generic_file_buffered_write)
80 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060082 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110083 * sb_lock (fs/fs-writeback.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * ->mapping->tree_lock (__sync_single_inode)
85 *
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070086 * ->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * ->anon_vma.lock (vma_adjust)
88 *
89 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070090 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070092 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070093 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ->private_lock (try_to_unmap_one)
95 * ->tree_lock (try_to_unmap_one)
96 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -080097 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * ->private_lock (page_remove_rmap->set_page_dirty)
99 * ->tree_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600100 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100101 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600102 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100103 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
105 *
Andi Kleen9a3c5312012-03-21 16:34:09 -0700106 * ->i_mmap_mutex
107 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
109
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700110static void page_cache_tree_delete(struct address_space *mapping,
111 struct page *page, void *shadow)
112{
Johannes Weiner449dd692014-04-03 14:47:56 -0700113 struct radix_tree_node *node;
114 unsigned long index;
115 unsigned int offset;
116 unsigned int tag;
117 void **slot;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700118
Johannes Weiner449dd692014-04-03 14:47:56 -0700119 VM_BUG_ON(!PageLocked(page));
120
121 __radix_tree_lookup(&mapping->page_tree, page->index, &node, &slot);
122
123 if (shadow) {
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700124 mapping->nrshadows++;
125 /*
126 * Make sure the nrshadows update is committed before
127 * the nrpages update so that final truncate racing
128 * with reclaim does not see both counters 0 at the
129 * same time and miss a shadow entry.
130 */
131 smp_wmb();
Johannes Weiner449dd692014-04-03 14:47:56 -0700132 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700133 mapping->nrpages--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700134
135 if (!node) {
136 /* Clear direct pointer tags in root node */
137 mapping->page_tree.gfp_mask &= __GFP_BITS_MASK;
138 radix_tree_replace_slot(slot, shadow);
139 return;
140 }
141
142 /* Clear tree tags for the removed page */
143 index = page->index;
144 offset = index & RADIX_TREE_MAP_MASK;
145 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
146 if (test_bit(offset, node->tags[tag]))
147 radix_tree_tag_clear(&mapping->page_tree, index, tag);
148 }
149
150 /* Delete page, swap shadow entry */
151 radix_tree_replace_slot(slot, shadow);
152 workingset_node_pages_dec(node);
153 if (shadow)
154 workingset_node_shadows_inc(node);
155 else
156 if (__radix_tree_delete_node(&mapping->page_tree, node))
157 return;
158
159 /*
160 * Track node that only contains shadow entries.
161 *
162 * Avoid acquiring the list_lru lock if already tracked. The
163 * list_empty() test is safe as node->private_list is
164 * protected by mapping->tree_lock.
165 */
166 if (!workingset_node_pages(node) &&
167 list_empty(&node->private_list)) {
168 node->private_data = mapping;
169 list_lru_add(&workingset_shadow_nodes, &node->private_list);
170 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
Minchan Kime64a7822011-03-22 16:32:44 -0700174 * Delete a page from the page cache and free it. Caller has to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * sure the page is locked and that nobody else uses it - or that usage
Nick Piggin19fd6232008-07-25 19:45:32 -0700176 * is safe. The caller must hold the mapping's tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700178void __delete_from_page_cache(struct page *page, void *shadow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct address_space *mapping = page->mapping;
181
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -0700182 trace_mm_filemap_delete_from_page_cache(page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600183 /*
184 * if we're uptodate, flush out into the cleancache, otherwise
185 * invalidate any existing cleancache entries. We can't leave
186 * stale data around in the cleancache once our page is gone
187 */
188 if (PageUptodate(page) && PageMappedToDisk(page))
189 cleancache_put_page(page);
190 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400191 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600192
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700193 page_cache_tree_delete(mapping, page, shadow);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700196 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700197
Christoph Lameter347ce432006-06-30 01:55:35 -0700198 __dec_zone_page_state(page, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700199 if (PageSwapBacked(page))
200 __dec_zone_page_state(page, NR_SHMEM);
Nick Piggin45426812007-07-15 23:38:12 -0700201 BUG_ON(page_mapped(page));
Linus Torvalds3a692792007-12-19 14:05:13 -0800202
203 /*
204 * Some filesystems seem to re-dirty the page even after
205 * the VM has canceled the dirty bit (eg ext3 journaling).
206 *
207 * Fix it up by doing a final dirty accounting check after
208 * having removed the page entirely.
209 */
210 if (PageDirty(page) && mapping_cap_account_dirty(mapping)) {
211 dec_zone_page_state(page, NR_FILE_DIRTY);
212 dec_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Minchan Kim702cfbf2011-03-22 16:32:43 -0700216/**
217 * delete_from_page_cache - delete page from page cache
218 * @page: the page which the kernel is trying to remove from page cache
219 *
220 * This must be called only on pages that have been verified to be in the page
221 * cache and locked. It will never put the page into the free list, the caller
222 * has a reference on the page.
223 */
224void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct address_space *mapping = page->mapping;
Linus Torvalds6072d132010-12-01 13:35:19 -0500227 void (*freepage)(struct page *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Matt Mackallcd7619d2005-05-01 08:59:01 -0700229 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds6072d132010-12-01 13:35:19 -0500231 freepage = mapping->a_ops->freepage;
Nick Piggin19fd6232008-07-25 19:45:32 -0700232 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700233 __delete_from_page_cache(page, NULL);
Nick Piggin19fd6232008-07-25 19:45:32 -0700234 spin_unlock_irq(&mapping->tree_lock);
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700235 mem_cgroup_uncharge_cache_page(page);
Linus Torvalds6072d132010-12-01 13:35:19 -0500236
237 if (freepage)
238 freepage(page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700239 page_cache_release(page);
240}
241EXPORT_SYMBOL(delete_from_page_cache);
242
Jens Axboe7eaceac2011-03-10 08:52:07 +0100243static int sleep_on_page(void *word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 io_schedule();
246 return 0;
247}
248
Jens Axboe7eaceac2011-03-10 08:52:07 +0100249static int sleep_on_page_killable(void *word)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500250{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100251 sleep_on_page(word);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500252 return fatal_signal_pending(current) ? -EINTR : 0;
253}
254
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700255static int filemap_check_errors(struct address_space *mapping)
256{
257 int ret = 0;
258 /* Check for outstanding write errors */
259 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
260 ret = -ENOSPC;
261 if (test_and_clear_bit(AS_EIO, &mapping->flags))
262 ret = -EIO;
263 return ret;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700267 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700268 * @mapping: address space structure to write
269 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800270 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700271 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700273 * Start writeback against all of a mapping's dirty pages that lie
274 * within the byte offsets <start, end> inclusive.
275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700277 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * these two operations is that if a dirty page/buffer is encountered, it must
279 * be waited upon, and not just skipped over.
280 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800281int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
282 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 int ret;
285 struct writeback_control wbc = {
286 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800287 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700288 .range_start = start,
289 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 };
291
292 if (!mapping_cap_writeback_dirty(mapping))
293 return 0;
294
295 ret = do_writepages(mapping, &wbc);
296 return ret;
297}
298
299static inline int __filemap_fdatawrite(struct address_space *mapping,
300 int sync_mode)
301{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700302 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305int filemap_fdatawrite(struct address_space *mapping)
306{
307 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
308}
309EXPORT_SYMBOL(filemap_fdatawrite);
310
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400311int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800312 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
315}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400316EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Randy Dunlap485bb992006-06-23 02:03:49 -0700318/**
319 * filemap_flush - mostly a non-blocking flush
320 * @mapping: target address_space
321 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * This is a mostly non-blocking flush. Not suitable for data-integrity
323 * purposes - I/O may not be started against all dirty pages.
324 */
325int filemap_flush(struct address_space *mapping)
326{
327 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
328}
329EXPORT_SYMBOL(filemap_flush);
330
Randy Dunlap485bb992006-06-23 02:03:49 -0700331/**
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200332 * filemap_fdatawait_range - wait for writeback to complete
333 * @mapping: address space structure to wait for
334 * @start_byte: offset in bytes where the range starts
335 * @end_byte: offset in bytes where the range ends (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -0700336 *
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200337 * Walk the list of under-writeback pages of the given address space
338 * in the given range and wait for all of them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200340int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
341 loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200343 pgoff_t index = start_byte >> PAGE_CACHE_SHIFT;
344 pgoff_t end = end_byte >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct pagevec pvec;
346 int nr_pages;
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700347 int ret2, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200349 if (end_byte < start_byte)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 pagevec_init(&pvec, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 while ((index <= end) &&
354 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
355 PAGECACHE_TAG_WRITEBACK,
356 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
357 unsigned i;
358
359 for (i = 0; i < nr_pages; i++) {
360 struct page *page = pvec.pages[i];
361
362 /* until radix tree lookup accepts end_index */
363 if (page->index > end)
364 continue;
365
366 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800367 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ret = -EIO;
369 }
370 pagevec_release(&pvec);
371 cond_resched();
372 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700373out:
374 ret2 = filemap_check_errors(mapping);
375 if (!ret)
376 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 return ret;
379}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200380EXPORT_SYMBOL(filemap_fdatawait_range);
381
382/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700383 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700385 *
386 * Walk the list of under-writeback pages of the given address space
387 * and wait for all of them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
389int filemap_fdatawait(struct address_space *mapping)
390{
391 loff_t i_size = i_size_read(mapping->host);
392
393 if (i_size == 0)
394 return 0;
395
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200396 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398EXPORT_SYMBOL(filemap_fdatawait);
399
400int filemap_write_and_wait(struct address_space *mapping)
401{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800402 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800405 err = filemap_fdatawrite(mapping);
406 /*
407 * Even if the above returned error, the pages may be
408 * written partially (e.g. -ENOSPC), so we wait for it.
409 * But the -EIO is special case, it may indicate the worst
410 * thing (e.g. bug) happened, so we avoid waiting for it.
411 */
412 if (err != -EIO) {
413 int err2 = filemap_fdatawait(mapping);
414 if (!err)
415 err = err2;
416 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700417 } else {
418 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800422EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Randy Dunlap485bb992006-06-23 02:03:49 -0700424/**
425 * filemap_write_and_wait_range - write out & wait on a file range
426 * @mapping: the address_space for the pages
427 * @lstart: offset in bytes where the range starts
428 * @lend: offset in bytes where the range ends (inclusive)
429 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800430 * Write out and wait upon file offsets lstart->lend, inclusive.
431 *
432 * Note that `lend' is inclusive (describes the last byte to be written) so
433 * that this function can be used to write to the very end-of-file (end = -1).
434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435int filemap_write_and_wait_range(struct address_space *mapping,
436 loff_t lstart, loff_t lend)
437{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800438 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800441 err = __filemap_fdatawrite_range(mapping, lstart, lend,
442 WB_SYNC_ALL);
443 /* See comment of filemap_write_and_wait() */
444 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200445 int err2 = filemap_fdatawait_range(mapping,
446 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800447 if (!err)
448 err = err2;
449 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700450 } else {
451 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
Chris Masonf6995582009-04-15 13:22:37 -0400455EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Randy Dunlap485bb992006-06-23 02:03:49 -0700457/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700458 * replace_page_cache_page - replace a pagecache page with a new one
459 * @old: page to be replaced
460 * @new: page to replace with
461 * @gfp_mask: allocation mode
462 *
463 * This function replaces a page in the pagecache with a new one. On
464 * success it acquires the pagecache reference for the new page and
465 * drops it for the old page. Both the old and new pages must be
466 * locked. This function does not add the new page to the LRU, the
467 * caller must do that.
468 *
469 * The remove + add is atomic. The only way this function can fail is
470 * memory allocation failure.
471 */
472int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
473{
474 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700475
Sasha Levin309381fea2014-01-23 15:52:54 -0800476 VM_BUG_ON_PAGE(!PageLocked(old), old);
477 VM_BUG_ON_PAGE(!PageLocked(new), new);
478 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700479
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700480 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
481 if (!error) {
482 struct address_space *mapping = old->mapping;
483 void (*freepage)(struct page *);
484
485 pgoff_t offset = old->index;
486 freepage = mapping->a_ops->freepage;
487
488 page_cache_get(new);
489 new->mapping = mapping;
490 new->index = offset;
491
492 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700493 __delete_from_page_cache(old, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700494 error = radix_tree_insert(&mapping->page_tree, offset, new);
495 BUG_ON(error);
496 mapping->nrpages++;
497 __inc_zone_page_state(new, NR_FILE_PAGES);
498 if (PageSwapBacked(new))
499 __inc_zone_page_state(new, NR_SHMEM);
500 spin_unlock_irq(&mapping->tree_lock);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800501 /* mem_cgroup codes must not be called under tree_lock */
502 mem_cgroup_replace_page_cache(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700503 radix_tree_preload_end();
504 if (freepage)
505 freepage(old);
506 page_cache_release(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700507 }
508
509 return error;
510}
511EXPORT_SYMBOL_GPL(replace_page_cache_page);
512
Johannes Weiner0cd61442014-04-03 14:47:46 -0700513static int page_cache_tree_insert(struct address_space *mapping,
Johannes Weinera5289102014-04-03 14:47:51 -0700514 struct page *page, void **shadowp)
Johannes Weiner0cd61442014-04-03 14:47:46 -0700515{
Johannes Weiner449dd692014-04-03 14:47:56 -0700516 struct radix_tree_node *node;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700517 void **slot;
518 int error;
519
Johannes Weiner449dd692014-04-03 14:47:56 -0700520 error = __radix_tree_create(&mapping->page_tree, page->index,
521 &node, &slot);
522 if (error)
523 return error;
524 if (*slot) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700525 void *p;
526
527 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
528 if (!radix_tree_exceptional_entry(p))
529 return -EEXIST;
Johannes Weinera5289102014-04-03 14:47:51 -0700530 if (shadowp)
531 *shadowp = p;
Johannes Weiner449dd692014-04-03 14:47:56 -0700532 mapping->nrshadows--;
533 if (node)
534 workingset_node_shadows_dec(node);
Johannes Weiner0cd61442014-04-03 14:47:46 -0700535 }
Johannes Weiner449dd692014-04-03 14:47:56 -0700536 radix_tree_replace_slot(slot, page);
537 mapping->nrpages++;
538 if (node) {
539 workingset_node_pages_inc(node);
540 /*
541 * Don't track node that contains actual pages.
542 *
543 * Avoid acquiring the list_lru lock if already
544 * untracked. The list_empty() test is safe as
545 * node->private_list is protected by
546 * mapping->tree_lock.
547 */
548 if (!list_empty(&node->private_list))
549 list_lru_del(&workingset_shadow_nodes,
550 &node->private_list);
551 }
552 return 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700553}
554
Johannes Weinera5289102014-04-03 14:47:51 -0700555static int __add_to_page_cache_locked(struct page *page,
556 struct address_space *mapping,
557 pgoff_t offset, gfp_t gfp_mask,
558 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Nick Piggine2867812008-07-25 19:45:30 -0700560 int error;
561
Sasha Levin309381fea2014-01-23 15:52:54 -0800562 VM_BUG_ON_PAGE(!PageLocked(page), page);
563 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700564
565 error = mem_cgroup_cache_charge(page, current->mm,
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -0800566 gfp_mask & GFP_RECLAIM_MASK);
Balbir Singh35c754d2008-02-07 00:14:05 -0800567 if (error)
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700568 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jan Kara5e4c0d972013-09-11 14:26:05 -0700570 error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700571 if (error) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700572 mem_cgroup_uncharge_cache_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700573 return error;
574 }
575
576 page_cache_get(page);
577 page->mapping = mapping;
578 page->index = offset;
579
580 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700581 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700582 radix_tree_preload_end();
583 if (unlikely(error))
584 goto err_insert;
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700585 __inc_zone_page_state(page, NR_FILE_PAGES);
586 spin_unlock_irq(&mapping->tree_lock);
587 trace_mm_filemap_add_to_page_cache(page);
588 return 0;
589err_insert:
590 page->mapping = NULL;
591 /* Leave page->index set: truncation relies upon it */
592 spin_unlock_irq(&mapping->tree_lock);
593 mem_cgroup_uncharge_cache_page(page);
594 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return error;
596}
Johannes Weinera5289102014-04-03 14:47:51 -0700597
598/**
599 * add_to_page_cache_locked - add a locked page to the pagecache
600 * @page: page to add
601 * @mapping: the page's address_space
602 * @offset: page index
603 * @gfp_mask: page allocation mode
604 *
605 * This function is used to add a page to the pagecache. It must be locked.
606 * This function does not add the page to the LRU. The caller must do that.
607 */
608int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
609 pgoff_t offset, gfp_t gfp_mask)
610{
611 return __add_to_page_cache_locked(page, mapping, offset,
612 gfp_mask, NULL);
613}
Nick Piggine2867812008-07-25 19:45:30 -0700614EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400617 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Johannes Weinera5289102014-04-03 14:47:51 -0700619 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700620 int ret;
621
Johannes Weinera5289102014-04-03 14:47:51 -0700622 __set_page_locked(page);
623 ret = __add_to_page_cache_locked(page, mapping, offset,
624 gfp_mask, &shadow);
625 if (unlikely(ret))
626 __clear_page_locked(page);
627 else {
628 /*
629 * The page might have been evicted from cache only
630 * recently, in which case it should be activated like
631 * any other repeatedly accessed page.
632 */
633 if (shadow && workingset_refault(shadow)) {
634 SetPageActive(page);
635 workingset_activation(page);
636 } else
637 ClearPageActive(page);
638 lru_cache_add(page);
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return ret;
641}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300642EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Paul Jackson44110fe2006-03-24 03:16:04 -0800644#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700645struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800646{
Miao Xiec0ff7452010-05-24 14:32:08 -0700647 int n;
648 struct page *page;
649
Paul Jackson44110fe2006-03-24 03:16:04 -0800650 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700651 unsigned int cpuset_mems_cookie;
652 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700653 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700654 n = cpuset_mem_spread_node();
655 page = alloc_pages_exact_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700656 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700657
Miao Xiec0ff7452010-05-24 14:32:08 -0700658 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800659 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700660 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800661}
Nick Piggin2ae88142006-10-28 10:38:23 -0700662EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800663#endif
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
666 * In order to wait for pages to become available there must be
667 * waitqueues associated with pages. By using a hash table of
668 * waitqueues where the bucket discipline is to maintain all
669 * waiters on the same queue and wake all when any of the pages
670 * become available, and for the woken contexts to check to be
671 * sure the appropriate page became available, this saves space
672 * at a cost of "thundering herd" phenomena during rare hash
673 * collisions.
674 */
675static wait_queue_head_t *page_waitqueue(struct page *page)
676{
677 const struct zone *zone = page_zone(page);
678
679 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
680}
681
682static inline void wake_up_page(struct page *page, int bit)
683{
684 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
685}
686
Harvey Harrison920c7a52008-02-04 22:29:26 -0800687void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
690
691 if (test_bit(bit_nr, &page->flags))
Jens Axboe7eaceac2011-03-10 08:52:07 +0100692 __wait_on_bit(page_waitqueue(page), &wait, sleep_on_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 TASK_UNINTERRUPTIBLE);
694}
695EXPORT_SYMBOL(wait_on_page_bit);
696
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700697int wait_on_page_bit_killable(struct page *page, int bit_nr)
698{
699 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
700
701 if (!test_bit(bit_nr, &page->flags))
702 return 0;
703
704 return __wait_on_bit(page_waitqueue(page), &wait,
705 sleep_on_page_killable, TASK_KILLABLE);
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100709 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700710 * @page: Page defining the wait queue of interest
711 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100712 *
713 * Add an arbitrary @waiter to the wait queue for the nominated @page.
714 */
715void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
716{
717 wait_queue_head_t *q = page_waitqueue(page);
718 unsigned long flags;
719
720 spin_lock_irqsave(&q->lock, flags);
721 __add_wait_queue(q, waiter);
722 spin_unlock_irqrestore(&q->lock, flags);
723}
724EXPORT_SYMBOL_GPL(add_page_wait_queue);
725
726/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700727 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * @page: the page
729 *
730 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
731 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
732 * mechananism between PageLocked pages and PageWriteback pages is shared.
733 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
734 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700735 * The mb is necessary to enforce ordering between the clear_bit and the read
736 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800738void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Sasha Levin309381fea2014-01-23 15:52:54 -0800740 VM_BUG_ON_PAGE(!PageLocked(page), page);
Nick Piggin8413ac92008-10-18 20:26:59 -0700741 clear_bit_unlock(PG_locked, &page->flags);
742 smp_mb__after_clear_bit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 wake_up_page(page, PG_locked);
744}
745EXPORT_SYMBOL(unlock_page);
746
Randy Dunlap485bb992006-06-23 02:03:49 -0700747/**
748 * end_page_writeback - end writeback against a page
749 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
751void end_page_writeback(struct page *page)
752{
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700753 if (TestClearPageReclaim(page))
754 rotate_reclaimable_page(page);
755
756 if (!test_clear_page_writeback(page))
757 BUG();
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 smp_mb__after_clear_bit();
760 wake_up_page(page, PG_writeback);
761}
762EXPORT_SYMBOL(end_page_writeback);
763
Randy Dunlap485bb992006-06-23 02:03:49 -0700764/**
765 * __lock_page - get a lock on the page, assuming we need to sleep to get it
766 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800768void __lock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
771
Jens Axboe7eaceac2011-03-10 08:52:07 +0100772 __wait_on_bit_lock(page_waitqueue(page), &wait, sleep_on_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 TASK_UNINTERRUPTIBLE);
774}
775EXPORT_SYMBOL(__lock_page);
776
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800777int __lock_page_killable(struct page *page)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500778{
779 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
780
781 return __wait_on_bit_lock(page_waitqueue(page), &wait,
Jens Axboe7eaceac2011-03-10 08:52:07 +0100782 sleep_on_page_killable, TASK_KILLABLE);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500783}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300784EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500785
Michel Lespinassed065bd82010-10-26 14:21:57 -0700786int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
787 unsigned int flags)
788{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700789 if (flags & FAULT_FLAG_ALLOW_RETRY) {
790 /*
791 * CAUTION! In this case, mmap_sem is not released
792 * even though return 0.
793 */
794 if (flags & FAULT_FLAG_RETRY_NOWAIT)
795 return 0;
796
797 up_read(&mm->mmap_sem);
798 if (flags & FAULT_FLAG_KILLABLE)
799 wait_on_page_locked_killable(page);
800 else
Gleb Natapov318b2752011-03-22 16:30:51 -0700801 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -0700802 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700803 } else {
804 if (flags & FAULT_FLAG_KILLABLE) {
805 int ret;
806
807 ret = __lock_page_killable(page);
808 if (ret) {
809 up_read(&mm->mmap_sem);
810 return 0;
811 }
812 } else
813 __lock_page(page);
814 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -0700815 }
816}
817
Randy Dunlap485bb992006-06-23 02:03:49 -0700818/**
Johannes Weinere7b563b2014-04-03 14:47:44 -0700819 * page_cache_next_hole - find the next hole (not-present entry)
820 * @mapping: mapping
821 * @index: index
822 * @max_scan: maximum range to search
823 *
824 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
825 * lowest indexed hole.
826 *
827 * Returns: the index of the hole if found, otherwise returns an index
828 * outside of the set specified (in which case 'return - index >=
829 * max_scan' will be true). In rare cases of index wrap-around, 0 will
830 * be returned.
831 *
832 * page_cache_next_hole may be called under rcu_read_lock. However,
833 * like radix_tree_gang_lookup, this will not atomically search a
834 * snapshot of the tree at a single point in time. For example, if a
835 * hole is created at index 5, then subsequently a hole is created at
836 * index 10, page_cache_next_hole covering both indexes may return 10
837 * if called under rcu_read_lock.
838 */
839pgoff_t page_cache_next_hole(struct address_space *mapping,
840 pgoff_t index, unsigned long max_scan)
841{
842 unsigned long i;
843
844 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700845 struct page *page;
846
847 page = radix_tree_lookup(&mapping->page_tree, index);
848 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -0700849 break;
850 index++;
851 if (index == 0)
852 break;
853 }
854
855 return index;
856}
857EXPORT_SYMBOL(page_cache_next_hole);
858
859/**
860 * page_cache_prev_hole - find the prev hole (not-present entry)
861 * @mapping: mapping
862 * @index: index
863 * @max_scan: maximum range to search
864 *
865 * Search backwards in the range [max(index-max_scan+1, 0), index] for
866 * the first hole.
867 *
868 * Returns: the index of the hole if found, otherwise returns an index
869 * outside of the set specified (in which case 'index - return >=
870 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
871 * will be returned.
872 *
873 * page_cache_prev_hole may be called under rcu_read_lock. However,
874 * like radix_tree_gang_lookup, this will not atomically search a
875 * snapshot of the tree at a single point in time. For example, if a
876 * hole is created at index 10, then subsequently a hole is created at
877 * index 5, page_cache_prev_hole covering both indexes may return 5 if
878 * called under rcu_read_lock.
879 */
880pgoff_t page_cache_prev_hole(struct address_space *mapping,
881 pgoff_t index, unsigned long max_scan)
882{
883 unsigned long i;
884
885 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700886 struct page *page;
887
888 page = radix_tree_lookup(&mapping->page_tree, index);
889 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -0700890 break;
891 index--;
892 if (index == ULONG_MAX)
893 break;
894 }
895
896 return index;
897}
898EXPORT_SYMBOL(page_cache_prev_hole);
899
900/**
Johannes Weiner0cd61442014-04-03 14:47:46 -0700901 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -0700902 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -0700903 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -0700904 *
Johannes Weiner0cd61442014-04-03 14:47:46 -0700905 * Looks up the page cache slot at @mapping & @offset. If there is a
906 * page cache page, it is returned with an increased refcount.
907 *
908 * If the slot holds a shadow entry of a previously evicted page, it
909 * is returned.
910 *
911 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700913struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Nick Piggina60637c2008-07-25 19:45:31 -0700915 void **pagep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct page *page;
917
Nick Piggina60637c2008-07-25 19:45:31 -0700918 rcu_read_lock();
919repeat:
920 page = NULL;
921 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
922 if (pagep) {
923 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -0800924 if (unlikely(!page))
925 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700926 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700927 if (radix_tree_deref_retry(page))
928 goto repeat;
929 /*
930 * Otherwise, shmem/tmpfs must be storing a swap entry
931 * here as an exceptional entry: so return it without
932 * attempting to raise page count.
933 */
934 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700935 }
Nick Piggina60637c2008-07-25 19:45:31 -0700936 if (!page_cache_get_speculative(page))
937 goto repeat;
938
939 /*
940 * Has the page moved?
941 * This is part of the lockless pagecache protocol. See
942 * include/linux/pagemap.h for details.
943 */
944 if (unlikely(page != *pagep)) {
945 page_cache_release(page);
946 goto repeat;
947 }
948 }
Nick Piggin27d20fd2010-11-11 14:05:19 -0800949out:
Nick Piggina60637c2008-07-25 19:45:31 -0700950 rcu_read_unlock();
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return page;
953}
Johannes Weiner0cd61442014-04-03 14:47:46 -0700954EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Randy Dunlap485bb992006-06-23 02:03:49 -0700956/**
Johannes Weiner0cd61442014-04-03 14:47:46 -0700957 * find_get_page - find and get a page reference
Martin Waitz67be2dd2005-05-01 08:59:26 -0700958 * @mapping: the address_space to search
959 * @offset: the page index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *
Johannes Weiner0cd61442014-04-03 14:47:46 -0700961 * Looks up the page cache slot at @mapping & @offset. If there is a
962 * page cache page, it is returned with an increased refcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *
Johannes Weiner0cd61442014-04-03 14:47:46 -0700964 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700966struct page *find_get_page(struct address_space *mapping, pgoff_t offset)
967{
968 struct page *page = find_get_entry(mapping, offset);
969
970 if (radix_tree_exceptional_entry(page))
971 page = NULL;
972 return page;
973}
974EXPORT_SYMBOL(find_get_page);
975
976/**
977 * find_lock_entry - locate, pin and lock a page cache entry
978 * @mapping: the address_space to search
979 * @offset: the page cache index
980 *
981 * Looks up the page cache slot at @mapping & @offset. If there is a
982 * page cache page, it is returned locked and with an increased
983 * refcount.
984 *
985 * If the slot holds a shadow entry of a previously evicted page, it
986 * is returned.
987 *
988 * Otherwise, %NULL is returned.
989 *
990 * find_lock_entry() may sleep.
991 */
992struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 struct page *page;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -0700997 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700998 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -0700999 lock_page(page);
1000 /* Has the page been truncated? */
1001 if (unlikely(page->mapping != mapping)) {
1002 unlock_page(page);
1003 page_cache_release(page);
1004 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001006 VM_BUG_ON_PAGE(page->index != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return page;
1009}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001010EXPORT_SYMBOL(find_lock_entry);
1011
1012/**
1013 * find_lock_page - locate, pin and lock a pagecache page
1014 * @mapping: the address_space to search
1015 * @offset: the page index
1016 *
1017 * Looks up the page cache slot at @mapping & @offset. If there is a
1018 * page cache page, it is returned locked and with an increased
1019 * refcount.
1020 *
1021 * Otherwise, %NULL is returned.
1022 *
1023 * find_lock_page() may sleep.
1024 */
1025struct page *find_lock_page(struct address_space *mapping, pgoff_t offset)
1026{
1027 struct page *page = find_lock_entry(mapping, offset);
1028
1029 if (radix_tree_exceptional_entry(page))
1030 page = NULL;
1031 return page;
1032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033EXPORT_SYMBOL(find_lock_page);
1034
1035/**
1036 * find_or_create_page - locate or add a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -07001037 * @mapping: the page's address_space
1038 * @index: the page's index into the mapping
1039 * @gfp_mask: page allocation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001041 * Looks up the page cache slot at @mapping & @offset. If there is a
1042 * page cache page, it is returned locked and with an increased
1043 * refcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001045 * If the page is not present, a new page is allocated using @gfp_mask
1046 * and added to the page cache and the VM's LRU list. The page is
1047 * returned locked and with an increased refcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001049 * On memory exhaustion, %NULL is returned.
1050 *
1051 * find_or_create_page() may sleep, even if @gfp_flags specifies an
1052 * atomic allocation!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
1054struct page *find_or_create_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001055 pgoff_t index, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Nick Piggineb2be182007-10-16 01:24:57 -07001057 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int err;
1059repeat:
1060 page = find_lock_page(mapping, index);
1061 if (!page) {
Nick Piggineb2be182007-10-16 01:24:57 -07001062 page = __page_cache_alloc(gfp_mask);
1063 if (!page)
1064 return NULL;
Nick Piggin67d58ac2009-01-06 14:40:28 -08001065 /*
1066 * We want a regular kernel memory (not highmem or DMA etc)
1067 * allocation for the radix tree nodes, but we need to honour
1068 * the context-specific requirements the caller has asked for.
1069 * GFP_RECLAIM_MASK collects those requirements.
1070 */
1071 err = add_to_page_cache_lru(page, mapping, index,
1072 (gfp_mask & GFP_RECLAIM_MASK));
Nick Piggineb2be182007-10-16 01:24:57 -07001073 if (unlikely(err)) {
1074 page_cache_release(page);
1075 page = NULL;
1076 if (err == -EEXIST)
1077 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return page;
1081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082EXPORT_SYMBOL(find_or_create_page);
1083
1084/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001085 * find_get_entries - gang pagecache lookup
1086 * @mapping: The address_space to search
1087 * @start: The starting page cache index
1088 * @nr_entries: The maximum number of entries
1089 * @entries: Where the resulting entries are placed
1090 * @indices: The cache indices corresponding to the entries in @entries
1091 *
1092 * find_get_entries() will search for and return a group of up to
1093 * @nr_entries entries in the mapping. The entries are placed at
1094 * @entries. find_get_entries() takes a reference against any actual
1095 * pages it returns.
1096 *
1097 * The search returns a group of mapping-contiguous page cache entries
1098 * with ascending indexes. There may be holes in the indices due to
1099 * not-present pages.
1100 *
1101 * Any shadow entries of evicted pages are included in the returned
1102 * array.
1103 *
1104 * find_get_entries() returns the number of pages and shadow entries
1105 * which were found.
1106 */
1107unsigned find_get_entries(struct address_space *mapping,
1108 pgoff_t start, unsigned int nr_entries,
1109 struct page **entries, pgoff_t *indices)
1110{
1111 void **slot;
1112 unsigned int ret = 0;
1113 struct radix_tree_iter iter;
1114
1115 if (!nr_entries)
1116 return 0;
1117
1118 rcu_read_lock();
1119restart:
1120 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1121 struct page *page;
1122repeat:
1123 page = radix_tree_deref_slot(slot);
1124 if (unlikely(!page))
1125 continue;
1126 if (radix_tree_exception(page)) {
1127 if (radix_tree_deref_retry(page))
1128 goto restart;
1129 /*
1130 * Otherwise, we must be storing a swap entry
1131 * here as an exceptional entry: so return it
1132 * without attempting to raise page count.
1133 */
1134 goto export;
1135 }
1136 if (!page_cache_get_speculative(page))
1137 goto repeat;
1138
1139 /* Has the page moved? */
1140 if (unlikely(page != *slot)) {
1141 page_cache_release(page);
1142 goto repeat;
1143 }
1144export:
1145 indices[ret] = iter.index;
1146 entries[ret] = page;
1147 if (++ret == nr_entries)
1148 break;
1149 }
1150 rcu_read_unlock();
1151 return ret;
1152}
1153
1154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * find_get_pages - gang pagecache lookup
1156 * @mapping: The address_space to search
1157 * @start: The starting page index
1158 * @nr_pages: The maximum number of pages
1159 * @pages: Where the resulting pages are placed
1160 *
1161 * find_get_pages() will search for and return a group of up to
1162 * @nr_pages pages in the mapping. The pages are placed at @pages.
1163 * find_get_pages() takes a reference against the returned pages.
1164 *
1165 * The search returns a group of mapping-contiguous pages with ascending
1166 * indexes. There may be holes in the indices due to not-present pages.
1167 *
1168 * find_get_pages() returns the number of pages which were found.
1169 */
1170unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1171 unsigned int nr_pages, struct page **pages)
1172{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001173 struct radix_tree_iter iter;
1174 void **slot;
1175 unsigned ret = 0;
1176
1177 if (unlikely(!nr_pages))
1178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Nick Piggina60637c2008-07-25 19:45:31 -07001180 rcu_read_lock();
1181restart:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001182 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Nick Piggina60637c2008-07-25 19:45:31 -07001183 struct page *page;
1184repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001185 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001186 if (unlikely(!page))
1187 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001188
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001189 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001190 if (radix_tree_deref_retry(page)) {
1191 /*
1192 * Transient condition which can only trigger
1193 * when entry at index 0 moves out of or back
1194 * to root: none yet gotten, safe to restart.
1195 */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001196 WARN_ON(iter.index);
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001197 goto restart;
1198 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001199 /*
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001200 * Otherwise, shmem/tmpfs must be storing a swap entry
1201 * here as an exceptional entry: so skip over it -
1202 * we only reach this from invalidate_mapping_pages().
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001203 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001204 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001205 }
Nick Piggina60637c2008-07-25 19:45:31 -07001206
1207 if (!page_cache_get_speculative(page))
1208 goto repeat;
1209
1210 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001211 if (unlikely(page != *slot)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001212 page_cache_release(page);
1213 goto repeat;
1214 }
1215
1216 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001217 if (++ret == nr_pages)
1218 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001219 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001220
Nick Piggina60637c2008-07-25 19:45:31 -07001221 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return ret;
1223}
1224
Jens Axboeebf43502006-04-27 08:46:01 +02001225/**
1226 * find_get_pages_contig - gang contiguous pagecache lookup
1227 * @mapping: The address_space to search
1228 * @index: The starting page index
1229 * @nr_pages: The maximum number of pages
1230 * @pages: Where the resulting pages are placed
1231 *
1232 * find_get_pages_contig() works exactly like find_get_pages(), except
1233 * that the returned number of pages are guaranteed to be contiguous.
1234 *
1235 * find_get_pages_contig() returns the number of pages which were found.
1236 */
1237unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1238 unsigned int nr_pages, struct page **pages)
1239{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001240 struct radix_tree_iter iter;
1241 void **slot;
1242 unsigned int ret = 0;
1243
1244 if (unlikely(!nr_pages))
1245 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001246
Nick Piggina60637c2008-07-25 19:45:31 -07001247 rcu_read_lock();
1248restart:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001249 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Nick Piggina60637c2008-07-25 19:45:31 -07001250 struct page *page;
1251repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001252 page = radix_tree_deref_slot(slot);
1253 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001254 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001255 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001256
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001257 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001258 if (radix_tree_deref_retry(page)) {
1259 /*
1260 * Transient condition which can only trigger
1261 * when entry at index 0 moves out of or back
1262 * to root: none yet gotten, safe to restart.
1263 */
1264 goto restart;
1265 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001266 /*
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001267 * Otherwise, shmem/tmpfs must be storing a swap entry
1268 * here as an exceptional entry: so stop looking for
1269 * contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001270 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001271 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001272 }
Nick Piggina60637c2008-07-25 19:45:31 -07001273
Nick Piggina60637c2008-07-25 19:45:31 -07001274 if (!page_cache_get_speculative(page))
1275 goto repeat;
1276
1277 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001278 if (unlikely(page != *slot)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001279 page_cache_release(page);
1280 goto repeat;
1281 }
1282
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001283 /*
1284 * must check mapping and index after taking the ref.
1285 * otherwise we can get both false positives and false
1286 * negatives, which is just confusing to the caller.
1287 */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001288 if (page->mapping == NULL || page->index != iter.index) {
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001289 page_cache_release(page);
1290 break;
1291 }
1292
Nick Piggina60637c2008-07-25 19:45:31 -07001293 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001294 if (++ret == nr_pages)
1295 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001296 }
Nick Piggina60637c2008-07-25 19:45:31 -07001297 rcu_read_unlock();
1298 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001299}
David Howellsef71c152007-05-09 02:33:44 -07001300EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001301
Randy Dunlap485bb992006-06-23 02:03:49 -07001302/**
1303 * find_get_pages_tag - find and return pages that match @tag
1304 * @mapping: the address_space to search
1305 * @index: the starting page index
1306 * @tag: the tag index
1307 * @nr_pages: the maximum number of pages
1308 * @pages: where the resulting pages are placed
1309 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001311 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 */
1313unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
1314 int tag, unsigned int nr_pages, struct page **pages)
1315{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001316 struct radix_tree_iter iter;
1317 void **slot;
1318 unsigned ret = 0;
1319
1320 if (unlikely(!nr_pages))
1321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Nick Piggina60637c2008-07-25 19:45:31 -07001323 rcu_read_lock();
1324restart:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001325 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1326 &iter, *index, tag) {
Nick Piggina60637c2008-07-25 19:45:31 -07001327 struct page *page;
1328repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001329 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001330 if (unlikely(!page))
1331 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001332
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001333 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001334 if (radix_tree_deref_retry(page)) {
1335 /*
1336 * Transient condition which can only trigger
1337 * when entry at index 0 moves out of or back
1338 * to root: none yet gotten, safe to restart.
1339 */
1340 goto restart;
1341 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001342 /*
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001343 * This function is never used on a shmem/tmpfs
1344 * mapping, so a swap entry won't be found here.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001345 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001346 BUG();
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001347 }
Nick Piggina60637c2008-07-25 19:45:31 -07001348
1349 if (!page_cache_get_speculative(page))
1350 goto repeat;
1351
1352 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001353 if (unlikely(page != *slot)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001354 page_cache_release(page);
1355 goto repeat;
1356 }
1357
1358 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001359 if (++ret == nr_pages)
1360 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001361 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001362
Nick Piggina60637c2008-07-25 19:45:31 -07001363 rcu_read_unlock();
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (ret)
1366 *index = pages[ret - 1]->index + 1;
Nick Piggina60637c2008-07-25 19:45:31 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return ret;
1369}
David Howellsef71c152007-05-09 02:33:44 -07001370EXPORT_SYMBOL(find_get_pages_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Randy Dunlap485bb992006-06-23 02:03:49 -07001372/**
1373 * grab_cache_page_nowait - returns locked page at given index in given cache
1374 * @mapping: target address_space
1375 * @index: the page index
1376 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001377 * Same as grab_cache_page(), but do not wait if the page is unavailable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * This is intended for speculative data generators, where the data can
1379 * be regenerated if the page couldn't be grabbed. This routine should
1380 * be safe to call while holding the lock for another page.
1381 *
1382 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
1383 * and deadlock against the caller's locked page.
1384 */
1385struct page *
Fengguang Wu57f6b962007-10-16 01:24:37 -07001386grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct page *page = find_get_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if (page) {
Nick Piggin529ae9a2008-08-02 12:01:03 +02001391 if (trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return page;
1393 page_cache_release(page);
1394 return NULL;
1395 }
Nick Piggin2ae88142006-10-28 10:38:23 -07001396 page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
Nick Piggin67d58ac2009-01-06 14:40:28 -08001397 if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 page_cache_release(page);
1399 page = NULL;
1400 }
1401 return page;
1402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403EXPORT_SYMBOL(grab_cache_page_nowait);
1404
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001405/*
1406 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1407 * a _large_ part of the i/o request. Imagine the worst scenario:
1408 *
1409 * ---R__________________________________________B__________
1410 * ^ reading here ^ bad block(assume 4k)
1411 *
1412 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1413 * => failing the whole request => read(R) => read(R+1) =>
1414 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1415 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1416 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1417 *
1418 * It is going insane. Fix it by quickly scaling down the readahead size.
1419 */
1420static void shrink_readahead_size_eio(struct file *filp,
1421 struct file_ra_state *ra)
1422{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001423 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001424}
1425
Randy Dunlap485bb992006-06-23 02:03:49 -07001426/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001427 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001428 * @filp: the file to read
1429 * @ppos: current file position
1430 * @desc: read_descriptor
Randy Dunlap485bb992006-06-23 02:03:49 -07001431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001433 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 *
1435 * This is really ugly. But the goto's actually try to clarify some
1436 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
Christoph Hellwig36e78912008-02-08 04:21:24 -08001438static void do_generic_file_read(struct file *filp, loff_t *ppos,
Kirill A. Shutemovb77d88d2013-11-14 14:30:40 -08001439 read_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001441 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001443 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001444 pgoff_t index;
1445 pgoff_t last_index;
1446 pgoff_t prev_index;
1447 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001448 unsigned int prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 index = *ppos >> PAGE_CACHE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07001452 prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
1453 prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
1455 offset = *ppos & ~PAGE_CACHE_MASK;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 for (;;) {
1458 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001459 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001460 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 unsigned long nr, ret;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464find_page:
1465 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001466 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001467 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001468 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001469 index, last_index - index);
1470 page = find_get_page(mapping, index);
1471 if (unlikely(page == NULL))
1472 goto no_cached_page;
1473 }
1474 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001475 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001476 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001477 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001479 if (!PageUptodate(page)) {
1480 if (inode->i_blkbits == PAGE_CACHE_SHIFT ||
1481 !mapping->a_ops->is_partially_uptodate)
1482 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001483 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001484 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001485 /* Did it get truncated before we got the lock? */
1486 if (!page->mapping)
1487 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001488 if (!mapping->a_ops->is_partially_uptodate(page,
1489 desc, offset))
1490 goto page_not_up_to_date_locked;
1491 unlock_page(page);
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001494 /*
1495 * i_size must be checked after we know the page is Uptodate.
1496 *
1497 * Checking i_size after the check allows us to calculate
1498 * the correct value for "nr", which means the zero-filled
1499 * part of the page is not copied back to userspace (unless
1500 * another truncate extends the file - this is desired though).
1501 */
1502
1503 isize = i_size_read(inode);
1504 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1505 if (unlikely(!isize || index > end_index)) {
1506 page_cache_release(page);
1507 goto out;
1508 }
1509
1510 /* nr is the maximum number of bytes to copy from this page */
1511 nr = PAGE_CACHE_SIZE;
1512 if (index == end_index) {
1513 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1514 if (nr <= offset) {
1515 page_cache_release(page);
1516 goto out;
1517 }
1518 }
1519 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /* If users can be writing to this page using arbitrary
1522 * virtual addresses, take care about potential aliasing
1523 * before reading the page on the kernel side.
1524 */
1525 if (mapping_writably_mapped(mapping))
1526 flush_dcache_page(page);
1527
1528 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001529 * When a sequential read accesses a page several times,
1530 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
Jan Karaec0f1632007-05-06 14:49:25 -07001532 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 mark_page_accessed(page);
1534 prev_index = index;
1535
1536 /*
1537 * Ok, we have the page, and it's up-to-date, so
1538 * now we can copy it to user space...
1539 *
Kirill A. Shutemovb77d88d2013-11-14 14:30:40 -08001540 * The file_read_actor routine returns how many bytes were
1541 * actually used..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * NOTE! This may not be the same as how much of a user buffer
1543 * we filled up (we may be padding etc), so we can only update
1544 * "pos" here (the actor routine has to update the user buffer
1545 * pointers and the remaining count).
1546 */
Kirill A. Shutemovb77d88d2013-11-14 14:30:40 -08001547 ret = file_read_actor(desc, page, offset, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 offset += ret;
1549 index += offset >> PAGE_CACHE_SHIFT;
1550 offset &= ~PAGE_CACHE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001551 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 page_cache_release(page);
1554 if (ret == nr && desc->count)
1555 continue;
1556 goto out;
1557
1558page_not_up_to_date:
1559 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001560 error = lock_page_killable(page);
1561 if (unlikely(error))
1562 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001564page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001565 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (!page->mapping) {
1567 unlock_page(page);
1568 page_cache_release(page);
1569 continue;
1570 }
1571
1572 /* Did somebody else fill it already? */
1573 if (PageUptodate(page)) {
1574 unlock_page(page);
1575 goto page_ok;
1576 }
1577
1578readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001579 /*
1580 * A previous I/O error may have been due to temporary
1581 * failures, eg. multipath errors.
1582 * PG_error will be set again if readpage fails.
1583 */
1584 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* Start the actual read. The read will unlock the page. */
1586 error = mapping->a_ops->readpage(filp, page);
1587
Zach Brown994fc28c2005-12-15 14:28:17 -08001588 if (unlikely(error)) {
1589 if (error == AOP_TRUNCATED_PAGE) {
1590 page_cache_release(page);
1591 goto find_page;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08001594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04001597 error = lock_page_killable(page);
1598 if (unlikely(error))
1599 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!PageUptodate(page)) {
1601 if (page->mapping == NULL) {
1602 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001603 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 */
1605 unlock_page(page);
1606 page_cache_release(page);
1607 goto find_page;
1608 }
1609 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07001610 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04001611 error = -EIO;
1612 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 unlock_page(page);
1615 }
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto page_ok;
1618
1619readpage_error:
1620 /* UHHUH! A synchronous read error occurred. Report it */
1621 desc->error = error;
1622 page_cache_release(page);
1623 goto out;
1624
1625no_cached_page:
1626 /*
1627 * Ok, it wasn't cached, so we need to create a new
1628 * page..
1629 */
Nick Piggineb2be182007-10-16 01:24:57 -07001630 page = page_cache_alloc_cold(mapping);
1631 if (!page) {
1632 desc->error = -ENOMEM;
1633 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Nick Piggineb2be182007-10-16 01:24:57 -07001635 error = add_to_page_cache_lru(page, mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 index, GFP_KERNEL);
1637 if (error) {
Nick Piggineb2be182007-10-16 01:24:57 -07001638 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (error == -EEXIST)
1640 goto find_page;
1641 desc->error = error;
1642 goto out;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto readpage;
1645 }
1646
1647out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07001648 ra->prev_pos = prev_index;
1649 ra->prev_pos <<= PAGE_CACHE_SHIFT;
1650 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Fengguang Wuf4e6b492007-10-16 01:24:33 -07001652 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07001653 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656int file_read_actor(read_descriptor_t *desc, struct page *page,
1657 unsigned long offset, unsigned long size)
1658{
1659 char *kaddr;
1660 unsigned long left, count = desc->count;
1661
1662 if (size > count)
1663 size = count;
1664
1665 /*
1666 * Faults on the destination of a read are common, so do it before
1667 * taking the kmap.
1668 */
1669 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08001670 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 left = __copy_to_user_inatomic(desc->arg.buf,
1672 kaddr + offset, size);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001673 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (left == 0)
1675 goto success;
1676 }
1677
1678 /* Do it the slow way */
1679 kaddr = kmap(page);
1680 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1681 kunmap(page);
1682
1683 if (left) {
1684 size -= left;
1685 desc->error = -EFAULT;
1686 }
1687success:
1688 desc->count = count - size;
1689 desc->written += size;
1690 desc->arg.buf += size;
1691 return size;
1692}
1693
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07001694/*
1695 * Performs necessary checks before doing a write
1696 * @iov: io vector request
1697 * @nr_segs: number of segments in the iovec
1698 * @count: number of bytes to write
1699 * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1700 *
1701 * Adjust number of segments and amount of bytes to write (nr_segs should be
1702 * properly initialized first). Returns appropriate error code that caller
1703 * should return or zero in case that write should be allowed.
1704 */
1705int generic_segment_checks(const struct iovec *iov,
1706 unsigned long *nr_segs, size_t *count, int access_flags)
1707{
1708 unsigned long seg;
1709 size_t cnt = 0;
1710 for (seg = 0; seg < *nr_segs; seg++) {
1711 const struct iovec *iv = &iov[seg];
1712
1713 /*
1714 * If any segment has a negative length, or the cumulative
1715 * length ever wraps negative then return -EINVAL.
1716 */
1717 cnt += iv->iov_len;
1718 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1719 return -EINVAL;
1720 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1721 continue;
1722 if (seg == 0)
1723 return -EFAULT;
1724 *nr_segs = seg;
1725 cnt -= iv->iov_len; /* This segment is no good */
1726 break;
1727 }
1728 *count = cnt;
1729 return 0;
1730}
1731EXPORT_SYMBOL(generic_segment_checks);
1732
Randy Dunlap485bb992006-06-23 02:03:49 -07001733/**
Henrik Kretzschmarb2abacf2006-10-04 02:15:22 -07001734 * generic_file_aio_read - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001735 * @iocb: kernel I/O control block
1736 * @iov: io vector request
1737 * @nr_segs: number of segments in the iovec
Henrik Kretzschmarb2abacf2006-10-04 02:15:22 -07001738 * @pos: current file position
Randy Dunlap485bb992006-06-23 02:03:49 -07001739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * This is the "read()" routine for all filesystems
1741 * that can use the page cache directly.
1742 */
1743ssize_t
Badari Pulavarty543ade12006-09-30 23:28:48 -07001744generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1745 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct file *filp = iocb->ki_filp;
1748 ssize_t retval;
Josef Bacik66f998f2010-05-23 11:00:54 -04001749 unsigned long seg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 size_t count;
Badari Pulavarty543ade12006-09-30 23:28:48 -07001751 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 count = 0;
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07001754 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1755 if (retval)
1756 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1759 if (filp->f_flags & O_DIRECT) {
Badari Pulavarty543ade12006-09-30 23:28:48 -07001760 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct address_space *mapping;
1762 struct inode *inode;
1763
1764 mapping = filp->f_mapping;
1765 inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (!count)
1767 goto out; /* skip atime */
1768 size = i_size_read(inode);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001769 retval = filemap_write_and_wait_range(mapping, pos,
Nick Piggin48b47c52009-01-06 14:40:22 -08001770 pos + iov_length(iov, nr_segs) - 1);
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001771 if (!retval) {
1772 retval = mapping->a_ops->direct_IO(READ, iocb,
1773 iov, pos, nr_segs);
1774 }
1775 if (retval > 0) {
1776 *ppos = pos + retval;
1777 count -= retval;
1778 }
Josef Bacik66f998f2010-05-23 11:00:54 -04001779
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00001780 /*
1781 * Btrfs can have a short DIO read if we encounter
1782 * compressed extents, so if there was an error, or if
1783 * we've already read everything we wanted to, or if
1784 * there was a short read because we hit EOF, go ahead
1785 * and return. Otherwise fallthrough to buffered io for
1786 * the rest of the read.
1787 */
1788 if (retval < 0 || !count || *ppos >= size) {
1789 file_accessed(filp);
1790 goto out;
Steven Whitehouse0e0bcae2006-09-27 14:45:07 -04001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
Josef Bacik66f998f2010-05-23 11:00:54 -04001794 count = retval;
Hugh Dickins11fa9772008-07-23 21:27:34 -07001795 for (seg = 0; seg < nr_segs; seg++) {
1796 read_descriptor_t desc;
Josef Bacik66f998f2010-05-23 11:00:54 -04001797 loff_t offset = 0;
1798
1799 /*
1800 * If we did a short DIO read we need to skip the section of the
1801 * iov that we've already read data into.
1802 */
1803 if (count) {
1804 if (count > iov[seg].iov_len) {
1805 count -= iov[seg].iov_len;
1806 continue;
1807 }
1808 offset = count;
1809 count = 0;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Hugh Dickins11fa9772008-07-23 21:27:34 -07001812 desc.written = 0;
Josef Bacik66f998f2010-05-23 11:00:54 -04001813 desc.arg.buf = iov[seg].iov_base + offset;
1814 desc.count = iov[seg].iov_len - offset;
Hugh Dickins11fa9772008-07-23 21:27:34 -07001815 if (desc.count == 0)
1816 continue;
1817 desc.error = 0;
Kirill A. Shutemovb77d88d2013-11-14 14:30:40 -08001818 do_generic_file_read(filp, ppos, &desc);
Hugh Dickins11fa9772008-07-23 21:27:34 -07001819 retval += desc.written;
1820 if (desc.error) {
1821 retval = retval ?: desc.error;
1822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Hugh Dickins11fa9772008-07-23 21:27:34 -07001824 if (desc.count > 0)
1825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827out:
1828 return retval;
1829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830EXPORT_SYMBOL(generic_file_aio_read);
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001833/**
1834 * page_cache_read - adds requested page to the page cache if not already there
1835 * @file: file to read
1836 * @offset: page index
1837 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * This adds the requested page to the page cache if it isn't already there,
1839 * and schedules an I/O to read in its contents from disk.
1840 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001841static int page_cache_read(struct file *file, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct address_space *mapping = file->f_mapping;
1844 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001845 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Zach Brown994fc28c2005-12-15 14:28:17 -08001847 do {
1848 page = page_cache_alloc_cold(mapping);
1849 if (!page)
1850 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Zach Brown994fc28c2005-12-15 14:28:17 -08001852 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1853 if (ret == 0)
1854 ret = mapping->a_ops->readpage(file, page);
1855 else if (ret == -EEXIST)
1856 ret = 0; /* losing race to add is OK */
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Zach Brown994fc28c2005-12-15 14:28:17 -08001860 } while (ret == AOP_TRUNCATED_PAGE);
1861
1862 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865#define MMAP_LOTSAMISS (100)
1866
Linus Torvaldsef00e082009-06-16 15:31:25 -07001867/*
1868 * Synchronous readahead happens when we don't even find
1869 * a page in the page cache at all.
1870 */
1871static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1872 struct file_ra_state *ra,
1873 struct file *file,
1874 pgoff_t offset)
1875{
1876 unsigned long ra_pages;
1877 struct address_space *mapping = file->f_mapping;
1878
1879 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07001880 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001881 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001882 if (!ra->ra_pages)
1883 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001884
Joe Perches64363aa2013-07-08 16:00:18 -07001885 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07001886 page_cache_sync_readahead(mapping, ra, file, offset,
1887 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001888 return;
1889 }
1890
Andi Kleen207d04b2011-05-24 17:12:29 -07001891 /* Avoid banging the cache line if not needed */
1892 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001893 ra->mmap_miss++;
1894
1895 /*
1896 * Do we miss much more than hit in this file? If so,
1897 * stop bothering with read-ahead. It will only hurt.
1898 */
1899 if (ra->mmap_miss > MMAP_LOTSAMISS)
1900 return;
1901
Wu Fengguangd30a1102009-06-16 15:31:30 -07001902 /*
1903 * mmap read-around
1904 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001905 ra_pages = max_sane_readahead(ra->ra_pages);
Wu Fengguang275b12b2011-05-24 17:12:28 -07001906 ra->start = max_t(long, 0, offset - ra_pages / 2);
1907 ra->size = ra_pages;
Wu Fengguang2cbea1d2011-05-24 17:12:30 -07001908 ra->async_size = ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001909 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001910}
1911
1912/*
1913 * Asynchronous readahead happens when we find the page and PG_readahead,
1914 * so we want to possibly extend the readahead further..
1915 */
1916static void do_async_mmap_readahead(struct vm_area_struct *vma,
1917 struct file_ra_state *ra,
1918 struct file *file,
1919 struct page *page,
1920 pgoff_t offset)
1921{
1922 struct address_space *mapping = file->f_mapping;
1923
1924 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07001925 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001926 return;
1927 if (ra->mmap_miss > 0)
1928 ra->mmap_miss--;
1929 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07001930 page_cache_async_readahead(mapping, ra, file,
1931 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001932}
1933
Randy Dunlap485bb992006-06-23 02:03:49 -07001934/**
Nick Piggin54cb8822007-07-19 01:46:59 -07001935 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07001936 * @vma: vma in which the fault was taken
1937 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07001938 *
Nick Piggin54cb8822007-07-19 01:46:59 -07001939 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * mapped memory region to read in file data during a page fault.
1941 *
1942 * The goto's are kind of ugly, but this streamlines the normal case of having
1943 * it in the page cache, and handles the special cases reasonably without
1944 * having a lot of duplicated code.
1945 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001946int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07001949 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct address_space *mapping = file->f_mapping;
1951 struct file_ra_state *ra = &file->f_ra;
1952 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001953 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct page *page;
Jan Kara2004dc82008-02-08 04:20:11 -08001955 pgoff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07001956 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001959 if (offset >= size)
Linus Torvalds5307cc12007-10-31 09:19:46 -07001960 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001963 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001965 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07001966 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07001968 * We found the page, so try async readahead before
1969 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001971 do_async_mmap_readahead(vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07001972 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07001973 /* No page in the page cache at all */
1974 do_sync_mmap_readahead(vma, ra, file, offset);
1975 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07001976 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001977 ret = VM_FAULT_MAJOR;
1978retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07001979 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!page)
1981 goto no_cached_page;
1982 }
1983
Michel Lespinassed88c0922010-11-02 13:05:18 -07001984 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
1985 page_cache_release(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001986 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07001987 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07001988
1989 /* Did it get truncated? */
1990 if (unlikely(page->mapping != mapping)) {
1991 unlock_page(page);
1992 put_page(page);
1993 goto retry_find;
1994 }
Sasha Levin309381fea2014-01-23 15:52:54 -08001995 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /*
Nick Piggind00806b2007-07-19 01:46:57 -07001998 * We have a locked page in the page cache, now we need to check
1999 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
Nick Piggind00806b2007-07-19 01:46:57 -07002001 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 goto page_not_uptodate;
2003
Linus Torvaldsef00e082009-06-16 15:31:25 -07002004 /*
2005 * Found the page and have a reference on it.
2006 * We must recheck i_size under page lock.
2007 */
Nick Piggind00806b2007-07-19 01:46:57 -07002008 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002009 if (unlikely(offset >= size)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002010 unlock_page(page);
Yan Zheng745ad482007-10-08 10:08:37 -07002011 page_cache_release(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002012 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002013 }
2014
Nick Piggind0217ac2007-07-19 01:47:03 -07002015 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002016 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018no_cached_page:
2019 /*
2020 * We're only likely to ever get here if MADV_RANDOM is in
2021 * effect.
2022 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002023 error = page_cache_read(file, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /*
2026 * The page we want has now been added to the page cache.
2027 * In the unlikely event that someone removed it in the
2028 * meantime, we'll just come back here and read it again.
2029 */
2030 if (error >= 0)
2031 goto retry_find;
2032
2033 /*
2034 * An error return from page_cache_read can result if the
2035 * system is low on memory, or a problem occurs while trying
2036 * to schedule I/O.
2037 */
2038 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002039 return VM_FAULT_OOM;
2040 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /*
2044 * Umm, take care of errors if the page isn't up-to-date.
2045 * Try to re-read it _once_. We do this synchronously,
2046 * because there really aren't any performance issues here
2047 * and we need to check for errors.
2048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002050 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002051 if (!error) {
2052 wait_on_page_locked(page);
2053 if (!PageUptodate(page))
2054 error = -EIO;
2055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 page_cache_release(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002057
2058 if (!error || error == AOP_TRUNCATED_PAGE)
2059 goto retry_find;
2060
2061 /* Things didn't work out. Return zero to tell the mm layer so. */
2062 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002063 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002064}
2065EXPORT_SYMBOL(filemap_fault);
2066
Jan Kara4fcf1c62012-06-12 16:20:29 +02002067int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2068{
2069 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002070 struct inode *inode = file_inode(vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002071 int ret = VM_FAULT_LOCKED;
2072
Jan Kara14da9202012-06-12 16:20:37 +02002073 sb_start_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002074 file_update_time(vma->vm_file);
2075 lock_page(page);
2076 if (page->mapping != inode->i_mapping) {
2077 unlock_page(page);
2078 ret = VM_FAULT_NOPAGE;
2079 goto out;
2080 }
Jan Kara14da9202012-06-12 16:20:37 +02002081 /*
2082 * We mark the page dirty already here so that when freeze is in
2083 * progress, we are guaranteed that writeback during freezing will
2084 * see the dirty page and writeprotect it again.
2085 */
2086 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002087 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002088out:
Jan Kara14da9202012-06-12 16:20:37 +02002089 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002090 return ret;
2091}
2092EXPORT_SYMBOL(filemap_page_mkwrite);
2093
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002094const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002095 .fault = filemap_fault,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002096 .page_mkwrite = filemap_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002097 .remap_pages = generic_file_remap_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098};
2099
2100/* This is used for a general mmap of a disk file */
2101
2102int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2103{
2104 struct address_space *mapping = file->f_mapping;
2105
2106 if (!mapping->a_ops->readpage)
2107 return -ENOEXEC;
2108 file_accessed(file);
2109 vma->vm_ops = &generic_file_vm_ops;
2110 return 0;
2111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113/*
2114 * This is for filesystems which do not implement ->writepage.
2115 */
2116int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2117{
2118 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2119 return -EINVAL;
2120 return generic_file_mmap(file, vma);
2121}
2122#else
2123int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2124{
2125 return -ENOSYS;
2126}
2127int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2128{
2129 return -ENOSYS;
2130}
2131#endif /* CONFIG_MMU */
2132
2133EXPORT_SYMBOL(generic_file_mmap);
2134EXPORT_SYMBOL(generic_file_readonly_mmap);
2135
Nick Piggin6fe69002007-05-06 14:49:04 -07002136static struct page *__read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002137 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002138 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002139 void *data,
2140 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Nick Piggineb2be182007-10-16 01:24:57 -07002142 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 int err;
2144repeat:
2145 page = find_get_page(mapping, index);
2146 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002147 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002148 if (!page)
2149 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002150 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002151 if (unlikely(err)) {
2152 page_cache_release(page);
2153 if (err == -EEXIST)
2154 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return ERR_PTR(err);
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 err = filler(data, page);
2159 if (err < 0) {
2160 page_cache_release(page);
2161 page = ERR_PTR(err);
2162 }
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return page;
2165}
2166
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002167static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002168 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002169 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002170 void *data,
2171 gfp_t gfp)
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct page *page;
2175 int err;
2176
2177retry:
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002178 page = __read_cache_page(mapping, index, filler, data, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (IS_ERR(page))
David Howellsc855ff32007-05-09 13:42:20 +01002180 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (PageUptodate(page))
2182 goto out;
2183
2184 lock_page(page);
2185 if (!page->mapping) {
2186 unlock_page(page);
2187 page_cache_release(page);
2188 goto retry;
2189 }
2190 if (PageUptodate(page)) {
2191 unlock_page(page);
2192 goto out;
2193 }
2194 err = filler(data, page);
2195 if (err < 0) {
2196 page_cache_release(page);
David Howellsc855ff32007-05-09 13:42:20 +01002197 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
David Howellsc855ff32007-05-09 13:42:20 +01002199out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002200 mark_page_accessed(page);
2201 return page;
2202}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002203
2204/**
2205 * read_cache_page_async - read into page cache, fill it if needed
2206 * @mapping: the page's address_space
2207 * @index: the page index
2208 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002209 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002210 *
2211 * Same as read_cache_page, but don't wait for page to become unlocked
2212 * after submitting it to the filler.
2213 *
2214 * Read into the page cache. If a page already exists, and PageUptodate() is
2215 * not set, try to fill the page but don't wait for it to become unlocked.
2216 *
2217 * If the page does not get brought uptodate, return -EIO.
2218 */
2219struct page *read_cache_page_async(struct address_space *mapping,
2220 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002221 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002222 void *data)
2223{
2224 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2225}
Nick Piggin6fe69002007-05-06 14:49:04 -07002226EXPORT_SYMBOL(read_cache_page_async);
2227
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002228static struct page *wait_on_page_read(struct page *page)
2229{
2230 if (!IS_ERR(page)) {
2231 wait_on_page_locked(page);
2232 if (!PageUptodate(page)) {
2233 page_cache_release(page);
2234 page = ERR_PTR(-EIO);
2235 }
2236 }
2237 return page;
2238}
2239
2240/**
2241 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2242 * @mapping: the page's address_space
2243 * @index: the page index
2244 * @gfp: the page allocator flags to use if allocating
2245 *
2246 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002247 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002248 *
2249 * If the page does not get brought uptodate, return -EIO.
2250 */
2251struct page *read_cache_page_gfp(struct address_space *mapping,
2252 pgoff_t index,
2253 gfp_t gfp)
2254{
2255 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2256
2257 return wait_on_page_read(do_read_cache_page(mapping, index, filler, NULL, gfp));
2258}
2259EXPORT_SYMBOL(read_cache_page_gfp);
2260
Nick Piggin6fe69002007-05-06 14:49:04 -07002261/**
2262 * read_cache_page - read into page cache, fill it if needed
2263 * @mapping: the page's address_space
2264 * @index: the page index
2265 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002266 * @data: first arg to filler(data, page) function, often left as NULL
Nick Piggin6fe69002007-05-06 14:49:04 -07002267 *
2268 * Read into the page cache. If a page already exists, and PageUptodate() is
2269 * not set, try to fill the page then wait for it to become unlocked.
2270 *
2271 * If the page does not get brought uptodate, return -EIO.
2272 */
2273struct page *read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002274 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002275 int (*filler)(void *, struct page *),
Nick Piggin6fe69002007-05-06 14:49:04 -07002276 void *data)
2277{
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002278 return wait_on_page_read(read_cache_page_async(mapping, index, filler, data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280EXPORT_SYMBOL(read_cache_page);
2281
Nick Piggin2f718ff2007-10-16 01:24:59 -07002282static size_t __iovec_copy_from_user_inatomic(char *vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 const struct iovec *iov, size_t base, size_t bytes)
2284{
Ingo Molnarf1800532009-03-02 11:00:57 +01002285 size_t copied = 0, left = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 while (bytes) {
2288 char __user *buf = iov->iov_base + base;
2289 int copy = min(bytes, iov->iov_len - base);
2290
2291 base = 0;
Ingo Molnarf1800532009-03-02 11:00:57 +01002292 left = __copy_from_user_inatomic(vaddr, buf, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 copied += copy;
2294 bytes -= copy;
2295 vaddr += copy;
2296 iov++;
2297
NeilBrown01408c42006-06-25 05:47:58 -07002298 if (unlikely(left))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 return copied - left;
2302}
2303
2304/*
Nick Piggin2f718ff2007-10-16 01:24:59 -07002305 * Copy as much as we can into the page and return the number of bytes which
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002306 * were successfully copied. If a fault is encountered then return the number of
Nick Piggin2f718ff2007-10-16 01:24:59 -07002307 * bytes which were copied.
2308 */
2309size_t iov_iter_copy_from_user_atomic(struct page *page,
2310 struct iov_iter *i, unsigned long offset, size_t bytes)
2311{
2312 char *kaddr;
2313 size_t copied;
2314
2315 BUG_ON(!in_atomic());
Cong Wang9b04c5f2011-11-25 23:14:39 +08002316 kaddr = kmap_atomic(page);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002317 if (likely(i->nr_segs == 1)) {
2318 int left;
2319 char __user *buf = i->iov->iov_base + i->iov_offset;
Ingo Molnarf1800532009-03-02 11:00:57 +01002320 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002321 copied = bytes - left;
2322 } else {
2323 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2324 i->iov, i->iov_offset, bytes);
2325 }
Cong Wang9b04c5f2011-11-25 23:14:39 +08002326 kunmap_atomic(kaddr);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002327
2328 return copied;
2329}
Nick Piggin89e10782007-10-16 01:25:07 -07002330EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002331
2332/*
2333 * This has the same sideeffects and return value as
2334 * iov_iter_copy_from_user_atomic().
2335 * The difference is that it attempts to resolve faults.
2336 * Page must not be locked.
2337 */
2338size_t iov_iter_copy_from_user(struct page *page,
2339 struct iov_iter *i, unsigned long offset, size_t bytes)
2340{
2341 char *kaddr;
2342 size_t copied;
2343
2344 kaddr = kmap(page);
2345 if (likely(i->nr_segs == 1)) {
2346 int left;
2347 char __user *buf = i->iov->iov_base + i->iov_offset;
Ingo Molnarf1800532009-03-02 11:00:57 +01002348 left = __copy_from_user(kaddr + offset, buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002349 copied = bytes - left;
2350 } else {
2351 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2352 i->iov, i->iov_offset, bytes);
2353 }
2354 kunmap(page);
2355 return copied;
2356}
Nick Piggin89e10782007-10-16 01:25:07 -07002357EXPORT_SYMBOL(iov_iter_copy_from_user);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002358
Nick Pigginf7009262008-03-10 11:43:59 -07002359void iov_iter_advance(struct iov_iter *i, size_t bytes)
Nick Piggin2f718ff2007-10-16 01:24:59 -07002360{
Nick Pigginf7009262008-03-10 11:43:59 -07002361 BUG_ON(i->count < bytes);
2362
Nick Piggin2f718ff2007-10-16 01:24:59 -07002363 if (likely(i->nr_segs == 1)) {
2364 i->iov_offset += bytes;
Nick Pigginf7009262008-03-10 11:43:59 -07002365 i->count -= bytes;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002366 } else {
2367 const struct iovec *iov = i->iov;
2368 size_t base = i->iov_offset;
Jeff Layton39be79c2011-10-27 23:53:08 +02002369 unsigned long nr_segs = i->nr_segs;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002370
Nick Piggin124d3b72008-02-02 15:01:17 +01002371 /*
2372 * The !iov->iov_len check ensures we skip over unlikely
Nick Pigginf7009262008-03-10 11:43:59 -07002373 * zero-length segments (without overruning the iovec).
Nick Piggin124d3b72008-02-02 15:01:17 +01002374 */
Linus Torvalds94ad3742008-07-30 14:45:12 -07002375 while (bytes || unlikely(i->count && !iov->iov_len)) {
Nick Pigginf7009262008-03-10 11:43:59 -07002376 int copy;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002377
Nick Pigginf7009262008-03-10 11:43:59 -07002378 copy = min(bytes, iov->iov_len - base);
2379 BUG_ON(!i->count || i->count < copy);
2380 i->count -= copy;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002381 bytes -= copy;
2382 base += copy;
2383 if (iov->iov_len == base) {
2384 iov++;
Jeff Layton39be79c2011-10-27 23:53:08 +02002385 nr_segs--;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002386 base = 0;
2387 }
2388 }
2389 i->iov = iov;
2390 i->iov_offset = base;
Jeff Layton39be79c2011-10-27 23:53:08 +02002391 i->nr_segs = nr_segs;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002392 }
2393}
Nick Piggin89e10782007-10-16 01:25:07 -07002394EXPORT_SYMBOL(iov_iter_advance);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002395
Nick Pigginafddba42007-10-16 01:25:01 -07002396/*
2397 * Fault in the first iovec of the given iov_iter, to a maximum length
2398 * of bytes. Returns 0 on success, or non-zero if the memory could not be
2399 * accessed (ie. because it is an invalid address).
2400 *
2401 * writev-intensive code may want this to prefault several iovecs -- that
2402 * would be possible (callers must not rely on the fact that _only_ the
2403 * first iovec will be faulted with the current implementation).
2404 */
2405int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Nick Piggin2f718ff2007-10-16 01:24:59 -07002406{
Nick Piggin2f718ff2007-10-16 01:24:59 -07002407 char __user *buf = i->iov->iov_base + i->iov_offset;
Nick Pigginafddba42007-10-16 01:25:01 -07002408 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
2409 return fault_in_pages_readable(buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002410}
Nick Piggin89e10782007-10-16 01:25:07 -07002411EXPORT_SYMBOL(iov_iter_fault_in_readable);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002412
2413/*
2414 * Return the count of just the current iov_iter segment.
2415 */
Maxim Patlasovd28574e2012-10-26 19:50:04 +04002416size_t iov_iter_single_seg_count(const struct iov_iter *i)
Nick Piggin2f718ff2007-10-16 01:24:59 -07002417{
2418 const struct iovec *iov = i->iov;
2419 if (i->nr_segs == 1)
2420 return i->count;
2421 else
2422 return min(i->count, iov->iov_len - i->iov_offset);
2423}
Nick Piggin89e10782007-10-16 01:25:07 -07002424EXPORT_SYMBOL(iov_iter_single_seg_count);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002425
2426/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * Performs necessary checks before doing a write
2428 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002429 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 * Returns appropriate error code that caller should return or
2431 * zero in case that write should be allowed.
2432 */
2433inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
2434{
2435 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002436 unsigned long limit = rlimit(RLIMIT_FSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (unlikely(*pos < 0))
2439 return -EINVAL;
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (!isblk) {
2442 /* FIXME: this is for backwards compatibility with 2.4 */
2443 if (file->f_flags & O_APPEND)
2444 *pos = i_size_read(inode);
2445
2446 if (limit != RLIM_INFINITY) {
2447 if (*pos >= limit) {
2448 send_sig(SIGXFSZ, current, 0);
2449 return -EFBIG;
2450 }
2451 if (*count > limit - (typeof(limit))*pos) {
2452 *count = limit - (typeof(limit))*pos;
2453 }
2454 }
2455 }
2456
2457 /*
2458 * LFS rule
2459 */
2460 if (unlikely(*pos + *count > MAX_NON_LFS &&
2461 !(file->f_flags & O_LARGEFILE))) {
2462 if (*pos >= MAX_NON_LFS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return -EFBIG;
2464 }
2465 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
2466 *count = MAX_NON_LFS - (unsigned long)*pos;
2467 }
2468 }
2469
2470 /*
2471 * Are we about to exceed the fs block limit ?
2472 *
2473 * If we have written data it becomes a short write. If we have
2474 * exceeded without writing data we send a signal and return EFBIG.
2475 * Linus frestrict idea will clean these up nicely..
2476 */
2477 if (likely(!isblk)) {
2478 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
2479 if (*count || *pos > inode->i_sb->s_maxbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -EFBIG;
2481 }
2482 /* zero-length writes at ->s_maxbytes are OK */
2483 }
2484
2485 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
2486 *count = inode->i_sb->s_maxbytes - *pos;
2487 } else {
David Howells93614012006-09-30 20:45:40 +02002488#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 loff_t isize;
2490 if (bdev_read_only(I_BDEV(inode)))
2491 return -EPERM;
2492 isize = i_size_read(inode);
2493 if (*pos >= isize) {
2494 if (*count || *pos > isize)
2495 return -ENOSPC;
2496 }
2497
2498 if (*pos + *count > isize)
2499 *count = isize - *pos;
David Howells93614012006-09-30 20:45:40 +02002500#else
2501 return -EPERM;
2502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504 return 0;
2505}
2506EXPORT_SYMBOL(generic_write_checks);
2507
Nick Pigginafddba42007-10-16 01:25:01 -07002508int pagecache_write_begin(struct file *file, struct address_space *mapping,
2509 loff_t pos, unsigned len, unsigned flags,
2510 struct page **pagep, void **fsdata)
2511{
2512 const struct address_space_operations *aops = mapping->a_ops;
2513
Nick Piggin4e02ed42008-10-29 14:00:55 -07002514 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002515 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002516}
2517EXPORT_SYMBOL(pagecache_write_begin);
2518
2519int pagecache_write_end(struct file *file, struct address_space *mapping,
2520 loff_t pos, unsigned len, unsigned copied,
2521 struct page *page, void *fsdata)
2522{
2523 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002524
Nick Piggin4e02ed42008-10-29 14:00:55 -07002525 mark_page_accessed(page);
2526 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002527}
2528EXPORT_SYMBOL(pagecache_write_end);
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530ssize_t
2531generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
2532 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
2533 size_t count, size_t ocount)
2534{
2535 struct file *file = iocb->ki_filp;
2536 struct address_space *mapping = file->f_mapping;
2537 struct inode *inode = mapping->host;
2538 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002539 size_t write_len;
2540 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 if (count != ocount)
2543 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
2544
Christoph Hellwiga969e902008-07-23 21:27:04 -07002545 write_len = iov_length(iov, *nr_segs);
2546 end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002547
Nick Piggin48b47c52009-01-06 14:40:22 -08002548 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002549 if (written)
2550 goto out;
2551
2552 /*
2553 * After a write we want buffered reads to be sure to go to disk to get
2554 * the new data. We invalidate clean cached page from the region we're
2555 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002556 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002557 */
2558 if (mapping->nrpages) {
2559 written = invalidate_inode_pages2_range(mapping,
2560 pos >> PAGE_CACHE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002561 /*
2562 * If a page can not be invalidated, return 0 to fall back
2563 * to buffered write.
2564 */
2565 if (written) {
2566 if (written == -EBUSY)
2567 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002568 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002569 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002570 }
2571
2572 written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs);
2573
2574 /*
2575 * Finally, try again to invalidate clean pages which might have been
2576 * cached by non-direct readahead, or faulted in by get_user_pages()
2577 * if the source of the write was an mmap'ed region of the file
2578 * we're writing. Either one is a pretty crazy thing to do,
2579 * so we don't support it 100%. If this invalidation
2580 * fails, tough, the write still worked...
2581 */
2582 if (mapping->nrpages) {
2583 invalidate_inode_pages2_range(mapping,
2584 pos >> PAGE_CACHE_SHIFT, end);
2585 }
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002588 pos += written;
2589 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2590 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 mark_inode_dirty(inode);
2592 }
Namhyung Kim01166512010-10-26 14:21:58 -07002593 *ppos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002595out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return written;
2597}
2598EXPORT_SYMBOL(generic_file_direct_write);
2599
Nick Piggineb2be182007-10-16 01:24:57 -07002600/*
2601 * Find or create a page at the given pagecache position. Return the locked
2602 * page. This function is specifically for buffered writes.
2603 */
Nick Piggin54566b22009-01-04 12:00:53 -08002604struct page *grab_cache_page_write_begin(struct address_space *mapping,
2605 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002606{
2607 int status;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002608 gfp_t gfp_mask;
Nick Piggineb2be182007-10-16 01:24:57 -07002609 struct page *page;
Nick Piggin54566b22009-01-04 12:00:53 -08002610 gfp_t gfp_notmask = 0;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002611
Fengguang Wu1010bb12012-03-21 16:34:08 -07002612 gfp_mask = mapping_gfp_mask(mapping);
2613 if (mapping_cap_account_dirty(mapping))
2614 gfp_mask |= __GFP_WRITE;
Nick Piggin54566b22009-01-04 12:00:53 -08002615 if (flags & AOP_FLAG_NOFS)
2616 gfp_notmask = __GFP_FS;
Nick Piggineb2be182007-10-16 01:24:57 -07002617repeat:
2618 page = find_lock_page(mapping, index);
Steven Rostedtc585a262011-01-13 15:46:18 -08002619 if (page)
Darrick J. Wong3d08bcc2011-05-27 12:23:34 -07002620 goto found;
Nick Piggineb2be182007-10-16 01:24:57 -07002621
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002622 page = __page_cache_alloc(gfp_mask & ~gfp_notmask);
Nick Piggineb2be182007-10-16 01:24:57 -07002623 if (!page)
2624 return NULL;
Nick Piggin54566b22009-01-04 12:00:53 -08002625 status = add_to_page_cache_lru(page, mapping, index,
2626 GFP_KERNEL & ~gfp_notmask);
Nick Piggineb2be182007-10-16 01:24:57 -07002627 if (unlikely(status)) {
2628 page_cache_release(page);
2629 if (status == -EEXIST)
2630 goto repeat;
2631 return NULL;
2632 }
Darrick J. Wong3d08bcc2011-05-27 12:23:34 -07002633found:
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002634 wait_for_stable_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002635 return page;
2636}
Nick Piggin54566b22009-01-04 12:00:53 -08002637EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002638
Nick Pigginafddba42007-10-16 01:25:01 -07002639static ssize_t generic_perform_write(struct file *file,
2640 struct iov_iter *i, loff_t pos)
2641{
2642 struct address_space *mapping = file->f_mapping;
2643 const struct address_space_operations *a_ops = mapping->a_ops;
2644 long status = 0;
2645 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002646 unsigned int flags = 0;
2647
2648 /*
2649 * Copies from kernel address space cannot fail (NFSD is a big user).
2650 */
2651 if (segment_eq(get_fs(), KERNEL_DS))
2652 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002653
2654 do {
2655 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002656 unsigned long offset; /* Offset into pagecache page */
2657 unsigned long bytes; /* Bytes to write to page */
2658 size_t copied; /* Bytes copied from user */
2659 void *fsdata;
2660
2661 offset = (pos & (PAGE_CACHE_SIZE - 1));
Nick Pigginafddba42007-10-16 01:25:01 -07002662 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2663 iov_iter_count(i));
2664
2665again:
Nick Pigginafddba42007-10-16 01:25:01 -07002666 /*
2667 * Bring in the user page that we will copy from _first_.
2668 * Otherwise there's a nasty deadlock on copying from the
2669 * same page as we're writing to, without it being marked
2670 * up-to-date.
2671 *
2672 * Not only is this an optimisation, but it is also required
2673 * to check that the address is actually valid, when atomic
2674 * usercopies are used, below.
2675 */
2676 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2677 status = -EFAULT;
2678 break;
2679 }
2680
Nick Piggin674b8922007-10-16 01:25:03 -07002681 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002682 &page, &fsdata);
2683 if (unlikely(status))
2684 break;
2685
anfei zhou931e80e2010-02-02 13:44:02 -08002686 if (mapping_writably_mapped(mapping))
2687 flush_dcache_page(page);
2688
Nick Pigginafddba42007-10-16 01:25:01 -07002689 pagefault_disable();
2690 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
2691 pagefault_enable();
2692 flush_dcache_page(page);
2693
Josef Bacikc8236db2009-07-05 12:08:18 -07002694 mark_page_accessed(page);
Nick Pigginafddba42007-10-16 01:25:01 -07002695 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2696 page, fsdata);
2697 if (unlikely(status < 0))
2698 break;
2699 copied = status;
2700
2701 cond_resched();
2702
Nick Piggin124d3b72008-02-02 15:01:17 +01002703 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002704 if (unlikely(copied == 0)) {
2705 /*
2706 * If we were unable to copy any data at all, we must
2707 * fall back to a single segment length write.
2708 *
2709 * If we didn't fallback here, we could livelock
2710 * because not all segments in the iov can be copied at
2711 * once without a pagefault.
2712 */
2713 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2714 iov_iter_single_seg_count(i));
2715 goto again;
2716 }
Nick Pigginafddba42007-10-16 01:25:01 -07002717 pos += copied;
2718 written += copied;
2719
2720 balance_dirty_pages_ratelimited(mapping);
Jan Karaa50527b2011-12-02 09:17:02 +08002721 if (fatal_signal_pending(current)) {
2722 status = -EINTR;
2723 break;
2724 }
Nick Pigginafddba42007-10-16 01:25:01 -07002725 } while (iov_iter_count(i));
2726
2727 return written ? written : status;
2728}
2729
2730ssize_t
2731generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2732 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2733 size_t count, ssize_t written)
2734{
2735 struct file *file = iocb->ki_filp;
Nick Pigginafddba42007-10-16 01:25:01 -07002736 ssize_t status;
2737 struct iov_iter i;
2738
2739 iov_iter_init(&i, iov, nr_segs, count, written);
Nick Piggin4e02ed42008-10-29 14:00:55 -07002740 status = generic_perform_write(file, &i, pos);
Nick Pigginafddba42007-10-16 01:25:01 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (likely(status >= 0)) {
Nick Pigginafddba42007-10-16 01:25:01 -07002743 written += status;
2744 *ppos = pos + status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return written ? written : status;
2748}
2749EXPORT_SYMBOL(generic_file_buffered_write);
2750
Jan Karae4dd9de2009-08-17 18:10:06 +02002751/**
2752 * __generic_file_aio_write - write data to a file
2753 * @iocb: IO state structure (file, offset, etc.)
2754 * @iov: vector with data to write
2755 * @nr_segs: number of segments in the vector
2756 * @ppos: position where to write
2757 *
2758 * This function does all the work needed for actually writing data to a
2759 * file. It does all basic checks, removes SUID from the file, updates
2760 * modification times and calls proper subroutines depending on whether we
2761 * do direct IO or a standard buffered write.
2762 *
2763 * It expects i_mutex to be grabbed unless we work on a block device or similar
2764 * object which does not need locking at all.
2765 *
2766 * This function does *not* take care of syncing data in case of O_SYNC write.
2767 * A caller has to handle it. This is mainly due to the fact that we want to
2768 * avoid syncing under i_mutex.
2769 */
2770ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2771 unsigned long nr_segs, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002774 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 size_t ocount; /* original count */
2776 size_t count; /* after file limit checks */
2777 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 loff_t pos;
2779 ssize_t written;
2780 ssize_t err;
2781
2782 ocount = 0;
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07002783 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2784 if (err)
2785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 count = ocount;
2788 pos = *ppos;
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 /* We can write back this queue in page reclaim */
2791 current->backing_dev_info = mapping->backing_dev_info;
2792 written = 0;
2793
2794 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2795 if (err)
2796 goto out;
2797
2798 if (count == 0)
2799 goto out;
2800
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02002801 err = file_remove_suid(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (err)
2803 goto out;
2804
Josef Bacikc3b2da32012-03-26 09:59:21 -04002805 err = file_update_time(file);
2806 if (err)
2807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2810 if (unlikely(file->f_flags & O_DIRECT)) {
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002811 loff_t endbyte;
2812 ssize_t written_buffered;
2813
2814 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
2815 ppos, count, ocount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (written < 0 || written == count)
2817 goto out;
2818 /*
2819 * direct-io write to a hole: fall through to buffered I/O
2820 * for completing the rest of the request.
2821 */
2822 pos += written;
2823 count -= written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002824 written_buffered = generic_file_buffered_write(iocb, iov,
2825 nr_segs, pos, ppos, count,
2826 written);
2827 /*
2828 * If generic_file_buffered_write() retuned a synchronous error
2829 * then we want to return the number of bytes which were
2830 * direct-written, or the error code if that was zero. Note
2831 * that this differs from normal direct-io semantics, which
2832 * will return -EFOO even if some bytes were written.
2833 */
2834 if (written_buffered < 0) {
2835 err = written_buffered;
2836 goto out;
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002839 /*
2840 * We need to ensure that the page cache pages are written to
2841 * disk and invalidated to preserve the expected O_DIRECT
2842 * semantics.
2843 */
2844 endbyte = pos + written_buffered - written - 1;
Christoph Hellwigc05c4ed2009-09-23 15:07:30 +02002845 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002846 if (err == 0) {
2847 written = written_buffered;
2848 invalidate_mapping_pages(mapping,
2849 pos >> PAGE_CACHE_SHIFT,
2850 endbyte >> PAGE_CACHE_SHIFT);
2851 } else {
2852 /*
2853 * We don't know how much we wrote, so just return
2854 * the number of bytes which were direct-written
2855 */
2856 }
2857 } else {
2858 written = generic_file_buffered_write(iocb, iov, nr_segs,
2859 pos, ppos, count, written);
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861out:
2862 current->backing_dev_info = NULL;
2863 return written ? written : err;
2864}
Jan Karae4dd9de2009-08-17 18:10:06 +02002865EXPORT_SYMBOL(__generic_file_aio_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Jan Karae4dd9de2009-08-17 18:10:06 +02002867/**
2868 * generic_file_aio_write - write data to a file
2869 * @iocb: IO state structure
2870 * @iov: vector with data to write
2871 * @nr_segs: number of segments in the vector
2872 * @pos: position in file where to write
2873 *
2874 * This is a wrapper around __generic_file_aio_write() to be used by most
2875 * filesystems. It takes care of syncing the file in case of O_SYNC file
2876 * and acquires i_mutex as needed.
2877 */
Badari Pulavarty027445c2006-09-30 23:28:46 -07002878ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2879 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02002882 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 BUG_ON(iocb->ki_pos != pos);
2886
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002887 mutex_lock(&inode->i_mutex);
Jan Karae4dd9de2009-08-17 18:10:06 +02002888 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002889 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Christoph Hellwig02afc272013-09-04 15:04:40 +02002891 if (ret > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 ssize_t err;
2893
Al Virod311d792014-02-09 15:18:09 -05002894 err = generic_write_sync(file, iocb->ki_pos - ret, ret);
2895 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 ret = err;
2897 }
2898 return ret;
2899}
2900EXPORT_SYMBOL(generic_file_aio_write);
2901
David Howellscf9a2ae2006-08-29 19:05:54 +01002902/**
2903 * try_to_release_page() - release old fs-specific metadata on a page
2904 *
2905 * @page: the page which the kernel is trying to free
2906 * @gfp_mask: memory allocation flags (and I/O mode)
2907 *
2908 * The address_space is to try to release any data against the page
2909 * (presumably at page->private). If the release was successful, return `1'.
2910 * Otherwise return zero.
2911 *
David Howells266cf652009-04-03 16:42:36 +01002912 * This may also be called if PG_fscache is set on a page, indicating that the
2913 * page is known to the local caching routines.
2914 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002915 * The @gfp_mask argument specifies whether I/O may be performed to release
Mingming Cao3f31fdd2008-07-25 01:46:22 -07002916 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01002917 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002918 */
2919int try_to_release_page(struct page *page, gfp_t gfp_mask)
2920{
2921 struct address_space * const mapping = page->mapping;
2922
2923 BUG_ON(!PageLocked(page));
2924 if (PageWriteback(page))
2925 return 0;
2926
2927 if (mapping && mapping->a_ops->releasepage)
2928 return mapping->a_ops->releasepage(page, gfp_mask);
2929 return try_to_free_buffers(page);
2930}
2931
2932EXPORT_SYMBOL(try_to_release_page);