blob: 7624c26fcea68cd53991298793c364cf707a3415 [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 */
12#include <linux/config.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/compiler.h>
16#include <linux/fs.h>
17#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>
Carsten Otteceffc072005-06-23 22:05:25 -070032#include "filemap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * FIXME: remove all knowledge of the buffer layer from the core VM
35 */
36#include <linux/buffer_head.h> /* for generic_osync_inode */
37
38#include <asm/uaccess.h>
39#include <asm/mman.h>
40
Adrian Bunk5ce78522005-09-10 00:26:28 -070041static ssize_t
42generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
43 loff_t offset, unsigned long nr_segs);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Shared mappings implemented 30.11.1994. It's not fully working yet,
47 * though.
48 *
49 * Shared mappings now work. 15.8.1995 Bruno.
50 *
51 * finished 'unifying' the page and buffer cache and SMP-threaded the
52 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
53 *
54 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
55 */
56
57/*
58 * Lock ordering:
59 *
60 * ->i_mmap_lock (vmtruncate)
61 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070062 * ->swap_lock (exclusive_swap_page, others)
63 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080065 * ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * ->i_mmap_lock (truncate->unmap_mapping_range)
67 *
68 * ->mmap_sem
69 * ->i_mmap_lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070070 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
72 *
73 * ->mmap_sem
74 * ->lock_page (access_process_vm)
75 *
76 * ->mmap_sem
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080077 * ->i_mutex (msync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080079 * ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * ->i_alloc_sem (various)
81 *
82 * ->inode_lock
83 * ->sb_lock (fs/fs-writeback.c)
84 * ->mapping->tree_lock (__sync_single_inode)
85 *
86 * ->i_mmap_lock
87 * ->anon_vma.lock (vma_adjust)
88 *
89 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070090 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070092 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070093 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ->private_lock (try_to_unmap_one)
95 * ->tree_lock (try_to_unmap_one)
96 * ->zone.lru_lock (follow_page->mark_page_accessed)
Nick Piggin053837f2006-01-18 17:42:27 -080097 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * ->private_lock (page_remove_rmap->set_page_dirty)
99 * ->tree_lock (page_remove_rmap->set_page_dirty)
100 * ->inode_lock (page_remove_rmap->set_page_dirty)
101 * ->inode_lock (zap_pte_range->set_page_dirty)
102 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
103 *
104 * ->task->proc_lock
105 * ->dcache_lock (proc_pid_lookup)
106 */
107
108/*
109 * Remove a page from the page cache and free it. Caller has to make
110 * sure the page is locked and that nobody else uses it - or that usage
111 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
112 */
113void __remove_from_page_cache(struct page *page)
114{
115 struct address_space *mapping = page->mapping;
116
117 radix_tree_delete(&mapping->page_tree, page->index);
118 page->mapping = NULL;
119 mapping->nrpages--;
120 pagecache_acct(-1);
121}
122
123void remove_from_page_cache(struct page *page)
124{
125 struct address_space *mapping = page->mapping;
126
Matt Mackallcd7619d2005-05-01 08:59:01 -0700127 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 write_lock_irq(&mapping->tree_lock);
130 __remove_from_page_cache(page);
131 write_unlock_irq(&mapping->tree_lock);
132}
133
134static int sync_page(void *word)
135{
136 struct address_space *mapping;
137 struct page *page;
138
Andi Kleen07808b72005-11-05 17:25:53 +0100139 page = container_of((unsigned long *)word, struct page, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /*
William Lee Irwin IIIdd1d5af2005-05-01 08:58:38 -0700142 * page_mapping() is being called without PG_locked held.
143 * Some knowledge of the state and use of the page is used to
144 * reduce the requirements down to a memory barrier.
145 * The danger here is of a stale page_mapping() return value
146 * indicating a struct address_space different from the one it's
147 * associated with when it is associated with one.
148 * After smp_mb(), it's either the correct page_mapping() for
149 * the page, or an old page_mapping() and the page's own
150 * page_mapping() has gone NULL.
151 * The ->sync_page() address_space operation must tolerate
152 * page_mapping() going NULL. By an amazing coincidence,
153 * this comes about because none of the users of the page
154 * in the ->sync_page() methods make essential use of the
155 * page_mapping(), merely passing the page down to the backing
156 * device's unplug functions when it's non-NULL, which in turn
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700157 * ignore it for all cases but swap, where only page_private(page) is
William Lee Irwin IIIdd1d5af2005-05-01 08:58:38 -0700158 * of interest. When page_mapping() does go NULL, the entire
159 * call stack gracefully ignores the page and returns.
160 * -- wli
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
162 smp_mb();
163 mapping = page_mapping(page);
164 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
165 mapping->a_ops->sync_page(page);
166 io_schedule();
167 return 0;
168}
169
170/**
171 * filemap_fdatawrite_range - start writeback against all of a mapping's
172 * dirty pages that lie within the byte offsets <start, end>
Martin Waitz67be2dd2005-05-01 08:59:26 -0700173 * @mapping: address space structure to write
174 * @start: offset in bytes where the range starts
175 * @end: offset in bytes where the range ends
176 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 *
178 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
179 * opposed to a regular memory * cleansing writeback. The difference between
180 * these two operations is that if a dirty page/buffer is encountered, it must
181 * be waited upon, and not just skipped over.
182 */
183static int __filemap_fdatawrite_range(struct address_space *mapping,
184 loff_t start, loff_t end, int sync_mode)
185{
186 int ret;
187 struct writeback_control wbc = {
188 .sync_mode = sync_mode,
189 .nr_to_write = mapping->nrpages * 2,
190 .start = start,
191 .end = end,
192 };
193
194 if (!mapping_cap_writeback_dirty(mapping))
195 return 0;
196
197 ret = do_writepages(mapping, &wbc);
198 return ret;
199}
200
201static inline int __filemap_fdatawrite(struct address_space *mapping,
202 int sync_mode)
203{
204 return __filemap_fdatawrite_range(mapping, 0, 0, sync_mode);
205}
206
207int filemap_fdatawrite(struct address_space *mapping)
208{
209 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
210}
211EXPORT_SYMBOL(filemap_fdatawrite);
212
213static int filemap_fdatawrite_range(struct address_space *mapping,
214 loff_t start, loff_t end)
215{
216 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
217}
218
219/*
220 * This is a mostly non-blocking flush. Not suitable for data-integrity
221 * purposes - I/O may not be started against all dirty pages.
222 */
223int filemap_flush(struct address_space *mapping)
224{
225 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
226}
227EXPORT_SYMBOL(filemap_flush);
228
229/*
230 * Wait for writeback to complete against pages indexed by start->end
231 * inclusive
232 */
233static int wait_on_page_writeback_range(struct address_space *mapping,
234 pgoff_t start, pgoff_t end)
235{
236 struct pagevec pvec;
237 int nr_pages;
238 int ret = 0;
239 pgoff_t index;
240
241 if (end < start)
242 return 0;
243
244 pagevec_init(&pvec, 0);
245 index = start;
246 while ((index <= end) &&
247 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
248 PAGECACHE_TAG_WRITEBACK,
249 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
250 unsigned i;
251
252 for (i = 0; i < nr_pages; i++) {
253 struct page *page = pvec.pages[i];
254
255 /* until radix tree lookup accepts end_index */
256 if (page->index > end)
257 continue;
258
259 wait_on_page_writeback(page);
260 if (PageError(page))
261 ret = -EIO;
262 }
263 pagevec_release(&pvec);
264 cond_resched();
265 }
266
267 /* Check for outstanding write errors */
268 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
269 ret = -ENOSPC;
270 if (test_and_clear_bit(AS_EIO, &mapping->flags))
271 ret = -EIO;
272
273 return ret;
274}
275
276/*
277 * Write and wait upon all the pages in the passed range. This is a "data
278 * integrity" operation. It waits upon in-flight writeout before starting and
279 * waiting upon new writeout. If there was an IO error, return it.
280 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800281 * We need to re-take i_mutex during the generic_osync_inode list walk because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * it is otherwise livelockable.
283 */
284int sync_page_range(struct inode *inode, struct address_space *mapping,
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800285 loff_t pos, loff_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
288 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
289 int ret;
290
291 if (!mapping_cap_writeback_dirty(mapping) || !count)
292 return 0;
293 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
294 if (ret == 0) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800295 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800297 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 if (ret == 0)
300 ret = wait_on_page_writeback_range(mapping, start, end);
301 return ret;
302}
303EXPORT_SYMBOL(sync_page_range);
304
305/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800306 * Note: Holding i_mutex across sync_page_range_nolock is not a good idea
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * as it forces O_SYNC writers to different parts of the same file
308 * to be serialised right until io completion.
309 */
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800310int sync_page_range_nolock(struct inode *inode, struct address_space *mapping,
311 loff_t pos, loff_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
314 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
315 int ret;
316
317 if (!mapping_cap_writeback_dirty(mapping) || !count)
318 return 0;
319 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
320 if (ret == 0)
321 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
322 if (ret == 0)
323 ret = wait_on_page_writeback_range(mapping, start, end);
324 return ret;
325}
OGAWA Hirofumi268fc162006-01-08 01:02:12 -0800326EXPORT_SYMBOL(sync_page_range_nolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328/**
329 * filemap_fdatawait - walk the list of under-writeback pages of the given
330 * address space and wait for all of them.
331 *
332 * @mapping: address space structure to wait for
333 */
334int filemap_fdatawait(struct address_space *mapping)
335{
336 loff_t i_size = i_size_read(mapping->host);
337
338 if (i_size == 0)
339 return 0;
340
341 return wait_on_page_writeback_range(mapping, 0,
342 (i_size - 1) >> PAGE_CACHE_SHIFT);
343}
344EXPORT_SYMBOL(filemap_fdatawait);
345
346int filemap_write_and_wait(struct address_space *mapping)
347{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800348 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800351 err = filemap_fdatawrite(mapping);
352 /*
353 * Even if the above returned error, the pages may be
354 * written partially (e.g. -ENOSPC), so we wait for it.
355 * But the -EIO is special case, it may indicate the worst
356 * thing (e.g. bug) happened, so we avoid waiting for it.
357 */
358 if (err != -EIO) {
359 int err2 = filemap_fdatawait(mapping);
360 if (!err)
361 err = err2;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800366EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368int filemap_write_and_wait_range(struct address_space *mapping,
369 loff_t lstart, loff_t lend)
370{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800371 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (mapping->nrpages) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800374 err = __filemap_fdatawrite_range(mapping, lstart, lend,
375 WB_SYNC_ALL);
376 /* See comment of filemap_write_and_wait() */
377 if (err != -EIO) {
378 int err2 = wait_on_page_writeback_range(mapping,
379 lstart >> PAGE_CACHE_SHIFT,
380 lend >> PAGE_CACHE_SHIFT);
381 if (!err)
382 err = err2;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388/*
389 * This function is used to add newly allocated pagecache pages:
390 * the page is new, so we can just run SetPageLocked() against it.
391 * The other page state flags were set by rmqueue().
392 *
393 * This function does not add the page to the LRU. The caller must do that.
394 */
395int add_to_page_cache(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400396 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
399
400 if (error == 0) {
401 write_lock_irq(&mapping->tree_lock);
402 error = radix_tree_insert(&mapping->page_tree, offset, page);
403 if (!error) {
404 page_cache_get(page);
405 SetPageLocked(page);
406 page->mapping = mapping;
407 page->index = offset;
408 mapping->nrpages++;
409 pagecache_acct(1);
410 }
411 write_unlock_irq(&mapping->tree_lock);
412 radix_tree_preload_end();
413 }
414 return error;
415}
416
417EXPORT_SYMBOL(add_to_page_cache);
418
419int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400420 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
423 if (ret == 0)
424 lru_cache_add(page);
425 return ret;
426}
427
428/*
429 * In order to wait for pages to become available there must be
430 * waitqueues associated with pages. By using a hash table of
431 * waitqueues where the bucket discipline is to maintain all
432 * waiters on the same queue and wake all when any of the pages
433 * become available, and for the woken contexts to check to be
434 * sure the appropriate page became available, this saves space
435 * at a cost of "thundering herd" phenomena during rare hash
436 * collisions.
437 */
438static wait_queue_head_t *page_waitqueue(struct page *page)
439{
440 const struct zone *zone = page_zone(page);
441
442 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
443}
444
445static inline void wake_up_page(struct page *page, int bit)
446{
447 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
448}
449
450void fastcall wait_on_page_bit(struct page *page, int bit_nr)
451{
452 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
453
454 if (test_bit(bit_nr, &page->flags))
455 __wait_on_bit(page_waitqueue(page), &wait, sync_page,
456 TASK_UNINTERRUPTIBLE);
457}
458EXPORT_SYMBOL(wait_on_page_bit);
459
460/**
461 * unlock_page() - unlock a locked page
462 *
463 * @page: the page
464 *
465 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
466 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
467 * mechananism between PageLocked pages and PageWriteback pages is shared.
468 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
469 *
470 * The first mb is necessary to safely close the critical section opened by the
471 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
472 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
473 * parallel wait_on_page_locked()).
474 */
475void fastcall unlock_page(struct page *page)
476{
477 smp_mb__before_clear_bit();
478 if (!TestClearPageLocked(page))
479 BUG();
480 smp_mb__after_clear_bit();
481 wake_up_page(page, PG_locked);
482}
483EXPORT_SYMBOL(unlock_page);
484
485/*
486 * End writeback against a page.
487 */
488void end_page_writeback(struct page *page)
489{
490 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
491 if (!test_clear_page_writeback(page))
492 BUG();
493 }
494 smp_mb__after_clear_bit();
495 wake_up_page(page, PG_writeback);
496}
497EXPORT_SYMBOL(end_page_writeback);
498
499/*
500 * Get a lock on the page, assuming we need to sleep to get it.
501 *
502 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
503 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
504 * chances are that on the second loop, the block layer's plug list is empty,
505 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
506 */
507void fastcall __lock_page(struct page *page)
508{
509 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
510
511 __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
512 TASK_UNINTERRUPTIBLE);
513}
514EXPORT_SYMBOL(__lock_page);
515
516/*
517 * a rather lightweight function, finding and getting a reference to a
518 * hashed page atomically.
519 */
520struct page * find_get_page(struct address_space *mapping, unsigned long offset)
521{
522 struct page *page;
523
524 read_lock_irq(&mapping->tree_lock);
525 page = radix_tree_lookup(&mapping->page_tree, offset);
526 if (page)
527 page_cache_get(page);
528 read_unlock_irq(&mapping->tree_lock);
529 return page;
530}
531
532EXPORT_SYMBOL(find_get_page);
533
534/*
535 * Same as above, but trylock it instead of incrementing the count.
536 */
537struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
538{
539 struct page *page;
540
541 read_lock_irq(&mapping->tree_lock);
542 page = radix_tree_lookup(&mapping->page_tree, offset);
543 if (page && TestSetPageLocked(page))
544 page = NULL;
545 read_unlock_irq(&mapping->tree_lock);
546 return page;
547}
548
549EXPORT_SYMBOL(find_trylock_page);
550
551/**
552 * find_lock_page - locate, pin and lock a pagecache page
553 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700554 * @mapping: the address_space to search
555 * @offset: the page index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
557 * Locates the desired pagecache page, locks it, increments its reference
558 * count and returns its address.
559 *
560 * Returns zero if the page was not present. find_lock_page() may sleep.
561 */
562struct page *find_lock_page(struct address_space *mapping,
563 unsigned long offset)
564{
565 struct page *page;
566
567 read_lock_irq(&mapping->tree_lock);
568repeat:
569 page = radix_tree_lookup(&mapping->page_tree, offset);
570 if (page) {
571 page_cache_get(page);
572 if (TestSetPageLocked(page)) {
573 read_unlock_irq(&mapping->tree_lock);
Nikita Danilovbbfbb7c2006-01-06 00:11:08 -0800574 __lock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 read_lock_irq(&mapping->tree_lock);
576
577 /* Has the page been truncated while we slept? */
Nikita Danilovbbfbb7c2006-01-06 00:11:08 -0800578 if (unlikely(page->mapping != mapping ||
579 page->index != offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unlock_page(page);
581 page_cache_release(page);
582 goto repeat;
583 }
584 }
585 }
586 read_unlock_irq(&mapping->tree_lock);
587 return page;
588}
589
590EXPORT_SYMBOL(find_lock_page);
591
592/**
593 * find_or_create_page - locate or add a pagecache page
594 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700595 * @mapping: the page's address_space
596 * @index: the page's index into the mapping
597 * @gfp_mask: page allocation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
599 * Locates a page in the pagecache. If the page is not present, a new page
600 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
601 * LRU list. The returned page is locked and has its reference count
602 * incremented.
603 *
604 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
605 * allocation!
606 *
607 * find_or_create_page() returns the desired page's address, or zero on
608 * memory exhaustion.
609 */
610struct page *find_or_create_page(struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400611 unsigned long index, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct page *page, *cached_page = NULL;
614 int err;
615repeat:
616 page = find_lock_page(mapping, index);
617 if (!page) {
618 if (!cached_page) {
619 cached_page = alloc_page(gfp_mask);
620 if (!cached_page)
621 return NULL;
622 }
623 err = add_to_page_cache_lru(cached_page, mapping,
624 index, gfp_mask);
625 if (!err) {
626 page = cached_page;
627 cached_page = NULL;
628 } else if (err == -EEXIST)
629 goto repeat;
630 }
631 if (cached_page)
632 page_cache_release(cached_page);
633 return page;
634}
635
636EXPORT_SYMBOL(find_or_create_page);
637
638/**
639 * find_get_pages - gang pagecache lookup
640 * @mapping: The address_space to search
641 * @start: The starting page index
642 * @nr_pages: The maximum number of pages
643 * @pages: Where the resulting pages are placed
644 *
645 * find_get_pages() will search for and return a group of up to
646 * @nr_pages pages in the mapping. The pages are placed at @pages.
647 * find_get_pages() takes a reference against the returned pages.
648 *
649 * The search returns a group of mapping-contiguous pages with ascending
650 * indexes. There may be holes in the indices due to not-present pages.
651 *
652 * find_get_pages() returns the number of pages which were found.
653 */
654unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
655 unsigned int nr_pages, struct page **pages)
656{
657 unsigned int i;
658 unsigned int ret;
659
660 read_lock_irq(&mapping->tree_lock);
661 ret = radix_tree_gang_lookup(&mapping->page_tree,
662 (void **)pages, start, nr_pages);
663 for (i = 0; i < ret; i++)
664 page_cache_get(pages[i]);
665 read_unlock_irq(&mapping->tree_lock);
666 return ret;
667}
668
669/*
670 * Like find_get_pages, except we only return pages which are tagged with
671 * `tag'. We update *index to index the next page for the traversal.
672 */
673unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
674 int tag, unsigned int nr_pages, struct page **pages)
675{
676 unsigned int i;
677 unsigned int ret;
678
679 read_lock_irq(&mapping->tree_lock);
680 ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
681 (void **)pages, *index, nr_pages, tag);
682 for (i = 0; i < ret; i++)
683 page_cache_get(pages[i]);
684 if (ret)
685 *index = pages[ret - 1]->index + 1;
686 read_unlock_irq(&mapping->tree_lock);
687 return ret;
688}
689
690/*
691 * Same as grab_cache_page, but do not wait if the page is unavailable.
692 * This is intended for speculative data generators, where the data can
693 * be regenerated if the page couldn't be grabbed. This routine should
694 * be safe to call while holding the lock for another page.
695 *
696 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
697 * and deadlock against the caller's locked page.
698 */
699struct page *
700grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
701{
702 struct page *page = find_get_page(mapping, index);
Al Viro6daa0e22005-10-21 03:18:50 -0400703 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (page) {
706 if (!TestSetPageLocked(page))
707 return page;
708 page_cache_release(page);
709 return NULL;
710 }
711 gfp_mask = mapping_gfp_mask(mapping) & ~__GFP_FS;
712 page = alloc_pages(gfp_mask, 0);
713 if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
714 page_cache_release(page);
715 page = NULL;
716 }
717 return page;
718}
719
720EXPORT_SYMBOL(grab_cache_page_nowait);
721
722/*
723 * This is a generic file read routine, and uses the
724 * mapping->a_ops->readpage() function for the actual low-level
725 * stuff.
726 *
727 * This is really ugly. But the goto's actually try to clarify some
728 * of the logic when it comes to error handling etc.
729 *
730 * Note the struct file* is only passed for the use of readpage. It may be
731 * NULL.
732 */
733void do_generic_mapping_read(struct address_space *mapping,
734 struct file_ra_state *_ra,
735 struct file *filp,
736 loff_t *ppos,
737 read_descriptor_t *desc,
738 read_actor_t actor)
739{
740 struct inode *inode = mapping->host;
741 unsigned long index;
742 unsigned long end_index;
743 unsigned long offset;
744 unsigned long last_index;
745 unsigned long next_index;
746 unsigned long prev_index;
747 loff_t isize;
748 struct page *cached_page;
749 int error;
750 struct file_ra_state ra = *_ra;
751
752 cached_page = NULL;
753 index = *ppos >> PAGE_CACHE_SHIFT;
754 next_index = index;
755 prev_index = ra.prev_page;
756 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
757 offset = *ppos & ~PAGE_CACHE_MASK;
758
759 isize = i_size_read(inode);
760 if (!isize)
761 goto out;
762
763 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
764 for (;;) {
765 struct page *page;
766 unsigned long nr, ret;
767
768 /* nr is the maximum number of bytes to copy from this page */
769 nr = PAGE_CACHE_SIZE;
770 if (index >= end_index) {
771 if (index > end_index)
772 goto out;
773 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
774 if (nr <= offset) {
775 goto out;
776 }
777 }
778 nr = nr - offset;
779
780 cond_resched();
781 if (index == next_index)
782 next_index = page_cache_readahead(mapping, &ra, filp,
783 index, last_index - index);
784
785find_page:
786 page = find_get_page(mapping, index);
787 if (unlikely(page == NULL)) {
788 handle_ra_miss(mapping, &ra, index);
789 goto no_cached_page;
790 }
791 if (!PageUptodate(page))
792 goto page_not_up_to_date;
793page_ok:
794
795 /* If users can be writing to this page using arbitrary
796 * virtual addresses, take care about potential aliasing
797 * before reading the page on the kernel side.
798 */
799 if (mapping_writably_mapped(mapping))
800 flush_dcache_page(page);
801
802 /*
803 * When (part of) the same page is read multiple times
804 * in succession, only mark it as accessed the first time.
805 */
806 if (prev_index != index)
807 mark_page_accessed(page);
808 prev_index = index;
809
810 /*
811 * Ok, we have the page, and it's up-to-date, so
812 * now we can copy it to user space...
813 *
814 * The actor routine returns how many bytes were actually used..
815 * NOTE! This may not be the same as how much of a user buffer
816 * we filled up (we may be padding etc), so we can only update
817 * "pos" here (the actor routine has to update the user buffer
818 * pointers and the remaining count).
819 */
820 ret = actor(desc, page, offset, nr);
821 offset += ret;
822 index += offset >> PAGE_CACHE_SHIFT;
823 offset &= ~PAGE_CACHE_MASK;
824
825 page_cache_release(page);
826 if (ret == nr && desc->count)
827 continue;
828 goto out;
829
830page_not_up_to_date:
831 /* Get exclusive access to the page ... */
832 lock_page(page);
833
834 /* Did it get unhashed before we got the lock? */
835 if (!page->mapping) {
836 unlock_page(page);
837 page_cache_release(page);
838 continue;
839 }
840
841 /* Did somebody else fill it already? */
842 if (PageUptodate(page)) {
843 unlock_page(page);
844 goto page_ok;
845 }
846
847readpage:
848 /* Start the actual read. The read will unlock the page. */
849 error = mapping->a_ops->readpage(filp, page);
850
Zach Brown994fc28c2005-12-15 14:28:17 -0800851 if (unlikely(error)) {
852 if (error == AOP_TRUNCATED_PAGE) {
853 page_cache_release(page);
854 goto find_page;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -0800857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 if (!PageUptodate(page)) {
860 lock_page(page);
861 if (!PageUptodate(page)) {
862 if (page->mapping == NULL) {
863 /*
864 * invalidate_inode_pages got it
865 */
866 unlock_page(page);
867 page_cache_release(page);
868 goto find_page;
869 }
870 unlock_page(page);
871 error = -EIO;
872 goto readpage_error;
873 }
874 unlock_page(page);
875 }
876
877 /*
878 * i_size must be checked after we have done ->readpage.
879 *
880 * Checking i_size after the readpage allows us to calculate
881 * the correct value for "nr", which means the zero-filled
882 * part of the page is not copied back to userspace (unless
883 * another truncate extends the file - this is desired though).
884 */
885 isize = i_size_read(inode);
886 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
887 if (unlikely(!isize || index > end_index)) {
888 page_cache_release(page);
889 goto out;
890 }
891
892 /* nr is the maximum number of bytes to copy from this page */
893 nr = PAGE_CACHE_SIZE;
894 if (index == end_index) {
895 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
896 if (nr <= offset) {
897 page_cache_release(page);
898 goto out;
899 }
900 }
901 nr = nr - offset;
902 goto page_ok;
903
904readpage_error:
905 /* UHHUH! A synchronous read error occurred. Report it */
906 desc->error = error;
907 page_cache_release(page);
908 goto out;
909
910no_cached_page:
911 /*
912 * Ok, it wasn't cached, so we need to create a new
913 * page..
914 */
915 if (!cached_page) {
916 cached_page = page_cache_alloc_cold(mapping);
917 if (!cached_page) {
918 desc->error = -ENOMEM;
919 goto out;
920 }
921 }
922 error = add_to_page_cache_lru(cached_page, mapping,
923 index, GFP_KERNEL);
924 if (error) {
925 if (error == -EEXIST)
926 goto find_page;
927 desc->error = error;
928 goto out;
929 }
930 page = cached_page;
931 cached_page = NULL;
932 goto readpage;
933 }
934
935out:
936 *_ra = ra;
937
938 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
939 if (cached_page)
940 page_cache_release(cached_page);
941 if (filp)
942 file_accessed(filp);
943}
944
945EXPORT_SYMBOL(do_generic_mapping_read);
946
947int file_read_actor(read_descriptor_t *desc, struct page *page,
948 unsigned long offset, unsigned long size)
949{
950 char *kaddr;
951 unsigned long left, count = desc->count;
952
953 if (size > count)
954 size = count;
955
956 /*
957 * Faults on the destination of a read are common, so do it before
958 * taking the kmap.
959 */
960 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
961 kaddr = kmap_atomic(page, KM_USER0);
962 left = __copy_to_user_inatomic(desc->arg.buf,
963 kaddr + offset, size);
964 kunmap_atomic(kaddr, KM_USER0);
965 if (left == 0)
966 goto success;
967 }
968
969 /* Do it the slow way */
970 kaddr = kmap(page);
971 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
972 kunmap(page);
973
974 if (left) {
975 size -= left;
976 desc->error = -EFAULT;
977 }
978success:
979 desc->count = count - size;
980 desc->written += size;
981 desc->arg.buf += size;
982 return size;
983}
Steven Whitehousec25ec8f2006-01-30 08:57:31 +0000984EXPORT_SYMBOL(file_read_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986/*
987 * This is the "read()" routine for all filesystems
988 * that can use the page cache directly.
989 */
990ssize_t
991__generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
992 unsigned long nr_segs, loff_t *ppos)
993{
994 struct file *filp = iocb->ki_filp;
995 ssize_t retval;
996 unsigned long seg;
997 size_t count;
998
999 count = 0;
1000 for (seg = 0; seg < nr_segs; seg++) {
1001 const struct iovec *iv = &iov[seg];
1002
1003 /*
1004 * If any segment has a negative length, or the cumulative
1005 * length ever wraps negative then return -EINVAL.
1006 */
1007 count += iv->iov_len;
1008 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1009 return -EINVAL;
1010 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1011 continue;
1012 if (seg == 0)
1013 return -EFAULT;
1014 nr_segs = seg;
1015 count -= iv->iov_len; /* This segment is no good */
1016 break;
1017 }
1018
1019 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1020 if (filp->f_flags & O_DIRECT) {
1021 loff_t pos = *ppos, size;
1022 struct address_space *mapping;
1023 struct inode *inode;
1024
1025 mapping = filp->f_mapping;
1026 inode = mapping->host;
1027 retval = 0;
1028 if (!count)
1029 goto out; /* skip atime */
1030 size = i_size_read(inode);
1031 if (pos < size) {
1032 retval = generic_file_direct_IO(READ, iocb,
1033 iov, pos, nr_segs);
Suparna Bhattacharyab5c44c22005-05-21 16:33:36 -07001034 if (retval > 0 && !is_sync_kiocb(iocb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 retval = -EIOCBQUEUED;
1036 if (retval > 0)
1037 *ppos = pos + retval;
1038 }
1039 file_accessed(filp);
1040 goto out;
1041 }
1042
1043 retval = 0;
1044 if (count) {
1045 for (seg = 0; seg < nr_segs; seg++) {
1046 read_descriptor_t desc;
1047
1048 desc.written = 0;
1049 desc.arg.buf = iov[seg].iov_base;
1050 desc.count = iov[seg].iov_len;
1051 if (desc.count == 0)
1052 continue;
1053 desc.error = 0;
1054 do_generic_file_read(filp,ppos,&desc,file_read_actor);
1055 retval += desc.written;
Tejun Heo39e88ca2005-10-30 15:02:40 -08001056 if (desc.error) {
1057 retval = retval ?: desc.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 break;
1059 }
1060 }
1061 }
1062out:
1063 return retval;
1064}
1065
1066EXPORT_SYMBOL(__generic_file_aio_read);
1067
1068ssize_t
1069generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1070{
1071 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1072
1073 BUG_ON(iocb->ki_pos != pos);
1074 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1075}
1076
1077EXPORT_SYMBOL(generic_file_aio_read);
1078
1079ssize_t
1080generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1081{
1082 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1083 struct kiocb kiocb;
1084 ssize_t ret;
1085
1086 init_sync_kiocb(&kiocb, filp);
1087 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
1088 if (-EIOCBQUEUED == ret)
1089 ret = wait_on_sync_kiocb(&kiocb);
1090 return ret;
1091}
1092
1093EXPORT_SYMBOL(generic_file_read);
1094
1095int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1096{
1097 ssize_t written;
1098 unsigned long count = desc->count;
1099 struct file *file = desc->arg.data;
1100
1101 if (size > count)
1102 size = count;
1103
1104 written = file->f_op->sendpage(file, page, offset,
1105 size, &file->f_pos, size<count);
1106 if (written < 0) {
1107 desc->error = written;
1108 written = 0;
1109 }
1110 desc->count = count - written;
1111 desc->written += written;
1112 return written;
1113}
1114
1115ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1116 size_t count, read_actor_t actor, void *target)
1117{
1118 read_descriptor_t desc;
1119
1120 if (!count)
1121 return 0;
1122
1123 desc.written = 0;
1124 desc.count = count;
1125 desc.arg.data = target;
1126 desc.error = 0;
1127
1128 do_generic_file_read(in_file, ppos, &desc, actor);
1129 if (desc.written)
1130 return desc.written;
1131 return desc.error;
1132}
1133
1134EXPORT_SYMBOL(generic_file_sendfile);
1135
1136static ssize_t
1137do_readahead(struct address_space *mapping, struct file *filp,
1138 unsigned long index, unsigned long nr)
1139{
1140 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1141 return -EINVAL;
1142
1143 force_page_cache_readahead(mapping, filp, index,
1144 max_sane_readahead(nr));
1145 return 0;
1146}
1147
1148asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1149{
1150 ssize_t ret;
1151 struct file *file;
1152
1153 ret = -EBADF;
1154 file = fget(fd);
1155 if (file) {
1156 if (file->f_mode & FMODE_READ) {
1157 struct address_space *mapping = file->f_mapping;
1158 unsigned long start = offset >> PAGE_CACHE_SHIFT;
1159 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1160 unsigned long len = end - start + 1;
1161 ret = do_readahead(mapping, file, start, len);
1162 }
1163 fput(file);
1164 }
1165 return ret;
1166}
1167
1168#ifdef CONFIG_MMU
1169/*
1170 * This adds the requested page to the page cache if it isn't already there,
1171 * and schedules an I/O to read in its contents from disk.
1172 */
1173static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
1174static int fastcall page_cache_read(struct file * file, unsigned long offset)
1175{
1176 struct address_space *mapping = file->f_mapping;
1177 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08001178 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Zach Brown994fc28c2005-12-15 14:28:17 -08001180 do {
1181 page = page_cache_alloc_cold(mapping);
1182 if (!page)
1183 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Zach Brown994fc28c2005-12-15 14:28:17 -08001185 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1186 if (ret == 0)
1187 ret = mapping->a_ops->readpage(file, page);
1188 else if (ret == -EEXIST)
1189 ret = 0; /* losing race to add is OK */
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Zach Brown994fc28c2005-12-15 14:28:17 -08001193 } while (ret == AOP_TRUNCATED_PAGE);
1194
1195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198#define MMAP_LOTSAMISS (100)
1199
1200/*
1201 * filemap_nopage() is invoked via the vma operations vector for a
1202 * mapped memory region to read in file data during a page fault.
1203 *
1204 * The goto's are kind of ugly, but this streamlines the normal case of having
1205 * it in the page cache, and handles the special cases reasonably without
1206 * having a lot of duplicated code.
1207 */
1208struct page *filemap_nopage(struct vm_area_struct *area,
1209 unsigned long address, int *type)
1210{
1211 int error;
1212 struct file *file = area->vm_file;
1213 struct address_space *mapping = file->f_mapping;
1214 struct file_ra_state *ra = &file->f_ra;
1215 struct inode *inode = mapping->host;
1216 struct page *page;
1217 unsigned long size, pgoff;
1218 int did_readaround = 0, majmin = VM_FAULT_MINOR;
1219
1220 pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1221
1222retry_all:
1223 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1224 if (pgoff >= size)
1225 goto outside_data_content;
1226
1227 /* If we don't want any read-ahead, don't bother */
1228 if (VM_RandomReadHint(area))
1229 goto no_cached_page;
1230
1231 /*
1232 * The readahead code wants to be told about each and every page
1233 * so it can build and shrink its windows appropriately
1234 *
1235 * For sequential accesses, we use the generic readahead logic.
1236 */
1237 if (VM_SequentialReadHint(area))
1238 page_cache_readahead(mapping, ra, file, pgoff, 1);
1239
1240 /*
1241 * Do we have something in the page cache already?
1242 */
1243retry_find:
1244 page = find_get_page(mapping, pgoff);
1245 if (!page) {
1246 unsigned long ra_pages;
1247
1248 if (VM_SequentialReadHint(area)) {
1249 handle_ra_miss(mapping, ra, pgoff);
1250 goto no_cached_page;
1251 }
1252 ra->mmap_miss++;
1253
1254 /*
1255 * Do we miss much more than hit in this file? If so,
1256 * stop bothering with read-ahead. It will only hurt.
1257 */
1258 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1259 goto no_cached_page;
1260
1261 /*
1262 * To keep the pgmajfault counter straight, we need to
1263 * check did_readaround, as this is an inner loop.
1264 */
1265 if (!did_readaround) {
1266 majmin = VM_FAULT_MAJOR;
1267 inc_page_state(pgmajfault);
1268 }
1269 did_readaround = 1;
1270 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1271 if (ra_pages) {
1272 pgoff_t start = 0;
1273
1274 if (pgoff > ra_pages / 2)
1275 start = pgoff - ra_pages / 2;
1276 do_page_cache_readahead(mapping, file, start, ra_pages);
1277 }
1278 page = find_get_page(mapping, pgoff);
1279 if (!page)
1280 goto no_cached_page;
1281 }
1282
1283 if (!did_readaround)
1284 ra->mmap_hit++;
1285
1286 /*
1287 * Ok, found a page in the page cache, now we need to check
1288 * that it's up-to-date.
1289 */
1290 if (!PageUptodate(page))
1291 goto page_not_uptodate;
1292
1293success:
1294 /*
1295 * Found the page and have a reference on it.
1296 */
1297 mark_page_accessed(page);
1298 if (type)
1299 *type = majmin;
1300 return page;
1301
1302outside_data_content:
1303 /*
1304 * An external ptracer can access pages that normally aren't
1305 * accessible..
1306 */
1307 if (area->vm_mm == current->mm)
1308 return NULL;
1309 /* Fall through to the non-read-ahead case */
1310no_cached_page:
1311 /*
1312 * We're only likely to ever get here if MADV_RANDOM is in
1313 * effect.
1314 */
1315 error = page_cache_read(file, pgoff);
1316 grab_swap_token();
1317
1318 /*
1319 * The page we want has now been added to the page cache.
1320 * In the unlikely event that someone removed it in the
1321 * meantime, we'll just come back here and read it again.
1322 */
1323 if (error >= 0)
1324 goto retry_find;
1325
1326 /*
1327 * An error return from page_cache_read can result if the
1328 * system is low on memory, or a problem occurs while trying
1329 * to schedule I/O.
1330 */
1331 if (error == -ENOMEM)
1332 return NOPAGE_OOM;
1333 return NULL;
1334
1335page_not_uptodate:
1336 if (!did_readaround) {
1337 majmin = VM_FAULT_MAJOR;
1338 inc_page_state(pgmajfault);
1339 }
1340 lock_page(page);
1341
1342 /* Did it get unhashed while we waited for it? */
1343 if (!page->mapping) {
1344 unlock_page(page);
1345 page_cache_release(page);
1346 goto retry_all;
1347 }
1348
1349 /* Did somebody else get it up-to-date? */
1350 if (PageUptodate(page)) {
1351 unlock_page(page);
1352 goto success;
1353 }
1354
Zach Brown994fc28c2005-12-15 14:28:17 -08001355 error = mapping->a_ops->readpage(file, page);
1356 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 wait_on_page_locked(page);
1358 if (PageUptodate(page))
1359 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001360 } else if (error == AOP_TRUNCATED_PAGE) {
1361 page_cache_release(page);
1362 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
1365 /*
1366 * Umm, take care of errors if the page isn't up-to-date.
1367 * Try to re-read it _once_. We do this synchronously,
1368 * because there really aren't any performance issues here
1369 * and we need to check for errors.
1370 */
1371 lock_page(page);
1372
1373 /* Somebody truncated the page on us? */
1374 if (!page->mapping) {
1375 unlock_page(page);
1376 page_cache_release(page);
1377 goto retry_all;
1378 }
1379
1380 /* Somebody else successfully read it in? */
1381 if (PageUptodate(page)) {
1382 unlock_page(page);
1383 goto success;
1384 }
1385 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08001386 error = mapping->a_ops->readpage(file, page);
1387 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 wait_on_page_locked(page);
1389 if (PageUptodate(page))
1390 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001391 } else if (error == AOP_TRUNCATED_PAGE) {
1392 page_cache_release(page);
1393 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
1396 /*
1397 * Things didn't work out. Return zero to tell the
1398 * mm layer so, possibly freeing the page cache page first.
1399 */
1400 page_cache_release(page);
1401 return NULL;
1402}
1403
1404EXPORT_SYMBOL(filemap_nopage);
1405
1406static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1407 int nonblock)
1408{
1409 struct address_space *mapping = file->f_mapping;
1410 struct page *page;
1411 int error;
1412
1413 /*
1414 * Do we have something in the page cache already?
1415 */
1416retry_find:
1417 page = find_get_page(mapping, pgoff);
1418 if (!page) {
1419 if (nonblock)
1420 return NULL;
1421 goto no_cached_page;
1422 }
1423
1424 /*
1425 * Ok, found a page in the page cache, now we need to check
1426 * that it's up-to-date.
1427 */
Jeff Moyerd3457342005-04-16 15:24:05 -07001428 if (!PageUptodate(page)) {
1429 if (nonblock) {
1430 page_cache_release(page);
1431 return NULL;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto page_not_uptodate;
Jeff Moyerd3457342005-04-16 15:24:05 -07001434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436success:
1437 /*
1438 * Found the page and have a reference on it.
1439 */
1440 mark_page_accessed(page);
1441 return page;
1442
1443no_cached_page:
1444 error = page_cache_read(file, pgoff);
1445
1446 /*
1447 * The page we want has now been added to the page cache.
1448 * In the unlikely event that someone removed it in the
1449 * meantime, we'll just come back here and read it again.
1450 */
1451 if (error >= 0)
1452 goto retry_find;
1453
1454 /*
1455 * An error return from page_cache_read can result if the
1456 * system is low on memory, or a problem occurs while trying
1457 * to schedule I/O.
1458 */
1459 return NULL;
1460
1461page_not_uptodate:
1462 lock_page(page);
1463
1464 /* Did it get unhashed while we waited for it? */
1465 if (!page->mapping) {
1466 unlock_page(page);
1467 goto err;
1468 }
1469
1470 /* Did somebody else get it up-to-date? */
1471 if (PageUptodate(page)) {
1472 unlock_page(page);
1473 goto success;
1474 }
1475
Zach Brown994fc28c2005-12-15 14:28:17 -08001476 error = mapping->a_ops->readpage(file, page);
1477 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 wait_on_page_locked(page);
1479 if (PageUptodate(page))
1480 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001481 } else if (error == AOP_TRUNCATED_PAGE) {
1482 page_cache_release(page);
1483 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
1486 /*
1487 * Umm, take care of errors if the page isn't up-to-date.
1488 * Try to re-read it _once_. We do this synchronously,
1489 * because there really aren't any performance issues here
1490 * and we need to check for errors.
1491 */
1492 lock_page(page);
1493
1494 /* Somebody truncated the page on us? */
1495 if (!page->mapping) {
1496 unlock_page(page);
1497 goto err;
1498 }
1499 /* Somebody else successfully read it in? */
1500 if (PageUptodate(page)) {
1501 unlock_page(page);
1502 goto success;
1503 }
1504
1505 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08001506 error = mapping->a_ops->readpage(file, page);
1507 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 wait_on_page_locked(page);
1509 if (PageUptodate(page))
1510 goto success;
Zach Brown994fc28c2005-12-15 14:28:17 -08001511 } else if (error == AOP_TRUNCATED_PAGE) {
1512 page_cache_release(page);
1513 goto retry_find;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515
1516 /*
1517 * Things didn't work out. Return zero to tell the
1518 * mm layer so, possibly freeing the page cache page first.
1519 */
1520err:
1521 page_cache_release(page);
1522
1523 return NULL;
1524}
1525
1526int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
1527 unsigned long len, pgprot_t prot, unsigned long pgoff,
1528 int nonblock)
1529{
1530 struct file *file = vma->vm_file;
1531 struct address_space *mapping = file->f_mapping;
1532 struct inode *inode = mapping->host;
1533 unsigned long size;
1534 struct mm_struct *mm = vma->vm_mm;
1535 struct page *page;
1536 int err;
1537
1538 if (!nonblock)
1539 force_page_cache_readahead(mapping, vma->vm_file,
1540 pgoff, len >> PAGE_CACHE_SHIFT);
1541
1542repeat:
1543 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1544 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1545 return -EINVAL;
1546
1547 page = filemap_getpage(file, pgoff, nonblock);
Paolo 'Blaisorblade' Giarrussod44ed4f2005-09-03 15:54:55 -07001548
1549 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1550 * done in shmem_populate calling shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (!page && !nonblock)
1552 return -ENOMEM;
Paolo 'Blaisorblade' Giarrussod44ed4f2005-09-03 15:54:55 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (page) {
1555 err = install_page(mm, vma, addr, page, prot);
1556 if (err) {
1557 page_cache_release(page);
1558 return err;
1559 }
Hugh Dickins65500d22005-10-29 18:15:59 -07001560 } else if (vma->vm_flags & VM_NONLINEAR) {
Paolo 'Blaisorblade' Giarrussod44ed4f2005-09-03 15:54:55 -07001561 /* No page was found just because we can't read it in now (being
1562 * here implies nonblock != 0), but the page may exist, so set
1563 * the PTE to fault it in later. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 err = install_file_pte(mm, vma, addr, pgoff, prot);
1565 if (err)
1566 return err;
1567 }
1568
1569 len -= PAGE_SIZE;
1570 addr += PAGE_SIZE;
1571 pgoff++;
1572 if (len)
1573 goto repeat;
1574
1575 return 0;
1576}
Nikita Danilovb1459462005-10-29 18:17:02 -07001577EXPORT_SYMBOL(filemap_populate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579struct vm_operations_struct generic_file_vm_ops = {
1580 .nopage = filemap_nopage,
1581 .populate = filemap_populate,
1582};
1583
1584/* This is used for a general mmap of a disk file */
1585
1586int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1587{
1588 struct address_space *mapping = file->f_mapping;
1589
1590 if (!mapping->a_ops->readpage)
1591 return -ENOEXEC;
1592 file_accessed(file);
1593 vma->vm_ops = &generic_file_vm_ops;
1594 return 0;
1595}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*
1598 * This is for filesystems which do not implement ->writepage.
1599 */
1600int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1601{
1602 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1603 return -EINVAL;
1604 return generic_file_mmap(file, vma);
1605}
1606#else
1607int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1608{
1609 return -ENOSYS;
1610}
1611int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1612{
1613 return -ENOSYS;
1614}
1615#endif /* CONFIG_MMU */
1616
1617EXPORT_SYMBOL(generic_file_mmap);
1618EXPORT_SYMBOL(generic_file_readonly_mmap);
1619
1620static inline struct page *__read_cache_page(struct address_space *mapping,
1621 unsigned long index,
1622 int (*filler)(void *,struct page*),
1623 void *data)
1624{
1625 struct page *page, *cached_page = NULL;
1626 int err;
1627repeat:
1628 page = find_get_page(mapping, index);
1629 if (!page) {
1630 if (!cached_page) {
1631 cached_page = page_cache_alloc_cold(mapping);
1632 if (!cached_page)
1633 return ERR_PTR(-ENOMEM);
1634 }
1635 err = add_to_page_cache_lru(cached_page, mapping,
1636 index, GFP_KERNEL);
1637 if (err == -EEXIST)
1638 goto repeat;
1639 if (err < 0) {
1640 /* Presumably ENOMEM for radix tree node */
1641 page_cache_release(cached_page);
1642 return ERR_PTR(err);
1643 }
1644 page = cached_page;
1645 cached_page = NULL;
1646 err = filler(data, page);
1647 if (err < 0) {
1648 page_cache_release(page);
1649 page = ERR_PTR(err);
1650 }
1651 }
1652 if (cached_page)
1653 page_cache_release(cached_page);
1654 return page;
1655}
1656
1657/*
1658 * Read into the page cache. If a page already exists,
1659 * and PageUptodate() is not set, try to fill the page.
1660 */
1661struct page *read_cache_page(struct address_space *mapping,
1662 unsigned long index,
1663 int (*filler)(void *,struct page*),
1664 void *data)
1665{
1666 struct page *page;
1667 int err;
1668
1669retry:
1670 page = __read_cache_page(mapping, index, filler, data);
1671 if (IS_ERR(page))
1672 goto out;
1673 mark_page_accessed(page);
1674 if (PageUptodate(page))
1675 goto out;
1676
1677 lock_page(page);
1678 if (!page->mapping) {
1679 unlock_page(page);
1680 page_cache_release(page);
1681 goto retry;
1682 }
1683 if (PageUptodate(page)) {
1684 unlock_page(page);
1685 goto out;
1686 }
1687 err = filler(data, page);
1688 if (err < 0) {
1689 page_cache_release(page);
1690 page = ERR_PTR(err);
1691 }
1692 out:
1693 return page;
1694}
1695
1696EXPORT_SYMBOL(read_cache_page);
1697
1698/*
1699 * If the page was newly created, increment its refcount and add it to the
1700 * caller's lru-buffering pagevec. This function is specifically for
1701 * generic_file_write().
1702 */
1703static inline struct page *
1704__grab_cache_page(struct address_space *mapping, unsigned long index,
1705 struct page **cached_page, struct pagevec *lru_pvec)
1706{
1707 int err;
1708 struct page *page;
1709repeat:
1710 page = find_lock_page(mapping, index);
1711 if (!page) {
1712 if (!*cached_page) {
1713 *cached_page = page_cache_alloc(mapping);
1714 if (!*cached_page)
1715 return NULL;
1716 }
1717 err = add_to_page_cache(*cached_page, mapping,
1718 index, GFP_KERNEL);
1719 if (err == -EEXIST)
1720 goto repeat;
1721 if (err == 0) {
1722 page = *cached_page;
1723 page_cache_get(page);
1724 if (!pagevec_add(lru_pvec, page))
1725 __pagevec_lru_add(lru_pvec);
1726 *cached_page = NULL;
1727 }
1728 }
1729 return page;
1730}
1731
1732/*
1733 * The logic we want is
1734 *
1735 * if suid or (sgid and xgrp)
1736 * remove privs
1737 */
1738int remove_suid(struct dentry *dentry)
1739{
1740 mode_t mode = dentry->d_inode->i_mode;
1741 int kill = 0;
1742 int result = 0;
1743
1744 /* suid always must be killed */
1745 if (unlikely(mode & S_ISUID))
1746 kill = ATTR_KILL_SUID;
1747
1748 /*
1749 * sgid without any exec bits is just a mandatory locking mark; leave
1750 * it alone. If some exec bits are set, it's a real sgid; kill it.
1751 */
1752 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1753 kill |= ATTR_KILL_SGID;
1754
1755 if (unlikely(kill && !capable(CAP_FSETID))) {
1756 struct iattr newattrs;
1757
1758 newattrs.ia_valid = ATTR_FORCE | kill;
1759 result = notify_change(dentry, &newattrs);
1760 }
1761 return result;
1762}
1763EXPORT_SYMBOL(remove_suid);
1764
Carsten Otteceffc072005-06-23 22:05:25 -07001765size_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766__filemap_copy_from_user_iovec(char *vaddr,
1767 const struct iovec *iov, size_t base, size_t bytes)
1768{
1769 size_t copied = 0, left = 0;
1770
1771 while (bytes) {
1772 char __user *buf = iov->iov_base + base;
1773 int copy = min(bytes, iov->iov_len - base);
1774
1775 base = 0;
1776 left = __copy_from_user_inatomic(vaddr, buf, copy);
1777 copied += copy;
1778 bytes -= copy;
1779 vaddr += copy;
1780 iov++;
1781
1782 if (unlikely(left)) {
1783 /* zero the rest of the target like __copy_from_user */
1784 if (bytes)
1785 memset(vaddr, 0, bytes);
1786 break;
1787 }
1788 }
1789 return copied - left;
1790}
1791
1792/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 * Performs necessary checks before doing a write
1794 *
1795 * Can adjust writing position aor amount of bytes to write.
1796 * Returns appropriate error code that caller should return or
1797 * zero in case that write should be allowed.
1798 */
1799inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1800{
1801 struct inode *inode = file->f_mapping->host;
1802 unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1803
1804 if (unlikely(*pos < 0))
1805 return -EINVAL;
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!isblk) {
1808 /* FIXME: this is for backwards compatibility with 2.4 */
1809 if (file->f_flags & O_APPEND)
1810 *pos = i_size_read(inode);
1811
1812 if (limit != RLIM_INFINITY) {
1813 if (*pos >= limit) {
1814 send_sig(SIGXFSZ, current, 0);
1815 return -EFBIG;
1816 }
1817 if (*count > limit - (typeof(limit))*pos) {
1818 *count = limit - (typeof(limit))*pos;
1819 }
1820 }
1821 }
1822
1823 /*
1824 * LFS rule
1825 */
1826 if (unlikely(*pos + *count > MAX_NON_LFS &&
1827 !(file->f_flags & O_LARGEFILE))) {
1828 if (*pos >= MAX_NON_LFS) {
1829 send_sig(SIGXFSZ, current, 0);
1830 return -EFBIG;
1831 }
1832 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1833 *count = MAX_NON_LFS - (unsigned long)*pos;
1834 }
1835 }
1836
1837 /*
1838 * Are we about to exceed the fs block limit ?
1839 *
1840 * If we have written data it becomes a short write. If we have
1841 * exceeded without writing data we send a signal and return EFBIG.
1842 * Linus frestrict idea will clean these up nicely..
1843 */
1844 if (likely(!isblk)) {
1845 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1846 if (*count || *pos > inode->i_sb->s_maxbytes) {
1847 send_sig(SIGXFSZ, current, 0);
1848 return -EFBIG;
1849 }
1850 /* zero-length writes at ->s_maxbytes are OK */
1851 }
1852
1853 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1854 *count = inode->i_sb->s_maxbytes - *pos;
1855 } else {
1856 loff_t isize;
1857 if (bdev_read_only(I_BDEV(inode)))
1858 return -EPERM;
1859 isize = i_size_read(inode);
1860 if (*pos >= isize) {
1861 if (*count || *pos > isize)
1862 return -ENOSPC;
1863 }
1864
1865 if (*pos + *count > isize)
1866 *count = isize - *pos;
1867 }
1868 return 0;
1869}
1870EXPORT_SYMBOL(generic_write_checks);
1871
1872ssize_t
1873generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1874 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1875 size_t count, size_t ocount)
1876{
1877 struct file *file = iocb->ki_filp;
1878 struct address_space *mapping = file->f_mapping;
1879 struct inode *inode = mapping->host;
1880 ssize_t written;
1881
1882 if (count != ocount)
1883 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1884
1885 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1886 if (written > 0) {
1887 loff_t end = pos + written;
1888 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1889 i_size_write(inode, end);
1890 mark_inode_dirty(inode);
1891 }
1892 *ppos = end;
1893 }
1894
1895 /*
1896 * Sync the fs metadata but not the minor inode changes and
1897 * of course not the data as we did direct DMA for the IO.
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001898 * i_mutex is held, which protects generic_osync_inode() from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * livelocking.
1900 */
Hifumi Hisashi1e8a81c2005-06-25 14:54:32 -07001901 if (written >= 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
1902 int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
1903 if (err < 0)
1904 written = err;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (written == count && !is_sync_kiocb(iocb))
1907 written = -EIOCBQUEUED;
1908 return written;
1909}
1910EXPORT_SYMBOL(generic_file_direct_write);
1911
1912ssize_t
1913generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
1914 unsigned long nr_segs, loff_t pos, loff_t *ppos,
1915 size_t count, ssize_t written)
1916{
1917 struct file *file = iocb->ki_filp;
1918 struct address_space * mapping = file->f_mapping;
1919 struct address_space_operations *a_ops = mapping->a_ops;
1920 struct inode *inode = mapping->host;
1921 long status = 0;
1922 struct page *page;
1923 struct page *cached_page = NULL;
1924 size_t bytes;
1925 struct pagevec lru_pvec;
1926 const struct iovec *cur_iov = iov; /* current iovec */
1927 size_t iov_base = 0; /* offset in the current iovec */
1928 char __user *buf;
1929
1930 pagevec_init(&lru_pvec, 0);
1931
1932 /*
1933 * handle partial DIO write. Adjust cur_iov if needed.
1934 */
1935 if (likely(nr_segs == 1))
1936 buf = iov->iov_base + written;
1937 else {
1938 filemap_set_next_iovec(&cur_iov, &iov_base, written);
akpm@osdl.orgf021e922005-05-01 08:58:35 -07001939 buf = cur_iov->iov_base + iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
1942 do {
1943 unsigned long index;
1944 unsigned long offset;
Martin Schwidefskya5117182005-06-06 13:35:54 -07001945 unsigned long maxlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 size_t copied;
1947
1948 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1949 index = pos >> PAGE_CACHE_SHIFT;
1950 bytes = PAGE_CACHE_SIZE - offset;
1951 if (bytes > count)
1952 bytes = count;
1953
1954 /*
1955 * Bring in the user page that we will copy from _first_.
1956 * Otherwise there's a nasty deadlock on copying from the
1957 * same page as we're writing to, without it being marked
1958 * up-to-date.
1959 */
Martin Schwidefskya5117182005-06-06 13:35:54 -07001960 maxlen = cur_iov->iov_len - iov_base;
1961 if (maxlen > bytes)
1962 maxlen = bytes;
1963 fault_in_pages_readable(buf, maxlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1966 if (!page) {
1967 status = -ENOMEM;
1968 break;
1969 }
1970
1971 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1972 if (unlikely(status)) {
1973 loff_t isize = i_size_read(inode);
Zach Brown994fc28c2005-12-15 14:28:17 -08001974
1975 if (status != AOP_TRUNCATED_PAGE)
1976 unlock_page(page);
1977 page_cache_release(page);
1978 if (status == AOP_TRUNCATED_PAGE)
1979 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /*
1981 * prepare_write() may have instantiated a few blocks
1982 * outside i_size. Trim these off again.
1983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (pos + bytes > isize)
1985 vmtruncate(inode, isize);
1986 break;
1987 }
1988 if (likely(nr_segs == 1))
1989 copied = filemap_copy_from_user(page, offset,
1990 buf, bytes);
1991 else
1992 copied = filemap_copy_from_user_iovec(page, offset,
1993 cur_iov, iov_base, bytes);
1994 flush_dcache_page(page);
1995 status = a_ops->commit_write(file, page, offset, offset+bytes);
Zach Brown994fc28c2005-12-15 14:28:17 -08001996 if (status == AOP_TRUNCATED_PAGE) {
1997 page_cache_release(page);
1998 continue;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (likely(copied > 0)) {
2001 if (!status)
2002 status = copied;
2003
2004 if (status >= 0) {
2005 written += status;
2006 count -= status;
2007 pos += status;
2008 buf += status;
akpm@osdl.orgf021e922005-05-01 08:58:35 -07002009 if (unlikely(nr_segs > 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 filemap_set_next_iovec(&cur_iov,
2011 &iov_base, status);
Badari Pulavartyb0cfbd92005-06-25 14:55:42 -07002012 if (count)
2013 buf = cur_iov->iov_base +
2014 iov_base;
Martin Schwidefskya5117182005-06-06 13:35:54 -07002015 } else {
2016 iov_base += status;
akpm@osdl.orgf021e922005-05-01 08:58:35 -07002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019 }
2020 if (unlikely(copied != bytes))
2021 if (status >= 0)
2022 status = -EFAULT;
2023 unlock_page(page);
2024 mark_page_accessed(page);
2025 page_cache_release(page);
2026 if (status < 0)
2027 break;
2028 balance_dirty_pages_ratelimited(mapping);
2029 cond_resched();
2030 } while (count);
2031 *ppos = pos;
2032
2033 if (cached_page)
2034 page_cache_release(cached_page);
2035
2036 /*
2037 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2038 */
2039 if (likely(status >= 0)) {
2040 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2041 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2042 status = generic_osync_inode(inode, mapping,
2043 OSYNC_METADATA|OSYNC_DATA);
2044 }
2045 }
2046
2047 /*
2048 * If we get here for O_DIRECT writes then we must have fallen through
2049 * to buffered writes (block instantiation inside i_size). So we sync
2050 * the file data here, to try to honour O_DIRECT expectations.
2051 */
2052 if (unlikely(file->f_flags & O_DIRECT) && written)
2053 status = filemap_write_and_wait(mapping);
2054
2055 pagevec_lru_add(&lru_pvec);
2056 return written ? written : status;
2057}
2058EXPORT_SYMBOL(generic_file_buffered_write);
2059
Adrian Bunk5ce78522005-09-10 00:26:28 -07002060static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061__generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2062 unsigned long nr_segs, loff_t *ppos)
2063{
2064 struct file *file = iocb->ki_filp;
2065 struct address_space * mapping = file->f_mapping;
2066 size_t ocount; /* original count */
2067 size_t count; /* after file limit checks */
2068 struct inode *inode = mapping->host;
2069 unsigned long seg;
2070 loff_t pos;
2071 ssize_t written;
2072 ssize_t err;
2073
2074 ocount = 0;
2075 for (seg = 0; seg < nr_segs; seg++) {
2076 const struct iovec *iv = &iov[seg];
2077
2078 /*
2079 * If any segment has a negative length, or the cumulative
2080 * length ever wraps negative then return -EINVAL.
2081 */
2082 ocount += iv->iov_len;
2083 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2084 return -EINVAL;
2085 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2086 continue;
2087 if (seg == 0)
2088 return -EFAULT;
2089 nr_segs = seg;
2090 ocount -= iv->iov_len; /* This segment is no good */
2091 break;
2092 }
2093
2094 count = ocount;
2095 pos = *ppos;
2096
2097 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2098
2099 /* We can write back this queue in page reclaim */
2100 current->backing_dev_info = mapping->backing_dev_info;
2101 written = 0;
2102
2103 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2104 if (err)
2105 goto out;
2106
2107 if (count == 0)
2108 goto out;
2109
2110 err = remove_suid(file->f_dentry);
2111 if (err)
2112 goto out;
2113
Christoph Hellwig870f4812006-01-09 20:52:01 -08002114 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2117 if (unlikely(file->f_flags & O_DIRECT)) {
2118 written = generic_file_direct_write(iocb, iov,
2119 &nr_segs, pos, ppos, count, ocount);
2120 if (written < 0 || written == count)
2121 goto out;
2122 /*
2123 * direct-io write to a hole: fall through to buffered I/O
2124 * for completing the rest of the request.
2125 */
2126 pos += written;
2127 count -= written;
2128 }
2129
2130 written = generic_file_buffered_write(iocb, iov, nr_segs,
2131 pos, ppos, count, written);
2132out:
2133 current->backing_dev_info = NULL;
2134 return written ? written : err;
2135}
2136EXPORT_SYMBOL(generic_file_aio_write_nolock);
2137
2138ssize_t
2139generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2140 unsigned long nr_segs, loff_t *ppos)
2141{
2142 struct file *file = iocb->ki_filp;
2143 struct address_space *mapping = file->f_mapping;
2144 struct inode *inode = mapping->host;
2145 ssize_t ret;
2146 loff_t pos = *ppos;
2147
2148 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
2149
2150 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2151 int err;
2152
2153 err = sync_page_range_nolock(inode, mapping, pos, ret);
2154 if (err < 0)
2155 ret = err;
2156 }
2157 return ret;
2158}
2159
Adrian Bunk5ce78522005-09-10 00:26:28 -07002160static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161__generic_file_write_nolock(struct file *file, const struct iovec *iov,
2162 unsigned long nr_segs, loff_t *ppos)
2163{
2164 struct kiocb kiocb;
2165 ssize_t ret;
2166
2167 init_sync_kiocb(&kiocb, file);
2168 ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2169 if (ret == -EIOCBQUEUED)
2170 ret = wait_on_sync_kiocb(&kiocb);
2171 return ret;
2172}
2173
2174ssize_t
2175generic_file_write_nolock(struct file *file, const struct iovec *iov,
2176 unsigned long nr_segs, loff_t *ppos)
2177{
2178 struct kiocb kiocb;
2179 ssize_t ret;
2180
2181 init_sync_kiocb(&kiocb, file);
2182 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2183 if (-EIOCBQUEUED == ret)
2184 ret = wait_on_sync_kiocb(&kiocb);
2185 return ret;
2186}
2187EXPORT_SYMBOL(generic_file_write_nolock);
2188
2189ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2190 size_t count, loff_t pos)
2191{
2192 struct file *file = iocb->ki_filp;
2193 struct address_space *mapping = file->f_mapping;
2194 struct inode *inode = mapping->host;
2195 ssize_t ret;
2196 struct iovec local_iov = { .iov_base = (void __user *)buf,
2197 .iov_len = count };
2198
2199 BUG_ON(iocb->ki_pos != pos);
2200
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002201 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1,
2203 &iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002204 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2207 ssize_t err;
2208
2209 err = sync_page_range(inode, mapping, pos, ret);
2210 if (err < 0)
2211 ret = err;
2212 }
2213 return ret;
2214}
2215EXPORT_SYMBOL(generic_file_aio_write);
2216
2217ssize_t generic_file_write(struct file *file, const char __user *buf,
2218 size_t count, loff_t *ppos)
2219{
2220 struct address_space *mapping = file->f_mapping;
2221 struct inode *inode = mapping->host;
2222 ssize_t ret;
2223 struct iovec local_iov = { .iov_base = (void __user *)buf,
2224 .iov_len = count };
2225
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002226 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002228 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2231 ssize_t err;
2232
2233 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2234 if (err < 0)
2235 ret = err;
2236 }
2237 return ret;
2238}
2239EXPORT_SYMBOL(generic_file_write);
2240
2241ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2242 unsigned long nr_segs, loff_t *ppos)
2243{
2244 struct kiocb kiocb;
2245 ssize_t ret;
2246
2247 init_sync_kiocb(&kiocb, filp);
2248 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
2249 if (-EIOCBQUEUED == ret)
2250 ret = wait_on_sync_kiocb(&kiocb);
2251 return ret;
2252}
2253EXPORT_SYMBOL(generic_file_readv);
2254
2255ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2256 unsigned long nr_segs, loff_t *ppos)
2257{
2258 struct address_space *mapping = file->f_mapping;
2259 struct inode *inode = mapping->host;
2260 ssize_t ret;
2261
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002262 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002264 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2267 int err;
2268
2269 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2270 if (err < 0)
2271 ret = err;
2272 }
2273 return ret;
2274}
2275EXPORT_SYMBOL(generic_file_writev);
2276
2277/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002278 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 * went wrong during pagecache shootdown.
2280 */
Adrian Bunk5ce78522005-09-10 00:26:28 -07002281static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2283 loff_t offset, unsigned long nr_segs)
2284{
2285 struct file *file = iocb->ki_filp;
2286 struct address_space *mapping = file->f_mapping;
2287 ssize_t retval;
2288 size_t write_len = 0;
2289
2290 /*
2291 * If it's a write, unmap all mmappings of the file up-front. This
2292 * will cause any pte dirty bits to be propagated into the pageframes
2293 * for the subsequent filemap_write_and_wait().
2294 */
2295 if (rw == WRITE) {
2296 write_len = iov_length(iov, nr_segs);
2297 if (mapping_mapped(mapping))
2298 unmap_mapping_range(mapping, offset, write_len, 0);
2299 }
2300
2301 retval = filemap_write_and_wait(mapping);
2302 if (retval == 0) {
2303 retval = mapping->a_ops->direct_IO(rw, iocb, iov,
2304 offset, nr_segs);
2305 if (rw == WRITE && mapping->nrpages) {
2306 pgoff_t end = (offset + write_len - 1)
2307 >> PAGE_CACHE_SHIFT;
2308 int err = invalidate_inode_pages2_range(mapping,
2309 offset >> PAGE_CACHE_SHIFT, end);
2310 if (err)
2311 retval = err;
2312 }
2313 }
2314 return retval;
2315}