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