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