blob: 455119cc7f40c532540b92a39d2354522ef09dcc [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/compiler.h>
15#include <linux/fs.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/aio.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
20#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>
Emil Medve3a424f22007-10-24 14:18:32 +020031#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/security.h>
33#include <linux/syscalls.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080034#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070035#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
Nick Piggin0f8053a2006-03-22 00:08:33 -080036#include "internal.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * FIXME: remove all knowledge of the buffer layer from the core VM
40 */
41#include <linux/buffer_head.h> /* for generic_osync_inode */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/mman.h>
44
Adrian Bunk5ce78522005-09-10 00:26:28 -070045static ssize_t
46generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
47 loff_t offset, unsigned long nr_segs);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Shared mappings implemented 30.11.1994. It's not fully working yet,
51 * though.
52 *
53 * Shared mappings now work. 15.8.1995 Bruno.
54 *
55 * finished 'unifying' the page and buffer cache and SMP-threaded the
56 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
57 *
58 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
59 */
60
61/*
62 * Lock ordering:
63 *
64 * ->i_mmap_lock (vmtruncate)
65 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070066 * ->swap_lock (exclusive_swap_page, others)
67 * ->mapping->tree_lock
Nick Piggin7a405072007-10-16 23:26:02 -070068 * ->zone.lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080070 * ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * ->i_mmap_lock (truncate->unmap_mapping_range)
72 *
73 * ->mmap_sem
74 * ->i_mmap_lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070075 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
77 *
78 * ->mmap_sem
79 * ->lock_page (access_process_vm)
80 *
Nick Piggin82591e62006-10-19 23:29:10 -070081 * ->i_mutex (generic_file_buffered_write)
82 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080084 * ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * ->i_alloc_sem (various)
86 *
87 * ->inode_lock
88 * ->sb_lock (fs/fs-writeback.c)
89 * ->mapping->tree_lock (__sync_single_inode)
90 *
91 * ->i_mmap_lock
92 * ->anon_vma.lock (vma_adjust)
93 *
94 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070095 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070097 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070098 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * ->private_lock (try_to_unmap_one)
100 * ->tree_lock (try_to_unmap_one)
101 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -0800102 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * ->private_lock (page_remove_rmap->set_page_dirty)
104 * ->tree_lock (page_remove_rmap->set_page_dirty)
105 * ->inode_lock (page_remove_rmap->set_page_dirty)
106 * ->inode_lock (zap_pte_range->set_page_dirty)
107 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 *
109 * ->task->proc_lock
110 * ->dcache_lock (proc_pid_lookup)
111 */
112
113/*
114 * Remove a page from the page cache and free it. Caller has to make
115 * sure the page is locked and that nobody else uses it - or that usage
116 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
117 */
118void __remove_from_page_cache(struct page *page)
119{
120 struct address_space *mapping = page->mapping;
121
122 radix_tree_delete(&mapping->page_tree, page->index);
123 page->mapping = NULL;
124 mapping->nrpages--;
Christoph Lameter347ce432006-06-30 01:55:35 -0700125 __dec_zone_page_state(page, NR_FILE_PAGES);
Nick Piggin45426812007-07-15 23:38:12 -0700126 BUG_ON(page_mapped(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129void remove_from_page_cache(struct page *page)
130{
131 struct address_space *mapping = page->mapping;
132
Matt Mackallcd7619d2005-05-01 08:59:01 -0700133 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 write_lock_irq(&mapping->tree_lock);
136 __remove_from_page_cache(page);
137 write_unlock_irq(&mapping->tree_lock);
138}
139
140static int sync_page(void *word)
141{
142 struct address_space *mapping;
143 struct page *page;
144
Andi Kleen07808b72005-11-05 17:25:53 +0100145 page = container_of((unsigned long *)word, struct page, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /*
William Lee Irwin IIIdd1d5af2005-05-01 08:58:38 -0700148 * page_mapping() is being called without PG_locked held.
149 * Some knowledge of the state and use of the page is used to
150 * reduce the requirements down to a memory barrier.
151 * The danger here is of a stale page_mapping() return value
152 * indicating a struct address_space different from the one it's
153 * associated with when it is associated with one.
154 * After smp_mb(), it's either the correct page_mapping() for
155 * the page, or an old page_mapping() and the page's own
156 * page_mapping() has gone NULL.
157 * The ->sync_page() address_space operation must tolerate
158 * page_mapping() going NULL. By an amazing coincidence,
159 * this comes about because none of the users of the page
160 * in the ->sync_page() methods make essential use of the
161 * page_mapping(), merely passing the page down to the backing
162 * device's unplug functions when it's non-NULL, which in turn
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700163 * ignore it for all cases but swap, where only page_private(page) is
William Lee Irwin IIIdd1d5af2005-05-01 08:58:38 -0700164 * of interest. When page_mapping() does go NULL, the entire
165 * call stack gracefully ignores the page and returns.
166 * -- wli
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168 smp_mb();
169 mapping = page_mapping(page);
170 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
171 mapping->a_ops->sync_page(page);
172 io_schedule();
173 return 0;
174}
175
Matthew Wilcox2687a352007-12-06 11:18:49 -0500176static int sync_page_killable(void *word)
177{
178 sync_page(word);
179 return fatal_signal_pending(current) ? -EINTR : 0;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700183 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700184 * @mapping: address space structure to write
185 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800186 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700187 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700189 * Start writeback against all of a mapping's dirty pages that lie
190 * within the byte offsets <start, end> inclusive.
191 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700193 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * these two operations is that if a dirty page/buffer is encountered, it must
195 * be waited upon, and not just skipped over.
196 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800197int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
198 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int ret;
201 struct writeback_control wbc = {
202 .sync_mode = sync_mode,
203 .nr_to_write = mapping->nrpages * 2,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700204 .range_start = start,
205 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 };
207
208 if (!mapping_cap_writeback_dirty(mapping))
209 return 0;
210
211 ret = do_writepages(mapping, &wbc);
212 return ret;
213}
214
215static inline int __filemap_fdatawrite(struct address_space *mapping,
216 int sync_mode)
217{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700218 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221int filemap_fdatawrite(struct address_space *mapping)
222{
223 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
224}
225EXPORT_SYMBOL(filemap_fdatawrite);
226
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800227static int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
228 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
231}
232
Randy Dunlap485bb992006-06-23 02:03:49 -0700233/**
234 * filemap_flush - mostly a non-blocking flush
235 * @mapping: target address_space
236 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * This is a mostly non-blocking flush. Not suitable for data-integrity
238 * purposes - I/O may not be started against all dirty pages.
239 */
240int filemap_flush(struct address_space *mapping)
241{
242 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
243}
244EXPORT_SYMBOL(filemap_flush);
245
Randy Dunlap485bb992006-06-23 02:03:49 -0700246/**
247 * wait_on_page_writeback_range - wait for writeback to complete
248 * @mapping: target address_space
249 * @start: beginning page index
250 * @end: ending page index
251 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * Wait for writeback to complete against pages indexed by start->end
253 * inclusive
254 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800255int wait_on_page_writeback_range(struct address_space *mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 pgoff_t start, pgoff_t end)
257{
258 struct pagevec pvec;
259 int nr_pages;
260 int ret = 0;
261 pgoff_t index;
262
263 if (end < start)
264 return 0;
265
266 pagevec_init(&pvec, 0);
267 index = start;
268 while ((index <= end) &&
269 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
270 PAGECACHE_TAG_WRITEBACK,
271 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
272 unsigned i;
273
274 for (i = 0; i < nr_pages; i++) {
275 struct page *page = pvec.pages[i];
276
277 /* until radix tree lookup accepts end_index */
278 if (page->index > end)
279 continue;
280
281 wait_on_page_writeback(page);
282 if (PageError(page))
283 ret = -EIO;
284 }
285 pagevec_release(&pvec);
286 cond_resched();
287 }
288
289 /* Check for outstanding write errors */
290 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
291 ret = -ENOSPC;
292 if (test_and_clear_bit(AS_EIO, &mapping->flags))
293 ret = -EIO;
294
295 return ret;
296}
297
Randy Dunlap485bb992006-06-23 02:03:49 -0700298/**
299 * sync_page_range - write and wait on all pages in the passed range
300 * @inode: target inode
301 * @mapping: target address_space
302 * @pos: beginning offset in pages to write
303 * @count: number of bytes to write
304 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * Write and wait upon all the pages in the passed range. This is a "data
306 * integrity" operation. It waits upon in-flight writeout before starting and
307 * waiting upon new writeout. If there was an IO error, return it.
308 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800309 * We need to re-take i_mutex during the generic_osync_inode list walk because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * it is otherwise livelockable.
311 */
312int sync_page_range(struct inode *inode, struct address_space *mapping,
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800313 loff_t pos, loff_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
316 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
317 int ret;
318
319 if (!mapping_cap_writeback_dirty(mapping) || !count)
320 return 0;
321 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
322 if (ret == 0) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800323 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800325 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 if (ret == 0)
328 ret = wait_on_page_writeback_range(mapping, start, end);
329 return ret;
330}
331EXPORT_SYMBOL(sync_page_range);
332
Randy Dunlap485bb992006-06-23 02:03:49 -0700333/**
334 * sync_page_range_nolock
335 * @inode: target inode
336 * @mapping: target address_space
337 * @pos: beginning offset in pages to write
338 * @count: number of bytes to write
339 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800340 * Note: Holding i_mutex across sync_page_range_nolock() is not a good idea
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * as it forces O_SYNC writers to different parts of the same file
342 * to be serialised right until io completion.
343 */
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800344int sync_page_range_nolock(struct inode *inode, struct address_space *mapping,
345 loff_t pos, loff_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
348 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
349 int ret;
350
351 if (!mapping_cap_writeback_dirty(mapping) || !count)
352 return 0;
353 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
354 if (ret == 0)
355 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
356 if (ret == 0)
357 ret = wait_on_page_writeback_range(mapping, start, end);
358 return ret;
359}
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800360EXPORT_SYMBOL(sync_page_range_nolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700363 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700365 *
366 * Walk the list of under-writeback pages of the given address space
367 * and wait for all of them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
369int filemap_fdatawait(struct address_space *mapping)
370{
371 loff_t i_size = i_size_read(mapping->host);
372
373 if (i_size == 0)
374 return 0;
375
376 return wait_on_page_writeback_range(mapping, 0,
377 (i_size - 1) >> PAGE_CACHE_SHIFT);
378}
379EXPORT_SYMBOL(filemap_fdatawait);
380
381int filemap_write_and_wait(struct address_space *mapping)
382{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800383 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800386 err = filemap_fdatawrite(mapping);
387 /*
388 * Even if the above returned error, the pages may be
389 * written partially (e.g. -ENOSPC), so we wait for it.
390 * But the -EIO is special case, it may indicate the worst
391 * thing (e.g. bug) happened, so we avoid waiting for it.
392 */
393 if (err != -EIO) {
394 int err2 = filemap_fdatawait(mapping);
395 if (!err)
396 err = err2;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800401EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Randy Dunlap485bb992006-06-23 02:03:49 -0700403/**
404 * filemap_write_and_wait_range - write out & wait on a file range
405 * @mapping: the address_space for the pages
406 * @lstart: offset in bytes where the range starts
407 * @lend: offset in bytes where the range ends (inclusive)
408 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800409 * Write out and wait upon file offsets lstart->lend, inclusive.
410 *
411 * Note that `lend' is inclusive (describes the last byte to be written) so
412 * that this function can be used to write to the very end-of-file (end = -1).
413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414int filemap_write_and_wait_range(struct address_space *mapping,
415 loff_t lstart, loff_t lend)
416{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800417 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800420 err = __filemap_fdatawrite_range(mapping, lstart, lend,
421 WB_SYNC_ALL);
422 /* See comment of filemap_write_and_wait() */
423 if (err != -EIO) {
424 int err2 = wait_on_page_writeback_range(mapping,
425 lstart >> PAGE_CACHE_SHIFT,
426 lend >> PAGE_CACHE_SHIFT);
427 if (!err)
428 err = err2;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800431 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Randy Dunlap485bb992006-06-23 02:03:49 -0700434/**
435 * add_to_page_cache - add newly allocated pagecache pages
436 * @page: page to add
437 * @mapping: the page's address_space
438 * @offset: page index
439 * @gfp_mask: page allocation mode
440 *
441 * This function is used to add newly allocated pagecache pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * the page is new, so we can just run SetPageLocked() against it.
443 * The other page state flags were set by rmqueue().
444 *
445 * This function does not add the page to the LRU. The caller must do that.
446 */
447int add_to_page_cache(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400448 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
451
452 if (error == 0) {
453 write_lock_irq(&mapping->tree_lock);
454 error = radix_tree_insert(&mapping->page_tree, offset, page);
455 if (!error) {
456 page_cache_get(page);
457 SetPageLocked(page);
458 page->mapping = mapping;
459 page->index = offset;
460 mapping->nrpages++;
Christoph Lameter347ce432006-06-30 01:55:35 -0700461 __inc_zone_page_state(page, NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 write_unlock_irq(&mapping->tree_lock);
464 radix_tree_preload_end();
465 }
466 return error;
467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468EXPORT_SYMBOL(add_to_page_cache);
469
470int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400471 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
474 if (ret == 0)
475 lru_cache_add(page);
476 return ret;
477}
478
Paul Jackson44110fe2006-03-24 03:16:04 -0800479#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700480struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800481{
482 if (cpuset_do_page_mem_spread()) {
483 int n = cpuset_mem_spread_node();
Nick Piggin2ae88142006-10-28 10:38:23 -0700484 return alloc_pages_node(n, gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800485 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700486 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800487}
Nick Piggin2ae88142006-10-28 10:38:23 -0700488EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800489#endif
490
Nick Piggindb376482006-09-25 23:31:24 -0700491static int __sleep_on_page_lock(void *word)
492{
493 io_schedule();
494 return 0;
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * In order to wait for pages to become available there must be
499 * waitqueues associated with pages. By using a hash table of
500 * waitqueues where the bucket discipline is to maintain all
501 * waiters on the same queue and wake all when any of the pages
502 * become available, and for the woken contexts to check to be
503 * sure the appropriate page became available, this saves space
504 * at a cost of "thundering herd" phenomena during rare hash
505 * collisions.
506 */
507static wait_queue_head_t *page_waitqueue(struct page *page)
508{
509 const struct zone *zone = page_zone(page);
510
511 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
512}
513
514static inline void wake_up_page(struct page *page, int bit)
515{
516 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
517}
518
519void fastcall wait_on_page_bit(struct page *page, int bit_nr)
520{
521 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
522
523 if (test_bit(bit_nr, &page->flags))
524 __wait_on_bit(page_waitqueue(page), &wait, sync_page,
525 TASK_UNINTERRUPTIBLE);
526}
527EXPORT_SYMBOL(wait_on_page_bit);
528
529/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700530 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * @page: the page
532 *
533 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
534 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
535 * mechananism between PageLocked pages and PageWriteback pages is shared.
536 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
537 *
538 * The first mb is necessary to safely close the critical section opened by the
539 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
540 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
541 * parallel wait_on_page_locked()).
542 */
543void fastcall unlock_page(struct page *page)
544{
545 smp_mb__before_clear_bit();
546 if (!TestClearPageLocked(page))
547 BUG();
548 smp_mb__after_clear_bit();
549 wake_up_page(page, PG_locked);
550}
551EXPORT_SYMBOL(unlock_page);
552
Randy Dunlap485bb992006-06-23 02:03:49 -0700553/**
554 * end_page_writeback - end writeback against a page
555 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
557void end_page_writeback(struct page *page)
558{
559 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
560 if (!test_clear_page_writeback(page))
561 BUG();
562 }
563 smp_mb__after_clear_bit();
564 wake_up_page(page, PG_writeback);
565}
566EXPORT_SYMBOL(end_page_writeback);
567
Randy Dunlap485bb992006-06-23 02:03:49 -0700568/**
569 * __lock_page - get a lock on the page, assuming we need to sleep to get it
570 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700572 * Ugly. Running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
574 * chances are that on the second loop, the block layer's plug list is empty,
575 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
576 */
577void fastcall __lock_page(struct page *page)
578{
579 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
580
581 __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
582 TASK_UNINTERRUPTIBLE);
583}
584EXPORT_SYMBOL(__lock_page);
585
Matthew Wilcox2687a352007-12-06 11:18:49 -0500586int fastcall __lock_page_killable(struct page *page)
587{
588 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
589
590 return __wait_on_bit_lock(page_waitqueue(page), &wait,
591 sync_page_killable, TASK_KILLABLE);
592}
593
Nick Piggindb376482006-09-25 23:31:24 -0700594/*
595 * Variant of lock_page that does not require the caller to hold a reference
596 * on the page's mapping.
597 */
598void fastcall __lock_page_nosync(struct page *page)
599{
600 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
601 __wait_on_bit_lock(page_waitqueue(page), &wait, __sleep_on_page_lock,
602 TASK_UNINTERRUPTIBLE);
603}
604
Randy Dunlap485bb992006-06-23 02:03:49 -0700605/**
606 * find_get_page - find and get a page reference
607 * @mapping: the address_space to search
608 * @offset: the page index
609 *
Nick Pigginda6052f2006-09-25 23:31:35 -0700610 * Is there a pagecache struct page at the given (mapping, offset) tuple?
611 * If yes, increment its refcount and return it; if no, return NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Fengguang Wu57f6b962007-10-16 01:24:37 -0700613struct page * find_get_page(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct page *page;
616
617 read_lock_irq(&mapping->tree_lock);
618 page = radix_tree_lookup(&mapping->page_tree, offset);
619 if (page)
620 page_cache_get(page);
621 read_unlock_irq(&mapping->tree_lock);
622 return page;
623}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624EXPORT_SYMBOL(find_get_page);
625
Randy Dunlap485bb992006-06-23 02:03:49 -0700626/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * find_lock_page - locate, pin and lock a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -0700628 * @mapping: the address_space to search
629 * @offset: the page index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * Locates the desired pagecache page, locks it, increments its reference
632 * count and returns its address.
633 *
634 * Returns zero if the page was not present. find_lock_page() may sleep.
635 */
636struct page *find_lock_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700637 pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct page *page;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641repeat:
Nick Piggin45726cb2007-10-16 01:24:41 -0700642 read_lock_irq(&mapping->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 page = radix_tree_lookup(&mapping->page_tree, offset);
644 if (page) {
645 page_cache_get(page);
646 if (TestSetPageLocked(page)) {
647 read_unlock_irq(&mapping->tree_lock);
Nikita Danilovbbfbb7c2006-01-06 00:11:08 -0800648 __lock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* Has the page been truncated while we slept? */
Nick Piggin45726cb2007-10-16 01:24:41 -0700651 if (unlikely(page->mapping != mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unlock_page(page);
653 page_cache_release(page);
654 goto repeat;
655 }
Nick Piggin45726cb2007-10-16 01:24:41 -0700656 VM_BUG_ON(page->index != offset);
657 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 }
660 read_unlock_irq(&mapping->tree_lock);
Nick Piggin45726cb2007-10-16 01:24:41 -0700661out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return page;
663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664EXPORT_SYMBOL(find_lock_page);
665
666/**
667 * find_or_create_page - locate or add a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -0700668 * @mapping: the page's address_space
669 * @index: the page's index into the mapping
670 * @gfp_mask: page allocation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
672 * Locates a page in the pagecache. If the page is not present, a new page
673 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
674 * LRU list. The returned page is locked and has its reference count
675 * incremented.
676 *
677 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
678 * allocation!
679 *
680 * find_or_create_page() returns the desired page's address, or zero on
681 * memory exhaustion.
682 */
683struct page *find_or_create_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -0700684 pgoff_t index, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Nick Piggineb2be182007-10-16 01:24:57 -0700686 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int err;
688repeat:
689 page = find_lock_page(mapping, index);
690 if (!page) {
Nick Piggineb2be182007-10-16 01:24:57 -0700691 page = __page_cache_alloc(gfp_mask);
692 if (!page)
693 return NULL;
694 err = add_to_page_cache_lru(page, mapping, index, gfp_mask);
695 if (unlikely(err)) {
696 page_cache_release(page);
697 page = NULL;
698 if (err == -EEXIST)
699 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return page;
703}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704EXPORT_SYMBOL(find_or_create_page);
705
706/**
707 * find_get_pages - gang pagecache lookup
708 * @mapping: The address_space to search
709 * @start: The starting page index
710 * @nr_pages: The maximum number of pages
711 * @pages: Where the resulting pages are placed
712 *
713 * find_get_pages() will search for and return a group of up to
714 * @nr_pages pages in the mapping. The pages are placed at @pages.
715 * find_get_pages() takes a reference against the returned pages.
716 *
717 * The search returns a group of mapping-contiguous pages with ascending
718 * indexes. There may be holes in the indices due to not-present pages.
719 *
720 * find_get_pages() returns the number of pages which were found.
721 */
722unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
723 unsigned int nr_pages, struct page **pages)
724{
725 unsigned int i;
726 unsigned int ret;
727
728 read_lock_irq(&mapping->tree_lock);
729 ret = radix_tree_gang_lookup(&mapping->page_tree,
730 (void **)pages, start, nr_pages);
731 for (i = 0; i < ret; i++)
732 page_cache_get(pages[i]);
733 read_unlock_irq(&mapping->tree_lock);
734 return ret;
735}
736
Jens Axboeebf43502006-04-27 08:46:01 +0200737/**
738 * find_get_pages_contig - gang contiguous pagecache lookup
739 * @mapping: The address_space to search
740 * @index: The starting page index
741 * @nr_pages: The maximum number of pages
742 * @pages: Where the resulting pages are placed
743 *
744 * find_get_pages_contig() works exactly like find_get_pages(), except
745 * that the returned number of pages are guaranteed to be contiguous.
746 *
747 * find_get_pages_contig() returns the number of pages which were found.
748 */
749unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
750 unsigned int nr_pages, struct page **pages)
751{
752 unsigned int i;
753 unsigned int ret;
754
755 read_lock_irq(&mapping->tree_lock);
756 ret = radix_tree_gang_lookup(&mapping->page_tree,
757 (void **)pages, index, nr_pages);
758 for (i = 0; i < ret; i++) {
759 if (pages[i]->mapping == NULL || pages[i]->index != index)
760 break;
761
762 page_cache_get(pages[i]);
763 index++;
764 }
765 read_unlock_irq(&mapping->tree_lock);
766 return i;
767}
David Howellsef71c152007-05-09 02:33:44 -0700768EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +0200769
Randy Dunlap485bb992006-06-23 02:03:49 -0700770/**
771 * find_get_pages_tag - find and return pages that match @tag
772 * @mapping: the address_space to search
773 * @index: the starting page index
774 * @tag: the tag index
775 * @nr_pages: the maximum number of pages
776 * @pages: where the resulting pages are placed
777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -0700779 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
781unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
782 int tag, unsigned int nr_pages, struct page **pages)
783{
784 unsigned int i;
785 unsigned int ret;
786
787 read_lock_irq(&mapping->tree_lock);
788 ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
789 (void **)pages, *index, nr_pages, tag);
790 for (i = 0; i < ret; i++)
791 page_cache_get(pages[i]);
792 if (ret)
793 *index = pages[ret - 1]->index + 1;
794 read_unlock_irq(&mapping->tree_lock);
795 return ret;
796}
David Howellsef71c152007-05-09 02:33:44 -0700797EXPORT_SYMBOL(find_get_pages_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Randy Dunlap485bb992006-06-23 02:03:49 -0700799/**
800 * grab_cache_page_nowait - returns locked page at given index in given cache
801 * @mapping: target address_space
802 * @index: the page index
803 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800804 * Same as grab_cache_page(), but do not wait if the page is unavailable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * This is intended for speculative data generators, where the data can
806 * be regenerated if the page couldn't be grabbed. This routine should
807 * be safe to call while holding the lock for another page.
808 *
809 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
810 * and deadlock against the caller's locked page.
811 */
812struct page *
Fengguang Wu57f6b962007-10-16 01:24:37 -0700813grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct page *page = find_get_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (page) {
818 if (!TestSetPageLocked(page))
819 return page;
820 page_cache_release(page);
821 return NULL;
822 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700823 page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
824 if (page && add_to_page_cache_lru(page, mapping, index, GFP_KERNEL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 page_cache_release(page);
826 page = NULL;
827 }
828 return page;
829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830EXPORT_SYMBOL(grab_cache_page_nowait);
831
Wu Fengguang76d42bd2006-06-25 05:48:43 -0700832/*
833 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
834 * a _large_ part of the i/o request. Imagine the worst scenario:
835 *
836 * ---R__________________________________________B__________
837 * ^ reading here ^ bad block(assume 4k)
838 *
839 * read(R) => miss => readahead(R...B) => media error => frustrating retries
840 * => failing the whole request => read(R) => read(R+1) =>
841 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
842 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
843 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
844 *
845 * It is going insane. Fix it by quickly scaling down the readahead size.
846 */
847static void shrink_readahead_size_eio(struct file *filp,
848 struct file_ra_state *ra)
849{
850 if (!ra->ra_pages)
851 return;
852
853 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -0700854}
855
Randy Dunlap485bb992006-06-23 02:03:49 -0700856/**
857 * do_generic_mapping_read - generic file read routine
858 * @mapping: address_space to be read
Randy Dunlap8f731f72007-10-18 23:39:28 -0700859 * @ra: file's readahead state
Randy Dunlap485bb992006-06-23 02:03:49 -0700860 * @filp: the file to read
861 * @ppos: current file position
862 * @desc: read_descriptor
863 * @actor: read method
864 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -0700866 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *
868 * This is really ugly. But the goto's actually try to clarify some
869 * of the logic when it comes to error handling etc.
870 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700871 * Note the struct file* is only passed for the use of readpage.
872 * It may be NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
874void do_generic_mapping_read(struct address_space *mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -0700875 struct file_ra_state *ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct file *filp,
877 loff_t *ppos,
878 read_descriptor_t *desc,
879 read_actor_t actor)
880{
881 struct inode *inode = mapping->host;
Fengguang Wu57f6b962007-10-16 01:24:37 -0700882 pgoff_t index;
883 pgoff_t last_index;
884 pgoff_t prev_index;
885 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -0700886 unsigned int prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 index = *ppos >> PAGE_CACHE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -0700890 prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
891 prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
893 offset = *ppos & ~PAGE_CACHE_MASK;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 for (;;) {
896 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -0700897 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -0700898 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long nr, ret;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902find_page:
903 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -0700904 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -0700905 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -0700906 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -0700907 index, last_index - index);
908 page = find_get_page(mapping, index);
909 if (unlikely(page == NULL))
910 goto no_cached_page;
911 }
912 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -0700913 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -0700914 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -0700915 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 if (!PageUptodate(page))
918 goto page_not_up_to_date;
919page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -0700920 /*
921 * i_size must be checked after we know the page is Uptodate.
922 *
923 * Checking i_size after the check allows us to calculate
924 * the correct value for "nr", which means the zero-filled
925 * part of the page is not copied back to userspace (unless
926 * another truncate extends the file - this is desired though).
927 */
928
929 isize = i_size_read(inode);
930 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
931 if (unlikely(!isize || index > end_index)) {
932 page_cache_release(page);
933 goto out;
934 }
935
936 /* nr is the maximum number of bytes to copy from this page */
937 nr = PAGE_CACHE_SIZE;
938 if (index == end_index) {
939 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
940 if (nr <= offset) {
941 page_cache_release(page);
942 goto out;
943 }
944 }
945 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* If users can be writing to this page using arbitrary
948 * virtual addresses, take care about potential aliasing
949 * before reading the page on the kernel side.
950 */
951 if (mapping_writably_mapped(mapping))
952 flush_dcache_page(page);
953
954 /*
Jan Karaec0f1632007-05-06 14:49:25 -0700955 * When a sequential read accesses a page several times,
956 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Jan Karaec0f1632007-05-06 14:49:25 -0700958 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 mark_page_accessed(page);
960 prev_index = index;
961
962 /*
963 * Ok, we have the page, and it's up-to-date, so
964 * now we can copy it to user space...
965 *
966 * The actor routine returns how many bytes were actually used..
967 * NOTE! This may not be the same as how much of a user buffer
968 * we filled up (we may be padding etc), so we can only update
969 * "pos" here (the actor routine has to update the user buffer
970 * pointers and the remaining count).
971 */
972 ret = actor(desc, page, offset, nr);
973 offset += ret;
974 index += offset >> PAGE_CACHE_SHIFT;
975 offset &= ~PAGE_CACHE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -0700976 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 page_cache_release(page);
979 if (ret == nr && desc->count)
980 continue;
981 goto out;
982
983page_not_up_to_date:
984 /* Get exclusive access to the page ... */
Matthew Wilcox0b94e972007-12-06 11:19:57 -0500985 if (lock_page_killable(page))
986 goto readpage_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Nick Pigginda6052f2006-09-25 23:31:35 -0700988 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!page->mapping) {
990 unlock_page(page);
991 page_cache_release(page);
992 continue;
993 }
994
995 /* Did somebody else fill it already? */
996 if (PageUptodate(page)) {
997 unlock_page(page);
998 goto page_ok;
999 }
1000
1001readpage:
1002 /* Start the actual read. The read will unlock the page. */
1003 error = mapping->a_ops->readpage(filp, page);
1004
Zach Brown994fc28c2005-12-15 14:28:17 -08001005 if (unlikely(error)) {
1006 if (error == AOP_TRUNCATED_PAGE) {
1007 page_cache_release(page);
1008 goto find_page;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (!PageUptodate(page)) {
Matthew Wilcox0b94e972007-12-06 11:19:57 -05001014 if (lock_page_killable(page))
1015 goto readpage_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!PageUptodate(page)) {
1017 if (page->mapping == NULL) {
1018 /*
1019 * invalidate_inode_pages got it
1020 */
1021 unlock_page(page);
1022 page_cache_release(page);
1023 goto find_page;
1024 }
1025 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07001026 shrink_readahead_size_eio(filp, ra);
Matthew Wilcox0b94e972007-12-06 11:19:57 -05001027 goto readpage_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 unlock_page(page);
1030 }
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 goto page_ok;
1033
Matthew Wilcox0b94e972007-12-06 11:19:57 -05001034readpage_eio:
1035 error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036readpage_error:
1037 /* UHHUH! A synchronous read error occurred. Report it */
1038 desc->error = error;
1039 page_cache_release(page);
1040 goto out;
1041
1042no_cached_page:
1043 /*
1044 * Ok, it wasn't cached, so we need to create a new
1045 * page..
1046 */
Nick Piggineb2be182007-10-16 01:24:57 -07001047 page = page_cache_alloc_cold(mapping);
1048 if (!page) {
1049 desc->error = -ENOMEM;
1050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Nick Piggineb2be182007-10-16 01:24:57 -07001052 error = add_to_page_cache_lru(page, mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 index, GFP_KERNEL);
1054 if (error) {
Nick Piggineb2be182007-10-16 01:24:57 -07001055 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (error == -EEXIST)
1057 goto find_page;
1058 desc->error = error;
1059 goto out;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto readpage;
1062 }
1063
1064out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07001065 ra->prev_pos = prev_index;
1066 ra->prev_pos <<= PAGE_CACHE_SHIFT;
1067 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Fengguang Wuf4e6b492007-10-16 01:24:33 -07001069 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (filp)
1071 file_accessed(filp);
1072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073EXPORT_SYMBOL(do_generic_mapping_read);
1074
1075int file_read_actor(read_descriptor_t *desc, struct page *page,
1076 unsigned long offset, unsigned long size)
1077{
1078 char *kaddr;
1079 unsigned long left, count = desc->count;
1080
1081 if (size > count)
1082 size = count;
1083
1084 /*
1085 * Faults on the destination of a read are common, so do it before
1086 * taking the kmap.
1087 */
1088 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1089 kaddr = kmap_atomic(page, KM_USER0);
1090 left = __copy_to_user_inatomic(desc->arg.buf,
1091 kaddr + offset, size);
1092 kunmap_atomic(kaddr, KM_USER0);
1093 if (left == 0)
1094 goto success;
1095 }
1096
1097 /* Do it the slow way */
1098 kaddr = kmap(page);
1099 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1100 kunmap(page);
1101
1102 if (left) {
1103 size -= left;
1104 desc->error = -EFAULT;
1105 }
1106success:
1107 desc->count = count - size;
1108 desc->written += size;
1109 desc->arg.buf += size;
1110 return size;
1111}
1112
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07001113/*
1114 * Performs necessary checks before doing a write
1115 * @iov: io vector request
1116 * @nr_segs: number of segments in the iovec
1117 * @count: number of bytes to write
1118 * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1119 *
1120 * Adjust number of segments and amount of bytes to write (nr_segs should be
1121 * properly initialized first). Returns appropriate error code that caller
1122 * should return or zero in case that write should be allowed.
1123 */
1124int generic_segment_checks(const struct iovec *iov,
1125 unsigned long *nr_segs, size_t *count, int access_flags)
1126{
1127 unsigned long seg;
1128 size_t cnt = 0;
1129 for (seg = 0; seg < *nr_segs; seg++) {
1130 const struct iovec *iv = &iov[seg];
1131
1132 /*
1133 * If any segment has a negative length, or the cumulative
1134 * length ever wraps negative then return -EINVAL.
1135 */
1136 cnt += iv->iov_len;
1137 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1138 return -EINVAL;
1139 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1140 continue;
1141 if (seg == 0)
1142 return -EFAULT;
1143 *nr_segs = seg;
1144 cnt -= iv->iov_len; /* This segment is no good */
1145 break;
1146 }
1147 *count = cnt;
1148 return 0;
1149}
1150EXPORT_SYMBOL(generic_segment_checks);
1151
Randy Dunlap485bb992006-06-23 02:03:49 -07001152/**
Henrik Kretzschmarb2abacf2006-10-04 02:15:22 -07001153 * generic_file_aio_read - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07001154 * @iocb: kernel I/O control block
1155 * @iov: io vector request
1156 * @nr_segs: number of segments in the iovec
Henrik Kretzschmarb2abacf2006-10-04 02:15:22 -07001157 * @pos: current file position
Randy Dunlap485bb992006-06-23 02:03:49 -07001158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * This is the "read()" routine for all filesystems
1160 * that can use the page cache directly.
1161 */
1162ssize_t
Badari Pulavarty543ade12006-09-30 23:28:48 -07001163generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1164 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 struct file *filp = iocb->ki_filp;
1167 ssize_t retval;
1168 unsigned long seg;
1169 size_t count;
Badari Pulavarty543ade12006-09-30 23:28:48 -07001170 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 count = 0;
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07001173 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1174 if (retval)
1175 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1178 if (filp->f_flags & O_DIRECT) {
Badari Pulavarty543ade12006-09-30 23:28:48 -07001179 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct address_space *mapping;
1181 struct inode *inode;
1182
1183 mapping = filp->f_mapping;
1184 inode = mapping->host;
1185 retval = 0;
1186 if (!count)
1187 goto out; /* skip atime */
1188 size = i_size_read(inode);
1189 if (pos < size) {
1190 retval = generic_file_direct_IO(READ, iocb,
1191 iov, pos, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (retval > 0)
1193 *ppos = pos + retval;
1194 }
Steven Whitehouse0e0bcae2006-09-27 14:45:07 -04001195 if (likely(retval != 0)) {
Steven Whitehouse3f1a9aa2006-09-27 14:52:48 -04001196 file_accessed(filp);
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -04001197 goto out;
Steven Whitehouse0e0bcae2006-09-27 14:45:07 -04001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
1201 retval = 0;
1202 if (count) {
1203 for (seg = 0; seg < nr_segs; seg++) {
1204 read_descriptor_t desc;
1205
1206 desc.written = 0;
1207 desc.arg.buf = iov[seg].iov_base;
1208 desc.count = iov[seg].iov_len;
1209 if (desc.count == 0)
1210 continue;
1211 desc.error = 0;
1212 do_generic_file_read(filp,ppos,&desc,file_read_actor);
1213 retval += desc.written;
Tejun Heo39e88ca2005-10-30 15:02:40 -08001214 if (desc.error) {
1215 retval = retval ?: desc.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
1217 }
akpm@linux-foundation.orgc44939e2007-07-15 23:38:25 -07001218 if (desc.count > 0)
1219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 }
1222out:
1223 return retval;
1224}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225EXPORT_SYMBOL(generic_file_aio_read);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227static ssize_t
1228do_readahead(struct address_space *mapping, struct file *filp,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001229 pgoff_t index, unsigned long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1232 return -EINVAL;
1233
1234 force_page_cache_readahead(mapping, filp, index,
1235 max_sane_readahead(nr));
1236 return 0;
1237}
1238
1239asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1240{
1241 ssize_t ret;
1242 struct file *file;
1243
1244 ret = -EBADF;
1245 file = fget(fd);
1246 if (file) {
1247 if (file->f_mode & FMODE_READ) {
1248 struct address_space *mapping = file->f_mapping;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001249 pgoff_t start = offset >> PAGE_CACHE_SHIFT;
1250 pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 unsigned long len = end - start + 1;
1252 ret = do_readahead(mapping, file, start, len);
1253 }
1254 fput(file);
1255 }
1256 return ret;
1257}
1258
1259#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001260/**
1261 * page_cache_read - adds requested page to the page cache if not already there
1262 * @file: file to read
1263 * @offset: page index
1264 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * This adds the requested page to the page cache if it isn't already there,
1266 * and schedules an I/O to read in its contents from disk.
1267 */
Fengguang Wu57f6b962007-10-16 01:24:37 -07001268static int fastcall page_cache_read(struct file * file, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct address_space *mapping = file->f_mapping;
1271 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Zach Brown994fc28c2005-12-15 14:28:17 -08001274 do {
1275 page = page_cache_alloc_cold(mapping);
1276 if (!page)
1277 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Zach Brown994fc28c2005-12-15 14:28:17 -08001279 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1280 if (ret == 0)
1281 ret = mapping->a_ops->readpage(file, page);
1282 else if (ret == -EEXIST)
1283 ret = 0; /* losing race to add is OK */
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Zach Brown994fc28c2005-12-15 14:28:17 -08001287 } while (ret == AOP_TRUNCATED_PAGE);
1288
1289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
1292#define MMAP_LOTSAMISS (100)
1293
Randy Dunlap485bb992006-06-23 02:03:49 -07001294/**
Nick Piggin54cb8822007-07-19 01:46:59 -07001295 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07001296 * @vma: vma in which the fault was taken
1297 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07001298 *
Nick Piggin54cb8822007-07-19 01:46:59 -07001299 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * mapped memory region to read in file data during a page fault.
1301 *
1302 * The goto's are kind of ugly, but this streamlines the normal case of having
1303 * it in the page cache, and handles the special cases reasonably without
1304 * having a lot of duplicated code.
1305 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001306int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int error;
Nick Piggin54cb8822007-07-19 01:46:59 -07001309 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct address_space *mapping = file->f_mapping;
1311 struct file_ra_state *ra = &file->f_ra;
1312 struct inode *inode = mapping->host;
1313 struct page *page;
Nick Piggin54cb8822007-07-19 01:46:59 -07001314 unsigned long size;
1315 int did_readaround = 0;
Nick Piggin83c54072007-07-19 01:47:05 -07001316 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Nick Piggind0217ac2007-07-19 01:47:03 -07001319 if (vmf->pgoff >= size)
Linus Torvalds5307cc12007-10-31 09:19:46 -07001320 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* If we don't want any read-ahead, don't bother */
Nick Piggin54cb8822007-07-19 01:46:59 -07001323 if (VM_RandomReadHint(vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto no_cached_page;
1325
1326 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * Do we have something in the page cache already?
1328 */
1329retry_find:
Nick Piggind0217ac2007-07-19 01:47:03 -07001330 page = find_lock_page(mapping, vmf->pgoff);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001331 /*
1332 * For sequential accesses, we use the generic readahead logic.
1333 */
1334 if (VM_SequentialReadHint(vma)) {
1335 if (!page) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001336 page_cache_sync_readahead(mapping, ra, file,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001337 vmf->pgoff, 1);
1338 page = find_lock_page(mapping, vmf->pgoff);
1339 if (!page)
1340 goto no_cached_page;
1341 }
1342 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07001343 page_cache_async_readahead(mapping, ra, file, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001344 vmf->pgoff, 1);
1345 }
1346 }
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (!page) {
1349 unsigned long ra_pages;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ra->mmap_miss++;
1352
1353 /*
1354 * Do we miss much more than hit in this file? If so,
1355 * stop bothering with read-ahead. It will only hurt.
1356 */
Fengguang Wu0bb7ba62007-10-16 01:24:32 -07001357 if (ra->mmap_miss > MMAP_LOTSAMISS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto no_cached_page;
1359
1360 /*
1361 * To keep the pgmajfault counter straight, we need to
1362 * check did_readaround, as this is an inner loop.
1363 */
1364 if (!did_readaround) {
Nick Piggind0217ac2007-07-19 01:47:03 -07001365 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001366 count_vm_event(PGMAJFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 did_readaround = 1;
1369 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1370 if (ra_pages) {
1371 pgoff_t start = 0;
1372
Nick Piggind0217ac2007-07-19 01:47:03 -07001373 if (vmf->pgoff > ra_pages / 2)
1374 start = vmf->pgoff - ra_pages / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 do_page_cache_readahead(mapping, file, start, ra_pages);
1376 }
Nick Piggind0217ac2007-07-19 01:47:03 -07001377 page = find_lock_page(mapping, vmf->pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!page)
1379 goto no_cached_page;
1380 }
1381
1382 if (!did_readaround)
Fengguang Wu0bb7ba62007-10-16 01:24:32 -07001383 ra->mmap_miss--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
Nick Piggind00806b2007-07-19 01:46:57 -07001386 * We have a locked page in the page cache, now we need to check
1387 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
Nick Piggind00806b2007-07-19 01:46:57 -07001389 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto page_not_uptodate;
1391
Nick Piggind00806b2007-07-19 01:46:57 -07001392 /* Must recheck i_size under page lock */
1393 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Nick Piggind0217ac2007-07-19 01:47:03 -07001394 if (unlikely(vmf->pgoff >= size)) {
Nick Piggind00806b2007-07-19 01:46:57 -07001395 unlock_page(page);
Yan Zheng745ad482007-10-08 10:08:37 -07001396 page_cache_release(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07001397 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07001398 }
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
1401 * Found the page and have a reference on it.
1402 */
1403 mark_page_accessed(page);
Fengguang Wuf4e6b492007-10-16 01:24:33 -07001404 ra->prev_pos = (loff_t)page->index << PAGE_CACHE_SHIFT;
Nick Piggind0217ac2007-07-19 01:47:03 -07001405 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07001406 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408no_cached_page:
1409 /*
1410 * We're only likely to ever get here if MADV_RANDOM is in
1411 * effect.
1412 */
Nick Piggind0217ac2007-07-19 01:47:03 -07001413 error = page_cache_read(file, vmf->pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /*
1416 * The page we want has now been added to the page cache.
1417 * In the unlikely event that someone removed it in the
1418 * meantime, we'll just come back here and read it again.
1419 */
1420 if (error >= 0)
1421 goto retry_find;
1422
1423 /*
1424 * An error return from page_cache_read can result if the
1425 * system is low on memory, or a problem occurs while trying
1426 * to schedule I/O.
1427 */
1428 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07001429 return VM_FAULT_OOM;
1430 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432page_not_uptodate:
Nick Piggind00806b2007-07-19 01:46:57 -07001433 /* IO error path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!did_readaround) {
Nick Piggind0217ac2007-07-19 01:47:03 -07001435 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001436 count_vm_event(PGMAJFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /*
1440 * Umm, take care of errors if the page isn't up-to-date.
1441 * Try to re-read it _once_. We do this synchronously,
1442 * because there really aren't any performance issues here
1443 * and we need to check for errors.
1444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08001446 error = mapping->a_ops->readpage(file, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 page_cache_release(page);
Nick Piggind00806b2007-07-19 01:46:57 -07001448
1449 if (!error || error == AOP_TRUNCATED_PAGE)
1450 goto retry_find;
1451
1452 /* Things didn't work out. Return zero to tell the mm layer so. */
1453 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07001454 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07001455}
1456EXPORT_SYMBOL(filemap_fault);
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001459 .fault = filemap_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460};
1461
1462/* This is used for a general mmap of a disk file */
1463
1464int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1465{
1466 struct address_space *mapping = file->f_mapping;
1467
1468 if (!mapping->a_ops->readpage)
1469 return -ENOEXEC;
1470 file_accessed(file);
1471 vma->vm_ops = &generic_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001472 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
1474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476/*
1477 * This is for filesystems which do not implement ->writepage.
1478 */
1479int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1480{
1481 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1482 return -EINVAL;
1483 return generic_file_mmap(file, vma);
1484}
1485#else
1486int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1487{
1488 return -ENOSYS;
1489}
1490int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1491{
1492 return -ENOSYS;
1493}
1494#endif /* CONFIG_MMU */
1495
1496EXPORT_SYMBOL(generic_file_mmap);
1497EXPORT_SYMBOL(generic_file_readonly_mmap);
1498
Nick Piggin6fe69002007-05-06 14:49:04 -07001499static struct page *__read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001500 pgoff_t index,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int (*filler)(void *,struct page*),
1502 void *data)
1503{
Nick Piggineb2be182007-10-16 01:24:57 -07001504 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int err;
1506repeat:
1507 page = find_get_page(mapping, index);
1508 if (!page) {
Nick Piggineb2be182007-10-16 01:24:57 -07001509 page = page_cache_alloc_cold(mapping);
1510 if (!page)
1511 return ERR_PTR(-ENOMEM);
1512 err = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL);
1513 if (unlikely(err)) {
1514 page_cache_release(page);
1515 if (err == -EEXIST)
1516 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return ERR_PTR(err);
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 err = filler(data, page);
1521 if (err < 0) {
1522 page_cache_release(page);
1523 page = ERR_PTR(err);
1524 }
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return page;
1527}
1528
Nick Piggin6fe69002007-05-06 14:49:04 -07001529/*
1530 * Same as read_cache_page, but don't wait for page to become unlocked
1531 * after submitting it to the filler.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
Nick Piggin6fe69002007-05-06 14:49:04 -07001533struct page *read_cache_page_async(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001534 pgoff_t index,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int (*filler)(void *,struct page*),
1536 void *data)
1537{
1538 struct page *page;
1539 int err;
1540
1541retry:
1542 page = __read_cache_page(mapping, index, filler, data);
1543 if (IS_ERR(page))
David Howellsc855ff32007-05-09 13:42:20 +01001544 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (PageUptodate(page))
1546 goto out;
1547
1548 lock_page(page);
1549 if (!page->mapping) {
1550 unlock_page(page);
1551 page_cache_release(page);
1552 goto retry;
1553 }
1554 if (PageUptodate(page)) {
1555 unlock_page(page);
1556 goto out;
1557 }
1558 err = filler(data, page);
1559 if (err < 0) {
1560 page_cache_release(page);
David Howellsc855ff32007-05-09 13:42:20 +01001561 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
David Howellsc855ff32007-05-09 13:42:20 +01001563out:
Nick Piggin6fe69002007-05-06 14:49:04 -07001564 mark_page_accessed(page);
1565 return page;
1566}
1567EXPORT_SYMBOL(read_cache_page_async);
1568
1569/**
1570 * read_cache_page - read into page cache, fill it if needed
1571 * @mapping: the page's address_space
1572 * @index: the page index
1573 * @filler: function to perform the read
1574 * @data: destination for read data
1575 *
1576 * Read into the page cache. If a page already exists, and PageUptodate() is
1577 * not set, try to fill the page then wait for it to become unlocked.
1578 *
1579 * If the page does not get brought uptodate, return -EIO.
1580 */
1581struct page *read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07001582 pgoff_t index,
Nick Piggin6fe69002007-05-06 14:49:04 -07001583 int (*filler)(void *,struct page*),
1584 void *data)
1585{
1586 struct page *page;
1587
1588 page = read_cache_page_async(mapping, index, filler, data);
1589 if (IS_ERR(page))
1590 goto out;
1591 wait_on_page_locked(page);
1592 if (!PageUptodate(page)) {
1593 page_cache_release(page);
1594 page = ERR_PTR(-EIO);
1595 }
1596 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return page;
1598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599EXPORT_SYMBOL(read_cache_page);
1600
1601/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 * The logic we want is
1603 *
1604 * if suid or (sgid and xgrp)
1605 * remove privs
1606 */
Jens Axboe01de85e2006-10-17 19:50:36 +02001607int should_remove_suid(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 mode_t mode = dentry->d_inode->i_mode;
1610 int kill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /* suid always must be killed */
1613 if (unlikely(mode & S_ISUID))
1614 kill = ATTR_KILL_SUID;
1615
1616 /*
1617 * sgid without any exec bits is just a mandatory locking mark; leave
1618 * it alone. If some exec bits are set, it's a real sgid; kill it.
1619 */
1620 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1621 kill |= ATTR_KILL_SGID;
1622
Jens Axboe01de85e2006-10-17 19:50:36 +02001623 if (unlikely(kill && !capable(CAP_FSETID)))
1624 return kill;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Jens Axboe01de85e2006-10-17 19:50:36 +02001626 return 0;
1627}
Mark Fashehd23a1472006-10-17 17:05:18 -07001628EXPORT_SYMBOL(should_remove_suid);
Jens Axboe01de85e2006-10-17 19:50:36 +02001629
1630int __remove_suid(struct dentry *dentry, int kill)
1631{
1632 struct iattr newattrs;
1633
1634 newattrs.ia_valid = ATTR_FORCE | kill;
1635 return notify_change(dentry, &newattrs);
1636}
1637
1638int remove_suid(struct dentry *dentry)
1639{
Serge E. Hallynb5376772007-10-16 23:31:36 -07001640 int killsuid = should_remove_suid(dentry);
1641 int killpriv = security_inode_need_killpriv(dentry);
1642 int error = 0;
Jens Axboe01de85e2006-10-17 19:50:36 +02001643
Serge E. Hallynb5376772007-10-16 23:31:36 -07001644 if (killpriv < 0)
1645 return killpriv;
1646 if (killpriv)
1647 error = security_inode_killpriv(dentry);
1648 if (!error && killsuid)
1649 error = __remove_suid(dentry, killsuid);
Jens Axboe01de85e2006-10-17 19:50:36 +02001650
Serge E. Hallynb5376772007-10-16 23:31:36 -07001651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653EXPORT_SYMBOL(remove_suid);
1654
Nick Piggin2f718ff2007-10-16 01:24:59 -07001655static size_t __iovec_copy_from_user_inatomic(char *vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 const struct iovec *iov, size_t base, size_t bytes)
1657{
1658 size_t copied = 0, left = 0;
1659
1660 while (bytes) {
1661 char __user *buf = iov->iov_base + base;
1662 int copy = min(bytes, iov->iov_len - base);
1663
1664 base = 0;
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07001665 left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 copied += copy;
1667 bytes -= copy;
1668 vaddr += copy;
1669 iov++;
1670
NeilBrown01408c42006-06-25 05:47:58 -07001671 if (unlikely(left))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 return copied - left;
1675}
1676
1677/*
Nick Piggin2f718ff2007-10-16 01:24:59 -07001678 * Copy as much as we can into the page and return the number of bytes which
1679 * were sucessfully copied. If a fault is encountered then return the number of
1680 * bytes which were copied.
1681 */
1682size_t iov_iter_copy_from_user_atomic(struct page *page,
1683 struct iov_iter *i, unsigned long offset, size_t bytes)
1684{
1685 char *kaddr;
1686 size_t copied;
1687
1688 BUG_ON(!in_atomic());
1689 kaddr = kmap_atomic(page, KM_USER0);
1690 if (likely(i->nr_segs == 1)) {
1691 int left;
1692 char __user *buf = i->iov->iov_base + i->iov_offset;
1693 left = __copy_from_user_inatomic_nocache(kaddr + offset,
1694 buf, bytes);
1695 copied = bytes - left;
1696 } else {
1697 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
1698 i->iov, i->iov_offset, bytes);
1699 }
1700 kunmap_atomic(kaddr, KM_USER0);
1701
1702 return copied;
1703}
Nick Piggin89e10782007-10-16 01:25:07 -07001704EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
Nick Piggin2f718ff2007-10-16 01:24:59 -07001705
1706/*
1707 * This has the same sideeffects and return value as
1708 * iov_iter_copy_from_user_atomic().
1709 * The difference is that it attempts to resolve faults.
1710 * Page must not be locked.
1711 */
1712size_t iov_iter_copy_from_user(struct page *page,
1713 struct iov_iter *i, unsigned long offset, size_t bytes)
1714{
1715 char *kaddr;
1716 size_t copied;
1717
1718 kaddr = kmap(page);
1719 if (likely(i->nr_segs == 1)) {
1720 int left;
1721 char __user *buf = i->iov->iov_base + i->iov_offset;
1722 left = __copy_from_user_nocache(kaddr + offset, buf, bytes);
1723 copied = bytes - left;
1724 } else {
1725 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
1726 i->iov, i->iov_offset, bytes);
1727 }
1728 kunmap(page);
1729 return copied;
1730}
Nick Piggin89e10782007-10-16 01:25:07 -07001731EXPORT_SYMBOL(iov_iter_copy_from_user);
Nick Piggin2f718ff2007-10-16 01:24:59 -07001732
1733static void __iov_iter_advance_iov(struct iov_iter *i, size_t bytes)
1734{
1735 if (likely(i->nr_segs == 1)) {
1736 i->iov_offset += bytes;
1737 } else {
1738 const struct iovec *iov = i->iov;
1739 size_t base = i->iov_offset;
1740
1741 while (bytes) {
1742 int copy = min(bytes, iov->iov_len - base);
1743
1744 bytes -= copy;
1745 base += copy;
1746 if (iov->iov_len == base) {
1747 iov++;
1748 base = 0;
1749 }
1750 }
1751 i->iov = iov;
1752 i->iov_offset = base;
1753 }
1754}
1755
1756void iov_iter_advance(struct iov_iter *i, size_t bytes)
1757{
1758 BUG_ON(i->count < bytes);
1759
1760 __iov_iter_advance_iov(i, bytes);
1761 i->count -= bytes;
1762}
Nick Piggin89e10782007-10-16 01:25:07 -07001763EXPORT_SYMBOL(iov_iter_advance);
Nick Piggin2f718ff2007-10-16 01:24:59 -07001764
Nick Pigginafddba42007-10-16 01:25:01 -07001765/*
1766 * Fault in the first iovec of the given iov_iter, to a maximum length
1767 * of bytes. Returns 0 on success, or non-zero if the memory could not be
1768 * accessed (ie. because it is an invalid address).
1769 *
1770 * writev-intensive code may want this to prefault several iovecs -- that
1771 * would be possible (callers must not rely on the fact that _only_ the
1772 * first iovec will be faulted with the current implementation).
1773 */
1774int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
Nick Piggin2f718ff2007-10-16 01:24:59 -07001775{
Nick Piggin2f718ff2007-10-16 01:24:59 -07001776 char __user *buf = i->iov->iov_base + i->iov_offset;
Nick Pigginafddba42007-10-16 01:25:01 -07001777 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
1778 return fault_in_pages_readable(buf, bytes);
Nick Piggin2f718ff2007-10-16 01:24:59 -07001779}
Nick Piggin89e10782007-10-16 01:25:07 -07001780EXPORT_SYMBOL(iov_iter_fault_in_readable);
Nick Piggin2f718ff2007-10-16 01:24:59 -07001781
1782/*
1783 * Return the count of just the current iov_iter segment.
1784 */
1785size_t iov_iter_single_seg_count(struct iov_iter *i)
1786{
1787 const struct iovec *iov = i->iov;
1788 if (i->nr_segs == 1)
1789 return i->count;
1790 else
1791 return min(i->count, iov->iov_len - i->iov_offset);
1792}
Nick Piggin89e10782007-10-16 01:25:07 -07001793EXPORT_SYMBOL(iov_iter_single_seg_count);
Nick Piggin2f718ff2007-10-16 01:24:59 -07001794
1795/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * Performs necessary checks before doing a write
1797 *
Randy Dunlap485bb992006-06-23 02:03:49 -07001798 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * Returns appropriate error code that caller should return or
1800 * zero in case that write should be allowed.
1801 */
1802inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1803{
1804 struct inode *inode = file->f_mapping->host;
1805 unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1806
1807 if (unlikely(*pos < 0))
1808 return -EINVAL;
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (!isblk) {
1811 /* FIXME: this is for backwards compatibility with 2.4 */
1812 if (file->f_flags & O_APPEND)
1813 *pos = i_size_read(inode);
1814
1815 if (limit != RLIM_INFINITY) {
1816 if (*pos >= limit) {
1817 send_sig(SIGXFSZ, current, 0);
1818 return -EFBIG;
1819 }
1820 if (*count > limit - (typeof(limit))*pos) {
1821 *count = limit - (typeof(limit))*pos;
1822 }
1823 }
1824 }
1825
1826 /*
1827 * LFS rule
1828 */
1829 if (unlikely(*pos + *count > MAX_NON_LFS &&
1830 !(file->f_flags & O_LARGEFILE))) {
1831 if (*pos >= MAX_NON_LFS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -EFBIG;
1833 }
1834 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1835 *count = MAX_NON_LFS - (unsigned long)*pos;
1836 }
1837 }
1838
1839 /*
1840 * Are we about to exceed the fs block limit ?
1841 *
1842 * If we have written data it becomes a short write. If we have
1843 * exceeded without writing data we send a signal and return EFBIG.
1844 * Linus frestrict idea will clean these up nicely..
1845 */
1846 if (likely(!isblk)) {
1847 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1848 if (*count || *pos > inode->i_sb->s_maxbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return -EFBIG;
1850 }
1851 /* zero-length writes at ->s_maxbytes are OK */
1852 }
1853
1854 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1855 *count = inode->i_sb->s_maxbytes - *pos;
1856 } else {
David Howells93614012006-09-30 20:45:40 +02001857#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 loff_t isize;
1859 if (bdev_read_only(I_BDEV(inode)))
1860 return -EPERM;
1861 isize = i_size_read(inode);
1862 if (*pos >= isize) {
1863 if (*count || *pos > isize)
1864 return -ENOSPC;
1865 }
1866
1867 if (*pos + *count > isize)
1868 *count = isize - *pos;
David Howells93614012006-09-30 20:45:40 +02001869#else
1870 return -EPERM;
1871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 return 0;
1874}
1875EXPORT_SYMBOL(generic_write_checks);
1876
Nick Pigginafddba42007-10-16 01:25:01 -07001877int pagecache_write_begin(struct file *file, struct address_space *mapping,
1878 loff_t pos, unsigned len, unsigned flags,
1879 struct page **pagep, void **fsdata)
1880{
1881 const struct address_space_operations *aops = mapping->a_ops;
1882
1883 if (aops->write_begin) {
1884 return aops->write_begin(file, mapping, pos, len, flags,
1885 pagep, fsdata);
1886 } else {
1887 int ret;
1888 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1889 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1890 struct inode *inode = mapping->host;
1891 struct page *page;
1892again:
1893 page = __grab_cache_page(mapping, index);
1894 *pagep = page;
1895 if (!page)
1896 return -ENOMEM;
1897
1898 if (flags & AOP_FLAG_UNINTERRUPTIBLE && !PageUptodate(page)) {
1899 /*
1900 * There is no way to resolve a short write situation
1901 * for a !Uptodate page (except by double copying in
1902 * the caller done by generic_perform_write_2copy).
1903 *
1904 * Instead, we have to bring it uptodate here.
1905 */
1906 ret = aops->readpage(file, page);
1907 page_cache_release(page);
1908 if (ret) {
1909 if (ret == AOP_TRUNCATED_PAGE)
1910 goto again;
1911 return ret;
1912 }
1913 goto again;
1914 }
1915
1916 ret = aops->prepare_write(file, page, offset, offset+len);
1917 if (ret) {
Nick Piggin55144762007-10-16 01:25:26 -07001918 unlock_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -07001919 page_cache_release(page);
1920 if (pos + len > inode->i_size)
1921 vmtruncate(inode, inode->i_size);
Nick Pigginafddba42007-10-16 01:25:01 -07001922 }
1923 return ret;
1924 }
1925}
1926EXPORT_SYMBOL(pagecache_write_begin);
1927
1928int pagecache_write_end(struct file *file, struct address_space *mapping,
1929 loff_t pos, unsigned len, unsigned copied,
1930 struct page *page, void *fsdata)
1931{
1932 const struct address_space_operations *aops = mapping->a_ops;
1933 int ret;
1934
1935 if (aops->write_end) {
1936 mark_page_accessed(page);
1937 ret = aops->write_end(file, mapping, pos, len, copied,
1938 page, fsdata);
1939 } else {
1940 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1941 struct inode *inode = mapping->host;
1942
1943 flush_dcache_page(page);
1944 ret = aops->commit_write(file, page, offset, offset+len);
1945 unlock_page(page);
1946 mark_page_accessed(page);
1947 page_cache_release(page);
Nick Pigginafddba42007-10-16 01:25:01 -07001948
1949 if (ret < 0) {
1950 if (pos + len > inode->i_size)
1951 vmtruncate(inode, inode->i_size);
1952 } else if (ret > 0)
1953 ret = min_t(size_t, copied, ret);
1954 else
1955 ret = copied;
1956 }
1957
1958 return ret;
1959}
1960EXPORT_SYMBOL(pagecache_write_end);
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962ssize_t
1963generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1964 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1965 size_t count, size_t ocount)
1966{
1967 struct file *file = iocb->ki_filp;
1968 struct address_space *mapping = file->f_mapping;
1969 struct inode *inode = mapping->host;
1970 ssize_t written;
1971
1972 if (count != ocount)
1973 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1974
1975 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1976 if (written > 0) {
1977 loff_t end = pos + written;
1978 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1979 i_size_write(inode, end);
1980 mark_inode_dirty(inode);
1981 }
1982 *ppos = end;
1983 }
1984
1985 /*
1986 * Sync the fs metadata but not the minor inode changes and
1987 * of course not the data as we did direct DMA for the IO.
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001988 * i_mutex is held, which protects generic_osync_inode() from
Zach Brown8459d862006-12-10 02:21:05 -08001989 * livelocking. AIO O_DIRECT ops attempt to sync metadata here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 */
Zach Brown8459d862006-12-10 02:21:05 -08001991 if ((written >= 0 || written == -EIOCBQUEUED) &&
1992 ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
Hifumi Hisashi1e8a81c2005-06-25 14:54:32 -07001993 int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
1994 if (err < 0)
1995 written = err;
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return written;
1998}
1999EXPORT_SYMBOL(generic_file_direct_write);
2000
Nick Piggineb2be182007-10-16 01:24:57 -07002001/*
2002 * Find or create a page at the given pagecache position. Return the locked
2003 * page. This function is specifically for buffered writes.
2004 */
Nick Pigginafddba42007-10-16 01:25:01 -07002005struct page *__grab_cache_page(struct address_space *mapping, pgoff_t index)
Nick Piggineb2be182007-10-16 01:24:57 -07002006{
2007 int status;
2008 struct page *page;
2009repeat:
2010 page = find_lock_page(mapping, index);
2011 if (likely(page))
2012 return page;
2013
2014 page = page_cache_alloc(mapping);
2015 if (!page)
2016 return NULL;
2017 status = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL);
2018 if (unlikely(status)) {
2019 page_cache_release(page);
2020 if (status == -EEXIST)
2021 goto repeat;
2022 return NULL;
2023 }
2024 return page;
2025}
Nick Pigginafddba42007-10-16 01:25:01 -07002026EXPORT_SYMBOL(__grab_cache_page);
Nick Piggineb2be182007-10-16 01:24:57 -07002027
Nick Pigginafddba42007-10-16 01:25:01 -07002028static ssize_t generic_perform_write_2copy(struct file *file,
2029 struct iov_iter *i, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Andrew Mortonae374612007-10-16 01:24:55 -07002031 struct address_space *mapping = file->f_mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002032 const struct address_space_operations *a_ops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002033 struct inode *inode = mapping->host;
2034 long status = 0;
2035 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 do {
Nick Piggin08291422007-10-16 01:24:59 -07002038 struct page *src_page;
Nick Piggineb2be182007-10-16 01:24:57 -07002039 struct page *page;
Andrew Mortonae374612007-10-16 01:24:55 -07002040 pgoff_t index; /* Pagecache index for current page */
2041 unsigned long offset; /* Offset into pagecache page */
Nick Piggin08291422007-10-16 01:24:59 -07002042 unsigned long bytes; /* Bytes to write to page */
Andrew Mortonae374612007-10-16 01:24:55 -07002043 size_t copied; /* Bytes copied from user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Andrew Mortonae374612007-10-16 01:24:55 -07002045 offset = (pos & (PAGE_CACHE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin2f718ff2007-10-16 01:24:59 -07002047 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07002048 iov_iter_count(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Nick Piggin08291422007-10-16 01:24:59 -07002050 /*
2051 * a non-NULL src_page indicates that we're doing the
2052 * copy via get_user_pages and kmap.
2053 */
2054 src_page = NULL;
Andrew Mortonae374612007-10-16 01:24:55 -07002055
Nick Piggin41cb8ac2007-10-16 01:24:53 -07002056 /*
2057 * Bring in the user page that we will copy from _first_.
2058 * Otherwise there's a nasty deadlock on copying from the
2059 * same page as we're writing to, without it being marked
2060 * up-to-date.
Nick Piggin08291422007-10-16 01:24:59 -07002061 *
2062 * Not only is this an optimisation, but it is also required
2063 * to check that the address is actually valid, when atomic
2064 * usercopies are used, below.
Nick Piggin41cb8ac2007-10-16 01:24:53 -07002065 */
Nick Pigginafddba42007-10-16 01:25:01 -07002066 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
Nick Piggin08291422007-10-16 01:24:59 -07002067 status = -EFAULT;
2068 break;
2069 }
Nick Piggineb2be182007-10-16 01:24:57 -07002070
2071 page = __grab_cache_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (!page) {
2073 status = -ENOMEM;
2074 break;
2075 }
2076
Nick Piggin08291422007-10-16 01:24:59 -07002077 /*
2078 * non-uptodate pages cannot cope with short copies, and we
2079 * cannot take a pagefault with the destination page locked.
2080 * So pin the source page to copy it.
2081 */
Nick Piggin674b8922007-10-16 01:25:03 -07002082 if (!PageUptodate(page) && !segment_eq(get_fs(), KERNEL_DS)) {
Nick Piggin08291422007-10-16 01:24:59 -07002083 unlock_page(page);
2084
2085 src_page = alloc_page(GFP_KERNEL);
2086 if (!src_page) {
2087 page_cache_release(page);
2088 status = -ENOMEM;
2089 break;
2090 }
2091
2092 /*
2093 * Cannot get_user_pages with a page locked for the
2094 * same reason as we can't take a page fault with a
2095 * page locked (as explained below).
2096 */
Nick Pigginafddba42007-10-16 01:25:01 -07002097 copied = iov_iter_copy_from_user(src_page, i,
Nick Piggin2f718ff2007-10-16 01:24:59 -07002098 offset, bytes);
Nick Piggin08291422007-10-16 01:24:59 -07002099 if (unlikely(copied == 0)) {
2100 status = -EFAULT;
2101 page_cache_release(page);
2102 page_cache_release(src_page);
2103 break;
2104 }
2105 bytes = copied;
2106
2107 lock_page(page);
2108 /*
2109 * Can't handle the page going uptodate here, because
2110 * that means we would use non-atomic usercopies, which
2111 * zero out the tail of the page, which can cause
2112 * zeroes to become transiently visible. We could just
2113 * use a non-zeroing copy, but the APIs aren't too
2114 * consistent.
2115 */
2116 if (unlikely(!page->mapping || PageUptodate(page))) {
2117 unlock_page(page);
2118 page_cache_release(page);
2119 page_cache_release(src_page);
2120 continue;
2121 }
Nick Piggin08291422007-10-16 01:24:59 -07002122 }
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 status = a_ops->prepare_write(file, page, offset, offset+bytes);
Nick Piggin64649a52007-10-16 01:24:56 -07002125 if (unlikely(status))
2126 goto fs_write_aop_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002127
Nick Piggin08291422007-10-16 01:24:59 -07002128 if (!src_page) {
2129 /*
2130 * Must not enter the pagefault handler here, because
2131 * we hold the page lock, so we might recursively
2132 * deadlock on the same lock, or get an ABBA deadlock
2133 * against a different lock, or against the mmap_sem
2134 * (which nests outside the page lock). So increment
2135 * preempt count, and use _atomic usercopies.
2136 *
2137 * The page is uptodate so we are OK to encounter a
2138 * short copy: if unmodified parts of the page are
2139 * marked dirty and written out to disk, it doesn't
2140 * really matter.
2141 */
2142 pagefault_disable();
Nick Pigginafddba42007-10-16 01:25:01 -07002143 copied = iov_iter_copy_from_user_atomic(page, i,
Nick Piggin2f718ff2007-10-16 01:24:59 -07002144 offset, bytes);
Nick Piggin08291422007-10-16 01:24:59 -07002145 pagefault_enable();
2146 } else {
2147 void *src, *dst;
2148 src = kmap_atomic(src_page, KM_USER0);
2149 dst = kmap_atomic(page, KM_USER1);
2150 memcpy(dst + offset, src + offset, bytes);
2151 kunmap_atomic(dst, KM_USER1);
2152 kunmap_atomic(src, KM_USER0);
2153 copied = bytes;
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 flush_dcache_page(page);
Nick Piggin4a9e5ef2007-10-16 01:24:58 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 status = a_ops->commit_write(file, page, offset, offset+bytes);
Nick Piggin55144762007-10-16 01:25:26 -07002158 if (unlikely(status < 0))
Nick Piggin64649a52007-10-16 01:24:56 -07002159 goto fs_write_aop_error;
Nick Piggin64649a52007-10-16 01:24:56 -07002160 if (unlikely(status > 0)) /* filesystem did partial write */
Nick Piggin08291422007-10-16 01:24:59 -07002161 copied = min_t(size_t, copied, status);
2162
2163 unlock_page(page);
2164 mark_page_accessed(page);
2165 page_cache_release(page);
2166 if (src_page)
2167 page_cache_release(src_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Nick Pigginafddba42007-10-16 01:25:01 -07002169 iov_iter_advance(i, copied);
Nick Piggin4a9e5ef2007-10-16 01:24:58 -07002170 pos += copied;
Nick Pigginafddba42007-10-16 01:25:01 -07002171 written += copied;
Nick Piggin4a9e5ef2007-10-16 01:24:58 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 balance_dirty_pages_ratelimited(mapping);
2174 cond_resched();
Nick Piggin64649a52007-10-16 01:24:56 -07002175 continue;
2176
2177fs_write_aop_error:
Nick Piggin55144762007-10-16 01:25:26 -07002178 unlock_page(page);
Nick Piggin64649a52007-10-16 01:24:56 -07002179 page_cache_release(page);
Nick Piggin08291422007-10-16 01:24:59 -07002180 if (src_page)
2181 page_cache_release(src_page);
Nick Piggin64649a52007-10-16 01:24:56 -07002182
2183 /*
2184 * prepare_write() may have instantiated a few blocks
2185 * outside i_size. Trim these off again. Don't need
2186 * i_size_read because we hold i_mutex.
2187 */
2188 if (pos + bytes > inode->i_size)
2189 vmtruncate(inode, inode->i_size);
Nick Piggin55144762007-10-16 01:25:26 -07002190 break;
Nick Pigginafddba42007-10-16 01:25:01 -07002191 } while (iov_iter_count(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Nick Pigginafddba42007-10-16 01:25:01 -07002193 return written ? written : status;
2194}
2195
2196static ssize_t generic_perform_write(struct file *file,
2197 struct iov_iter *i, loff_t pos)
2198{
2199 struct address_space *mapping = file->f_mapping;
2200 const struct address_space_operations *a_ops = mapping->a_ops;
2201 long status = 0;
2202 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002203 unsigned int flags = 0;
2204
2205 /*
2206 * Copies from kernel address space cannot fail (NFSD is a big user).
2207 */
2208 if (segment_eq(get_fs(), KERNEL_DS))
2209 flags |= AOP_FLAG_UNINTERRUPTIBLE;
Nick Pigginafddba42007-10-16 01:25:01 -07002210
2211 do {
2212 struct page *page;
2213 pgoff_t index; /* Pagecache index for current page */
2214 unsigned long offset; /* Offset into pagecache page */
2215 unsigned long bytes; /* Bytes to write to page */
2216 size_t copied; /* Bytes copied from user */
2217 void *fsdata;
2218
2219 offset = (pos & (PAGE_CACHE_SIZE - 1));
2220 index = pos >> PAGE_CACHE_SHIFT;
2221 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2222 iov_iter_count(i));
2223
2224again:
2225
2226 /*
2227 * Bring in the user page that we will copy from _first_.
2228 * Otherwise there's a nasty deadlock on copying from the
2229 * same page as we're writing to, without it being marked
2230 * up-to-date.
2231 *
2232 * Not only is this an optimisation, but it is also required
2233 * to check that the address is actually valid, when atomic
2234 * usercopies are used, below.
2235 */
2236 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2237 status = -EFAULT;
2238 break;
2239 }
2240
Nick Piggin674b8922007-10-16 01:25:03 -07002241 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002242 &page, &fsdata);
2243 if (unlikely(status))
2244 break;
2245
2246 pagefault_disable();
2247 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
2248 pagefault_enable();
2249 flush_dcache_page(page);
2250
2251 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2252 page, fsdata);
2253 if (unlikely(status < 0))
2254 break;
2255 copied = status;
2256
2257 cond_resched();
2258
2259 if (unlikely(copied == 0)) {
2260 /*
2261 * If we were unable to copy any data at all, we must
2262 * fall back to a single segment length write.
2263 *
2264 * If we didn't fallback here, we could livelock
2265 * because not all segments in the iov can be copied at
2266 * once without a pagefault.
2267 */
2268 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2269 iov_iter_single_seg_count(i));
2270 goto again;
2271 }
2272 iov_iter_advance(i, copied);
2273 pos += copied;
2274 written += copied;
2275
2276 balance_dirty_pages_ratelimited(mapping);
2277
2278 } while (iov_iter_count(i));
2279
2280 return written ? written : status;
2281}
2282
2283ssize_t
2284generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2285 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2286 size_t count, ssize_t written)
2287{
2288 struct file *file = iocb->ki_filp;
2289 struct address_space *mapping = file->f_mapping;
2290 const struct address_space_operations *a_ops = mapping->a_ops;
2291 struct inode *inode = mapping->host;
2292 ssize_t status;
2293 struct iov_iter i;
2294
2295 iov_iter_init(&i, iov, nr_segs, count, written);
2296 if (a_ops->write_begin)
2297 status = generic_perform_write(file, &i, pos);
2298 else
2299 status = generic_perform_write_2copy(file, &i, pos);
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (likely(status >= 0)) {
Nick Pigginafddba42007-10-16 01:25:01 -07002302 written += status;
2303 *ppos = pos + status;
2304
2305 /*
2306 * For now, when the user asks for O_SYNC, we'll actually give
2307 * O_DSYNC
2308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2310 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2311 status = generic_osync_inode(inode, mapping,
2312 OSYNC_METADATA|OSYNC_DATA);
2313 }
2314 }
2315
2316 /*
2317 * If we get here for O_DIRECT writes then we must have fallen through
2318 * to buffered writes (block instantiation inside i_size). So we sync
2319 * the file data here, to try to honour O_DIRECT expectations.
2320 */
2321 if (unlikely(file->f_flags & O_DIRECT) && written)
2322 status = filemap_write_and_wait(mapping);
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return written ? written : status;
2325}
2326EXPORT_SYMBOL(generic_file_buffered_write);
2327
Adrian Bunk5ce78522005-09-10 00:26:28 -07002328static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329__generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2330 unsigned long nr_segs, loff_t *ppos)
2331{
2332 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002333 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 size_t ocount; /* original count */
2335 size_t count; /* after file limit checks */
2336 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 loff_t pos;
2338 ssize_t written;
2339 ssize_t err;
2340
2341 ocount = 0;
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -07002342 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2343 if (err)
2344 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 count = ocount;
2347 pos = *ppos;
2348
2349 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2350
2351 /* We can write back this queue in page reclaim */
2352 current->backing_dev_info = mapping->backing_dev_info;
2353 written = 0;
2354
2355 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2356 if (err)
2357 goto out;
2358
2359 if (count == 0)
2360 goto out;
2361
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08002362 err = remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (err)
2364 goto out;
2365
Christoph Hellwig870f4812006-01-09 20:52:01 -08002366 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2369 if (unlikely(file->f_flags & O_DIRECT)) {
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002370 loff_t endbyte;
2371 ssize_t written_buffered;
2372
2373 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
2374 ppos, count, ocount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (written < 0 || written == count)
2376 goto out;
2377 /*
2378 * direct-io write to a hole: fall through to buffered I/O
2379 * for completing the rest of the request.
2380 */
2381 pos += written;
2382 count -= written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002383 written_buffered = generic_file_buffered_write(iocb, iov,
2384 nr_segs, pos, ppos, count,
2385 written);
2386 /*
2387 * If generic_file_buffered_write() retuned a synchronous error
2388 * then we want to return the number of bytes which were
2389 * direct-written, or the error code if that was zero. Note
2390 * that this differs from normal direct-io semantics, which
2391 * will return -EFOO even if some bytes were written.
2392 */
2393 if (written_buffered < 0) {
2394 err = written_buffered;
2395 goto out;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002398 /*
2399 * We need to ensure that the page cache pages are written to
2400 * disk and invalidated to preserve the expected O_DIRECT
2401 * semantics.
2402 */
2403 endbyte = pos + written_buffered - written - 1;
Mark Fashehef51c972007-05-08 00:27:10 -07002404 err = do_sync_mapping_range(file->f_mapping, pos, endbyte,
2405 SYNC_FILE_RANGE_WAIT_BEFORE|
2406 SYNC_FILE_RANGE_WRITE|
2407 SYNC_FILE_RANGE_WAIT_AFTER);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07002408 if (err == 0) {
2409 written = written_buffered;
2410 invalidate_mapping_pages(mapping,
2411 pos >> PAGE_CACHE_SHIFT,
2412 endbyte >> PAGE_CACHE_SHIFT);
2413 } else {
2414 /*
2415 * We don't know how much we wrote, so just return
2416 * the number of bytes which were direct-written
2417 */
2418 }
2419 } else {
2420 written = generic_file_buffered_write(iocb, iov, nr_segs,
2421 pos, ppos, count, written);
2422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423out:
2424 current->backing_dev_info = NULL;
2425 return written ? written : err;
2426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Badari Pulavarty027445c2006-09-30 23:28:46 -07002428ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
2429 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 struct file *file = iocb->ki_filp;
2432 struct address_space *mapping = file->f_mapping;
2433 struct inode *inode = mapping->host;
2434 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Badari Pulavarty027445c2006-09-30 23:28:46 -07002436 BUG_ON(iocb->ki_pos != pos);
2437
2438 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
2439 &iocb->ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
Badari Pulavarty027445c2006-09-30 23:28:46 -07002442 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 err = sync_page_range_nolock(inode, mapping, pos, ret);
2445 if (err < 0)
2446 ret = err;
2447 }
2448 return ret;
2449}
Badari Pulavarty027445c2006-09-30 23:28:46 -07002450EXPORT_SYMBOL(generic_file_aio_write_nolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Badari Pulavarty027445c2006-09-30 23:28:46 -07002452ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2453 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 struct file *file = iocb->ki_filp;
2456 struct address_space *mapping = file->f_mapping;
2457 struct inode *inode = mapping->host;
2458 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 BUG_ON(iocb->ki_pos != pos);
2461
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002462 mutex_lock(&inode->i_mutex);
Badari Pulavarty027445c2006-09-30 23:28:46 -07002463 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
2464 &iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002465 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2468 ssize_t err;
2469
2470 err = sync_page_range(inode, mapping, pos, ret);
2471 if (err < 0)
2472 ret = err;
2473 }
2474 return ret;
2475}
2476EXPORT_SYMBOL(generic_file_aio_write);
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002479 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 * went wrong during pagecache shootdown.
2481 */
Adrian Bunk5ce78522005-09-10 00:26:28 -07002482static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2484 loff_t offset, unsigned long nr_segs)
2485{
2486 struct file *file = iocb->ki_filp;
2487 struct address_space *mapping = file->f_mapping;
2488 ssize_t retval;
Zach Brown65b82912007-03-16 13:38:11 -08002489 size_t write_len;
2490 pgoff_t end = 0; /* silence gcc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 /*
2493 * If it's a write, unmap all mmappings of the file up-front. This
2494 * will cause any pte dirty bits to be propagated into the pageframes
2495 * for the subsequent filemap_write_and_wait().
2496 */
2497 if (rw == WRITE) {
2498 write_len = iov_length(iov, nr_segs);
Zach Brown65b82912007-03-16 13:38:11 -08002499 end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (mapping_mapped(mapping))
2501 unmap_mapping_range(mapping, offset, write_len, 0);
2502 }
2503
2504 retval = filemap_write_and_wait(mapping);
Zach Brown65b82912007-03-16 13:38:11 -08002505 if (retval)
2506 goto out;
2507
2508 /*
2509 * After a write we want buffered reads to be sure to go to disk to get
2510 * the new data. We invalidate clean cached page from the region we're
2511 * about to write. We do this *before* the write so that we can return
2512 * -EIO without clobbering -EIOCBQUEUED from ->direct_IO().
2513 */
2514 if (rw == WRITE && mapping->nrpages) {
2515 retval = invalidate_inode_pages2_range(mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 offset >> PAGE_CACHE_SHIFT, end);
Zach Brown65b82912007-03-16 13:38:11 -08002517 if (retval)
2518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Zach Brown65b82912007-03-16 13:38:11 -08002520
2521 retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
Zach Brown65b82912007-03-16 13:38:11 -08002522
2523 /*
2524 * Finally, try again to invalidate clean pages which might have been
Zach Brownbdb76ef2007-10-30 11:45:46 -07002525 * cached by non-direct readahead, or faulted in by get_user_pages()
2526 * if the source of the write was an mmap'ed region of the file
2527 * we're writing. Either one is a pretty crazy thing to do,
2528 * so we don't support it 100%. If this invalidation
2529 * fails, tough, the write still worked...
Zach Brown65b82912007-03-16 13:38:11 -08002530 */
2531 if (rw == WRITE && mapping->nrpages) {
Zach Brownbdb76ef2007-10-30 11:45:46 -07002532 invalidate_inode_pages2_range(mapping, offset >> PAGE_CACHE_SHIFT, end);
Zach Brown65b82912007-03-16 13:38:11 -08002533 }
2534out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return retval;
2536}
David Howellscf9a2ae2006-08-29 19:05:54 +01002537
2538/**
2539 * try_to_release_page() - release old fs-specific metadata on a page
2540 *
2541 * @page: the page which the kernel is trying to free
2542 * @gfp_mask: memory allocation flags (and I/O mode)
2543 *
2544 * The address_space is to try to release any data against the page
2545 * (presumably at page->private). If the release was successful, return `1'.
2546 * Otherwise return zero.
2547 *
2548 * The @gfp_mask argument specifies whether I/O may be performed to release
2549 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
2550 *
2551 * NOTE: @gfp_mask may go away, and this function may become non-blocking.
2552 */
2553int try_to_release_page(struct page *page, gfp_t gfp_mask)
2554{
2555 struct address_space * const mapping = page->mapping;
2556
2557 BUG_ON(!PageLocked(page));
2558 if (PageWriteback(page))
2559 return 0;
2560
2561 if (mapping && mapping->a_ops->releasepage)
2562 return mapping->a_ops->releasepage(page, gfp_mask);
2563 return try_to_free_buffers(page);
2564}
2565
2566EXPORT_SYMBOL(try_to_release_page);