blob: a92d690b3ae53676d529398230dc6a0d806ce888 [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>
28#include <linux/pagevec.h>
29#include <linux/blkdev.h>
30#include <linux/security.h>
31#include <linux/syscalls.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080032#include <linux/cpuset.h>
Carsten Otteceffc072005-06-23 22:05:25 -070033#include "filemap.h"
Nick Piggin0f8053a2006-03-22 00:08:33 -080034#include "internal.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * FIXME: remove all knowledge of the buffer layer from the core VM
38 */
39#include <linux/buffer_head.h> /* for generic_osync_inode */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/mman.h>
42
Adrian Bunk5ce78522005-09-10 00:26:28 -070043static ssize_t
44generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
45 loff_t offset, unsigned long nr_segs);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Shared mappings implemented 30.11.1994. It's not fully working yet,
49 * though.
50 *
51 * Shared mappings now work. 15.8.1995 Bruno.
52 *
53 * finished 'unifying' the page and buffer cache and SMP-threaded the
54 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
55 *
56 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
57 */
58
59/*
60 * Lock ordering:
61 *
62 * ->i_mmap_lock (vmtruncate)
63 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070064 * ->swap_lock (exclusive_swap_page, others)
65 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080067 * ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * ->i_mmap_lock (truncate->unmap_mapping_range)
69 *
70 * ->mmap_sem
71 * ->i_mmap_lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070072 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
74 *
75 * ->mmap_sem
76 * ->lock_page (access_process_vm)
77 *
78 * ->mmap_sem
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080079 * ->i_mutex (msync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080081 * ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * ->i_alloc_sem (various)
83 *
84 * ->inode_lock
85 * ->sb_lock (fs/fs-writeback.c)
86 * ->mapping->tree_lock (__sync_single_inode)
87 *
88 * ->i_mmap_lock
89 * ->anon_vma.lock (vma_adjust)
90 *
91 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070092 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070094 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070095 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * ->private_lock (try_to_unmap_one)
97 * ->tree_lock (try_to_unmap_one)
98 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -080099 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ->private_lock (page_remove_rmap->set_page_dirty)
101 * ->tree_lock (page_remove_rmap->set_page_dirty)
102 * ->inode_lock (page_remove_rmap->set_page_dirty)
103 * ->inode_lock (zap_pte_range->set_page_dirty)
104 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
105 *
106 * ->task->proc_lock
107 * ->dcache_lock (proc_pid_lookup)
108 */
109
110/*
111 * Remove a page from the page cache and free it. Caller has to make
112 * sure the page is locked and that nobody else uses it - or that usage
113 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
114 */
115void __remove_from_page_cache(struct page *page)
116{
117 struct address_space *mapping = page->mapping;
118
119 radix_tree_delete(&mapping->page_tree, page->index);
120 page->mapping = NULL;
121 mapping->nrpages--;
Christoph Lameter347ce432006-06-30 01:55:35 -0700122 __dec_zone_page_state(page, NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125void remove_from_page_cache(struct page *page)
126{
127 struct address_space *mapping = page->mapping;
128
Matt Mackallcd7619d2005-05-01 08:59:01 -0700129 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 write_lock_irq(&mapping->tree_lock);
132 __remove_from_page_cache(page);
133 write_unlock_irq(&mapping->tree_lock);
134}
135
136static int sync_page(void *word)
137{
138 struct address_space *mapping;
139 struct page *page;
140
Andi Kleen07808b72005-11-05 17:25:53 +0100141 page = container_of((unsigned long *)word, struct page, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /*
William Lee Irwin IIIdd1d5af2005-05-01 08:58:38 -0700144 * page_mapping() is being called without PG_locked held.
145 * Some knowledge of the state and use of the page is used to
146 * reduce the requirements down to a memory barrier.
147 * The danger here is of a stale page_mapping() return value
148 * indicating a struct address_space different from the one it's
149 * associated with when it is associated with one.
150 * After smp_mb(), it's either the correct page_mapping() for
151 * the page, or an old page_mapping() and the page's own
152 * page_mapping() has gone NULL.
153 * The ->sync_page() address_space operation must tolerate
154 * page_mapping() going NULL. By an amazing coincidence,
155 * this comes about because none of the users of the page
156 * in the ->sync_page() methods make essential use of the
157 * page_mapping(), merely passing the page down to the backing
158 * device's unplug functions when it's non-NULL, which in turn
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700159 * ignore it for all cases but swap, where only page_private(page) is
William Lee Irwin IIIdd1d5af2005-05-01 08:58:38 -0700160 * of interest. When page_mapping() does go NULL, the entire
161 * call stack gracefully ignores the page and returns.
162 * -- wli
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
164 smp_mb();
165 mapping = page_mapping(page);
166 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
167 mapping->a_ops->sync_page(page);
168 io_schedule();
169 return 0;
170}
171
172/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700173 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700174 * @mapping: address space structure to write
175 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800176 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700177 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700179 * Start writeback against all of a mapping's dirty pages that lie
180 * within the byte offsets <start, end> inclusive.
181 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700183 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * these two operations is that if a dirty page/buffer is encountered, it must
185 * be waited upon, and not just skipped over.
186 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800187int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
188 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int ret;
191 struct writeback_control wbc = {
192 .sync_mode = sync_mode,
193 .nr_to_write = mapping->nrpages * 2,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700194 .range_start = start,
195 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 };
197
198 if (!mapping_cap_writeback_dirty(mapping))
199 return 0;
200
201 ret = do_writepages(mapping, &wbc);
202 return ret;
203}
204
205static inline int __filemap_fdatawrite(struct address_space *mapping,
206 int sync_mode)
207{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700208 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211int filemap_fdatawrite(struct address_space *mapping)
212{
213 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
214}
215EXPORT_SYMBOL(filemap_fdatawrite);
216
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800217static int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
218 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
221}
222
Randy Dunlap485bb992006-06-23 02:03:49 -0700223/**
224 * filemap_flush - mostly a non-blocking flush
225 * @mapping: target address_space
226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * This is a mostly non-blocking flush. Not suitable for data-integrity
228 * purposes - I/O may not be started against all dirty pages.
229 */
230int filemap_flush(struct address_space *mapping)
231{
232 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
233}
234EXPORT_SYMBOL(filemap_flush);
235
Randy Dunlap485bb992006-06-23 02:03:49 -0700236/**
237 * wait_on_page_writeback_range - wait for writeback to complete
238 * @mapping: target address_space
239 * @start: beginning page index
240 * @end: ending page index
241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * Wait for writeback to complete against pages indexed by start->end
243 * inclusive
244 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800245int wait_on_page_writeback_range(struct address_space *mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 pgoff_t start, pgoff_t end)
247{
248 struct pagevec pvec;
249 int nr_pages;
250 int ret = 0;
251 pgoff_t index;
252
253 if (end < start)
254 return 0;
255
256 pagevec_init(&pvec, 0);
257 index = start;
258 while ((index <= end) &&
259 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
260 PAGECACHE_TAG_WRITEBACK,
261 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
262 unsigned i;
263
264 for (i = 0; i < nr_pages; i++) {
265 struct page *page = pvec.pages[i];
266
267 /* until radix tree lookup accepts end_index */
268 if (page->index > end)
269 continue;
270
271 wait_on_page_writeback(page);
272 if (PageError(page))
273 ret = -EIO;
274 }
275 pagevec_release(&pvec);
276 cond_resched();
277 }
278
279 /* Check for outstanding write errors */
280 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
281 ret = -ENOSPC;
282 if (test_and_clear_bit(AS_EIO, &mapping->flags))
283 ret = -EIO;
284
285 return ret;
286}
287
Randy Dunlap485bb992006-06-23 02:03:49 -0700288/**
289 * sync_page_range - write and wait on all pages in the passed range
290 * @inode: target inode
291 * @mapping: target address_space
292 * @pos: beginning offset in pages to write
293 * @count: number of bytes to write
294 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * Write and wait upon all the pages in the passed range. This is a "data
296 * integrity" operation. It waits upon in-flight writeout before starting and
297 * waiting upon new writeout. If there was an IO error, return it.
298 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800299 * We need to re-take i_mutex during the generic_osync_inode list walk because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * it is otherwise livelockable.
301 */
302int sync_page_range(struct inode *inode, struct address_space *mapping,
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800303 loff_t pos, loff_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
306 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
307 int ret;
308
309 if (!mapping_cap_writeback_dirty(mapping) || !count)
310 return 0;
311 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
312 if (ret == 0) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800313 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800315 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 if (ret == 0)
318 ret = wait_on_page_writeback_range(mapping, start, end);
319 return ret;
320}
321EXPORT_SYMBOL(sync_page_range);
322
Randy Dunlap485bb992006-06-23 02:03:49 -0700323/**
324 * sync_page_range_nolock
325 * @inode: target inode
326 * @mapping: target address_space
327 * @pos: beginning offset in pages to write
328 * @count: number of bytes to write
329 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800330 * Note: Holding i_mutex across sync_page_range_nolock is not a good idea
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * as it forces O_SYNC writers to different parts of the same file
332 * to be serialised right until io completion.
333 */
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800334int sync_page_range_nolock(struct inode *inode, struct address_space *mapping,
335 loff_t pos, loff_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
338 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
339 int ret;
340
341 if (!mapping_cap_writeback_dirty(mapping) || !count)
342 return 0;
343 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
344 if (ret == 0)
345 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
346 if (ret == 0)
347 ret = wait_on_page_writeback_range(mapping, start, end);
348 return ret;
349}
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800350EXPORT_SYMBOL(sync_page_range_nolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700353 * filemap_fdatawait - wait for all under-writeback pages to complete
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * @mapping: address space structure to wait for
Randy Dunlap485bb992006-06-23 02:03:49 -0700355 *
356 * Walk the list of under-writeback pages of the given address space
357 * and wait for all of them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
359int filemap_fdatawait(struct address_space *mapping)
360{
361 loff_t i_size = i_size_read(mapping->host);
362
363 if (i_size == 0)
364 return 0;
365
366 return wait_on_page_writeback_range(mapping, 0,
367 (i_size - 1) >> PAGE_CACHE_SHIFT);
368}
369EXPORT_SYMBOL(filemap_fdatawait);
370
371int filemap_write_and_wait(struct address_space *mapping)
372{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800373 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800376 err = filemap_fdatawrite(mapping);
377 /*
378 * Even if the above returned error, the pages may be
379 * written partially (e.g. -ENOSPC), so we wait for it.
380 * But the -EIO is special case, it may indicate the worst
381 * thing (e.g. bug) happened, so we avoid waiting for it.
382 */
383 if (err != -EIO) {
384 int err2 = filemap_fdatawait(mapping);
385 if (!err)
386 err = err2;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800391EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Randy Dunlap485bb992006-06-23 02:03:49 -0700393/**
394 * filemap_write_and_wait_range - write out & wait on a file range
395 * @mapping: the address_space for the pages
396 * @lstart: offset in bytes where the range starts
397 * @lend: offset in bytes where the range ends (inclusive)
398 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800399 * Write out and wait upon file offsets lstart->lend, inclusive.
400 *
401 * Note that `lend' is inclusive (describes the last byte to be written) so
402 * that this function can be used to write to the very end-of-file (end = -1).
403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404int filemap_write_and_wait_range(struct address_space *mapping,
405 loff_t lstart, loff_t lend)
406{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800407 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800410 err = __filemap_fdatawrite_range(mapping, lstart, lend,
411 WB_SYNC_ALL);
412 /* See comment of filemap_write_and_wait() */
413 if (err != -EIO) {
414 int err2 = wait_on_page_writeback_range(mapping,
415 lstart >> PAGE_CACHE_SHIFT,
416 lend >> PAGE_CACHE_SHIFT);
417 if (!err)
418 err = err2;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Randy Dunlap485bb992006-06-23 02:03:49 -0700424/**
425 * add_to_page_cache - add newly allocated pagecache pages
426 * @page: page to add
427 * @mapping: the page's address_space
428 * @offset: page index
429 * @gfp_mask: page allocation mode
430 *
431 * This function is used to add newly allocated pagecache pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * the page is new, so we can just run SetPageLocked() against it.
433 * The other page state flags were set by rmqueue().
434 *
435 * This function does not add the page to the LRU. The caller must do that.
436 */
437int add_to_page_cache(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400438 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
441
442 if (error == 0) {
443 write_lock_irq(&mapping->tree_lock);
444 error = radix_tree_insert(&mapping->page_tree, offset, page);
445 if (!error) {
446 page_cache_get(page);
447 SetPageLocked(page);
448 page->mapping = mapping;
449 page->index = offset;
450 mapping->nrpages++;
Christoph Lameter347ce432006-06-30 01:55:35 -0700451 __inc_zone_page_state(page, NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 write_unlock_irq(&mapping->tree_lock);
454 radix_tree_preload_end();
455 }
456 return error;
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458EXPORT_SYMBOL(add_to_page_cache);
459
460int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400461 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
464 if (ret == 0)
465 lru_cache_add(page);
466 return ret;
467}
468
Paul Jackson44110fe2006-03-24 03:16:04 -0800469#ifdef CONFIG_NUMA
470struct page *page_cache_alloc(struct address_space *x)
471{
472 if (cpuset_do_page_mem_spread()) {
473 int n = cpuset_mem_spread_node();
474 return alloc_pages_node(n, mapping_gfp_mask(x), 0);
475 }
476 return alloc_pages(mapping_gfp_mask(x), 0);
477}
478EXPORT_SYMBOL(page_cache_alloc);
479
480struct page *page_cache_alloc_cold(struct address_space *x)
481{
482 if (cpuset_do_page_mem_spread()) {
483 int n = cpuset_mem_spread_node();
484 return alloc_pages_node(n, mapping_gfp_mask(x)|__GFP_COLD, 0);
485 }
486 return alloc_pages(mapping_gfp_mask(x)|__GFP_COLD, 0);
487}
488EXPORT_SYMBOL(page_cache_alloc_cold);
489#endif
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * In order to wait for pages to become available there must be
493 * waitqueues associated with pages. By using a hash table of
494 * waitqueues where the bucket discipline is to maintain all
495 * waiters on the same queue and wake all when any of the pages
496 * become available, and for the woken contexts to check to be
497 * sure the appropriate page became available, this saves space
498 * at a cost of "thundering herd" phenomena during rare hash
499 * collisions.
500 */
501static wait_queue_head_t *page_waitqueue(struct page *page)
502{
503 const struct zone *zone = page_zone(page);
504
505 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
506}
507
508static inline void wake_up_page(struct page *page, int bit)
509{
510 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
511}
512
513void fastcall wait_on_page_bit(struct page *page, int bit_nr)
514{
515 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
516
517 if (test_bit(bit_nr, &page->flags))
518 __wait_on_bit(page_waitqueue(page), &wait, sync_page,
519 TASK_UNINTERRUPTIBLE);
520}
521EXPORT_SYMBOL(wait_on_page_bit);
522
523/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700524 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * @page: the page
526 *
527 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
528 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
529 * mechananism between PageLocked pages and PageWriteback pages is shared.
530 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
531 *
532 * The first mb is necessary to safely close the critical section opened by the
533 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
534 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
535 * parallel wait_on_page_locked()).
536 */
537void fastcall unlock_page(struct page *page)
538{
539 smp_mb__before_clear_bit();
540 if (!TestClearPageLocked(page))
541 BUG();
542 smp_mb__after_clear_bit();
543 wake_up_page(page, PG_locked);
544}
545EXPORT_SYMBOL(unlock_page);
546
Randy Dunlap485bb992006-06-23 02:03:49 -0700547/**
548 * end_page_writeback - end writeback against a page
549 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
551void end_page_writeback(struct page *page)
552{
553 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
554 if (!test_clear_page_writeback(page))
555 BUG();
556 }
557 smp_mb__after_clear_bit();
558 wake_up_page(page, PG_writeback);
559}
560EXPORT_SYMBOL(end_page_writeback);
561
Randy Dunlap485bb992006-06-23 02:03:49 -0700562/**
563 * __lock_page - get a lock on the page, assuming we need to sleep to get it
564 * @page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700566 * Ugly. Running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
568 * chances are that on the second loop, the block layer's plug list is empty,
569 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
570 */
571void fastcall __lock_page(struct page *page)
572{
573 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
574
575 __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
576 TASK_UNINTERRUPTIBLE);
577}
578EXPORT_SYMBOL(__lock_page);
579
Randy Dunlap485bb992006-06-23 02:03:49 -0700580/**
581 * find_get_page - find and get a page reference
582 * @mapping: the address_space to search
583 * @offset: the page index
584 *
585 * A rather lightweight function, finding and getting a reference to a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * hashed page atomically.
587 */
588struct page * find_get_page(struct address_space *mapping, unsigned long offset)
589{
590 struct page *page;
591
592 read_lock_irq(&mapping->tree_lock);
593 page = radix_tree_lookup(&mapping->page_tree, offset);
594 if (page)
595 page_cache_get(page);
596 read_unlock_irq(&mapping->tree_lock);
597 return page;
598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599EXPORT_SYMBOL(find_get_page);
600
Randy Dunlap485bb992006-06-23 02:03:49 -0700601/**
602 * find_trylock_page - find and lock a page
603 * @mapping: the address_space to search
604 * @offset: the page index
605 *
606 * Same as find_get_page(), but trylock it instead of incrementing the count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
609{
610 struct page *page;
611
612 read_lock_irq(&mapping->tree_lock);
613 page = radix_tree_lookup(&mapping->page_tree, offset);
614 if (page && TestSetPageLocked(page))
615 page = NULL;
616 read_unlock_irq(&mapping->tree_lock);
617 return page;
618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619EXPORT_SYMBOL(find_trylock_page);
620
621/**
622 * find_lock_page - locate, pin and lock a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -0700623 * @mapping: the address_space to search
624 * @offset: the page index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
626 * Locates the desired pagecache page, locks it, increments its reference
627 * count and returns its address.
628 *
629 * Returns zero if the page was not present. find_lock_page() may sleep.
630 */
631struct page *find_lock_page(struct address_space *mapping,
632 unsigned long offset)
633{
634 struct page *page;
635
636 read_lock_irq(&mapping->tree_lock);
637repeat:
638 page = radix_tree_lookup(&mapping->page_tree, offset);
639 if (page) {
640 page_cache_get(page);
641 if (TestSetPageLocked(page)) {
642 read_unlock_irq(&mapping->tree_lock);
Nikita Danilovbbfbb7c2006-01-06 00:11:08 -0800643 __lock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 read_lock_irq(&mapping->tree_lock);
645
646 /* Has the page been truncated while we slept? */
Nikita Danilovbbfbb7c2006-01-06 00:11:08 -0800647 if (unlikely(page->mapping != mapping ||
648 page->index != offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 unlock_page(page);
650 page_cache_release(page);
651 goto repeat;
652 }
653 }
654 }
655 read_unlock_irq(&mapping->tree_lock);
656 return page;
657}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658EXPORT_SYMBOL(find_lock_page);
659
660/**
661 * find_or_create_page - locate or add a pagecache page
Martin Waitz67be2dd2005-05-01 08:59:26 -0700662 * @mapping: the page's address_space
663 * @index: the page's index into the mapping
664 * @gfp_mask: page allocation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 *
666 * Locates a page in the pagecache. If the page is not present, a new page
667 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
668 * LRU list. The returned page is locked and has its reference count
669 * incremented.
670 *
671 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
672 * allocation!
673 *
674 * find_or_create_page() returns the desired page's address, or zero on
675 * memory exhaustion.
676 */
677struct page *find_or_create_page(struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400678 unsigned long index, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct page *page, *cached_page = NULL;
681 int err;
682repeat:
683 page = find_lock_page(mapping, index);
684 if (!page) {
685 if (!cached_page) {
686 cached_page = alloc_page(gfp_mask);
687 if (!cached_page)
688 return NULL;
689 }
690 err = add_to_page_cache_lru(cached_page, mapping,
691 index, gfp_mask);
692 if (!err) {
693 page = cached_page;
694 cached_page = NULL;
695 } else if (err == -EEXIST)
696 goto repeat;
697 }
698 if (cached_page)
699 page_cache_release(cached_page);
700 return page;
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702EXPORT_SYMBOL(find_or_create_page);
703
704/**
705 * find_get_pages - gang pagecache lookup
706 * @mapping: The address_space to search
707 * @start: The starting page index
708 * @nr_pages: The maximum number of pages
709 * @pages: Where the resulting pages are placed
710 *
711 * find_get_pages() will search for and return a group of up to
712 * @nr_pages pages in the mapping. The pages are placed at @pages.
713 * find_get_pages() takes a reference against the returned pages.
714 *
715 * The search returns a group of mapping-contiguous pages with ascending
716 * indexes. There may be holes in the indices due to not-present pages.
717 *
718 * find_get_pages() returns the number of pages which were found.
719 */
720unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
721 unsigned int nr_pages, struct page **pages)
722{
723 unsigned int i;
724 unsigned int ret;
725
726 read_lock_irq(&mapping->tree_lock);
727 ret = radix_tree_gang_lookup(&mapping->page_tree,
728 (void **)pages, start, nr_pages);
729 for (i = 0; i < ret; i++)
730 page_cache_get(pages[i]);
731 read_unlock_irq(&mapping->tree_lock);
732 return ret;
733}
734
Jens Axboeebf43502006-04-27 08:46:01 +0200735/**
736 * find_get_pages_contig - gang contiguous pagecache lookup
737 * @mapping: The address_space to search
738 * @index: The starting page index
739 * @nr_pages: The maximum number of pages
740 * @pages: Where the resulting pages are placed
741 *
742 * find_get_pages_contig() works exactly like find_get_pages(), except
743 * that the returned number of pages are guaranteed to be contiguous.
744 *
745 * find_get_pages_contig() returns the number of pages which were found.
746 */
747unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
748 unsigned int nr_pages, struct page **pages)
749{
750 unsigned int i;
751 unsigned int ret;
752
753 read_lock_irq(&mapping->tree_lock);
754 ret = radix_tree_gang_lookup(&mapping->page_tree,
755 (void **)pages, index, nr_pages);
756 for (i = 0; i < ret; i++) {
757 if (pages[i]->mapping == NULL || pages[i]->index != index)
758 break;
759
760 page_cache_get(pages[i]);
761 index++;
762 }
763 read_unlock_irq(&mapping->tree_lock);
764 return i;
765}
766
Randy Dunlap485bb992006-06-23 02:03:49 -0700767/**
768 * find_get_pages_tag - find and return pages that match @tag
769 * @mapping: the address_space to search
770 * @index: the starting page index
771 * @tag: the tag index
772 * @nr_pages: the maximum number of pages
773 * @pages: where the resulting pages are placed
774 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -0700776 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
779 int tag, unsigned int nr_pages, struct page **pages)
780{
781 unsigned int i;
782 unsigned int ret;
783
784 read_lock_irq(&mapping->tree_lock);
785 ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
786 (void **)pages, *index, nr_pages, tag);
787 for (i = 0; i < ret; i++)
788 page_cache_get(pages[i]);
789 if (ret)
790 *index = pages[ret - 1]->index + 1;
791 read_unlock_irq(&mapping->tree_lock);
792 return ret;
793}
794
Randy Dunlap485bb992006-06-23 02:03:49 -0700795/**
796 * grab_cache_page_nowait - returns locked page at given index in given cache
797 * @mapping: target address_space
798 * @index: the page index
799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * Same as grab_cache_page, but do not wait if the page is unavailable.
801 * This is intended for speculative data generators, where the data can
802 * be regenerated if the page couldn't be grabbed. This routine should
803 * be safe to call while holding the lock for another page.
804 *
805 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
806 * and deadlock against the caller's locked page.
807 */
808struct page *
809grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
810{
811 struct page *page = find_get_page(mapping, index);
Al Viro6daa0e22005-10-21 03:18:50 -0400812 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (page) {
815 if (!TestSetPageLocked(page))
816 return page;
817 page_cache_release(page);
818 return NULL;
819 }
820 gfp_mask = mapping_gfp_mask(mapping) & ~__GFP_FS;
821 page = alloc_pages(gfp_mask, 0);
822 if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
823 page_cache_release(page);
824 page = NULL;
825 }
826 return page;
827}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828EXPORT_SYMBOL(grab_cache_page_nowait);
829
830/*
Wu Fengguang76d42bd2006-06-25 05:48:43 -0700831 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
832 * a _large_ part of the i/o request. Imagine the worst scenario:
833 *
834 * ---R__________________________________________B__________
835 * ^ reading here ^ bad block(assume 4k)
836 *
837 * read(R) => miss => readahead(R...B) => media error => frustrating retries
838 * => failing the whole request => read(R) => read(R+1) =>
839 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
840 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
841 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
842 *
843 * It is going insane. Fix it by quickly scaling down the readahead size.
844 */
845static void shrink_readahead_size_eio(struct file *filp,
846 struct file_ra_state *ra)
847{
848 if (!ra->ra_pages)
849 return;
850
851 ra->ra_pages /= 4;
852 printk(KERN_WARNING "Reducing readahead size to %luK\n",
853 ra->ra_pages << (PAGE_CACHE_SHIFT - 10));
854}
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
859 * @_ra: file's readahead state
860 * @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,
875 struct file_ra_state *_ra,
876 struct file *filp,
877 loff_t *ppos,
878 read_descriptor_t *desc,
879 read_actor_t actor)
880{
881 struct inode *inode = mapping->host;
882 unsigned long index;
883 unsigned long end_index;
884 unsigned long offset;
885 unsigned long last_index;
886 unsigned long next_index;
887 unsigned long prev_index;
888 loff_t isize;
889 struct page *cached_page;
890 int error;
891 struct file_ra_state ra = *_ra;
892
893 cached_page = NULL;
894 index = *ppos >> PAGE_CACHE_SHIFT;
895 next_index = index;
896 prev_index = ra.prev_page;
897 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
898 offset = *ppos & ~PAGE_CACHE_MASK;
899
900 isize = i_size_read(inode);
901 if (!isize)
902 goto out;
903
904 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
905 for (;;) {
906 struct page *page;
907 unsigned long nr, ret;
908
909 /* nr is the maximum number of bytes to copy from this page */
910 nr = PAGE_CACHE_SIZE;
911 if (index >= end_index) {
912 if (index > end_index)
913 goto out;
914 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
915 if (nr <= offset) {
916 goto out;
917 }
918 }
919 nr = nr - offset;
920
921 cond_resched();
922 if (index == next_index)
923 next_index = page_cache_readahead(mapping, &ra, filp,
924 index, last_index - index);
925
926find_page:
927 page = find_get_page(mapping, index);
928 if (unlikely(page == NULL)) {
929 handle_ra_miss(mapping, &ra, index);
930 goto no_cached_page;
931 }
932 if (!PageUptodate(page))
933 goto page_not_up_to_date;
934page_ok:
935
936 /* If users can be writing to this page using arbitrary
937 * virtual addresses, take care about potential aliasing
938 * before reading the page on the kernel side.
939 */
940 if (mapping_writably_mapped(mapping))
941 flush_dcache_page(page);
942
943 /*
944 * When (part of) the same page is read multiple times
945 * in succession, only mark it as accessed the first time.
946 */
947 if (prev_index != index)
948 mark_page_accessed(page);
949 prev_index = index;
950
951 /*
952 * Ok, we have the page, and it's up-to-date, so
953 * now we can copy it to user space...
954 *
955 * The actor routine returns how many bytes were actually used..
956 * NOTE! This may not be the same as how much of a user buffer
957 * we filled up (we may be padding etc), so we can only update
958 * "pos" here (the actor routine has to update the user buffer
959 * pointers and the remaining count).
960 */
961 ret = actor(desc, page, offset, nr);
962 offset += ret;
963 index += offset >> PAGE_CACHE_SHIFT;
964 offset &= ~PAGE_CACHE_MASK;
965
966 page_cache_release(page);
967 if (ret == nr && desc->count)
968 continue;
969 goto out;
970
971page_not_up_to_date:
972 /* Get exclusive access to the page ... */
973 lock_page(page);
974
975 /* Did it get unhashed before we got the lock? */
976 if (!page->mapping) {
977 unlock_page(page);
978 page_cache_release(page);
979 continue;
980 }
981
982 /* Did somebody else fill it already? */
983 if (PageUptodate(page)) {
984 unlock_page(page);
985 goto page_ok;
986 }
987
988readpage:
989 /* Start the actual read. The read will unlock the page. */
990 error = mapping->a_ops->readpage(filp, page);
991
Zach Brown994fc28c2005-12-15 14:28:17 -0800992 if (unlikely(error)) {
993 if (error == AOP_TRUNCATED_PAGE) {
994 page_cache_release(page);
995 goto find_page;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -0800998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (!PageUptodate(page)) {
1001 lock_page(page);
1002 if (!PageUptodate(page)) {
1003 if (page->mapping == NULL) {
1004 /*
1005 * invalidate_inode_pages got it
1006 */
1007 unlock_page(page);
1008 page_cache_release(page);
1009 goto find_page;
1010 }
1011 unlock_page(page);
1012 error = -EIO;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001013 shrink_readahead_size_eio(filp, &ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto readpage_error;
1015 }
1016 unlock_page(page);
1017 }
1018
1019 /*
1020 * i_size must be checked after we have done ->readpage.
1021 *
1022 * Checking i_size after the readpage allows us to calculate
1023 * the correct value for "nr", which means the zero-filled
1024 * part of the page is not copied back to userspace (unless
1025 * another truncate extends the file - this is desired though).
1026 */
1027 isize = i_size_read(inode);
1028 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1029 if (unlikely(!isize || index > end_index)) {
1030 page_cache_release(page);
1031 goto out;
1032 }
1033
1034 /* nr is the maximum number of bytes to copy from this page */
1035 nr = PAGE_CACHE_SIZE;
1036 if (index == end_index) {
1037 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1038 if (nr <= offset) {
1039 page_cache_release(page);
1040 goto out;
1041 }
1042 }
1043 nr = nr - offset;
1044 goto page_ok;
1045
1046readpage_error:
1047 /* UHHUH! A synchronous read error occurred. Report it */
1048 desc->error = error;
1049 page_cache_release(page);
1050 goto out;
1051
1052no_cached_page:
1053 /*
1054 * Ok, it wasn't cached, so we need to create a new
1055 * page..
1056 */
1057 if (!cached_page) {
1058 cached_page = page_cache_alloc_cold(mapping);
1059 if (!cached_page) {
1060 desc->error = -ENOMEM;
1061 goto out;
1062 }
1063 }
1064 error = add_to_page_cache_lru(cached_page, mapping,
1065 index, GFP_KERNEL);
1066 if (error) {
1067 if (error == -EEXIST)
1068 goto find_page;
1069 desc->error = error;
1070 goto out;
1071 }
1072 page = cached_page;
1073 cached_page = NULL;
1074 goto readpage;
1075 }
1076
1077out:
1078 *_ra = ra;
1079
1080 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1081 if (cached_page)
1082 page_cache_release(cached_page);
1083 if (filp)
1084 file_accessed(filp);
1085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086EXPORT_SYMBOL(do_generic_mapping_read);
1087
1088int file_read_actor(read_descriptor_t *desc, struct page *page,
1089 unsigned long offset, unsigned long size)
1090{
1091 char *kaddr;
1092 unsigned long left, count = desc->count;
1093
1094 if (size > count)
1095 size = count;
1096
1097 /*
1098 * Faults on the destination of a read are common, so do it before
1099 * taking the kmap.
1100 */
1101 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1102 kaddr = kmap_atomic(page, KM_USER0);
1103 left = __copy_to_user_inatomic(desc->arg.buf,
1104 kaddr + offset, size);
1105 kunmap_atomic(kaddr, KM_USER0);
1106 if (left == 0)
1107 goto success;
1108 }
1109
1110 /* Do it the slow way */
1111 kaddr = kmap(page);
1112 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1113 kunmap(page);
1114
1115 if (left) {
1116 size -= left;
1117 desc->error = -EFAULT;
1118 }
1119success:
1120 desc->count = count - size;
1121 desc->written += size;
1122 desc->arg.buf += size;
1123 return size;
1124}
1125
Randy Dunlap485bb992006-06-23 02:03:49 -07001126/**
1127 * __generic_file_aio_read - generic filesystem read routine
1128 * @iocb: kernel I/O control block
1129 * @iov: io vector request
1130 * @nr_segs: number of segments in the iovec
1131 * @ppos: current file position
1132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * This is the "read()" routine for all filesystems
1134 * that can use the page cache directly.
1135 */
1136ssize_t
1137__generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1138 unsigned long nr_segs, loff_t *ppos)
1139{
1140 struct file *filp = iocb->ki_filp;
1141 ssize_t retval;
1142 unsigned long seg;
1143 size_t count;
1144
1145 count = 0;
1146 for (seg = 0; seg < nr_segs; seg++) {
1147 const struct iovec *iv = &iov[seg];
1148
1149 /*
1150 * If any segment has a negative length, or the cumulative
1151 * length ever wraps negative then return -EINVAL.
1152 */
1153 count += iv->iov_len;
1154 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1155 return -EINVAL;
1156 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1157 continue;
1158 if (seg == 0)
1159 return -EFAULT;
1160 nr_segs = seg;
1161 count -= iv->iov_len; /* This segment is no good */
1162 break;
1163 }
1164
1165 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1166 if (filp->f_flags & O_DIRECT) {
1167 loff_t pos = *ppos, size;
1168 struct address_space *mapping;
1169 struct inode *inode;
1170
1171 mapping = filp->f_mapping;
1172 inode = mapping->host;
1173 retval = 0;
1174 if (!count)
1175 goto out; /* skip atime */
1176 size = i_size_read(inode);
1177 if (pos < size) {
1178 retval = generic_file_direct_IO(READ, iocb,
1179 iov, pos, nr_segs);
Suparna Bhattacharyab5c44c22005-05-21 16:33:36 -07001180 if (retval > 0 && !is_sync_kiocb(iocb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 retval = -EIOCBQUEUED;
1182 if (retval > 0)
1183 *ppos = pos + retval;
1184 }
1185 file_accessed(filp);
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -04001186 if (retval != 0)
1187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
1190 retval = 0;
1191 if (count) {
1192 for (seg = 0; seg < nr_segs; seg++) {
1193 read_descriptor_t desc;
1194
1195 desc.written = 0;
1196 desc.arg.buf = iov[seg].iov_base;
1197 desc.count = iov[seg].iov_len;
1198 if (desc.count == 0)
1199 continue;
1200 desc.error = 0;
1201 do_generic_file_read(filp,ppos,&desc,file_read_actor);
1202 retval += desc.written;
Tejun Heo39e88ca2005-10-30 15:02:40 -08001203 if (desc.error) {
1204 retval = retval ?: desc.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
1206 }
1207 }
1208 }
1209out:
1210 return retval;
1211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212EXPORT_SYMBOL(__generic_file_aio_read);
1213
1214ssize_t
1215generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1216{
1217 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1218
1219 BUG_ON(iocb->ki_pos != pos);
1220 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222EXPORT_SYMBOL(generic_file_aio_read);
1223
1224ssize_t
1225generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1226{
1227 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1228 struct kiocb kiocb;
1229 ssize_t ret;
1230
1231 init_sync_kiocb(&kiocb, filp);
1232 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
1233 if (-EIOCBQUEUED == ret)
1234 ret = wait_on_sync_kiocb(&kiocb);
1235 return ret;
1236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237EXPORT_SYMBOL(generic_file_read);
1238
1239int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1240{
1241 ssize_t written;
1242 unsigned long count = desc->count;
1243 struct file *file = desc->arg.data;
1244
1245 if (size > count)
1246 size = count;
1247
1248 written = file->f_op->sendpage(file, page, offset,
1249 size, &file->f_pos, size<count);
1250 if (written < 0) {
1251 desc->error = written;
1252 written = 0;
1253 }
1254 desc->count = count - written;
1255 desc->written += written;
1256 return written;
1257}
1258
1259ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1260 size_t count, read_actor_t actor, void *target)
1261{
1262 read_descriptor_t desc;
1263
1264 if (!count)
1265 return 0;
1266
1267 desc.written = 0;
1268 desc.count = count;
1269 desc.arg.data = target;
1270 desc.error = 0;
1271
1272 do_generic_file_read(in_file, ppos, &desc, actor);
1273 if (desc.written)
1274 return desc.written;
1275 return desc.error;
1276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277EXPORT_SYMBOL(generic_file_sendfile);
1278
1279static ssize_t
1280do_readahead(struct address_space *mapping, struct file *filp,
1281 unsigned long index, unsigned long nr)
1282{
1283 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1284 return -EINVAL;
1285
1286 force_page_cache_readahead(mapping, filp, index,
1287 max_sane_readahead(nr));
1288 return 0;
1289}
1290
1291asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1292{
1293 ssize_t ret;
1294 struct file *file;
1295
1296 ret = -EBADF;
1297 file = fget(fd);
1298 if (file) {
1299 if (file->f_mode & FMODE_READ) {
1300 struct address_space *mapping = file->f_mapping;
1301 unsigned long start = offset >> PAGE_CACHE_SHIFT;
1302 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1303 unsigned long len = end - start + 1;
1304 ret = do_readahead(mapping, file, start, len);
1305 }
1306 fput(file);
1307 }
1308 return ret;
1309}
1310
1311#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07001312static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
1313/**
1314 * page_cache_read - adds requested page to the page cache if not already there
1315 * @file: file to read
1316 * @offset: page index
1317 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * This adds the requested page to the page cache if it isn't already there,
1319 * and schedules an I/O to read in its contents from disk.
1320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321static int fastcall page_cache_read(struct file * file, unsigned long offset)
1322{
1323 struct address_space *mapping = file->f_mapping;
1324 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001325 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Zach Brown994fc28c2005-12-15 14:28:17 -08001327 do {
1328 page = page_cache_alloc_cold(mapping);
1329 if (!page)
1330 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Zach Brown994fc28c2005-12-15 14:28:17 -08001332 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1333 if (ret == 0)
1334 ret = mapping->a_ops->readpage(file, page);
1335 else if (ret == -EEXIST)
1336 ret = 0; /* losing race to add is OK */
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Zach Brown994fc28c2005-12-15 14:28:17 -08001340 } while (ret == AOP_TRUNCATED_PAGE);
1341
1342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345#define MMAP_LOTSAMISS (100)
1346
Randy Dunlap485bb992006-06-23 02:03:49 -07001347/**
1348 * filemap_nopage - read in file data for page fault handling
1349 * @area: the applicable vm_area
1350 * @address: target address to read in
1351 * @type: returned with VM_FAULT_{MINOR,MAJOR} if not %NULL
1352 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * filemap_nopage() is invoked via the vma operations vector for a
1354 * mapped memory region to read in file data during a page fault.
1355 *
1356 * The goto's are kind of ugly, but this streamlines the normal case of having
1357 * it in the page cache, and handles the special cases reasonably without
1358 * having a lot of duplicated code.
1359 */
1360struct page *filemap_nopage(struct vm_area_struct *area,
1361 unsigned long address, int *type)
1362{
1363 int error;
1364 struct file *file = area->vm_file;
1365 struct address_space *mapping = file->f_mapping;
1366 struct file_ra_state *ra = &file->f_ra;
1367 struct inode *inode = mapping->host;
1368 struct page *page;
1369 unsigned long size, pgoff;
1370 int did_readaround = 0, majmin = VM_FAULT_MINOR;
1371
1372 pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1373
1374retry_all:
1375 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1376 if (pgoff >= size)
1377 goto outside_data_content;
1378
1379 /* If we don't want any read-ahead, don't bother */
1380 if (VM_RandomReadHint(area))
1381 goto no_cached_page;
1382
1383 /*
1384 * The readahead code wants to be told about each and every page
1385 * so it can build and shrink its windows appropriately
1386 *
1387 * For sequential accesses, we use the generic readahead logic.
1388 */
1389 if (VM_SequentialReadHint(area))
1390 page_cache_readahead(mapping, ra, file, pgoff, 1);
1391
1392 /*
1393 * Do we have something in the page cache already?
1394 */
1395retry_find:
1396 page = find_get_page(mapping, pgoff);
1397 if (!page) {
1398 unsigned long ra_pages;
1399
1400 if (VM_SequentialReadHint(area)) {
1401 handle_ra_miss(mapping, ra, pgoff);
1402 goto no_cached_page;
1403 }
1404 ra->mmap_miss++;
1405
1406 /*
1407 * Do we miss much more than hit in this file? If so,
1408 * stop bothering with read-ahead. It will only hurt.
1409 */
1410 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1411 goto no_cached_page;
1412
1413 /*
1414 * To keep the pgmajfault counter straight, we need to
1415 * check did_readaround, as this is an inner loop.
1416 */
1417 if (!did_readaround) {
1418 majmin = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001419 count_vm_event(PGMAJFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 did_readaround = 1;
1422 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1423 if (ra_pages) {
1424 pgoff_t start = 0;
1425
1426 if (pgoff > ra_pages / 2)
1427 start = pgoff - ra_pages / 2;
1428 do_page_cache_readahead(mapping, file, start, ra_pages);
1429 }
1430 page = find_get_page(mapping, pgoff);
1431 if (!page)
1432 goto no_cached_page;
1433 }
1434
1435 if (!did_readaround)
1436 ra->mmap_hit++;
1437
1438 /*
1439 * Ok, found a page in the page cache, now we need to check
1440 * that it's up-to-date.
1441 */
1442 if (!PageUptodate(page))
1443 goto page_not_uptodate;
1444
1445success:
1446 /*
1447 * Found the page and have a reference on it.
1448 */
1449 mark_page_accessed(page);
1450 if (type)
1451 *type = majmin;
1452 return page;
1453
1454outside_data_content:
1455 /*
1456 * An external ptracer can access pages that normally aren't
1457 * accessible..
1458 */
1459 if (area->vm_mm == current->mm)
1460 return NULL;
1461 /* Fall through to the non-read-ahead case */
1462no_cached_page:
1463 /*
1464 * We're only likely to ever get here if MADV_RANDOM is in
1465 * effect.
1466 */
1467 error = page_cache_read(file, pgoff);
1468 grab_swap_token();
1469
1470 /*
1471 * The page we want has now been added to the page cache.
1472 * In the unlikely event that someone removed it in the
1473 * meantime, we'll just come back here and read it again.
1474 */
1475 if (error >= 0)
1476 goto retry_find;
1477
1478 /*
1479 * An error return from page_cache_read can result if the
1480 * system is low on memory, or a problem occurs while trying
1481 * to schedule I/O.
1482 */
1483 if (error == -ENOMEM)
1484 return NOPAGE_OOM;
1485 return NULL;
1486
1487page_not_uptodate:
1488 if (!did_readaround) {
1489 majmin = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001490 count_vm_event(PGMAJFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492 lock_page(page);
1493
1494 /* Did it get unhashed while we waited for it? */
1495 if (!page->mapping) {
1496 unlock_page(page);
1497 page_cache_release(page);
1498 goto retry_all;
1499 }
1500
1501 /* Did somebody else get it up-to-date? */
1502 if (PageUptodate(page)) {
1503 unlock_page(page);
1504 goto success;
1505 }
1506
Zach Brown994fc28c2005-12-15 14:28:17 -08001507 error = mapping->a_ops->readpage(file, page);
1508 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 wait_on_page_locked(page);
1510 if (PageUptodate(page))
1511 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001512 } else if (error == AOP_TRUNCATED_PAGE) {
1513 page_cache_release(page);
1514 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
1517 /*
1518 * Umm, take care of errors if the page isn't up-to-date.
1519 * Try to re-read it _once_. We do this synchronously,
1520 * because there really aren't any performance issues here
1521 * and we need to check for errors.
1522 */
1523 lock_page(page);
1524
1525 /* Somebody truncated the page on us? */
1526 if (!page->mapping) {
1527 unlock_page(page);
1528 page_cache_release(page);
1529 goto retry_all;
1530 }
1531
1532 /* Somebody else successfully read it in? */
1533 if (PageUptodate(page)) {
1534 unlock_page(page);
1535 goto success;
1536 }
1537 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08001538 error = mapping->a_ops->readpage(file, page);
1539 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 wait_on_page_locked(page);
1541 if (PageUptodate(page))
1542 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001543 } else if (error == AOP_TRUNCATED_PAGE) {
1544 page_cache_release(page);
1545 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547
1548 /*
1549 * Things didn't work out. Return zero to tell the
1550 * mm layer so, possibly freeing the page cache page first.
1551 */
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001552 shrink_readahead_size_eio(file, ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 page_cache_release(page);
1554 return NULL;
1555}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556EXPORT_SYMBOL(filemap_nopage);
1557
1558static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1559 int nonblock)
1560{
1561 struct address_space *mapping = file->f_mapping;
1562 struct page *page;
1563 int error;
1564
1565 /*
1566 * Do we have something in the page cache already?
1567 */
1568retry_find:
1569 page = find_get_page(mapping, pgoff);
1570 if (!page) {
1571 if (nonblock)
1572 return NULL;
1573 goto no_cached_page;
1574 }
1575
1576 /*
1577 * Ok, found a page in the page cache, now we need to check
1578 * that it's up-to-date.
1579 */
Jeff Moyerd3457342005-04-16 15:24:05 -07001580 if (!PageUptodate(page)) {
1581 if (nonblock) {
1582 page_cache_release(page);
1583 return NULL;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto page_not_uptodate;
Jeff Moyerd3457342005-04-16 15:24:05 -07001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588success:
1589 /*
1590 * Found the page and have a reference on it.
1591 */
1592 mark_page_accessed(page);
1593 return page;
1594
1595no_cached_page:
1596 error = page_cache_read(file, pgoff);
1597
1598 /*
1599 * The page we want has now been added to the page cache.
1600 * In the unlikely event that someone removed it in the
1601 * meantime, we'll just come back here and read it again.
1602 */
1603 if (error >= 0)
1604 goto retry_find;
1605
1606 /*
1607 * An error return from page_cache_read can result if the
1608 * system is low on memory, or a problem occurs while trying
1609 * to schedule I/O.
1610 */
1611 return NULL;
1612
1613page_not_uptodate:
1614 lock_page(page);
1615
1616 /* Did it get unhashed while we waited for it? */
1617 if (!page->mapping) {
1618 unlock_page(page);
1619 goto err;
1620 }
1621
1622 /* Did somebody else get it up-to-date? */
1623 if (PageUptodate(page)) {
1624 unlock_page(page);
1625 goto success;
1626 }
1627
Zach Brown994fc28c2005-12-15 14:28:17 -08001628 error = mapping->a_ops->readpage(file, page);
1629 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 wait_on_page_locked(page);
1631 if (PageUptodate(page))
1632 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001633 } else if (error == AOP_TRUNCATED_PAGE) {
1634 page_cache_release(page);
1635 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
1638 /*
1639 * Umm, take care of errors if the page isn't up-to-date.
1640 * Try to re-read it _once_. We do this synchronously,
1641 * because there really aren't any performance issues here
1642 * and we need to check for errors.
1643 */
1644 lock_page(page);
1645
1646 /* Somebody truncated the page on us? */
1647 if (!page->mapping) {
1648 unlock_page(page);
1649 goto err;
1650 }
1651 /* Somebody else successfully read it in? */
1652 if (PageUptodate(page)) {
1653 unlock_page(page);
1654 goto success;
1655 }
1656
1657 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08001658 error = mapping->a_ops->readpage(file, page);
1659 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 wait_on_page_locked(page);
1661 if (PageUptodate(page))
1662 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001663 } else if (error == AOP_TRUNCATED_PAGE) {
1664 page_cache_release(page);
1665 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
1668 /*
1669 * Things didn't work out. Return zero to tell the
1670 * mm layer so, possibly freeing the page cache page first.
1671 */
1672err:
1673 page_cache_release(page);
1674
1675 return NULL;
1676}
1677
1678int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
1679 unsigned long len, pgprot_t prot, unsigned long pgoff,
1680 int nonblock)
1681{
1682 struct file *file = vma->vm_file;
1683 struct address_space *mapping = file->f_mapping;
1684 struct inode *inode = mapping->host;
1685 unsigned long size;
1686 struct mm_struct *mm = vma->vm_mm;
1687 struct page *page;
1688 int err;
1689
1690 if (!nonblock)
1691 force_page_cache_readahead(mapping, vma->vm_file,
1692 pgoff, len >> PAGE_CACHE_SHIFT);
1693
1694repeat:
1695 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1696 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1697 return -EINVAL;
1698
1699 page = filemap_getpage(file, pgoff, nonblock);
Paolo 'Blaisorblade' Giarrussod44ed4f2005-09-03 15:54:55 -07001700
1701 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1702 * done in shmem_populate calling shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (!page && !nonblock)
1704 return -ENOMEM;
Paolo 'Blaisorblade' Giarrussod44ed4f2005-09-03 15:54:55 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (page) {
1707 err = install_page(mm, vma, addr, page, prot);
1708 if (err) {
1709 page_cache_release(page);
1710 return err;
1711 }
Hugh Dickins65500d22005-10-29 18:15:59 -07001712 } else if (vma->vm_flags & VM_NONLINEAR) {
Paolo 'Blaisorblade' Giarrussod44ed4f2005-09-03 15:54:55 -07001713 /* No page was found just because we can't read it in now (being
1714 * here implies nonblock != 0), but the page may exist, so set
1715 * the PTE to fault it in later. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 err = install_file_pte(mm, vma, addr, pgoff, prot);
1717 if (err)
1718 return err;
1719 }
1720
1721 len -= PAGE_SIZE;
1722 addr += PAGE_SIZE;
1723 pgoff++;
1724 if (len)
1725 goto repeat;
1726
1727 return 0;
1728}
Nikita Danilovb1459462005-10-29 18:17:02 -07001729EXPORT_SYMBOL(filemap_populate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731struct vm_operations_struct generic_file_vm_ops = {
1732 .nopage = filemap_nopage,
1733 .populate = filemap_populate,
1734};
1735
1736/* This is used for a general mmap of a disk file */
1737
1738int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1739{
1740 struct address_space *mapping = file->f_mapping;
1741
1742 if (!mapping->a_ops->readpage)
1743 return -ENOEXEC;
1744 file_accessed(file);
1745 vma->vm_ops = &generic_file_vm_ops;
1746 return 0;
1747}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749/*
1750 * This is for filesystems which do not implement ->writepage.
1751 */
1752int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1753{
1754 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1755 return -EINVAL;
1756 return generic_file_mmap(file, vma);
1757}
1758#else
1759int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1760{
1761 return -ENOSYS;
1762}
1763int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1764{
1765 return -ENOSYS;
1766}
1767#endif /* CONFIG_MMU */
1768
1769EXPORT_SYMBOL(generic_file_mmap);
1770EXPORT_SYMBOL(generic_file_readonly_mmap);
1771
1772static inline struct page *__read_cache_page(struct address_space *mapping,
1773 unsigned long index,
1774 int (*filler)(void *,struct page*),
1775 void *data)
1776{
1777 struct page *page, *cached_page = NULL;
1778 int err;
1779repeat:
1780 page = find_get_page(mapping, index);
1781 if (!page) {
1782 if (!cached_page) {
1783 cached_page = page_cache_alloc_cold(mapping);
1784 if (!cached_page)
1785 return ERR_PTR(-ENOMEM);
1786 }
1787 err = add_to_page_cache_lru(cached_page, mapping,
1788 index, GFP_KERNEL);
1789 if (err == -EEXIST)
1790 goto repeat;
1791 if (err < 0) {
1792 /* Presumably ENOMEM for radix tree node */
1793 page_cache_release(cached_page);
1794 return ERR_PTR(err);
1795 }
1796 page = cached_page;
1797 cached_page = NULL;
1798 err = filler(data, page);
1799 if (err < 0) {
1800 page_cache_release(page);
1801 page = ERR_PTR(err);
1802 }
1803 }
1804 if (cached_page)
1805 page_cache_release(cached_page);
1806 return page;
1807}
1808
Randy Dunlap485bb992006-06-23 02:03:49 -07001809/**
1810 * read_cache_page - read into page cache, fill it if needed
1811 * @mapping: the page's address_space
1812 * @index: the page index
1813 * @filler: function to perform the read
1814 * @data: destination for read data
1815 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * Read into the page cache. If a page already exists,
1817 * and PageUptodate() is not set, try to fill the page.
1818 */
1819struct page *read_cache_page(struct address_space *mapping,
1820 unsigned long index,
1821 int (*filler)(void *,struct page*),
1822 void *data)
1823{
1824 struct page *page;
1825 int err;
1826
1827retry:
1828 page = __read_cache_page(mapping, index, filler, data);
1829 if (IS_ERR(page))
1830 goto out;
1831 mark_page_accessed(page);
1832 if (PageUptodate(page))
1833 goto out;
1834
1835 lock_page(page);
1836 if (!page->mapping) {
1837 unlock_page(page);
1838 page_cache_release(page);
1839 goto retry;
1840 }
1841 if (PageUptodate(page)) {
1842 unlock_page(page);
1843 goto out;
1844 }
1845 err = filler(data, page);
1846 if (err < 0) {
1847 page_cache_release(page);
1848 page = ERR_PTR(err);
1849 }
1850 out:
1851 return page;
1852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853EXPORT_SYMBOL(read_cache_page);
1854
1855/*
1856 * If the page was newly created, increment its refcount and add it to the
1857 * caller's lru-buffering pagevec. This function is specifically for
1858 * generic_file_write().
1859 */
1860static inline struct page *
1861__grab_cache_page(struct address_space *mapping, unsigned long index,
1862 struct page **cached_page, struct pagevec *lru_pvec)
1863{
1864 int err;
1865 struct page *page;
1866repeat:
1867 page = find_lock_page(mapping, index);
1868 if (!page) {
1869 if (!*cached_page) {
1870 *cached_page = page_cache_alloc(mapping);
1871 if (!*cached_page)
1872 return NULL;
1873 }
1874 err = add_to_page_cache(*cached_page, mapping,
1875 index, GFP_KERNEL);
1876 if (err == -EEXIST)
1877 goto repeat;
1878 if (err == 0) {
1879 page = *cached_page;
1880 page_cache_get(page);
1881 if (!pagevec_add(lru_pvec, page))
1882 __pagevec_lru_add(lru_pvec);
1883 *cached_page = NULL;
1884 }
1885 }
1886 return page;
1887}
1888
1889/*
1890 * The logic we want is
1891 *
1892 * if suid or (sgid and xgrp)
1893 * remove privs
1894 */
1895int remove_suid(struct dentry *dentry)
1896{
1897 mode_t mode = dentry->d_inode->i_mode;
1898 int kill = 0;
1899 int result = 0;
1900
1901 /* suid always must be killed */
1902 if (unlikely(mode & S_ISUID))
1903 kill = ATTR_KILL_SUID;
1904
1905 /*
1906 * sgid without any exec bits is just a mandatory locking mark; leave
1907 * it alone. If some exec bits are set, it's a real sgid; kill it.
1908 */
1909 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1910 kill |= ATTR_KILL_SGID;
1911
1912 if (unlikely(kill && !capable(CAP_FSETID))) {
1913 struct iattr newattrs;
1914
1915 newattrs.ia_valid = ATTR_FORCE | kill;
1916 result = notify_change(dentry, &newattrs);
1917 }
1918 return result;
1919}
1920EXPORT_SYMBOL(remove_suid);
1921
Carsten Otteceffc072005-06-23 22:05:25 -07001922size_t
NeilBrown01408c42006-06-25 05:47:58 -07001923__filemap_copy_from_user_iovec_inatomic(char *vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 const struct iovec *iov, size_t base, size_t bytes)
1925{
1926 size_t copied = 0, left = 0;
1927
1928 while (bytes) {
1929 char __user *buf = iov->iov_base + base;
1930 int copy = min(bytes, iov->iov_len - base);
1931
1932 base = 0;
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07001933 left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 copied += copy;
1935 bytes -= copy;
1936 vaddr += copy;
1937 iov++;
1938
NeilBrown01408c42006-06-25 05:47:58 -07001939 if (unlikely(left))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 return copied - left;
1943}
1944
1945/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 * Performs necessary checks before doing a write
1947 *
Randy Dunlap485bb992006-06-23 02:03:49 -07001948 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 * Returns appropriate error code that caller should return or
1950 * zero in case that write should be allowed.
1951 */
1952inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1953{
1954 struct inode *inode = file->f_mapping->host;
1955 unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1956
1957 if (unlikely(*pos < 0))
1958 return -EINVAL;
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (!isblk) {
1961 /* FIXME: this is for backwards compatibility with 2.4 */
1962 if (file->f_flags & O_APPEND)
1963 *pos = i_size_read(inode);
1964
1965 if (limit != RLIM_INFINITY) {
1966 if (*pos >= limit) {
1967 send_sig(SIGXFSZ, current, 0);
1968 return -EFBIG;
1969 }
1970 if (*count > limit - (typeof(limit))*pos) {
1971 *count = limit - (typeof(limit))*pos;
1972 }
1973 }
1974 }
1975
1976 /*
1977 * LFS rule
1978 */
1979 if (unlikely(*pos + *count > MAX_NON_LFS &&
1980 !(file->f_flags & O_LARGEFILE))) {
1981 if (*pos >= MAX_NON_LFS) {
1982 send_sig(SIGXFSZ, current, 0);
1983 return -EFBIG;
1984 }
1985 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1986 *count = MAX_NON_LFS - (unsigned long)*pos;
1987 }
1988 }
1989
1990 /*
1991 * Are we about to exceed the fs block limit ?
1992 *
1993 * If we have written data it becomes a short write. If we have
1994 * exceeded without writing data we send a signal and return EFBIG.
1995 * Linus frestrict idea will clean these up nicely..
1996 */
1997 if (likely(!isblk)) {
1998 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1999 if (*count || *pos > inode->i_sb->s_maxbytes) {
2000 send_sig(SIGXFSZ, current, 0);
2001 return -EFBIG;
2002 }
2003 /* zero-length writes at ->s_maxbytes are OK */
2004 }
2005
2006 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
2007 *count = inode->i_sb->s_maxbytes - *pos;
2008 } else {
2009 loff_t isize;
2010 if (bdev_read_only(I_BDEV(inode)))
2011 return -EPERM;
2012 isize = i_size_read(inode);
2013 if (*pos >= isize) {
2014 if (*count || *pos > isize)
2015 return -ENOSPC;
2016 }
2017
2018 if (*pos + *count > isize)
2019 *count = isize - *pos;
2020 }
2021 return 0;
2022}
2023EXPORT_SYMBOL(generic_write_checks);
2024
2025ssize_t
2026generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
2027 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
2028 size_t count, size_t ocount)
2029{
2030 struct file *file = iocb->ki_filp;
2031 struct address_space *mapping = file->f_mapping;
2032 struct inode *inode = mapping->host;
2033 ssize_t written;
2034
2035 if (count != ocount)
2036 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
2037
2038 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
2039 if (written > 0) {
2040 loff_t end = pos + written;
2041 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2042 i_size_write(inode, end);
2043 mark_inode_dirty(inode);
2044 }
2045 *ppos = end;
2046 }
2047
2048 /*
2049 * Sync the fs metadata but not the minor inode changes and
2050 * of course not the data as we did direct DMA for the IO.
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002051 * i_mutex is held, which protects generic_osync_inode() from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 * livelocking.
2053 */
Hifumi Hisashi1e8a81c2005-06-25 14:54:32 -07002054 if (written >= 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2055 int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
2056 if (err < 0)
2057 written = err;
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (written == count && !is_sync_kiocb(iocb))
2060 written = -EIOCBQUEUED;
2061 return written;
2062}
2063EXPORT_SYMBOL(generic_file_direct_write);
2064
2065ssize_t
2066generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2067 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2068 size_t count, ssize_t written)
2069{
2070 struct file *file = iocb->ki_filp;
2071 struct address_space * mapping = file->f_mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002072 const struct address_space_operations *a_ops = mapping->a_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct inode *inode = mapping->host;
2074 long status = 0;
2075 struct page *page;
2076 struct page *cached_page = NULL;
2077 size_t bytes;
2078 struct pagevec lru_pvec;
2079 const struct iovec *cur_iov = iov; /* current iovec */
2080 size_t iov_base = 0; /* offset in the current iovec */
2081 char __user *buf;
2082
2083 pagevec_init(&lru_pvec, 0);
2084
2085 /*
2086 * handle partial DIO write. Adjust cur_iov if needed.
2087 */
2088 if (likely(nr_segs == 1))
2089 buf = iov->iov_base + written;
2090 else {
2091 filemap_set_next_iovec(&cur_iov, &iov_base, written);
akpm@osdl.orgf021e922005-05-01 08:58:35 -07002092 buf = cur_iov->iov_base + iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
2095 do {
2096 unsigned long index;
2097 unsigned long offset;
2098 size_t copied;
2099
2100 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
2101 index = pos >> PAGE_CACHE_SHIFT;
2102 bytes = PAGE_CACHE_SIZE - offset;
Vladimir V. Saveliev6527c2b2006-06-27 02:53:57 -07002103
2104 /* Limit the size of the copy to the caller's write size */
2105 bytes = min(bytes, count);
2106
2107 /*
2108 * Limit the size of the copy to that of the current segment,
2109 * because fault_in_pages_readable() doesn't know how to walk
2110 * segments.
2111 */
2112 bytes = min(bytes, cur_iov->iov_len - iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /*
2115 * Bring in the user page that we will copy from _first_.
2116 * Otherwise there's a nasty deadlock on copying from the
2117 * same page as we're writing to, without it being marked
2118 * up-to-date.
2119 */
Vladimir V. Saveliev6527c2b2006-06-27 02:53:57 -07002120 fault_in_pages_readable(buf, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
2123 if (!page) {
2124 status = -ENOMEM;
2125 break;
2126 }
2127
Andrew Morton81b0c872006-06-29 02:24:26 -07002128 if (unlikely(bytes == 0)) {
2129 status = 0;
2130 copied = 0;
2131 goto zero_length_segment;
2132 }
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 status = a_ops->prepare_write(file, page, offset, offset+bytes);
2135 if (unlikely(status)) {
2136 loff_t isize = i_size_read(inode);
Zach Brown994fc28c2005-12-15 14:28:17 -08002137
2138 if (status != AOP_TRUNCATED_PAGE)
2139 unlock_page(page);
2140 page_cache_release(page);
2141 if (status == AOP_TRUNCATED_PAGE)
2142 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
2144 * prepare_write() may have instantiated a few blocks
2145 * outside i_size. Trim these off again.
2146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (pos + bytes > isize)
2148 vmtruncate(inode, isize);
2149 break;
2150 }
2151 if (likely(nr_segs == 1))
2152 copied = filemap_copy_from_user(page, offset,
2153 buf, bytes);
2154 else
2155 copied = filemap_copy_from_user_iovec(page, offset,
2156 cur_iov, iov_base, bytes);
2157 flush_dcache_page(page);
2158 status = a_ops->commit_write(file, page, offset, offset+bytes);
Zach Brown994fc28c2005-12-15 14:28:17 -08002159 if (status == AOP_TRUNCATED_PAGE) {
2160 page_cache_release(page);
2161 continue;
2162 }
Andrew Morton81b0c872006-06-29 02:24:26 -07002163zero_length_segment:
2164 if (likely(copied >= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (!status)
2166 status = copied;
2167
2168 if (status >= 0) {
2169 written += status;
2170 count -= status;
2171 pos += status;
2172 buf += status;
akpm@osdl.orgf021e922005-05-01 08:58:35 -07002173 if (unlikely(nr_segs > 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 filemap_set_next_iovec(&cur_iov,
2175 &iov_base, status);
Badari Pulavartyb0cfbd92005-06-25 14:55:42 -07002176 if (count)
2177 buf = cur_iov->iov_base +
2178 iov_base;
Martin Schwidefskya5117182005-06-06 13:35:54 -07002179 } else {
2180 iov_base += status;
akpm@osdl.orgf021e922005-05-01 08:58:35 -07002181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 }
2184 if (unlikely(copied != bytes))
2185 if (status >= 0)
2186 status = -EFAULT;
2187 unlock_page(page);
2188 mark_page_accessed(page);
2189 page_cache_release(page);
2190 if (status < 0)
2191 break;
2192 balance_dirty_pages_ratelimited(mapping);
2193 cond_resched();
2194 } while (count);
2195 *ppos = pos;
2196
2197 if (cached_page)
2198 page_cache_release(cached_page);
2199
2200 /*
2201 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2202 */
2203 if (likely(status >= 0)) {
2204 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2205 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2206 status = generic_osync_inode(inode, mapping,
2207 OSYNC_METADATA|OSYNC_DATA);
2208 }
2209 }
2210
2211 /*
2212 * If we get here for O_DIRECT writes then we must have fallen through
2213 * to buffered writes (block instantiation inside i_size). So we sync
2214 * the file data here, to try to honour O_DIRECT expectations.
2215 */
2216 if (unlikely(file->f_flags & O_DIRECT) && written)
2217 status = filemap_write_and_wait(mapping);
2218
2219 pagevec_lru_add(&lru_pvec);
2220 return written ? written : status;
2221}
2222EXPORT_SYMBOL(generic_file_buffered_write);
2223
Adrian Bunk5ce78522005-09-10 00:26:28 -07002224static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225__generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2226 unsigned long nr_segs, loff_t *ppos)
2227{
2228 struct file *file = iocb->ki_filp;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002229 const struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 size_t ocount; /* original count */
2231 size_t count; /* after file limit checks */
2232 struct inode *inode = mapping->host;
2233 unsigned long seg;
2234 loff_t pos;
2235 ssize_t written;
2236 ssize_t err;
2237
2238 ocount = 0;
2239 for (seg = 0; seg < nr_segs; seg++) {
2240 const struct iovec *iv = &iov[seg];
2241
2242 /*
2243 * If any segment has a negative length, or the cumulative
2244 * length ever wraps negative then return -EINVAL.
2245 */
2246 ocount += iv->iov_len;
2247 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2248 return -EINVAL;
2249 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2250 continue;
2251 if (seg == 0)
2252 return -EFAULT;
2253 nr_segs = seg;
2254 ocount -= iv->iov_len; /* This segment is no good */
2255 break;
2256 }
2257
2258 count = ocount;
2259 pos = *ppos;
2260
2261 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2262
2263 /* We can write back this queue in page reclaim */
2264 current->backing_dev_info = mapping->backing_dev_info;
2265 written = 0;
2266
2267 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2268 if (err)
2269 goto out;
2270
2271 if (count == 0)
2272 goto out;
2273
2274 err = remove_suid(file->f_dentry);
2275 if (err)
2276 goto out;
2277
Christoph Hellwig870f4812006-01-09 20:52:01 -08002278 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2281 if (unlikely(file->f_flags & O_DIRECT)) {
2282 written = generic_file_direct_write(iocb, iov,
2283 &nr_segs, pos, ppos, count, ocount);
2284 if (written < 0 || written == count)
2285 goto out;
2286 /*
2287 * direct-io write to a hole: fall through to buffered I/O
2288 * for completing the rest of the request.
2289 */
2290 pos += written;
2291 count -= written;
2292 }
2293
2294 written = generic_file_buffered_write(iocb, iov, nr_segs,
2295 pos, ppos, count, written);
2296out:
2297 current->backing_dev_info = NULL;
2298 return written ? written : err;
2299}
2300EXPORT_SYMBOL(generic_file_aio_write_nolock);
2301
2302ssize_t
2303generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2304 unsigned long nr_segs, loff_t *ppos)
2305{
2306 struct file *file = iocb->ki_filp;
2307 struct address_space *mapping = file->f_mapping;
2308 struct inode *inode = mapping->host;
2309 ssize_t ret;
2310 loff_t pos = *ppos;
2311
2312 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
2313
2314 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2315 int err;
2316
2317 err = sync_page_range_nolock(inode, mapping, pos, ret);
2318 if (err < 0)
2319 ret = err;
2320 }
2321 return ret;
2322}
2323
Adrian Bunk5ce78522005-09-10 00:26:28 -07002324static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325__generic_file_write_nolock(struct file *file, const struct iovec *iov,
2326 unsigned long nr_segs, loff_t *ppos)
2327{
2328 struct kiocb kiocb;
2329 ssize_t ret;
2330
2331 init_sync_kiocb(&kiocb, file);
2332 ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2333 if (ret == -EIOCBQUEUED)
2334 ret = wait_on_sync_kiocb(&kiocb);
2335 return ret;
2336}
2337
2338ssize_t
2339generic_file_write_nolock(struct file *file, const struct iovec *iov,
2340 unsigned long nr_segs, loff_t *ppos)
2341{
2342 struct kiocb kiocb;
2343 ssize_t ret;
2344
2345 init_sync_kiocb(&kiocb, file);
2346 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2347 if (-EIOCBQUEUED == ret)
2348 ret = wait_on_sync_kiocb(&kiocb);
2349 return ret;
2350}
2351EXPORT_SYMBOL(generic_file_write_nolock);
2352
2353ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2354 size_t count, loff_t pos)
2355{
2356 struct file *file = iocb->ki_filp;
2357 struct address_space *mapping = file->f_mapping;
2358 struct inode *inode = mapping->host;
2359 ssize_t ret;
2360 struct iovec local_iov = { .iov_base = (void __user *)buf,
2361 .iov_len = count };
2362
2363 BUG_ON(iocb->ki_pos != pos);
2364
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002365 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1,
2367 &iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002368 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2371 ssize_t err;
2372
2373 err = sync_page_range(inode, mapping, pos, ret);
2374 if (err < 0)
2375 ret = err;
2376 }
2377 return ret;
2378}
2379EXPORT_SYMBOL(generic_file_aio_write);
2380
2381ssize_t generic_file_write(struct file *file, const char __user *buf,
2382 size_t count, loff_t *ppos)
2383{
2384 struct address_space *mapping = file->f_mapping;
2385 struct inode *inode = mapping->host;
2386 ssize_t ret;
2387 struct iovec local_iov = { .iov_base = (void __user *)buf,
2388 .iov_len = count };
2389
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002390 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002392 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2395 ssize_t err;
2396
2397 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2398 if (err < 0)
2399 ret = err;
2400 }
2401 return ret;
2402}
2403EXPORT_SYMBOL(generic_file_write);
2404
2405ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2406 unsigned long nr_segs, loff_t *ppos)
2407{
2408 struct kiocb kiocb;
2409 ssize_t ret;
2410
2411 init_sync_kiocb(&kiocb, filp);
2412 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
2413 if (-EIOCBQUEUED == ret)
2414 ret = wait_on_sync_kiocb(&kiocb);
2415 return ret;
2416}
2417EXPORT_SYMBOL(generic_file_readv);
2418
2419ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2420 unsigned long nr_segs, loff_t *ppos)
2421{
2422 struct address_space *mapping = file->f_mapping;
2423 struct inode *inode = mapping->host;
2424 ssize_t ret;
2425
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002426 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002428 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2431 int err;
2432
2433 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2434 if (err < 0)
2435 ret = err;
2436 }
2437 return ret;
2438}
2439EXPORT_SYMBOL(generic_file_writev);
2440
2441/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002442 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 * went wrong during pagecache shootdown.
2444 */
Adrian Bunk5ce78522005-09-10 00:26:28 -07002445static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2447 loff_t offset, unsigned long nr_segs)
2448{
2449 struct file *file = iocb->ki_filp;
2450 struct address_space *mapping = file->f_mapping;
2451 ssize_t retval;
2452 size_t write_len = 0;
2453
2454 /*
2455 * If it's a write, unmap all mmappings of the file up-front. This
2456 * will cause any pte dirty bits to be propagated into the pageframes
2457 * for the subsequent filemap_write_and_wait().
2458 */
2459 if (rw == WRITE) {
2460 write_len = iov_length(iov, nr_segs);
2461 if (mapping_mapped(mapping))
2462 unmap_mapping_range(mapping, offset, write_len, 0);
2463 }
2464
2465 retval = filemap_write_and_wait(mapping);
2466 if (retval == 0) {
2467 retval = mapping->a_ops->direct_IO(rw, iocb, iov,
2468 offset, nr_segs);
2469 if (rw == WRITE && mapping->nrpages) {
2470 pgoff_t end = (offset + write_len - 1)
2471 >> PAGE_CACHE_SHIFT;
2472 int err = invalidate_inode_pages2_range(mapping,
2473 offset >> PAGE_CACHE_SHIFT, end);
2474 if (err)
2475 retval = err;
2476 }
2477 }
2478 return retval;
2479}