7c4dbca26cd3e6b29e987f7ec472a0803519faf9
[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                              int nonblock)
690 {
691         struct inode *inode = mapping->host;
692         unsigned long index, end_index, offset;
693         loff_t isize;
694         struct page *cached_page;
695         int error;
696         struct file_ra_state ra = *_ra;
697
698         cached_page = NULL;
699         index = *ppos >> PAGE_CACHE_SHIFT;
700         offset = *ppos & ~PAGE_CACHE_MASK;
701
702         isize = i_size_read(inode);
703         if (!isize)
704                 goto out;
705
706         end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
707         for (;;) {
708                 struct page *page;
709                 unsigned long nr, ret;
710
711                 /* nr is the maximum number of bytes to copy from this page */
712                 nr = PAGE_CACHE_SIZE;
713                 if (index >= end_index) {
714                         if (index > end_index)
715                                 goto out;
716                         nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
717                         if (nr <= offset) {
718                                 goto out;
719                         }
720                 }
721                 nr = nr - offset;
722
723                 cond_resched();
724                 page_cache_readahead(mapping, &ra, filp, index);
725
726 find_page:
727                 page = find_get_page(mapping, index);
728                 if (unlikely(page == NULL)) {
729                         if (nonblock) {
730                                 desc->error = -EWOULDBLOCKIO;
731                                 break;
732                         }
733                         handle_ra_miss(mapping, &ra, index);
734                         goto no_cached_page;
735                 }
736                 if (!PageUptodate(page)) {
737                         if (nonblock) {
738                                 page_cache_release(page);
739                                 desc->error = -EWOULDBLOCKIO;
740                                 break;
741                         }
742                         goto page_not_up_to_date;
743                 }
744 page_ok:
745
746                 /* If users can be writing to this page using arbitrary
747                  * virtual addresses, take care about potential aliasing
748                  * before reading the page on the kernel side.
749                  */
750                 if (mapping_writably_mapped(mapping))
751                         flush_dcache_page(page);
752
753                 /*
754                  * Mark the page accessed if we read the beginning.
755                  */
756                 if (!offset)
757                         mark_page_accessed(page);
758
759                 /*
760                  * Ok, we have the page, and it's up-to-date, so
761                  * now we can copy it to user space...
762                  *
763                  * The actor routine returns how many bytes were actually used..
764                  * NOTE! This may not be the same as how much of a user buffer
765                  * we filled up (we may be padding etc), so we can only update
766                  * "pos" here (the actor routine has to update the user buffer
767                  * pointers and the remaining count).
768                  */
769                 ret = actor(desc, page, offset, nr);
770                 offset += ret;
771                 index += offset >> PAGE_CACHE_SHIFT;
772                 offset &= ~PAGE_CACHE_MASK;
773
774                 page_cache_release(page);
775                 if (ret == nr && desc->count)
776                         continue;
777                 goto out;
778
779 page_not_up_to_date:
780                 /* Get exclusive access to the page ... */
781                 lock_page(page);
782
783                 /* Did it get unhashed before we got the lock? */
784                 if (!page->mapping) {
785                         unlock_page(page);
786                         page_cache_release(page);
787                         continue;
788                 }
789
790                 /* Did somebody else fill it already? */
791                 if (PageUptodate(page)) {
792                         unlock_page(page);
793                         goto page_ok;
794                 }
795
796 readpage:
797                 /* Start the actual read. The read will unlock the page. */
798                 error = mapping->a_ops->readpage(filp, page);
799
800                 if (unlikely(error))
801                         goto readpage_error;
802
803                 if (!PageUptodate(page)) {
804                         wait_on_page_locked(page);
805                         if (!PageUptodate(page)) {
806                                 error = -EIO;
807                                 goto readpage_error;
808                         }
809                 }
810
811                 /*
812                  * i_size must be checked after we have done ->readpage.
813                  *
814                  * Checking i_size after the readpage allows us to calculate
815                  * the correct value for "nr", which means the zero-filled
816                  * part of the page is not copied back to userspace (unless
817                  * another truncate extends the file - this is desired though).
818                  */
819                 isize = i_size_read(inode);
820                 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
821                 if (unlikely(!isize || index > end_index)) {
822                         page_cache_release(page);
823                         goto out;
824                 }
825
826                 /* nr is the maximum number of bytes to copy from this page */
827                 nr = PAGE_CACHE_SIZE;
828                 if (index == end_index) {
829                         nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
830                         if (nr <= offset) {
831                                 page_cache_release(page);
832                                 goto out;
833                         }
834                 }
835                 nr = nr - offset;
836                 goto page_ok;
837
838 readpage_error:
839                 /* UHHUH! A synchronous read error occurred. Report it */
840                 desc->error = error;
841                 page_cache_release(page);
842                 goto out;
843
844 no_cached_page:
845                 /*
846                  * Ok, it wasn't cached, so we need to create a new
847                  * page..
848                  */
849                 if (!cached_page) {
850                         cached_page = page_cache_alloc_cold(mapping);
851                         if (!cached_page) {
852                                 desc->error = -ENOMEM;
853                                 goto out;
854                         }
855                 }
856                 error = add_to_page_cache_lru(cached_page, mapping,
857                                                 index, GFP_KERNEL);
858                 if (error) {
859                         if (error == -EEXIST)
860                                 goto find_page;
861                         desc->error = error;
862                         goto out;
863                 }
864                 page = cached_page;
865                 cached_page = NULL;
866                 goto readpage;
867         }
868
869 out:
870         *_ra = ra;
871
872         *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
873         if (cached_page)
874                 page_cache_release(cached_page);
875         if (filp)
876                 file_accessed(filp);
877 }
878
879 EXPORT_SYMBOL(do_generic_mapping_read);
880
881 int file_read_actor(read_descriptor_t *desc, struct page *page,
882                         unsigned long offset, unsigned long size)
883 {
884         char *kaddr;
885         unsigned long left, count = desc->count;
886
887         if (size > count)
888                 size = count;
889
890         /*
891          * Faults on the destination of a read are common, so do it before
892          * taking the kmap.
893          */
894         if (!fault_in_pages_writeable(desc->arg.buf, size)) {
895                 kaddr = kmap_atomic(page, KM_USER0);
896                 left = __copy_to_user_inatomic(desc->arg.buf,
897                                                 kaddr + offset, size);
898                 kunmap_atomic(kaddr, KM_USER0);
899                 if (left == 0)
900                         goto success;
901         }
902
903         /* Do it the slow way */
904         kaddr = kmap(page);
905         left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
906         kunmap(page);
907
908         if (left) {
909                 size -= left;
910                 desc->error = -EFAULT;
911         }
912 success:
913         desc->count = count - size;
914         desc->written += size;
915         desc->arg.buf += size;
916         return size;
917 }
918
919 /*
920  * This is the "read()" routine for all filesystems
921  * that can use the page cache directly.
922  */
923 ssize_t
924 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
925                 unsigned long nr_segs, loff_t *ppos)
926 {
927         struct file *filp = iocb->ki_filp;
928         ssize_t retval;
929         unsigned long seg;
930         size_t count;
931
932         count = 0;
933         for (seg = 0; seg < nr_segs; seg++) {
934                 const struct iovec *iv = &iov[seg];
935
936                 /*
937                  * If any segment has a negative length, or the cumulative
938                  * length ever wraps negative then return -EINVAL.
939                  */
940                 count += iv->iov_len;
941                 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
942                         return -EINVAL;
943                 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
944                         continue;
945                 if (seg == 0)
946                         return -EFAULT;
947                 nr_segs = seg;
948                 count -= iv->iov_len;   /* This segment is no good */
949                 break;
950         }
951
952         /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
953         if (filp->f_flags & O_DIRECT) {
954                 loff_t pos = *ppos, size;
955                 struct address_space *mapping;
956                 struct inode *inode;
957
958                 mapping = filp->f_mapping;
959                 inode = mapping->host;
960                 retval = 0;
961                 if (!count)
962                         goto out; /* skip atime */
963                 size = i_size_read(inode);
964                 if (pos < size) {
965                         retval = generic_file_direct_IO(READ, iocb,
966                                                 iov, pos, nr_segs);
967                         if (retval >= 0 && !is_sync_kiocb(iocb))
968                                 retval = -EIOCBQUEUED;
969                         if (retval > 0)
970                                 *ppos = pos + retval;
971                 }
972                 file_accessed(filp);
973                 goto out;
974         }
975
976         retval = 0;
977         if (count) {
978                 for (seg = 0; seg < nr_segs; seg++) {
979                         read_descriptor_t desc;
980
981                         desc.written = 0;
982                         desc.arg.buf = iov[seg].iov_base;
983                         desc.count = iov[seg].iov_len;
984                         if (desc.count == 0)
985                                 continue;
986                         desc.error = 0;
987                         do_generic_file_read(filp,ppos,&desc,file_read_actor,0);
988                         retval += desc.written;
989                         if (!retval) {
990                                 retval = desc.error;
991                                 break;
992                         }
993                 }
994         }
995 out:
996         return retval;
997 }
998
999 EXPORT_SYMBOL(__generic_file_aio_read);
1000
1001 ssize_t
1002 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1003 {
1004         struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1005
1006         BUG_ON(iocb->ki_pos != pos);
1007         return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1008 }
1009
1010 EXPORT_SYMBOL(generic_file_aio_read);
1011
1012 ssize_t
1013 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1014 {
1015         struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1016         struct kiocb kiocb;
1017         ssize_t ret;
1018
1019         init_sync_kiocb(&kiocb, filp);
1020         ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
1021         if (-EIOCBQUEUED == ret)
1022                 ret = wait_on_sync_kiocb(&kiocb);
1023         return ret;
1024 }
1025
1026 EXPORT_SYMBOL(generic_file_read);
1027
1028 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1029 {
1030         ssize_t written;
1031         unsigned long count = desc->count;
1032         struct file *file = desc->arg.data;
1033
1034         if (size > count)
1035                 size = count;
1036
1037         written = file->f_op->sendpage(file, page, offset,
1038                                        size, &file->f_pos, size<count);
1039         if (written < 0) {
1040                 desc->error = written;
1041                 written = 0;
1042         }
1043         desc->count = count - written;
1044         desc->written += written;
1045         return written;
1046 }
1047
1048 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1049                          size_t count, read_actor_t actor, void *target)
1050 {
1051         read_descriptor_t desc;
1052
1053         if (!count)
1054                 return 0;
1055
1056         desc.written = 0;
1057         desc.count = count;
1058         desc.arg.data = target;
1059         desc.error = 0;
1060
1061         do_generic_file_read(in_file, ppos, &desc, actor, 0);
1062         if (desc.written)
1063                 return desc.written;
1064         return desc.error;
1065 }
1066
1067 EXPORT_SYMBOL(generic_file_sendfile);
1068
1069 static ssize_t
1070 do_readahead(struct address_space *mapping, struct file *filp,
1071              unsigned long index, unsigned long nr)
1072 {
1073         if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1074                 return -EINVAL;
1075
1076         force_page_cache_readahead(mapping, filp, index,
1077                                         max_sane_readahead(nr));
1078         return 0;
1079 }
1080
1081 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1082 {
1083         ssize_t ret;
1084         struct file *file;
1085
1086         ret = -EBADF;
1087         file = fget(fd);
1088         if (file) {
1089                 if (file->f_mode & FMODE_READ) {
1090                         struct address_space *mapping = file->f_mapping;
1091                         unsigned long start = offset >> PAGE_CACHE_SHIFT;
1092                         unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1093                         unsigned long len = end - start + 1;
1094                         ret = do_readahead(mapping, file, start, len);
1095                 }
1096                 fput(file);
1097         }
1098         return ret;
1099 }
1100
1101 #ifdef CONFIG_MMU
1102 /*
1103  * This adds the requested page to the page cache if it isn't already there,
1104  * and schedules an I/O to read in its contents from disk.
1105  */
1106 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
1107 static int fastcall page_cache_read(struct file * file, unsigned long offset)
1108 {
1109         struct address_space *mapping = file->f_mapping;
1110         struct page *page; 
1111         int error;
1112
1113         page = page_cache_alloc_cold(mapping);
1114         if (!page)
1115                 return -ENOMEM;
1116
1117         error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1118         if (!error) {
1119                 error = mapping->a_ops->readpage(file, page);
1120                 page_cache_release(page);
1121                 return error;
1122         }
1123
1124         /*
1125          * We arrive here in the unlikely event that someone 
1126          * raced with us and added our page to the cache first
1127          * or we are out of memory for radix-tree nodes.
1128          */
1129         page_cache_release(page);
1130         return error == -EEXIST ? 0 : error;
1131 }
1132
1133 #define MMAP_LOTSAMISS  (100)
1134
1135 /*
1136  * filemap_nopage() is invoked via the vma operations vector for a
1137  * mapped memory region to read in file data during a page fault.
1138  *
1139  * The goto's are kind of ugly, but this streamlines the normal case of having
1140  * it in the page cache, and handles the special cases reasonably without
1141  * having a lot of duplicated code.
1142  */
1143 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int *type)
1144 {
1145         int error;
1146         struct file *file = area->vm_file;
1147         struct address_space *mapping = file->f_mapping;
1148         struct file_ra_state *ra = &file->f_ra;
1149         struct inode *inode = mapping->host;
1150         struct page *page;
1151         unsigned long size, pgoff, endoff;
1152         int did_readaround = 0, majmin = VM_FAULT_MINOR;
1153
1154         pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1155         endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1156
1157 retry_all:
1158         size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1159         if (pgoff >= size)
1160                 goto outside_data_content;
1161
1162         /* If we don't want any read-ahead, don't bother */
1163         if (VM_RandomReadHint(area))
1164                 goto no_cached_page;
1165
1166         /*
1167          * The "size" of the file, as far as mmap is concerned, isn't bigger
1168          * than the mapping
1169          */
1170         if (size > endoff)
1171                 size = endoff;
1172
1173         /*
1174          * The readahead code wants to be told about each and every page
1175          * so it can build and shrink its windows appropriately
1176          *
1177          * For sequential accesses, we use the generic readahead logic.
1178          */
1179         if (VM_SequentialReadHint(area))
1180                 page_cache_readahead(mapping, ra, file, pgoff);
1181
1182         /*
1183          * Do we have something in the page cache already?
1184          */
1185 retry_find:
1186         page = find_get_page(mapping, pgoff);
1187         if (!page) {
1188                 unsigned long ra_pages;
1189
1190                 if (VM_SequentialReadHint(area)) {
1191                         handle_ra_miss(mapping, ra, pgoff);
1192                         goto no_cached_page;
1193                 }
1194                 ra->mmap_miss++;
1195
1196                 /*
1197                  * Do we miss much more than hit in this file? If so,
1198                  * stop bothering with read-ahead. It will only hurt.
1199                  */
1200                 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1201                         goto no_cached_page;
1202
1203                 /*
1204                  * To keep the pgmajfault counter straight, we need to
1205                  * check did_readaround, as this is an inner loop.
1206                  */
1207                 if (!did_readaround) {
1208                         majmin = VM_FAULT_MAJOR;
1209                         inc_page_state(pgmajfault);
1210                 }
1211                 did_readaround = 1;
1212                 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1213                 if (ra_pages) {
1214                         pgoff_t start = 0;
1215
1216                         if (pgoff > ra_pages / 2)
1217                                 start = pgoff - ra_pages / 2;
1218                         do_page_cache_readahead(mapping, file, start, ra_pages);
1219                 }
1220                 page = find_get_page(mapping, pgoff);
1221                 if (!page)
1222                         goto no_cached_page;
1223         }
1224
1225         if (!did_readaround)
1226                 ra->mmap_hit++;
1227
1228         /*
1229          * Ok, found a page in the page cache, now we need to check
1230          * that it's up-to-date.
1231          */
1232         if (!PageUptodate(page))
1233                 goto page_not_uptodate;
1234
1235 success:
1236         /*
1237          * Found the page and have a reference on it.
1238          */
1239         mark_page_accessed(page);
1240         if (type)
1241                 *type = majmin;
1242         return page;
1243
1244 outside_data_content:
1245         /*
1246          * An external ptracer can access pages that normally aren't
1247          * accessible..
1248          */
1249         if (area->vm_mm == current->mm)
1250                 return NULL;
1251         /* Fall through to the non-read-ahead case */
1252 no_cached_page:
1253         /*
1254          * We're only likely to ever get here if MADV_RANDOM is in
1255          * effect.
1256          */
1257         error = page_cache_read(file, pgoff);
1258         grab_swap_token();
1259
1260         /*
1261          * The page we want has now been added to the page cache.
1262          * In the unlikely event that someone removed it in the
1263          * meantime, we'll just come back here and read it again.
1264          */
1265         if (error >= 0)
1266                 goto retry_find;
1267
1268         /*
1269          * An error return from page_cache_read can result if the
1270          * system is low on memory, or a problem occurs while trying
1271          * to schedule I/O.
1272          */
1273         if (error == -ENOMEM)
1274                 return NOPAGE_OOM;
1275         return NULL;
1276
1277 page_not_uptodate:
1278         if (!did_readaround) {
1279                 majmin = VM_FAULT_MAJOR;
1280                 inc_page_state(pgmajfault);
1281         }
1282         lock_page(page);
1283
1284         /* Did it get unhashed while we waited for it? */
1285         if (!page->mapping) {
1286                 unlock_page(page);
1287                 page_cache_release(page);
1288                 goto retry_all;
1289         }
1290
1291         /* Did somebody else get it up-to-date? */
1292         if (PageUptodate(page)) {
1293                 unlock_page(page);
1294                 goto success;
1295         }
1296
1297         if (!mapping->a_ops->readpage(file, page)) {
1298                 wait_on_page_locked(page);
1299                 if (PageUptodate(page))
1300                         goto success;
1301         }
1302
1303         /*
1304          * Umm, take care of errors if the page isn't up-to-date.
1305          * Try to re-read it _once_. We do this synchronously,
1306          * because there really aren't any performance issues here
1307          * and we need to check for errors.
1308          */
1309         lock_page(page);
1310
1311         /* Somebody truncated the page on us? */
1312         if (!page->mapping) {
1313                 unlock_page(page);
1314                 page_cache_release(page);
1315                 goto retry_all;
1316         }
1317
1318         /* Somebody else successfully read it in? */
1319         if (PageUptodate(page)) {
1320                 unlock_page(page);
1321                 goto success;
1322         }
1323         ClearPageError(page);
1324         if (!mapping->a_ops->readpage(file, page)) {
1325                 wait_on_page_locked(page);
1326                 if (PageUptodate(page))
1327                         goto success;
1328         }
1329
1330         /*
1331          * Things didn't work out. Return zero to tell the
1332          * mm layer so, possibly freeing the page cache page first.
1333          */
1334         page_cache_release(page);
1335         return NULL;
1336 }
1337
1338 EXPORT_SYMBOL(filemap_nopage);
1339
1340 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1341                                         int nonblock)
1342 {
1343         struct address_space *mapping = file->f_mapping;
1344         struct page *page;
1345         int error;
1346
1347         /*
1348          * Do we have something in the page cache already?
1349          */
1350 retry_find:
1351         page = find_get_page(mapping, pgoff);
1352         if (!page) {
1353                 if (nonblock)
1354                         return NULL;
1355                 goto no_cached_page;
1356         }
1357
1358         /*
1359          * Ok, found a page in the page cache, now we need to check
1360          * that it's up-to-date.
1361          */
1362         if (!PageUptodate(page))
1363                 goto page_not_uptodate;
1364
1365 success:
1366         /*
1367          * Found the page and have a reference on it.
1368          */
1369         mark_page_accessed(page);
1370         return page;
1371
1372 no_cached_page:
1373         error = page_cache_read(file, pgoff);
1374
1375         /*
1376          * The page we want has now been added to the page cache.
1377          * In the unlikely event that someone removed it in the
1378          * meantime, we'll just come back here and read it again.
1379          */
1380         if (error >= 0)
1381                 goto retry_find;
1382
1383         /*
1384          * An error return from page_cache_read can result if the
1385          * system is low on memory, or a problem occurs while trying
1386          * to schedule I/O.
1387          */
1388         return NULL;
1389
1390 page_not_uptodate:
1391         lock_page(page);
1392
1393         /* Did it get unhashed while we waited for it? */
1394         if (!page->mapping) {
1395                 unlock_page(page);
1396                 goto err;
1397         }
1398
1399         /* Did somebody else get it up-to-date? */
1400         if (PageUptodate(page)) {
1401                 unlock_page(page);
1402                 goto success;
1403         }
1404
1405         if (!mapping->a_ops->readpage(file, page)) {
1406                 wait_on_page_locked(page);
1407                 if (PageUptodate(page))
1408                         goto success;
1409         }
1410
1411         /*
1412          * Umm, take care of errors if the page isn't up-to-date.
1413          * Try to re-read it _once_. We do this synchronously,
1414          * because there really aren't any performance issues here
1415          * and we need to check for errors.
1416          */
1417         lock_page(page);
1418
1419         /* Somebody truncated the page on us? */
1420         if (!page->mapping) {
1421                 unlock_page(page);
1422                 goto err;
1423         }
1424         /* Somebody else successfully read it in? */
1425         if (PageUptodate(page)) {
1426                 unlock_page(page);
1427                 goto success;
1428         }
1429
1430         ClearPageError(page);
1431         if (!mapping->a_ops->readpage(file, page)) {
1432                 wait_on_page_locked(page);
1433                 if (PageUptodate(page))
1434                         goto success;
1435         }
1436
1437         /*
1438          * Things didn't work out. Return zero to tell the
1439          * mm layer so, possibly freeing the page cache page first.
1440          */
1441 err:
1442         page_cache_release(page);
1443
1444         return NULL;
1445 }
1446
1447 static int filemap_populate(struct vm_area_struct *vma,
1448                         unsigned long addr,
1449                         unsigned long len,
1450                         pgprot_t prot,
1451                         unsigned long pgoff,
1452                         int nonblock)
1453 {
1454         struct file *file = vma->vm_file;
1455         struct address_space *mapping = file->f_mapping;
1456         struct inode *inode = mapping->host;
1457         unsigned long size;
1458         struct mm_struct *mm = vma->vm_mm;
1459         struct page *page;
1460         int err;
1461
1462         if (!nonblock)
1463                 force_page_cache_readahead(mapping, vma->vm_file,
1464                                         pgoff, len >> PAGE_CACHE_SHIFT);
1465
1466 repeat:
1467         size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1468         if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1469                 return -EINVAL;
1470
1471         page = filemap_getpage(file, pgoff, nonblock);
1472         if (!page && !nonblock)
1473                 return -ENOMEM;
1474         if (page) {
1475                 err = install_page(mm, vma, addr, page, prot);
1476                 if (err) {
1477                         page_cache_release(page);
1478                         return err;
1479                 }
1480         } else {
1481                 err = install_file_pte(mm, vma, addr, pgoff, prot);
1482                 if (err)
1483                         return err;
1484         }
1485
1486         len -= PAGE_SIZE;
1487         addr += PAGE_SIZE;
1488         pgoff++;
1489         if (len)
1490                 goto repeat;
1491
1492         return 0;
1493 }
1494
1495 struct vm_operations_struct generic_file_vm_ops = {
1496         .nopage         = filemap_nopage,
1497         .populate       = filemap_populate,
1498 };
1499
1500 /* This is used for a general mmap of a disk file */
1501
1502 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1503 {
1504         struct address_space *mapping = file->f_mapping;
1505
1506         if (!mapping->a_ops->readpage)
1507                 return -ENOEXEC;
1508         file_accessed(file);
1509         vma->vm_ops = &generic_file_vm_ops;
1510         return 0;
1511 }
1512
1513 /*
1514  * This is for filesystems which do not implement ->writepage.
1515  */
1516 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1517 {
1518         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1519                 return -EINVAL;
1520         return generic_file_mmap(file, vma);
1521 }
1522 #else
1523 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1524 {
1525         return -ENOSYS;
1526 }
1527 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1528 {
1529         return -ENOSYS;
1530 }
1531 #endif /* CONFIG_MMU */
1532
1533 EXPORT_SYMBOL(generic_file_mmap);
1534 EXPORT_SYMBOL(generic_file_readonly_mmap);
1535
1536 static inline struct page *__read_cache_page(struct address_space *mapping,
1537                                 unsigned long index,
1538                                 int (*filler)(void *,struct page*),
1539                                 void *data)
1540 {
1541         struct page *page, *cached_page = NULL;
1542         int err;
1543 repeat:
1544         page = find_get_page(mapping, index);
1545         if (!page) {
1546                 if (!cached_page) {
1547                         cached_page = page_cache_alloc_cold(mapping);
1548                         if (!cached_page)
1549                                 return ERR_PTR(-ENOMEM);
1550                 }
1551                 err = add_to_page_cache_lru(cached_page, mapping,
1552                                         index, GFP_KERNEL);
1553                 if (err == -EEXIST)
1554                         goto repeat;
1555                 if (err < 0) {
1556                         /* Presumably ENOMEM for radix tree node */
1557                         page_cache_release(cached_page);
1558                         return ERR_PTR(err);
1559                 }
1560                 page = cached_page;
1561                 cached_page = NULL;
1562                 err = filler(data, page);
1563                 if (err < 0) {
1564                         page_cache_release(page);
1565                         page = ERR_PTR(err);
1566                 }
1567         }
1568         if (cached_page)
1569                 page_cache_release(cached_page);
1570         return page;
1571 }
1572
1573 /*
1574  * Read into the page cache. If a page already exists,
1575  * and PageUptodate() is not set, try to fill the page.
1576  */
1577 struct page *read_cache_page(struct address_space *mapping,
1578                                 unsigned long index,
1579                                 int (*filler)(void *,struct page*),
1580                                 void *data)
1581 {
1582         struct page *page;
1583         int err;
1584
1585 retry:
1586         page = __read_cache_page(mapping, index, filler, data);
1587         if (IS_ERR(page))
1588                 goto out;
1589         mark_page_accessed(page);
1590         if (PageUptodate(page))
1591                 goto out;
1592
1593         lock_page(page);
1594         if (!page->mapping) {
1595                 unlock_page(page);
1596                 page_cache_release(page);
1597                 goto retry;
1598         }
1599         if (PageUptodate(page)) {
1600                 unlock_page(page);
1601                 goto out;
1602         }
1603         err = filler(data, page);
1604         if (err < 0) {
1605                 page_cache_release(page);
1606                 page = ERR_PTR(err);
1607         }
1608  out:
1609         return page;
1610 }
1611
1612 EXPORT_SYMBOL(read_cache_page);
1613
1614 /*
1615  * If the page was newly created, increment its refcount and add it to the
1616  * caller's lru-buffering pagevec.  This function is specifically for
1617  * generic_file_write().
1618  */
1619 static inline struct page *
1620 __grab_cache_page(struct address_space *mapping, unsigned long index,
1621                         struct page **cached_page, struct pagevec *lru_pvec)
1622 {
1623         int err;
1624         struct page *page;
1625 repeat:
1626         page = find_lock_page(mapping, index);
1627         if (!page) {
1628                 if (!*cached_page) {
1629                         *cached_page = page_cache_alloc(mapping);
1630                         if (!*cached_page)
1631                                 return NULL;
1632                 }
1633                 err = add_to_page_cache(*cached_page, mapping,
1634                                         index, GFP_KERNEL);
1635                 if (err == -EEXIST)
1636                         goto repeat;
1637                 if (err == 0) {
1638                         page = *cached_page;
1639                         page_cache_get(page);
1640                         if (!pagevec_add(lru_pvec, page))
1641                                 __pagevec_lru_add(lru_pvec);
1642                         *cached_page = NULL;
1643                 }
1644         }
1645         return page;
1646 }
1647
1648 /*
1649  * The logic we want is
1650  *
1651  *      if suid or (sgid and xgrp)
1652  *              remove privs
1653  */
1654 int remove_suid(struct dentry *dentry)
1655 {
1656         mode_t mode = dentry->d_inode->i_mode;
1657         int kill = 0;
1658         int result = 0;
1659
1660         /* suid always must be killed */
1661         if (unlikely(mode & S_ISUID))
1662                 kill = ATTR_KILL_SUID;
1663
1664         /*
1665          * sgid without any exec bits is just a mandatory locking mark; leave
1666          * it alone.  If some exec bits are set, it's a real sgid; kill it.
1667          */
1668         if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1669                 kill |= ATTR_KILL_SGID;
1670
1671         if (unlikely(kill && !capable(CAP_FSETID))) {
1672                 struct iattr newattrs;
1673
1674                 newattrs.ia_valid = ATTR_FORCE | kill;
1675                 result = notify_change(dentry, &newattrs);
1676         }
1677         return result;
1678 }
1679 EXPORT_SYMBOL(remove_suid);
1680
1681 /*
1682  * Copy as much as we can into the page and return the number of bytes which
1683  * were sucessfully copied.  If a fault is encountered then clear the page
1684  * out to (offset+bytes) and return the number of bytes which were copied.
1685  */
1686 static inline size_t
1687 filemap_copy_from_user(struct page *page, unsigned long offset,
1688                         const char __user *buf, unsigned bytes)
1689 {
1690         char *kaddr;
1691         int left;
1692
1693         kaddr = kmap_atomic(page, KM_USER0);
1694         left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
1695         kunmap_atomic(kaddr, KM_USER0);
1696
1697         if (left != 0) {
1698                 /* Do it the slow way */
1699                 kaddr = kmap(page);
1700                 left = __copy_from_user(kaddr + offset, buf, bytes);
1701                 kunmap(page);
1702         }
1703         return bytes - left;
1704 }
1705
1706 static size_t
1707 __filemap_copy_from_user_iovec(char *vaddr, 
1708                         const struct iovec *iov, size_t base, size_t bytes)
1709 {
1710         size_t copied = 0, left = 0;
1711
1712         while (bytes) {
1713                 char __user *buf = iov->iov_base + base;
1714                 int copy = min(bytes, iov->iov_len - base);
1715
1716                 base = 0;
1717                 left = __copy_from_user_inatomic(vaddr, buf, copy);
1718                 copied += copy;
1719                 bytes -= copy;
1720                 vaddr += copy;
1721                 iov++;
1722
1723                 if (unlikely(left)) {
1724                         /* zero the rest of the target like __copy_from_user */
1725                         if (bytes)
1726                                 memset(vaddr, 0, bytes);
1727                         break;
1728                 }
1729         }
1730         return copied - left;
1731 }
1732
1733 /*
1734  * This has the same sideeffects and return value as filemap_copy_from_user().
1735  * The difference is that on a fault we need to memset the remainder of the
1736  * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1737  * single-segment behaviour.
1738  */
1739 static inline size_t
1740 filemap_copy_from_user_iovec(struct page *page, unsigned long offset,
1741                         const struct iovec *iov, size_t base, size_t bytes)
1742 {
1743         char *kaddr;
1744         size_t copied;
1745
1746         kaddr = kmap_atomic(page, KM_USER0);
1747         copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1748                                                 base, bytes);
1749         kunmap_atomic(kaddr, KM_USER0);
1750         if (copied != bytes) {
1751                 kaddr = kmap(page);
1752                 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1753                                                         base, bytes);
1754                 kunmap(page);
1755         }
1756         return copied;
1757 }
1758
1759 static inline void
1760 filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1761 {
1762         const struct iovec *iov = *iovp;
1763         size_t base = *basep;
1764
1765         while (bytes) {
1766                 int copy = min(bytes, iov->iov_len - base);
1767
1768                 bytes -= copy;
1769                 base += copy;
1770                 if (iov->iov_len == base) {
1771                         iov++;
1772                         base = 0;
1773                 }
1774         }
1775         *iovp = iov;
1776         *basep = base;
1777 }
1778
1779 /*
1780  * Performs necessary checks before doing a write
1781  *
1782  * Can adjust writing position aor amount of bytes to write.
1783  * Returns appropriate error code that caller should return or
1784  * zero in case that write should be allowed.
1785  */
1786 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1787 {
1788         struct inode *inode = file->f_mapping->host;
1789         unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1790
1791         if (unlikely(*pos < 0))
1792                 return -EINVAL;
1793
1794         if (unlikely(file->f_error)) {
1795                 int err = file->f_error;
1796                 file->f_error = 0;
1797                 return err;
1798         }
1799
1800         if (!isblk) {
1801                 /* FIXME: this is for backwards compatibility with 2.4 */
1802                 if (file->f_flags & O_APPEND)
1803                         *pos = i_size_read(inode);
1804
1805                 if (limit != RLIM_INFINITY) {
1806                         if (*pos >= limit) {
1807                                 send_sig(SIGXFSZ, current, 0);
1808                                 return -EFBIG;
1809                         }
1810                         if (*count > limit - (typeof(limit))*pos) {
1811                                 *count = limit - (typeof(limit))*pos;
1812                         }
1813                 }
1814         }
1815
1816         /*
1817          * LFS rule
1818          */
1819         if (unlikely(*pos + *count > MAX_NON_LFS &&
1820                                 !(file->f_flags & O_LARGEFILE))) {
1821                 if (*pos >= MAX_NON_LFS) {
1822                         send_sig(SIGXFSZ, current, 0);
1823                         return -EFBIG;
1824                 }
1825                 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1826                         *count = MAX_NON_LFS - (unsigned long)*pos;
1827                 }
1828         }
1829
1830         /*
1831          * Are we about to exceed the fs block limit ?
1832          *
1833          * If we have written data it becomes a short write.  If we have
1834          * exceeded without writing data we send a signal and return EFBIG.
1835          * Linus frestrict idea will clean these up nicely..
1836          */
1837         if (likely(!isblk)) {
1838                 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1839                         if (*count || *pos > inode->i_sb->s_maxbytes) {
1840                                 send_sig(SIGXFSZ, current, 0);
1841                                 return -EFBIG;
1842                         }
1843                         /* zero-length writes at ->s_maxbytes are OK */
1844                 }
1845
1846                 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1847                         *count = inode->i_sb->s_maxbytes - *pos;
1848         } else {
1849                 loff_t isize;
1850                 if (bdev_read_only(I_BDEV(inode)))
1851                         return -EPERM;
1852                 isize = i_size_read(inode);
1853                 if (*pos >= isize) {
1854                         if (*count || *pos > isize)
1855                                 return -ENOSPC;
1856                 }
1857
1858                 if (*pos + *count > isize)
1859                         *count = isize - *pos;
1860         }
1861         return 0;
1862 }
1863 EXPORT_SYMBOL(generic_write_checks);
1864
1865 ssize_t
1866 generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1867                 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1868                 size_t count, size_t ocount)
1869 {
1870         struct file     *file = iocb->ki_filp;
1871         struct address_space *mapping = file->f_mapping;
1872         struct inode    *inode = mapping->host;
1873         ssize_t         written;
1874
1875         if (count != ocount)
1876                 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1877
1878         written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1879         if (written > 0) {
1880                 loff_t end = pos + written;
1881                 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1882                         i_size_write(inode,  end);
1883                         mark_inode_dirty(inode);
1884                 }
1885                 *ppos = end;
1886         }
1887
1888         /*
1889          * Sync the fs metadata but not the minor inode changes and
1890          * of course not the data as we did direct DMA for the IO.
1891          * i_sem is held, which protects generic_osync_inode() from
1892          * livelocking.
1893          */
1894         if (written >= 0 && file->f_flags & O_SYNC)
1895                 generic_osync_inode(inode, mapping, OSYNC_METADATA);
1896         if (written == count && !is_sync_kiocb(iocb))
1897                 written = -EIOCBQUEUED;
1898         return written;
1899 }
1900 EXPORT_SYMBOL(generic_file_direct_write);
1901
1902 ssize_t
1903 generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
1904                 unsigned long nr_segs, loff_t pos, loff_t *ppos,
1905                 size_t count, ssize_t written)
1906 {
1907         struct file *file = iocb->ki_filp;
1908         struct address_space * mapping = file->f_mapping;
1909         struct address_space_operations *a_ops = mapping->a_ops;
1910         struct inode    *inode = mapping->host;
1911         long            status = 0;
1912         struct page     *page;
1913         struct page     *cached_page = NULL;
1914         size_t          bytes;
1915         struct pagevec  lru_pvec;
1916         const struct iovec *cur_iov = iov; /* current iovec */
1917         size_t          iov_base = 0;      /* offset in the current iovec */
1918         char __user     *buf;
1919
1920         pagevec_init(&lru_pvec, 0);
1921
1922         buf = iov->iov_base + written;  /* handle partial DIO write */
1923         do {
1924                 unsigned long index;
1925                 unsigned long offset;
1926                 size_t copied;
1927
1928                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1929                 index = pos >> PAGE_CACHE_SHIFT;
1930                 bytes = PAGE_CACHE_SIZE - offset;
1931                 if (bytes > count)
1932                         bytes = count;
1933
1934                 /*
1935                  * Bring in the user page that we will copy from _first_.
1936                  * Otherwise there's a nasty deadlock on copying from the
1937                  * same page as we're writing to, without it being marked
1938                  * up-to-date.
1939                  */
1940                 fault_in_pages_readable(buf, bytes);
1941
1942                 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1943                 if (!page) {
1944                         status = -ENOMEM;
1945                         break;
1946                 }
1947
1948                 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1949                 if (unlikely(status)) {
1950                         loff_t isize = i_size_read(inode);
1951                         /*
1952                          * prepare_write() may have instantiated a few blocks
1953                          * outside i_size.  Trim these off again.
1954                          */
1955                         unlock_page(page);
1956                         page_cache_release(page);
1957                         if (pos + bytes > isize)
1958                                 vmtruncate(inode, isize);
1959                         break;
1960                 }
1961                 if (likely(nr_segs == 1))
1962                         copied = filemap_copy_from_user(page, offset,
1963                                                         buf, bytes);
1964                 else
1965                         copied = filemap_copy_from_user_iovec(page, offset,
1966                                                 cur_iov, iov_base, bytes);
1967                 flush_dcache_page(page);
1968                 status = a_ops->commit_write(file, page, offset, offset+bytes);
1969                 if (likely(copied > 0)) {
1970                         if (!status)
1971                                 status = copied;
1972
1973                         if (status >= 0) {
1974                                 written += status;
1975                                 count -= status;
1976                                 pos += status;
1977                                 buf += status;
1978                                 if (unlikely(nr_segs > 1))
1979                                         filemap_set_next_iovec(&cur_iov,
1980                                                         &iov_base, status);
1981                         }
1982                 }
1983                 if (unlikely(copied != bytes))
1984                         if (status >= 0)
1985                                 status = -EFAULT;
1986                 unlock_page(page);
1987                 mark_page_accessed(page);
1988                 page_cache_release(page);
1989                 if (status < 0)
1990                         break;
1991                 balance_dirty_pages_ratelimited(mapping);
1992                 cond_resched();
1993         } while (count);
1994         *ppos = pos;
1995
1996         if (cached_page)
1997                 page_cache_release(cached_page);
1998
1999         /*
2000          * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2001          */
2002         if (likely(status >= 0)) {
2003                 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2004                         if (!a_ops->writepage || !is_sync_kiocb(iocb))
2005                                 status = generic_osync_inode(inode, mapping,
2006                                                 OSYNC_METADATA|OSYNC_DATA);
2007                 }
2008         }
2009         
2010         /*
2011          * If we get here for O_DIRECT writes then we must have fallen through
2012          * to buffered writes (block instantiation inside i_size).  So we sync
2013          * the file data here, to try to honour O_DIRECT expectations.
2014          */
2015         if (unlikely(file->f_flags & O_DIRECT) && written)
2016                 status = filemap_write_and_wait(mapping);
2017
2018         pagevec_lru_add(&lru_pvec);
2019         return written ? written : status;
2020 }
2021 EXPORT_SYMBOL(generic_file_buffered_write);
2022
2023 ssize_t
2024 __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2025                                 unsigned long nr_segs, loff_t *ppos)
2026 {
2027         struct file *file = iocb->ki_filp;
2028         struct address_space * mapping = file->f_mapping;
2029         size_t ocount;          /* original count */
2030         size_t count;           /* after file limit checks */
2031         struct inode    *inode = mapping->host;
2032         unsigned long   seg;
2033         loff_t          pos;
2034         ssize_t         written;
2035         ssize_t         err;
2036
2037         ocount = 0;
2038         for (seg = 0; seg < nr_segs; seg++) {
2039                 const struct iovec *iv = &iov[seg];
2040
2041                 /*
2042                  * If any segment has a negative length, or the cumulative
2043                  * length ever wraps negative then return -EINVAL.
2044                  */
2045                 ocount += iv->iov_len;
2046                 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2047                         return -EINVAL;
2048                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2049                         continue;
2050                 if (seg == 0)
2051                         return -EFAULT;
2052                 nr_segs = seg;
2053                 ocount -= iv->iov_len;  /* This segment is no good */
2054                 break;
2055         }
2056
2057         count = ocount;
2058         pos = *ppos;
2059
2060         /* We can write back this queue in page reclaim */
2061         current->backing_dev_info = mapping->backing_dev_info;
2062         written = 0;
2063
2064         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2065         if (err)
2066                 goto out;
2067
2068         if (count == 0)
2069                 goto out;
2070
2071         err = remove_suid(file->f_dentry);
2072         if (err)
2073                 goto out;
2074
2075         inode_update_time(inode, file->f_vfsmnt, 1);
2076
2077         /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2078         if (unlikely(file->f_flags & O_DIRECT)) {
2079                 written = generic_file_direct_write(iocb, iov,
2080                                 &nr_segs, pos, ppos, count, ocount);
2081                 if (written < 0 || written == count)
2082                         goto out;
2083                 /*
2084                  * direct-io write to a hole: fall through to buffered I/O
2085                  * for completing the rest of the request.
2086                  */
2087                 pos += written;
2088                 count -= written;
2089         }
2090
2091         written = generic_file_buffered_write(iocb, iov, nr_segs,
2092                         pos, ppos, count, written);
2093 out:
2094         current->backing_dev_info = NULL;
2095         return written ? written : err;
2096 }
2097 EXPORT_SYMBOL(generic_file_aio_write_nolock);
2098
2099 ssize_t
2100 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2101                                 unsigned long nr_segs, loff_t *ppos)
2102 {
2103         struct file *file = iocb->ki_filp;
2104         struct address_space *mapping = file->f_mapping;
2105         struct inode *inode = mapping->host;
2106         ssize_t ret;
2107         loff_t pos = *ppos;
2108
2109         ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
2110
2111         if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2112                 int err;
2113
2114                 err = sync_page_range_nolock(inode, mapping, pos, ret);
2115                 if (err < 0)
2116                         ret = err;
2117         }
2118         return ret;
2119 }
2120
2121 ssize_t
2122 __generic_file_write_nolock(struct file *file, const struct iovec *iov,
2123                                 unsigned long nr_segs, loff_t *ppos)
2124 {
2125         struct kiocb kiocb;
2126         ssize_t ret;
2127
2128         init_sync_kiocb(&kiocb, file);
2129         ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2130         if (ret == -EIOCBQUEUED)
2131                 ret = wait_on_sync_kiocb(&kiocb);
2132         return ret;
2133 }
2134
2135 ssize_t
2136 generic_file_write_nolock(struct file *file, const struct iovec *iov,
2137                                 unsigned long nr_segs, loff_t *ppos)
2138 {
2139         struct kiocb kiocb;
2140         ssize_t ret;
2141
2142         init_sync_kiocb(&kiocb, file);
2143         ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2144         if (-EIOCBQUEUED == ret)
2145                 ret = wait_on_sync_kiocb(&kiocb);
2146         return ret;
2147 }
2148 EXPORT_SYMBOL(generic_file_write_nolock);
2149
2150 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2151                                size_t count, loff_t pos)
2152 {
2153         struct file *file = iocb->ki_filp;
2154         struct address_space *mapping = file->f_mapping;
2155         struct inode *inode = mapping->host;
2156         ssize_t ret;
2157         struct iovec local_iov = { .iov_base = (void __user *)buf,
2158                                         .iov_len = count };
2159
2160         BUG_ON(iocb->ki_pos != pos);
2161
2162         down(&inode->i_sem);
2163         ret = generic_file_aio_write_nolock(iocb, &local_iov, 1,
2164                                                 &iocb->ki_pos);
2165         up(&inode->i_sem);
2166
2167         if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2168                 ssize_t err;
2169
2170                 err = sync_page_range(inode, mapping, pos, ret);
2171                 if (err < 0)
2172                         ret = err;
2173         }
2174         return ret;
2175 }
2176 EXPORT_SYMBOL(generic_file_aio_write);
2177
2178 ssize_t generic_file_write(struct file *file, const char __user *buf,
2179                            size_t count, loff_t *ppos)
2180 {
2181         struct address_space *mapping = file->f_mapping;
2182         struct inode *inode = mapping->host;
2183         ssize_t ret;
2184         struct iovec local_iov = { .iov_base = (void __user *)buf,
2185                                         .iov_len = count };
2186
2187         down(&inode->i_sem);
2188         ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
2189         up(&inode->i_sem);
2190
2191         if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2192                 ssize_t err;
2193
2194                 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2195                 if (err < 0)
2196                         ret = err;
2197         }
2198         return ret;
2199 }
2200 EXPORT_SYMBOL(generic_file_write);
2201
2202 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2203                         unsigned long nr_segs, loff_t *ppos)
2204 {
2205         struct kiocb kiocb;
2206         ssize_t ret;
2207
2208         init_sync_kiocb(&kiocb, filp);
2209         ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
2210         if (-EIOCBQUEUED == ret)
2211                 ret = wait_on_sync_kiocb(&kiocb);
2212         return ret;
2213 }
2214 EXPORT_SYMBOL(generic_file_readv);
2215
2216 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2217                         unsigned long nr_segs, loff_t *ppos)
2218 {
2219         struct address_space *mapping = file->f_mapping;
2220         struct inode *inode = mapping->host;
2221         ssize_t ret;
2222
2223         down(&inode->i_sem);
2224         ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
2225         up(&inode->i_sem);
2226
2227         if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2228                 int err;
2229
2230                 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2231                 if (err < 0)
2232                         ret = err;
2233         }
2234         return ret;
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 EXPORT_SYMBOL_GPL(generic_file_direct_IO);