Merge tag 'backport/v3.14.24-ltsi-rc1/rcar-snd-to-next-20141121' into backport/v3...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / filemap.c
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  */
12 #include <linux/export.h>
13 #include <linux/compiler.h>
14 #include <linux/fs.h>
15 #include <linux/uaccess.h>
16 #include <linux/aio.h>
17 #include <linux/capability.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/gfp.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/backing-dev.h>
29 #include <linux/pagevec.h>
30 #include <linux/blkdev.h>
31 #include <linux/security.h>
32 #include <linux/cpuset.h>
33 #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
34 #include <linux/memcontrol.h>
35 #include <linux/cleancache.h>
36 #include "internal.h"
37
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/filemap.h>
40
41 /*
42  * FIXME: remove all knowledge of the buffer layer from the core VM
43  */
44 #include <linux/buffer_head.h> /* for try_to_free_buffers */
45
46 #include <asm/mman.h>
47
48 /*
49  * Shared mappings implemented 30.11.1994. It's not fully working yet,
50  * though.
51  *
52  * Shared mappings now work. 15.8.1995  Bruno.
53  *
54  * finished 'unifying' the page and buffer cache and SMP-threaded the
55  * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
56  *
57  * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
58  */
59
60 /*
61  * Lock ordering:
62  *
63  *  ->i_mmap_mutex              (truncate_pagecache)
64  *    ->private_lock            (__free_pte->__set_page_dirty_buffers)
65  *      ->swap_lock             (exclusive_swap_page, others)
66  *        ->mapping->tree_lock
67  *
68  *  ->i_mutex
69  *    ->i_mmap_mutex            (truncate->unmap_mapping_range)
70  *
71  *  ->mmap_sem
72  *    ->i_mmap_mutex
73  *      ->page_table_lock or pte_lock   (various, mainly in memory.c)
74  *        ->mapping->tree_lock  (arch-dependent flush_dcache_mmap_lock)
75  *
76  *  ->mmap_sem
77  *    ->lock_page               (access_process_vm)
78  *
79  *  ->i_mutex                   (generic_file_buffered_write)
80  *    ->mmap_sem                (fault_in_pages_readable->do_page_fault)
81  *
82  *  bdi->wb.list_lock
83  *    sb_lock                   (fs/fs-writeback.c)
84  *    ->mapping->tree_lock      (__sync_single_inode)
85  *
86  *  ->i_mmap_mutex
87  *    ->anon_vma.lock           (vma_adjust)
88  *
89  *  ->anon_vma.lock
90  *    ->page_table_lock or pte_lock     (anon_vma_prepare and various)
91  *
92  *  ->page_table_lock or pte_lock
93  *    ->swap_lock               (try_to_unmap_one)
94  *    ->private_lock            (try_to_unmap_one)
95  *    ->tree_lock               (try_to_unmap_one)
96  *    ->zone.lru_lock           (follow_page->mark_page_accessed)
97  *    ->zone.lru_lock           (check_pte_range->isolate_lru_page)
98  *    ->private_lock            (page_remove_rmap->set_page_dirty)
99  *    ->tree_lock               (page_remove_rmap->set_page_dirty)
100  *    bdi.wb->list_lock         (page_remove_rmap->set_page_dirty)
101  *    ->inode->i_lock           (page_remove_rmap->set_page_dirty)
102  *    bdi.wb->list_lock         (zap_pte_range->set_page_dirty)
103  *    ->inode->i_lock           (zap_pte_range->set_page_dirty)
104  *    ->private_lock            (zap_pte_range->__set_page_dirty_buffers)
105  *
106  * ->i_mmap_mutex
107  *   ->tasklist_lock            (memory_failure, collect_procs_ao)
108  */
109
110 /*
111  * Delete a page from the page cache and free it. Caller has to make
112  * sure the page is locked and that nobody else uses it - or that usage
113  * is safe.  The caller must hold the mapping's tree_lock.
114  */
115 void __delete_from_page_cache(struct page *page)
116 {
117         struct address_space *mapping = page->mapping;
118
119         trace_mm_filemap_delete_from_page_cache(page);
120         /*
121          * if we're uptodate, flush out into the cleancache, otherwise
122          * invalidate any existing cleancache entries.  We can't leave
123          * stale data around in the cleancache once our page is gone
124          */
125         if (PageUptodate(page) && PageMappedToDisk(page))
126                 cleancache_put_page(page);
127         else
128                 cleancache_invalidate_page(mapping, page);
129
130         radix_tree_delete(&mapping->page_tree, page->index);
131         page->mapping = NULL;
132         /* Leave page->index set: truncation lookup relies upon it */
133         mapping->nrpages--;
134         __dec_zone_page_state(page, NR_FILE_PAGES);
135         if (PageSwapBacked(page))
136                 __dec_zone_page_state(page, NR_SHMEM);
137         BUG_ON(page_mapped(page));
138
139         /*
140          * Some filesystems seem to re-dirty the page even after
141          * the VM has canceled the dirty bit (eg ext3 journaling).
142          *
143          * Fix it up by doing a final dirty accounting check after
144          * having removed the page entirely.
145          */
146         if (PageDirty(page) && mapping_cap_account_dirty(mapping)) {
147                 dec_zone_page_state(page, NR_FILE_DIRTY);
148                 dec_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
149         }
150 }
151
152 /**
153  * delete_from_page_cache - delete page from page cache
154  * @page: the page which the kernel is trying to remove from page cache
155  *
156  * This must be called only on pages that have been verified to be in the page
157  * cache and locked.  It will never put the page into the free list, the caller
158  * has a reference on the page.
159  */
160 void delete_from_page_cache(struct page *page)
161 {
162         struct address_space *mapping = page->mapping;
163         void (*freepage)(struct page *);
164
165         BUG_ON(!PageLocked(page));
166
167         freepage = mapping->a_ops->freepage;
168         spin_lock_irq(&mapping->tree_lock);
169         __delete_from_page_cache(page);
170         spin_unlock_irq(&mapping->tree_lock);
171         mem_cgroup_uncharge_cache_page(page);
172
173         if (freepage)
174                 freepage(page);
175         page_cache_release(page);
176 }
177 EXPORT_SYMBOL(delete_from_page_cache);
178
179 static int sleep_on_page(void *word)
180 {
181         io_schedule();
182         return 0;
183 }
184
185 static int sleep_on_page_killable(void *word)
186 {
187         sleep_on_page(word);
188         return fatal_signal_pending(current) ? -EINTR : 0;
189 }
190
191 static int filemap_check_errors(struct address_space *mapping)
192 {
193         int ret = 0;
194         /* Check for outstanding write errors */
195         if (test_bit(AS_ENOSPC, &mapping->flags) &&
196             test_and_clear_bit(AS_ENOSPC, &mapping->flags))
197                 ret = -ENOSPC;
198         if (test_bit(AS_EIO, &mapping->flags) &&
199             test_and_clear_bit(AS_EIO, &mapping->flags))
200                 ret = -EIO;
201         return ret;
202 }
203
204 /**
205  * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
206  * @mapping:    address space structure to write
207  * @start:      offset in bytes where the range starts
208  * @end:        offset in bytes where the range ends (inclusive)
209  * @sync_mode:  enable synchronous operation
210  *
211  * Start writeback against all of a mapping's dirty pages that lie
212  * within the byte offsets <start, end> inclusive.
213  *
214  * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
215  * opposed to a regular memory cleansing writeback.  The difference between
216  * these two operations is that if a dirty page/buffer is encountered, it must
217  * be waited upon, and not just skipped over.
218  */
219 int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
220                                 loff_t end, int sync_mode)
221 {
222         int ret;
223         struct writeback_control wbc = {
224                 .sync_mode = sync_mode,
225                 .nr_to_write = LONG_MAX,
226                 .range_start = start,
227                 .range_end = end,
228         };
229
230         if (!mapping_cap_writeback_dirty(mapping))
231                 return 0;
232
233         ret = do_writepages(mapping, &wbc);
234         return ret;
235 }
236
237 static inline int __filemap_fdatawrite(struct address_space *mapping,
238         int sync_mode)
239 {
240         return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
241 }
242
243 int filemap_fdatawrite(struct address_space *mapping)
244 {
245         return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
246 }
247 EXPORT_SYMBOL(filemap_fdatawrite);
248
249 int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
250                                 loff_t end)
251 {
252         return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
253 }
254 EXPORT_SYMBOL(filemap_fdatawrite_range);
255
256 /**
257  * filemap_flush - mostly a non-blocking flush
258  * @mapping:    target address_space
259  *
260  * This is a mostly non-blocking flush.  Not suitable for data-integrity
261  * purposes - I/O may not be started against all dirty pages.
262  */
263 int filemap_flush(struct address_space *mapping)
264 {
265         return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
266 }
267 EXPORT_SYMBOL(filemap_flush);
268
269 /**
270  * filemap_fdatawait_range - wait for writeback to complete
271  * @mapping:            address space structure to wait for
272  * @start_byte:         offset in bytes where the range starts
273  * @end_byte:           offset in bytes where the range ends (inclusive)
274  *
275  * Walk the list of under-writeback pages of the given address space
276  * in the given range and wait for all of them.
277  */
278 int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
279                             loff_t end_byte)
280 {
281         pgoff_t index = start_byte >> PAGE_CACHE_SHIFT;
282         pgoff_t end = end_byte >> PAGE_CACHE_SHIFT;
283         struct pagevec pvec;
284         int nr_pages;
285         int ret2, ret = 0;
286
287         if (end_byte < start_byte)
288                 goto out;
289
290         pagevec_init(&pvec, 0);
291         while ((index <= end) &&
292                         (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
293                         PAGECACHE_TAG_WRITEBACK,
294                         min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
295                 unsigned i;
296
297                 for (i = 0; i < nr_pages; i++) {
298                         struct page *page = pvec.pages[i];
299
300                         /* until radix tree lookup accepts end_index */
301                         if (page->index > end)
302                                 continue;
303
304                         wait_on_page_writeback(page);
305                         if (TestClearPageError(page))
306                                 ret = -EIO;
307                 }
308                 pagevec_release(&pvec);
309                 cond_resched();
310         }
311 out:
312         ret2 = filemap_check_errors(mapping);
313         if (!ret)
314                 ret = ret2;
315
316         return ret;
317 }
318 EXPORT_SYMBOL(filemap_fdatawait_range);
319
320 /**
321  * filemap_fdatawait - wait for all under-writeback pages to complete
322  * @mapping: address space structure to wait for
323  *
324  * Walk the list of under-writeback pages of the given address space
325  * and wait for all of them.
326  */
327 int filemap_fdatawait(struct address_space *mapping)
328 {
329         loff_t i_size = i_size_read(mapping->host);
330
331         if (i_size == 0)
332                 return 0;
333
334         return filemap_fdatawait_range(mapping, 0, i_size - 1);
335 }
336 EXPORT_SYMBOL(filemap_fdatawait);
337
338 int filemap_write_and_wait(struct address_space *mapping)
339 {
340         int err = 0;
341
342         if (mapping->nrpages) {
343                 err = filemap_fdatawrite(mapping);
344                 /*
345                  * Even if the above returned error, the pages may be
346                  * written partially (e.g. -ENOSPC), so we wait for it.
347                  * But the -EIO is special case, it may indicate the worst
348                  * thing (e.g. bug) happened, so we avoid waiting for it.
349                  */
350                 if (err != -EIO) {
351                         int err2 = filemap_fdatawait(mapping);
352                         if (!err)
353                                 err = err2;
354                 }
355         } else {
356                 err = filemap_check_errors(mapping);
357         }
358         return err;
359 }
360 EXPORT_SYMBOL(filemap_write_and_wait);
361
362 /**
363  * filemap_write_and_wait_range - write out & wait on a file range
364  * @mapping:    the address_space for the pages
365  * @lstart:     offset in bytes where the range starts
366  * @lend:       offset in bytes where the range ends (inclusive)
367  *
368  * Write out and wait upon file offsets lstart->lend, inclusive.
369  *
370  * Note that `lend' is inclusive (describes the last byte to be written) so
371  * that this function can be used to write to the very end-of-file (end = -1).
372  */
373 int filemap_write_and_wait_range(struct address_space *mapping,
374                                  loff_t lstart, loff_t lend)
375 {
376         int err = 0;
377
378         if (mapping->nrpages) {
379                 err = __filemap_fdatawrite_range(mapping, lstart, lend,
380                                                  WB_SYNC_ALL);
381                 /* See comment of filemap_write_and_wait() */
382                 if (err != -EIO) {
383                         int err2 = filemap_fdatawait_range(mapping,
384                                                 lstart, lend);
385                         if (!err)
386                                 err = err2;
387                 }
388         } else {
389                 err = filemap_check_errors(mapping);
390         }
391         return err;
392 }
393 EXPORT_SYMBOL(filemap_write_and_wait_range);
394
395 /**
396  * replace_page_cache_page - replace a pagecache page with a new one
397  * @old:        page to be replaced
398  * @new:        page to replace with
399  * @gfp_mask:   allocation mode
400  *
401  * This function replaces a page in the pagecache with a new one.  On
402  * success it acquires the pagecache reference for the new page and
403  * drops it for the old page.  Both the old and new pages must be
404  * locked.  This function does not add the new page to the LRU, the
405  * caller must do that.
406  *
407  * The remove + add is atomic.  The only way this function can fail is
408  * memory allocation failure.
409  */
410 int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
411 {
412         int error;
413
414         VM_BUG_ON_PAGE(!PageLocked(old), old);
415         VM_BUG_ON_PAGE(!PageLocked(new), new);
416         VM_BUG_ON_PAGE(new->mapping, new);
417
418         error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
419         if (!error) {
420                 struct address_space *mapping = old->mapping;
421                 void (*freepage)(struct page *);
422
423                 pgoff_t offset = old->index;
424                 freepage = mapping->a_ops->freepage;
425
426                 page_cache_get(new);
427                 new->mapping = mapping;
428                 new->index = offset;
429
430                 spin_lock_irq(&mapping->tree_lock);
431                 __delete_from_page_cache(old);
432                 error = radix_tree_insert(&mapping->page_tree, offset, new);
433                 BUG_ON(error);
434                 mapping->nrpages++;
435                 __inc_zone_page_state(new, NR_FILE_PAGES);
436                 if (PageSwapBacked(new))
437                         __inc_zone_page_state(new, NR_SHMEM);
438                 spin_unlock_irq(&mapping->tree_lock);
439                 /* mem_cgroup codes must not be called under tree_lock */
440                 mem_cgroup_replace_page_cache(old, new);
441                 radix_tree_preload_end();
442                 if (freepage)
443                         freepage(old);
444                 page_cache_release(old);
445         }
446
447         return error;
448 }
449 EXPORT_SYMBOL_GPL(replace_page_cache_page);
450
451 static int page_cache_tree_insert(struct address_space *mapping,
452                                   struct page *page)
453 {
454         void **slot;
455         int error;
456
457         slot = radix_tree_lookup_slot(&mapping->page_tree, page->index);
458         if (slot) {
459                 void *p;
460
461                 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
462                 if (!radix_tree_exceptional_entry(p))
463                         return -EEXIST;
464                 radix_tree_replace_slot(slot, page);
465                 mapping->nrpages++;
466                 return 0;
467         }
468         error = radix_tree_insert(&mapping->page_tree, page->index, page);
469         if (!error)
470                 mapping->nrpages++;
471         return error;
472 }
473
474 /**
475  * add_to_page_cache_locked - add a locked page to the pagecache
476  * @page:       page to add
477  * @mapping:    the page's address_space
478  * @offset:     page index
479  * @gfp_mask:   page allocation mode
480  *
481  * This function is used to add a page to the pagecache. It must be locked.
482  * This function does not add the page to the LRU.  The caller must do that.
483  */
484 int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
485                 pgoff_t offset, gfp_t gfp_mask)
486 {
487         int error;
488
489         VM_BUG_ON_PAGE(!PageLocked(page), page);
490         VM_BUG_ON_PAGE(PageSwapBacked(page), page);
491
492         error = mem_cgroup_cache_charge(page, current->mm,
493                                         gfp_mask & GFP_RECLAIM_MASK);
494         if (error)
495                 return error;
496
497         error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
498         if (error) {
499                 mem_cgroup_uncharge_cache_page(page);
500                 return error;
501         }
502
503         page_cache_get(page);
504         page->mapping = mapping;
505         page->index = offset;
506
507         spin_lock_irq(&mapping->tree_lock);
508         error = page_cache_tree_insert(mapping, page);
509         radix_tree_preload_end();
510         if (unlikely(error))
511                 goto err_insert;
512         __inc_zone_page_state(page, NR_FILE_PAGES);
513         spin_unlock_irq(&mapping->tree_lock);
514         trace_mm_filemap_add_to_page_cache(page);
515         return 0;
516 err_insert:
517         page->mapping = NULL;
518         /* Leave page->index set: truncation relies upon it */
519         spin_unlock_irq(&mapping->tree_lock);
520         mem_cgroup_uncharge_cache_page(page);
521         page_cache_release(page);
522         return error;
523 }
524 EXPORT_SYMBOL(add_to_page_cache_locked);
525
526 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
527                                 pgoff_t offset, gfp_t gfp_mask)
528 {
529         int ret;
530
531         ret = add_to_page_cache(page, mapping, offset, gfp_mask);
532         if (ret == 0)
533                 lru_cache_add_file(page);
534         return ret;
535 }
536 EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
537
538 #ifdef CONFIG_NUMA
539 struct page *__page_cache_alloc(gfp_t gfp)
540 {
541         int n;
542         struct page *page;
543
544         if (cpuset_do_page_mem_spread()) {
545                 unsigned int cpuset_mems_cookie;
546                 do {
547                         cpuset_mems_cookie = read_mems_allowed_begin();
548                         n = cpuset_mem_spread_node();
549                         page = alloc_pages_exact_node(n, gfp, 0);
550                 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
551
552                 return page;
553         }
554         return alloc_pages(gfp, 0);
555 }
556 EXPORT_SYMBOL(__page_cache_alloc);
557 #endif
558
559 /*
560  * In order to wait for pages to become available there must be
561  * waitqueues associated with pages. By using a hash table of
562  * waitqueues where the bucket discipline is to maintain all
563  * waiters on the same queue and wake all when any of the pages
564  * become available, and for the woken contexts to check to be
565  * sure the appropriate page became available, this saves space
566  * at a cost of "thundering herd" phenomena during rare hash
567  * collisions.
568  */
569 static wait_queue_head_t *page_waitqueue(struct page *page)
570 {
571         const struct zone *zone = page_zone(page);
572
573         return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
574 }
575
576 static inline void wake_up_page(struct page *page, int bit)
577 {
578         __wake_up_bit(page_waitqueue(page), &page->flags, bit);
579 }
580
581 void wait_on_page_bit(struct page *page, int bit_nr)
582 {
583         DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
584
585         if (test_bit(bit_nr, &page->flags))
586                 __wait_on_bit(page_waitqueue(page), &wait, sleep_on_page,
587                                                         TASK_UNINTERRUPTIBLE);
588 }
589 EXPORT_SYMBOL(wait_on_page_bit);
590
591 int wait_on_page_bit_killable(struct page *page, int bit_nr)
592 {
593         DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
594
595         if (!test_bit(bit_nr, &page->flags))
596                 return 0;
597
598         return __wait_on_bit(page_waitqueue(page), &wait,
599                              sleep_on_page_killable, TASK_KILLABLE);
600 }
601
602 /**
603  * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
604  * @page: Page defining the wait queue of interest
605  * @waiter: Waiter to add to the queue
606  *
607  * Add an arbitrary @waiter to the wait queue for the nominated @page.
608  */
609 void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
610 {
611         wait_queue_head_t *q = page_waitqueue(page);
612         unsigned long flags;
613
614         spin_lock_irqsave(&q->lock, flags);
615         __add_wait_queue(q, waiter);
616         spin_unlock_irqrestore(&q->lock, flags);
617 }
618 EXPORT_SYMBOL_GPL(add_page_wait_queue);
619
620 /**
621  * unlock_page - unlock a locked page
622  * @page: the page
623  *
624  * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
625  * Also wakes sleepers in wait_on_page_writeback() because the wakeup
626  * mechananism between PageLocked pages and PageWriteback pages is shared.
627  * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
628  *
629  * The mb is necessary to enforce ordering between the clear_bit and the read
630  * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
631  */
632 void unlock_page(struct page *page)
633 {
634         VM_BUG_ON_PAGE(!PageLocked(page), page);
635         clear_bit_unlock(PG_locked, &page->flags);
636         smp_mb__after_clear_bit();
637         wake_up_page(page, PG_locked);
638 }
639 EXPORT_SYMBOL(unlock_page);
640
641 /**
642  * end_page_writeback - end writeback against a page
643  * @page: the page
644  */
645 void end_page_writeback(struct page *page)
646 {
647         if (TestClearPageReclaim(page))
648                 rotate_reclaimable_page(page);
649
650         if (!test_clear_page_writeback(page))
651                 BUG();
652
653         smp_mb__after_clear_bit();
654         wake_up_page(page, PG_writeback);
655 }
656 EXPORT_SYMBOL(end_page_writeback);
657
658 /**
659  * __lock_page - get a lock on the page, assuming we need to sleep to get it
660  * @page: the page to lock
661  */
662 void __lock_page(struct page *page)
663 {
664         DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
665
666         __wait_on_bit_lock(page_waitqueue(page), &wait, sleep_on_page,
667                                                         TASK_UNINTERRUPTIBLE);
668 }
669 EXPORT_SYMBOL(__lock_page);
670
671 int __lock_page_killable(struct page *page)
672 {
673         DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
674
675         return __wait_on_bit_lock(page_waitqueue(page), &wait,
676                                         sleep_on_page_killable, TASK_KILLABLE);
677 }
678 EXPORT_SYMBOL_GPL(__lock_page_killable);
679
680 int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
681                          unsigned int flags)
682 {
683         if (flags & FAULT_FLAG_ALLOW_RETRY) {
684                 /*
685                  * CAUTION! In this case, mmap_sem is not released
686                  * even though return 0.
687                  */
688                 if (flags & FAULT_FLAG_RETRY_NOWAIT)
689                         return 0;
690
691                 up_read(&mm->mmap_sem);
692                 if (flags & FAULT_FLAG_KILLABLE)
693                         wait_on_page_locked_killable(page);
694                 else
695                         wait_on_page_locked(page);
696                 return 0;
697         } else {
698                 if (flags & FAULT_FLAG_KILLABLE) {
699                         int ret;
700
701                         ret = __lock_page_killable(page);
702                         if (ret) {
703                                 up_read(&mm->mmap_sem);
704                                 return 0;
705                         }
706                 } else
707                         __lock_page(page);
708                 return 1;
709         }
710 }
711
712 /**
713  * page_cache_next_hole - find the next hole (not-present entry)
714  * @mapping: mapping
715  * @index: index
716  * @max_scan: maximum range to search
717  *
718  * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
719  * lowest indexed hole.
720  *
721  * Returns: the index of the hole if found, otherwise returns an index
722  * outside of the set specified (in which case 'return - index >=
723  * max_scan' will be true). In rare cases of index wrap-around, 0 will
724  * be returned.
725  *
726  * page_cache_next_hole may be called under rcu_read_lock. However,
727  * like radix_tree_gang_lookup, this will not atomically search a
728  * snapshot of the tree at a single point in time. For example, if a
729  * hole is created at index 5, then subsequently a hole is created at
730  * index 10, page_cache_next_hole covering both indexes may return 10
731  * if called under rcu_read_lock.
732  */
733 pgoff_t page_cache_next_hole(struct address_space *mapping,
734                              pgoff_t index, unsigned long max_scan)
735 {
736         unsigned long i;
737
738         for (i = 0; i < max_scan; i++) {
739                 struct page *page;
740
741                 page = radix_tree_lookup(&mapping->page_tree, index);
742                 if (!page || radix_tree_exceptional_entry(page))
743                         break;
744                 index++;
745                 if (index == 0)
746                         break;
747         }
748
749         return index;
750 }
751 EXPORT_SYMBOL(page_cache_next_hole);
752
753 /**
754  * page_cache_prev_hole - find the prev hole (not-present entry)
755  * @mapping: mapping
756  * @index: index
757  * @max_scan: maximum range to search
758  *
759  * Search backwards in the range [max(index-max_scan+1, 0), index] for
760  * the first hole.
761  *
762  * Returns: the index of the hole if found, otherwise returns an index
763  * outside of the set specified (in which case 'index - return >=
764  * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
765  * will be returned.
766  *
767  * page_cache_prev_hole may be called under rcu_read_lock. However,
768  * like radix_tree_gang_lookup, this will not atomically search a
769  * snapshot of the tree at a single point in time. For example, if a
770  * hole is created at index 10, then subsequently a hole is created at
771  * index 5, page_cache_prev_hole covering both indexes may return 5 if
772  * called under rcu_read_lock.
773  */
774 pgoff_t page_cache_prev_hole(struct address_space *mapping,
775                              pgoff_t index, unsigned long max_scan)
776 {
777         unsigned long i;
778
779         for (i = 0; i < max_scan; i++) {
780                 struct page *page;
781
782                 page = radix_tree_lookup(&mapping->page_tree, index);
783                 if (!page || radix_tree_exceptional_entry(page))
784                         break;
785                 index--;
786                 if (index == ULONG_MAX)
787                         break;
788         }
789
790         return index;
791 }
792 EXPORT_SYMBOL(page_cache_prev_hole);
793
794 /**
795  * find_get_entry - find and get a page cache entry
796  * @mapping: the address_space to search
797  * @offset: the page cache index
798  *
799  * Looks up the page cache slot at @mapping & @offset.  If there is a
800  * page cache page, it is returned with an increased refcount.
801  *
802  * If the slot holds a shadow entry of a previously evicted page, it
803  * is returned.
804  *
805  * Otherwise, %NULL is returned.
806  */
807 struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
808 {
809         void **pagep;
810         struct page *page;
811
812         rcu_read_lock();
813 repeat:
814         page = NULL;
815         pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
816         if (pagep) {
817                 page = radix_tree_deref_slot(pagep);
818                 if (unlikely(!page))
819                         goto out;
820                 if (radix_tree_exception(page)) {
821                         if (radix_tree_deref_retry(page))
822                                 goto repeat;
823                         /*
824                          * Otherwise, shmem/tmpfs must be storing a swap entry
825                          * here as an exceptional entry: so return it without
826                          * attempting to raise page count.
827                          */
828                         goto out;
829                 }
830                 if (!page_cache_get_speculative(page))
831                         goto repeat;
832
833                 /*
834                  * Has the page moved?
835                  * This is part of the lockless pagecache protocol. See
836                  * include/linux/pagemap.h for details.
837                  */
838                 if (unlikely(page != *pagep)) {
839                         page_cache_release(page);
840                         goto repeat;
841                 }
842         }
843 out:
844         rcu_read_unlock();
845
846         return page;
847 }
848 EXPORT_SYMBOL(find_get_entry);
849
850 /**
851  * find_get_page - find and get a page reference
852  * @mapping: the address_space to search
853  * @offset: the page index
854  *
855  * Looks up the page cache slot at @mapping & @offset.  If there is a
856  * page cache page, it is returned with an increased refcount.
857  *
858  * Otherwise, %NULL is returned.
859  */
860 struct page *find_get_page(struct address_space *mapping, pgoff_t offset)
861 {
862         struct page *page = find_get_entry(mapping, offset);
863
864         if (radix_tree_exceptional_entry(page))
865                 page = NULL;
866         return page;
867 }
868 EXPORT_SYMBOL(find_get_page);
869
870 /**
871  * find_lock_entry - locate, pin and lock a page cache entry
872  * @mapping: the address_space to search
873  * @offset: the page cache index
874  *
875  * Looks up the page cache slot at @mapping & @offset.  If there is a
876  * page cache page, it is returned locked and with an increased
877  * refcount.
878  *
879  * If the slot holds a shadow entry of a previously evicted page, it
880  * is returned.
881  *
882  * Otherwise, %NULL is returned.
883  *
884  * find_lock_entry() may sleep.
885  */
886 struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
887 {
888         struct page *page;
889
890 repeat:
891         page = find_get_entry(mapping, offset);
892         if (page && !radix_tree_exception(page)) {
893                 lock_page(page);
894                 /* Has the page been truncated? */
895                 if (unlikely(page->mapping != mapping)) {
896                         unlock_page(page);
897                         page_cache_release(page);
898                         goto repeat;
899                 }
900                 VM_BUG_ON_PAGE(page->index != offset, page);
901         }
902         return page;
903 }
904 EXPORT_SYMBOL(find_lock_entry);
905
906 /**
907  * find_lock_page - locate, pin and lock a pagecache page
908  * @mapping: the address_space to search
909  * @offset: the page index
910  *
911  * Looks up the page cache slot at @mapping & @offset.  If there is a
912  * page cache page, it is returned locked and with an increased
913  * refcount.
914  *
915  * Otherwise, %NULL is returned.
916  *
917  * find_lock_page() may sleep.
918  */
919 struct page *find_lock_page(struct address_space *mapping, pgoff_t offset)
920 {
921         struct page *page = find_lock_entry(mapping, offset);
922
923         if (radix_tree_exceptional_entry(page))
924                 page = NULL;
925         return page;
926 }
927 EXPORT_SYMBOL(find_lock_page);
928
929 /**
930  * find_or_create_page - locate or add a pagecache page
931  * @mapping: the page's address_space
932  * @index: the page's index into the mapping
933  * @gfp_mask: page allocation mode
934  *
935  * Looks up the page cache slot at @mapping & @offset.  If there is a
936  * page cache page, it is returned locked and with an increased
937  * refcount.
938  *
939  * If the page is not present, a new page is allocated using @gfp_mask
940  * and added to the page cache and the VM's LRU list.  The page is
941  * returned locked and with an increased refcount.
942  *
943  * On memory exhaustion, %NULL is returned.
944  *
945  * find_or_create_page() may sleep, even if @gfp_flags specifies an
946  * atomic allocation!
947  */
948 struct page *find_or_create_page(struct address_space *mapping,
949                 pgoff_t index, gfp_t gfp_mask)
950 {
951         struct page *page;
952         int err;
953 repeat:
954         page = find_lock_page(mapping, index);
955         if (!page) {
956                 page = __page_cache_alloc(gfp_mask);
957                 if (!page)
958                         return NULL;
959                 /*
960                  * We want a regular kernel memory (not highmem or DMA etc)
961                  * allocation for the radix tree nodes, but we need to honour
962                  * the context-specific requirements the caller has asked for.
963                  * GFP_RECLAIM_MASK collects those requirements.
964                  */
965                 err = add_to_page_cache_lru(page, mapping, index,
966                         (gfp_mask & GFP_RECLAIM_MASK));
967                 if (unlikely(err)) {
968                         page_cache_release(page);
969                         page = NULL;
970                         if (err == -EEXIST)
971                                 goto repeat;
972                 }
973         }
974         return page;
975 }
976 EXPORT_SYMBOL(find_or_create_page);
977
978 /**
979  * find_get_entries - gang pagecache lookup
980  * @mapping:    The address_space to search
981  * @start:      The starting page cache index
982  * @nr_entries: The maximum number of entries
983  * @entries:    Where the resulting entries are placed
984  * @indices:    The cache indices corresponding to the entries in @entries
985  *
986  * find_get_entries() will search for and return a group of up to
987  * @nr_entries entries in the mapping.  The entries are placed at
988  * @entries.  find_get_entries() takes a reference against any actual
989  * pages it returns.
990  *
991  * The search returns a group of mapping-contiguous page cache entries
992  * with ascending indexes.  There may be holes in the indices due to
993  * not-present pages.
994  *
995  * Any shadow entries of evicted pages are included in the returned
996  * array.
997  *
998  * find_get_entries() returns the number of pages and shadow entries
999  * which were found.
1000  */
1001 unsigned find_get_entries(struct address_space *mapping,
1002                           pgoff_t start, unsigned int nr_entries,
1003                           struct page **entries, pgoff_t *indices)
1004 {
1005         void **slot;
1006         unsigned int ret = 0;
1007         struct radix_tree_iter iter;
1008
1009         if (!nr_entries)
1010                 return 0;
1011
1012         rcu_read_lock();
1013 restart:
1014         radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1015                 struct page *page;
1016 repeat:
1017                 page = radix_tree_deref_slot(slot);
1018                 if (unlikely(!page))
1019                         continue;
1020                 if (radix_tree_exception(page)) {
1021                         if (radix_tree_deref_retry(page))
1022                                 goto restart;
1023                         /*
1024                          * Otherwise, we must be storing a swap entry
1025                          * here as an exceptional entry: so return it
1026                          * without attempting to raise page count.
1027                          */
1028                         goto export;
1029                 }
1030                 if (!page_cache_get_speculative(page))
1031                         goto repeat;
1032
1033                 /* Has the page moved? */
1034                 if (unlikely(page != *slot)) {
1035                         page_cache_release(page);
1036                         goto repeat;
1037                 }
1038 export:
1039                 indices[ret] = iter.index;
1040                 entries[ret] = page;
1041                 if (++ret == nr_entries)
1042                         break;
1043         }
1044         rcu_read_unlock();
1045         return ret;
1046 }
1047
1048 /**
1049  * find_get_pages - gang pagecache lookup
1050  * @mapping:    The address_space to search
1051  * @start:      The starting page index
1052  * @nr_pages:   The maximum number of pages
1053  * @pages:      Where the resulting pages are placed
1054  *
1055  * find_get_pages() will search for and return a group of up to
1056  * @nr_pages pages in the mapping.  The pages are placed at @pages.
1057  * find_get_pages() takes a reference against the returned pages.
1058  *
1059  * The search returns a group of mapping-contiguous pages with ascending
1060  * indexes.  There may be holes in the indices due to not-present pages.
1061  *
1062  * find_get_pages() returns the number of pages which were found.
1063  */
1064 unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1065                             unsigned int nr_pages, struct page **pages)
1066 {
1067         struct radix_tree_iter iter;
1068         void **slot;
1069         unsigned ret = 0;
1070
1071         if (unlikely(!nr_pages))
1072                 return 0;
1073
1074         rcu_read_lock();
1075 restart:
1076         radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1077                 struct page *page;
1078 repeat:
1079                 page = radix_tree_deref_slot(slot);
1080                 if (unlikely(!page))
1081                         continue;
1082
1083                 if (radix_tree_exception(page)) {
1084                         if (radix_tree_deref_retry(page)) {
1085                                 /*
1086                                  * Transient condition which can only trigger
1087                                  * when entry at index 0 moves out of or back
1088                                  * to root: none yet gotten, safe to restart.
1089                                  */
1090                                 WARN_ON(iter.index);
1091                                 goto restart;
1092                         }
1093                         /*
1094                          * Otherwise, shmem/tmpfs must be storing a swap entry
1095                          * here as an exceptional entry: so skip over it -
1096                          * we only reach this from invalidate_mapping_pages().
1097                          */
1098                         continue;
1099                 }
1100
1101                 if (!page_cache_get_speculative(page))
1102                         goto repeat;
1103
1104                 /* Has the page moved? */
1105                 if (unlikely(page != *slot)) {
1106                         page_cache_release(page);
1107                         goto repeat;
1108                 }
1109
1110                 pages[ret] = page;
1111                 if (++ret == nr_pages)
1112                         break;
1113         }
1114
1115         rcu_read_unlock();
1116         return ret;
1117 }
1118
1119 /**
1120  * find_get_pages_contig - gang contiguous pagecache lookup
1121  * @mapping:    The address_space to search
1122  * @index:      The starting page index
1123  * @nr_pages:   The maximum number of pages
1124  * @pages:      Where the resulting pages are placed
1125  *
1126  * find_get_pages_contig() works exactly like find_get_pages(), except
1127  * that the returned number of pages are guaranteed to be contiguous.
1128  *
1129  * find_get_pages_contig() returns the number of pages which were found.
1130  */
1131 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1132                                unsigned int nr_pages, struct page **pages)
1133 {
1134         struct radix_tree_iter iter;
1135         void **slot;
1136         unsigned int ret = 0;
1137
1138         if (unlikely(!nr_pages))
1139                 return 0;
1140
1141         rcu_read_lock();
1142 restart:
1143         radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
1144                 struct page *page;
1145 repeat:
1146                 page = radix_tree_deref_slot(slot);
1147                 /* The hole, there no reason to continue */
1148                 if (unlikely(!page))
1149                         break;
1150
1151                 if (radix_tree_exception(page)) {
1152                         if (radix_tree_deref_retry(page)) {
1153                                 /*
1154                                  * Transient condition which can only trigger
1155                                  * when entry at index 0 moves out of or back
1156                                  * to root: none yet gotten, safe to restart.
1157                                  */
1158                                 goto restart;
1159                         }
1160                         /*
1161                          * Otherwise, shmem/tmpfs must be storing a swap entry
1162                          * here as an exceptional entry: so stop looking for
1163                          * contiguous pages.
1164                          */
1165                         break;
1166                 }
1167
1168                 if (!page_cache_get_speculative(page))
1169                         goto repeat;
1170
1171                 /* Has the page moved? */
1172                 if (unlikely(page != *slot)) {
1173                         page_cache_release(page);
1174                         goto repeat;
1175                 }
1176
1177                 /*
1178                  * must check mapping and index after taking the ref.
1179                  * otherwise we can get both false positives and false
1180                  * negatives, which is just confusing to the caller.
1181                  */
1182                 if (page->mapping == NULL || page->index != iter.index) {
1183                         page_cache_release(page);
1184                         break;
1185                 }
1186
1187                 pages[ret] = page;
1188                 if (++ret == nr_pages)
1189                         break;
1190         }
1191         rcu_read_unlock();
1192         return ret;
1193 }
1194 EXPORT_SYMBOL(find_get_pages_contig);
1195
1196 /**
1197  * find_get_pages_tag - find and return pages that match @tag
1198  * @mapping:    the address_space to search
1199  * @index:      the starting page index
1200  * @tag:        the tag index
1201  * @nr_pages:   the maximum number of pages
1202  * @pages:      where the resulting pages are placed
1203  *
1204  * Like find_get_pages, except we only return pages which are tagged with
1205  * @tag.   We update @index to index the next page for the traversal.
1206  */
1207 unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
1208                         int tag, unsigned int nr_pages, struct page **pages)
1209 {
1210         struct radix_tree_iter iter;
1211         void **slot;
1212         unsigned ret = 0;
1213
1214         if (unlikely(!nr_pages))
1215                 return 0;
1216
1217         rcu_read_lock();
1218 restart:
1219         radix_tree_for_each_tagged(slot, &mapping->page_tree,
1220                                    &iter, *index, tag) {
1221                 struct page *page;
1222 repeat:
1223                 page = radix_tree_deref_slot(slot);
1224                 if (unlikely(!page))
1225                         continue;
1226
1227                 if (radix_tree_exception(page)) {
1228                         if (radix_tree_deref_retry(page)) {
1229                                 /*
1230                                  * Transient condition which can only trigger
1231                                  * when entry at index 0 moves out of or back
1232                                  * to root: none yet gotten, safe to restart.
1233                                  */
1234                                 goto restart;
1235                         }
1236                         /*
1237                          * This function is never used on a shmem/tmpfs
1238                          * mapping, so a swap entry won't be found here.
1239                          */
1240                         BUG();
1241                 }
1242
1243                 if (!page_cache_get_speculative(page))
1244                         goto repeat;
1245
1246                 /* Has the page moved? */
1247                 if (unlikely(page != *slot)) {
1248                         page_cache_release(page);
1249                         goto repeat;
1250                 }
1251
1252                 pages[ret] = page;
1253                 if (++ret == nr_pages)
1254                         break;
1255         }
1256
1257         rcu_read_unlock();
1258
1259         if (ret)
1260                 *index = pages[ret - 1]->index + 1;
1261
1262         return ret;
1263 }
1264 EXPORT_SYMBOL(find_get_pages_tag);
1265
1266 /**
1267  * grab_cache_page_nowait - returns locked page at given index in given cache
1268  * @mapping: target address_space
1269  * @index: the page index
1270  *
1271  * Same as grab_cache_page(), but do not wait if the page is unavailable.
1272  * This is intended for speculative data generators, where the data can
1273  * be regenerated if the page couldn't be grabbed.  This routine should
1274  * be safe to call while holding the lock for another page.
1275  *
1276  * Clear __GFP_FS when allocating the page to avoid recursion into the fs
1277  * and deadlock against the caller's locked page.
1278  */
1279 struct page *
1280 grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
1281 {
1282         struct page *page = find_get_page(mapping, index);
1283
1284         if (page) {
1285                 if (trylock_page(page))
1286                         return page;
1287                 page_cache_release(page);
1288                 return NULL;
1289         }
1290         page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
1291         if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) {
1292                 page_cache_release(page);
1293                 page = NULL;
1294         }
1295         return page;
1296 }
1297 EXPORT_SYMBOL(grab_cache_page_nowait);
1298
1299 /*
1300  * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1301  * a _large_ part of the i/o request. Imagine the worst scenario:
1302  *
1303  *      ---R__________________________________________B__________
1304  *         ^ reading here                             ^ bad block(assume 4k)
1305  *
1306  * read(R) => miss => readahead(R...B) => media error => frustrating retries
1307  * => failing the whole request => read(R) => read(R+1) =>
1308  * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1309  * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1310  * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1311  *
1312  * It is going insane. Fix it by quickly scaling down the readahead size.
1313  */
1314 static void shrink_readahead_size_eio(struct file *filp,
1315                                         struct file_ra_state *ra)
1316 {
1317         ra->ra_pages /= 4;
1318 }
1319
1320 /**
1321  * do_generic_file_read - generic file read routine
1322  * @filp:       the file to read
1323  * @ppos:       current file position
1324  * @desc:       read_descriptor
1325  *
1326  * This is a generic file read routine, and uses the
1327  * mapping->a_ops->readpage() function for the actual low-level stuff.
1328  *
1329  * This is really ugly. But the goto's actually try to clarify some
1330  * of the logic when it comes to error handling etc.
1331  */
1332 static void do_generic_file_read(struct file *filp, loff_t *ppos,
1333                 read_descriptor_t *desc)
1334 {
1335         struct address_space *mapping = filp->f_mapping;
1336         struct inode *inode = mapping->host;
1337         struct file_ra_state *ra = &filp->f_ra;
1338         pgoff_t index;
1339         pgoff_t last_index;
1340         pgoff_t prev_index;
1341         unsigned long offset;      /* offset into pagecache page */
1342         unsigned int prev_offset;
1343         int error;
1344
1345         index = *ppos >> PAGE_CACHE_SHIFT;
1346         prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
1347         prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
1348         last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
1349         offset = *ppos & ~PAGE_CACHE_MASK;
1350
1351         for (;;) {
1352                 struct page *page;
1353                 pgoff_t end_index;
1354                 loff_t isize;
1355                 unsigned long nr, ret;
1356
1357                 cond_resched();
1358 find_page:
1359                 page = find_get_page(mapping, index);
1360                 if (!page) {
1361                         page_cache_sync_readahead(mapping,
1362                                         ra, filp,
1363                                         index, last_index - index);
1364                         page = find_get_page(mapping, index);
1365                         if (unlikely(page == NULL))
1366                                 goto no_cached_page;
1367                 }
1368                 if (PageReadahead(page)) {
1369                         page_cache_async_readahead(mapping,
1370                                         ra, filp, page,
1371                                         index, last_index - index);
1372                 }
1373                 if (!PageUptodate(page)) {
1374                         if (inode->i_blkbits == PAGE_CACHE_SHIFT ||
1375                                         !mapping->a_ops->is_partially_uptodate)
1376                                 goto page_not_up_to_date;
1377                         if (!trylock_page(page))
1378                                 goto page_not_up_to_date;
1379                         /* Did it get truncated before we got the lock? */
1380                         if (!page->mapping)
1381                                 goto page_not_up_to_date_locked;
1382                         if (!mapping->a_ops->is_partially_uptodate(page,
1383                                                                 desc, offset))
1384                                 goto page_not_up_to_date_locked;
1385                         unlock_page(page);
1386                 }
1387 page_ok:
1388                 /*
1389                  * i_size must be checked after we know the page is Uptodate.
1390                  *
1391                  * Checking i_size after the check allows us to calculate
1392                  * the correct value for "nr", which means the zero-filled
1393                  * part of the page is not copied back to userspace (unless
1394                  * another truncate extends the file - this is desired though).
1395                  */
1396
1397                 isize = i_size_read(inode);
1398                 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1399                 if (unlikely(!isize || index > end_index)) {
1400                         page_cache_release(page);
1401                         goto out;
1402                 }
1403
1404                 /* nr is the maximum number of bytes to copy from this page */
1405                 nr = PAGE_CACHE_SIZE;
1406                 if (index == end_index) {
1407                         nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1408                         if (nr <= offset) {
1409                                 page_cache_release(page);
1410                                 goto out;
1411                         }
1412                 }
1413                 nr = nr - offset;
1414
1415                 /* If users can be writing to this page using arbitrary
1416                  * virtual addresses, take care about potential aliasing
1417                  * before reading the page on the kernel side.
1418                  */
1419                 if (mapping_writably_mapped(mapping))
1420                         flush_dcache_page(page);
1421
1422                 /*
1423                  * When a sequential read accesses a page several times,
1424                  * only mark it as accessed the first time.
1425                  */
1426                 if (prev_index != index || offset != prev_offset)
1427                         mark_page_accessed(page);
1428                 prev_index = index;
1429
1430                 /*
1431                  * Ok, we have the page, and it's up-to-date, so
1432                  * now we can copy it to user space...
1433                  *
1434                  * The file_read_actor routine returns how many bytes were
1435                  * actually used..
1436                  * NOTE! This may not be the same as how much of a user buffer
1437                  * we filled up (we may be padding etc), so we can only update
1438                  * "pos" here (the actor routine has to update the user buffer
1439                  * pointers and the remaining count).
1440                  */
1441                 ret = file_read_actor(desc, page, offset, nr);
1442                 offset += ret;
1443                 index += offset >> PAGE_CACHE_SHIFT;
1444                 offset &= ~PAGE_CACHE_MASK;
1445                 prev_offset = offset;
1446
1447                 page_cache_release(page);
1448                 if (ret == nr && desc->count)
1449                         continue;
1450                 goto out;
1451
1452 page_not_up_to_date:
1453                 /* Get exclusive access to the page ... */
1454                 error = lock_page_killable(page);
1455                 if (unlikely(error))
1456                         goto readpage_error;
1457
1458 page_not_up_to_date_locked:
1459                 /* Did it get truncated before we got the lock? */
1460                 if (!page->mapping) {
1461                         unlock_page(page);
1462                         page_cache_release(page);
1463                         continue;
1464                 }
1465
1466                 /* Did somebody else fill it already? */
1467                 if (PageUptodate(page)) {
1468                         unlock_page(page);
1469                         goto page_ok;
1470                 }
1471
1472 readpage:
1473                 /*
1474                  * A previous I/O error may have been due to temporary
1475                  * failures, eg. multipath errors.
1476                  * PG_error will be set again if readpage fails.
1477                  */
1478                 ClearPageError(page);
1479                 /* Start the actual read. The read will unlock the page. */
1480                 error = mapping->a_ops->readpage(filp, page);
1481
1482                 if (unlikely(error)) {
1483                         if (error == AOP_TRUNCATED_PAGE) {
1484                                 page_cache_release(page);
1485                                 goto find_page;
1486                         }
1487                         goto readpage_error;
1488                 }
1489
1490                 if (!PageUptodate(page)) {
1491                         error = lock_page_killable(page);
1492                         if (unlikely(error))
1493                                 goto readpage_error;
1494                         if (!PageUptodate(page)) {
1495                                 if (page->mapping == NULL) {
1496                                         /*
1497                                          * invalidate_mapping_pages got it
1498                                          */
1499                                         unlock_page(page);
1500                                         page_cache_release(page);
1501                                         goto find_page;
1502                                 }
1503                                 unlock_page(page);
1504                                 shrink_readahead_size_eio(filp, ra);
1505                                 error = -EIO;
1506                                 goto readpage_error;
1507                         }
1508                         unlock_page(page);
1509                 }
1510
1511                 goto page_ok;
1512
1513 readpage_error:
1514                 /* UHHUH! A synchronous read error occurred. Report it */
1515                 desc->error = error;
1516                 page_cache_release(page);
1517                 goto out;
1518
1519 no_cached_page:
1520                 /*
1521                  * Ok, it wasn't cached, so we need to create a new
1522                  * page..
1523                  */
1524                 page = page_cache_alloc_cold(mapping);
1525                 if (!page) {
1526                         desc->error = -ENOMEM;
1527                         goto out;
1528                 }
1529                 error = add_to_page_cache_lru(page, mapping,
1530                                                 index, GFP_KERNEL);
1531                 if (error) {
1532                         page_cache_release(page);
1533                         if (error == -EEXIST)
1534                                 goto find_page;
1535                         desc->error = error;
1536                         goto out;
1537                 }
1538                 goto readpage;
1539         }
1540
1541 out:
1542         ra->prev_pos = prev_index;
1543         ra->prev_pos <<= PAGE_CACHE_SHIFT;
1544         ra->prev_pos |= prev_offset;
1545
1546         *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
1547         file_accessed(filp);
1548 }
1549
1550 int file_read_actor(read_descriptor_t *desc, struct page *page,
1551                         unsigned long offset, unsigned long size)
1552 {
1553         char *kaddr;
1554         unsigned long left, count = desc->count;
1555
1556         if (size > count)
1557                 size = count;
1558
1559         /*
1560          * Faults on the destination of a read are common, so do it before
1561          * taking the kmap.
1562          */
1563         if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1564                 kaddr = kmap_atomic(page);
1565                 left = __copy_to_user_inatomic(desc->arg.buf,
1566                                                 kaddr + offset, size);
1567                 kunmap_atomic(kaddr);
1568                 if (left == 0)
1569                         goto success;
1570         }
1571
1572         /* Do it the slow way */
1573         kaddr = kmap(page);
1574         left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1575         kunmap(page);
1576
1577         if (left) {
1578                 size -= left;
1579                 desc->error = -EFAULT;
1580         }
1581 success:
1582         desc->count = count - size;
1583         desc->written += size;
1584         desc->arg.buf += size;
1585         return size;
1586 }
1587
1588 /*
1589  * Performs necessary checks before doing a write
1590  * @iov:        io vector request
1591  * @nr_segs:    number of segments in the iovec
1592  * @count:      number of bytes to write
1593  * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1594  *
1595  * Adjust number of segments and amount of bytes to write (nr_segs should be
1596  * properly initialized first). Returns appropriate error code that caller
1597  * should return or zero in case that write should be allowed.
1598  */
1599 int generic_segment_checks(const struct iovec *iov,
1600                         unsigned long *nr_segs, size_t *count, int access_flags)
1601 {
1602         unsigned long   seg;
1603         size_t cnt = 0;
1604         for (seg = 0; seg < *nr_segs; seg++) {
1605                 const struct iovec *iv = &iov[seg];
1606
1607                 /*
1608                  * If any segment has a negative length, or the cumulative
1609                  * length ever wraps negative then return -EINVAL.
1610                  */
1611                 cnt += iv->iov_len;
1612                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1613                         return -EINVAL;
1614                 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1615                         continue;
1616                 if (seg == 0)
1617                         return -EFAULT;
1618                 *nr_segs = seg;
1619                 cnt -= iv->iov_len;     /* This segment is no good */
1620                 break;
1621         }
1622         *count = cnt;
1623         return 0;
1624 }
1625 EXPORT_SYMBOL(generic_segment_checks);
1626
1627 /**
1628  * generic_file_aio_read - generic filesystem read routine
1629  * @iocb:       kernel I/O control block
1630  * @iov:        io vector request
1631  * @nr_segs:    number of segments in the iovec
1632  * @pos:        current file position
1633  *
1634  * This is the "read()" routine for all filesystems
1635  * that can use the page cache directly.
1636  */
1637 ssize_t
1638 generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1639                 unsigned long nr_segs, loff_t pos)
1640 {
1641         struct file *filp = iocb->ki_filp;
1642         ssize_t retval;
1643         unsigned long seg = 0;
1644         size_t count;
1645         loff_t *ppos = &iocb->ki_pos;
1646
1647         count = 0;
1648         retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1649         if (retval)
1650                 return retval;
1651
1652         /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1653         if (filp->f_flags & O_DIRECT) {
1654                 loff_t size;
1655                 struct address_space *mapping;
1656                 struct inode *inode;
1657
1658                 mapping = filp->f_mapping;
1659                 inode = mapping->host;
1660                 if (!count)
1661                         goto out; /* skip atime */
1662                 size = i_size_read(inode);
1663                 retval = filemap_write_and_wait_range(mapping, pos,
1664                                         pos + iov_length(iov, nr_segs) - 1);
1665                 if (!retval) {
1666                         retval = mapping->a_ops->direct_IO(READ, iocb,
1667                                                            iov, pos, nr_segs);
1668                 }
1669                 if (retval > 0) {
1670                         *ppos = pos + retval;
1671                         count -= retval;
1672                 }
1673
1674                 /*
1675                  * Btrfs can have a short DIO read if we encounter
1676                  * compressed extents, so if there was an error, or if
1677                  * we've already read everything we wanted to, or if
1678                  * there was a short read because we hit EOF, go ahead
1679                  * and return.  Otherwise fallthrough to buffered io for
1680                  * the rest of the read.
1681                  */
1682                 if (retval < 0 || !count || *ppos >= size) {
1683                         file_accessed(filp);
1684                         goto out;
1685                 }
1686         }
1687
1688         count = retval;
1689         for (seg = 0; seg < nr_segs; seg++) {
1690                 read_descriptor_t desc;
1691                 loff_t offset = 0;
1692
1693                 /*
1694                  * If we did a short DIO read we need to skip the section of the
1695                  * iov that we've already read data into.
1696                  */
1697                 if (count) {
1698                         if (count > iov[seg].iov_len) {
1699                                 count -= iov[seg].iov_len;
1700                                 continue;
1701                         }
1702                         offset = count;
1703                         count = 0;
1704                 }
1705
1706                 desc.written = 0;
1707                 desc.arg.buf = iov[seg].iov_base + offset;
1708                 desc.count = iov[seg].iov_len - offset;
1709                 if (desc.count == 0)
1710                         continue;
1711                 desc.error = 0;
1712                 do_generic_file_read(filp, ppos, &desc);
1713                 retval += desc.written;
1714                 if (desc.error) {
1715                         retval = retval ?: desc.error;
1716                         break;
1717                 }
1718                 if (desc.count > 0)
1719                         break;
1720         }
1721 out:
1722         return retval;
1723 }
1724 EXPORT_SYMBOL(generic_file_aio_read);
1725
1726 #ifdef CONFIG_MMU
1727 /**
1728  * page_cache_read - adds requested page to the page cache if not already there
1729  * @file:       file to read
1730  * @offset:     page index
1731  *
1732  * This adds the requested page to the page cache if it isn't already there,
1733  * and schedules an I/O to read in its contents from disk.
1734  */
1735 static int page_cache_read(struct file *file, pgoff_t offset)
1736 {
1737         struct address_space *mapping = file->f_mapping;
1738         struct page *page; 
1739         int ret;
1740
1741         do {
1742                 page = page_cache_alloc_cold(mapping);
1743                 if (!page)
1744                         return -ENOMEM;
1745
1746                 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1747                 if (ret == 0)
1748                         ret = mapping->a_ops->readpage(file, page);
1749                 else if (ret == -EEXIST)
1750                         ret = 0; /* losing race to add is OK */
1751
1752                 page_cache_release(page);
1753
1754         } while (ret == AOP_TRUNCATED_PAGE);
1755                 
1756         return ret;
1757 }
1758
1759 #define MMAP_LOTSAMISS  (100)
1760
1761 /*
1762  * Synchronous readahead happens when we don't even find
1763  * a page in the page cache at all.
1764  */
1765 static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1766                                    struct file_ra_state *ra,
1767                                    struct file *file,
1768                                    pgoff_t offset)
1769 {
1770         unsigned long ra_pages;
1771         struct address_space *mapping = file->f_mapping;
1772
1773         /* If we don't want any read-ahead, don't bother */
1774         if (vma->vm_flags & VM_RAND_READ)
1775                 return;
1776         if (!ra->ra_pages)
1777                 return;
1778
1779         if (vma->vm_flags & VM_SEQ_READ) {
1780                 page_cache_sync_readahead(mapping, ra, file, offset,
1781                                           ra->ra_pages);
1782                 return;
1783         }
1784
1785         /* Avoid banging the cache line if not needed */
1786         if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
1787                 ra->mmap_miss++;
1788
1789         /*
1790          * Do we miss much more than hit in this file? If so,
1791          * stop bothering with read-ahead. It will only hurt.
1792          */
1793         if (ra->mmap_miss > MMAP_LOTSAMISS)
1794                 return;
1795
1796         /*
1797          * mmap read-around
1798          */
1799         ra_pages = max_sane_readahead(ra->ra_pages);
1800         ra->start = max_t(long, 0, offset - ra_pages / 2);
1801         ra->size = ra_pages;
1802         ra->async_size = ra_pages / 4;
1803         ra_submit(ra, mapping, file);
1804 }
1805
1806 /*
1807  * Asynchronous readahead happens when we find the page and PG_readahead,
1808  * so we want to possibly extend the readahead further..
1809  */
1810 static void do_async_mmap_readahead(struct vm_area_struct *vma,
1811                                     struct file_ra_state *ra,
1812                                     struct file *file,
1813                                     struct page *page,
1814                                     pgoff_t offset)
1815 {
1816         struct address_space *mapping = file->f_mapping;
1817
1818         /* If we don't want any read-ahead, don't bother */
1819         if (vma->vm_flags & VM_RAND_READ)
1820                 return;
1821         if (ra->mmap_miss > 0)
1822                 ra->mmap_miss--;
1823         if (PageReadahead(page))
1824                 page_cache_async_readahead(mapping, ra, file,
1825                                            page, offset, ra->ra_pages);
1826 }
1827
1828 /**
1829  * filemap_fault - read in file data for page fault handling
1830  * @vma:        vma in which the fault was taken
1831  * @vmf:        struct vm_fault containing details of the fault
1832  *
1833  * filemap_fault() is invoked via the vma operations vector for a
1834  * mapped memory region to read in file data during a page fault.
1835  *
1836  * The goto's are kind of ugly, but this streamlines the normal case of having
1837  * it in the page cache, and handles the special cases reasonably without
1838  * having a lot of duplicated code.
1839  */
1840 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1841 {
1842         int error;
1843         struct file *file = vma->vm_file;
1844         struct address_space *mapping = file->f_mapping;
1845         struct file_ra_state *ra = &file->f_ra;
1846         struct inode *inode = mapping->host;
1847         pgoff_t offset = vmf->pgoff;
1848         struct page *page;
1849         pgoff_t size;
1850         int ret = 0;
1851
1852         size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1853         if (offset >= size)
1854                 return VM_FAULT_SIGBUS;
1855
1856         /*
1857          * Do we have something in the page cache already?
1858          */
1859         page = find_get_page(mapping, offset);
1860         if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
1861                 /*
1862                  * We found the page, so try async readahead before
1863                  * waiting for the lock.
1864                  */
1865                 do_async_mmap_readahead(vma, ra, file, page, offset);
1866         } else if (!page) {
1867                 /* No page in the page cache at all */
1868                 do_sync_mmap_readahead(vma, ra, file, offset);
1869                 count_vm_event(PGMAJFAULT);
1870                 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1871                 ret = VM_FAULT_MAJOR;
1872 retry_find:
1873                 page = find_get_page(mapping, offset);
1874                 if (!page)
1875                         goto no_cached_page;
1876         }
1877
1878         if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
1879                 page_cache_release(page);
1880                 return ret | VM_FAULT_RETRY;
1881         }
1882
1883         /* Did it get truncated? */
1884         if (unlikely(page->mapping != mapping)) {
1885                 unlock_page(page);
1886                 put_page(page);
1887                 goto retry_find;
1888         }
1889         VM_BUG_ON_PAGE(page->index != offset, page);
1890
1891         /*
1892          * We have a locked page in the page cache, now we need to check
1893          * that it's up-to-date. If not, it is going to be due to an error.
1894          */
1895         if (unlikely(!PageUptodate(page)))
1896                 goto page_not_uptodate;
1897
1898         /*
1899          * Found the page and have a reference on it.
1900          * We must recheck i_size under page lock.
1901          */
1902         size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1903         if (unlikely(offset >= size)) {
1904                 unlock_page(page);
1905                 page_cache_release(page);
1906                 return VM_FAULT_SIGBUS;
1907         }
1908
1909         vmf->page = page;
1910         return ret | VM_FAULT_LOCKED;
1911
1912 no_cached_page:
1913         /*
1914          * We're only likely to ever get here if MADV_RANDOM is in
1915          * effect.
1916          */
1917         error = page_cache_read(file, offset);
1918
1919         /*
1920          * The page we want has now been added to the page cache.
1921          * In the unlikely event that someone removed it in the
1922          * meantime, we'll just come back here and read it again.
1923          */
1924         if (error >= 0)
1925                 goto retry_find;
1926
1927         /*
1928          * An error return from page_cache_read can result if the
1929          * system is low on memory, or a problem occurs while trying
1930          * to schedule I/O.
1931          */
1932         if (error == -ENOMEM)
1933                 return VM_FAULT_OOM;
1934         return VM_FAULT_SIGBUS;
1935
1936 page_not_uptodate:
1937         /*
1938          * Umm, take care of errors if the page isn't up-to-date.
1939          * Try to re-read it _once_. We do this synchronously,
1940          * because there really aren't any performance issues here
1941          * and we need to check for errors.
1942          */
1943         ClearPageError(page);
1944         error = mapping->a_ops->readpage(file, page);
1945         if (!error) {
1946                 wait_on_page_locked(page);
1947                 if (!PageUptodate(page))
1948                         error = -EIO;
1949         }
1950         page_cache_release(page);
1951
1952         if (!error || error == AOP_TRUNCATED_PAGE)
1953                 goto retry_find;
1954
1955         /* Things didn't work out. Return zero to tell the mm layer so. */
1956         shrink_readahead_size_eio(file, ra);
1957         return VM_FAULT_SIGBUS;
1958 }
1959 EXPORT_SYMBOL(filemap_fault);
1960
1961 int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1962 {
1963         struct page *page = vmf->page;
1964         struct inode *inode = file_inode(vma->vm_file);
1965         int ret = VM_FAULT_LOCKED;
1966
1967         sb_start_pagefault(inode->i_sb);
1968         file_update_time(vma->vm_file);
1969         lock_page(page);
1970         if (page->mapping != inode->i_mapping) {
1971                 unlock_page(page);
1972                 ret = VM_FAULT_NOPAGE;
1973                 goto out;
1974         }
1975         /*
1976          * We mark the page dirty already here so that when freeze is in
1977          * progress, we are guaranteed that writeback during freezing will
1978          * see the dirty page and writeprotect it again.
1979          */
1980         set_page_dirty(page);
1981         wait_for_stable_page(page);
1982 out:
1983         sb_end_pagefault(inode->i_sb);
1984         return ret;
1985 }
1986 EXPORT_SYMBOL(filemap_page_mkwrite);
1987
1988 const struct vm_operations_struct generic_file_vm_ops = {
1989         .fault          = filemap_fault,
1990         .page_mkwrite   = filemap_page_mkwrite,
1991         .remap_pages    = generic_file_remap_pages,
1992 };
1993
1994 /* This is used for a general mmap of a disk file */
1995
1996 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1997 {
1998         struct address_space *mapping = file->f_mapping;
1999
2000         if (!mapping->a_ops->readpage)
2001                 return -ENOEXEC;
2002         file_accessed(file);
2003         vma->vm_ops = &generic_file_vm_ops;
2004         return 0;
2005 }
2006
2007 /*
2008  * This is for filesystems which do not implement ->writepage.
2009  */
2010 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2011 {
2012         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2013                 return -EINVAL;
2014         return generic_file_mmap(file, vma);
2015 }
2016 #else
2017 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2018 {
2019         return -ENOSYS;
2020 }
2021 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2022 {
2023         return -ENOSYS;
2024 }
2025 #endif /* CONFIG_MMU */
2026
2027 EXPORT_SYMBOL(generic_file_mmap);
2028 EXPORT_SYMBOL(generic_file_readonly_mmap);
2029
2030 static struct page *wait_on_page_read(struct page *page)
2031 {
2032         if (!IS_ERR(page)) {
2033                 wait_on_page_locked(page);
2034                 if (!PageUptodate(page)) {
2035                         page_cache_release(page);
2036                         page = ERR_PTR(-EIO);
2037                 }
2038         }
2039         return page;
2040 }
2041
2042 static struct page *__read_cache_page(struct address_space *mapping,
2043                                 pgoff_t index,
2044                                 int (*filler)(void *, struct page *),
2045                                 void *data,
2046                                 gfp_t gfp)
2047 {
2048         struct page *page;
2049         int err;
2050 repeat:
2051         page = find_get_page(mapping, index);
2052         if (!page) {
2053                 page = __page_cache_alloc(gfp | __GFP_COLD);
2054                 if (!page)
2055                         return ERR_PTR(-ENOMEM);
2056                 err = add_to_page_cache_lru(page, mapping, index, gfp);
2057                 if (unlikely(err)) {
2058                         page_cache_release(page);
2059                         if (err == -EEXIST)
2060                                 goto repeat;
2061                         /* Presumably ENOMEM for radix tree node */
2062                         return ERR_PTR(err);
2063                 }
2064                 err = filler(data, page);
2065                 if (err < 0) {
2066                         page_cache_release(page);
2067                         page = ERR_PTR(err);
2068                 } else {
2069                         page = wait_on_page_read(page);
2070                 }
2071         }
2072         return page;
2073 }
2074
2075 static struct page *do_read_cache_page(struct address_space *mapping,
2076                                 pgoff_t index,
2077                                 int (*filler)(void *, struct page *),
2078                                 void *data,
2079                                 gfp_t gfp)
2080
2081 {
2082         struct page *page;
2083         int err;
2084
2085 retry:
2086         page = __read_cache_page(mapping, index, filler, data, gfp);
2087         if (IS_ERR(page))
2088                 return page;
2089         if (PageUptodate(page))
2090                 goto out;
2091
2092         lock_page(page);
2093         if (!page->mapping) {
2094                 unlock_page(page);
2095                 page_cache_release(page);
2096                 goto retry;
2097         }
2098         if (PageUptodate(page)) {
2099                 unlock_page(page);
2100                 goto out;
2101         }
2102         err = filler(data, page);
2103         if (err < 0) {
2104                 page_cache_release(page);
2105                 return ERR_PTR(err);
2106         } else {
2107                 page = wait_on_page_read(page);
2108                 if (IS_ERR(page))
2109                         return page;
2110         }
2111 out:
2112         mark_page_accessed(page);
2113         return page;
2114 }
2115
2116 /**
2117  * read_cache_page - read into page cache, fill it if needed
2118  * @mapping:    the page's address_space
2119  * @index:      the page index
2120  * @filler:     function to perform the read
2121  * @data:       first arg to filler(data, page) function, often left as NULL
2122  *
2123  * Read into the page cache. If a page already exists, and PageUptodate() is
2124  * not set, try to fill the page and wait for it to become unlocked.
2125  *
2126  * If the page does not get brought uptodate, return -EIO.
2127  */
2128 struct page *read_cache_page(struct address_space *mapping,
2129                                 pgoff_t index,
2130                                 int (*filler)(void *, struct page *),
2131                                 void *data)
2132 {
2133         return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2134 }
2135 EXPORT_SYMBOL(read_cache_page);
2136
2137 /**
2138  * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2139  * @mapping:    the page's address_space
2140  * @index:      the page index
2141  * @gfp:        the page allocator flags to use if allocating
2142  *
2143  * This is the same as "read_mapping_page(mapping, index, NULL)", but with
2144  * any new page allocations done using the specified allocation flags.
2145  *
2146  * If the page does not get brought uptodate, return -EIO.
2147  */
2148 struct page *read_cache_page_gfp(struct address_space *mapping,
2149                                 pgoff_t index,
2150                                 gfp_t gfp)
2151 {
2152         filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2153
2154         return do_read_cache_page(mapping, index, filler, NULL, gfp);
2155 }
2156 EXPORT_SYMBOL(read_cache_page_gfp);
2157
2158 static size_t __iovec_copy_from_user_inatomic(char *vaddr,
2159                         const struct iovec *iov, size_t base, size_t bytes)
2160 {
2161         size_t copied = 0, left = 0;
2162
2163         while (bytes) {
2164                 char __user *buf = iov->iov_base + base;
2165                 int copy = min(bytes, iov->iov_len - base);
2166
2167                 base = 0;
2168                 left = __copy_from_user_inatomic(vaddr, buf, copy);
2169                 copied += copy;
2170                 bytes -= copy;
2171                 vaddr += copy;
2172                 iov++;
2173
2174                 if (unlikely(left))
2175                         break;
2176         }
2177         return copied - left;
2178 }
2179
2180 /*
2181  * Copy as much as we can into the page and return the number of bytes which
2182  * were successfully copied.  If a fault is encountered then return the number of
2183  * bytes which were copied.
2184  */
2185 size_t iov_iter_copy_from_user_atomic(struct page *page,
2186                 struct iov_iter *i, unsigned long offset, size_t bytes)
2187 {
2188         char *kaddr;
2189         size_t copied;
2190
2191         kaddr = kmap_atomic(page);
2192         if (likely(i->nr_segs == 1)) {
2193                 int left;
2194                 char __user *buf = i->iov->iov_base + i->iov_offset;
2195                 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
2196                 copied = bytes - left;
2197         } else {
2198                 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2199                                                 i->iov, i->iov_offset, bytes);
2200         }
2201         kunmap_atomic(kaddr);
2202
2203         return copied;
2204 }
2205 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
2206
2207 /*
2208  * This has the same sideeffects and return value as
2209  * iov_iter_copy_from_user_atomic().
2210  * The difference is that it attempts to resolve faults.
2211  * Page must not be locked.
2212  */
2213 size_t iov_iter_copy_from_user(struct page *page,
2214                 struct iov_iter *i, unsigned long offset, size_t bytes)
2215 {
2216         char *kaddr;
2217         size_t copied;
2218
2219         kaddr = kmap(page);
2220         if (likely(i->nr_segs == 1)) {
2221                 int left;
2222                 char __user *buf = i->iov->iov_base + i->iov_offset;
2223                 left = __copy_from_user(kaddr + offset, buf, bytes);
2224                 copied = bytes - left;
2225         } else {
2226                 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2227                                                 i->iov, i->iov_offset, bytes);
2228         }
2229         kunmap(page);
2230         return copied;
2231 }
2232 EXPORT_SYMBOL(iov_iter_copy_from_user);
2233
2234 void iov_iter_advance(struct iov_iter *i, size_t bytes)
2235 {
2236         BUG_ON(i->count < bytes);
2237
2238         if (likely(i->nr_segs == 1)) {
2239                 i->iov_offset += bytes;
2240                 i->count -= bytes;
2241         } else {
2242                 const struct iovec *iov = i->iov;
2243                 size_t base = i->iov_offset;
2244                 unsigned long nr_segs = i->nr_segs;
2245
2246                 /*
2247                  * The !iov->iov_len check ensures we skip over unlikely
2248                  * zero-length segments (without overruning the iovec).
2249                  */
2250                 while (bytes || unlikely(i->count && !iov->iov_len)) {
2251                         int copy;
2252
2253                         copy = min(bytes, iov->iov_len - base);
2254                         BUG_ON(!i->count || i->count < copy);
2255                         i->count -= copy;
2256                         bytes -= copy;
2257                         base += copy;
2258                         if (iov->iov_len == base) {
2259                                 iov++;
2260                                 nr_segs--;
2261                                 base = 0;
2262                         }
2263                 }
2264                 i->iov = iov;
2265                 i->iov_offset = base;
2266                 i->nr_segs = nr_segs;
2267         }
2268 }
2269 EXPORT_SYMBOL(iov_iter_advance);
2270
2271 /*
2272  * Fault in the first iovec of the given iov_iter, to a maximum length
2273  * of bytes. Returns 0 on success, or non-zero if the memory could not be
2274  * accessed (ie. because it is an invalid address).
2275  *
2276  * writev-intensive code may want this to prefault several iovecs -- that
2277  * would be possible (callers must not rely on the fact that _only_ the
2278  * first iovec will be faulted with the current implementation).
2279  */
2280 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
2281 {
2282         char __user *buf = i->iov->iov_base + i->iov_offset;
2283         bytes = min(bytes, i->iov->iov_len - i->iov_offset);
2284         return fault_in_pages_readable(buf, bytes);
2285 }
2286 EXPORT_SYMBOL(iov_iter_fault_in_readable);
2287
2288 /*
2289  * Return the count of just the current iov_iter segment.
2290  */
2291 size_t iov_iter_single_seg_count(const struct iov_iter *i)
2292 {
2293         const struct iovec *iov = i->iov;
2294         if (i->nr_segs == 1)
2295                 return i->count;
2296         else
2297                 return min(i->count, iov->iov_len - i->iov_offset);
2298 }
2299 EXPORT_SYMBOL(iov_iter_single_seg_count);
2300
2301 /*
2302  * Performs necessary checks before doing a write
2303  *
2304  * Can adjust writing position or amount of bytes to write.
2305  * Returns appropriate error code that caller should return or
2306  * zero in case that write should be allowed.
2307  */
2308 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
2309 {
2310         struct inode *inode = file->f_mapping->host;
2311         unsigned long limit = rlimit(RLIMIT_FSIZE);
2312
2313         if (unlikely(*pos < 0))
2314                 return -EINVAL;
2315
2316         if (!isblk) {
2317                 /* FIXME: this is for backwards compatibility with 2.4 */
2318                 if (file->f_flags & O_APPEND)
2319                         *pos = i_size_read(inode);
2320
2321                 if (limit != RLIM_INFINITY) {
2322                         if (*pos >= limit) {
2323                                 send_sig(SIGXFSZ, current, 0);
2324                                 return -EFBIG;
2325                         }
2326                         if (*count > limit - (typeof(limit))*pos) {
2327                                 *count = limit - (typeof(limit))*pos;
2328                         }
2329                 }
2330         }
2331
2332         /*
2333          * LFS rule
2334          */
2335         if (unlikely(*pos + *count > MAX_NON_LFS &&
2336                                 !(file->f_flags & O_LARGEFILE))) {
2337                 if (*pos >= MAX_NON_LFS) {
2338                         return -EFBIG;
2339                 }
2340                 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
2341                         *count = MAX_NON_LFS - (unsigned long)*pos;
2342                 }
2343         }
2344
2345         /*
2346          * Are we about to exceed the fs block limit ?
2347          *
2348          * If we have written data it becomes a short write.  If we have
2349          * exceeded without writing data we send a signal and return EFBIG.
2350          * Linus frestrict idea will clean these up nicely..
2351          */
2352         if (likely(!isblk)) {
2353                 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
2354                         if (*count || *pos > inode->i_sb->s_maxbytes) {
2355                                 return -EFBIG;
2356                         }
2357                         /* zero-length writes at ->s_maxbytes are OK */
2358                 }
2359
2360                 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
2361                         *count = inode->i_sb->s_maxbytes - *pos;
2362         } else {
2363 #ifdef CONFIG_BLOCK
2364                 loff_t isize;
2365                 if (bdev_read_only(I_BDEV(inode)))
2366                         return -EPERM;
2367                 isize = i_size_read(inode);
2368                 if (*pos >= isize) {
2369                         if (*count || *pos > isize)
2370                                 return -ENOSPC;
2371                 }
2372
2373                 if (*pos + *count > isize)
2374                         *count = isize - *pos;
2375 #else
2376                 return -EPERM;
2377 #endif
2378         }
2379         return 0;
2380 }
2381 EXPORT_SYMBOL(generic_write_checks);
2382
2383 int pagecache_write_begin(struct file *file, struct address_space *mapping,
2384                                 loff_t pos, unsigned len, unsigned flags,
2385                                 struct page **pagep, void **fsdata)
2386 {
2387         const struct address_space_operations *aops = mapping->a_ops;
2388
2389         return aops->write_begin(file, mapping, pos, len, flags,
2390                                                         pagep, fsdata);
2391 }
2392 EXPORT_SYMBOL(pagecache_write_begin);
2393
2394 int pagecache_write_end(struct file *file, struct address_space *mapping,
2395                                 loff_t pos, unsigned len, unsigned copied,
2396                                 struct page *page, void *fsdata)
2397 {
2398         const struct address_space_operations *aops = mapping->a_ops;
2399
2400         mark_page_accessed(page);
2401         return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
2402 }
2403 EXPORT_SYMBOL(pagecache_write_end);
2404
2405 ssize_t
2406 generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
2407                 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
2408                 size_t count, size_t ocount)
2409 {
2410         struct file     *file = iocb->ki_filp;
2411         struct address_space *mapping = file->f_mapping;
2412         struct inode    *inode = mapping->host;
2413         ssize_t         written;
2414         size_t          write_len;
2415         pgoff_t         end;
2416
2417         if (count != ocount)
2418                 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
2419
2420         write_len = iov_length(iov, *nr_segs);
2421         end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
2422
2423         written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
2424         if (written)
2425                 goto out;
2426
2427         /*
2428          * After a write we want buffered reads to be sure to go to disk to get
2429          * the new data.  We invalidate clean cached page from the region we're
2430          * about to write.  We do this *before* the write so that we can return
2431          * without clobbering -EIOCBQUEUED from ->direct_IO().
2432          */
2433         if (mapping->nrpages) {
2434                 written = invalidate_inode_pages2_range(mapping,
2435                                         pos >> PAGE_CACHE_SHIFT, end);
2436                 /*
2437                  * If a page can not be invalidated, return 0 to fall back
2438                  * to buffered write.
2439                  */
2440                 if (written) {
2441                         if (written == -EBUSY)
2442                                 return 0;
2443                         goto out;
2444                 }
2445         }
2446
2447         written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs);
2448
2449         /*
2450          * Finally, try again to invalidate clean pages which might have been
2451          * cached by non-direct readahead, or faulted in by get_user_pages()
2452          * if the source of the write was an mmap'ed region of the file
2453          * we're writing.  Either one is a pretty crazy thing to do,
2454          * so we don't support it 100%.  If this invalidation
2455          * fails, tough, the write still worked...
2456          */
2457         if (mapping->nrpages) {
2458                 invalidate_inode_pages2_range(mapping,
2459                                               pos >> PAGE_CACHE_SHIFT, end);
2460         }
2461
2462         if (written > 0) {
2463                 pos += written;
2464                 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2465                         i_size_write(inode, pos);
2466                         mark_inode_dirty(inode);
2467                 }
2468                 *ppos = pos;
2469         }
2470 out:
2471         return written;
2472 }
2473 EXPORT_SYMBOL(generic_file_direct_write);
2474
2475 /*
2476  * Find or create a page at the given pagecache position. Return the locked
2477  * page. This function is specifically for buffered writes.
2478  */
2479 struct page *grab_cache_page_write_begin(struct address_space *mapping,
2480                                         pgoff_t index, unsigned flags)
2481 {
2482         int status;
2483         gfp_t gfp_mask;
2484         struct page *page;
2485         gfp_t gfp_notmask = 0;
2486
2487         gfp_mask = mapping_gfp_mask(mapping);
2488         if (mapping_cap_account_dirty(mapping))
2489                 gfp_mask |= __GFP_WRITE;
2490         if (flags & AOP_FLAG_NOFS)
2491                 gfp_notmask = __GFP_FS;
2492 repeat:
2493         page = find_lock_page(mapping, index);
2494         if (page)
2495                 goto found;
2496
2497         page = __page_cache_alloc(gfp_mask & ~gfp_notmask);
2498         if (!page)
2499                 return NULL;
2500         status = add_to_page_cache_lru(page, mapping, index,
2501                                                 GFP_KERNEL & ~gfp_notmask);
2502         if (unlikely(status)) {
2503                 page_cache_release(page);
2504                 if (status == -EEXIST)
2505                         goto repeat;
2506                 return NULL;
2507         }
2508 found:
2509         wait_for_stable_page(page);
2510         return page;
2511 }
2512 EXPORT_SYMBOL(grab_cache_page_write_begin);
2513
2514 static ssize_t generic_perform_write(struct file *file,
2515                                 struct iov_iter *i, loff_t pos)
2516 {
2517         struct address_space *mapping = file->f_mapping;
2518         const struct address_space_operations *a_ops = mapping->a_ops;
2519         long status = 0;
2520         ssize_t written = 0;
2521         unsigned int flags = 0;
2522
2523         /*
2524          * Copies from kernel address space cannot fail (NFSD is a big user).
2525          */
2526         if (segment_eq(get_fs(), KERNEL_DS))
2527                 flags |= AOP_FLAG_UNINTERRUPTIBLE;
2528
2529         do {
2530                 struct page *page;
2531                 unsigned long offset;   /* Offset into pagecache page */
2532                 unsigned long bytes;    /* Bytes to write to page */
2533                 size_t copied;          /* Bytes copied from user */
2534                 void *fsdata;
2535
2536                 offset = (pos & (PAGE_CACHE_SIZE - 1));
2537                 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2538                                                 iov_iter_count(i));
2539
2540 again:
2541                 /*
2542                  * Bring in the user page that we will copy from _first_.
2543                  * Otherwise there's a nasty deadlock on copying from the
2544                  * same page as we're writing to, without it being marked
2545                  * up-to-date.
2546                  *
2547                  * Not only is this an optimisation, but it is also required
2548                  * to check that the address is actually valid, when atomic
2549                  * usercopies are used, below.
2550                  */
2551                 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2552                         status = -EFAULT;
2553                         break;
2554                 }
2555
2556                 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
2557                                                 &page, &fsdata);
2558                 if (unlikely(status))
2559                         break;
2560
2561                 if (mapping_writably_mapped(mapping))
2562                         flush_dcache_page(page);
2563
2564                 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
2565                 flush_dcache_page(page);
2566
2567                 mark_page_accessed(page);
2568                 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2569                                                 page, fsdata);
2570                 if (unlikely(status < 0))
2571                         break;
2572                 copied = status;
2573
2574                 cond_resched();
2575
2576                 iov_iter_advance(i, copied);
2577                 if (unlikely(copied == 0)) {
2578                         /*
2579                          * If we were unable to copy any data at all, we must
2580                          * fall back to a single segment length write.
2581                          *
2582                          * If we didn't fallback here, we could livelock
2583                          * because not all segments in the iov can be copied at
2584                          * once without a pagefault.
2585                          */
2586                         bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2587                                                 iov_iter_single_seg_count(i));
2588                         goto again;
2589                 }
2590                 pos += copied;
2591                 written += copied;
2592
2593                 balance_dirty_pages_ratelimited(mapping);
2594                 if (fatal_signal_pending(current)) {
2595                         status = -EINTR;
2596                         break;
2597                 }
2598         } while (iov_iter_count(i));
2599
2600         return written ? written : status;
2601 }
2602
2603 ssize_t
2604 generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2605                 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2606                 size_t count, ssize_t written)
2607 {
2608         struct file *file = iocb->ki_filp;
2609         ssize_t status;
2610         struct iov_iter i;
2611
2612         iov_iter_init(&i, iov, nr_segs, count, written);
2613         status = generic_perform_write(file, &i, pos);
2614
2615         if (likely(status >= 0)) {
2616                 written += status;
2617                 *ppos = pos + status;
2618         }
2619         
2620         return written ? written : status;
2621 }
2622 EXPORT_SYMBOL(generic_file_buffered_write);
2623
2624 /**
2625  * __generic_file_aio_write - write data to a file
2626  * @iocb:       IO state structure (file, offset, etc.)
2627  * @iov:        vector with data to write
2628  * @nr_segs:    number of segments in the vector
2629  * @ppos:       position where to write
2630  *
2631  * This function does all the work needed for actually writing data to a
2632  * file. It does all basic checks, removes SUID from the file, updates
2633  * modification times and calls proper subroutines depending on whether we
2634  * do direct IO or a standard buffered write.
2635  *
2636  * It expects i_mutex to be grabbed unless we work on a block device or similar
2637  * object which does not need locking at all.
2638  *
2639  * This function does *not* take care of syncing data in case of O_SYNC write.
2640  * A caller has to handle it. This is mainly due to the fact that we want to
2641  * avoid syncing under i_mutex.
2642  */
2643 ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2644                                  unsigned long nr_segs, loff_t *ppos)
2645 {
2646         struct file *file = iocb->ki_filp;
2647         struct address_space * mapping = file->f_mapping;
2648         size_t ocount;          /* original count */
2649         size_t count;           /* after file limit checks */
2650         struct inode    *inode = mapping->host;
2651         loff_t          pos;
2652         ssize_t         written;
2653         ssize_t         err;
2654
2655         ocount = 0;
2656         err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2657         if (err)
2658                 return err;
2659
2660         count = ocount;
2661         pos = *ppos;
2662
2663         /* We can write back this queue in page reclaim */
2664         current->backing_dev_info = mapping->backing_dev_info;
2665         written = 0;
2666
2667         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2668         if (err)
2669                 goto out;
2670
2671         if (count == 0)
2672                 goto out;
2673
2674         err = file_remove_suid(file);
2675         if (err)
2676                 goto out;
2677
2678         err = file_update_time(file);
2679         if (err)
2680                 goto out;
2681
2682         /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2683         if (unlikely(file->f_flags & O_DIRECT)) {
2684                 loff_t endbyte;
2685                 ssize_t written_buffered;
2686
2687                 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
2688                                                         ppos, count, ocount);
2689                 if (written < 0 || written == count)
2690                         goto out;
2691                 /*
2692                  * direct-io write to a hole: fall through to buffered I/O
2693                  * for completing the rest of the request.
2694                  */
2695                 pos += written;
2696                 count -= written;
2697                 written_buffered = generic_file_buffered_write(iocb, iov,
2698                                                 nr_segs, pos, ppos, count,
2699                                                 written);
2700                 /*
2701                  * If generic_file_buffered_write() retuned a synchronous error
2702                  * then we want to return the number of bytes which were
2703                  * direct-written, or the error code if that was zero.  Note
2704                  * that this differs from normal direct-io semantics, which
2705                  * will return -EFOO even if some bytes were written.
2706                  */
2707                 if (written_buffered < 0) {
2708                         err = written_buffered;
2709                         goto out;
2710                 }
2711
2712                 /*
2713                  * We need to ensure that the page cache pages are written to
2714                  * disk and invalidated to preserve the expected O_DIRECT
2715                  * semantics.
2716                  */
2717                 endbyte = pos + written_buffered - written - 1;
2718                 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
2719                 if (err == 0) {
2720                         written = written_buffered;
2721                         invalidate_mapping_pages(mapping,
2722                                                  pos >> PAGE_CACHE_SHIFT,
2723                                                  endbyte >> PAGE_CACHE_SHIFT);
2724                 } else {
2725                         /*
2726                          * We don't know how much we wrote, so just return
2727                          * the number of bytes which were direct-written
2728                          */
2729                 }
2730         } else {
2731                 written = generic_file_buffered_write(iocb, iov, nr_segs,
2732                                 pos, ppos, count, written);
2733         }
2734 out:
2735         current->backing_dev_info = NULL;
2736         return written ? written : err;
2737 }
2738 EXPORT_SYMBOL(__generic_file_aio_write);
2739
2740 /**
2741  * generic_file_aio_write - write data to a file
2742  * @iocb:       IO state structure
2743  * @iov:        vector with data to write
2744  * @nr_segs:    number of segments in the vector
2745  * @pos:        position in file where to write
2746  *
2747  * This is a wrapper around __generic_file_aio_write() to be used by most
2748  * filesystems. It takes care of syncing the file in case of O_SYNC file
2749  * and acquires i_mutex as needed.
2750  */
2751 ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2752                 unsigned long nr_segs, loff_t pos)
2753 {
2754         struct file *file = iocb->ki_filp;
2755         struct inode *inode = file->f_mapping->host;
2756         ssize_t ret;
2757
2758         BUG_ON(iocb->ki_pos != pos);
2759
2760         mutex_lock(&inode->i_mutex);
2761         ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
2762         mutex_unlock(&inode->i_mutex);
2763
2764         if (ret > 0) {
2765                 ssize_t err;
2766
2767                 err = generic_write_sync(file, iocb->ki_pos - ret, ret);
2768                 if (err < 0)
2769                         ret = err;
2770         }
2771         return ret;
2772 }
2773 EXPORT_SYMBOL(generic_file_aio_write);
2774
2775 /**
2776  * try_to_release_page() - release old fs-specific metadata on a page
2777  *
2778  * @page: the page which the kernel is trying to free
2779  * @gfp_mask: memory allocation flags (and I/O mode)
2780  *
2781  * The address_space is to try to release any data against the page
2782  * (presumably at page->private).  If the release was successful, return `1'.
2783  * Otherwise return zero.
2784  *
2785  * This may also be called if PG_fscache is set on a page, indicating that the
2786  * page is known to the local caching routines.
2787  *
2788  * The @gfp_mask argument specifies whether I/O may be performed to release
2789  * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
2790  *
2791  */
2792 int try_to_release_page(struct page *page, gfp_t gfp_mask)
2793 {
2794         struct address_space * const mapping = page->mapping;
2795
2796         BUG_ON(!PageLocked(page));
2797         if (PageWriteback(page))
2798                 return 0;
2799
2800         if (mapping && mapping->a_ops->releasepage)
2801                 return mapping->a_ops->releasepage(page, gfp_mask);
2802         return try_to_free_buffers(page);
2803 }
2804
2805 EXPORT_SYMBOL(try_to_release_page);