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_mmap_lock (vmtruncate)
59 * ->private_lock (__free_pte->__set_page_dirty_buffers)
61 * ->swap_device_lock (exclusive_swap_page, others)
62 * ->mapping->tree_lock
65 * ->i_mmap_lock (truncate->unmap_mapping_range)
69 * ->page_table_lock (various places, mainly in mmap.c)
70 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
73 * ->lock_page (access_process_vm)
79 * ->i_alloc_sem (various)
82 * ->sb_lock (fs/fs-writeback.c)
83 * ->mapping->tree_lock (__sync_single_inode)
86 * ->anon_vma.lock (vma_adjust)
89 * ->page_table_lock (anon_vma_prepare and various)
92 * ->swap_device_lock (try_to_unmap_one)
93 * ->private_lock (try_to_unmap_one)
94 * ->tree_lock (try_to_unmap_one)
95 * ->zone.lru_lock (follow_page->mark_page_accessed)
96 * ->private_lock (page_remove_rmap->set_page_dirty)
97 * ->tree_lock (page_remove_rmap->set_page_dirty)
98 * ->inode_lock (page_remove_rmap->set_page_dirty)
99 * ->inode_lock (zap_pte_range->set_page_dirty)
100 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
103 * ->dcache_lock (proc_pid_lookup)
107 * Remove a page from the page cache and free it. Caller has to make
108 * sure the page is locked and that nobody else uses it - or that usage
109 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
111 void __remove_from_page_cache(struct page *page)
113 struct address_space *mapping = page->mapping;
115 radix_tree_delete(&mapping->page_tree, page->index);
116 page->mapping = NULL;
121 void remove_from_page_cache(struct page *page)
123 struct address_space *mapping = page->mapping;
125 if (unlikely(!PageLocked(page)))
128 spin_lock_irq(&mapping->tree_lock);
129 __remove_from_page_cache(page);
130 spin_unlock_irq(&mapping->tree_lock);
133 static inline int sync_page(struct page *page)
135 struct address_space *mapping;
138 * FIXME, fercrissake. What is this barrier here for?
141 mapping = page_mapping(page);
142 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
143 return mapping->a_ops->sync_page(page);
148 * filemap_fdatawrite_range - start writeback against all of a mapping's
149 * dirty pages that lie within the byte offsets <start, end>
150 * @mapping: address space structure to write
151 * @start: offset in bytes where the range starts
152 * @end : offset in bytes where the range ends
154 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
155 * opposed to a regular memory * cleansing writeback. The difference between
156 * these two operations is that if a dirty page/buffer is encountered, it must
157 * be waited upon, and not just skipped over.
159 static int __filemap_fdatawrite_range(struct address_space *mapping,
160 loff_t start, loff_t end, int sync_mode)
163 struct writeback_control wbc = {
164 .sync_mode = sync_mode,
165 .nr_to_write = mapping->nrpages * 2,
170 if (mapping->backing_dev_info->memory_backed)
173 ret = do_writepages(mapping, &wbc);
177 static inline int __filemap_fdatawrite(struct address_space *mapping,
180 return __filemap_fdatawrite_range(mapping, 0, 0, sync_mode);
183 int filemap_fdatawrite(struct address_space *mapping)
185 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
187 EXPORT_SYMBOL(filemap_fdatawrite);
189 static int filemap_fdatawrite_range(struct address_space *mapping,
190 loff_t start, loff_t end)
192 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
196 * This is a mostly non-blocking flush. Not suitable for data-integrity
197 * purposes - I/O may not be started against all dirty pages.
199 int filemap_flush(struct address_space *mapping)
201 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
203 EXPORT_SYMBOL(filemap_flush);
206 * Wait for writeback to complete against pages indexed by start->end
209 static int wait_on_page_writeback_range(struct address_space *mapping,
210 pgoff_t start, pgoff_t end)
220 pagevec_init(&pvec, 0);
222 while ((index <= end) &&
223 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
224 PAGECACHE_TAG_WRITEBACK,
225 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
228 for (i = 0; i < nr_pages; i++) {
229 struct page *page = pvec.pages[i];
231 /* until radix tree lookup accepts end_index */
232 if (page->index > end)
235 wait_on_page_writeback(page);
239 pagevec_release(&pvec);
243 /* Check for outstanding write errors */
244 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
246 if (test_and_clear_bit(AS_EIO, &mapping->flags))
253 * Write and wait upon all the pages in the passed range. This is a "data
254 * integrity" operation. It waits upon in-flight writeout before starting and
255 * waiting upon new writeout. If there was an IO error, return it.
257 * We need to re-take i_sem during the generic_osync_inode list walk because
258 * it is otherwise livelockable.
260 int sync_page_range(struct inode *inode, struct address_space *mapping,
261 loff_t pos, size_t count)
263 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
264 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
267 if (mapping->backing_dev_info->memory_backed || !count)
269 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
272 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
276 ret = wait_on_page_writeback_range(mapping, start, end);
279 EXPORT_SYMBOL(sync_page_range);
282 * filemap_fdatawait - walk the list of under-writeback pages of the given
283 * address space and wait for all of them.
285 * @mapping: address space structure to wait for
287 int filemap_fdatawait(struct address_space *mapping)
289 loff_t i_size = i_size_read(mapping->host);
294 return wait_on_page_writeback_range(mapping, 0,
295 (i_size - 1) >> PAGE_CACHE_SHIFT);
297 EXPORT_SYMBOL(filemap_fdatawait);
299 int filemap_write_and_wait(struct address_space *mapping)
303 if (mapping->nrpages) {
304 retval = filemap_fdatawrite(mapping);
306 retval = filemap_fdatawait(mapping);
312 * This function is used to add newly allocated pagecache pages:
313 * the page is new, so we can just run SetPageLocked() against it.
314 * The other page state flags were set by rmqueue().
316 * This function does not add the page to the LRU. The caller must do that.
318 int add_to_page_cache(struct page *page, struct address_space *mapping,
319 pgoff_t offset, int gfp_mask)
321 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
324 spin_lock_irq(&mapping->tree_lock);
325 error = radix_tree_insert(&mapping->page_tree, offset, page);
327 page_cache_get(page);
329 page->mapping = mapping;
330 page->index = offset;
334 spin_unlock_irq(&mapping->tree_lock);
335 radix_tree_preload_end();
340 EXPORT_SYMBOL(add_to_page_cache);
342 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
343 pgoff_t offset, int gfp_mask)
345 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
352 * In order to wait for pages to become available there must be
353 * waitqueues associated with pages. By using a hash table of
354 * waitqueues where the bucket discipline is to maintain all
355 * waiters on the same queue and wake all when any of the pages
356 * become available, and for the woken contexts to check to be
357 * sure the appropriate page became available, this saves space
358 * at a cost of "thundering herd" phenomena during rare hash
361 struct page_wait_queue {
367 static int page_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
369 struct page *page = key;
370 struct page_wait_queue *wq;
372 wq = container_of(wait, struct page_wait_queue, wait);
373 if (wq->page != page || test_bit(wq->bit, &page->flags))
376 return autoremove_wake_function(wait, mode, sync, NULL);
379 #define __DEFINE_PAGE_WAIT(name, p, b, f) \
380 struct page_wait_queue name = { \
385 .func = page_wake_function, \
387 .task_list = LIST_HEAD_INIT(name.wait.task_list),\
391 #define DEFINE_PAGE_WAIT(name, p, b) __DEFINE_PAGE_WAIT(name, p, b, 0)
392 #define DEFINE_PAGE_WAIT_EXCLUSIVE(name, p, b) \
393 __DEFINE_PAGE_WAIT(name, p, b, WQ_FLAG_EXCLUSIVE)
395 static wait_queue_head_t *page_waitqueue(struct page *page)
397 const struct zone *zone = page_zone(page);
399 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
402 static void wake_up_page(struct page *page)
404 const unsigned int mode = TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE;
405 wait_queue_head_t *waitqueue = page_waitqueue(page);
407 if (waitqueue_active(waitqueue))
408 __wake_up(waitqueue, mode, 1, page);
411 void fastcall wait_on_page_bit(struct page *page, int bit_nr)
413 wait_queue_head_t *waitqueue = page_waitqueue(page);
414 DEFINE_PAGE_WAIT(wait, page, bit_nr);
417 prepare_to_wait(waitqueue, &wait.wait, TASK_UNINTERRUPTIBLE);
418 if (test_bit(bit_nr, &page->flags)) {
422 } while (test_bit(bit_nr, &page->flags));
423 finish_wait(waitqueue, &wait.wait);
426 EXPORT_SYMBOL(wait_on_page_bit);
429 * unlock_page() - unlock a locked page
433 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
434 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
435 * mechananism between PageLocked pages and PageWriteback pages is shared.
436 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
438 * The first mb is necessary to safely close the critical section opened by the
439 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
440 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
441 * parallel wait_on_page_locked()).
443 void fastcall unlock_page(struct page *page)
445 smp_mb__before_clear_bit();
446 if (!TestClearPageLocked(page))
448 smp_mb__after_clear_bit();
452 EXPORT_SYMBOL(unlock_page);
453 EXPORT_SYMBOL(lock_page);
456 * End writeback against a page.
458 void end_page_writeback(struct page *page)
460 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
461 if (!test_clear_page_writeback(page))
463 smp_mb__after_clear_bit();
468 EXPORT_SYMBOL(end_page_writeback);
471 * Get a lock on the page, assuming we need to sleep to get it.
473 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
474 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
475 * chances are that on the second loop, the block layer's plug list is empty,
476 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
478 void fastcall __lock_page(struct page *page)
480 wait_queue_head_t *wqh = page_waitqueue(page);
481 DEFINE_PAGE_WAIT_EXCLUSIVE(wait, page, PG_locked);
483 while (TestSetPageLocked(page)) {
484 prepare_to_wait_exclusive(wqh, &wait.wait, TASK_UNINTERRUPTIBLE);
485 if (PageLocked(page)) {
490 finish_wait(wqh, &wait.wait);
493 EXPORT_SYMBOL(__lock_page);
496 * a rather lightweight function, finding and getting a reference to a
497 * hashed page atomically.
499 struct page * find_get_page(struct address_space *mapping, unsigned long offset)
503 spin_lock_irq(&mapping->tree_lock);
504 page = radix_tree_lookup(&mapping->page_tree, offset);
506 page_cache_get(page);
507 spin_unlock_irq(&mapping->tree_lock);
511 EXPORT_SYMBOL(find_get_page);
514 * Same as above, but trylock it instead of incrementing the count.
516 struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
520 spin_lock_irq(&mapping->tree_lock);
521 page = radix_tree_lookup(&mapping->page_tree, offset);
522 if (page && TestSetPageLocked(page))
524 spin_unlock_irq(&mapping->tree_lock);
528 EXPORT_SYMBOL(find_trylock_page);
531 * find_lock_page - locate, pin and lock a pagecache page
533 * @mapping - the address_space to search
534 * @offset - the page index
536 * Locates the desired pagecache page, locks it, increments its reference
537 * count and returns its address.
539 * Returns zero if the page was not present. find_lock_page() may sleep.
541 struct page *find_lock_page(struct address_space *mapping,
542 unsigned long offset)
546 spin_lock_irq(&mapping->tree_lock);
548 page = radix_tree_lookup(&mapping->page_tree, offset);
550 page_cache_get(page);
551 if (TestSetPageLocked(page)) {
552 spin_unlock_irq(&mapping->tree_lock);
554 spin_lock_irq(&mapping->tree_lock);
556 /* Has the page been truncated while we slept? */
557 if (page->mapping != mapping || page->index != offset) {
559 page_cache_release(page);
564 spin_unlock_irq(&mapping->tree_lock);
568 EXPORT_SYMBOL(find_lock_page);
571 * find_or_create_page - locate or add a pagecache page
573 * @mapping - the page's address_space
574 * @index - the page's index into the mapping
575 * @gfp_mask - page allocation mode
577 * Locates a page in the pagecache. If the page is not present, a new page
578 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
579 * LRU list. The returned page is locked and has its reference count
582 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
585 * find_or_create_page() returns the desired page's address, or zero on
588 struct page *find_or_create_page(struct address_space *mapping,
589 unsigned long index, unsigned int gfp_mask)
591 struct page *page, *cached_page = NULL;
594 page = find_lock_page(mapping, index);
597 cached_page = alloc_page(gfp_mask);
601 err = add_to_page_cache_lru(cached_page, mapping,
606 } else if (err == -EEXIST)
610 page_cache_release(cached_page);
614 EXPORT_SYMBOL(find_or_create_page);
617 * find_get_pages - gang pagecache lookup
618 * @mapping: The address_space to search
619 * @start: The starting page index
620 * @nr_pages: The maximum number of pages
621 * @pages: Where the resulting pages are placed
623 * find_get_pages() will search for and return a group of up to
624 * @nr_pages pages in the mapping. The pages are placed at @pages.
625 * find_get_pages() takes a reference against the returned pages.
627 * The search returns a group of mapping-contiguous pages with ascending
628 * indexes. There may be holes in the indices due to not-present pages.
630 * find_get_pages() returns the number of pages which were found.
632 unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
633 unsigned int nr_pages, struct page **pages)
638 spin_lock_irq(&mapping->tree_lock);
639 ret = radix_tree_gang_lookup(&mapping->page_tree,
640 (void **)pages, start, nr_pages);
641 for (i = 0; i < ret; i++)
642 page_cache_get(pages[i]);
643 spin_unlock_irq(&mapping->tree_lock);
648 * Like find_get_pages, except we only return pages which are tagged with
649 * `tag'. We update *index to index the next page for the traversal.
651 unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
652 int tag, unsigned int nr_pages, struct page **pages)
657 spin_lock_irq(&mapping->tree_lock);
658 ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
659 (void **)pages, *index, nr_pages, tag);
660 for (i = 0; i < ret; i++)
661 page_cache_get(pages[i]);
663 *index = pages[ret - 1]->index + 1;
664 spin_unlock_irq(&mapping->tree_lock);
669 * Same as grab_cache_page, but do not wait if the page is unavailable.
670 * This is intended for speculative data generators, where the data can
671 * be regenerated if the page couldn't be grabbed. This routine should
672 * be safe to call while holding the lock for another page.
674 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
675 * and deadlock against the caller's locked page.
678 grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
680 struct page *page = find_get_page(mapping, index);
684 if (!TestSetPageLocked(page))
686 page_cache_release(page);
689 gfp_mask = mapping_gfp_mask(mapping) & ~__GFP_FS;
690 page = alloc_pages(gfp_mask, 0);
691 if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
692 page_cache_release(page);
698 EXPORT_SYMBOL(grab_cache_page_nowait);
701 * This is a generic file read routine, and uses the
702 * mapping->a_ops->readpage() function for the actual low-level
705 * This is really ugly. But the goto's actually try to clarify some
706 * of the logic when it comes to error handling etc.
708 * Note the struct file* is only passed for the use of readpage. It may be
711 void do_generic_mapping_read(struct address_space *mapping,
712 struct file_ra_state *_ra,
715 read_descriptor_t *desc,
719 struct inode *inode = mapping->host;
720 unsigned long index, end_index, offset;
722 struct page *cached_page;
724 struct file_ra_state ra = *_ra;
727 index = *ppos >> PAGE_CACHE_SHIFT;
728 offset = *ppos & ~PAGE_CACHE_MASK;
730 isize = i_size_read(inode);
734 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
737 unsigned long nr, ret;
739 /* nr is the maximum number of bytes to copy from this page */
740 nr = PAGE_CACHE_SIZE;
741 if (index >= end_index) {
742 if (index > end_index)
744 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
753 page_cache_readahead(mapping, &ra, filp, index);
756 page = find_get_page(mapping, index);
757 if (unlikely(page == NULL)) {
759 desc->error = -EWOULDBLOCKIO;
762 handle_ra_miss(mapping, &ra, index);
765 if (!PageUptodate(page)) {
767 page_cache_release(page);
768 desc->error = -EWOULDBLOCKIO;
771 goto page_not_up_to_date;
775 /* If users can be writing to this page using arbitrary
776 * virtual addresses, take care about potential aliasing
777 * before reading the page on the kernel side.
779 if (mapping_writably_mapped(mapping))
780 flush_dcache_page(page);
783 * Mark the page accessed if we read the beginning.
786 mark_page_accessed(page);
789 * Ok, we have the page, and it's up-to-date, so
790 * now we can copy it to user space...
792 * The actor routine returns how many bytes were actually used..
793 * NOTE! This may not be the same as how much of a user buffer
794 * we filled up (we may be padding etc), so we can only update
795 * "pos" here (the actor routine has to update the user buffer
796 * pointers and the remaining count).
798 ret = actor(desc, page, offset, nr);
800 index += offset >> PAGE_CACHE_SHIFT;
801 offset &= ~PAGE_CACHE_MASK;
803 page_cache_release(page);
804 if (ret == nr && desc->count)
809 /* Get exclusive access to the page ... */
812 /* Did it get unhashed before we got the lock? */
813 if (!page->mapping) {
815 page_cache_release(page);
819 /* Did somebody else fill it already? */
820 if (PageUptodate(page)) {
826 /* Start the actual read. The read will unlock the page. */
827 error = mapping->a_ops->readpage(filp, page);
832 if (!PageUptodate(page)) {
833 wait_on_page_locked(page);
834 if (!PageUptodate(page)) {
841 * i_size must be checked after we have done ->readpage.
843 * Checking i_size after the readpage allows us to calculate
844 * the correct value for "nr", which means the zero-filled
845 * part of the page is not copied back to userspace (unless
846 * another truncate extends the file - this is desired though).
848 isize = i_size_read(inode);
849 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
850 if (unlikely(!isize || index > end_index)) {
851 page_cache_release(page);
855 /* nr is the maximum number of bytes to copy from this page */
856 nr = PAGE_CACHE_SIZE;
857 if (index == end_index) {
858 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
860 page_cache_release(page);
868 /* UHHUH! A synchronous read error occurred. Report it */
870 page_cache_release(page);
875 * Ok, it wasn't cached, so we need to create a new
879 cached_page = page_cache_alloc_cold(mapping);
881 desc->error = -ENOMEM;
885 error = add_to_page_cache_lru(cached_page, mapping,
888 if (error == -EEXIST)
901 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
903 page_cache_release(cached_page);
908 EXPORT_SYMBOL(do_generic_mapping_read);
910 int file_read_actor(read_descriptor_t *desc, struct page *page,
911 unsigned long offset, unsigned long size)
914 unsigned long left, count = desc->count;
920 * Faults on the destination of a read are common, so do it before
923 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
924 kaddr = kmap_atomic(page, KM_USER0);
925 left = __copy_to_user_inatomic(desc->arg.buf,
926 kaddr + offset, size);
927 kunmap_atomic(kaddr, KM_USER0);
932 /* Do it the slow way */
934 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
939 desc->error = -EFAULT;
942 desc->count = count - size;
943 desc->written += size;
944 desc->arg.buf += size;
949 * This is the "read()" routine for all filesystems
950 * that can use the page cache directly.
953 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
954 unsigned long nr_segs, loff_t *ppos)
956 struct file *filp = iocb->ki_filp;
962 for (seg = 0; seg < nr_segs; seg++) {
963 const struct iovec *iv = &iov[seg];
966 * If any segment has a negative length, or the cumulative
967 * length ever wraps negative then return -EINVAL.
969 count += iv->iov_len;
970 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
972 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
977 count -= iv->iov_len; /* This segment is no good */
981 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
982 if (filp->f_flags & O_DIRECT) {
983 loff_t pos = *ppos, size;
984 struct address_space *mapping;
987 mapping = filp->f_mapping;
988 inode = mapping->host;
991 goto out; /* skip atime */
992 size = i_size_read(inode);
994 retval = generic_file_direct_IO(READ, iocb,
996 if (retval >= 0 && !is_sync_kiocb(iocb))
997 retval = -EIOCBQUEUED;
999 *ppos = pos + retval;
1001 file_accessed(filp);
1007 for (seg = 0; seg < nr_segs; seg++) {
1008 read_descriptor_t desc;
1011 desc.arg.buf = iov[seg].iov_base;
1012 desc.count = iov[seg].iov_len;
1013 if (desc.count == 0)
1016 do_generic_file_read(filp,ppos,&desc,file_read_actor,0);
1017 retval += desc.written;
1019 retval = desc.error;
1028 EXPORT_SYMBOL(__generic_file_aio_read);
1031 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1033 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1035 BUG_ON(iocb->ki_pos != pos);
1036 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1039 EXPORT_SYMBOL(generic_file_aio_read);
1042 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1044 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1048 init_sync_kiocb(&kiocb, filp);
1049 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
1050 if (-EIOCBQUEUED == ret)
1051 ret = wait_on_sync_kiocb(&kiocb);
1055 EXPORT_SYMBOL(generic_file_read);
1057 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1060 unsigned long count = desc->count;
1061 struct file *file = desc->arg.data;
1066 written = file->f_op->sendpage(file, page, offset,
1067 size, &file->f_pos, size<count);
1069 desc->error = written;
1072 desc->count = count - written;
1073 desc->written += written;
1077 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1078 size_t count, read_actor_t actor, void *target)
1080 read_descriptor_t desc;
1087 desc.arg.data = target;
1090 do_generic_file_read(in_file, ppos, &desc, actor, 0);
1092 return desc.written;
1096 EXPORT_SYMBOL(generic_file_sendfile);
1099 do_readahead(struct address_space *mapping, struct file *filp,
1100 unsigned long index, unsigned long nr)
1102 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1105 force_page_cache_readahead(mapping, filp, index,
1106 max_sane_readahead(nr));
1110 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1118 if (file->f_mode & FMODE_READ) {
1119 struct address_space *mapping = file->f_mapping;
1120 unsigned long start = offset >> PAGE_CACHE_SHIFT;
1121 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1122 unsigned long len = end - start + 1;
1123 ret = do_readahead(mapping, file, start, len);
1132 * This adds the requested page to the page cache if it isn't already there,
1133 * and schedules an I/O to read in its contents from disk.
1135 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
1136 static int fastcall page_cache_read(struct file * file, unsigned long offset)
1138 struct address_space *mapping = file->f_mapping;
1142 page = page_cache_alloc_cold(mapping);
1146 error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1148 error = mapping->a_ops->readpage(file, page);
1149 page_cache_release(page);
1154 * We arrive here in the unlikely event that someone
1155 * raced with us and added our page to the cache first
1156 * or we are out of memory for radix-tree nodes.
1158 page_cache_release(page);
1159 return error == -EEXIST ? 0 : error;
1162 #define MMAP_LOTSAMISS (100)
1165 * filemap_nopage() is invoked via the vma operations vector for a
1166 * mapped memory region to read in file data during a page fault.
1168 * The goto's are kind of ugly, but this streamlines the normal case of having
1169 * it in the page cache, and handles the special cases reasonably without
1170 * having a lot of duplicated code.
1172 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int *type)
1175 struct file *file = area->vm_file;
1176 struct address_space *mapping = file->f_mapping;
1177 struct file_ra_state *ra = &file->f_ra;
1178 struct inode *inode = mapping->host;
1180 unsigned long size, pgoff, endoff;
1181 int did_readaround = 0, majmin = VM_FAULT_MINOR;
1183 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1184 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1187 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1189 goto outside_data_content;
1191 /* If we don't want any read-ahead, don't bother */
1192 if (VM_RandomReadHint(area))
1193 goto no_cached_page;
1196 * The "size" of the file, as far as mmap is concerned, isn't bigger
1203 * The readahead code wants to be told about each and every page
1204 * so it can build and shrink its windows appropriately
1206 * For sequential accesses, we use the generic readahead logic.
1208 if (VM_SequentialReadHint(area))
1209 page_cache_readahead(mapping, ra, file, pgoff);
1212 * Do we have something in the page cache already?
1215 page = find_get_page(mapping, pgoff);
1217 unsigned long ra_pages;
1219 if (VM_SequentialReadHint(area)) {
1220 handle_ra_miss(mapping, ra, pgoff);
1221 goto no_cached_page;
1226 * Do we miss much more than hit in this file? If so,
1227 * stop bothering with read-ahead. It will only hurt.
1229 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1230 goto no_cached_page;
1233 * To keep the pgmajfault counter straight, we need to
1234 * check did_readaround, as this is an inner loop.
1236 if (!did_readaround) {
1237 majmin = VM_FAULT_MAJOR;
1238 inc_page_state(pgmajfault);
1241 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1245 if (pgoff > ra_pages / 2)
1246 start = pgoff - ra_pages / 2;
1247 do_page_cache_readahead(mapping, file, start, ra_pages);
1249 page = find_get_page(mapping, pgoff);
1251 goto no_cached_page;
1254 if (!did_readaround)
1258 * Ok, found a page in the page cache, now we need to check
1259 * that it's up-to-date.
1261 if (!PageUptodate(page))
1262 goto page_not_uptodate;
1266 * Found the page and have a reference on it.
1268 mark_page_accessed(page);
1273 outside_data_content:
1275 * An external ptracer can access pages that normally aren't
1278 if (area->vm_mm == current->mm)
1280 /* Fall through to the non-read-ahead case */
1283 * We're only likely to ever get here if MADV_RANDOM is in
1286 error = page_cache_read(file, pgoff);
1290 * The page we want has now been added to the page cache.
1291 * In the unlikely event that someone removed it in the
1292 * meantime, we'll just come back here and read it again.
1298 * An error return from page_cache_read can result if the
1299 * system is low on memory, or a problem occurs while trying
1302 if (error == -ENOMEM)
1307 if (!did_readaround) {
1308 majmin = VM_FAULT_MAJOR;
1309 inc_page_state(pgmajfault);
1313 /* Did it get unhashed while we waited for it? */
1314 if (!page->mapping) {
1316 page_cache_release(page);
1320 /* Did somebody else get it up-to-date? */
1321 if (PageUptodate(page)) {
1326 if (!mapping->a_ops->readpage(file, page)) {
1327 wait_on_page_locked(page);
1328 if (PageUptodate(page))
1333 * Umm, take care of errors if the page isn't up-to-date.
1334 * Try to re-read it _once_. We do this synchronously,
1335 * because there really aren't any performance issues here
1336 * and we need to check for errors.
1340 /* Somebody truncated the page on us? */
1341 if (!page->mapping) {
1343 page_cache_release(page);
1347 /* Somebody else successfully read it in? */
1348 if (PageUptodate(page)) {
1352 ClearPageError(page);
1353 if (!mapping->a_ops->readpage(file, page)) {
1354 wait_on_page_locked(page);
1355 if (PageUptodate(page))
1360 * Things didn't work out. Return zero to tell the
1361 * mm layer so, possibly freeing the page cache page first.
1363 page_cache_release(page);
1367 EXPORT_SYMBOL(filemap_nopage);
1369 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1372 struct address_space *mapping = file->f_mapping;
1377 * Do we have something in the page cache already?
1380 page = find_get_page(mapping, pgoff);
1384 goto no_cached_page;
1388 * Ok, found a page in the page cache, now we need to check
1389 * that it's up-to-date.
1391 if (!PageUptodate(page))
1392 goto page_not_uptodate;
1396 * Found the page and have a reference on it.
1398 mark_page_accessed(page);
1402 error = page_cache_read(file, pgoff);
1405 * The page we want has now been added to the page cache.
1406 * In the unlikely event that someone removed it in the
1407 * meantime, we'll just come back here and read it again.
1413 * An error return from page_cache_read can result if the
1414 * system is low on memory, or a problem occurs while trying
1422 /* Did it get unhashed while we waited for it? */
1423 if (!page->mapping) {
1428 /* Did somebody else get it up-to-date? */
1429 if (PageUptodate(page)) {
1434 if (!mapping->a_ops->readpage(file, page)) {
1435 wait_on_page_locked(page);
1436 if (PageUptodate(page))
1441 * Umm, take care of errors if the page isn't up-to-date.
1442 * Try to re-read it _once_. We do this synchronously,
1443 * because there really aren't any performance issues here
1444 * and we need to check for errors.
1448 /* Somebody truncated the page on us? */
1449 if (!page->mapping) {
1453 /* Somebody else successfully read it in? */
1454 if (PageUptodate(page)) {
1459 ClearPageError(page);
1460 if (!mapping->a_ops->readpage(file, page)) {
1461 wait_on_page_locked(page);
1462 if (PageUptodate(page))
1467 * Things didn't work out. Return zero to tell the
1468 * mm layer so, possibly freeing the page cache page first.
1471 page_cache_release(page);
1476 static int filemap_populate(struct vm_area_struct *vma,
1480 unsigned long pgoff,
1483 struct file *file = vma->vm_file;
1484 struct address_space *mapping = file->f_mapping;
1485 struct inode *inode = mapping->host;
1487 struct mm_struct *mm = vma->vm_mm;
1492 force_page_cache_readahead(mapping, vma->vm_file,
1493 pgoff, len >> PAGE_CACHE_SHIFT);
1496 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1497 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1500 page = filemap_getpage(file, pgoff, nonblock);
1501 if (!page && !nonblock)
1504 err = install_page(mm, vma, addr, page, prot);
1506 page_cache_release(page);
1510 err = install_file_pte(mm, vma, addr, pgoff, prot);
1524 struct vm_operations_struct generic_file_vm_ops = {
1525 .nopage = filemap_nopage,
1526 .populate = filemap_populate,
1529 /* This is used for a general mmap of a disk file */
1531 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1533 struct address_space *mapping = file->f_mapping;
1535 if (!mapping->a_ops->readpage)
1537 file_accessed(file);
1538 vma->vm_ops = &generic_file_vm_ops;
1543 * This is for filesystems which do not implement ->writepage.
1545 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1547 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1549 return generic_file_mmap(file, vma);
1552 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1556 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1560 #endif /* CONFIG_MMU */
1562 EXPORT_SYMBOL(generic_file_mmap);
1563 EXPORT_SYMBOL(generic_file_readonly_mmap);
1565 static inline struct page *__read_cache_page(struct address_space *mapping,
1566 unsigned long index,
1567 int (*filler)(void *,struct page*),
1570 struct page *page, *cached_page = NULL;
1573 page = find_get_page(mapping, index);
1576 cached_page = page_cache_alloc_cold(mapping);
1578 return ERR_PTR(-ENOMEM);
1580 err = add_to_page_cache_lru(cached_page, mapping,
1585 /* Presumably ENOMEM for radix tree node */
1586 page_cache_release(cached_page);
1587 return ERR_PTR(err);
1591 err = filler(data, page);
1593 page_cache_release(page);
1594 page = ERR_PTR(err);
1598 page_cache_release(cached_page);
1603 * Read into the page cache. If a page already exists,
1604 * and PageUptodate() is not set, try to fill the page.
1606 struct page *read_cache_page(struct address_space *mapping,
1607 unsigned long index,
1608 int (*filler)(void *,struct page*),
1615 page = __read_cache_page(mapping, index, filler, data);
1618 mark_page_accessed(page);
1619 if (PageUptodate(page))
1623 if (!page->mapping) {
1625 page_cache_release(page);
1628 if (PageUptodate(page)) {
1632 err = filler(data, page);
1634 page_cache_release(page);
1635 page = ERR_PTR(err);
1641 EXPORT_SYMBOL(read_cache_page);
1644 * If the page was newly created, increment its refcount and add it to the
1645 * caller's lru-buffering pagevec. This function is specifically for
1646 * generic_file_write().
1648 static inline struct page *
1649 __grab_cache_page(struct address_space *mapping, unsigned long index,
1650 struct page **cached_page, struct pagevec *lru_pvec)
1655 page = find_lock_page(mapping, index);
1657 if (!*cached_page) {
1658 *cached_page = page_cache_alloc(mapping);
1662 err = add_to_page_cache(*cached_page, mapping,
1667 page = *cached_page;
1668 page_cache_get(page);
1669 if (!pagevec_add(lru_pvec, page))
1670 __pagevec_lru_add(lru_pvec);
1671 *cached_page = NULL;
1678 * The logic we want is
1680 * if suid or (sgid and xgrp)
1683 int remove_suid(struct dentry *dentry)
1685 mode_t mode = dentry->d_inode->i_mode;
1689 /* suid always must be killed */
1690 if (unlikely(mode & S_ISUID))
1691 kill = ATTR_KILL_SUID;
1694 * sgid without any exec bits is just a mandatory locking mark; leave
1695 * it alone. If some exec bits are set, it's a real sgid; kill it.
1697 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1698 kill |= ATTR_KILL_SGID;
1700 if (unlikely(kill && !capable(CAP_FSETID))) {
1701 struct iattr newattrs;
1703 newattrs.ia_valid = ATTR_FORCE | kill;
1704 result = notify_change(dentry, &newattrs);
1708 EXPORT_SYMBOL(remove_suid);
1711 * Copy as much as we can into the page and return the number of bytes which
1712 * were sucessfully copied. If a fault is encountered then clear the page
1713 * out to (offset+bytes) and return the number of bytes which were copied.
1715 static inline size_t
1716 filemap_copy_from_user(struct page *page, unsigned long offset,
1717 const char __user *buf, unsigned bytes)
1722 kaddr = kmap_atomic(page, KM_USER0);
1723 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
1724 kunmap_atomic(kaddr, KM_USER0);
1727 /* Do it the slow way */
1729 left = __copy_from_user(kaddr + offset, buf, bytes);
1732 return bytes - left;
1736 __filemap_copy_from_user_iovec(char *vaddr,
1737 const struct iovec *iov, size_t base, size_t bytes)
1739 size_t copied = 0, left = 0;
1742 char __user *buf = iov->iov_base + base;
1743 int copy = min(bytes, iov->iov_len - base);
1746 left = __copy_from_user_inatomic(vaddr, buf, copy);
1752 if (unlikely(left)) {
1753 /* zero the rest of the target like __copy_from_user */
1755 memset(vaddr, 0, bytes);
1759 return copied - left;
1763 * This has the same sideeffects and return value as filemap_copy_from_user().
1764 * The difference is that on a fault we need to memset the remainder of the
1765 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1766 * single-segment behaviour.
1768 static inline size_t
1769 filemap_copy_from_user_iovec(struct page *page, unsigned long offset,
1770 const struct iovec *iov, size_t base, size_t bytes)
1775 kaddr = kmap_atomic(page, KM_USER0);
1776 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1778 kunmap_atomic(kaddr, KM_USER0);
1779 if (copied != bytes) {
1781 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1789 filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1791 const struct iovec *iov = *iovp;
1792 size_t base = *basep;
1795 int copy = min(bytes, iov->iov_len - base);
1799 if (iov->iov_len == base) {
1809 * Performs necessary checks before doing a write
1811 * Can adjust writing position aor amount of bytes to write.
1812 * Returns appropriate error code that caller should return or
1813 * zero in case that write should be allowed.
1815 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1817 struct inode *inode = file->f_mapping->host;
1818 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1820 if (unlikely(*pos < 0))
1823 if (unlikely(file->f_error)) {
1824 int err = file->f_error;
1830 /* FIXME: this is for backwards compatibility with 2.4 */
1831 if (file->f_flags & O_APPEND)
1832 *pos = i_size_read(inode);
1834 if (limit != RLIM_INFINITY) {
1835 if (*pos >= limit) {
1836 send_sig(SIGXFSZ, current, 0);
1839 if (*count > limit - (typeof(limit))*pos) {
1840 *count = limit - (typeof(limit))*pos;
1848 if (unlikely(*pos + *count > MAX_NON_LFS &&
1849 !(file->f_flags & O_LARGEFILE))) {
1850 if (*pos >= MAX_NON_LFS) {
1851 send_sig(SIGXFSZ, current, 0);
1854 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1855 *count = MAX_NON_LFS - (unsigned long)*pos;
1860 * Are we about to exceed the fs block limit ?
1862 * If we have written data it becomes a short write. If we have
1863 * exceeded without writing data we send a signal and return EFBIG.
1864 * Linus frestrict idea will clean these up nicely..
1866 if (likely(!isblk)) {
1867 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1868 if (*count || *pos > inode->i_sb->s_maxbytes) {
1869 send_sig(SIGXFSZ, current, 0);
1872 /* zero-length writes at ->s_maxbytes are OK */
1875 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1876 *count = inode->i_sb->s_maxbytes - *pos;
1879 if (bdev_read_only(I_BDEV(inode)))
1881 isize = i_size_read(inode);
1882 if (*pos >= isize) {
1883 if (*count || *pos > isize)
1887 if (*pos + *count > isize)
1888 *count = isize - *pos;
1893 EXPORT_SYMBOL(generic_write_checks);
1896 generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1897 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1898 size_t count, size_t ocount)
1900 struct file *file = iocb->ki_filp;
1901 struct address_space *mapping = file->f_mapping;
1902 struct inode *inode = mapping->host;
1905 if (count != ocount)
1906 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1908 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1910 loff_t end = pos + written;
1911 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1912 i_size_write(inode, end);
1913 mark_inode_dirty(inode);
1919 * Sync the fs metadata but not the minor inode changes and
1920 * of course not the data as we did direct DMA for the IO.
1921 * i_sem is held, which protects generic_osync_inode() from
1924 if (written >= 0 && file->f_flags & O_SYNC)
1925 generic_osync_inode(inode, mapping, OSYNC_METADATA);
1926 if (written == count && !is_sync_kiocb(iocb))
1927 written = -EIOCBQUEUED;
1931 EXPORT_SYMBOL(generic_file_direct_write);
1934 generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
1935 unsigned long nr_segs, loff_t pos, loff_t *ppos,
1936 size_t count, ssize_t written)
1938 struct file *file = iocb->ki_filp;
1939 struct address_space * mapping = file->f_mapping;
1940 struct address_space_operations *a_ops = mapping->a_ops;
1941 struct inode *inode = mapping->host;
1944 struct page *cached_page = NULL;
1946 struct pagevec lru_pvec;
1947 const struct iovec *cur_iov = iov; /* current iovec */
1948 size_t iov_base = 0; /* offset in the current iovec */
1951 pagevec_init(&lru_pvec, 0);
1953 buf = iov->iov_base + written; /* handle partial DIO write */
1955 unsigned long index;
1956 unsigned long offset;
1959 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1960 index = pos >> PAGE_CACHE_SHIFT;
1961 bytes = PAGE_CACHE_SIZE - offset;
1966 * Bring in the user page that we will copy from _first_.
1967 * Otherwise there's a nasty deadlock on copying from the
1968 * same page as we're writing to, without it being marked
1971 fault_in_pages_readable(buf, bytes);
1973 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1979 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1980 if (unlikely(status)) {
1981 loff_t isize = i_size_read(inode);
1983 * prepare_write() may have instantiated a few blocks
1984 * outside i_size. Trim these off again.
1987 page_cache_release(page);
1988 if (pos + bytes > isize)
1989 vmtruncate(inode, isize);
1992 if (likely(nr_segs == 1))
1993 copied = filemap_copy_from_user(page, offset,
1996 copied = filemap_copy_from_user_iovec(page, offset,
1997 cur_iov, iov_base, bytes);
1998 flush_dcache_page(page);
1999 status = a_ops->commit_write(file, page, offset, offset+bytes);
2000 if (likely(copied > 0)) {
2009 if (unlikely(nr_segs > 1))
2010 filemap_set_next_iovec(&cur_iov,
2014 if (unlikely(copied != bytes))
2018 mark_page_accessed(page);
2019 page_cache_release(page);
2022 balance_dirty_pages_ratelimited(mapping);
2028 page_cache_release(cached_page);
2031 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2033 if (likely(status >= 0)) {
2034 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2035 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2036 status = generic_osync_inode(inode, mapping,
2037 OSYNC_METADATA|OSYNC_DATA);
2042 * If we get here for O_DIRECT writes then we must have fallen through
2043 * to buffered writes (block instantiation inside i_size). So we sync
2044 * the file data here, to try to honour O_DIRECT expectations.
2046 if (unlikely(file->f_flags & O_DIRECT) && written)
2047 status = filemap_write_and_wait(mapping);
2049 pagevec_lru_add(&lru_pvec);
2050 return written ? written : status;
2053 EXPORT_SYMBOL(generic_file_buffered_write);
2056 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2057 unsigned long nr_segs, loff_t *ppos)
2059 struct file *file = iocb->ki_filp;
2060 struct address_space * mapping = file->f_mapping;
2061 size_t ocount; /* original count */
2062 size_t count; /* after file limit checks */
2063 struct inode *inode = mapping->host;
2070 for (seg = 0; seg < nr_segs; seg++) {
2071 const struct iovec *iv = &iov[seg];
2074 * If any segment has a negative length, or the cumulative
2075 * length ever wraps negative then return -EINVAL.
2077 ocount += iv->iov_len;
2078 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2080 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2085 ocount -= iv->iov_len; /* This segment is no good */
2092 /* We can write back this queue in page reclaim */
2093 current->backing_dev_info = mapping->backing_dev_info;
2096 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2103 err = remove_suid(file->f_dentry);
2107 inode_update_time(inode, file->f_vfsmnt, 1);
2109 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2110 if (unlikely(file->f_flags & O_DIRECT)) {
2111 written = generic_file_direct_write(iocb, iov,
2112 &nr_segs, pos, ppos, count, ocount);
2113 if (written < 0 || written == count)
2116 * direct-io write to a hole: fall through to buffered I/O
2117 * for completing the rest of the request.
2123 written = generic_file_buffered_write(iocb, iov, nr_segs,
2124 pos, ppos, count, written);
2126 current->backing_dev_info = NULL;
2127 return written ? written : err;
2130 EXPORT_SYMBOL(generic_file_aio_write_nolock);
2133 generic_file_write_nolock(struct file *file, const struct iovec *iov,
2134 unsigned long nr_segs, loff_t *ppos)
2139 init_sync_kiocb(&kiocb, file);
2140 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2141 if (-EIOCBQUEUED == ret)
2142 ret = wait_on_sync_kiocb(&kiocb);
2146 EXPORT_SYMBOL(generic_file_write_nolock);
2148 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2149 size_t count, loff_t pos)
2151 struct file *file = iocb->ki_filp;
2152 struct address_space *mapping = file->f_mapping;
2153 struct inode *inode = mapping->host;
2155 struct iovec local_iov = { .iov_base = (void __user *)buf,
2158 BUG_ON(iocb->ki_pos != pos);
2160 down(&inode->i_sem);
2161 ret = generic_file_aio_write_nolock(iocb, &local_iov, 1,
2165 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2168 err = sync_page_range(inode, mapping, pos, ret);
2174 EXPORT_SYMBOL(generic_file_aio_write);
2176 ssize_t generic_file_write(struct file *file, const char __user *buf,
2177 size_t count, loff_t *ppos)
2179 struct address_space *mapping = file->f_mapping;
2180 struct inode *inode = mapping->host;
2182 struct iovec local_iov = { .iov_base = (void __user *)buf,
2185 down(&inode->i_sem);
2186 ret = generic_file_write_nolock(file, &local_iov, 1, ppos);
2189 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2192 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2198 EXPORT_SYMBOL(generic_file_write);
2200 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2201 unsigned long nr_segs, loff_t *ppos)
2206 init_sync_kiocb(&kiocb, filp);
2207 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
2208 if (-EIOCBQUEUED == ret)
2209 ret = wait_on_sync_kiocb(&kiocb);
2213 EXPORT_SYMBOL(generic_file_readv);
2215 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2216 unsigned long nr_segs, loff_t *ppos)
2218 struct address_space *mapping = file->f_mapping;
2219 struct inode *inode = mapping->host;
2222 down(&inode->i_sem);
2223 ret = generic_file_write_nolock(file, iov, nr_segs, ppos);
2226 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2229 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);
2259 EXPORT_SYMBOL_GPL(generic_file_direct_IO);