Add lock_page_killable
[deliverable/linux.git] / mm / filemap.c
CommitLineData
1da177e4
LT
1/*
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 */
1da177e4
LT
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/compiler.h>
15#include <linux/fs.h>
c22ce143 16#include <linux/uaccess.h>
1da177e4 17#include <linux/aio.h>
c59ede7b 18#include <linux/capability.h>
1da177e4
LT
19#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>
53253383 28#include <linux/backing-dev.h>
1da177e4
LT
29#include <linux/pagevec.h>
30#include <linux/blkdev.h>
3a424f2d 31#include <linux/backing-dev.h>
1da177e4
LT
32#include <linux/security.h>
33#include <linux/syscalls.h>
44110fe3 34#include <linux/cpuset.h>
2f718ffc 35#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
0f8053a5
NP
36#include "internal.h"
37
1da177e4 38/*
1da177e4
LT
39 * FIXME: remove all knowledge of the buffer layer from the core VM
40 */
41#include <linux/buffer_head.h> /* for generic_osync_inode */
42
1da177e4
LT
43#include <asm/mman.h>
44
5ce7852c
AB
45static ssize_t
46generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
47 loff_t offset, unsigned long nr_segs);
48
1da177e4
LT
49/*
50 * Shared mappings implemented 30.11.1994. It's not fully working yet,
51 * though.
52 *
53 * Shared mappings now work. 15.8.1995 Bruno.
54 *
55 * finished 'unifying' the page and buffer cache and SMP-threaded the
56 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
57 *
58 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
59 */
60
61/*
62 * Lock ordering:
63 *
64 * ->i_mmap_lock (vmtruncate)
65 * ->private_lock (__free_pte->__set_page_dirty_buffers)
5d337b91
HD
66 * ->swap_lock (exclusive_swap_page, others)
67 * ->mapping->tree_lock
7a405079 68 * ->zone.lock
1da177e4 69 *
1b1dcc1b 70 * ->i_mutex
1da177e4
LT
71 * ->i_mmap_lock (truncate->unmap_mapping_range)
72 *
73 * ->mmap_sem
74 * ->i_mmap_lock
b8072f09 75 * ->page_table_lock or pte_lock (various, mainly in memory.c)
1da177e4
LT
76 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
77 *
78 * ->mmap_sem
79 * ->lock_page (access_process_vm)
80 *
82591e6e
NP
81 * ->i_mutex (generic_file_buffered_write)
82 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
1da177e4 83 *
1b1dcc1b 84 * ->i_mutex
1da177e4
LT
85 * ->i_alloc_sem (various)
86 *
87 * ->inode_lock
88 * ->sb_lock (fs/fs-writeback.c)
89 * ->mapping->tree_lock (__sync_single_inode)
90 *
91 * ->i_mmap_lock
92 * ->anon_vma.lock (vma_adjust)
93 *
94 * ->anon_vma.lock
b8072f09 95 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
1da177e4 96 *
b8072f09 97 * ->page_table_lock or pte_lock
5d337b91 98 * ->swap_lock (try_to_unmap_one)
1da177e4
LT
99 * ->private_lock (try_to_unmap_one)
100 * ->tree_lock (try_to_unmap_one)
101 * ->zone.lru_lock (follow_page->mark_page_accessed)
053837fc 102 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
1da177e4
LT
103 * ->private_lock (page_remove_rmap->set_page_dirty)
104 * ->tree_lock (page_remove_rmap->set_page_dirty)
105 * ->inode_lock (page_remove_rmap->set_page_dirty)
106 * ->inode_lock (zap_pte_range->set_page_dirty)
107 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 *
109 * ->task->proc_lock
110 * ->dcache_lock (proc_pid_lookup)
111 */
112
113/*
114 * Remove a page from the page cache and free it. Caller has to make
115 * sure the page is locked and that nobody else uses it - or that usage
116 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
117 */
118void __remove_from_page_cache(struct page *page)
119{
120 struct address_space *mapping = page->mapping;
121
122 radix_tree_delete(&mapping->page_tree, page->index);
123 page->mapping = NULL;
124 mapping->nrpages--;
347ce434 125 __dec_zone_page_state(page, NR_FILE_PAGES);
45426812 126 BUG_ON(page_mapped(page));
1da177e4
LT
127}
128
129void remove_from_page_cache(struct page *page)
130{
131 struct address_space *mapping = page->mapping;
132
cd7619d6 133 BUG_ON(!PageLocked(page));
1da177e4
LT
134
135 write_lock_irq(&mapping->tree_lock);
136 __remove_from_page_cache(page);
137 write_unlock_irq(&mapping->tree_lock);
138}
139
140static int sync_page(void *word)
141{
142 struct address_space *mapping;
143 struct page *page;
144
07808b74 145 page = container_of((unsigned long *)word, struct page, flags);
1da177e4
LT
146
147 /*
dd1d5afc
WLII
148 * page_mapping() is being called without PG_locked held.
149 * Some knowledge of the state and use of the page is used to
150 * reduce the requirements down to a memory barrier.
151 * The danger here is of a stale page_mapping() return value
152 * indicating a struct address_space different from the one it's
153 * associated with when it is associated with one.
154 * After smp_mb(), it's either the correct page_mapping() for
155 * the page, or an old page_mapping() and the page's own
156 * page_mapping() has gone NULL.
157 * The ->sync_page() address_space operation must tolerate
158 * page_mapping() going NULL. By an amazing coincidence,
159 * this comes about because none of the users of the page
160 * in the ->sync_page() methods make essential use of the
161 * page_mapping(), merely passing the page down to the backing
162 * device's unplug functions when it's non-NULL, which in turn
4c21e2f2 163 * ignore it for all cases but swap, where only page_private(page) is
dd1d5afc
WLII
164 * of interest. When page_mapping() does go NULL, the entire
165 * call stack gracefully ignores the page and returns.
166 * -- wli
1da177e4
LT
167 */
168 smp_mb();
169 mapping = page_mapping(page);
170 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
171 mapping->a_ops->sync_page(page);
172 io_schedule();
173 return 0;
174}
175
2687a356
MW
176static int sync_page_killable(void *word)
177{
178 sync_page(word);
179 return fatal_signal_pending(current) ? -EINTR : 0;
180}
181
1da177e4 182/**
485bb99b 183 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
67be2dd1
MW
184 * @mapping: address space structure to write
185 * @start: offset in bytes where the range starts
469eb4d0 186 * @end: offset in bytes where the range ends (inclusive)
67be2dd1 187 * @sync_mode: enable synchronous operation
1da177e4 188 *
485bb99b
RD
189 * Start writeback against all of a mapping's dirty pages that lie
190 * within the byte offsets <start, end> inclusive.
191 *
1da177e4 192 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
485bb99b 193 * opposed to a regular memory cleansing writeback. The difference between
1da177e4
LT
194 * these two operations is that if a dirty page/buffer is encountered, it must
195 * be waited upon, and not just skipped over.
196 */
ebcf28e1
AM
197int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
198 loff_t end, int sync_mode)
1da177e4
LT
199{
200 int ret;
201 struct writeback_control wbc = {
202 .sync_mode = sync_mode,
203 .nr_to_write = mapping->nrpages * 2,
111ebb6e
OH
204 .range_start = start,
205 .range_end = end,
1da177e4
LT
206 };
207
208 if (!mapping_cap_writeback_dirty(mapping))
209 return 0;
210
211 ret = do_writepages(mapping, &wbc);
212 return ret;
213}
214
215static inline int __filemap_fdatawrite(struct address_space *mapping,
216 int sync_mode)
217{
111ebb6e 218 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
1da177e4
LT
219}
220
221int filemap_fdatawrite(struct address_space *mapping)
222{
223 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
224}
225EXPORT_SYMBOL(filemap_fdatawrite);
226
ebcf28e1
AM
227static int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
228 loff_t end)
1da177e4
LT
229{
230 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
231}
232
485bb99b
RD
233/**
234 * filemap_flush - mostly a non-blocking flush
235 * @mapping: target address_space
236 *
1da177e4
LT
237 * This is a mostly non-blocking flush. Not suitable for data-integrity
238 * purposes - I/O may not be started against all dirty pages.
239 */
240int filemap_flush(struct address_space *mapping)
241{
242 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
243}
244EXPORT_SYMBOL(filemap_flush);
245
485bb99b
RD
246/**
247 * wait_on_page_writeback_range - wait for writeback to complete
248 * @mapping: target address_space
249 * @start: beginning page index
250 * @end: ending page index
251 *
1da177e4
LT
252 * Wait for writeback to complete against pages indexed by start->end
253 * inclusive
254 */
ebcf28e1 255int wait_on_page_writeback_range(struct address_space *mapping,
1da177e4
LT
256 pgoff_t start, pgoff_t end)
257{
258 struct pagevec pvec;
259 int nr_pages;
260 int ret = 0;
261 pgoff_t index;
262
263 if (end < start)
264 return 0;
265
266 pagevec_init(&pvec, 0);
267 index = start;
268 while ((index <= end) &&
269 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
270 PAGECACHE_TAG_WRITEBACK,
271 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
272 unsigned i;
273
274 for (i = 0; i < nr_pages; i++) {
275 struct page *page = pvec.pages[i];
276
277 /* until radix tree lookup accepts end_index */
278 if (page->index > end)
279 continue;
280
281 wait_on_page_writeback(page);
282 if (PageError(page))
283 ret = -EIO;
284 }
285 pagevec_release(&pvec);
286 cond_resched();
287 }
288
289 /* Check for outstanding write errors */
290 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
291 ret = -ENOSPC;
292 if (test_and_clear_bit(AS_EIO, &mapping->flags))
293 ret = -EIO;
294
295 return ret;
296}
297
485bb99b
RD
298/**
299 * sync_page_range - write and wait on all pages in the passed range
300 * @inode: target inode
301 * @mapping: target address_space
302 * @pos: beginning offset in pages to write
303 * @count: number of bytes to write
304 *
1da177e4
LT
305 * Write and wait upon all the pages in the passed range. This is a "data
306 * integrity" operation. It waits upon in-flight writeout before starting and
307 * waiting upon new writeout. If there was an IO error, return it.
308 *
1b1dcc1b 309 * We need to re-take i_mutex during the generic_osync_inode list walk because
1da177e4
LT
310 * it is otherwise livelockable.
311 */
312int sync_page_range(struct inode *inode, struct address_space *mapping,
268fc16e 313 loff_t pos, loff_t count)
1da177e4
LT
314{
315 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
316 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
317 int ret;
318
319 if (!mapping_cap_writeback_dirty(mapping) || !count)
320 return 0;
321 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
322 if (ret == 0) {
1b1dcc1b 323 mutex_lock(&inode->i_mutex);
1da177e4 324 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
1b1dcc1b 325 mutex_unlock(&inode->i_mutex);
1da177e4
LT
326 }
327 if (ret == 0)
328 ret = wait_on_page_writeback_range(mapping, start, end);
329 return ret;
330}
331EXPORT_SYMBOL(sync_page_range);
332
485bb99b
RD
333/**
334 * sync_page_range_nolock
335 * @inode: target inode
336 * @mapping: target address_space
337 * @pos: beginning offset in pages to write
338 * @count: number of bytes to write
339 *
72fd4a35 340 * Note: Holding i_mutex across sync_page_range_nolock() is not a good idea
1da177e4
LT
341 * as it forces O_SYNC writers to different parts of the same file
342 * to be serialised right until io completion.
343 */
268fc16e
OH
344int sync_page_range_nolock(struct inode *inode, struct address_space *mapping,
345 loff_t pos, loff_t count)
1da177e4
LT
346{
347 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
348 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
349 int ret;
350
351 if (!mapping_cap_writeback_dirty(mapping) || !count)
352 return 0;
353 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
354 if (ret == 0)
355 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
356 if (ret == 0)
357 ret = wait_on_page_writeback_range(mapping, start, end);
358 return ret;
359}
268fc16e 360EXPORT_SYMBOL(sync_page_range_nolock);
1da177e4
LT
361
362/**
485bb99b 363 * filemap_fdatawait - wait for all under-writeback pages to complete
1da177e4 364 * @mapping: address space structure to wait for
485bb99b
RD
365 *
366 * Walk the list of under-writeback pages of the given address space
367 * and wait for all of them.
1da177e4
LT
368 */
369int filemap_fdatawait(struct address_space *mapping)
370{
371 loff_t i_size = i_size_read(mapping->host);
372
373 if (i_size == 0)
374 return 0;
375
376 return wait_on_page_writeback_range(mapping, 0,
377 (i_size - 1) >> PAGE_CACHE_SHIFT);
378}
379EXPORT_SYMBOL(filemap_fdatawait);
380
381int filemap_write_and_wait(struct address_space *mapping)
382{
28fd1298 383 int err = 0;
1da177e4
LT
384
385 if (mapping->nrpages) {
28fd1298
OH
386 err = filemap_fdatawrite(mapping);
387 /*
388 * Even if the above returned error, the pages may be
389 * written partially (e.g. -ENOSPC), so we wait for it.
390 * But the -EIO is special case, it may indicate the worst
391 * thing (e.g. bug) happened, so we avoid waiting for it.
392 */
393 if (err != -EIO) {
394 int err2 = filemap_fdatawait(mapping);
395 if (!err)
396 err = err2;
397 }
1da177e4 398 }
28fd1298 399 return err;
1da177e4 400}
28fd1298 401EXPORT_SYMBOL(filemap_write_and_wait);
1da177e4 402
485bb99b
RD
403/**
404 * filemap_write_and_wait_range - write out & wait on a file range
405 * @mapping: the address_space for the pages
406 * @lstart: offset in bytes where the range starts
407 * @lend: offset in bytes where the range ends (inclusive)
408 *
469eb4d0
AM
409 * Write out and wait upon file offsets lstart->lend, inclusive.
410 *
411 * Note that `lend' is inclusive (describes the last byte to be written) so
412 * that this function can be used to write to the very end-of-file (end = -1).
413 */
1da177e4
LT
414int filemap_write_and_wait_range(struct address_space *mapping,
415 loff_t lstart, loff_t lend)
416{
28fd1298 417 int err = 0;
1da177e4
LT
418
419 if (mapping->nrpages) {
28fd1298
OH
420 err = __filemap_fdatawrite_range(mapping, lstart, lend,
421 WB_SYNC_ALL);
422 /* See comment of filemap_write_and_wait() */
423 if (err != -EIO) {
424 int err2 = wait_on_page_writeback_range(mapping,
425 lstart >> PAGE_CACHE_SHIFT,
426 lend >> PAGE_CACHE_SHIFT);
427 if (!err)
428 err = err2;
429 }
1da177e4 430 }
28fd1298 431 return err;
1da177e4
LT
432}
433
485bb99b
RD
434/**
435 * add_to_page_cache - add newly allocated pagecache pages
436 * @page: page to add
437 * @mapping: the page's address_space
438 * @offset: page index
439 * @gfp_mask: page allocation mode
440 *
441 * This function is used to add newly allocated pagecache pages;
1da177e4
LT
442 * the page is new, so we can just run SetPageLocked() against it.
443 * The other page state flags were set by rmqueue().
444 *
445 * This function does not add the page to the LRU. The caller must do that.
446 */
447int add_to_page_cache(struct page *page, struct address_space *mapping,
6daa0e28 448 pgoff_t offset, gfp_t gfp_mask)
1da177e4
LT
449{
450 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
451
452 if (error == 0) {
453 write_lock_irq(&mapping->tree_lock);
454 error = radix_tree_insert(&mapping->page_tree, offset, page);
455 if (!error) {
456 page_cache_get(page);
457 SetPageLocked(page);
458 page->mapping = mapping;
459 page->index = offset;
460 mapping->nrpages++;
347ce434 461 __inc_zone_page_state(page, NR_FILE_PAGES);
1da177e4
LT
462 }
463 write_unlock_irq(&mapping->tree_lock);
464 radix_tree_preload_end();
465 }
466 return error;
467}
1da177e4
LT
468EXPORT_SYMBOL(add_to_page_cache);
469
470int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
6daa0e28 471 pgoff_t offset, gfp_t gfp_mask)
1da177e4
LT
472{
473 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
474 if (ret == 0)
475 lru_cache_add(page);
476 return ret;
477}
478
44110fe3 479#ifdef CONFIG_NUMA
2ae88149 480struct page *__page_cache_alloc(gfp_t gfp)
44110fe3
PJ
481{
482 if (cpuset_do_page_mem_spread()) {
483 int n = cpuset_mem_spread_node();
2ae88149 484 return alloc_pages_node(n, gfp, 0);
44110fe3 485 }
2ae88149 486 return alloc_pages(gfp, 0);
44110fe3 487}
2ae88149 488EXPORT_SYMBOL(__page_cache_alloc);
44110fe3
PJ
489#endif
490
db37648c
NP
491static int __sleep_on_page_lock(void *word)
492{
493 io_schedule();
494 return 0;
495}
496
1da177e4
LT
497/*
498 * In order to wait for pages to become available there must be
499 * waitqueues associated with pages. By using a hash table of
500 * waitqueues where the bucket discipline is to maintain all
501 * waiters on the same queue and wake all when any of the pages
502 * become available, and for the woken contexts to check to be
503 * sure the appropriate page became available, this saves space
504 * at a cost of "thundering herd" phenomena during rare hash
505 * collisions.
506 */
507static wait_queue_head_t *page_waitqueue(struct page *page)
508{
509 const struct zone *zone = page_zone(page);
510
511 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
512}
513
514static inline void wake_up_page(struct page *page, int bit)
515{
516 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
517}
518
519void fastcall wait_on_page_bit(struct page *page, int bit_nr)
520{
521 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
522
523 if (test_bit(bit_nr, &page->flags))
524 __wait_on_bit(page_waitqueue(page), &wait, sync_page,
525 TASK_UNINTERRUPTIBLE);
526}
527EXPORT_SYMBOL(wait_on_page_bit);
528
529/**
485bb99b 530 * unlock_page - unlock a locked page
1da177e4
LT
531 * @page: the page
532 *
533 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
534 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
535 * mechananism between PageLocked pages and PageWriteback pages is shared.
536 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
537 *
538 * The first mb is necessary to safely close the critical section opened by the
539 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
540 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
541 * parallel wait_on_page_locked()).
542 */
543void fastcall unlock_page(struct page *page)
544{
545 smp_mb__before_clear_bit();
546 if (!TestClearPageLocked(page))
547 BUG();
548 smp_mb__after_clear_bit();
549 wake_up_page(page, PG_locked);
550}
551EXPORT_SYMBOL(unlock_page);
552
485bb99b
RD
553/**
554 * end_page_writeback - end writeback against a page
555 * @page: the page
1da177e4
LT
556 */
557void end_page_writeback(struct page *page)
558{
559 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
560 if (!test_clear_page_writeback(page))
561 BUG();
562 }
563 smp_mb__after_clear_bit();
564 wake_up_page(page, PG_writeback);
565}
566EXPORT_SYMBOL(end_page_writeback);
567
485bb99b
RD
568/**
569 * __lock_page - get a lock on the page, assuming we need to sleep to get it
570 * @page: the page to lock
1da177e4 571 *
485bb99b 572 * Ugly. Running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
1da177e4
LT
573 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
574 * chances are that on the second loop, the block layer's plug list is empty,
575 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
576 */
577void fastcall __lock_page(struct page *page)
578{
579 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
580
581 __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
582 TASK_UNINTERRUPTIBLE);
583}
584EXPORT_SYMBOL(__lock_page);
585
2687a356
MW
586int fastcall __lock_page_killable(struct page *page)
587{
588 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
589
590 return __wait_on_bit_lock(page_waitqueue(page), &wait,
591 sync_page_killable, TASK_KILLABLE);
592}
593
db37648c
NP
594/*
595 * Variant of lock_page that does not require the caller to hold a reference
596 * on the page's mapping.
597 */
598void fastcall __lock_page_nosync(struct page *page)
599{
600 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
601 __wait_on_bit_lock(page_waitqueue(page), &wait, __sleep_on_page_lock,
602 TASK_UNINTERRUPTIBLE);
603}
604
485bb99b
RD
605/**
606 * find_get_page - find and get a page reference
607 * @mapping: the address_space to search
608 * @offset: the page index
609 *
da6052f7
NP
610 * Is there a pagecache struct page at the given (mapping, offset) tuple?
611 * If yes, increment its refcount and return it; if no, return NULL.
1da177e4 612 */
57f6b96c 613struct page * find_get_page(struct address_space *mapping, pgoff_t offset)
1da177e4
LT
614{
615 struct page *page;
616
617 read_lock_irq(&mapping->tree_lock);
618 page = radix_tree_lookup(&mapping->page_tree, offset);
619 if (page)
620 page_cache_get(page);
621 read_unlock_irq(&mapping->tree_lock);
622 return page;
623}
1da177e4
LT
624EXPORT_SYMBOL(find_get_page);
625
1da177e4
LT
626/**
627 * find_lock_page - locate, pin and lock a pagecache page
67be2dd1
MW
628 * @mapping: the address_space to search
629 * @offset: the page index
1da177e4
LT
630 *
631 * Locates the desired pagecache page, locks it, increments its reference
632 * count and returns its address.
633 *
634 * Returns zero if the page was not present. find_lock_page() may sleep.
635 */
636struct page *find_lock_page(struct address_space *mapping,
57f6b96c 637 pgoff_t offset)
1da177e4
LT
638{
639 struct page *page;
640
1da177e4 641repeat:
45726cb4 642 read_lock_irq(&mapping->tree_lock);
1da177e4
LT
643 page = radix_tree_lookup(&mapping->page_tree, offset);
644 if (page) {
645 page_cache_get(page);
646 if (TestSetPageLocked(page)) {
647 read_unlock_irq(&mapping->tree_lock);
bbfbb7ce 648 __lock_page(page);
1da177e4
LT
649
650 /* Has the page been truncated while we slept? */
45726cb4 651 if (unlikely(page->mapping != mapping)) {
1da177e4
LT
652 unlock_page(page);
653 page_cache_release(page);
654 goto repeat;
655 }
45726cb4
NP
656 VM_BUG_ON(page->index != offset);
657 goto out;
1da177e4
LT
658 }
659 }
660 read_unlock_irq(&mapping->tree_lock);
45726cb4 661out:
1da177e4
LT
662 return page;
663}
1da177e4
LT
664EXPORT_SYMBOL(find_lock_page);
665
666/**
667 * find_or_create_page - locate or add a pagecache page
67be2dd1
MW
668 * @mapping: the page's address_space
669 * @index: the page's index into the mapping
670 * @gfp_mask: page allocation mode
1da177e4
LT
671 *
672 * Locates a page in the pagecache. If the page is not present, a new page
673 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
674 * LRU list. The returned page is locked and has its reference count
675 * incremented.
676 *
677 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
678 * allocation!
679 *
680 * find_or_create_page() returns the desired page's address, or zero on
681 * memory exhaustion.
682 */
683struct page *find_or_create_page(struct address_space *mapping,
57f6b96c 684 pgoff_t index, gfp_t gfp_mask)
1da177e4 685{
eb2be189 686 struct page *page;
1da177e4
LT
687 int err;
688repeat:
689 page = find_lock_page(mapping, index);
690 if (!page) {
eb2be189
NP
691 page = __page_cache_alloc(gfp_mask);
692 if (!page)
693 return NULL;
694 err = add_to_page_cache_lru(page, mapping, index, gfp_mask);
695 if (unlikely(err)) {
696 page_cache_release(page);
697 page = NULL;
698 if (err == -EEXIST)
699 goto repeat;
1da177e4 700 }
1da177e4 701 }
1da177e4
LT
702 return page;
703}
1da177e4
LT
704EXPORT_SYMBOL(find_or_create_page);
705
706/**
707 * find_get_pages - gang pagecache lookup
708 * @mapping: The address_space to search
709 * @start: The starting page index
710 * @nr_pages: The maximum number of pages
711 * @pages: Where the resulting pages are placed
712 *
713 * find_get_pages() will search for and return a group of up to
714 * @nr_pages pages in the mapping. The pages are placed at @pages.
715 * find_get_pages() takes a reference against the returned pages.
716 *
717 * The search returns a group of mapping-contiguous pages with ascending
718 * indexes. There may be holes in the indices due to not-present pages.
719 *
720 * find_get_pages() returns the number of pages which were found.
721 */
722unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
723 unsigned int nr_pages, struct page **pages)
724{
725 unsigned int i;
726 unsigned int ret;
727
728 read_lock_irq(&mapping->tree_lock);
729 ret = radix_tree_gang_lookup(&mapping->page_tree,
730 (void **)pages, start, nr_pages);
731 for (i = 0; i < ret; i++)
732 page_cache_get(pages[i]);
733 read_unlock_irq(&mapping->tree_lock);
734 return ret;
735}
736
ebf43500
JA
737/**
738 * find_get_pages_contig - gang contiguous pagecache lookup
739 * @mapping: The address_space to search
740 * @index: The starting page index
741 * @nr_pages: The maximum number of pages
742 * @pages: Where the resulting pages are placed
743 *
744 * find_get_pages_contig() works exactly like find_get_pages(), except
745 * that the returned number of pages are guaranteed to be contiguous.
746 *
747 * find_get_pages_contig() returns the number of pages which were found.
748 */
749unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
750 unsigned int nr_pages, struct page **pages)
751{
752 unsigned int i;
753 unsigned int ret;
754
755 read_lock_irq(&mapping->tree_lock);
756 ret = radix_tree_gang_lookup(&mapping->page_tree,
757 (void **)pages, index, nr_pages);
758 for (i = 0; i < ret; i++) {
759 if (pages[i]->mapping == NULL || pages[i]->index != index)
760 break;
761
762 page_cache_get(pages[i]);
763 index++;
764 }
765 read_unlock_irq(&mapping->tree_lock);
766 return i;
767}
ef71c15c 768EXPORT_SYMBOL(find_get_pages_contig);
ebf43500 769
485bb99b
RD
770/**
771 * find_get_pages_tag - find and return pages that match @tag
772 * @mapping: the address_space to search
773 * @index: the starting page index
774 * @tag: the tag index
775 * @nr_pages: the maximum number of pages
776 * @pages: where the resulting pages are placed
777 *
1da177e4 778 * Like find_get_pages, except we only return pages which are tagged with
485bb99b 779 * @tag. We update @index to index the next page for the traversal.
1da177e4
LT
780 */
781unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
782 int tag, unsigned int nr_pages, struct page **pages)
783{
784 unsigned int i;
785 unsigned int ret;
786
787 read_lock_irq(&mapping->tree_lock);
788 ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
789 (void **)pages, *index, nr_pages, tag);
790 for (i = 0; i < ret; i++)
791 page_cache_get(pages[i]);
792 if (ret)
793 *index = pages[ret - 1]->index + 1;
794 read_unlock_irq(&mapping->tree_lock);
795 return ret;
796}
ef71c15c 797EXPORT_SYMBOL(find_get_pages_tag);
1da177e4 798
485bb99b
RD
799/**
800 * grab_cache_page_nowait - returns locked page at given index in given cache
801 * @mapping: target address_space
802 * @index: the page index
803 *
72fd4a35 804 * Same as grab_cache_page(), but do not wait if the page is unavailable.
1da177e4
LT
805 * This is intended for speculative data generators, where the data can
806 * be regenerated if the page couldn't be grabbed. This routine should
807 * be safe to call while holding the lock for another page.
808 *
809 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
810 * and deadlock against the caller's locked page.
811 */
812struct page *
57f6b96c 813grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
1da177e4
LT
814{
815 struct page *page = find_get_page(mapping, index);
1da177e4
LT
816
817 if (page) {
818 if (!TestSetPageLocked(page))
819 return page;
820 page_cache_release(page);
821 return NULL;
822 }
2ae88149
NP
823 page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
824 if (page && add_to_page_cache_lru(page, mapping, index, GFP_KERNEL)) {
1da177e4
LT
825 page_cache_release(page);
826 page = NULL;
827 }
828 return page;
829}
1da177e4
LT
830EXPORT_SYMBOL(grab_cache_page_nowait);
831
76d42bd9
WF
832/*
833 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
834 * a _large_ part of the i/o request. Imagine the worst scenario:
835 *
836 * ---R__________________________________________B__________
837 * ^ reading here ^ bad block(assume 4k)
838 *
839 * read(R) => miss => readahead(R...B) => media error => frustrating retries
840 * => failing the whole request => read(R) => read(R+1) =>
841 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
842 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
843 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
844 *
845 * It is going insane. Fix it by quickly scaling down the readahead size.
846 */
847static void shrink_readahead_size_eio(struct file *filp,
848 struct file_ra_state *ra)
849{
850 if (!ra->ra_pages)
851 return;
852
853 ra->ra_pages /= 4;
76d42bd9
WF
854}
855
485bb99b
RD
856/**
857 * do_generic_mapping_read - generic file read routine
858 * @mapping: address_space to be read
8f731f7d 859 * @ra: file's readahead state
485bb99b
RD
860 * @filp: the file to read
861 * @ppos: current file position
862 * @desc: read_descriptor
863 * @actor: read method
864 *
1da177e4 865 * This is a generic file read routine, and uses the
485bb99b 866 * mapping->a_ops->readpage() function for the actual low-level stuff.
1da177e4
LT
867 *
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 *
485bb99b
RD
871 * Note the struct file* is only passed for the use of readpage.
872 * It may be NULL.
1da177e4
LT
873 */
874void do_generic_mapping_read(struct address_space *mapping,
7ff81078 875 struct file_ra_state *ra,
1da177e4
LT
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;
57f6b96c
FW
882 pgoff_t index;
883 pgoff_t last_index;
884 pgoff_t prev_index;
885 unsigned long offset; /* offset into pagecache page */
ec0f1637 886 unsigned int prev_offset;
1da177e4 887 int error;
1da177e4 888
1da177e4 889 index = *ppos >> PAGE_CACHE_SHIFT;
7ff81078
FW
890 prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
891 prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
1da177e4
LT
892 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
893 offset = *ppos & ~PAGE_CACHE_MASK;
894
1da177e4
LT
895 for (;;) {
896 struct page *page;
57f6b96c 897 pgoff_t end_index;
a32ea1e1 898 loff_t isize;
1da177e4
LT
899 unsigned long nr, ret;
900
1da177e4 901 cond_resched();
1da177e4
LT
902find_page:
903 page = find_get_page(mapping, index);
3ea89ee8 904 if (!page) {
cf914a7d 905 page_cache_sync_readahead(mapping,
7ff81078 906 ra, filp,
3ea89ee8
FW
907 index, last_index - index);
908 page = find_get_page(mapping, index);
909 if (unlikely(page == NULL))
910 goto no_cached_page;
911 }
912 if (PageReadahead(page)) {
cf914a7d 913 page_cache_async_readahead(mapping,
7ff81078 914 ra, filp, page,
3ea89ee8 915 index, last_index - index);
1da177e4
LT
916 }
917 if (!PageUptodate(page))
918 goto page_not_up_to_date;
919page_ok:
a32ea1e1
N
920 /*
921 * i_size must be checked after we know the page is Uptodate.
922 *
923 * Checking i_size after the check allows us to calculate
924 * the correct value for "nr", which means the zero-filled
925 * part of the page is not copied back to userspace (unless
926 * another truncate extends the file - this is desired though).
927 */
928
929 isize = i_size_read(inode);
930 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
931 if (unlikely(!isize || index > end_index)) {
932 page_cache_release(page);
933 goto out;
934 }
935
936 /* nr is the maximum number of bytes to copy from this page */
937 nr = PAGE_CACHE_SIZE;
938 if (index == end_index) {
939 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
940 if (nr <= offset) {
941 page_cache_release(page);
942 goto out;
943 }
944 }
945 nr = nr - offset;
1da177e4
LT
946
947 /* If users can be writing to this page using arbitrary
948 * virtual addresses, take care about potential aliasing
949 * before reading the page on the kernel side.
950 */
951 if (mapping_writably_mapped(mapping))
952 flush_dcache_page(page);
953
954 /*
ec0f1637
JK
955 * When a sequential read accesses a page several times,
956 * only mark it as accessed the first time.
1da177e4 957 */
ec0f1637 958 if (prev_index != index || offset != prev_offset)
1da177e4
LT
959 mark_page_accessed(page);
960 prev_index = index;
961
962 /*
963 * Ok, we have the page, and it's up-to-date, so
964 * now we can copy it to user space...
965 *
966 * The actor routine returns how many bytes were actually used..
967 * NOTE! This may not be the same as how much of a user buffer
968 * we filled up (we may be padding etc), so we can only update
969 * "pos" here (the actor routine has to update the user buffer
970 * pointers and the remaining count).
971 */
972 ret = actor(desc, page, offset, nr);
973 offset += ret;
974 index += offset >> PAGE_CACHE_SHIFT;
975 offset &= ~PAGE_CACHE_MASK;
6ce745ed 976 prev_offset = offset;
1da177e4
LT
977
978 page_cache_release(page);
979 if (ret == nr && desc->count)
980 continue;
981 goto out;
982
983page_not_up_to_date:
984 /* Get exclusive access to the page ... */
985 lock_page(page);
986
da6052f7 987 /* Did it get truncated before we got the lock? */
1da177e4
LT
988 if (!page->mapping) {
989 unlock_page(page);
990 page_cache_release(page);
991 continue;
992 }
993
994 /* Did somebody else fill it already? */
995 if (PageUptodate(page)) {
996 unlock_page(page);
997 goto page_ok;
998 }
999
1000readpage:
1001 /* Start the actual read. The read will unlock the page. */
1002 error = mapping->a_ops->readpage(filp, page);
1003
994fc28c
ZB
1004 if (unlikely(error)) {
1005 if (error == AOP_TRUNCATED_PAGE) {
1006 page_cache_release(page);
1007 goto find_page;
1008 }
1da177e4 1009 goto readpage_error;
994fc28c 1010 }
1da177e4
LT
1011
1012 if (!PageUptodate(page)) {
1013 lock_page(page);
1014 if (!PageUptodate(page)) {
1015 if (page->mapping == NULL) {
1016 /*
1017 * invalidate_inode_pages got it
1018 */
1019 unlock_page(page);
1020 page_cache_release(page);
1021 goto find_page;
1022 }
1023 unlock_page(page);
1024 error = -EIO;
7ff81078 1025 shrink_readahead_size_eio(filp, ra);
1da177e4
LT
1026 goto readpage_error;
1027 }
1028 unlock_page(page);
1029 }
1030
1da177e4
LT
1031 goto page_ok;
1032
1033readpage_error:
1034 /* UHHUH! A synchronous read error occurred. Report it */
1035 desc->error = error;
1036 page_cache_release(page);
1037 goto out;
1038
1039no_cached_page:
1040 /*
1041 * Ok, it wasn't cached, so we need to create a new
1042 * page..
1043 */
eb2be189
NP
1044 page = page_cache_alloc_cold(mapping);
1045 if (!page) {
1046 desc->error = -ENOMEM;
1047 goto out;
1da177e4 1048 }
eb2be189 1049 error = add_to_page_cache_lru(page, mapping,
1da177e4
LT
1050 index, GFP_KERNEL);
1051 if (error) {
eb2be189 1052 page_cache_release(page);
1da177e4
LT
1053 if (error == -EEXIST)
1054 goto find_page;
1055 desc->error = error;
1056 goto out;
1057 }
1da177e4
LT
1058 goto readpage;
1059 }
1060
1061out:
7ff81078
FW
1062 ra->prev_pos = prev_index;
1063 ra->prev_pos <<= PAGE_CACHE_SHIFT;
1064 ra->prev_pos |= prev_offset;
1da177e4 1065
f4e6b498 1066 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
1da177e4
LT
1067 if (filp)
1068 file_accessed(filp);
1069}
1da177e4
LT
1070EXPORT_SYMBOL(do_generic_mapping_read);
1071
1072int file_read_actor(read_descriptor_t *desc, struct page *page,
1073 unsigned long offset, unsigned long size)
1074{
1075 char *kaddr;
1076 unsigned long left, count = desc->count;
1077
1078 if (size > count)
1079 size = count;
1080
1081 /*
1082 * Faults on the destination of a read are common, so do it before
1083 * taking the kmap.
1084 */
1085 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1086 kaddr = kmap_atomic(page, KM_USER0);
1087 left = __copy_to_user_inatomic(desc->arg.buf,
1088 kaddr + offset, size);
1089 kunmap_atomic(kaddr, KM_USER0);
1090 if (left == 0)
1091 goto success;
1092 }
1093
1094 /* Do it the slow way */
1095 kaddr = kmap(page);
1096 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1097 kunmap(page);
1098
1099 if (left) {
1100 size -= left;
1101 desc->error = -EFAULT;
1102 }
1103success:
1104 desc->count = count - size;
1105 desc->written += size;
1106 desc->arg.buf += size;
1107 return size;
1108}
1109
0ceb3314
DM
1110/*
1111 * Performs necessary checks before doing a write
1112 * @iov: io vector request
1113 * @nr_segs: number of segments in the iovec
1114 * @count: number of bytes to write
1115 * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1116 *
1117 * Adjust number of segments and amount of bytes to write (nr_segs should be
1118 * properly initialized first). Returns appropriate error code that caller
1119 * should return or zero in case that write should be allowed.
1120 */
1121int generic_segment_checks(const struct iovec *iov,
1122 unsigned long *nr_segs, size_t *count, int access_flags)
1123{
1124 unsigned long seg;
1125 size_t cnt = 0;
1126 for (seg = 0; seg < *nr_segs; seg++) {
1127 const struct iovec *iv = &iov[seg];
1128
1129 /*
1130 * If any segment has a negative length, or the cumulative
1131 * length ever wraps negative then return -EINVAL.
1132 */
1133 cnt += iv->iov_len;
1134 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1135 return -EINVAL;
1136 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1137 continue;
1138 if (seg == 0)
1139 return -EFAULT;
1140 *nr_segs = seg;
1141 cnt -= iv->iov_len; /* This segment is no good */
1142 break;
1143 }
1144 *count = cnt;
1145 return 0;
1146}
1147EXPORT_SYMBOL(generic_segment_checks);
1148
485bb99b 1149/**
b2abacf3 1150 * generic_file_aio_read - generic filesystem read routine
485bb99b
RD
1151 * @iocb: kernel I/O control block
1152 * @iov: io vector request
1153 * @nr_segs: number of segments in the iovec
b2abacf3 1154 * @pos: current file position
485bb99b 1155 *
1da177e4
LT
1156 * This is the "read()" routine for all filesystems
1157 * that can use the page cache directly.
1158 */
1159ssize_t
543ade1f
BP
1160generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1161 unsigned long nr_segs, loff_t pos)
1da177e4
LT
1162{
1163 struct file *filp = iocb->ki_filp;
1164 ssize_t retval;
1165 unsigned long seg;
1166 size_t count;
543ade1f 1167 loff_t *ppos = &iocb->ki_pos;
1da177e4
LT
1168
1169 count = 0;
0ceb3314
DM
1170 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1171 if (retval)
1172 return retval;
1da177e4
LT
1173
1174 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1175 if (filp->f_flags & O_DIRECT) {
543ade1f 1176 loff_t size;
1da177e4
LT
1177 struct address_space *mapping;
1178 struct inode *inode;
1179
1180 mapping = filp->f_mapping;
1181 inode = mapping->host;
1182 retval = 0;
1183 if (!count)
1184 goto out; /* skip atime */
1185 size = i_size_read(inode);
1186 if (pos < size) {
1187 retval = generic_file_direct_IO(READ, iocb,
1188 iov, pos, nr_segs);
1da177e4
LT
1189 if (retval > 0)
1190 *ppos = pos + retval;
1191 }
0e0bcae3 1192 if (likely(retval != 0)) {
3f1a9aae 1193 file_accessed(filp);
a9e5f4d0 1194 goto out;
0e0bcae3 1195 }
1da177e4
LT
1196 }
1197
1198 retval = 0;
1199 if (count) {
1200 for (seg = 0; seg < nr_segs; seg++) {
1201 read_descriptor_t desc;
1202
1203 desc.written = 0;
1204 desc.arg.buf = iov[seg].iov_base;
1205 desc.count = iov[seg].iov_len;
1206 if (desc.count == 0)
1207 continue;
1208 desc.error = 0;
1209 do_generic_file_read(filp,ppos,&desc,file_read_actor);
1210 retval += desc.written;
39e88ca2
TH
1211 if (desc.error) {
1212 retval = retval ?: desc.error;
1da177e4
LT
1213 break;
1214 }
c44939ec 1215 if (desc.count > 0)
1216 break;
1da177e4
LT
1217 }
1218 }
1219out:
1220 return retval;
1221}
1da177e4
LT
1222EXPORT_SYMBOL(generic_file_aio_read);
1223
1da177e4
LT
1224static ssize_t
1225do_readahead(struct address_space *mapping, struct file *filp,
57f6b96c 1226 pgoff_t index, unsigned long nr)
1da177e4
LT
1227{
1228 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1229 return -EINVAL;
1230
1231 force_page_cache_readahead(mapping, filp, index,
1232 max_sane_readahead(nr));
1233 return 0;
1234}
1235
1236asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1237{
1238 ssize_t ret;
1239 struct file *file;
1240
1241 ret = -EBADF;
1242 file = fget(fd);
1243 if (file) {
1244 if (file->f_mode & FMODE_READ) {
1245 struct address_space *mapping = file->f_mapping;
57f6b96c
FW
1246 pgoff_t start = offset >> PAGE_CACHE_SHIFT;
1247 pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1da177e4
LT
1248 unsigned long len = end - start + 1;
1249 ret = do_readahead(mapping, file, start, len);
1250 }
1251 fput(file);
1252 }
1253 return ret;
1254}
1255
1256#ifdef CONFIG_MMU
485bb99b
RD
1257/**
1258 * page_cache_read - adds requested page to the page cache if not already there
1259 * @file: file to read
1260 * @offset: page index
1261 *
1da177e4
LT
1262 * This adds the requested page to the page cache if it isn't already there,
1263 * and schedules an I/O to read in its contents from disk.
1264 */
57f6b96c 1265static int fastcall page_cache_read(struct file * file, pgoff_t offset)
1da177e4
LT
1266{
1267 struct address_space *mapping = file->f_mapping;
1268 struct page *page;
994fc28c 1269 int ret;
1da177e4 1270
994fc28c
ZB
1271 do {
1272 page = page_cache_alloc_cold(mapping);
1273 if (!page)
1274 return -ENOMEM;
1275
1276 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1277 if (ret == 0)
1278 ret = mapping->a_ops->readpage(file, page);
1279 else if (ret == -EEXIST)
1280 ret = 0; /* losing race to add is OK */
1da177e4 1281
1da177e4 1282 page_cache_release(page);
1da177e4 1283
994fc28c
ZB
1284 } while (ret == AOP_TRUNCATED_PAGE);
1285
1286 return ret;
1da177e4
LT
1287}
1288
1289#define MMAP_LOTSAMISS (100)
1290
485bb99b 1291/**
54cb8821 1292 * filemap_fault - read in file data for page fault handling
d0217ac0
NP
1293 * @vma: vma in which the fault was taken
1294 * @vmf: struct vm_fault containing details of the fault
485bb99b 1295 *
54cb8821 1296 * filemap_fault() is invoked via the vma operations vector for a
1da177e4
LT
1297 * mapped memory region to read in file data during a page fault.
1298 *
1299 * The goto's are kind of ugly, but this streamlines the normal case of having
1300 * it in the page cache, and handles the special cases reasonably without
1301 * having a lot of duplicated code.
1302 */
d0217ac0 1303int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4
LT
1304{
1305 int error;
54cb8821 1306 struct file *file = vma->vm_file;
1da177e4
LT
1307 struct address_space *mapping = file->f_mapping;
1308 struct file_ra_state *ra = &file->f_ra;
1309 struct inode *inode = mapping->host;
1310 struct page *page;
54cb8821
NP
1311 unsigned long size;
1312 int did_readaround = 0;
83c54070 1313 int ret = 0;
1da177e4 1314
1da177e4 1315 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
d0217ac0 1316 if (vmf->pgoff >= size)
5307cc1a 1317 return VM_FAULT_SIGBUS;
1da177e4
LT
1318
1319 /* If we don't want any read-ahead, don't bother */
54cb8821 1320 if (VM_RandomReadHint(vma))
1da177e4
LT
1321 goto no_cached_page;
1322
1da177e4
LT
1323 /*
1324 * Do we have something in the page cache already?
1325 */
1326retry_find:
d0217ac0 1327 page = find_lock_page(mapping, vmf->pgoff);
3ea89ee8
FW
1328 /*
1329 * For sequential accesses, we use the generic readahead logic.
1330 */
1331 if (VM_SequentialReadHint(vma)) {
1332 if (!page) {
cf914a7d 1333 page_cache_sync_readahead(mapping, ra, file,
3ea89ee8
FW
1334 vmf->pgoff, 1);
1335 page = find_lock_page(mapping, vmf->pgoff);
1336 if (!page)
1337 goto no_cached_page;
1338 }
1339 if (PageReadahead(page)) {
cf914a7d 1340 page_cache_async_readahead(mapping, ra, file, page,
3ea89ee8
FW
1341 vmf->pgoff, 1);
1342 }
1343 }
1344
1da177e4
LT
1345 if (!page) {
1346 unsigned long ra_pages;
1347
1da177e4
LT
1348 ra->mmap_miss++;
1349
1350 /*
1351 * Do we miss much more than hit in this file? If so,
1352 * stop bothering with read-ahead. It will only hurt.
1353 */
0bb7ba6b 1354 if (ra->mmap_miss > MMAP_LOTSAMISS)
1da177e4
LT
1355 goto no_cached_page;
1356
1357 /*
1358 * To keep the pgmajfault counter straight, we need to
1359 * check did_readaround, as this is an inner loop.
1360 */
1361 if (!did_readaround) {
d0217ac0 1362 ret = VM_FAULT_MAJOR;
f8891e5e 1363 count_vm_event(PGMAJFAULT);
1da177e4
LT
1364 }
1365 did_readaround = 1;
1366 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1367 if (ra_pages) {
1368 pgoff_t start = 0;
1369
d0217ac0
NP
1370 if (vmf->pgoff > ra_pages / 2)
1371 start = vmf->pgoff - ra_pages / 2;
1da177e4
LT
1372 do_page_cache_readahead(mapping, file, start, ra_pages);
1373 }
d0217ac0 1374 page = find_lock_page(mapping, vmf->pgoff);
1da177e4
LT
1375 if (!page)
1376 goto no_cached_page;
1377 }
1378
1379 if (!did_readaround)
0bb7ba6b 1380 ra->mmap_miss--;
1da177e4
LT
1381
1382 /*
d00806b1
NP
1383 * We have a locked page in the page cache, now we need to check
1384 * that it's up-to-date. If not, it is going to be due to an error.
1da177e4 1385 */
d00806b1 1386 if (unlikely(!PageUptodate(page)))
1da177e4
LT
1387 goto page_not_uptodate;
1388
d00806b1
NP
1389 /* Must recheck i_size under page lock */
1390 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
d0217ac0 1391 if (unlikely(vmf->pgoff >= size)) {
d00806b1 1392 unlock_page(page);
745ad48e 1393 page_cache_release(page);
5307cc1a 1394 return VM_FAULT_SIGBUS;
d00806b1
NP
1395 }
1396
1da177e4
LT
1397 /*
1398 * Found the page and have a reference on it.
1399 */
1400 mark_page_accessed(page);
f4e6b498 1401 ra->prev_pos = (loff_t)page->index << PAGE_CACHE_SHIFT;
d0217ac0 1402 vmf->page = page;
83c54070 1403 return ret | VM_FAULT_LOCKED;
1da177e4 1404
1da177e4
LT
1405no_cached_page:
1406 /*
1407 * We're only likely to ever get here if MADV_RANDOM is in
1408 * effect.
1409 */
d0217ac0 1410 error = page_cache_read(file, vmf->pgoff);
1da177e4
LT
1411
1412 /*
1413 * The page we want has now been added to the page cache.
1414 * In the unlikely event that someone removed it in the
1415 * meantime, we'll just come back here and read it again.
1416 */
1417 if (error >= 0)
1418 goto retry_find;
1419
1420 /*
1421 * An error return from page_cache_read can result if the
1422 * system is low on memory, or a problem occurs while trying
1423 * to schedule I/O.
1424 */
1425 if (error == -ENOMEM)
d0217ac0
NP
1426 return VM_FAULT_OOM;
1427 return VM_FAULT_SIGBUS;
1da177e4
LT
1428
1429page_not_uptodate:
d00806b1 1430 /* IO error path */
1da177e4 1431 if (!did_readaround) {
d0217ac0 1432 ret = VM_FAULT_MAJOR;
f8891e5e 1433 count_vm_event(PGMAJFAULT);
1da177e4 1434 }
1da177e4
LT
1435
1436 /*
1437 * Umm, take care of errors if the page isn't up-to-date.
1438 * Try to re-read it _once_. We do this synchronously,
1439 * because there really aren't any performance issues here
1440 * and we need to check for errors.
1441 */
1da177e4 1442 ClearPageError(page);
994fc28c 1443 error = mapping->a_ops->readpage(file, page);
d00806b1
NP
1444 page_cache_release(page);
1445
1446 if (!error || error == AOP_TRUNCATED_PAGE)
994fc28c 1447 goto retry_find;
1da177e4 1448
d00806b1 1449 /* Things didn't work out. Return zero to tell the mm layer so. */
76d42bd9 1450 shrink_readahead_size_eio(file, ra);
d0217ac0 1451 return VM_FAULT_SIGBUS;
54cb8821
NP
1452}
1453EXPORT_SYMBOL(filemap_fault);
1454
1da177e4 1455struct vm_operations_struct generic_file_vm_ops = {
54cb8821 1456 .fault = filemap_fault,
1da177e4
LT
1457};
1458
1459/* This is used for a general mmap of a disk file */
1460
1461int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1462{
1463 struct address_space *mapping = file->f_mapping;
1464
1465 if (!mapping->a_ops->readpage)
1466 return -ENOEXEC;
1467 file_accessed(file);
1468 vma->vm_ops = &generic_file_vm_ops;
d0217ac0 1469 vma->vm_flags |= VM_CAN_NONLINEAR;
1da177e4
LT
1470 return 0;
1471}
1da177e4
LT
1472
1473/*
1474 * This is for filesystems which do not implement ->writepage.
1475 */
1476int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1477{
1478 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1479 return -EINVAL;
1480 return generic_file_mmap(file, vma);
1481}
1482#else
1483int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1484{
1485 return -ENOSYS;
1486}
1487int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1488{
1489 return -ENOSYS;
1490}
1491#endif /* CONFIG_MMU */
1492
1493EXPORT_SYMBOL(generic_file_mmap);
1494EXPORT_SYMBOL(generic_file_readonly_mmap);
1495
6fe6900e 1496static struct page *__read_cache_page(struct address_space *mapping,
57f6b96c 1497 pgoff_t index,
1da177e4
LT
1498 int (*filler)(void *,struct page*),
1499 void *data)
1500{
eb2be189 1501 struct page *page;
1da177e4
LT
1502 int err;
1503repeat:
1504 page = find_get_page(mapping, index);
1505 if (!page) {
eb2be189
NP
1506 page = page_cache_alloc_cold(mapping);
1507 if (!page)
1508 return ERR_PTR(-ENOMEM);
1509 err = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL);
1510 if (unlikely(err)) {
1511 page_cache_release(page);
1512 if (err == -EEXIST)
1513 goto repeat;
1da177e4 1514 /* Presumably ENOMEM for radix tree node */
1da177e4
LT
1515 return ERR_PTR(err);
1516 }
1da177e4
LT
1517 err = filler(data, page);
1518 if (err < 0) {
1519 page_cache_release(page);
1520 page = ERR_PTR(err);
1521 }
1522 }
1da177e4
LT
1523 return page;
1524}
1525
6fe6900e
NP
1526/*
1527 * Same as read_cache_page, but don't wait for page to become unlocked
1528 * after submitting it to the filler.
1da177e4 1529 */
6fe6900e 1530struct page *read_cache_page_async(struct address_space *mapping,
57f6b96c 1531 pgoff_t index,
1da177e4
LT
1532 int (*filler)(void *,struct page*),
1533 void *data)
1534{
1535 struct page *page;
1536 int err;
1537
1538retry:
1539 page = __read_cache_page(mapping, index, filler, data);
1540 if (IS_ERR(page))
c855ff37 1541 return page;
1da177e4
LT
1542 if (PageUptodate(page))
1543 goto out;
1544
1545 lock_page(page);
1546 if (!page->mapping) {
1547 unlock_page(page);
1548 page_cache_release(page);
1549 goto retry;
1550 }
1551 if (PageUptodate(page)) {
1552 unlock_page(page);
1553 goto out;
1554 }
1555 err = filler(data, page);
1556 if (err < 0) {
1557 page_cache_release(page);
c855ff37 1558 return ERR_PTR(err);
1da177e4 1559 }
c855ff37 1560out:
6fe6900e
NP
1561 mark_page_accessed(page);
1562 return page;
1563}
1564EXPORT_SYMBOL(read_cache_page_async);
1565
1566/**
1567 * read_cache_page - read into page cache, fill it if needed
1568 * @mapping: the page's address_space
1569 * @index: the page index
1570 * @filler: function to perform the read
1571 * @data: destination for read data
1572 *
1573 * Read into the page cache. If a page already exists, and PageUptodate() is
1574 * not set, try to fill the page then wait for it to become unlocked.
1575 *
1576 * If the page does not get brought uptodate, return -EIO.
1577 */
1578struct page *read_cache_page(struct address_space *mapping,
57f6b96c 1579 pgoff_t index,
6fe6900e
NP
1580 int (*filler)(void *,struct page*),
1581 void *data)
1582{
1583 struct page *page;
1584
1585 page = read_cache_page_async(mapping, index, filler, data);
1586 if (IS_ERR(page))
1587 goto out;
1588 wait_on_page_locked(page);
1589 if (!PageUptodate(page)) {
1590 page_cache_release(page);
1591 page = ERR_PTR(-EIO);
1592 }
1da177e4
LT
1593 out:
1594 return page;
1595}
1da177e4
LT
1596EXPORT_SYMBOL(read_cache_page);
1597
1da177e4
LT
1598/*
1599 * The logic we want is
1600 *
1601 * if suid or (sgid and xgrp)
1602 * remove privs
1603 */
01de85e0 1604int should_remove_suid(struct dentry *dentry)
1da177e4
LT
1605{
1606 mode_t mode = dentry->d_inode->i_mode;
1607 int kill = 0;
1da177e4
LT
1608
1609 /* suid always must be killed */
1610 if (unlikely(mode & S_ISUID))
1611 kill = ATTR_KILL_SUID;
1612
1613 /*
1614 * sgid without any exec bits is just a mandatory locking mark; leave
1615 * it alone. If some exec bits are set, it's a real sgid; kill it.
1616 */
1617 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1618 kill |= ATTR_KILL_SGID;
1619
01de85e0
JA
1620 if (unlikely(kill && !capable(CAP_FSETID)))
1621 return kill;
1da177e4 1622
01de85e0
JA
1623 return 0;
1624}
d23a147b 1625EXPORT_SYMBOL(should_remove_suid);
01de85e0
JA
1626
1627int __remove_suid(struct dentry *dentry, int kill)
1628{
1629 struct iattr newattrs;
1630
1631 newattrs.ia_valid = ATTR_FORCE | kill;
1632 return notify_change(dentry, &newattrs);
1633}
1634
1635int remove_suid(struct dentry *dentry)
1636{
b5376771
SH
1637 int killsuid = should_remove_suid(dentry);
1638 int killpriv = security_inode_need_killpriv(dentry);
1639 int error = 0;
01de85e0 1640
b5376771
SH
1641 if (killpriv < 0)
1642 return killpriv;
1643 if (killpriv)
1644 error = security_inode_killpriv(dentry);
1645 if (!error && killsuid)
1646 error = __remove_suid(dentry, killsuid);
01de85e0 1647
b5376771 1648 return error;
1da177e4
LT
1649}
1650EXPORT_SYMBOL(remove_suid);
1651
2f718ffc 1652static size_t __iovec_copy_from_user_inatomic(char *vaddr,
1da177e4
LT
1653 const struct iovec *iov, size_t base, size_t bytes)
1654{
1655 size_t copied = 0, left = 0;
1656
1657 while (bytes) {
1658 char __user *buf = iov->iov_base + base;
1659 int copy = min(bytes, iov->iov_len - base);
1660
1661 base = 0;
c22ce143 1662 left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
1da177e4
LT
1663 copied += copy;
1664 bytes -= copy;
1665 vaddr += copy;
1666 iov++;
1667
01408c49 1668 if (unlikely(left))
1da177e4 1669 break;
1da177e4
LT
1670 }
1671 return copied - left;
1672}
1673
2f718ffc
NP
1674/*
1675 * Copy as much as we can into the page and return the number of bytes which
1676 * were sucessfully copied. If a fault is encountered then return the number of
1677 * bytes which were copied.
1678 */
1679size_t iov_iter_copy_from_user_atomic(struct page *page,
1680 struct iov_iter *i, unsigned long offset, size_t bytes)
1681{
1682 char *kaddr;
1683 size_t copied;
1684
1685 BUG_ON(!in_atomic());
1686 kaddr = kmap_atomic(page, KM_USER0);
1687 if (likely(i->nr_segs == 1)) {
1688 int left;
1689 char __user *buf = i->iov->iov_base + i->iov_offset;
1690 left = __copy_from_user_inatomic_nocache(kaddr + offset,
1691 buf, bytes);
1692 copied = bytes - left;
1693 } else {
1694 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
1695 i->iov, i->iov_offset, bytes);
1696 }
1697 kunmap_atomic(kaddr, KM_USER0);
1698
1699 return copied;
1700}
89e10787 1701EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
2f718ffc
NP
1702
1703/*
1704 * This has the same sideeffects and return value as
1705 * iov_iter_copy_from_user_atomic().
1706 * The difference is that it attempts to resolve faults.
1707 * Page must not be locked.
1708 */
1709size_t iov_iter_copy_from_user(struct page *page,
1710 struct iov_iter *i, unsigned long offset, size_t bytes)
1711{
1712 char *kaddr;
1713 size_t copied;
1714
1715 kaddr = kmap(page);
1716 if (likely(i->nr_segs == 1)) {
1717 int left;
1718 char __user *buf = i->iov->iov_base + i->iov_offset;
1719 left = __copy_from_user_nocache(kaddr + offset, buf, bytes);
1720 copied = bytes - left;
1721 } else {
1722 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
1723 i->iov, i->iov_offset, bytes);
1724 }
1725 kunmap(page);
1726 return copied;
1727}
89e10787 1728EXPORT_SYMBOL(iov_iter_copy_from_user);
2f718ffc
NP
1729
1730static void __iov_iter_advance_iov(struct iov_iter *i, size_t bytes)
1731{
1732 if (likely(i->nr_segs == 1)) {
1733 i->iov_offset += bytes;
1734 } else {
1735 const struct iovec *iov = i->iov;
1736 size_t base = i->iov_offset;
1737
1738 while (bytes) {
1739 int copy = min(bytes, iov->iov_len - base);
1740
1741 bytes -= copy;
1742 base += copy;
1743 if (iov->iov_len == base) {
1744 iov++;
1745 base = 0;
1746 }
1747 }
1748 i->iov = iov;
1749 i->iov_offset = base;
1750 }
1751}
1752
1753void iov_iter_advance(struct iov_iter *i, size_t bytes)
1754{
1755 BUG_ON(i->count < bytes);
1756
1757 __iov_iter_advance_iov(i, bytes);
1758 i->count -= bytes;
1759}
89e10787 1760EXPORT_SYMBOL(iov_iter_advance);
2f718ffc 1761
afddba49
NP
1762/*
1763 * Fault in the first iovec of the given iov_iter, to a maximum length
1764 * of bytes. Returns 0 on success, or non-zero if the memory could not be
1765 * accessed (ie. because it is an invalid address).
1766 *
1767 * writev-intensive code may want this to prefault several iovecs -- that
1768 * would be possible (callers must not rely on the fact that _only_ the
1769 * first iovec will be faulted with the current implementation).
1770 */
1771int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
2f718ffc 1772{
2f718ffc 1773 char __user *buf = i->iov->iov_base + i->iov_offset;
afddba49
NP
1774 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
1775 return fault_in_pages_readable(buf, bytes);
2f718ffc 1776}
89e10787 1777EXPORT_SYMBOL(iov_iter_fault_in_readable);
2f718ffc
NP
1778
1779/*
1780 * Return the count of just the current iov_iter segment.
1781 */
1782size_t iov_iter_single_seg_count(struct iov_iter *i)
1783{
1784 const struct iovec *iov = i->iov;
1785 if (i->nr_segs == 1)
1786 return i->count;
1787 else
1788 return min(i->count, iov->iov_len - i->iov_offset);
1789}
89e10787 1790EXPORT_SYMBOL(iov_iter_single_seg_count);
2f718ffc 1791
1da177e4
LT
1792/*
1793 * Performs necessary checks before doing a write
1794 *
485bb99b 1795 * Can adjust writing position or amount of bytes to write.
1da177e4
LT
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
1da177e4
LT
1807 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) {
1da177e4
LT
1829 return -EFBIG;
1830 }
1831 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1832 *count = MAX_NON_LFS - (unsigned long)*pos;
1833 }
1834 }
1835
1836 /*
1837 * Are we about to exceed the fs block limit ?
1838 *
1839 * If we have written data it becomes a short write. If we have
1840 * exceeded without writing data we send a signal and return EFBIG.
1841 * Linus frestrict idea will clean these up nicely..
1842 */
1843 if (likely(!isblk)) {
1844 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1845 if (*count || *pos > inode->i_sb->s_maxbytes) {
1da177e4
LT
1846 return -EFBIG;
1847 }
1848 /* zero-length writes at ->s_maxbytes are OK */
1849 }
1850
1851 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1852 *count = inode->i_sb->s_maxbytes - *pos;
1853 } else {
9361401e 1854#ifdef CONFIG_BLOCK
1da177e4
LT
1855 loff_t isize;
1856 if (bdev_read_only(I_BDEV(inode)))
1857 return -EPERM;
1858 isize = i_size_read(inode);
1859 if (*pos >= isize) {
1860 if (*count || *pos > isize)
1861 return -ENOSPC;
1862 }
1863
1864 if (*pos + *count > isize)
1865 *count = isize - *pos;
9361401e
DH
1866#else
1867 return -EPERM;
1868#endif
1da177e4
LT
1869 }
1870 return 0;
1871}
1872EXPORT_SYMBOL(generic_write_checks);
1873
afddba49
NP
1874int pagecache_write_begin(struct file *file, struct address_space *mapping,
1875 loff_t pos, unsigned len, unsigned flags,
1876 struct page **pagep, void **fsdata)
1877{
1878 const struct address_space_operations *aops = mapping->a_ops;
1879
1880 if (aops->write_begin) {
1881 return aops->write_begin(file, mapping, pos, len, flags,
1882 pagep, fsdata);
1883 } else {
1884 int ret;
1885 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1886 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1887 struct inode *inode = mapping->host;
1888 struct page *page;
1889again:
1890 page = __grab_cache_page(mapping, index);
1891 *pagep = page;
1892 if (!page)
1893 return -ENOMEM;
1894
1895 if (flags & AOP_FLAG_UNINTERRUPTIBLE && !PageUptodate(page)) {
1896 /*
1897 * There is no way to resolve a short write situation
1898 * for a !Uptodate page (except by double copying in
1899 * the caller done by generic_perform_write_2copy).
1900 *
1901 * Instead, we have to bring it uptodate here.
1902 */
1903 ret = aops->readpage(file, page);
1904 page_cache_release(page);
1905 if (ret) {
1906 if (ret == AOP_TRUNCATED_PAGE)
1907 goto again;
1908 return ret;
1909 }
1910 goto again;
1911 }
1912
1913 ret = aops->prepare_write(file, page, offset, offset+len);
1914 if (ret) {
55144768 1915 unlock_page(page);
afddba49
NP
1916 page_cache_release(page);
1917 if (pos + len > inode->i_size)
1918 vmtruncate(inode, inode->i_size);
afddba49
NP
1919 }
1920 return ret;
1921 }
1922}
1923EXPORT_SYMBOL(pagecache_write_begin);
1924
1925int pagecache_write_end(struct file *file, struct address_space *mapping,
1926 loff_t pos, unsigned len, unsigned copied,
1927 struct page *page, void *fsdata)
1928{
1929 const struct address_space_operations *aops = mapping->a_ops;
1930 int ret;
1931
1932 if (aops->write_end) {
1933 mark_page_accessed(page);
1934 ret = aops->write_end(file, mapping, pos, len, copied,
1935 page, fsdata);
1936 } else {
1937 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1938 struct inode *inode = mapping->host;
1939
1940 flush_dcache_page(page);
1941 ret = aops->commit_write(file, page, offset, offset+len);
1942 unlock_page(page);
1943 mark_page_accessed(page);
1944 page_cache_release(page);
afddba49
NP
1945
1946 if (ret < 0) {
1947 if (pos + len > inode->i_size)
1948 vmtruncate(inode, inode->i_size);
1949 } else if (ret > 0)
1950 ret = min_t(size_t, copied, ret);
1951 else
1952 ret = copied;
1953 }
1954
1955 return ret;
1956}
1957EXPORT_SYMBOL(pagecache_write_end);
1958
1da177e4
LT
1959ssize_t
1960generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1961 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1962 size_t count, size_t ocount)
1963{
1964 struct file *file = iocb->ki_filp;
1965 struct address_space *mapping = file->f_mapping;
1966 struct inode *inode = mapping->host;
1967 ssize_t written;
1968
1969 if (count != ocount)
1970 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1971
1972 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1973 if (written > 0) {
1974 loff_t end = pos + written;
1975 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1976 i_size_write(inode, end);
1977 mark_inode_dirty(inode);
1978 }
1979 *ppos = end;
1980 }
1981
1982 /*
1983 * Sync the fs metadata but not the minor inode changes and
1984 * of course not the data as we did direct DMA for the IO.
1b1dcc1b 1985 * i_mutex is held, which protects generic_osync_inode() from
8459d86a 1986 * livelocking. AIO O_DIRECT ops attempt to sync metadata here.
1da177e4 1987 */
8459d86a
ZB
1988 if ((written >= 0 || written == -EIOCBQUEUED) &&
1989 ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
1e8a81c5
HH
1990 int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
1991 if (err < 0)
1992 written = err;
1993 }
1da177e4
LT
1994 return written;
1995}
1996EXPORT_SYMBOL(generic_file_direct_write);
1997
eb2be189
NP
1998/*
1999 * Find or create a page at the given pagecache position. Return the locked
2000 * page. This function is specifically for buffered writes.
2001 */
afddba49 2002struct page *__grab_cache_page(struct address_space *mapping, pgoff_t index)
eb2be189
NP
2003{
2004 int status;
2005 struct page *page;
2006repeat:
2007 page = find_lock_page(mapping, index);
2008 if (likely(page))
2009 return page;
2010
2011 page = page_cache_alloc(mapping);
2012 if (!page)
2013 return NULL;
2014 status = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL);
2015 if (unlikely(status)) {
2016 page_cache_release(page);
2017 if (status == -EEXIST)
2018 goto repeat;
2019 return NULL;
2020 }
2021 return page;
2022}
afddba49 2023EXPORT_SYMBOL(__grab_cache_page);
eb2be189 2024
afddba49
NP
2025static ssize_t generic_perform_write_2copy(struct file *file,
2026 struct iov_iter *i, loff_t pos)
1da177e4 2027{
ae37461c 2028 struct address_space *mapping = file->f_mapping;
f5e54d6e 2029 const struct address_space_operations *a_ops = mapping->a_ops;
afddba49
NP
2030 struct inode *inode = mapping->host;
2031 long status = 0;
2032 ssize_t written = 0;
1da177e4
LT
2033
2034 do {
08291429 2035 struct page *src_page;
eb2be189 2036 struct page *page;
ae37461c
AM
2037 pgoff_t index; /* Pagecache index for current page */
2038 unsigned long offset; /* Offset into pagecache page */
08291429 2039 unsigned long bytes; /* Bytes to write to page */
ae37461c 2040 size_t copied; /* Bytes copied from user */
1da177e4 2041
ae37461c 2042 offset = (pos & (PAGE_CACHE_SIZE - 1));
1da177e4 2043 index = pos >> PAGE_CACHE_SHIFT;
2f718ffc 2044 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
afddba49 2045 iov_iter_count(i));
41cb8ac0 2046
08291429
NP
2047 /*
2048 * a non-NULL src_page indicates that we're doing the
2049 * copy via get_user_pages and kmap.
2050 */
2051 src_page = NULL;
2052
41cb8ac0
NP
2053 /*
2054 * Bring in the user page that we will copy from _first_.
2055 * Otherwise there's a nasty deadlock on copying from the
2056 * same page as we're writing to, without it being marked
2057 * up-to-date.
08291429
NP
2058 *
2059 * Not only is this an optimisation, but it is also required
2060 * to check that the address is actually valid, when atomic
2061 * usercopies are used, below.
41cb8ac0 2062 */
afddba49 2063 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
08291429
NP
2064 status = -EFAULT;
2065 break;
2066 }
eb2be189
NP
2067
2068 page = __grab_cache_page(mapping, index);
1da177e4
LT
2069 if (!page) {
2070 status = -ENOMEM;
2071 break;
2072 }
2073
08291429
NP
2074 /*
2075 * non-uptodate pages cannot cope with short copies, and we
2076 * cannot take a pagefault with the destination page locked.
2077 * So pin the source page to copy it.
2078 */
674b892e 2079 if (!PageUptodate(page) && !segment_eq(get_fs(), KERNEL_DS)) {
08291429
NP
2080 unlock_page(page);
2081
2082 src_page = alloc_page(GFP_KERNEL);
2083 if (!src_page) {
2084 page_cache_release(page);
2085 status = -ENOMEM;
2086 break;
2087 }
2088
2089 /*
2090 * Cannot get_user_pages with a page locked for the
2091 * same reason as we can't take a page fault with a
2092 * page locked (as explained below).
2093 */
afddba49 2094 copied = iov_iter_copy_from_user(src_page, i,
2f718ffc 2095 offset, bytes);
08291429
NP
2096 if (unlikely(copied == 0)) {
2097 status = -EFAULT;
2098 page_cache_release(page);
2099 page_cache_release(src_page);
2100 break;
2101 }
2102 bytes = copied;
2103
2104 lock_page(page);
2105 /*
2106 * Can't handle the page going uptodate here, because
2107 * that means we would use non-atomic usercopies, which
2108 * zero out the tail of the page, which can cause
2109 * zeroes to become transiently visible. We could just
2110 * use a non-zeroing copy, but the APIs aren't too
2111 * consistent.
2112 */
2113 if (unlikely(!page->mapping || PageUptodate(page))) {
2114 unlock_page(page);
2115 page_cache_release(page);
2116 page_cache_release(src_page);
2117 continue;
2118 }
08291429
NP
2119 }
2120
1da177e4 2121 status = a_ops->prepare_write(file, page, offset, offset+bytes);
64649a58
NP
2122 if (unlikely(status))
2123 goto fs_write_aop_error;
994fc28c 2124
08291429
NP
2125 if (!src_page) {
2126 /*
2127 * Must not enter the pagefault handler here, because
2128 * we hold the page lock, so we might recursively
2129 * deadlock on the same lock, or get an ABBA deadlock
2130 * against a different lock, or against the mmap_sem
2131 * (which nests outside the page lock). So increment
2132 * preempt count, and use _atomic usercopies.
2133 *
2134 * The page is uptodate so we are OK to encounter a
2135 * short copy: if unmodified parts of the page are
2136 * marked dirty and written out to disk, it doesn't
2137 * really matter.
2138 */
2139 pagefault_disable();
afddba49 2140 copied = iov_iter_copy_from_user_atomic(page, i,
2f718ffc 2141 offset, bytes);
08291429
NP
2142 pagefault_enable();
2143 } else {
2144 void *src, *dst;
2145 src = kmap_atomic(src_page, KM_USER0);
2146 dst = kmap_atomic(page, KM_USER1);
2147 memcpy(dst + offset, src + offset, bytes);
2148 kunmap_atomic(dst, KM_USER1);
2149 kunmap_atomic(src, KM_USER0);
2150 copied = bytes;
2151 }
1da177e4 2152 flush_dcache_page(page);
4a9e5ef1 2153
1da177e4 2154 status = a_ops->commit_write(file, page, offset, offset+bytes);
55144768 2155 if (unlikely(status < 0))
64649a58 2156 goto fs_write_aop_error;
64649a58 2157 if (unlikely(status > 0)) /* filesystem did partial write */
08291429
NP
2158 copied = min_t(size_t, copied, status);
2159
2160 unlock_page(page);
2161 mark_page_accessed(page);
2162 page_cache_release(page);
2163 if (src_page)
2164 page_cache_release(src_page);
64649a58 2165
afddba49 2166 iov_iter_advance(i, copied);
4a9e5ef1 2167 pos += copied;
afddba49 2168 written += copied;
4a9e5ef1 2169
1da177e4
LT
2170 balance_dirty_pages_ratelimited(mapping);
2171 cond_resched();
64649a58
NP
2172 continue;
2173
2174fs_write_aop_error:
55144768 2175 unlock_page(page);
64649a58 2176 page_cache_release(page);
08291429
NP
2177 if (src_page)
2178 page_cache_release(src_page);
64649a58
NP
2179
2180 /*
2181 * prepare_write() may have instantiated a few blocks
2182 * outside i_size. Trim these off again. Don't need
2183 * i_size_read because we hold i_mutex.
2184 */
2185 if (pos + bytes > inode->i_size)
2186 vmtruncate(inode, inode->i_size);
55144768 2187 break;
afddba49
NP
2188 } while (iov_iter_count(i));
2189
2190 return written ? written : status;
2191}
2192
2193static ssize_t generic_perform_write(struct file *file,
2194 struct iov_iter *i, loff_t pos)
2195{
2196 struct address_space *mapping = file->f_mapping;
2197 const struct address_space_operations *a_ops = mapping->a_ops;
2198 long status = 0;
2199 ssize_t written = 0;
674b892e
NP
2200 unsigned int flags = 0;
2201
2202 /*
2203 * Copies from kernel address space cannot fail (NFSD is a big user).
2204 */
2205 if (segment_eq(get_fs(), KERNEL_DS))
2206 flags |= AOP_FLAG_UNINTERRUPTIBLE;
afddba49
NP
2207
2208 do {
2209 struct page *page;
2210 pgoff_t index; /* Pagecache index for current page */
2211 unsigned long offset; /* Offset into pagecache page */
2212 unsigned long bytes; /* Bytes to write to page */
2213 size_t copied; /* Bytes copied from user */
2214 void *fsdata;
2215
2216 offset = (pos & (PAGE_CACHE_SIZE - 1));
2217 index = pos >> PAGE_CACHE_SHIFT;
2218 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2219 iov_iter_count(i));
2220
2221again:
2222
2223 /*
2224 * Bring in the user page that we will copy from _first_.
2225 * Otherwise there's a nasty deadlock on copying from the
2226 * same page as we're writing to, without it being marked
2227 * up-to-date.
2228 *
2229 * Not only is this an optimisation, but it is also required
2230 * to check that the address is actually valid, when atomic
2231 * usercopies are used, below.
2232 */
2233 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2234 status = -EFAULT;
2235 break;
2236 }
2237
674b892e 2238 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
afddba49
NP
2239 &page, &fsdata);
2240 if (unlikely(status))
2241 break;
2242
2243 pagefault_disable();
2244 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
2245 pagefault_enable();
2246 flush_dcache_page(page);
2247
2248 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2249 page, fsdata);
2250 if (unlikely(status < 0))
2251 break;
2252 copied = status;
2253
2254 cond_resched();
2255
2256 if (unlikely(copied == 0)) {
2257 /*
2258 * If we were unable to copy any data at all, we must
2259 * fall back to a single segment length write.
2260 *
2261 * If we didn't fallback here, we could livelock
2262 * because not all segments in the iov can be copied at
2263 * once without a pagefault.
2264 */
2265 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2266 iov_iter_single_seg_count(i));
2267 goto again;
2268 }
2269 iov_iter_advance(i, copied);
2270 pos += copied;
2271 written += copied;
2272
2273 balance_dirty_pages_ratelimited(mapping);
2274
2275 } while (iov_iter_count(i));
2276
2277 return written ? written : status;
2278}
2279
2280ssize_t
2281generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2282 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2283 size_t count, ssize_t written)
2284{
2285 struct file *file = iocb->ki_filp;
2286 struct address_space *mapping = file->f_mapping;
2287 const struct address_space_operations *a_ops = mapping->a_ops;
2288 struct inode *inode = mapping->host;
2289 ssize_t status;
2290 struct iov_iter i;
2291
2292 iov_iter_init(&i, iov, nr_segs, count, written);
2293 if (a_ops->write_begin)
2294 status = generic_perform_write(file, &i, pos);
2295 else
2296 status = generic_perform_write_2copy(file, &i, pos);
1da177e4 2297
1da177e4 2298 if (likely(status >= 0)) {
afddba49
NP
2299 written += status;
2300 *ppos = pos + status;
2301
2302 /*
2303 * For now, when the user asks for O_SYNC, we'll actually give
2304 * O_DSYNC
2305 */
1da177e4
LT
2306 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2307 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2308 status = generic_osync_inode(inode, mapping,
2309 OSYNC_METADATA|OSYNC_DATA);
2310 }
2311 }
2312
2313 /*
2314 * If we get here for O_DIRECT writes then we must have fallen through
2315 * to buffered writes (block instantiation inside i_size). So we sync
2316 * the file data here, to try to honour O_DIRECT expectations.
2317 */
2318 if (unlikely(file->f_flags & O_DIRECT) && written)
2319 status = filemap_write_and_wait(mapping);
2320
1da177e4
LT
2321 return written ? written : status;
2322}
2323EXPORT_SYMBOL(generic_file_buffered_write);
2324
5ce7852c 2325static ssize_t
1da177e4
LT
2326__generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2327 unsigned long nr_segs, loff_t *ppos)
2328{
2329 struct file *file = iocb->ki_filp;
fb5527e6 2330 struct address_space * mapping = file->f_mapping;
1da177e4
LT
2331 size_t ocount; /* original count */
2332 size_t count; /* after file limit checks */
2333 struct inode *inode = mapping->host;
1da177e4
LT
2334 loff_t pos;
2335 ssize_t written;
2336 ssize_t err;
2337
2338 ocount = 0;
0ceb3314
DM
2339 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2340 if (err)
2341 return err;
1da177e4
LT
2342
2343 count = ocount;
2344 pos = *ppos;
2345
2346 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2347
2348 /* We can write back this queue in page reclaim */
2349 current->backing_dev_info = mapping->backing_dev_info;
2350 written = 0;
2351
2352 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2353 if (err)
2354 goto out;
2355
2356 if (count == 0)
2357 goto out;
2358
d3ac7f89 2359 err = remove_suid(file->f_path.dentry);
1da177e4
LT
2360 if (err)
2361 goto out;
2362
870f4817 2363 file_update_time(file);
1da177e4
LT
2364
2365 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2366 if (unlikely(file->f_flags & O_DIRECT)) {
fb5527e6
JM
2367 loff_t endbyte;
2368 ssize_t written_buffered;
2369
2370 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
2371 ppos, count, ocount);
1da177e4
LT
2372 if (written < 0 || written == count)
2373 goto out;
2374 /*
2375 * direct-io write to a hole: fall through to buffered I/O
2376 * for completing the rest of the request.
2377 */
2378 pos += written;
2379 count -= written;
fb5527e6
JM
2380 written_buffered = generic_file_buffered_write(iocb, iov,
2381 nr_segs, pos, ppos, count,
2382 written);
2383 /*
2384 * If generic_file_buffered_write() retuned a synchronous error
2385 * then we want to return the number of bytes which were
2386 * direct-written, or the error code if that was zero. Note
2387 * that this differs from normal direct-io semantics, which
2388 * will return -EFOO even if some bytes were written.
2389 */
2390 if (written_buffered < 0) {
2391 err = written_buffered;
2392 goto out;
2393 }
1da177e4 2394
fb5527e6
JM
2395 /*
2396 * We need to ensure that the page cache pages are written to
2397 * disk and invalidated to preserve the expected O_DIRECT
2398 * semantics.
2399 */
2400 endbyte = pos + written_buffered - written - 1;
ef51c976
MF
2401 err = do_sync_mapping_range(file->f_mapping, pos, endbyte,
2402 SYNC_FILE_RANGE_WAIT_BEFORE|
2403 SYNC_FILE_RANGE_WRITE|
2404 SYNC_FILE_RANGE_WAIT_AFTER);
fb5527e6
JM
2405 if (err == 0) {
2406 written = written_buffered;
2407 invalidate_mapping_pages(mapping,
2408 pos >> PAGE_CACHE_SHIFT,
2409 endbyte >> PAGE_CACHE_SHIFT);
2410 } else {
2411 /*
2412 * We don't know how much we wrote, so just return
2413 * the number of bytes which were direct-written
2414 */
2415 }
2416 } else {
2417 written = generic_file_buffered_write(iocb, iov, nr_segs,
2418 pos, ppos, count, written);
2419 }
1da177e4
LT
2420out:
2421 current->backing_dev_info = NULL;
2422 return written ? written : err;
2423}
1da177e4 2424
027445c3
BP
2425ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
2426 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1da177e4
LT
2427{
2428 struct file *file = iocb->ki_filp;
2429 struct address_space *mapping = file->f_mapping;
2430 struct inode *inode = mapping->host;
2431 ssize_t ret;
1da177e4 2432
027445c3
BP
2433 BUG_ON(iocb->ki_pos != pos);
2434
2435 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
2436 &iocb->ki_pos);
1da177e4
LT
2437
2438 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
027445c3 2439 ssize_t err;
1da177e4
LT
2440
2441 err = sync_page_range_nolock(inode, mapping, pos, ret);
2442 if (err < 0)
2443 ret = err;
2444 }
2445 return ret;
2446}
027445c3 2447EXPORT_SYMBOL(generic_file_aio_write_nolock);
1da177e4 2448
027445c3
BP
2449ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2450 unsigned long nr_segs, loff_t pos)
1da177e4
LT
2451{
2452 struct file *file = iocb->ki_filp;
2453 struct address_space *mapping = file->f_mapping;
2454 struct inode *inode = mapping->host;
2455 ssize_t ret;
1da177e4
LT
2456
2457 BUG_ON(iocb->ki_pos != pos);
2458
1b1dcc1b 2459 mutex_lock(&inode->i_mutex);
027445c3
BP
2460 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
2461 &iocb->ki_pos);
1b1dcc1b 2462 mutex_unlock(&inode->i_mutex);
1da177e4
LT
2463
2464 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2465 ssize_t err;
2466
2467 err = sync_page_range(inode, mapping, pos, ret);
2468 if (err < 0)
2469 ret = err;
2470 }
2471 return ret;
2472}
2473EXPORT_SYMBOL(generic_file_aio_write);
2474
1da177e4 2475/*
1b1dcc1b 2476 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
1da177e4
LT
2477 * went wrong during pagecache shootdown.
2478 */
5ce7852c 2479static ssize_t
1da177e4
LT
2480generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2481 loff_t offset, unsigned long nr_segs)
2482{
2483 struct file *file = iocb->ki_filp;
2484 struct address_space *mapping = file->f_mapping;
2485 ssize_t retval;
65b8291c
ZB
2486 size_t write_len;
2487 pgoff_t end = 0; /* silence gcc */
1da177e4
LT
2488
2489 /*
2490 * If it's a write, unmap all mmappings of the file up-front. This
2491 * will cause any pte dirty bits to be propagated into the pageframes
2492 * for the subsequent filemap_write_and_wait().
2493 */
2494 if (rw == WRITE) {
2495 write_len = iov_length(iov, nr_segs);
65b8291c 2496 end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT;
1da177e4
LT
2497 if (mapping_mapped(mapping))
2498 unmap_mapping_range(mapping, offset, write_len, 0);
2499 }
2500
2501 retval = filemap_write_and_wait(mapping);
65b8291c
ZB
2502 if (retval)
2503 goto out;
2504
2505 /*
2506 * After a write we want buffered reads to be sure to go to disk to get
2507 * the new data. We invalidate clean cached page from the region we're
2508 * about to write. We do this *before* the write so that we can return
2509 * -EIO without clobbering -EIOCBQUEUED from ->direct_IO().
2510 */
2511 if (rw == WRITE && mapping->nrpages) {
2512 retval = invalidate_inode_pages2_range(mapping,
1da177e4 2513 offset >> PAGE_CACHE_SHIFT, end);
65b8291c
ZB
2514 if (retval)
2515 goto out;
1da177e4 2516 }
65b8291c
ZB
2517
2518 retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
65b8291c
ZB
2519
2520 /*
2521 * Finally, try again to invalidate clean pages which might have been
bdb76ef5
ZB
2522 * cached by non-direct readahead, or faulted in by get_user_pages()
2523 * if the source of the write was an mmap'ed region of the file
2524 * we're writing. Either one is a pretty crazy thing to do,
2525 * so we don't support it 100%. If this invalidation
2526 * fails, tough, the write still worked...
65b8291c
ZB
2527 */
2528 if (rw == WRITE && mapping->nrpages) {
bdb76ef5 2529 invalidate_inode_pages2_range(mapping, offset >> PAGE_CACHE_SHIFT, end);
65b8291c
ZB
2530 }
2531out:
1da177e4
LT
2532 return retval;
2533}
cf9a2ae8
DH
2534
2535/**
2536 * try_to_release_page() - release old fs-specific metadata on a page
2537 *
2538 * @page: the page which the kernel is trying to free
2539 * @gfp_mask: memory allocation flags (and I/O mode)
2540 *
2541 * The address_space is to try to release any data against the page
2542 * (presumably at page->private). If the release was successful, return `1'.
2543 * Otherwise return zero.
2544 *
2545 * The @gfp_mask argument specifies whether I/O may be performed to release
2546 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
2547 *
2548 * NOTE: @gfp_mask may go away, and this function may become non-blocking.
2549 */
2550int try_to_release_page(struct page *page, gfp_t gfp_mask)
2551{
2552 struct address_space * const mapping = page->mapping;
2553
2554 BUG_ON(!PageLocked(page));
2555 if (PageWriteback(page))
2556 return 0;
2557
2558 if (mapping && mapping->a_ops->releasepage)
2559 return mapping->a_ops->releasepage(page, gfp_mask);
2560 return try_to_free_buffers(page);
2561}
2562
2563EXPORT_SYMBOL(try_to_release_page);
This page took 0.785441 seconds and 5 git commands to generate.