blob: 97f49ed35bd24fba29677857433814103949dd14 [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>
32#include <linux/syscalls.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080033#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070034#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060036#include <linux/cleancache.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080037#include "internal.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * FIXME: remove all knowledge of the buffer layer from the core VM
41 */
Jan Kara148f9482009-08-17 19:52:36 +020042#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/mman.h>
45
46/*
47 * Shared mappings implemented 30.11.1994. It's not fully working yet,
48 * though.
49 *
50 * Shared mappings now work. 15.8.1995 Bruno.
51 *
52 * finished 'unifying' the page and buffer cache and SMP-threaded the
53 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
54 *
55 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
56 */
57
58/*
59 * Lock ordering:
60 *
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070061 * ->i_mmap_mutex (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070063 * ->swap_lock (exclusive_swap_page, others)
64 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080066 * ->i_mutex
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070067 * ->i_mmap_mutex (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 *
69 * ->mmap_sem
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070070 * ->i_mmap_mutex
Hugh Dickinsb8072f02005-10-29 18:16:41 -070071 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
73 *
74 * ->mmap_sem
75 * ->lock_page (access_process_vm)
76 *
Nick Piggin82591e62006-10-19 23:29:10 -070077 * ->i_mutex (generic_file_buffered_write)
78 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060080 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110081 * sb_lock (fs/fs-writeback.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * ->mapping->tree_lock (__sync_single_inode)
83 *
Peter Zijlstra3d48ae42011-05-24 17:12:06 -070084 * ->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * ->anon_vma.lock (vma_adjust)
86 *
87 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070088 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070090 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070091 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ->private_lock (try_to_unmap_one)
93 * ->tree_lock (try_to_unmap_one)
94 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -080095 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * ->private_lock (page_remove_rmap->set_page_dirty)
97 * ->tree_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -060098 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +110099 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600100 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100101 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
103 *
Andi Kleen6a460792009-09-16 11:50:15 +0200104 * (code doesn't rely on that order, so you could switch it around)
105 * ->tasklist_lock (memory_failure, collect_procs_ao)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700106 * ->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
108
109/*
Minchan Kime64a7822011-03-22 16:32:44 -0700110 * Delete a page from the page cache and free it. Caller has to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * sure the page is locked and that nobody else uses it - or that usage
Nick Piggin19fd6232008-07-25 19:45:32 -0700112 * is safe. The caller must hold the mapping's tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
Minchan Kime64a7822011-03-22 16:32:44 -0700114void __delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct address_space *mapping = page->mapping;
117
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600118 /*
119 * if we're uptodate, flush out into the cleancache, otherwise
120 * invalidate any existing cleancache entries. We can't leave
121 * stale data around in the cleancache once our page is gone
122 */
123 if (PageUptodate(page) && PageMappedToDisk(page))
124 cleancache_put_page(page);
125 else
126 cleancache_flush_page(mapping, page);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 radix_tree_delete(&mapping->page_tree, page->index);
129 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700130 /* Leave page->index set: truncation lookup relies upon it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mapping->nrpages--;
Christoph Lameter347ce432006-06-30 01:55:35 -0700132 __dec_zone_page_state(page, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700133 if (PageSwapBacked(page))
134 __dec_zone_page_state(page, NR_SHMEM);
Nick Piggin45426812007-07-15 23:38:12 -0700135 BUG_ON(page_mapped(page));
Linus Torvalds3a692792007-12-19 14:05:13 -0800136
137 /*
138 * Some filesystems seem to re-dirty the page even after
139 * the VM has canceled the dirty bit (eg ext3 journaling).
140 *
141 * Fix it up by doing a final dirty accounting check after
142 * having removed the page entirely.
143 */
144 if (PageDirty(page) && mapping_cap_account_dirty(mapping)) {
145 dec_zone_page_state(page, NR_FILE_DIRTY);
146 dec_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Minchan Kim702cfbf2011-03-22 16:32:43 -0700150/**
151 * delete_from_page_cache - delete page from page cache
152 * @page: the page which the kernel is trying to remove from page cache
153 *
154 * This must be called only on pages that have been verified to be in the page
155 * cache and locked. It will never put the page into the free list, the caller
156 * has a reference on the page.
157 */
158void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct address_space *mapping = page->mapping;
Linus Torvalds6072d132010-12-01 13:35:19 -0500161 void (*freepage)(struct page *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Matt Mackallcd7619d2005-05-01 08:59:01 -0700163 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds6072d132010-12-01 13:35:19 -0500165 freepage = mapping->a_ops->freepage;
Nick Piggin19fd6232008-07-25 19:45:32 -0700166 spin_lock_irq(&mapping->tree_lock);
Minchan Kime64a7822011-03-22 16:32:44 -0700167 __delete_from_page_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700168 spin_unlock_irq(&mapping->tree_lock);
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700169 mem_cgroup_uncharge_cache_page(page);
Linus Torvalds6072d132010-12-01 13:35:19 -0500170
171 if (freepage)
172 freepage(page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700173 page_cache_release(page);
174}
175EXPORT_SYMBOL(delete_from_page_cache);
176
Jens Axboe7eaceac2011-03-10 08:52:07 +0100177static int sleep_on_page(void *word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 io_schedule();
180 return 0;
181}
182
Jens Axboe7eaceac2011-03-10 08:52:07 +0100183static int sleep_on_page_killable(void *word)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500184{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100185 sleep_on_page(word);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500186 return fatal_signal_pending(current) ? -EINTR : 0;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700190 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700191 * @mapping: address space structure to write
192 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800193 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700194 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700196 * Start writeback against all of a mapping's dirty pages that lie
197 * within the byte offsets <start, end> inclusive.
198 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700200 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * these two operations is that if a dirty page/buffer is encountered, it must
202 * be waited upon, and not just skipped over.
203 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800204int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
205 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int ret;
208 struct writeback_control wbc = {
209 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800210 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700211 .range_start = start,
212 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 };
214
215 if (!mapping_cap_writeback_dirty(mapping))
216 return 0;
217
218 ret = do_writepages(mapping, &wbc);
219 return ret;
220}
221
222static inline int __filemap_fdatawrite(struct address_space *mapping,
223 int sync_mode)
224{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700225 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228int filemap_fdatawrite(struct address_space *mapping)
229{
230 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
231}
232EXPORT_SYMBOL(filemap_fdatawrite);
233
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400234int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800235 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
238}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400239EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Randy Dunlap485bb992006-06-23 02:03:49 -0700241/**
242 * filemap_flush - mostly a non-blocking flush
243 * @mapping: target address_space
244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * This is a mostly non-blocking flush. Not suitable for data-integrity
246 * purposes - I/O may not be started against all dirty pages.
247 */
248int filemap_flush(struct address_space *mapping)
249{
250 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
251}
252EXPORT_SYMBOL(filemap_flush);
253
Randy Dunlap485bb992006-06-23 02:03:49 -0700254/**
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200255 * filemap_fdatawait_range - wait for writeback to complete
256 * @mapping: address space structure to wait for
257 * @start_byte: offset in bytes where the range starts
258 * @end_byte: offset in bytes where the range ends (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -0700259 *
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200260 * Walk the list of under-writeback pages of the given address space
261 * in the given range and wait for all of them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200263int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
264 loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200266 pgoff_t index = start_byte >> PAGE_CACHE_SHIFT;
267 pgoff_t end = end_byte >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct pagevec pvec;
269 int nr_pages;
270 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200272 if (end_byte < start_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274
275 pagevec_init(&pvec, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 while ((index <= end) &&
277 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
278 PAGECACHE_TAG_WRITEBACK,
279 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
280 unsigned i;
281
282 for (i = 0; i < nr_pages; i++) {
283 struct page *page = pvec.pages[i];
284
285 /* until radix tree lookup accepts end_index */
286 if (page->index > end)
287 continue;
288
289 wait_on_page_writeback(page);
Rik van Riel212260a2011-01-13 15:46:06 -0800290 if (TestClearPageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ret = -EIO;
292 }
293 pagevec_release(&pvec);
294 cond_resched();
295 }
296
297 /* Check for outstanding write errors */
298 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
299 ret = -ENOSPC;
300 if (test_and_clear_bit(AS_EIO, &mapping->flags))
301 ret = -EIO;
302
303 return ret;
304}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200305EXPORT_SYMBOL(filemap_fdatawait_range);
306
307/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700308 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700310 *
311 * Walk the list of under-writeback pages of the given address space
312 * and wait for all of them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314int filemap_fdatawait(struct address_space *mapping)
315{
316 loff_t i_size = i_size_read(mapping->host);
317
318 if (i_size == 0)
319 return 0;
320
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200321 return filemap_fdatawait_range(mapping, 0, i_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323EXPORT_SYMBOL(filemap_fdatawait);
324
325int filemap_write_and_wait(struct address_space *mapping)
326{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800327 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800330 err = filemap_fdatawrite(mapping);
331 /*
332 * Even if the above returned error, the pages may be
333 * written partially (e.g. -ENOSPC), so we wait for it.
334 * But the -EIO is special case, it may indicate the worst
335 * thing (e.g. bug) happened, so we avoid waiting for it.
336 */
337 if (err != -EIO) {
338 int err2 = filemap_fdatawait(mapping);
339 if (!err)
340 err = err2;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800345EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Randy Dunlap485bb992006-06-23 02:03:49 -0700347/**
348 * filemap_write_and_wait_range - write out & wait on a file range
349 * @mapping: the address_space for the pages
350 * @lstart: offset in bytes where the range starts
351 * @lend: offset in bytes where the range ends (inclusive)
352 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800353 * Write out and wait upon file offsets lstart->lend, inclusive.
354 *
355 * Note that `lend' is inclusive (describes the last byte to be written) so
356 * that this function can be used to write to the very end-of-file (end = -1).
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358int filemap_write_and_wait_range(struct address_space *mapping,
359 loff_t lstart, loff_t lend)
360{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800361 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800364 err = __filemap_fdatawrite_range(mapping, lstart, lend,
365 WB_SYNC_ALL);
366 /* See comment of filemap_write_and_wait() */
367 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200368 int err2 = filemap_fdatawait_range(mapping,
369 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800370 if (!err)
371 err = err2;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Chris Masonf6995582009-04-15 13:22:37 -0400376EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Randy Dunlap485bb992006-06-23 02:03:49 -0700378/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700379 * replace_page_cache_page - replace a pagecache page with a new one
380 * @old: page to be replaced
381 * @new: page to replace with
382 * @gfp_mask: allocation mode
383 *
384 * This function replaces a page in the pagecache with a new one. On
385 * success it acquires the pagecache reference for the new page and
386 * drops it for the old page. Both the old and new pages must be
387 * locked. This function does not add the new page to the LRU, the
388 * caller must do that.
389 *
390 * The remove + add is atomic. The only way this function can fail is
391 * memory allocation failure.
392 */
393int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
394{
395 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700396
397 VM_BUG_ON(!PageLocked(old));
398 VM_BUG_ON(!PageLocked(new));
399 VM_BUG_ON(new->mapping);
400
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700401 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
402 if (!error) {
403 struct address_space *mapping = old->mapping;
404 void (*freepage)(struct page *);
405
406 pgoff_t offset = old->index;
407 freepage = mapping->a_ops->freepage;
408
409 page_cache_get(new);
410 new->mapping = mapping;
411 new->index = offset;
412
413 spin_lock_irq(&mapping->tree_lock);
Minchan Kime64a7822011-03-22 16:32:44 -0700414 __delete_from_page_cache(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700415 error = radix_tree_insert(&mapping->page_tree, offset, new);
416 BUG_ON(error);
417 mapping->nrpages++;
418 __inc_zone_page_state(new, NR_FILE_PAGES);
419 if (PageSwapBacked(new))
420 __inc_zone_page_state(new, NR_SHMEM);
421 spin_unlock_irq(&mapping->tree_lock);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800422 /* mem_cgroup codes must not be called under tree_lock */
423 mem_cgroup_replace_page_cache(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700424 radix_tree_preload_end();
425 if (freepage)
426 freepage(old);
427 page_cache_release(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700428 }
429
430 return error;
431}
432EXPORT_SYMBOL_GPL(replace_page_cache_page);
433
434/**
Nick Piggine2867812008-07-25 19:45:30 -0700435 * add_to_page_cache_locked - add a locked page to the pagecache
Randy Dunlap485bb992006-06-23 02:03:49 -0700436 * @page: page to add
437 * @mapping: the page's address_space
438 * @offset: page index
439 * @gfp_mask: page allocation mode
440 *
Nick Piggine2867812008-07-25 19:45:30 -0700441 * This function is used to add a page to the pagecache. It must be locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * This function does not add the page to the LRU. The caller must do that.
443 */
Nick Piggine2867812008-07-25 19:45:30 -0700444int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400445 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Nick Piggine2867812008-07-25 19:45:30 -0700447 int error;
448
449 VM_BUG_ON(!PageLocked(page));
Hugh Dickins31475dd2011-08-03 16:21:27 -0700450 VM_BUG_ON(PageSwapBacked(page));
Nick Piggine2867812008-07-25 19:45:30 -0700451
452 error = mem_cgroup_cache_charge(page, current->mm,
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -0800453 gfp_mask & GFP_RECLAIM_MASK);
Balbir Singh35c754d2008-02-07 00:14:05 -0800454 if (error)
455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Balbir Singh35c754d2008-02-07 00:14:05 -0800457 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (error == 0) {
Nick Piggine2867812008-07-25 19:45:30 -0700459 page_cache_get(page);
460 page->mapping = mapping;
461 page->index = offset;
462
Nick Piggin19fd6232008-07-25 19:45:32 -0700463 spin_lock_irq(&mapping->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 error = radix_tree_insert(&mapping->page_tree, offset, page);
Nick Piggine2867812008-07-25 19:45:30 -0700465 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 mapping->nrpages++;
Christoph Lameter347ce432006-06-30 01:55:35 -0700467 __inc_zone_page_state(page, NR_FILE_PAGES);
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700468 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700469 } else {
470 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700471 /* Leave page->index set: truncation relies upon it */
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700472 spin_unlock_irq(&mapping->tree_lock);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700473 mem_cgroup_uncharge_cache_page(page);
Nick Piggine2867812008-07-25 19:45:30 -0700474 page_cache_release(page);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 radix_tree_preload_end();
Balbir Singh35c754d2008-02-07 00:14:05 -0800477 } else
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700478 mem_cgroup_uncharge_cache_page(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800479out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return error;
481}
Nick Piggine2867812008-07-25 19:45:30 -0700482EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400485 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700487 int ret;
488
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700489 ret = add_to_page_cache(page, mapping, offset, gfp_mask);
Hugh Dickins31475dd2011-08-03 16:21:27 -0700490 if (ret == 0)
491 lru_cache_add_file(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return ret;
493}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300494EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Paul Jackson44110fe2006-03-24 03:16:04 -0800496#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700497struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800498{
Miao Xiec0ff7452010-05-24 14:32:08 -0700499 int n;
500 struct page *page;
501
Paul Jackson44110fe2006-03-24 03:16:04 -0800502 if (cpuset_do_page_mem_spread()) {
Miao Xiec0ff7452010-05-24 14:32:08 -0700503 get_mems_allowed();
504 n = cpuset_mem_spread_node();
505 page = alloc_pages_exact_node(n, gfp, 0);
506 put_mems_allowed();
507 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800508 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700509 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800510}
Nick Piggin2ae88142006-10-28 10:38:23 -0700511EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800512#endif
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
515 * In order to wait for pages to become available there must be
516 * waitqueues associated with pages. By using a hash table of
517 * waitqueues where the bucket discipline is to maintain all
518 * waiters on the same queue and wake all when any of the pages
519 * become available, and for the woken contexts to check to be
520 * sure the appropriate page became available, this saves space
521 * at a cost of "thundering herd" phenomena during rare hash
522 * collisions.
523 */
524static wait_queue_head_t *page_waitqueue(struct page *page)
525{
526 const struct zone *zone = page_zone(page);
527
528 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
529}
530
531static inline void wake_up_page(struct page *page, int bit)
532{
533 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
534}
535
Harvey Harrison920c7a52008-02-04 22:29:26 -0800536void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
539
540 if (test_bit(bit_nr, &page->flags))
Jens Axboe7eaceac2011-03-10 08:52:07 +0100541 __wait_on_bit(page_waitqueue(page), &wait, sleep_on_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 TASK_UNINTERRUPTIBLE);
543}
544EXPORT_SYMBOL(wait_on_page_bit);
545
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -0700546int wait_on_page_bit_killable(struct page *page, int bit_nr)
547{
548 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
549
550 if (!test_bit(bit_nr, &page->flags))
551 return 0;
552
553 return __wait_on_bit(page_waitqueue(page), &wait,
554 sleep_on_page_killable, TASK_KILLABLE);
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
David Howells385e1ca5f2009-04-03 16:42:39 +0100558 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -0700559 * @page: Page defining the wait queue of interest
560 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +0100561 *
562 * Add an arbitrary @waiter to the wait queue for the nominated @page.
563 */
564void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
565{
566 wait_queue_head_t *q = page_waitqueue(page);
567 unsigned long flags;
568
569 spin_lock_irqsave(&q->lock, flags);
570 __add_wait_queue(q, waiter);
571 spin_unlock_irqrestore(&q->lock, flags);
572}
573EXPORT_SYMBOL_GPL(add_page_wait_queue);
574
575/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700576 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * @page: the page
578 *
579 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
580 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
581 * mechananism between PageLocked pages and PageWriteback pages is shared.
582 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
583 *
Nick Piggin8413ac92008-10-18 20:26:59 -0700584 * The mb is necessary to enforce ordering between the clear_bit and the read
585 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800587void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Nick Piggin8413ac92008-10-18 20:26:59 -0700589 VM_BUG_ON(!PageLocked(page));
590 clear_bit_unlock(PG_locked, &page->flags);
591 smp_mb__after_clear_bit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 wake_up_page(page, PG_locked);
593}
594EXPORT_SYMBOL(unlock_page);
595
Randy Dunlap485bb992006-06-23 02:03:49 -0700596/**
597 * end_page_writeback - end writeback against a page
598 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
600void end_page_writeback(struct page *page)
601{
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700602 if (TestClearPageReclaim(page))
603 rotate_reclaimable_page(page);
604
605 if (!test_clear_page_writeback(page))
606 BUG();
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 smp_mb__after_clear_bit();
609 wake_up_page(page, PG_writeback);
610}
611EXPORT_SYMBOL(end_page_writeback);
612
Randy Dunlap485bb992006-06-23 02:03:49 -0700613/**
614 * __lock_page - get a lock on the page, assuming we need to sleep to get it
615 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800617void __lock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
620
Jens Axboe7eaceac2011-03-10 08:52:07 +0100621 __wait_on_bit_lock(page_waitqueue(page), &wait, sleep_on_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 TASK_UNINTERRUPTIBLE);
623}
624EXPORT_SYMBOL(__lock_page);
625
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800626int __lock_page_killable(struct page *page)
Matthew Wilcox2687a352007-12-06 11:18:49 -0500627{
628 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
629
630 return __wait_on_bit_lock(page_waitqueue(page), &wait,
Jens Axboe7eaceac2011-03-10 08:52:07 +0100631 sleep_on_page_killable, TASK_KILLABLE);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500632}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300633EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -0500634
Michel Lespinassed065bd82010-10-26 14:21:57 -0700635int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
636 unsigned int flags)
637{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700638 if (flags & FAULT_FLAG_ALLOW_RETRY) {
639 /*
640 * CAUTION! In this case, mmap_sem is not released
641 * even though return 0.
642 */
643 if (flags & FAULT_FLAG_RETRY_NOWAIT)
644 return 0;
645
646 up_read(&mm->mmap_sem);
647 if (flags & FAULT_FLAG_KILLABLE)
648 wait_on_page_locked_killable(page);
649 else
Gleb Natapov318b2752011-03-22 16:30:51 -0700650 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -0700651 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -0700652 } else {
653 if (flags & FAULT_FLAG_KILLABLE) {
654 int ret;
655
656 ret = __lock_page_killable(page);
657 if (ret) {
658 up_read(&mm->mmap_sem);
659 return 0;
660 }
661 } else
662 __lock_page(page);
663 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -0700664 }
665}
666
Randy Dunlap485bb992006-06-23 02:03:49 -0700667/**
668 * find_get_page - find and get a page reference
669 * @mapping: the address_space to search
670 * @offset: the page index
671 *
Nick Pigginda6052f2006-09-25 23:31:35 -0700672 * Is there a pagecache struct page at the given (mapping, offset) tuple?
673 * If yes, increment its refcount and return it; if no, return NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Nick Piggina60637c2008-07-25 19:45:31 -0700675struct page *find_get_page(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Nick Piggina60637c2008-07-25 19:45:31 -0700677 void **pagep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct page *page;
679
Nick Piggina60637c2008-07-25 19:45:31 -0700680 rcu_read_lock();
681repeat:
682 page = NULL;
683 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
684 if (pagep) {
685 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -0800686 if (unlikely(!page))
687 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700688 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700689 if (radix_tree_deref_retry(page))
690 goto repeat;
691 /*
692 * Otherwise, shmem/tmpfs must be storing a swap entry
693 * here as an exceptional entry: so return it without
694 * attempting to raise page count.
695 */
696 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700697 }
Nick Piggina60637c2008-07-25 19:45:31 -0700698 if (!page_cache_get_speculative(page))
699 goto repeat;
700
701 /*
702 * Has the page moved?
703 * This is part of the lockless pagecache protocol. See
704 * include/linux/pagemap.h for details.
705 */
706 if (unlikely(page != *pagep)) {
707 page_cache_release(page);
708 goto repeat;
709 }
710 }
Nick Piggin27d20fd2010-11-11 14:05:19 -0800711out:
Nick Piggina60637c2008-07-25 19:45:31 -0700712 rcu_read_unlock();
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return page;
715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716EXPORT_SYMBOL(find_get_page);
717
Randy Dunlap485bb992006-06-23 02:03:49 -0700718/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * find_lock_page - locate, pin and lock a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -0700720 * @mapping: the address_space to search
721 * @offset: the page index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 *
723 * Locates the desired pagecache page, locks it, increments its reference
724 * count and returns its address.
725 *
726 * Returns zero if the page was not present. find_lock_page() may sleep.
727 */
Nick Piggina60637c2008-07-25 19:45:31 -0700728struct page *find_lock_page(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct page *page;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732repeat:
Nick Piggina60637c2008-07-25 19:45:31 -0700733 page = find_get_page(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700734 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -0700735 lock_page(page);
736 /* Has the page been truncated? */
737 if (unlikely(page->mapping != mapping)) {
738 unlock_page(page);
739 page_cache_release(page);
740 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Nick Piggina60637c2008-07-25 19:45:31 -0700742 VM_BUG_ON(page->index != offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return page;
745}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746EXPORT_SYMBOL(find_lock_page);
747
748/**
749 * find_or_create_page - locate or add a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -0700750 * @mapping: the page's address_space
751 * @index: the page's index into the mapping
752 * @gfp_mask: page allocation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *
754 * Locates a page in the pagecache. If the page is not present, a new page
755 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
756 * LRU list. The returned page is locked and has its reference count
757 * incremented.
758 *
759 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
760 * allocation!
761 *
762 * find_or_create_page() returns the desired page's address, or zero on
763 * memory exhaustion.
764 */
765struct page *find_or_create_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700766 pgoff_t index, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Nick Piggineb2be182007-10-16 01:24:57 -0700768 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int err;
770repeat:
771 page = find_lock_page(mapping, index);
772 if (!page) {
Nick Piggineb2be182007-10-16 01:24:57 -0700773 page = __page_cache_alloc(gfp_mask);
774 if (!page)
775 return NULL;
Nick Piggin67d58ac2009-01-06 14:40:28 -0800776 /*
777 * We want a regular kernel memory (not highmem or DMA etc)
778 * allocation for the radix tree nodes, but we need to honour
779 * the context-specific requirements the caller has asked for.
780 * GFP_RECLAIM_MASK collects those requirements.
781 */
782 err = add_to_page_cache_lru(page, mapping, index,
783 (gfp_mask & GFP_RECLAIM_MASK));
Nick Piggineb2be182007-10-16 01:24:57 -0700784 if (unlikely(err)) {
785 page_cache_release(page);
786 page = NULL;
787 if (err == -EEXIST)
788 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return page;
792}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793EXPORT_SYMBOL(find_or_create_page);
794
795/**
796 * find_get_pages - gang pagecache lookup
797 * @mapping: The address_space to search
798 * @start: The starting page index
799 * @nr_pages: The maximum number of pages
800 * @pages: Where the resulting pages are placed
801 *
802 * find_get_pages() will search for and return a group of up to
803 * @nr_pages pages in the mapping. The pages are placed at @pages.
804 * find_get_pages() takes a reference against the returned pages.
805 *
806 * The search returns a group of mapping-contiguous pages with ascending
807 * indexes. There may be holes in the indices due to not-present pages.
808 *
809 * find_get_pages() returns the number of pages which were found.
810 */
811unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
812 unsigned int nr_pages, struct page **pages)
813{
814 unsigned int i;
815 unsigned int ret;
Shaohua Licc39c6a2011-09-15 08:45:19 +0800816 unsigned int nr_found, nr_skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Nick Piggina60637c2008-07-25 19:45:31 -0700818 rcu_read_lock();
819restart:
820 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
Hugh Dickins63286502011-08-03 16:21:18 -0700821 (void ***)pages, NULL, start, nr_pages);
Nick Piggina60637c2008-07-25 19:45:31 -0700822 ret = 0;
Shaohua Licc39c6a2011-09-15 08:45:19 +0800823 nr_skip = 0;
Nick Piggina60637c2008-07-25 19:45:31 -0700824 for (i = 0; i < nr_found; i++) {
825 struct page *page;
826repeat:
827 page = radix_tree_deref_slot((void **)pages[i]);
828 if (unlikely(!page))
829 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -0700830
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700831 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700832 if (radix_tree_deref_retry(page)) {
833 /*
834 * Transient condition which can only trigger
835 * when entry at index 0 moves out of or back
836 * to root: none yet gotten, safe to restart.
837 */
838 WARN_ON(start | i);
839 goto restart;
840 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700841 /*
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700842 * Otherwise, shmem/tmpfs must be storing a swap entry
843 * here as an exceptional entry: so skip over it -
844 * we only reach this from invalidate_mapping_pages().
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700845 */
Shaohua Licc39c6a2011-09-15 08:45:19 +0800846 nr_skip++;
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700847 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800848 }
Nick Piggina60637c2008-07-25 19:45:31 -0700849
850 if (!page_cache_get_speculative(page))
851 goto repeat;
852
853 /* Has the page moved? */
854 if (unlikely(page != *((void **)pages[i]))) {
855 page_cache_release(page);
856 goto repeat;
857 }
858
859 pages[ret] = page;
860 ret++;
861 }
Hugh Dickins5b280c02011-03-22 16:33:07 -0700862
863 /*
864 * If all entries were removed before we could secure them,
865 * try again, because callers stop trying once 0 is returned.
866 */
Shaohua Licc39c6a2011-09-15 08:45:19 +0800867 if (unlikely(!ret && nr_found > nr_skip))
Hugh Dickins5b280c02011-03-22 16:33:07 -0700868 goto restart;
Nick Piggina60637c2008-07-25 19:45:31 -0700869 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return ret;
871}
872
Jens Axboeebf43502006-04-27 08:46:01 +0200873/**
874 * find_get_pages_contig - gang contiguous pagecache lookup
875 * @mapping: The address_space to search
876 * @index: The starting page index
877 * @nr_pages: The maximum number of pages
878 * @pages: Where the resulting pages are placed
879 *
880 * find_get_pages_contig() works exactly like find_get_pages(), except
881 * that the returned number of pages are guaranteed to be contiguous.
882 *
883 * find_get_pages_contig() returns the number of pages which were found.
884 */
885unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
886 unsigned int nr_pages, struct page **pages)
887{
888 unsigned int i;
889 unsigned int ret;
Nick Piggina60637c2008-07-25 19:45:31 -0700890 unsigned int nr_found;
Jens Axboeebf43502006-04-27 08:46:01 +0200891
Nick Piggina60637c2008-07-25 19:45:31 -0700892 rcu_read_lock();
893restart:
894 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
Hugh Dickins63286502011-08-03 16:21:18 -0700895 (void ***)pages, NULL, index, nr_pages);
Nick Piggina60637c2008-07-25 19:45:31 -0700896 ret = 0;
897 for (i = 0; i < nr_found; i++) {
898 struct page *page;
899repeat:
900 page = radix_tree_deref_slot((void **)pages[i]);
901 if (unlikely(!page))
902 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -0700903
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700904 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700905 if (radix_tree_deref_retry(page)) {
906 /*
907 * Transient condition which can only trigger
908 * when entry at index 0 moves out of or back
909 * to root: none yet gotten, safe to restart.
910 */
911 goto restart;
912 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700913 /*
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700914 * Otherwise, shmem/tmpfs must be storing a swap entry
915 * here as an exceptional entry: so stop looking for
916 * contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700917 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700918 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700919 }
Nick Piggina60637c2008-07-25 19:45:31 -0700920
Nick Piggina60637c2008-07-25 19:45:31 -0700921 if (!page_cache_get_speculative(page))
922 goto repeat;
923
924 /* Has the page moved? */
925 if (unlikely(page != *((void **)pages[i]))) {
926 page_cache_release(page);
927 goto repeat;
928 }
929
Nick Piggin9cbb4cb2011-01-13 15:45:51 -0800930 /*
931 * must check mapping and index after taking the ref.
932 * otherwise we can get both false positives and false
933 * negatives, which is just confusing to the caller.
934 */
935 if (page->mapping == NULL || page->index != index) {
936 page_cache_release(page);
937 break;
938 }
939
Nick Piggina60637c2008-07-25 19:45:31 -0700940 pages[ret] = page;
941 ret++;
Jens Axboeebf43502006-04-27 08:46:01 +0200942 index++;
943 }
Nick Piggina60637c2008-07-25 19:45:31 -0700944 rcu_read_unlock();
945 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +0200946}
David Howellsef71c152007-05-09 02:33:44 -0700947EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +0200948
Randy Dunlap485bb992006-06-23 02:03:49 -0700949/**
950 * find_get_pages_tag - find and return pages that match @tag
951 * @mapping: the address_space to search
952 * @index: the starting page index
953 * @tag: the tag index
954 * @nr_pages: the maximum number of pages
955 * @pages: where the resulting pages are placed
956 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -0700958 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
960unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
961 int tag, unsigned int nr_pages, struct page **pages)
962{
963 unsigned int i;
964 unsigned int ret;
Nick Piggina60637c2008-07-25 19:45:31 -0700965 unsigned int nr_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Nick Piggina60637c2008-07-25 19:45:31 -0700967 rcu_read_lock();
968restart:
969 nr_found = radix_tree_gang_lookup_tag_slot(&mapping->page_tree,
970 (void ***)pages, *index, nr_pages, tag);
971 ret = 0;
972 for (i = 0; i < nr_found; i++) {
973 struct page *page;
974repeat:
975 page = radix_tree_deref_slot((void **)pages[i]);
976 if (unlikely(!page))
977 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -0700978
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700979 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700980 if (radix_tree_deref_retry(page)) {
981 /*
982 * Transient condition which can only trigger
983 * when entry at index 0 moves out of or back
984 * to root: none yet gotten, safe to restart.
985 */
986 goto restart;
987 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700988 /*
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700989 * This function is never used on a shmem/tmpfs
990 * mapping, so a swap entry won't be found here.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700991 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700992 BUG();
Hugh Dickinsa2c16d62011-08-03 16:21:19 -0700993 }
Nick Piggina60637c2008-07-25 19:45:31 -0700994
995 if (!page_cache_get_speculative(page))
996 goto repeat;
997
998 /* Has the page moved? */
999 if (unlikely(page != *((void **)pages[i]))) {
1000 page_cache_release(page);
1001 goto repeat;
1002 }
1003
1004 pages[ret] = page;
1005 ret++;
1006 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001007
1008 /*
1009 * If all entries were removed before we could secure them,
1010 * try again, because callers stop trying once 0 is returned.
1011 */
1012 if (unlikely(!ret && nr_found))
1013 goto restart;
Nick Piggina60637c2008-07-25 19:45:31 -07001014 rcu_read_unlock();
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (ret)
1017 *index = pages[ret - 1]->index + 1;
Nick Piggina60637c2008-07-25 19:45:31 -07001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return ret;
1020}
David Howellsef71c152007-05-09 02:33:44 -07001021EXPORT_SYMBOL(find_get_pages_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Randy Dunlap485bb992006-06-23 02:03:49 -07001023/**
1024 * grab_cache_page_nowait - returns locked page at given index in given cache
1025 * @mapping: target address_space
1026 * @index: the page index
1027 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001028 * Same as grab_cache_page(), but do not wait if the page is unavailable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * This is intended for speculative data generators, where the data can
1030 * be regenerated if the page couldn't be grabbed. This routine should
1031 * be safe to call while holding the lock for another page.
1032 *
1033 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
1034 * and deadlock against the caller's locked page.
1035 */
1036struct page *
Fengguang Wu57f6b962007-10-16 01:24:37 -07001037grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct page *page = find_get_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (page) {
Nick Piggin529ae9a2008-08-02 12:01:03 +02001042 if (trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return page;
1044 page_cache_release(page);
1045 return NULL;
1046 }
Nick Piggin2ae88142006-10-28 10:38:23 -07001047 page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
Nick Piggin67d58ac2009-01-06 14:40:28 -08001048 if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 page_cache_release(page);
1050 page = NULL;
1051 }
1052 return page;
1053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054EXPORT_SYMBOL(grab_cache_page_nowait);
1055
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001056/*
1057 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1058 * a _large_ part of the i/o request. Imagine the worst scenario:
1059 *
1060 * ---R__________________________________________B__________
1061 * ^ reading here ^ bad block(assume 4k)
1062 *
1063 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1064 * => failing the whole request => read(R) => read(R+1) =>
1065 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1066 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1067 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1068 *
1069 * It is going insane. Fix it by quickly scaling down the readahead size.
1070 */
1071static void shrink_readahead_size_eio(struct file *filp,
1072 struct file_ra_state *ra)
1073{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001074 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001075}
1076
Randy Dunlap485bb992006-06-23 02:03:49 -07001077/**
Christoph Hellwig36e78912008-02-08 04:21:24 -08001078 * do_generic_file_read - generic file read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001079 * @filp: the file to read
1080 * @ppos: current file position
1081 * @desc: read_descriptor
1082 * @actor: read method
1083 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001085 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
1087 * This is really ugly. But the goto's actually try to clarify some
1088 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
Christoph Hellwig36e78912008-02-08 04:21:24 -08001090static void do_generic_file_read(struct file *filp, loff_t *ppos,
1091 read_descriptor_t *desc, read_actor_t actor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Christoph Hellwig36e78912008-02-08 04:21:24 -08001093 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001095 struct file_ra_state *ra = &filp->f_ra;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001096 pgoff_t index;
1097 pgoff_t last_index;
1098 pgoff_t prev_index;
1099 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001100 unsigned int prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 index = *ppos >> PAGE_CACHE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07001104 prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
1105 prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
1107 offset = *ppos & ~PAGE_CACHE_MASK;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 for (;;) {
1110 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001111 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001112 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 unsigned long nr, ret;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116find_page:
1117 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001118 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001119 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001120 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001121 index, last_index - index);
1122 page = find_get_page(mapping, index);
1123 if (unlikely(page == NULL))
1124 goto no_cached_page;
1125 }
1126 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001127 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001128 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001129 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001131 if (!PageUptodate(page)) {
1132 if (inode->i_blkbits == PAGE_CACHE_SHIFT ||
1133 !mapping->a_ops->is_partially_uptodate)
1134 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02001135 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001136 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08001137 /* Did it get truncated before we got the lock? */
1138 if (!page->mapping)
1139 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001140 if (!mapping->a_ops->is_partially_uptodate(page,
1141 desc, offset))
1142 goto page_not_up_to_date_locked;
1143 unlock_page(page);
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07001146 /*
1147 * i_size must be checked after we know the page is Uptodate.
1148 *
1149 * Checking i_size after the check allows us to calculate
1150 * the correct value for "nr", which means the zero-filled
1151 * part of the page is not copied back to userspace (unless
1152 * another truncate extends the file - this is desired though).
1153 */
1154
1155 isize = i_size_read(inode);
1156 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1157 if (unlikely(!isize || index > end_index)) {
1158 page_cache_release(page);
1159 goto out;
1160 }
1161
1162 /* nr is the maximum number of bytes to copy from this page */
1163 nr = PAGE_CACHE_SIZE;
1164 if (index == end_index) {
1165 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1166 if (nr <= offset) {
1167 page_cache_release(page);
1168 goto out;
1169 }
1170 }
1171 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /* If users can be writing to this page using arbitrary
1174 * virtual addresses, take care about potential aliasing
1175 * before reading the page on the kernel side.
1176 */
1177 if (mapping_writably_mapped(mapping))
1178 flush_dcache_page(page);
1179
1180 /*
Jan Karaec0f1632007-05-06 14:49:25 -07001181 * When a sequential read accesses a page several times,
1182 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Jan Karaec0f1632007-05-06 14:49:25 -07001184 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 mark_page_accessed(page);
1186 prev_index = index;
1187
1188 /*
1189 * Ok, we have the page, and it's up-to-date, so
1190 * now we can copy it to user space...
1191 *
1192 * The actor routine returns how many bytes were actually used..
1193 * NOTE! This may not be the same as how much of a user buffer
1194 * we filled up (we may be padding etc), so we can only update
1195 * "pos" here (the actor routine has to update the user buffer
1196 * pointers and the remaining count).
1197 */
1198 ret = actor(desc, page, offset, nr);
1199 offset += ret;
1200 index += offset >> PAGE_CACHE_SHIFT;
1201 offset &= ~PAGE_CACHE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07001202 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 page_cache_release(page);
1205 if (ret == nr && desc->count)
1206 continue;
1207 goto out;
1208
1209page_not_up_to_date:
1210 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04001211 error = lock_page_killable(page);
1212 if (unlikely(error))
1213 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001215page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07001216 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!page->mapping) {
1218 unlock_page(page);
1219 page_cache_release(page);
1220 continue;
1221 }
1222
1223 /* Did somebody else fill it already? */
1224 if (PageUptodate(page)) {
1225 unlock_page(page);
1226 goto page_ok;
1227 }
1228
1229readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04001230 /*
1231 * A previous I/O error may have been due to temporary
1232 * failures, eg. multipath errors.
1233 * PG_error will be set again if readpage fails.
1234 */
1235 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* Start the actual read. The read will unlock the page. */
1237 error = mapping->a_ops->readpage(filp, page);
1238
Zach Brown994fc28c2005-12-15 14:28:17 -08001239 if (unlikely(error)) {
1240 if (error == AOP_TRUNCATED_PAGE) {
1241 page_cache_release(page);
1242 goto find_page;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04001248 error = lock_page_killable(page);
1249 if (unlikely(error))
1250 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (!PageUptodate(page)) {
1252 if (page->mapping == NULL) {
1253 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001254 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
1256 unlock_page(page);
1257 page_cache_release(page);
1258 goto find_page;
1259 }
1260 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07001261 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04001262 error = -EIO;
1263 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 unlock_page(page);
1266 }
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto page_ok;
1269
1270readpage_error:
1271 /* UHHUH! A synchronous read error occurred. Report it */
1272 desc->error = error;
1273 page_cache_release(page);
1274 goto out;
1275
1276no_cached_page:
1277 /*
1278 * Ok, it wasn't cached, so we need to create a new
1279 * page..
1280 */
Nick Piggineb2be182007-10-16 01:24:57 -07001281 page = page_cache_alloc_cold(mapping);
1282 if (!page) {
1283 desc->error = -ENOMEM;
1284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Nick Piggineb2be182007-10-16 01:24:57 -07001286 error = add_to_page_cache_lru(page, mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 index, GFP_KERNEL);
1288 if (error) {
Nick Piggineb2be182007-10-16 01:24:57 -07001289 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (error == -EEXIST)
1291 goto find_page;
1292 desc->error = error;
1293 goto out;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto readpage;
1296 }
1297
1298out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07001299 ra->prev_pos = prev_index;
1300 ra->prev_pos <<= PAGE_CACHE_SHIFT;
1301 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Fengguang Wuf4e6b492007-10-16 01:24:33 -07001303 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07001304 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307int file_read_actor(read_descriptor_t *desc, struct page *page,
1308 unsigned long offset, unsigned long size)
1309{
1310 char *kaddr;
1311 unsigned long left, count = desc->count;
1312
1313 if (size > count)
1314 size = count;
1315
1316 /*
1317 * Faults on the destination of a read are common, so do it before
1318 * taking the kmap.
1319 */
1320 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1321 kaddr = kmap_atomic(page, KM_USER0);
1322 left = __copy_to_user_inatomic(desc->arg.buf,
1323 kaddr + offset, size);
1324 kunmap_atomic(kaddr, KM_USER0);
1325 if (left == 0)
1326 goto success;
1327 }
1328
1329 /* Do it the slow way */
1330 kaddr = kmap(page);
1331 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1332 kunmap(page);
1333
1334 if (left) {
1335 size -= left;
1336 desc->error = -EFAULT;
1337 }
1338success:
1339 desc->count = count - size;
1340 desc->written += size;
1341 desc->arg.buf += size;
1342 return size;
1343}
1344
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07001345/*
1346 * Performs necessary checks before doing a write
1347 * @iov: io vector request
1348 * @nr_segs: number of segments in the iovec
1349 * @count: number of bytes to write
1350 * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1351 *
1352 * Adjust number of segments and amount of bytes to write (nr_segs should be
1353 * properly initialized first). Returns appropriate error code that caller
1354 * should return or zero in case that write should be allowed.
1355 */
1356int generic_segment_checks(const struct iovec *iov,
1357 unsigned long *nr_segs, size_t *count, int access_flags)
1358{
1359 unsigned long seg;
1360 size_t cnt = 0;
1361 for (seg = 0; seg < *nr_segs; seg++) {
1362 const struct iovec *iv = &iov[seg];
1363
1364 /*
1365 * If any segment has a negative length, or the cumulative
1366 * length ever wraps negative then return -EINVAL.
1367 */
1368 cnt += iv->iov_len;
1369 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1370 return -EINVAL;
1371 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1372 continue;
1373 if (seg == 0)
1374 return -EFAULT;
1375 *nr_segs = seg;
1376 cnt -= iv->iov_len; /* This segment is no good */
1377 break;
1378 }
1379 *count = cnt;
1380 return 0;
1381}
1382EXPORT_SYMBOL(generic_segment_checks);
1383
Randy Dunlap485bb992006-06-23 02:03:49 -07001384/**
Henrik Kretzschmarb2abacf2006-10-04 02:15:22 -07001385 * generic_file_aio_read - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001386 * @iocb: kernel I/O control block
1387 * @iov: io vector request
1388 * @nr_segs: number of segments in the iovec
Henrik Kretzschmarb2abacf2006-10-04 02:15:22 -07001389 * @pos: current file position
Randy Dunlap485bb992006-06-23 02:03:49 -07001390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * This is the "read()" routine for all filesystems
1392 * that can use the page cache directly.
1393 */
1394ssize_t
Badari Pulavarty543ade12006-09-30 23:28:48 -07001395generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1396 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct file *filp = iocb->ki_filp;
1399 ssize_t retval;
Josef Bacik66f998f2010-05-23 11:00:54 -04001400 unsigned long seg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 size_t count;
Badari Pulavarty543ade12006-09-30 23:28:48 -07001402 loff_t *ppos = &iocb->ki_pos;
Jens Axboe55602dd2010-06-24 15:05:37 +02001403 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 count = 0;
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07001406 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1407 if (retval)
1408 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Jens Axboe55602dd2010-06-24 15:05:37 +02001410 blk_start_plug(&plug);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1413 if (filp->f_flags & O_DIRECT) {
Badari Pulavarty543ade12006-09-30 23:28:48 -07001414 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct address_space *mapping;
1416 struct inode *inode;
1417
1418 mapping = filp->f_mapping;
1419 inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!count)
1421 goto out; /* skip atime */
1422 size = i_size_read(inode);
1423 if (pos < size) {
Nick Piggin48b47c52009-01-06 14:40:22 -08001424 retval = filemap_write_and_wait_range(mapping, pos,
1425 pos + iov_length(iov, nr_segs) - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07001426 if (!retval) {
1427 retval = mapping->a_ops->direct_IO(READ, iocb,
1428 iov, pos, nr_segs);
1429 }
Josef Bacik66f998f2010-05-23 11:00:54 -04001430 if (retval > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 *ppos = pos + retval;
Josef Bacik66f998f2010-05-23 11:00:54 -04001432 count -= retval;
1433 }
1434
1435 /*
1436 * Btrfs can have a short DIO read if we encounter
1437 * compressed extents, so if there was an error, or if
1438 * we've already read everything we wanted to, or if
1439 * there was a short read because we hit EOF, go ahead
1440 * and return. Otherwise fallthrough to buffered io for
1441 * the rest of the read.
1442 */
1443 if (retval < 0 || !count || *ppos >= size) {
Hugh Dickins11fa9772008-07-23 21:27:34 -07001444 file_accessed(filp);
1445 goto out;
1446 }
Steven Whitehouse0e0bcae2006-09-27 14:45:07 -04001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
Josef Bacik66f998f2010-05-23 11:00:54 -04001450 count = retval;
Hugh Dickins11fa9772008-07-23 21:27:34 -07001451 for (seg = 0; seg < nr_segs; seg++) {
1452 read_descriptor_t desc;
Josef Bacik66f998f2010-05-23 11:00:54 -04001453 loff_t offset = 0;
1454
1455 /*
1456 * If we did a short DIO read we need to skip the section of the
1457 * iov that we've already read data into.
1458 */
1459 if (count) {
1460 if (count > iov[seg].iov_len) {
1461 count -= iov[seg].iov_len;
1462 continue;
1463 }
1464 offset = count;
1465 count = 0;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Hugh Dickins11fa9772008-07-23 21:27:34 -07001468 desc.written = 0;
Josef Bacik66f998f2010-05-23 11:00:54 -04001469 desc.arg.buf = iov[seg].iov_base + offset;
1470 desc.count = iov[seg].iov_len - offset;
Hugh Dickins11fa9772008-07-23 21:27:34 -07001471 if (desc.count == 0)
1472 continue;
1473 desc.error = 0;
1474 do_generic_file_read(filp, ppos, &desc, file_read_actor);
1475 retval += desc.written;
1476 if (desc.error) {
1477 retval = retval ?: desc.error;
1478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
Hugh Dickins11fa9772008-07-23 21:27:34 -07001480 if (desc.count > 0)
1481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483out:
Jens Axboe55602dd2010-06-24 15:05:37 +02001484 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return retval;
1486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487EXPORT_SYMBOL(generic_file_aio_read);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static ssize_t
1490do_readahead(struct address_space *mapping, struct file *filp,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001491 pgoff_t index, unsigned long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1494 return -EINVAL;
1495
Wu Fengguangf7e839d2009-06-16 15:31:20 -07001496 force_page_cache_readahead(mapping, filp, index, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
Heiko Carstens6673e0c2009-01-14 14:14:02 +01001500SYSCALL_DEFINE(readahead)(int fd, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 ssize_t ret;
1503 struct file *file;
1504
1505 ret = -EBADF;
1506 file = fget(fd);
1507 if (file) {
1508 if (file->f_mode & FMODE_READ) {
1509 struct address_space *mapping = file->f_mapping;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001510 pgoff_t start = offset >> PAGE_CACHE_SHIFT;
1511 pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned long len = end - start + 1;
1513 ret = do_readahead(mapping, file, start, len);
1514 }
1515 fput(file);
1516 }
1517 return ret;
1518}
Heiko Carstens6673e0c2009-01-14 14:14:02 +01001519#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
1520asmlinkage long SyS_readahead(long fd, loff_t offset, long count)
1521{
1522 return SYSC_readahead((int) fd, offset, (size_t) count);
1523}
1524SYSCALL_ALIAS(sys_readahead, SyS_readahead);
1525#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001528/**
1529 * page_cache_read - adds requested page to the page cache if not already there
1530 * @file: file to read
1531 * @offset: page index
1532 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * This adds the requested page to the page cache if it isn't already there,
1534 * and schedules an I/O to read in its contents from disk.
1535 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001536static int page_cache_read(struct file *file, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct address_space *mapping = file->f_mapping;
1539 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001540 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Zach Brown994fc28c2005-12-15 14:28:17 -08001542 do {
1543 page = page_cache_alloc_cold(mapping);
1544 if (!page)
1545 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Zach Brown994fc28c2005-12-15 14:28:17 -08001547 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1548 if (ret == 0)
1549 ret = mapping->a_ops->readpage(file, page);
1550 else if (ret == -EEXIST)
1551 ret = 0; /* losing race to add is OK */
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Zach Brown994fc28c2005-12-15 14:28:17 -08001555 } while (ret == AOP_TRUNCATED_PAGE);
1556
1557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560#define MMAP_LOTSAMISS (100)
1561
Linus Torvaldsef00e082009-06-16 15:31:25 -07001562/*
1563 * Synchronous readahead happens when we don't even find
1564 * a page in the page cache at all.
1565 */
1566static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1567 struct file_ra_state *ra,
1568 struct file *file,
1569 pgoff_t offset)
1570{
1571 unsigned long ra_pages;
1572 struct address_space *mapping = file->f_mapping;
1573
1574 /* If we don't want any read-ahead, don't bother */
1575 if (VM_RandomReadHint(vma))
1576 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001577 if (!ra->ra_pages)
1578 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001579
Wu Fengguang2cbea1d2011-05-24 17:12:30 -07001580 if (VM_SequentialReadHint(vma)) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07001581 page_cache_sync_readahead(mapping, ra, file, offset,
1582 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001583 return;
1584 }
1585
Andi Kleen207d04b2011-05-24 17:12:29 -07001586 /* Avoid banging the cache line if not needed */
1587 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07001588 ra->mmap_miss++;
1589
1590 /*
1591 * Do we miss much more than hit in this file? If so,
1592 * stop bothering with read-ahead. It will only hurt.
1593 */
1594 if (ra->mmap_miss > MMAP_LOTSAMISS)
1595 return;
1596
Wu Fengguangd30a1102009-06-16 15:31:30 -07001597 /*
1598 * mmap read-around
1599 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001600 ra_pages = max_sane_readahead(ra->ra_pages);
Wu Fengguang275b12b2011-05-24 17:12:28 -07001601 ra->start = max_t(long, 0, offset - ra_pages / 2);
1602 ra->size = ra_pages;
Wu Fengguang2cbea1d2011-05-24 17:12:30 -07001603 ra->async_size = ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07001604 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001605}
1606
1607/*
1608 * Asynchronous readahead happens when we find the page and PG_readahead,
1609 * so we want to possibly extend the readahead further..
1610 */
1611static void do_async_mmap_readahead(struct vm_area_struct *vma,
1612 struct file_ra_state *ra,
1613 struct file *file,
1614 struct page *page,
1615 pgoff_t offset)
1616{
1617 struct address_space *mapping = file->f_mapping;
1618
1619 /* If we don't want any read-ahead, don't bother */
1620 if (VM_RandomReadHint(vma))
1621 return;
1622 if (ra->mmap_miss > 0)
1623 ra->mmap_miss--;
1624 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07001625 page_cache_async_readahead(mapping, ra, file,
1626 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001627}
1628
Randy Dunlap485bb992006-06-23 02:03:49 -07001629/**
Nick Piggin54cb8822007-07-19 01:46:59 -07001630 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07001631 * @vma: vma in which the fault was taken
1632 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07001633 *
Nick Piggin54cb8822007-07-19 01:46:59 -07001634 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * mapped memory region to read in file data during a page fault.
1636 *
1637 * The goto's are kind of ugly, but this streamlines the normal case of having
1638 * it in the page cache, and handles the special cases reasonably without
1639 * having a lot of duplicated code.
1640 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001641int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07001644 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct address_space *mapping = file->f_mapping;
1646 struct file_ra_state *ra = &file->f_ra;
1647 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001648 pgoff_t offset = vmf->pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct page *page;
Jan Kara2004dc82008-02-08 04:20:11 -08001650 pgoff_t size;
Nick Piggin83c54072007-07-19 01:47:05 -07001651 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001654 if (offset >= size)
Linus Torvalds5307cc12007-10-31 09:19:46 -07001655 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * Do we have something in the page cache already?
1659 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001660 page = find_get_page(mapping, offset);
1661 if (likely(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07001663 * We found the page, so try async readahead before
1664 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001666 do_async_mmap_readahead(vma, ra, file, page, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001667 } else {
1668 /* No page in the page cache at all */
1669 do_sync_mmap_readahead(vma, ra, file, offset);
1670 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07001671 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07001672 ret = VM_FAULT_MAJOR;
1673retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07001674 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (!page)
1676 goto no_cached_page;
1677 }
1678
Michel Lespinassed88c0922010-11-02 13:05:18 -07001679 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
1680 page_cache_release(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001681 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07001682 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07001683
1684 /* Did it get truncated? */
1685 if (unlikely(page->mapping != mapping)) {
1686 unlock_page(page);
1687 put_page(page);
1688 goto retry_find;
1689 }
1690 VM_BUG_ON(page->index != offset);
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
Nick Piggind00806b2007-07-19 01:46:57 -07001693 * We have a locked page in the page cache, now we need to check
1694 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Nick Piggind00806b2007-07-19 01:46:57 -07001696 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 goto page_not_uptodate;
1698
Linus Torvaldsef00e082009-06-16 15:31:25 -07001699 /*
1700 * Found the page and have a reference on it.
1701 * We must recheck i_size under page lock.
1702 */
Nick Piggind00806b2007-07-19 01:46:57 -07001703 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvaldsef00e082009-06-16 15:31:25 -07001704 if (unlikely(offset >= size)) {
Nick Piggind00806b2007-07-19 01:46:57 -07001705 unlock_page(page);
Yan Zheng745ad482007-10-08 10:08:37 -07001706 page_cache_release(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07001707 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07001708 }
1709
Nick Piggind0217ac2007-07-19 01:47:03 -07001710 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07001711 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713no_cached_page:
1714 /*
1715 * We're only likely to ever get here if MADV_RANDOM is in
1716 * effect.
1717 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07001718 error = page_cache_read(file, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /*
1721 * The page we want has now been added to the page cache.
1722 * In the unlikely event that someone removed it in the
1723 * meantime, we'll just come back here and read it again.
1724 */
1725 if (error >= 0)
1726 goto retry_find;
1727
1728 /*
1729 * An error return from page_cache_read can result if the
1730 * system is low on memory, or a problem occurs while trying
1731 * to schedule I/O.
1732 */
1733 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07001734 return VM_FAULT_OOM;
1735 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /*
1739 * Umm, take care of errors if the page isn't up-to-date.
1740 * Try to re-read it _once_. We do this synchronously,
1741 * because there really aren't any performance issues here
1742 * and we need to check for errors.
1743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08001745 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07001746 if (!error) {
1747 wait_on_page_locked(page);
1748 if (!PageUptodate(page))
1749 error = -EIO;
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 page_cache_release(page);
Nick Piggind00806b2007-07-19 01:46:57 -07001752
1753 if (!error || error == AOP_TRUNCATED_PAGE)
1754 goto retry_find;
1755
1756 /* Things didn't work out. Return zero to tell the mm layer so. */
1757 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07001758 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07001759}
1760EXPORT_SYMBOL(filemap_fault);
1761
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001762const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001763 .fault = filemap_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764};
1765
1766/* This is used for a general mmap of a disk file */
1767
1768int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1769{
1770 struct address_space *mapping = file->f_mapping;
1771
1772 if (!mapping->a_ops->readpage)
1773 return -ENOEXEC;
1774 file_accessed(file);
1775 vma->vm_ops = &generic_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001776 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return 0;
1778}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780/*
1781 * This is for filesystems which do not implement ->writepage.
1782 */
1783int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1784{
1785 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1786 return -EINVAL;
1787 return generic_file_mmap(file, vma);
1788}
1789#else
1790int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1791{
1792 return -ENOSYS;
1793}
1794int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1795{
1796 return -ENOSYS;
1797}
1798#endif /* CONFIG_MMU */
1799
1800EXPORT_SYMBOL(generic_file_mmap);
1801EXPORT_SYMBOL(generic_file_readonly_mmap);
1802
Nick Piggin6fe69002007-05-06 14:49:04 -07001803static struct page *__read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001804 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07001805 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001806 void *data,
1807 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Nick Piggineb2be182007-10-16 01:24:57 -07001809 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int err;
1811repeat:
1812 page = find_get_page(mapping, index);
1813 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001814 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07001815 if (!page)
1816 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06001817 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07001818 if (unlikely(err)) {
1819 page_cache_release(page);
1820 if (err == -EEXIST)
1821 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return ERR_PTR(err);
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 err = filler(data, page);
1826 if (err < 0) {
1827 page_cache_release(page);
1828 page = ERR_PTR(err);
1829 }
1830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return page;
1832}
1833
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001834static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001835 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07001836 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001837 void *data,
1838 gfp_t gfp)
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct page *page;
1842 int err;
1843
1844retry:
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001845 page = __read_cache_page(mapping, index, filler, data, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (IS_ERR(page))
David Howellsc855ff32007-05-09 13:42:20 +01001847 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (PageUptodate(page))
1849 goto out;
1850
1851 lock_page(page);
1852 if (!page->mapping) {
1853 unlock_page(page);
1854 page_cache_release(page);
1855 goto retry;
1856 }
1857 if (PageUptodate(page)) {
1858 unlock_page(page);
1859 goto out;
1860 }
1861 err = filler(data, page);
1862 if (err < 0) {
1863 page_cache_release(page);
David Howellsc855ff32007-05-09 13:42:20 +01001864 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
David Howellsc855ff32007-05-09 13:42:20 +01001866out:
Nick Piggin6fe69002007-05-06 14:49:04 -07001867 mark_page_accessed(page);
1868 return page;
1869}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001870
1871/**
1872 * read_cache_page_async - read into page cache, fill it if needed
1873 * @mapping: the page's address_space
1874 * @index: the page index
1875 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07001876 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001877 *
1878 * Same as read_cache_page, but don't wait for page to become unlocked
1879 * after submitting it to the filler.
1880 *
1881 * Read into the page cache. If a page already exists, and PageUptodate() is
1882 * not set, try to fill the page but don't wait for it to become unlocked.
1883 *
1884 * If the page does not get brought uptodate, return -EIO.
1885 */
1886struct page *read_cache_page_async(struct address_space *mapping,
1887 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07001888 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001889 void *data)
1890{
1891 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
1892}
Nick Piggin6fe69002007-05-06 14:49:04 -07001893EXPORT_SYMBOL(read_cache_page_async);
1894
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001895static struct page *wait_on_page_read(struct page *page)
1896{
1897 if (!IS_ERR(page)) {
1898 wait_on_page_locked(page);
1899 if (!PageUptodate(page)) {
1900 page_cache_release(page);
1901 page = ERR_PTR(-EIO);
1902 }
1903 }
1904 return page;
1905}
1906
1907/**
1908 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
1909 * @mapping: the page's address_space
1910 * @index: the page index
1911 * @gfp: the page allocator flags to use if allocating
1912 *
1913 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06001914 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001915 *
1916 * If the page does not get brought uptodate, return -EIO.
1917 */
1918struct page *read_cache_page_gfp(struct address_space *mapping,
1919 pgoff_t index,
1920 gfp_t gfp)
1921{
1922 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
1923
1924 return wait_on_page_read(do_read_cache_page(mapping, index, filler, NULL, gfp));
1925}
1926EXPORT_SYMBOL(read_cache_page_gfp);
1927
Nick Piggin6fe69002007-05-06 14:49:04 -07001928/**
1929 * read_cache_page - read into page cache, fill it if needed
1930 * @mapping: the page's address_space
1931 * @index: the page index
1932 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07001933 * @data: first arg to filler(data, page) function, often left as NULL
Nick Piggin6fe69002007-05-06 14:49:04 -07001934 *
1935 * Read into the page cache. If a page already exists, and PageUptodate() is
1936 * not set, try to fill the page then wait for it to become unlocked.
1937 *
1938 * If the page does not get brought uptodate, return -EIO.
1939 */
1940struct page *read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001941 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07001942 int (*filler)(void *, struct page *),
Nick Piggin6fe69002007-05-06 14:49:04 -07001943 void *data)
1944{
Linus Torvalds0531b2a2010-01-27 09:20:03 -08001945 return wait_on_page_read(read_cache_page_async(mapping, index, filler, data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947EXPORT_SYMBOL(read_cache_page);
1948
1949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 * The logic we want is
1951 *
1952 * if suid or (sgid and xgrp)
1953 * remove privs
1954 */
Jens Axboe01de85e2006-10-17 19:50:36 +02001955int should_remove_suid(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Al Viro649fc7b2011-07-26 04:31:05 -04001957 umode_t mode = dentry->d_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 int kill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* suid always must be killed */
1961 if (unlikely(mode & S_ISUID))
1962 kill = ATTR_KILL_SUID;
1963
1964 /*
1965 * sgid without any exec bits is just a mandatory locking mark; leave
1966 * it alone. If some exec bits are set, it's a real sgid; kill it.
1967 */
1968 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1969 kill |= ATTR_KILL_SGID;
1970
Dmitri Monakhov7f5ff762008-12-01 14:34:56 -08001971 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
Jens Axboe01de85e2006-10-17 19:50:36 +02001972 return kill;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Jens Axboe01de85e2006-10-17 19:50:36 +02001974 return 0;
1975}
Mark Fashehd23a1472006-10-17 17:05:18 -07001976EXPORT_SYMBOL(should_remove_suid);
Jens Axboe01de85e2006-10-17 19:50:36 +02001977
Miklos Szeredi7f3d4ee2008-05-07 09:22:39 +02001978static int __remove_suid(struct dentry *dentry, int kill)
Jens Axboe01de85e2006-10-17 19:50:36 +02001979{
1980 struct iattr newattrs;
1981
1982 newattrs.ia_valid = ATTR_FORCE | kill;
1983 return notify_change(dentry, &newattrs);
1984}
1985
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001986int file_remove_suid(struct file *file)
Jens Axboe01de85e2006-10-17 19:50:36 +02001987{
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001988 struct dentry *dentry = file->f_path.dentry;
Andi Kleen69b45732011-05-28 08:25:51 -07001989 struct inode *inode = dentry->d_inode;
1990 int killsuid;
1991 int killpriv;
Serge E. Hallynb5376772007-10-16 23:31:36 -07001992 int error = 0;
Jens Axboe01de85e2006-10-17 19:50:36 +02001993
Andi Kleen69b45732011-05-28 08:25:51 -07001994 /* Fast path for nothing security related */
1995 if (IS_NOSEC(inode))
1996 return 0;
1997
1998 killsuid = should_remove_suid(dentry);
1999 killpriv = security_inode_need_killpriv(dentry);
2000
Serge E. Hallynb5376772007-10-16 23:31:36 -07002001 if (killpriv < 0)
2002 return killpriv;
2003 if (killpriv)
2004 error = security_inode_killpriv(dentry);
2005 if (!error && killsuid)
2006 error = __remove_suid(dentry, killsuid);
Al Viro9e1f1de2011-06-03 18:24:58 -04002007 if (!error && (inode->i_sb->s_flags & MS_NOSEC))
Andi Kleen69b45732011-05-28 08:25:51 -07002008 inode->i_flags |= S_NOSEC;
Jens Axboe01de85e2006-10-17 19:50:36 +02002009
Serge E. Hallynb5376772007-10-16 23:31:36 -07002010 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02002012EXPORT_SYMBOL(file_remove_suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Nick Piggin2f718ff2007-10-16 01:24:59 -07002014static size_t __iovec_copy_from_user_inatomic(char *vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 const struct iovec *iov, size_t base, size_t bytes)
2016{
Ingo Molnarf1800532009-03-02 11:00:57 +01002017 size_t copied = 0, left = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 while (bytes) {
2020 char __user *buf = iov->iov_base + base;
2021 int copy = min(bytes, iov->iov_len - base);
2022
2023 base = 0;
Ingo Molnarf1800532009-03-02 11:00:57 +01002024 left = __copy_from_user_inatomic(vaddr, buf, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 copied += copy;
2026 bytes -= copy;
2027 vaddr += copy;
2028 iov++;
2029
NeilBrown01408c42006-06-25 05:47:58 -07002030 if (unlikely(left))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 return copied - left;
2034}
2035
2036/*
Nick Piggin2f718ff2007-10-16 01:24:59 -07002037 * Copy as much as we can into the page and return the number of bytes which
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002038 * were successfully copied. If a fault is encountered then return the number of
Nick Piggin2f718ff2007-10-16 01:24:59 -07002039 * bytes which were copied.
2040 */
2041size_t iov_iter_copy_from_user_atomic(struct page *page,
2042 struct iov_iter *i, unsigned long offset, size_t bytes)
2043{
2044 char *kaddr;
2045 size_t copied;
2046
2047 BUG_ON(!in_atomic());
2048 kaddr = kmap_atomic(page, KM_USER0);
2049 if (likely(i->nr_segs == 1)) {
2050 int left;
2051 char __user *buf = i->iov->iov_base + i->iov_offset;
Ingo Molnarf1800532009-03-02 11:00:57 +01002052 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002053 copied = bytes - left;
2054 } else {
2055 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2056 i->iov, i->iov_offset, bytes);
2057 }
2058 kunmap_atomic(kaddr, KM_USER0);
2059
2060 return copied;
2061}
Nick Piggin89e10782007-10-16 01:25:07 -07002062EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002063
2064/*
2065 * This has the same sideeffects and return value as
2066 * iov_iter_copy_from_user_atomic().
2067 * The difference is that it attempts to resolve faults.
2068 * Page must not be locked.
2069 */
2070size_t iov_iter_copy_from_user(struct page *page,
2071 struct iov_iter *i, unsigned long offset, size_t bytes)
2072{
2073 char *kaddr;
2074 size_t copied;
2075
2076 kaddr = kmap(page);
2077 if (likely(i->nr_segs == 1)) {
2078 int left;
2079 char __user *buf = i->iov->iov_base + i->iov_offset;
Ingo Molnarf1800532009-03-02 11:00:57 +01002080 left = __copy_from_user(kaddr + offset, buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002081 copied = bytes - left;
2082 } else {
2083 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2084 i->iov, i->iov_offset, bytes);
2085 }
2086 kunmap(page);
2087 return copied;
2088}
Nick Piggin89e10782007-10-16 01:25:07 -07002089EXPORT_SYMBOL(iov_iter_copy_from_user);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002090
Nick Pigginf7009262008-03-10 11:43:59 -07002091void iov_iter_advance(struct iov_iter *i, size_t bytes)
Nick Piggin2f718ff2007-10-16 01:24:59 -07002092{
Nick Pigginf7009262008-03-10 11:43:59 -07002093 BUG_ON(i->count < bytes);
2094
Nick Piggin2f718ff2007-10-16 01:24:59 -07002095 if (likely(i->nr_segs == 1)) {
2096 i->iov_offset += bytes;
Nick Pigginf7009262008-03-10 11:43:59 -07002097 i->count -= bytes;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002098 } else {
2099 const struct iovec *iov = i->iov;
2100 size_t base = i->iov_offset;
Jeff Layton39be79c2011-10-27 23:53:08 +02002101 unsigned long nr_segs = i->nr_segs;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002102
Nick Piggin124d3b72008-02-02 15:01:17 +01002103 /*
2104 * The !iov->iov_len check ensures we skip over unlikely
Nick Pigginf7009262008-03-10 11:43:59 -07002105 * zero-length segments (without overruning the iovec).
Nick Piggin124d3b72008-02-02 15:01:17 +01002106 */
Linus Torvalds94ad3742008-07-30 14:45:12 -07002107 while (bytes || unlikely(i->count && !iov->iov_len)) {
Nick Pigginf7009262008-03-10 11:43:59 -07002108 int copy;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002109
Nick Pigginf7009262008-03-10 11:43:59 -07002110 copy = min(bytes, iov->iov_len - base);
2111 BUG_ON(!i->count || i->count < copy);
2112 i->count -= copy;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002113 bytes -= copy;
2114 base += copy;
2115 if (iov->iov_len == base) {
2116 iov++;
Jeff Layton39be79c2011-10-27 23:53:08 +02002117 nr_segs--;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002118 base = 0;
2119 }
2120 }
2121 i->iov = iov;
2122 i->iov_offset = base;
Jeff Layton39be79c2011-10-27 23:53:08 +02002123 i->nr_segs = nr_segs;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002124 }
2125}
Nick Piggin89e10782007-10-16 01:25:07 -07002126EXPORT_SYMBOL(iov_iter_advance);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002127
Nick Pigginafddba42007-10-16 01:25:01 -07002128/*
2129 * Fault in the first iovec of the given iov_iter, to a maximum length
2130 * of bytes. Returns 0 on success, or non-zero if the memory could not be
2131 * accessed (ie. because it is an invalid address).
2132 *
2133 * writev-intensive code may want this to prefault several iovecs -- that
2134 * would be possible (callers must not rely on the fact that _only_ the
2135 * first iovec will be faulted with the current implementation).
2136 */
2137int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Nick Piggin2f718ff2007-10-16 01:24:59 -07002138{
Nick Piggin2f718ff2007-10-16 01:24:59 -07002139 char __user *buf = i->iov->iov_base + i->iov_offset;
Nick Pigginafddba42007-10-16 01:25:01 -07002140 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
2141 return fault_in_pages_readable(buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002142}
Nick Piggin89e10782007-10-16 01:25:07 -07002143EXPORT_SYMBOL(iov_iter_fault_in_readable);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002144
2145/*
2146 * Return the count of just the current iov_iter segment.
2147 */
2148size_t iov_iter_single_seg_count(struct iov_iter *i)
2149{
2150 const struct iovec *iov = i->iov;
2151 if (i->nr_segs == 1)
2152 return i->count;
2153 else
2154 return min(i->count, iov->iov_len - i->iov_offset);
2155}
Nick Piggin89e10782007-10-16 01:25:07 -07002156EXPORT_SYMBOL(iov_iter_single_seg_count);
Nick Piggin2f718ff2007-10-16 01:24:59 -07002157
2158/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 * Performs necessary checks before doing a write
2160 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002161 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 * Returns appropriate error code that caller should return or
2163 * zero in case that write should be allowed.
2164 */
2165inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
2166{
2167 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002168 unsigned long limit = rlimit(RLIMIT_FSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 if (unlikely(*pos < 0))
2171 return -EINVAL;
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (!isblk) {
2174 /* FIXME: this is for backwards compatibility with 2.4 */
2175 if (file->f_flags & O_APPEND)
2176 *pos = i_size_read(inode);
2177
2178 if (limit != RLIM_INFINITY) {
2179 if (*pos >= limit) {
2180 send_sig(SIGXFSZ, current, 0);
2181 return -EFBIG;
2182 }
2183 if (*count > limit - (typeof(limit))*pos) {
2184 *count = limit - (typeof(limit))*pos;
2185 }
2186 }
2187 }
2188
2189 /*
2190 * LFS rule
2191 */
2192 if (unlikely(*pos + *count > MAX_NON_LFS &&
2193 !(file->f_flags & O_LARGEFILE))) {
2194 if (*pos >= MAX_NON_LFS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return -EFBIG;
2196 }
2197 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
2198 *count = MAX_NON_LFS - (unsigned long)*pos;
2199 }
2200 }
2201
2202 /*
2203 * Are we about to exceed the fs block limit ?
2204 *
2205 * If we have written data it becomes a short write. If we have
2206 * exceeded without writing data we send a signal and return EFBIG.
2207 * Linus frestrict idea will clean these up nicely..
2208 */
2209 if (likely(!isblk)) {
2210 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
2211 if (*count || *pos > inode->i_sb->s_maxbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return -EFBIG;
2213 }
2214 /* zero-length writes at ->s_maxbytes are OK */
2215 }
2216
2217 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
2218 *count = inode->i_sb->s_maxbytes - *pos;
2219 } else {
David Howells93614012006-09-30 20:45:40 +02002220#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 loff_t isize;
2222 if (bdev_read_only(I_BDEV(inode)))
2223 return -EPERM;
2224 isize = i_size_read(inode);
2225 if (*pos >= isize) {
2226 if (*count || *pos > isize)
2227 return -ENOSPC;
2228 }
2229
2230 if (*pos + *count > isize)
2231 *count = isize - *pos;
David Howells93614012006-09-30 20:45:40 +02002232#else
2233 return -EPERM;
2234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 return 0;
2237}
2238EXPORT_SYMBOL(generic_write_checks);
2239
Nick Pigginafddba42007-10-16 01:25:01 -07002240int pagecache_write_begin(struct file *file, struct address_space *mapping,
2241 loff_t pos, unsigned len, unsigned flags,
2242 struct page **pagep, void **fsdata)
2243{
2244 const struct address_space_operations *aops = mapping->a_ops;
2245
Nick Piggin4e02ed42008-10-29 14:00:55 -07002246 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002247 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002248}
2249EXPORT_SYMBOL(pagecache_write_begin);
2250
2251int pagecache_write_end(struct file *file, struct address_space *mapping,
2252 loff_t pos, unsigned len, unsigned copied,
2253 struct page *page, void *fsdata)
2254{
2255 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002256
Nick Piggin4e02ed42008-10-29 14:00:55 -07002257 mark_page_accessed(page);
2258 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002259}
2260EXPORT_SYMBOL(pagecache_write_end);
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262ssize_t
2263generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
2264 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
2265 size_t count, size_t ocount)
2266{
2267 struct file *file = iocb->ki_filp;
2268 struct address_space *mapping = file->f_mapping;
2269 struct inode *inode = mapping->host;
2270 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002271 size_t write_len;
2272 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 if (count != ocount)
2275 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
2276
Christoph Hellwiga969e902008-07-23 21:27:04 -07002277 write_len = iov_length(iov, *nr_segs);
2278 end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002279
Nick Piggin48b47c52009-01-06 14:40:22 -08002280 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002281 if (written)
2282 goto out;
2283
2284 /*
2285 * After a write we want buffered reads to be sure to go to disk to get
2286 * the new data. We invalidate clean cached page from the region we're
2287 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002288 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002289 */
2290 if (mapping->nrpages) {
2291 written = invalidate_inode_pages2_range(mapping,
2292 pos >> PAGE_CACHE_SHIFT, end);
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002293 /*
2294 * If a page can not be invalidated, return 0 to fall back
2295 * to buffered write.
2296 */
2297 if (written) {
2298 if (written == -EBUSY)
2299 return 0;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002300 goto out;
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002301 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002302 }
2303
2304 written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs);
2305
2306 /*
2307 * Finally, try again to invalidate clean pages which might have been
2308 * cached by non-direct readahead, or faulted in by get_user_pages()
2309 * if the source of the write was an mmap'ed region of the file
2310 * we're writing. Either one is a pretty crazy thing to do,
2311 * so we don't support it 100%. If this invalidation
2312 * fails, tough, the write still worked...
2313 */
2314 if (mapping->nrpages) {
2315 invalidate_inode_pages2_range(mapping,
2316 pos >> PAGE_CACHE_SHIFT, end);
2317 }
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002320 pos += written;
2321 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2322 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 mark_inode_dirty(inode);
2324 }
Namhyung Kim01166512010-10-26 14:21:58 -07002325 *ppos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002327out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return written;
2329}
2330EXPORT_SYMBOL(generic_file_direct_write);
2331
Nick Piggineb2be182007-10-16 01:24:57 -07002332/*
2333 * Find or create a page at the given pagecache position. Return the locked
2334 * page. This function is specifically for buffered writes.
2335 */
Nick Piggin54566b22009-01-04 12:00:53 -08002336struct page *grab_cache_page_write_begin(struct address_space *mapping,
2337 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002338{
2339 int status;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002340 gfp_t gfp_mask;
Nick Piggineb2be182007-10-16 01:24:57 -07002341 struct page *page;
Nick Piggin54566b22009-01-04 12:00:53 -08002342 gfp_t gfp_notmask = 0;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002343
2344 gfp_mask = mapping_gfp_mask(mapping) | __GFP_WRITE;
Nick Piggin54566b22009-01-04 12:00:53 -08002345 if (flags & AOP_FLAG_NOFS)
2346 gfp_notmask = __GFP_FS;
Nick Piggineb2be182007-10-16 01:24:57 -07002347repeat:
2348 page = find_lock_page(mapping, index);
Steven Rostedtc585a262011-01-13 15:46:18 -08002349 if (page)
Darrick J. Wong3d08bcc2011-05-27 12:23:34 -07002350 goto found;
Nick Piggineb2be182007-10-16 01:24:57 -07002351
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002352 page = __page_cache_alloc(gfp_mask & ~gfp_notmask);
Nick Piggineb2be182007-10-16 01:24:57 -07002353 if (!page)
2354 return NULL;
Nick Piggin54566b22009-01-04 12:00:53 -08002355 status = add_to_page_cache_lru(page, mapping, index,
2356 GFP_KERNEL & ~gfp_notmask);
Nick Piggineb2be182007-10-16 01:24:57 -07002357 if (unlikely(status)) {
2358 page_cache_release(page);
2359 if (status == -EEXIST)
2360 goto repeat;
2361 return NULL;
2362 }
Darrick J. Wong3d08bcc2011-05-27 12:23:34 -07002363found:
2364 wait_on_page_writeback(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002365 return page;
2366}
Nick Piggin54566b22009-01-04 12:00:53 -08002367EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002368
Nick Pigginafddba42007-10-16 01:25:01 -07002369static ssize_t generic_perform_write(struct file *file,
2370 struct iov_iter *i, loff_t pos)
2371{
2372 struct address_space *mapping = file->f_mapping;
2373 const struct address_space_operations *a_ops = mapping->a_ops;
2374 long status = 0;
2375 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002376 unsigned int flags = 0;
2377
2378 /*
2379 * Copies from kernel address space cannot fail (NFSD is a big user).
2380 */
2381 if (segment_eq(get_fs(), KERNEL_DS))
2382 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002383
2384 do {
2385 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002386 unsigned long offset; /* Offset into pagecache page */
2387 unsigned long bytes; /* Bytes to write to page */
2388 size_t copied; /* Bytes copied from user */
2389 void *fsdata;
2390
2391 offset = (pos & (PAGE_CACHE_SIZE - 1));
Nick Pigginafddba42007-10-16 01:25:01 -07002392 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2393 iov_iter_count(i));
2394
2395again:
Nick Pigginafddba42007-10-16 01:25:01 -07002396 /*
2397 * Bring in the user page that we will copy from _first_.
2398 * Otherwise there's a nasty deadlock on copying from the
2399 * same page as we're writing to, without it being marked
2400 * up-to-date.
2401 *
2402 * Not only is this an optimisation, but it is also required
2403 * to check that the address is actually valid, when atomic
2404 * usercopies are used, below.
2405 */
2406 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2407 status = -EFAULT;
2408 break;
2409 }
2410
Nick Piggin674b8922007-10-16 01:25:03 -07002411 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002412 &page, &fsdata);
2413 if (unlikely(status))
2414 break;
2415
anfei zhou931e80e2010-02-02 13:44:02 -08002416 if (mapping_writably_mapped(mapping))
2417 flush_dcache_page(page);
2418
Nick Pigginafddba42007-10-16 01:25:01 -07002419 pagefault_disable();
2420 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
2421 pagefault_enable();
2422 flush_dcache_page(page);
2423
Josef Bacikc8236db2009-07-05 12:08:18 -07002424 mark_page_accessed(page);
Nick Pigginafddba42007-10-16 01:25:01 -07002425 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2426 page, fsdata);
2427 if (unlikely(status < 0))
2428 break;
2429 copied = status;
2430
2431 cond_resched();
2432
Nick Piggin124d3b72008-02-02 15:01:17 +01002433 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07002434 if (unlikely(copied == 0)) {
2435 /*
2436 * If we were unable to copy any data at all, we must
2437 * fall back to a single segment length write.
2438 *
2439 * If we didn't fallback here, we could livelock
2440 * because not all segments in the iov can be copied at
2441 * once without a pagefault.
2442 */
2443 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2444 iov_iter_single_seg_count(i));
2445 goto again;
2446 }
Nick Pigginafddba42007-10-16 01:25:01 -07002447 pos += copied;
2448 written += copied;
2449
2450 balance_dirty_pages_ratelimited(mapping);
Jan Karaa50527b2011-12-02 09:17:02 +08002451 if (fatal_signal_pending(current)) {
2452 status = -EINTR;
2453 break;
2454 }
Nick Pigginafddba42007-10-16 01:25:01 -07002455 } while (iov_iter_count(i));
2456
2457 return written ? written : status;
2458}
2459
2460ssize_t
2461generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2462 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2463 size_t count, ssize_t written)
2464{
2465 struct file *file = iocb->ki_filp;
Nick Pigginafddba42007-10-16 01:25:01 -07002466 ssize_t status;
2467 struct iov_iter i;
2468
2469 iov_iter_init(&i, iov, nr_segs, count, written);
Nick Piggin4e02ed42008-10-29 14:00:55 -07002470 status = generic_perform_write(file, &i, pos);
Nick Pigginafddba42007-10-16 01:25:01 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (likely(status >= 0)) {
Nick Pigginafddba42007-10-16 01:25:01 -07002473 written += status;
2474 *ppos = pos + status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return written ? written : status;
2478}
2479EXPORT_SYMBOL(generic_file_buffered_write);
2480
Jan Karae4dd9de2009-08-17 18:10:06 +02002481/**
2482 * __generic_file_aio_write - write data to a file
2483 * @iocb: IO state structure (file, offset, etc.)
2484 * @iov: vector with data to write
2485 * @nr_segs: number of segments in the vector
2486 * @ppos: position where to write
2487 *
2488 * This function does all the work needed for actually writing data to a
2489 * file. It does all basic checks, removes SUID from the file, updates
2490 * modification times and calls proper subroutines depending on whether we
2491 * do direct IO or a standard buffered write.
2492 *
2493 * It expects i_mutex to be grabbed unless we work on a block device or similar
2494 * object which does not need locking at all.
2495 *
2496 * This function does *not* take care of syncing data in case of O_SYNC write.
2497 * A caller has to handle it. This is mainly due to the fact that we want to
2498 * avoid syncing under i_mutex.
2499 */
2500ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2501 unsigned long nr_segs, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002504 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 size_t ocount; /* original count */
2506 size_t count; /* after file limit checks */
2507 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 loff_t pos;
2509 ssize_t written;
2510 ssize_t err;
2511
2512 ocount = 0;
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07002513 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2514 if (err)
2515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 count = ocount;
2518 pos = *ppos;
2519
2520 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2521
2522 /* We can write back this queue in page reclaim */
2523 current->backing_dev_info = mapping->backing_dev_info;
2524 written = 0;
2525
2526 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2527 if (err)
2528 goto out;
2529
2530 if (count == 0)
2531 goto out;
2532
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02002533 err = file_remove_suid(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (err)
2535 goto out;
2536
Christoph Hellwig870f4812006-01-09 20:52:01 -08002537 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2540 if (unlikely(file->f_flags & O_DIRECT)) {
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002541 loff_t endbyte;
2542 ssize_t written_buffered;
2543
2544 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
2545 ppos, count, ocount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (written < 0 || written == count)
2547 goto out;
2548 /*
2549 * direct-io write to a hole: fall through to buffered I/O
2550 * for completing the rest of the request.
2551 */
2552 pos += written;
2553 count -= written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002554 written_buffered = generic_file_buffered_write(iocb, iov,
2555 nr_segs, pos, ppos, count,
2556 written);
2557 /*
2558 * If generic_file_buffered_write() retuned a synchronous error
2559 * then we want to return the number of bytes which were
2560 * direct-written, or the error code if that was zero. Note
2561 * that this differs from normal direct-io semantics, which
2562 * will return -EFOO even if some bytes were written.
2563 */
2564 if (written_buffered < 0) {
2565 err = written_buffered;
2566 goto out;
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002569 /*
2570 * We need to ensure that the page cache pages are written to
2571 * disk and invalidated to preserve the expected O_DIRECT
2572 * semantics.
2573 */
2574 endbyte = pos + written_buffered - written - 1;
Christoph Hellwigc05c4ed2009-09-23 15:07:30 +02002575 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002576 if (err == 0) {
2577 written = written_buffered;
2578 invalidate_mapping_pages(mapping,
2579 pos >> PAGE_CACHE_SHIFT,
2580 endbyte >> PAGE_CACHE_SHIFT);
2581 } else {
2582 /*
2583 * We don't know how much we wrote, so just return
2584 * the number of bytes which were direct-written
2585 */
2586 }
2587 } else {
2588 written = generic_file_buffered_write(iocb, iov, nr_segs,
2589 pos, ppos, count, written);
2590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591out:
2592 current->backing_dev_info = NULL;
2593 return written ? written : err;
2594}
Jan Karae4dd9de2009-08-17 18:10:06 +02002595EXPORT_SYMBOL(__generic_file_aio_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Jan Karae4dd9de2009-08-17 18:10:06 +02002597/**
2598 * generic_file_aio_write - write data to a file
2599 * @iocb: IO state structure
2600 * @iov: vector with data to write
2601 * @nr_segs: number of segments in the vector
2602 * @pos: position in file where to write
2603 *
2604 * This is a wrapper around __generic_file_aio_write() to be used by most
2605 * filesystems. It takes care of syncing the file in case of O_SYNC file
2606 * and acquires i_mutex as needed.
2607 */
Badari Pulavarty027445c2006-09-30 23:28:46 -07002608ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2609 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
2611 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02002612 struct inode *inode = file->f_mapping->host;
Jens Axboe55602dd2010-06-24 15:05:37 +02002613 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 BUG_ON(iocb->ki_pos != pos);
2617
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002618 mutex_lock(&inode->i_mutex);
Jens Axboe55602dd2010-06-24 15:05:37 +02002619 blk_start_plug(&plug);
Jan Karae4dd9de2009-08-17 18:10:06 +02002620 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002621 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Jan Kara148f9482009-08-17 19:52:36 +02002623 if (ret > 0 || ret == -EIOCBQUEUED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 ssize_t err;
2625
Jan Kara148f9482009-08-17 19:52:36 +02002626 err = generic_write_sync(file, pos, ret);
Jan Karac7b50db2009-08-18 16:18:20 +02002627 if (err < 0 && ret > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 ret = err;
2629 }
Jens Axboe55602dd2010-06-24 15:05:37 +02002630 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return ret;
2632}
2633EXPORT_SYMBOL(generic_file_aio_write);
2634
David Howellscf9a2ae2006-08-29 19:05:54 +01002635/**
2636 * try_to_release_page() - release old fs-specific metadata on a page
2637 *
2638 * @page: the page which the kernel is trying to free
2639 * @gfp_mask: memory allocation flags (and I/O mode)
2640 *
2641 * The address_space is to try to release any data against the page
2642 * (presumably at page->private). If the release was successful, return `1'.
2643 * Otherwise return zero.
2644 *
David Howells266cf652009-04-03 16:42:36 +01002645 * This may also be called if PG_fscache is set on a page, indicating that the
2646 * page is known to the local caching routines.
2647 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002648 * The @gfp_mask argument specifies whether I/O may be performed to release
Mingming Cao3f31fdd2008-07-25 01:46:22 -07002649 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01002650 *
David Howellscf9a2ae2006-08-29 19:05:54 +01002651 */
2652int try_to_release_page(struct page *page, gfp_t gfp_mask)
2653{
2654 struct address_space * const mapping = page->mapping;
2655
2656 BUG_ON(!PageLocked(page));
2657 if (PageWriteback(page))
2658 return 0;
2659
2660 if (mapping && mapping->a_ops->releasepage)
2661 return mapping->a_ops->releasepage(page, gfp_mask);
2662 return try_to_free_buffers(page);
2663}
2664
2665EXPORT_SYMBOL(try_to_release_page);