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>
30 #include <linux/syscalls.h>
32 * This is needed for the following functions:
33 * - try_to_release_page
34 * - block_invalidatepage
35 * - generic_osync_inode
37 * FIXME: remove all knowledge of the buffer layer from the core VM
39 #include <linux/buffer_head.h> /* for generic_osync_inode */
41 #include <asm/uaccess.h>
45 * Shared mappings implemented 30.11.1994. It's not fully working yet,
48 * Shared mappings now work. 15.8.1995 Bruno.
50 * finished 'unifying' the page and buffer cache and SMP-threaded the
51 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
53 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
59 * ->i_mmap_lock (vmtruncate)
60 * ->private_lock (__free_pte->__set_page_dirty_buffers)
62 * ->swap_device_lock (exclusive_swap_page, others)
63 * ->mapping->tree_lock
66 * ->i_mmap_lock (truncate->unmap_mapping_range)
70 * ->page_table_lock (various places, mainly in mmap.c)
71 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
74 * ->lock_page (access_process_vm)
80 * ->i_alloc_sem (various)
83 * ->sb_lock (fs/fs-writeback.c)
84 * ->mapping->tree_lock (__sync_single_inode)
87 * ->anon_vma.lock (vma_adjust)
90 * ->page_table_lock (anon_vma_prepare and various)
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)
104 * ->dcache_lock (proc_pid_lookup)
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.
112 void __remove_from_page_cache(struct page *page)
114 struct address_space *mapping = page->mapping;
116 radix_tree_delete(&mapping->page_tree, page->index);
117 page->mapping = NULL;
122 void remove_from_page_cache(struct page *page)
124 struct address_space *mapping = page->mapping;
126 if (unlikely(!PageLocked(page)))
129 spin_lock_irq(&mapping->tree_lock);
130 __remove_from_page_cache(page);
131 spin_unlock_irq(&mapping->tree_lock);
134 static int sync_page(void *word)
136 struct address_space *mapping;
139 page = container_of((page_flags_t *)word, struct page, flags);
142 * FIXME, fercrissake. What is this barrier here for?
145 mapping = page_mapping(page);
146 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
147 mapping->a_ops->sync_page(page);
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
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.
164 static int __filemap_fdatawrite_range(struct address_space *mapping,
165 loff_t start, loff_t end, int sync_mode)
168 struct writeback_control wbc = {
169 .sync_mode = sync_mode,
170 .nr_to_write = mapping->nrpages * 2,
175 if (mapping->backing_dev_info->memory_backed)
178 ret = do_writepages(mapping, &wbc);
182 static inline int __filemap_fdatawrite(struct address_space *mapping,
185 return __filemap_fdatawrite_range(mapping, 0, 0, sync_mode);
188 int filemap_fdatawrite(struct address_space *mapping)
190 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
192 EXPORT_SYMBOL(filemap_fdatawrite);
194 static int filemap_fdatawrite_range(struct address_space *mapping,
195 loff_t start, loff_t end)
197 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
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.
204 int filemap_flush(struct address_space *mapping)
206 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
208 EXPORT_SYMBOL(filemap_flush);
211 * Wait for writeback to complete against pages indexed by start->end
214 static int wait_on_page_writeback_range(struct address_space *mapping,
215 pgoff_t start, pgoff_t end)
225 pagevec_init(&pvec, 0);
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) {
233 for (i = 0; i < nr_pages; i++) {
234 struct page *page = pvec.pages[i];
236 /* until radix tree lookup accepts end_index */
237 if (page->index > end)
240 wait_on_page_writeback(page);
244 pagevec_release(&pvec);
248 /* Check for outstanding write errors */
249 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
251 if (test_and_clear_bit(AS_EIO, &mapping->flags))
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.
262 * We need to re-take i_sem during the generic_osync_inode list walk because
263 * it is otherwise livelockable.
265 int sync_page_range(struct inode *inode, struct address_space *mapping,
266 loff_t pos, size_t count)
268 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
269 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
272 if (mapping->backing_dev_info->memory_backed || !count)
274 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
277 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
281 ret = wait_on_page_writeback_range(mapping, start, end);
284 EXPORT_SYMBOL(sync_page_range);
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.
291 int sync_page_range_nolock(struct inode *inode, struct address_space *mapping,
292 loff_t pos, size_t count)
294 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
295 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
298 if (mapping->backing_dev_info->memory_backed || !count)
300 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
302 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
304 ret = wait_on_page_writeback_range(mapping, start, end);
307 EXPORT_SYMBOL(sync_page_range_nolock);
310 * filemap_fdatawait - walk the list of under-writeback pages of the given
311 * address space and wait for all of them.
313 * @mapping: address space structure to wait for
315 int filemap_fdatawait(struct address_space *mapping)
317 loff_t i_size = i_size_read(mapping->host);
322 return wait_on_page_writeback_range(mapping, 0,
323 (i_size - 1) >> PAGE_CACHE_SHIFT);
325 EXPORT_SYMBOL(filemap_fdatawait);
327 int filemap_write_and_wait(struct address_space *mapping)
331 if (mapping->nrpages) {
332 retval = filemap_fdatawrite(mapping);
334 retval = filemap_fdatawait(mapping);
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().
344 * This function does not add the page to the LRU. The caller must do that.
346 int add_to_page_cache(struct page *page, struct address_space *mapping,
347 pgoff_t offset, int gfp_mask)
349 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
352 spin_lock_irq(&mapping->tree_lock);
353 error = radix_tree_insert(&mapping->page_tree, offset, page);
355 page_cache_get(page);
357 page->mapping = mapping;
358 page->index = offset;
362 spin_unlock_irq(&mapping->tree_lock);
363 radix_tree_preload_end();
368 EXPORT_SYMBOL(add_to_page_cache);
370 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
371 pgoff_t offset, int gfp_mask)
373 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
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
389 static wait_queue_head_t *page_waitqueue(struct page *page)
391 const struct zone *zone = page_zone(page);
393 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
396 static inline void wake_up_page(struct page *page, int bit)
398 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
401 void fastcall wait_on_page_bit(struct page *page, int bit_nr)
403 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
405 if (test_bit(bit_nr, &page->flags))
406 __wait_on_bit(page_waitqueue(page), &wait, sync_page,
407 TASK_UNINTERRUPTIBLE);
409 EXPORT_SYMBOL(wait_on_page_bit);
412 * unlock_page() - unlock a locked page
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.
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()).
426 void fastcall unlock_page(struct page *page)
428 smp_mb__before_clear_bit();
429 if (!TestClearPageLocked(page))
431 smp_mb__after_clear_bit();
432 wake_up_page(page, PG_locked);
434 EXPORT_SYMBOL(unlock_page);
437 * End writeback against a page.
439 void end_page_writeback(struct page *page)
441 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
442 if (!test_clear_page_writeback(page))
445 smp_mb__after_clear_bit();
446 wake_up_page(page, PG_writeback);
448 EXPORT_SYMBOL(end_page_writeback);
451 * Get a lock on the page, assuming we need to sleep to get it.
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.
458 void fastcall __lock_page(struct page *page)
460 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
462 __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
463 TASK_UNINTERRUPTIBLE);
465 EXPORT_SYMBOL(__lock_page);
468 * a rather lightweight function, finding and getting a reference to a
469 * hashed page atomically.
471 struct page * find_get_page(struct address_space *mapping, unsigned long offset)
475 spin_lock_irq(&mapping->tree_lock);
476 page = radix_tree_lookup(&mapping->page_tree, offset);
478 page_cache_get(page);
479 spin_unlock_irq(&mapping->tree_lock);
483 EXPORT_SYMBOL(find_get_page);
486 * Same as above, but trylock it instead of incrementing the count.
488 struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
492 spin_lock_irq(&mapping->tree_lock);
493 page = radix_tree_lookup(&mapping->page_tree, offset);
494 if (page && TestSetPageLocked(page))
496 spin_unlock_irq(&mapping->tree_lock);
500 EXPORT_SYMBOL(find_trylock_page);
503 * find_lock_page - locate, pin and lock a pagecache page
505 * @mapping - the address_space to search
506 * @offset - the page index
508 * Locates the desired pagecache page, locks it, increments its reference
509 * count and returns its address.
511 * Returns zero if the page was not present. find_lock_page() may sleep.
513 struct page *find_lock_page(struct address_space *mapping,
514 unsigned long offset)
518 spin_lock_irq(&mapping->tree_lock);
520 page = radix_tree_lookup(&mapping->page_tree, offset);
522 page_cache_get(page);
523 if (TestSetPageLocked(page)) {
524 spin_unlock_irq(&mapping->tree_lock);
526 spin_lock_irq(&mapping->tree_lock);
528 /* Has the page been truncated while we slept? */
529 if (page->mapping != mapping || page->index != offset) {
531 page_cache_release(page);
536 spin_unlock_irq(&mapping->tree_lock);
540 EXPORT_SYMBOL(find_lock_page);
543 * find_or_create_page - locate or add a pagecache page
545 * @mapping - the page's address_space
546 * @index - the page's index into the mapping
547 * @gfp_mask - page allocation mode
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
554 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
557 * find_or_create_page() returns the desired page's address, or zero on
560 struct page *find_or_create_page(struct address_space *mapping,
561 unsigned long index, unsigned int gfp_mask)
563 struct page *page, *cached_page = NULL;
566 page = find_lock_page(mapping, index);
569 cached_page = alloc_page(gfp_mask);
573 err = add_to_page_cache_lru(cached_page, mapping,
578 } else if (err == -EEXIST)
582 page_cache_release(cached_page);
586 EXPORT_SYMBOL(find_or_create_page);
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
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.
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.
602 * find_get_pages() returns the number of pages which were found.
604 unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
605 unsigned int nr_pages, struct page **pages)
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);
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.
623 unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
624 int tag, unsigned int nr_pages, struct page **pages)
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]);
635 *index = pages[ret - 1]->index + 1;
636 spin_unlock_irq(&mapping->tree_lock);
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.
646 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
647 * and deadlock against the caller's locked page.
650 grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
652 struct page *page = find_get_page(mapping, index);
656 if (!TestSetPageLocked(page))
658 page_cache_release(page);
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);
670 EXPORT_SYMBOL(grab_cache_page_nowait);
673 * This is a generic file read routine, and uses the
674 * mapping->a_ops->readpage() function for the actual low-level
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.
680 * Note the struct file* is only passed for the use of readpage. It may be
683 void do_generic_mapping_read(struct address_space *mapping,
684 struct file_ra_state *_ra,
687 read_descriptor_t *desc,
691 struct inode *inode = mapping->host;
692 unsigned long index, end_index, offset;
694 struct page *cached_page;
696 struct file_ra_state ra = *_ra;
699 index = *ppos >> PAGE_CACHE_SHIFT;
700 offset = *ppos & ~PAGE_CACHE_MASK;
702 isize = i_size_read(inode);
706 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
709 unsigned long nr, ret;
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)
716 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
724 page_cache_readahead(mapping, &ra, filp, index);
727 page = find_get_page(mapping, index);
728 if (unlikely(page == NULL)) {
730 desc->error = -EWOULDBLOCKIO;
733 handle_ra_miss(mapping, &ra, index);
736 if (!PageUptodate(page)) {
738 page_cache_release(page);
739 desc->error = -EWOULDBLOCKIO;
742 goto page_not_up_to_date;
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.
750 if (mapping_writably_mapped(mapping))
751 flush_dcache_page(page);
754 * Mark the page accessed if we read the beginning.
757 mark_page_accessed(page);
760 * Ok, we have the page, and it's up-to-date, so
761 * now we can copy it to user space...
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).
769 ret = actor(desc, page, offset, nr);
771 index += offset >> PAGE_CACHE_SHIFT;
772 offset &= ~PAGE_CACHE_MASK;
774 page_cache_release(page);
775 if (ret == nr && desc->count)
780 /* Get exclusive access to the page ... */
783 /* Did it get unhashed before we got the lock? */
784 if (!page->mapping) {
786 page_cache_release(page);
790 /* Did somebody else fill it already? */
791 if (PageUptodate(page)) {
797 /* Start the actual read. The read will unlock the page. */
798 error = mapping->a_ops->readpage(filp, page);
803 if (!PageUptodate(page)) {
804 wait_on_page_locked(page);
805 if (!PageUptodate(page)) {
812 * i_size must be checked after we have done ->readpage.
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).
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);
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;
831 page_cache_release(page);
839 /* UHHUH! A synchronous read error occurred. Report it */
841 page_cache_release(page);
846 * Ok, it wasn't cached, so we need to create a new
850 cached_page = page_cache_alloc_cold(mapping);
852 desc->error = -ENOMEM;
856 error = add_to_page_cache_lru(cached_page, mapping,
859 if (error == -EEXIST)
872 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
874 page_cache_release(cached_page);
879 EXPORT_SYMBOL(do_generic_mapping_read);
881 int file_read_actor(read_descriptor_t *desc, struct page *page,
882 unsigned long offset, unsigned long size)
885 unsigned long left, count = desc->count;
891 * Faults on the destination of a read are common, so do it before
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);
903 /* Do it the slow way */
905 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
910 desc->error = -EFAULT;
913 desc->count = count - size;
914 desc->written += size;
915 desc->arg.buf += size;
920 * This is the "read()" routine for all filesystems
921 * that can use the page cache directly.
924 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
925 unsigned long nr_segs, loff_t *ppos)
927 struct file *filp = iocb->ki_filp;
933 for (seg = 0; seg < nr_segs; seg++) {
934 const struct iovec *iv = &iov[seg];
937 * If any segment has a negative length, or the cumulative
938 * length ever wraps negative then return -EINVAL.
940 count += iv->iov_len;
941 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
943 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
948 count -= iv->iov_len; /* This segment is no good */
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;
958 mapping = filp->f_mapping;
959 inode = mapping->host;
962 goto out; /* skip atime */
963 size = i_size_read(inode);
965 retval = generic_file_direct_IO(READ, iocb,
967 if (retval >= 0 && !is_sync_kiocb(iocb))
968 retval = -EIOCBQUEUED;
970 *ppos = pos + retval;
978 for (seg = 0; seg < nr_segs; seg++) {
979 read_descriptor_t desc;
982 desc.arg.buf = iov[seg].iov_base;
983 desc.count = iov[seg].iov_len;
987 do_generic_file_read(filp,ppos,&desc,file_read_actor,0);
988 retval += desc.written;
999 EXPORT_SYMBOL(__generic_file_aio_read);
1002 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1004 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1006 BUG_ON(iocb->ki_pos != pos);
1007 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1010 EXPORT_SYMBOL(generic_file_aio_read);
1013 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1015 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
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);
1026 EXPORT_SYMBOL(generic_file_read);
1028 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1031 unsigned long count = desc->count;
1032 struct file *file = desc->arg.data;
1037 written = file->f_op->sendpage(file, page, offset,
1038 size, &file->f_pos, size<count);
1040 desc->error = written;
1043 desc->count = count - written;
1044 desc->written += written;
1048 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1049 size_t count, read_actor_t actor, void *target)
1051 read_descriptor_t desc;
1058 desc.arg.data = target;
1061 do_generic_file_read(in_file, ppos, &desc, actor, 0);
1063 return desc.written;
1067 EXPORT_SYMBOL(generic_file_sendfile);
1070 do_readahead(struct address_space *mapping, struct file *filp,
1071 unsigned long index, unsigned long nr)
1073 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1076 force_page_cache_readahead(mapping, filp, index,
1077 max_sane_readahead(nr));
1081 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
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);
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.
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)
1109 struct address_space *mapping = file->f_mapping;
1113 page = page_cache_alloc_cold(mapping);
1117 error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1119 error = mapping->a_ops->readpage(file, page);
1120 page_cache_release(page);
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.
1129 page_cache_release(page);
1130 return error == -EEXIST ? 0 : error;
1133 #define MMAP_LOTSAMISS (100)
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.
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.
1143 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int *type)
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;
1151 unsigned long size, pgoff, endoff;
1152 int did_readaround = 0, majmin = VM_FAULT_MINOR;
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;
1158 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1160 goto outside_data_content;
1162 /* If we don't want any read-ahead, don't bother */
1163 if (VM_RandomReadHint(area))
1164 goto no_cached_page;
1167 * The "size" of the file, as far as mmap is concerned, isn't bigger
1174 * The readahead code wants to be told about each and every page
1175 * so it can build and shrink its windows appropriately
1177 * For sequential accesses, we use the generic readahead logic.
1179 if (VM_SequentialReadHint(area))
1180 page_cache_readahead(mapping, ra, file, pgoff);
1183 * Do we have something in the page cache already?
1186 page = find_get_page(mapping, pgoff);
1188 unsigned long ra_pages;
1190 if (VM_SequentialReadHint(area)) {
1191 handle_ra_miss(mapping, ra, pgoff);
1192 goto no_cached_page;
1197 * Do we miss much more than hit in this file? If so,
1198 * stop bothering with read-ahead. It will only hurt.
1200 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1201 goto no_cached_page;
1204 * To keep the pgmajfault counter straight, we need to
1205 * check did_readaround, as this is an inner loop.
1207 if (!did_readaround) {
1208 majmin = VM_FAULT_MAJOR;
1209 inc_page_state(pgmajfault);
1212 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1216 if (pgoff > ra_pages / 2)
1217 start = pgoff - ra_pages / 2;
1218 do_page_cache_readahead(mapping, file, start, ra_pages);
1220 page = find_get_page(mapping, pgoff);
1222 goto no_cached_page;
1225 if (!did_readaround)
1229 * Ok, found a page in the page cache, now we need to check
1230 * that it's up-to-date.
1232 if (!PageUptodate(page))
1233 goto page_not_uptodate;
1237 * Found the page and have a reference on it.
1239 mark_page_accessed(page);
1244 outside_data_content:
1246 * An external ptracer can access pages that normally aren't
1249 if (area->vm_mm == current->mm)
1251 /* Fall through to the non-read-ahead case */
1254 * We're only likely to ever get here if MADV_RANDOM is in
1257 error = page_cache_read(file, pgoff);
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.
1269 * An error return from page_cache_read can result if the
1270 * system is low on memory, or a problem occurs while trying
1273 if (error == -ENOMEM)
1278 if (!did_readaround) {
1279 majmin = VM_FAULT_MAJOR;
1280 inc_page_state(pgmajfault);
1284 /* Did it get unhashed while we waited for it? */
1285 if (!page->mapping) {
1287 page_cache_release(page);
1291 /* Did somebody else get it up-to-date? */
1292 if (PageUptodate(page)) {
1297 if (!mapping->a_ops->readpage(file, page)) {
1298 wait_on_page_locked(page);
1299 if (PageUptodate(page))
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.
1311 /* Somebody truncated the page on us? */
1312 if (!page->mapping) {
1314 page_cache_release(page);
1318 /* Somebody else successfully read it in? */
1319 if (PageUptodate(page)) {
1323 ClearPageError(page);
1324 if (!mapping->a_ops->readpage(file, page)) {
1325 wait_on_page_locked(page);
1326 if (PageUptodate(page))
1331 * Things didn't work out. Return zero to tell the
1332 * mm layer so, possibly freeing the page cache page first.
1334 page_cache_release(page);
1338 EXPORT_SYMBOL(filemap_nopage);
1340 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1343 struct address_space *mapping = file->f_mapping;
1348 * Do we have something in the page cache already?
1351 page = find_get_page(mapping, pgoff);
1355 goto no_cached_page;
1359 * Ok, found a page in the page cache, now we need to check
1360 * that it's up-to-date.
1362 if (!PageUptodate(page))
1363 goto page_not_uptodate;
1367 * Found the page and have a reference on it.
1369 mark_page_accessed(page);
1373 error = page_cache_read(file, pgoff);
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.
1384 * An error return from page_cache_read can result if the
1385 * system is low on memory, or a problem occurs while trying
1393 /* Did it get unhashed while we waited for it? */
1394 if (!page->mapping) {
1399 /* Did somebody else get it up-to-date? */
1400 if (PageUptodate(page)) {
1405 if (!mapping->a_ops->readpage(file, page)) {
1406 wait_on_page_locked(page);
1407 if (PageUptodate(page))
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.
1419 /* Somebody truncated the page on us? */
1420 if (!page->mapping) {
1424 /* Somebody else successfully read it in? */
1425 if (PageUptodate(page)) {
1430 ClearPageError(page);
1431 if (!mapping->a_ops->readpage(file, page)) {
1432 wait_on_page_locked(page);
1433 if (PageUptodate(page))
1438 * Things didn't work out. Return zero to tell the
1439 * mm layer so, possibly freeing the page cache page first.
1442 page_cache_release(page);
1447 static int filemap_populate(struct vm_area_struct *vma,
1451 unsigned long pgoff,
1454 struct file *file = vma->vm_file;
1455 struct address_space *mapping = file->f_mapping;
1456 struct inode *inode = mapping->host;
1458 struct mm_struct *mm = vma->vm_mm;
1463 force_page_cache_readahead(mapping, vma->vm_file,
1464 pgoff, len >> PAGE_CACHE_SHIFT);
1467 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1468 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1471 page = filemap_getpage(file, pgoff, nonblock);
1472 if (!page && !nonblock)
1475 err = install_page(mm, vma, addr, page, prot);
1477 page_cache_release(page);
1481 err = install_file_pte(mm, vma, addr, pgoff, prot);
1495 struct vm_operations_struct generic_file_vm_ops = {
1496 .nopage = filemap_nopage,
1497 .populate = filemap_populate,
1500 /* This is used for a general mmap of a disk file */
1502 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1504 struct address_space *mapping = file->f_mapping;
1506 if (!mapping->a_ops->readpage)
1508 file_accessed(file);
1509 vma->vm_ops = &generic_file_vm_ops;
1514 * This is for filesystems which do not implement ->writepage.
1516 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1518 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1520 return generic_file_mmap(file, vma);
1523 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1527 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1531 #endif /* CONFIG_MMU */
1533 EXPORT_SYMBOL(generic_file_mmap);
1534 EXPORT_SYMBOL(generic_file_readonly_mmap);
1536 static inline struct page *__read_cache_page(struct address_space *mapping,
1537 unsigned long index,
1538 int (*filler)(void *,struct page*),
1541 struct page *page, *cached_page = NULL;
1544 page = find_get_page(mapping, index);
1547 cached_page = page_cache_alloc_cold(mapping);
1549 return ERR_PTR(-ENOMEM);
1551 err = add_to_page_cache_lru(cached_page, mapping,
1556 /* Presumably ENOMEM for radix tree node */
1557 page_cache_release(cached_page);
1558 return ERR_PTR(err);
1562 err = filler(data, page);
1564 page_cache_release(page);
1565 page = ERR_PTR(err);
1569 page_cache_release(cached_page);
1574 * Read into the page cache. If a page already exists,
1575 * and PageUptodate() is not set, try to fill the page.
1577 struct page *read_cache_page(struct address_space *mapping,
1578 unsigned long index,
1579 int (*filler)(void *,struct page*),
1586 page = __read_cache_page(mapping, index, filler, data);
1589 mark_page_accessed(page);
1590 if (PageUptodate(page))
1594 if (!page->mapping) {
1596 page_cache_release(page);
1599 if (PageUptodate(page)) {
1603 err = filler(data, page);
1605 page_cache_release(page);
1606 page = ERR_PTR(err);
1612 EXPORT_SYMBOL(read_cache_page);
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().
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)
1626 page = find_lock_page(mapping, index);
1628 if (!*cached_page) {
1629 *cached_page = page_cache_alloc(mapping);
1633 err = add_to_page_cache(*cached_page, mapping,
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;
1649 * The logic we want is
1651 * if suid or (sgid and xgrp)
1654 int remove_suid(struct dentry *dentry)
1656 mode_t mode = dentry->d_inode->i_mode;
1660 /* suid always must be killed */
1661 if (unlikely(mode & S_ISUID))
1662 kill = ATTR_KILL_SUID;
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.
1668 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1669 kill |= ATTR_KILL_SGID;
1671 if (unlikely(kill && !capable(CAP_FSETID))) {
1672 struct iattr newattrs;
1674 newattrs.ia_valid = ATTR_FORCE | kill;
1675 result = notify_change(dentry, &newattrs);
1679 EXPORT_SYMBOL(remove_suid);
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.
1686 static inline size_t
1687 filemap_copy_from_user(struct page *page, unsigned long offset,
1688 const char __user *buf, unsigned bytes)
1693 kaddr = kmap_atomic(page, KM_USER0);
1694 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
1695 kunmap_atomic(kaddr, KM_USER0);
1698 /* Do it the slow way */
1700 left = __copy_from_user(kaddr + offset, buf, bytes);
1703 return bytes - left;
1707 __filemap_copy_from_user_iovec(char *vaddr,
1708 const struct iovec *iov, size_t base, size_t bytes)
1710 size_t copied = 0, left = 0;
1713 char __user *buf = iov->iov_base + base;
1714 int copy = min(bytes, iov->iov_len - base);
1717 left = __copy_from_user_inatomic(vaddr, buf, copy);
1723 if (unlikely(left)) {
1724 /* zero the rest of the target like __copy_from_user */
1726 memset(vaddr, 0, bytes);
1730 return copied - left;
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.
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)
1746 kaddr = kmap_atomic(page, KM_USER0);
1747 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1749 kunmap_atomic(kaddr, KM_USER0);
1750 if (copied != bytes) {
1752 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1760 filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1762 const struct iovec *iov = *iovp;
1763 size_t base = *basep;
1766 int copy = min(bytes, iov->iov_len - base);
1770 if (iov->iov_len == base) {
1780 * Performs necessary checks before doing a write
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.
1786 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1788 struct inode *inode = file->f_mapping->host;
1789 unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1791 if (unlikely(*pos < 0))
1794 if (unlikely(file->f_error)) {
1795 int err = file->f_error;
1801 /* FIXME: this is for backwards compatibility with 2.4 */
1802 if (file->f_flags & O_APPEND)
1803 *pos = i_size_read(inode);
1805 if (limit != RLIM_INFINITY) {
1806 if (*pos >= limit) {
1807 send_sig(SIGXFSZ, current, 0);
1810 if (*count > limit - (typeof(limit))*pos) {
1811 *count = limit - (typeof(limit))*pos;
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);
1825 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1826 *count = MAX_NON_LFS - (unsigned long)*pos;
1831 * Are we about to exceed the fs block limit ?
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..
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);
1843 /* zero-length writes at ->s_maxbytes are OK */
1846 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1847 *count = inode->i_sb->s_maxbytes - *pos;
1850 if (bdev_read_only(I_BDEV(inode)))
1852 isize = i_size_read(inode);
1853 if (*pos >= isize) {
1854 if (*count || *pos > isize)
1858 if (*pos + *count > isize)
1859 *count = isize - *pos;
1863 EXPORT_SYMBOL(generic_write_checks);
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)
1870 struct file *file = iocb->ki_filp;
1871 struct address_space *mapping = file->f_mapping;
1872 struct inode *inode = mapping->host;
1875 if (count != ocount)
1876 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1878 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
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);
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
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;
1900 EXPORT_SYMBOL(generic_file_direct_write);
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)
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;
1913 struct page *cached_page = NULL;
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 */
1920 pagevec_init(&lru_pvec, 0);
1922 buf = iov->iov_base + written; /* handle partial DIO write */
1924 unsigned long index;
1925 unsigned long offset;
1928 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1929 index = pos >> PAGE_CACHE_SHIFT;
1930 bytes = PAGE_CACHE_SIZE - offset;
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
1940 fault_in_pages_readable(buf, bytes);
1942 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1948 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1949 if (unlikely(status)) {
1950 loff_t isize = i_size_read(inode);
1952 * prepare_write() may have instantiated a few blocks
1953 * outside i_size. Trim these off again.
1956 page_cache_release(page);
1957 if (pos + bytes > isize)
1958 vmtruncate(inode, isize);
1961 if (likely(nr_segs == 1))
1962 copied = filemap_copy_from_user(page, offset,
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)) {
1978 if (unlikely(nr_segs > 1))
1979 filemap_set_next_iovec(&cur_iov,
1983 if (unlikely(copied != bytes))
1987 mark_page_accessed(page);
1988 page_cache_release(page);
1991 balance_dirty_pages_ratelimited(mapping);
1997 page_cache_release(cached_page);
2000 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
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);
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.
2015 if (unlikely(file->f_flags & O_DIRECT) && written)
2016 status = filemap_write_and_wait(mapping);
2018 pagevec_lru_add(&lru_pvec);
2019 return written ? written : status;
2021 EXPORT_SYMBOL(generic_file_buffered_write);
2024 __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2025 unsigned long nr_segs, loff_t *ppos)
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;
2038 for (seg = 0; seg < nr_segs; seg++) {
2039 const struct iovec *iv = &iov[seg];
2042 * If any segment has a negative length, or the cumulative
2043 * length ever wraps negative then return -EINVAL.
2045 ocount += iv->iov_len;
2046 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2048 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2053 ocount -= iv->iov_len; /* This segment is no good */
2060 /* We can write back this queue in page reclaim */
2061 current->backing_dev_info = mapping->backing_dev_info;
2064 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2071 err = remove_suid(file->f_dentry);
2075 inode_update_time(inode, file->f_vfsmnt, 1);
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)
2084 * direct-io write to a hole: fall through to buffered I/O
2085 * for completing the rest of the request.
2091 written = generic_file_buffered_write(iocb, iov, nr_segs,
2092 pos, ppos, count, written);
2094 current->backing_dev_info = NULL;
2095 return written ? written : err;
2097 EXPORT_SYMBOL(generic_file_aio_write_nolock);
2100 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2101 unsigned long nr_segs, loff_t *ppos)
2103 struct file *file = iocb->ki_filp;
2104 struct address_space *mapping = file->f_mapping;
2105 struct inode *inode = mapping->host;
2109 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
2111 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2114 err = sync_page_range_nolock(inode, mapping, pos, ret);
2122 __generic_file_write_nolock(struct file *file, const struct iovec *iov,
2123 unsigned long nr_segs, loff_t *ppos)
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);
2136 generic_file_write_nolock(struct file *file, const struct iovec *iov,
2137 unsigned long nr_segs, loff_t *ppos)
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);
2148 EXPORT_SYMBOL(generic_file_write_nolock);
2150 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2151 size_t count, loff_t pos)
2153 struct file *file = iocb->ki_filp;
2154 struct address_space *mapping = file->f_mapping;
2155 struct inode *inode = mapping->host;
2157 struct iovec local_iov = { .iov_base = (void __user *)buf,
2160 BUG_ON(iocb->ki_pos != pos);
2162 down(&inode->i_sem);
2163 ret = generic_file_aio_write_nolock(iocb, &local_iov, 1,
2167 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2170 err = sync_page_range(inode, mapping, pos, ret);
2176 EXPORT_SYMBOL(generic_file_aio_write);
2178 ssize_t generic_file_write(struct file *file, const char __user *buf,
2179 size_t count, loff_t *ppos)
2181 struct address_space *mapping = file->f_mapping;
2182 struct inode *inode = mapping->host;
2184 struct iovec local_iov = { .iov_base = (void __user *)buf,
2187 down(&inode->i_sem);
2188 ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
2191 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2194 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2200 EXPORT_SYMBOL(generic_file_write);
2202 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2203 unsigned long nr_segs, loff_t *ppos)
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);
2214 EXPORT_SYMBOL(generic_file_readv);
2216 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2217 unsigned long nr_segs, loff_t *ppos)
2219 struct address_space *mapping = file->f_mapping;
2220 struct inode *inode = mapping->host;
2223 down(&inode->i_sem);
2224 ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
2227 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2230 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2236 EXPORT_SYMBOL(generic_file_writev);
2239 * Called under i_sem for writes to S_ISREG files
2242 generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2243 loff_t offset, unsigned long nr_segs)
2245 struct file *file = iocb->ki_filp;
2246 struct address_space *mapping = file->f_mapping;
2249 retval = filemap_write_and_wait(mapping);
2251 retval = mapping->a_ops->direct_IO(rw, iocb, iov,
2253 if (rw == WRITE && mapping->nrpages)
2254 invalidate_inode_pages2(mapping);
2258 EXPORT_SYMBOL_GPL(generic_file_direct_IO);