4 * Copyright (C) 1994-1999 Linus Torvalds
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)
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/compiler.h>
17 #include <linux/aio.h>
18 #include <linux/kernel_stat.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>
31 * This is needed for the following functions:
32 * - try_to_release_page
33 * - block_invalidatepage
34 * - generic_osync_inode
36 * FIXME: remove all knowledge of the buffer layer from the core VM
38 #include <linux/buffer_head.h> /* for generic_osync_inode */
40 #include <asm/uaccess.h>
44 * Shared mappings implemented 30.11.1994. It's not fully working yet,
47 * Shared mappings now work. 15.8.1995 Bruno.
49 * finished 'unifying' the page and buffer cache and SMP-threaded the
50 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
52 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
58 * ->i_shared_sem (vmtruncate)
59 * ->private_lock (__free_pte->__set_page_dirty_buffers)
61 * ->swap_device_lock (exclusive_swap_page, others)
62 * ->mapping->page_lock
65 * ->i_shared_sem (truncate->invalidate_mmap_range)
68 * ->i_shared_sem (various places)
71 * ->lock_page (access_process_vm)
77 * ->sb_lock (fs/fs-writeback.c)
78 * ->mapping->page_lock (__sync_single_inode)
81 * ->swap_device_lock (try_to_unmap_one)
82 * ->private_lock (try_to_unmap_one)
83 * ->page_lock (try_to_unmap_one)
84 * ->zone.lru_lock (follow_page->mark_page_accessed)
87 * ->dcache_lock (proc_pid_lookup)
91 * Remove a page from the page cache and free it. Caller has to make
92 * sure the page is locked and that nobody else uses it - or that usage
93 * is safe. The caller must hold a write_lock on the mapping's page_lock.
95 void __remove_from_page_cache(struct page
*page
)
97 struct address_space
*mapping
= page
->mapping
;
99 radix_tree_delete(&mapping
->page_tree
, page
->index
);
100 list_del(&page
->list
);
101 page
->mapping
= NULL
;
107 void remove_from_page_cache(struct page
*page
)
109 struct address_space
*mapping
= page
->mapping
;
111 if (unlikely(!PageLocked(page
)))
114 spin_lock(&mapping
->page_lock
);
115 __remove_from_page_cache(page
);
116 spin_unlock(&mapping
->page_lock
);
119 static inline int sync_page(struct page
*page
)
121 struct address_space
*mapping
= page
->mapping
;
123 if (mapping
&& mapping
->a_ops
&& mapping
->a_ops
->sync_page
)
124 return mapping
->a_ops
->sync_page(page
);
129 * filemap_fdatawrite - start writeback against all of a mapping's dirty pages
130 * @mapping: address space structure to write
132 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
133 * opposed to a regular memory * cleansing writeback. The difference between
134 * these two operations is that if a dirty page/buffer is encountered, it must
135 * be waited upon, and not just skipped over.
137 static int __filemap_fdatawrite(struct address_space
*mapping
, int sync_mode
)
140 struct writeback_control wbc
= {
141 .sync_mode
= sync_mode
,
142 .nr_to_write
= mapping
->nrpages
* 2,
145 if (mapping
->backing_dev_info
->memory_backed
)
148 spin_lock(&mapping
->page_lock
);
149 list_splice_init(&mapping
->dirty_pages
, &mapping
->io_pages
);
150 spin_unlock(&mapping
->page_lock
);
151 ret
= do_writepages(mapping
, &wbc
);
155 int filemap_fdatawrite(struct address_space
*mapping
)
157 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
160 EXPORT_SYMBOL(filemap_fdatawrite
);
163 * This is a mostly non-blocking flush. Not suitable for data-integrity
164 * purposes - I/O may not be started against all dirty pages.
166 int filemap_flush(struct address_space
*mapping
)
168 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
171 EXPORT_SYMBOL(filemap_flush
);
174 * filemap_fdatawait - walk the list of locked pages of the given address
175 * space and wait for all of them.
176 * @mapping: address space structure to wait for
178 int filemap_fdatawait(struct address_space
* mapping
)
185 spin_lock(&mapping
->page_lock
);
186 while (!list_empty(&mapping
->locked_pages
)) {
189 page
= list_entry(mapping
->locked_pages
.next
,struct page
,list
);
190 list_del(&page
->list
);
192 list_add(&page
->list
, &mapping
->dirty_pages
);
194 list_add(&page
->list
, &mapping
->clean_pages
);
196 if (!PageWriteback(page
)) {
197 if (++progress
> 32) {
198 if (need_resched()) {
199 spin_unlock(&mapping
->page_lock
);
208 page_cache_get(page
);
209 spin_unlock(&mapping
->page_lock
);
211 wait_on_page_writeback(page
);
215 page_cache_release(page
);
216 spin_lock(&mapping
->page_lock
);
218 spin_unlock(&mapping
->page_lock
);
220 /* Check for outstanding write errors */
221 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
223 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
229 EXPORT_SYMBOL(filemap_fdatawait
);
232 * This adds a page to the page cache, starting out as locked, unreferenced,
233 * not uptodate and with no errors.
235 * This function is used for two things: adding newly allocated pagecache
236 * pages and for moving existing anon pages into swapcache.
238 * In the case of pagecache pages, the page is new, so we can just run
239 * SetPageLocked() against it. The other page state flags were set by
242 * In the case of swapcache, try_to_swap_out() has already locked the page, so
243 * SetPageLocked() is ugly-but-OK there too. The required page state has been
244 * set up by swap_out_add_to_swap_cache().
246 * This function does not add the page to the LRU. The caller must do that.
248 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
249 pgoff_t offset
, int gfp_mask
)
251 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
254 page_cache_get(page
);
255 spin_lock(&mapping
->page_lock
);
256 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
259 ___add_to_page_cache(page
, mapping
, offset
);
261 page_cache_release(page
);
263 spin_unlock(&mapping
->page_lock
);
264 radix_tree_preload_end();
269 EXPORT_SYMBOL(add_to_page_cache
);
271 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
272 pgoff_t offset
, int gfp_mask
)
274 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
281 * In order to wait for pages to become available there must be
282 * waitqueues associated with pages. By using a hash table of
283 * waitqueues where the bucket discipline is to maintain all
284 * waiters on the same queue and wake all when any of the pages
285 * become available, and for the woken contexts to check to be
286 * sure the appropriate page became available, this saves space
287 * at a cost of "thundering herd" phenomena during rare hash
290 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
292 const struct zone
*zone
= page_zone(page
);
294 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
297 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
299 wait_queue_head_t
*waitqueue
= page_waitqueue(page
);
303 prepare_to_wait(waitqueue
, &wait
, TASK_UNINTERRUPTIBLE
);
304 if (test_bit(bit_nr
, &page
->flags
)) {
308 } while (test_bit(bit_nr
, &page
->flags
));
309 finish_wait(waitqueue
, &wait
);
312 EXPORT_SYMBOL(wait_on_page_bit
);
315 * unlock_page() - unlock a locked page
319 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
320 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
321 * mechananism between PageLocked pages and PageWriteback pages is shared.
322 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
324 * The first mb is necessary to safely close the critical section opened by the
325 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
326 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
327 * parallel wait_on_page_locked()).
329 void fastcall
unlock_page(struct page
*page
)
331 wait_queue_head_t
*waitqueue
= page_waitqueue(page
);
332 smp_mb__before_clear_bit();
333 if (!TestClearPageLocked(page
))
335 smp_mb__after_clear_bit();
336 if (waitqueue_active(waitqueue
))
337 wake_up_all(waitqueue
);
340 EXPORT_SYMBOL(unlock_page
);
341 EXPORT_SYMBOL(lock_page
);
344 * End writeback against a page.
346 void end_page_writeback(struct page
*page
)
348 wait_queue_head_t
*waitqueue
= page_waitqueue(page
);
350 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
351 smp_mb__before_clear_bit();
352 if (!TestClearPageWriteback(page
))
354 smp_mb__after_clear_bit();
356 if (waitqueue_active(waitqueue
))
357 wake_up_all(waitqueue
);
360 EXPORT_SYMBOL(end_page_writeback
);
363 * Get a lock on the page, assuming we need to sleep to get it.
365 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
366 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
367 * chances are that on the second loop, the block layer's plug list is empty,
368 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
370 void fastcall
__lock_page(struct page
*page
)
372 wait_queue_head_t
*wqh
= page_waitqueue(page
);
375 while (TestSetPageLocked(page
)) {
376 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
377 if (PageLocked(page
)) {
382 finish_wait(wqh
, &wait
);
385 EXPORT_SYMBOL(__lock_page
);
388 * a rather lightweight function, finding and getting a reference to a
389 * hashed page atomically.
391 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
396 * We scan the hash list read-only. Addition to and removal from
397 * the hash-list needs a held write-lock.
399 spin_lock(&mapping
->page_lock
);
400 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
402 page_cache_get(page
);
403 spin_unlock(&mapping
->page_lock
);
407 EXPORT_SYMBOL(find_get_page
);
410 * Same as above, but trylock it instead of incrementing the count.
412 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
416 spin_lock(&mapping
->page_lock
);
417 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
418 if (page
&& TestSetPageLocked(page
))
420 spin_unlock(&mapping
->page_lock
);
424 EXPORT_SYMBOL(find_trylock_page
);
427 * find_lock_page - locate, pin and lock a pagecache page
429 * @mapping - the address_space to search
430 * @offset - the page index
432 * Locates the desired pagecache page, locks it, increments its reference
433 * count and returns its address.
435 * Returns zero if the page was not present. find_lock_page() may sleep.
437 struct page
*find_lock_page(struct address_space
*mapping
,
438 unsigned long offset
)
442 spin_lock(&mapping
->page_lock
);
444 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
446 page_cache_get(page
);
447 if (TestSetPageLocked(page
)) {
448 spin_unlock(&mapping
->page_lock
);
450 spin_lock(&mapping
->page_lock
);
452 /* Has the page been truncated while we slept? */
453 if (page
->mapping
!= mapping
|| page
->index
!= offset
) {
455 page_cache_release(page
);
460 spin_unlock(&mapping
->page_lock
);
464 EXPORT_SYMBOL(find_lock_page
);
467 * find_or_create_page - locate or add a pagecache page
469 * @mapping - the page's address_space
470 * @index - the page's index into the mapping
471 * @gfp_mask - page allocation mode
473 * Locates a page in the pagecache. If the page is not present, a new page
474 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
475 * LRU list. The returned page is locked and has its reference count
478 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
481 * find_or_create_page() returns the desired page's address, or zero on
484 struct page
*find_or_create_page(struct address_space
*mapping
,
485 unsigned long index
, unsigned int gfp_mask
)
487 struct page
*page
, *cached_page
= NULL
;
490 page
= find_lock_page(mapping
, index
);
493 cached_page
= alloc_page(gfp_mask
);
497 err
= add_to_page_cache_lru(cached_page
, mapping
,
502 } else if (err
== -EEXIST
)
506 page_cache_release(cached_page
);
510 EXPORT_SYMBOL(find_or_create_page
);
513 * find_get_pages - gang pagecache lookup
514 * @mapping: The address_space to search
515 * @start: The starting page index
516 * @nr_pages: The maximum number of pages
517 * @pages: Where the resulting pages are placed
519 * find_get_pages() will search for and return a group of up to
520 * @nr_pages pages in the mapping. The pages are placed at @pages.
521 * find_get_pages() takes a reference against the returned pages.
523 * The search returns a group of mapping-contiguous pages with ascending
524 * indexes. There may be holes in the indices due to not-present pages.
526 * find_get_pages() returns the number of pages which were found.
528 unsigned int find_get_pages(struct address_space
*mapping
, pgoff_t start
,
529 unsigned int nr_pages
, struct page
**pages
)
534 spin_lock(&mapping
->page_lock
);
535 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
536 (void **)pages
, start
, nr_pages
);
537 for (i
= 0; i
< ret
; i
++)
538 page_cache_get(pages
[i
]);
539 spin_unlock(&mapping
->page_lock
);
544 * Same as grab_cache_page, but do not wait if the page is unavailable.
545 * This is intended for speculative data generators, where the data can
546 * be regenerated if the page couldn't be grabbed. This routine should
547 * be safe to call while holding the lock for another page.
549 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
550 * and deadlock against the caller's locked page.
553 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
555 struct page
*page
= find_get_page(mapping
, index
);
559 if (!TestSetPageLocked(page
))
561 page_cache_release(page
);
564 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
565 page
= alloc_pages(gfp_mask
, 0);
566 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
567 page_cache_release(page
);
573 EXPORT_SYMBOL(grab_cache_page_nowait
);
576 * This is a generic file read routine, and uses the
577 * mapping->a_ops->readpage() function for the actual low-level
580 * This is really ugly. But the goto's actually try to clarify some
581 * of the logic when it comes to error handling etc.
582 * - note the struct file * is only passed for the use of readpage
584 void do_generic_mapping_read(struct address_space
*mapping
,
585 struct file_ra_state
*ra
,
588 read_descriptor_t
* desc
,
591 struct inode
*inode
= mapping
->host
;
592 unsigned long index
, offset
;
593 struct page
*cached_page
;
597 index
= *ppos
>> PAGE_CACHE_SHIFT
;
598 offset
= *ppos
& ~PAGE_CACHE_MASK
;
602 unsigned long end_index
, nr
, ret
;
603 loff_t isize
= i_size_read(inode
);
605 end_index
= isize
>> PAGE_CACHE_SHIFT
;
607 if (index
> end_index
)
609 nr
= PAGE_CACHE_SIZE
;
610 if (index
== end_index
) {
611 nr
= isize
& ~PAGE_CACHE_MASK
;
617 page_cache_readahead(mapping
, ra
, filp
, index
);
621 page
= find_get_page(mapping
, index
);
622 if (unlikely(page
== NULL
)) {
623 handle_ra_miss(mapping
, ra
, index
);
626 if (!PageUptodate(page
))
627 goto page_not_up_to_date
;
629 /* If users can be writing to this page using arbitrary
630 * virtual addresses, take care about potential aliasing
631 * before reading the page on the kernel side.
633 if (!list_empty(&mapping
->i_mmap_shared
))
634 flush_dcache_page(page
);
637 * Mark the page accessed if we read the beginning.
640 mark_page_accessed(page
);
643 * Ok, we have the page, and it's up-to-date, so
644 * now we can copy it to user space...
646 * The actor routine returns how many bytes were actually used..
647 * NOTE! This may not be the same as how much of a user buffer
648 * we filled up (we may be padding etc), so we can only update
649 * "pos" here (the actor routine has to update the user buffer
650 * pointers and the remaining count).
652 ret
= actor(desc
, page
, offset
, nr
);
654 index
+= offset
>> PAGE_CACHE_SHIFT
;
655 offset
&= ~PAGE_CACHE_MASK
;
657 page_cache_release(page
);
658 if (ret
== nr
&& desc
->count
)
663 if (PageUptodate(page
))
666 /* Get exclusive access to the page ... */
669 /* Did it get unhashed before we got the lock? */
670 if (!page
->mapping
) {
672 page_cache_release(page
);
676 /* Did somebody else fill it already? */
677 if (PageUptodate(page
)) {
683 /* ... and start the actual read. The read will unlock the page. */
684 error
= mapping
->a_ops
->readpage(filp
, page
);
687 if (PageUptodate(page
))
689 wait_on_page_locked(page
);
690 if (PageUptodate(page
))
695 /* UHHUH! A synchronous read error occurred. Report it */
697 page_cache_release(page
);
702 * Ok, it wasn't cached, so we need to create a new
706 cached_page
= page_cache_alloc_cold(mapping
);
708 desc
->error
= -ENOMEM
;
712 error
= add_to_page_cache_lru(cached_page
, mapping
,
715 if (error
== -EEXIST
)
725 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
727 page_cache_release(cached_page
);
731 EXPORT_SYMBOL(do_generic_mapping_read
);
733 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
734 unsigned long offset
, unsigned long size
)
737 unsigned long left
, count
= desc
->count
;
743 * Faults on the destination of a read are common, so do it before
746 if (!fault_in_pages_writeable(desc
->buf
, size
)) {
747 kaddr
= kmap_atomic(page
, KM_USER0
);
748 left
= __copy_to_user(desc
->buf
, kaddr
+ offset
, size
);
749 kunmap_atomic(kaddr
, KM_USER0
);
754 /* Do it the slow way */
756 left
= __copy_to_user(desc
->buf
, kaddr
+ offset
, size
);
761 desc
->error
= -EFAULT
;
764 desc
->count
= count
- size
;
765 desc
->written
+= size
;
771 * This is the "read()" routine for all filesystems
772 * that can use the page cache directly.
775 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
776 unsigned long nr_segs
, loff_t
*ppos
)
778 struct file
*filp
= iocb
->ki_filp
;
784 for (seg
= 0; seg
< nr_segs
; seg
++) {
785 const struct iovec
*iv
= &iov
[seg
];
788 * If any segment has a negative length, or the cumulative
789 * length ever wraps negative then return -EINVAL.
791 count
+= iv
->iov_len
;
792 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
794 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
799 count
-= iv
->iov_len
; /* This segment is no good */
803 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
804 if (filp
->f_flags
& O_DIRECT
) {
805 loff_t pos
= *ppos
, size
;
806 struct address_space
*mapping
;
809 mapping
= filp
->f_mapping
;
810 inode
= mapping
->host
;
813 goto out
; /* skip atime */
814 size
= i_size_read(inode
);
816 retval
= generic_file_direct_IO(READ
, iocb
,
818 if (retval
>= 0 && !is_sync_kiocb(iocb
))
819 retval
= -EIOCBQUEUED
;
821 *ppos
= pos
+ retval
;
829 for (seg
= 0; seg
< nr_segs
; seg
++) {
830 read_descriptor_t desc
;
833 desc
.buf
= iov
[seg
].iov_base
;
834 desc
.count
= iov
[seg
].iov_len
;
838 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
839 retval
+= desc
.written
;
850 EXPORT_SYMBOL(__generic_file_aio_read
);
853 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
855 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
857 BUG_ON(iocb
->ki_pos
!= pos
);
858 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
861 EXPORT_SYMBOL(generic_file_aio_read
);
864 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
866 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
870 init_sync_kiocb(&kiocb
, filp
);
871 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
872 if (-EIOCBQUEUED
== ret
)
873 ret
= wait_on_sync_kiocb(&kiocb
);
877 EXPORT_SYMBOL(generic_file_read
);
879 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
882 unsigned long count
= desc
->count
;
883 struct file
*file
= (struct file
*) desc
->buf
;
888 written
= file
->f_op
->sendpage(file
, page
, offset
,
889 size
, &file
->f_pos
, size
<count
);
891 desc
->error
= written
;
894 desc
->count
= count
- written
;
895 desc
->written
+= written
;
899 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
900 size_t count
, read_actor_t actor
, void __user
*target
)
902 read_descriptor_t desc
;
912 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
918 EXPORT_SYMBOL(generic_file_sendfile
);
921 do_readahead(struct address_space
*mapping
, struct file
*filp
,
922 unsigned long index
, unsigned long nr
)
924 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
927 force_page_cache_readahead(mapping
, filp
, index
,
928 max_sane_readahead(nr
));
932 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
940 if (file
->f_mode
& FMODE_READ
) {
941 struct address_space
*mapping
= file
->f_mapping
;
942 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
943 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
944 unsigned long len
= end
- start
+ 1;
945 ret
= do_readahead(mapping
, file
, start
, len
);
954 * This adds the requested page to the page cache if it isn't already there,
955 * and schedules an I/O to read in its contents from disk.
957 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
958 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
960 struct address_space
*mapping
= file
->f_mapping
;
964 page
= page_cache_alloc_cold(mapping
);
968 error
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
970 error
= mapping
->a_ops
->readpage(file
, page
);
971 page_cache_release(page
);
976 * We arrive here in the unlikely event that someone
977 * raced with us and added our page to the cache first
978 * or we are out of memory for radix-tree nodes.
980 page_cache_release(page
);
981 return error
== -EEXIST
? 0 : error
;
984 #define MMAP_READAROUND (16UL)
985 #define MMAP_LOTSAMISS (100)
988 * filemap_nopage() is invoked via the vma operations vector for a
989 * mapped memory region to read in file data during a page fault.
991 * The goto's are kind of ugly, but this streamlines the normal case of having
992 * it in the page cache, and handles the special cases reasonably without
993 * having a lot of duplicated code.
995 struct page
* filemap_nopage(struct vm_area_struct
* area
, unsigned long address
, int *type
)
998 struct file
*file
= area
->vm_file
;
999 struct address_space
*mapping
= file
->f_mapping
;
1000 struct file_ra_state
*ra
= &file
->f_ra
;
1001 struct inode
*inode
= mapping
->host
;
1003 unsigned long size
, pgoff
, endoff
;
1004 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1006 pgoff
= ((address
- area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1007 endoff
= ((area
->vm_end
- area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1010 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1012 goto outside_data_content
;
1014 /* If we don't want any read-ahead, don't bother */
1015 if (VM_RandomReadHint(area
))
1016 goto no_cached_page
;
1019 * The "size" of the file, as far as mmap is concerned, isn't bigger
1026 * The readahead code wants to be told about each and every page
1027 * so it can build and shrink its windows appropriately
1029 * For sequential accesses, we use the generic readahead logic.
1031 if (VM_SequentialReadHint(area
))
1032 page_cache_readahead(mapping
, ra
, file
, pgoff
);
1035 * Do we have something in the page cache already?
1038 page
= find_get_page(mapping
, pgoff
);
1040 if (VM_SequentialReadHint(area
)) {
1041 handle_ra_miss(mapping
, ra
, pgoff
);
1042 goto no_cached_page
;
1047 * Do we miss much more than hit in this file? If so,
1048 * stop bothering with read-ahead. It will only hurt.
1050 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1051 goto no_cached_page
;
1054 * To keep the pgmajfault counter straight, we need to
1055 * check did_readaround, as this is an inner loop.
1057 if (!did_readaround
) {
1058 majmin
= VM_FAULT_MAJOR
;
1059 inc_page_state(pgmajfault
);
1062 do_page_cache_readahead(mapping
, file
,
1063 pgoff
& ~(MMAP_READAROUND
-1), MMAP_READAROUND
);
1067 if (!did_readaround
)
1071 * Ok, found a page in the page cache, now we need to check
1072 * that it's up-to-date.
1074 if (!PageUptodate(page
))
1075 goto page_not_uptodate
;
1079 * Found the page and have a reference on it.
1081 mark_page_accessed(page
);
1086 outside_data_content
:
1088 * An external ptracer can access pages that normally aren't
1091 if (area
->vm_mm
== current
->mm
)
1093 /* Fall through to the non-read-ahead case */
1096 * We're only likely to ever get here if MADV_RANDOM is in
1099 error
= page_cache_read(file
, pgoff
);
1102 * The page we want has now been added to the page cache.
1103 * In the unlikely event that someone removed it in the
1104 * meantime, we'll just come back here and read it again.
1110 * An error return from page_cache_read can result if the
1111 * system is low on memory, or a problem occurs while trying
1114 if (error
== -ENOMEM
)
1119 if (!did_readaround
) {
1120 majmin
= VM_FAULT_MAJOR
;
1121 inc_page_state(pgmajfault
);
1125 /* Did it get unhashed while we waited for it? */
1126 if (!page
->mapping
) {
1128 page_cache_release(page
);
1132 /* Did somebody else get it up-to-date? */
1133 if (PageUptodate(page
)) {
1138 if (!mapping
->a_ops
->readpage(file
, page
)) {
1139 wait_on_page_locked(page
);
1140 if (PageUptodate(page
))
1145 * Umm, take care of errors if the page isn't up-to-date.
1146 * Try to re-read it _once_. We do this synchronously,
1147 * because there really aren't any performance issues here
1148 * and we need to check for errors.
1152 /* Somebody truncated the page on us? */
1153 if (!page
->mapping
) {
1155 page_cache_release(page
);
1159 /* Somebody else successfully read it in? */
1160 if (PageUptodate(page
)) {
1164 ClearPageError(page
);
1165 if (!mapping
->a_ops
->readpage(file
, page
)) {
1166 wait_on_page_locked(page
);
1167 if (PageUptodate(page
))
1172 * Things didn't work out. Return zero to tell the
1173 * mm layer so, possibly freeing the page cache page first.
1175 page_cache_release(page
);
1179 EXPORT_SYMBOL(filemap_nopage
);
1181 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1184 struct address_space
*mapping
= file
->f_mapping
;
1189 * Do we have something in the page cache already?
1192 page
= find_get_page(mapping
, pgoff
);
1196 goto no_cached_page
;
1200 * Ok, found a page in the page cache, now we need to check
1201 * that it's up-to-date.
1203 if (!PageUptodate(page
))
1204 goto page_not_uptodate
;
1208 * Found the page and have a reference on it.
1210 mark_page_accessed(page
);
1214 error
= page_cache_read(file
, pgoff
);
1217 * The page we want has now been added to the page cache.
1218 * In the unlikely event that someone removed it in the
1219 * meantime, we'll just come back here and read it again.
1225 * An error return from page_cache_read can result if the
1226 * system is low on memory, or a problem occurs while trying
1234 /* Did it get unhashed while we waited for it? */
1235 if (!page
->mapping
) {
1240 /* Did somebody else get it up-to-date? */
1241 if (PageUptodate(page
)) {
1246 if (!mapping
->a_ops
->readpage(file
, page
)) {
1247 wait_on_page_locked(page
);
1248 if (PageUptodate(page
))
1253 * Umm, take care of errors if the page isn't up-to-date.
1254 * Try to re-read it _once_. We do this synchronously,
1255 * because there really aren't any performance issues here
1256 * and we need to check for errors.
1260 /* Somebody truncated the page on us? */
1261 if (!page
->mapping
) {
1265 /* Somebody else successfully read it in? */
1266 if (PageUptodate(page
)) {
1271 ClearPageError(page
);
1272 if (!mapping
->a_ops
->readpage(file
, page
)) {
1273 wait_on_page_locked(page
);
1274 if (PageUptodate(page
))
1279 * Things didn't work out. Return zero to tell the
1280 * mm layer so, possibly freeing the page cache page first.
1283 page_cache_release(page
);
1288 static int filemap_populate(struct vm_area_struct
*vma
,
1292 unsigned long pgoff
,
1295 struct file
*file
= vma
->vm_file
;
1296 struct address_space
*mapping
= file
->f_mapping
;
1297 struct inode
*inode
= mapping
->host
;
1299 struct mm_struct
*mm
= vma
->vm_mm
;
1304 force_page_cache_readahead(mapping
, vma
->vm_file
,
1305 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1308 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1309 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1312 page
= filemap_getpage(file
, pgoff
, nonblock
);
1313 if (!page
&& !nonblock
)
1316 err
= install_page(mm
, vma
, addr
, page
, prot
);
1318 page_cache_release(page
);
1323 * If a nonlinear mapping then store the file page offset
1326 unsigned long pgidx
;
1327 pgidx
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1328 pgidx
+= vma
->vm_pgoff
;
1329 pgidx
>>= PAGE_CACHE_SHIFT
- PAGE_SHIFT
;
1330 if (pgoff
!= pgidx
) {
1331 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1346 static struct vm_operations_struct generic_file_vm_ops
= {
1347 .nopage
= filemap_nopage
,
1348 .populate
= filemap_populate
,
1351 /* This is used for a general mmap of a disk file */
1353 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1355 struct address_space
*mapping
= file
->f_mapping
;
1357 if (!mapping
->a_ops
->readpage
)
1359 file_accessed(file
);
1360 vma
->vm_ops
= &generic_file_vm_ops
;
1365 * This is for filesystems which do not implement ->writepage.
1367 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1369 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1371 return generic_file_mmap(file
, vma
);
1374 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1378 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1382 #endif /* CONFIG_MMU */
1384 EXPORT_SYMBOL(generic_file_mmap
);
1385 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1387 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1388 unsigned long index
,
1389 int (*filler
)(void *,struct page
*),
1392 struct page
*page
, *cached_page
= NULL
;
1395 page
= find_get_page(mapping
, index
);
1398 cached_page
= page_cache_alloc_cold(mapping
);
1400 return ERR_PTR(-ENOMEM
);
1402 err
= add_to_page_cache_lru(cached_page
, mapping
,
1407 /* Presumably ENOMEM for radix tree node */
1408 page_cache_release(cached_page
);
1409 return ERR_PTR(err
);
1413 err
= filler(data
, page
);
1415 page_cache_release(page
);
1416 page
= ERR_PTR(err
);
1420 page_cache_release(cached_page
);
1425 * Read into the page cache. If a page already exists,
1426 * and PageUptodate() is not set, try to fill the page.
1428 struct page
*read_cache_page(struct address_space
*mapping
,
1429 unsigned long index
,
1430 int (*filler
)(void *,struct page
*),
1437 page
= __read_cache_page(mapping
, index
, filler
, data
);
1440 mark_page_accessed(page
);
1441 if (PageUptodate(page
))
1445 if (!page
->mapping
) {
1447 page_cache_release(page
);
1450 if (PageUptodate(page
)) {
1454 err
= filler(data
, page
);
1456 page_cache_release(page
);
1457 page
= ERR_PTR(err
);
1463 EXPORT_SYMBOL(read_cache_page
);
1466 * If the page was newly created, increment its refcount and add it to the
1467 * caller's lru-buffering pagevec. This function is specifically for
1468 * generic_file_write().
1470 static inline struct page
*
1471 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1472 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1477 page
= find_lock_page(mapping
, index
);
1479 if (!*cached_page
) {
1480 *cached_page
= page_cache_alloc(mapping
);
1484 err
= add_to_page_cache(*cached_page
, mapping
,
1489 page
= *cached_page
;
1490 page_cache_get(page
);
1491 if (!pagevec_add(lru_pvec
, page
))
1492 __pagevec_lru_add(lru_pvec
);
1493 *cached_page
= NULL
;
1500 * The logic we want is
1502 * if suid or (sgid and xgrp)
1505 int remove_suid(struct dentry
*dentry
)
1507 mode_t mode
= dentry
->d_inode
->i_mode
;
1511 /* suid always must be killed */
1512 if (unlikely(mode
& S_ISUID
))
1513 kill
= ATTR_KILL_SUID
;
1516 * sgid without any exec bits is just a mandatory locking mark; leave
1517 * it alone. If some exec bits are set, it's a real sgid; kill it.
1519 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1520 kill
|= ATTR_KILL_SGID
;
1522 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1523 struct iattr newattrs
;
1525 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1526 result
= notify_change(dentry
, &newattrs
);
1530 EXPORT_SYMBOL(remove_suid
);
1533 * Copy as much as we can into the page and return the number of bytes which
1534 * were sucessfully copied. If a fault is encountered then clear the page
1535 * out to (offset+bytes) and return the number of bytes which were copied.
1537 static inline size_t
1538 filemap_copy_from_user(struct page
*page
, unsigned long offset
,
1539 const char __user
*buf
, unsigned bytes
)
1544 kaddr
= kmap_atomic(page
, KM_USER0
);
1545 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
1546 kunmap_atomic(kaddr
, KM_USER0
);
1549 /* Do it the slow way */
1551 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
1554 return bytes
- left
;
1558 __filemap_copy_from_user_iovec(char *vaddr
,
1559 const struct iovec
*iov
, size_t base
, size_t bytes
)
1561 size_t copied
= 0, left
= 0;
1564 char __user
*buf
= iov
->iov_base
+ base
;
1565 int copy
= min(bytes
, iov
->iov_len
- base
);
1568 left
= __copy_from_user(vaddr
, buf
, copy
);
1574 if (unlikely(left
)) {
1575 /* zero the rest of the target like __copy_from_user */
1577 memset(vaddr
, 0, bytes
);
1581 return copied
- left
;
1585 * This has the same sideeffects and return value as filemap_copy_from_user().
1586 * The difference is that on a fault we need to memset the remainder of the
1587 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1588 * single-segment behaviour.
1590 static inline size_t
1591 filemap_copy_from_user_iovec(struct page
*page
, unsigned long offset
,
1592 const struct iovec
*iov
, size_t base
, size_t bytes
)
1597 kaddr
= kmap_atomic(page
, KM_USER0
);
1598 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1600 kunmap_atomic(kaddr
, KM_USER0
);
1601 if (copied
!= bytes
) {
1603 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1611 filemap_set_next_iovec(const struct iovec
**iovp
, size_t *basep
, size_t bytes
)
1613 const struct iovec
*iov
= *iovp
;
1614 size_t base
= *basep
;
1617 int copy
= min(bytes
, iov
->iov_len
- base
);
1621 if (iov
->iov_len
== base
) {
1631 * Performs necessary checks before doing a write
1633 * Can adjust writing position aor amount of bytes to write.
1634 * Returns appropriate error code that caller should return or
1635 * zero in case that write should be allowed.
1637 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1639 struct inode
*inode
= file
->f_mapping
->host
;
1640 unsigned long limit
= current
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1642 if (unlikely(*pos
< 0))
1645 if (unlikely(file
->f_error
)) {
1646 int err
= file
->f_error
;
1652 /* FIXME: this is for backwards compatibility with 2.4 */
1653 if (file
->f_flags
& O_APPEND
)
1654 *pos
= i_size_read(inode
);
1656 if (limit
!= RLIM_INFINITY
) {
1657 if (*pos
>= limit
) {
1658 send_sig(SIGXFSZ
, current
, 0);
1661 if (*count
> limit
- (typeof(limit
))*pos
) {
1662 *count
= limit
- (typeof(limit
))*pos
;
1670 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1671 !(file
->f_flags
& O_LARGEFILE
))) {
1672 if (*pos
>= MAX_NON_LFS
) {
1673 send_sig(SIGXFSZ
, current
, 0);
1676 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1677 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1682 * Are we about to exceed the fs block limit ?
1684 * If we have written data it becomes a short write. If we have
1685 * exceeded without writing data we send a signal and return EFBIG.
1686 * Linus frestrict idea will clean these up nicely..
1688 if (likely(!isblk
)) {
1689 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1690 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1691 send_sig(SIGXFSZ
, current
, 0);
1694 /* zero-length writes at ->s_maxbytes are OK */
1697 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1698 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1701 if (bdev_read_only(I_BDEV(inode
)))
1703 isize
= i_size_read(inode
);
1704 if (*pos
>= isize
) {
1705 if (*count
|| *pos
> isize
)
1709 if (*pos
+ *count
> isize
)
1710 *count
= isize
- *pos
;
1715 EXPORT_SYMBOL(generic_write_checks
);
1718 * Write to a file through the page cache.
1720 * We put everything into the page cache prior to writing it. This is not a
1721 * problem when writing full pages. With partial pages, however, we first have
1722 * to read the data into the cache, then dirty the page, and finally schedule
1723 * it for writing by marking it dirty.
1727 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
1728 unsigned long nr_segs
, loff_t
*ppos
)
1730 struct file
*file
= iocb
->ki_filp
;
1731 struct address_space
* mapping
= file
->f_mapping
;
1732 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1733 size_t ocount
; /* original count */
1734 size_t count
; /* after file limit checks */
1735 struct inode
*inode
= mapping
->host
;
1739 struct page
*cached_page
= NULL
;
1740 const int isblk
= S_ISBLK(inode
->i_mode
);
1744 struct pagevec lru_pvec
;
1745 const struct iovec
*cur_iov
= iov
; /* current iovec */
1746 size_t iov_base
= 0; /* offset in the current iovec */
1751 for (seg
= 0; seg
< nr_segs
; seg
++) {
1752 const struct iovec
*iv
= &iov
[seg
];
1755 * If any segment has a negative length, or the cumulative
1756 * length ever wraps negative then return -EINVAL.
1758 ocount
+= iv
->iov_len
;
1759 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
1761 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
1766 ocount
-= iv
->iov_len
; /* This segment is no good */
1772 pagevec_init(&lru_pvec
, 0);
1774 /* We can write back this queue in page reclaim */
1775 current
->backing_dev_info
= mapping
->backing_dev_info
;
1778 err
= generic_write_checks(file
, &pos
, &count
, isblk
);
1785 err
= remove_suid(file
->f_dentry
);
1789 inode_update_time(inode
, 1);
1791 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1792 if (unlikely(file
->f_flags
& O_DIRECT
)) {
1793 if (count
!= ocount
)
1794 nr_segs
= iov_shorten((struct iovec
*)iov
,
1796 written
= generic_file_direct_IO(WRITE
, iocb
,
1799 loff_t end
= pos
+ written
;
1800 if (end
> i_size_read(inode
) && !isblk
) {
1801 i_size_write(inode
, end
);
1802 mark_inode_dirty(inode
);
1807 * Sync the fs metadata but not the minor inode changes and
1808 * of course not the data as we did direct DMA for the IO.
1810 if (written
>= 0 && file
->f_flags
& O_SYNC
)
1811 status
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1812 if (written
>= 0 && !is_sync_kiocb(iocb
))
1813 written
= -EIOCBQUEUED
;
1817 buf
= iov
->iov_base
;
1819 unsigned long index
;
1820 unsigned long offset
;
1823 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1824 index
= pos
>> PAGE_CACHE_SHIFT
;
1825 bytes
= PAGE_CACHE_SIZE
- offset
;
1830 * Bring in the user page that we will copy from _first_.
1831 * Otherwise there's a nasty deadlock on copying from the
1832 * same page as we're writing to, without it being marked
1835 fault_in_pages_readable(buf
, bytes
);
1837 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1843 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1844 if (unlikely(status
)) {
1845 loff_t isize
= i_size_read(inode
);
1847 * prepare_write() may have instantiated a few blocks
1848 * outside i_size. Trim these off again.
1851 page_cache_release(page
);
1852 if (pos
+ bytes
> isize
)
1853 vmtruncate(inode
, isize
);
1856 if (likely(nr_segs
== 1))
1857 copied
= filemap_copy_from_user(page
, offset
,
1860 copied
= filemap_copy_from_user_iovec(page
, offset
,
1861 cur_iov
, iov_base
, bytes
);
1862 flush_dcache_page(page
);
1863 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
1864 if (likely(copied
> 0)) {
1873 if (unlikely(nr_segs
> 1))
1874 filemap_set_next_iovec(&cur_iov
,
1878 if (unlikely(copied
!= bytes
))
1882 mark_page_accessed(page
);
1883 page_cache_release(page
);
1886 balance_dirty_pages_ratelimited(mapping
);
1892 page_cache_release(cached_page
);
1895 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
1898 if ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))
1899 status
= generic_osync_inode(inode
, mapping
,
1900 OSYNC_METADATA
|OSYNC_DATA
);
1904 err
= written
? written
: status
;
1906 pagevec_lru_add(&lru_pvec
);
1907 current
->backing_dev_info
= 0;
1911 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
1914 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
1915 unsigned long nr_segs
, loff_t
*ppos
)
1920 init_sync_kiocb(&kiocb
, file
);
1921 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
1922 if (-EIOCBQUEUED
== ret
)
1923 ret
= wait_on_sync_kiocb(&kiocb
);
1927 EXPORT_SYMBOL(generic_file_write_nolock
);
1929 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
1930 size_t count
, loff_t pos
)
1932 struct file
*file
= iocb
->ki_filp
;
1933 struct inode
*inode
= file
->f_mapping
->host
;
1935 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1937 BUG_ON(iocb
->ki_pos
!= pos
);
1939 down(&inode
->i_sem
);
1940 err
= generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
1947 EXPORT_SYMBOL(generic_file_aio_write
);
1949 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
1950 size_t count
, loff_t
*ppos
)
1952 struct inode
*inode
= file
->f_mapping
->host
;
1954 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1956 down(&inode
->i_sem
);
1957 err
= generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
1963 EXPORT_SYMBOL(generic_file_write
);
1965 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
1966 unsigned long nr_segs
, loff_t
*ppos
)
1971 init_sync_kiocb(&kiocb
, filp
);
1972 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
1973 if (-EIOCBQUEUED
== ret
)
1974 ret
= wait_on_sync_kiocb(&kiocb
);
1978 EXPORT_SYMBOL(generic_file_readv
);
1980 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
1981 unsigned long nr_segs
, loff_t
* ppos
)
1983 struct inode
*inode
= file
->f_mapping
->host
;
1986 down(&inode
->i_sem
);
1987 ret
= generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
1992 EXPORT_SYMBOL(generic_file_writev
);
1995 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
1996 loff_t offset
, unsigned long nr_segs
)
1998 struct file
*file
= iocb
->ki_filp
;
1999 struct address_space
*mapping
= file
->f_mapping
;
2002 if (mapping
->nrpages
) {
2003 retval
= filemap_fdatawrite(mapping
);
2005 retval
= filemap_fdatawait(mapping
);
2010 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
, offset
, nr_segs
);
2011 if (rw
== WRITE
&& mapping
->nrpages
)
2012 invalidate_inode_pages2(mapping
);
2017 EXPORT_SYMBOL_GPL(generic_file_direct_IO
);