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,
690 struct inode *inode = mapping->host;
692 unsigned long end_index;
693 unsigned long offset;
694 unsigned long req_size;
695 unsigned long next_index;
696 unsigned long prev_index;
698 struct page *cached_page;
700 struct file_ra_state ra = *_ra;
703 index = *ppos >> PAGE_CACHE_SHIFT;
705 prev_index = ra.prev_page;
706 req_size = (desc->count + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
707 offset = *ppos & ~PAGE_CACHE_MASK;
709 isize = i_size_read(inode);
713 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
716 unsigned long ret_size, nr, ret;
718 /* nr is the maximum number of bytes to copy from this page */
719 nr = PAGE_CACHE_SIZE;
720 if (index >= end_index) {
721 if (index > end_index)
723 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
731 if (index == next_index && req_size) {
732 ret_size = page_cache_readahead(mapping, &ra,
733 filp, index, req_size);
734 next_index += ret_size;
735 req_size -= ret_size;
739 page = find_get_page(mapping, index);
740 if (unlikely(page == NULL)) {
741 handle_ra_miss(mapping, &ra, index);
744 if (!PageUptodate(page))
745 goto page_not_up_to_date;
748 /* If users can be writing to this page using arbitrary
749 * virtual addresses, take care about potential aliasing
750 * before reading the page on the kernel side.
752 if (mapping_writably_mapped(mapping))
753 flush_dcache_page(page);
756 * When (part of) the same page is read multiple times
757 * in succession, only mark it as accessed the first time.
759 if (prev_index != index)
760 mark_page_accessed(page);
764 * Ok, we have the page, and it's up-to-date, so
765 * now we can copy it to user space...
767 * The actor routine returns how many bytes were actually used..
768 * NOTE! This may not be the same as how much of a user buffer
769 * we filled up (we may be padding etc), so we can only update
770 * "pos" here (the actor routine has to update the user buffer
771 * pointers and the remaining count).
773 ret = actor(desc, page, offset, nr);
775 index += offset >> PAGE_CACHE_SHIFT;
776 offset &= ~PAGE_CACHE_MASK;
778 page_cache_release(page);
779 if (ret == nr && desc->count)
784 /* Get exclusive access to the page ... */
787 /* Did it get unhashed before we got the lock? */
788 if (!page->mapping) {
790 page_cache_release(page);
794 /* Did somebody else fill it already? */
795 if (PageUptodate(page)) {
801 /* Start the actual read. The read will unlock the page. */
802 error = mapping->a_ops->readpage(filp, page);
807 if (!PageUptodate(page)) {
809 if (!PageUptodate(page)) {
810 if (page->mapping == NULL) {
812 * invalidate_inode_pages got it
815 page_cache_release(page);
826 * i_size must be checked after we have done ->readpage.
828 * Checking i_size after the readpage allows us to calculate
829 * the correct value for "nr", which means the zero-filled
830 * part of the page is not copied back to userspace (unless
831 * another truncate extends the file - this is desired though).
833 isize = i_size_read(inode);
834 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
835 if (unlikely(!isize || index > end_index)) {
836 page_cache_release(page);
840 /* nr is the maximum number of bytes to copy from this page */
841 nr = PAGE_CACHE_SIZE;
842 if (index == end_index) {
843 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
845 page_cache_release(page);
853 /* UHHUH! A synchronous read error occurred. Report it */
855 page_cache_release(page);
860 * Ok, it wasn't cached, so we need to create a new
864 cached_page = page_cache_alloc_cold(mapping);
866 desc->error = -ENOMEM;
870 error = add_to_page_cache_lru(cached_page, mapping,
873 if (error == -EEXIST)
886 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
888 page_cache_release(cached_page);
893 EXPORT_SYMBOL(do_generic_mapping_read);
895 int file_read_actor(read_descriptor_t *desc, struct page *page,
896 unsigned long offset, unsigned long size)
899 unsigned long left, count = desc->count;
905 * Faults on the destination of a read are common, so do it before
908 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
909 kaddr = kmap_atomic(page, KM_USER0);
910 left = __copy_to_user_inatomic(desc->arg.buf,
911 kaddr + offset, size);
912 kunmap_atomic(kaddr, KM_USER0);
917 /* Do it the slow way */
919 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
924 desc->error = -EFAULT;
927 desc->count = count - size;
928 desc->written += size;
929 desc->arg.buf += size;
934 * This is the "read()" routine for all filesystems
935 * that can use the page cache directly.
938 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
939 unsigned long nr_segs, loff_t *ppos)
941 struct file *filp = iocb->ki_filp;
947 for (seg = 0; seg < nr_segs; seg++) {
948 const struct iovec *iv = &iov[seg];
951 * If any segment has a negative length, or the cumulative
952 * length ever wraps negative then return -EINVAL.
954 count += iv->iov_len;
955 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
957 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
962 count -= iv->iov_len; /* This segment is no good */
966 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
967 if (filp->f_flags & O_DIRECT) {
968 loff_t pos = *ppos, size;
969 struct address_space *mapping;
972 mapping = filp->f_mapping;
973 inode = mapping->host;
976 goto out; /* skip atime */
977 size = i_size_read(inode);
979 retval = generic_file_direct_IO(READ, iocb,
981 if (retval >= 0 && !is_sync_kiocb(iocb))
982 retval = -EIOCBQUEUED;
984 *ppos = pos + retval;
992 for (seg = 0; seg < nr_segs; seg++) {
993 read_descriptor_t desc;
996 desc.arg.buf = iov[seg].iov_base;
997 desc.count = iov[seg].iov_len;
1001 do_generic_file_read(filp,ppos,&desc,file_read_actor);
1002 retval += desc.written;
1004 retval = desc.error;
1013 EXPORT_SYMBOL(__generic_file_aio_read);
1016 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1018 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1020 BUG_ON(iocb->ki_pos != pos);
1021 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1024 EXPORT_SYMBOL(generic_file_aio_read);
1027 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1029 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1033 init_sync_kiocb(&kiocb, filp);
1034 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
1035 if (-EIOCBQUEUED == ret)
1036 ret = wait_on_sync_kiocb(&kiocb);
1040 EXPORT_SYMBOL(generic_file_read);
1042 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1045 unsigned long count = desc->count;
1046 struct file *file = desc->arg.data;
1051 written = file->f_op->sendpage(file, page, offset,
1052 size, &file->f_pos, size<count);
1054 desc->error = written;
1057 desc->count = count - written;
1058 desc->written += written;
1062 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1063 size_t count, read_actor_t actor, void *target)
1065 read_descriptor_t desc;
1072 desc.arg.data = target;
1075 do_generic_file_read(in_file, ppos, &desc, actor);
1077 return desc.written;
1081 EXPORT_SYMBOL(generic_file_sendfile);
1084 do_readahead(struct address_space *mapping, struct file *filp,
1085 unsigned long index, unsigned long nr)
1087 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1090 force_page_cache_readahead(mapping, filp, index,
1091 max_sane_readahead(nr));
1095 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1103 if (file->f_mode & FMODE_READ) {
1104 struct address_space *mapping = file->f_mapping;
1105 unsigned long start = offset >> PAGE_CACHE_SHIFT;
1106 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1107 unsigned long len = end - start + 1;
1108 ret = do_readahead(mapping, file, start, len);
1117 * This adds the requested page to the page cache if it isn't already there,
1118 * and schedules an I/O to read in its contents from disk.
1120 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
1121 static int fastcall page_cache_read(struct file * file, unsigned long offset)
1123 struct address_space *mapping = file->f_mapping;
1127 page = page_cache_alloc_cold(mapping);
1131 error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1133 error = mapping->a_ops->readpage(file, page);
1134 page_cache_release(page);
1139 * We arrive here in the unlikely event that someone
1140 * raced with us and added our page to the cache first
1141 * or we are out of memory for radix-tree nodes.
1143 page_cache_release(page);
1144 return error == -EEXIST ? 0 : error;
1147 #define MMAP_LOTSAMISS (100)
1150 * filemap_nopage() is invoked via the vma operations vector for a
1151 * mapped memory region to read in file data during a page fault.
1153 * The goto's are kind of ugly, but this streamlines the normal case of having
1154 * it in the page cache, and handles the special cases reasonably without
1155 * having a lot of duplicated code.
1157 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int *type)
1160 struct file *file = area->vm_file;
1161 struct address_space *mapping = file->f_mapping;
1162 struct file_ra_state *ra = &file->f_ra;
1163 struct inode *inode = mapping->host;
1165 unsigned long size, pgoff, endoff;
1166 int did_readaround = 0, majmin = VM_FAULT_MINOR;
1168 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1169 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1172 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1174 goto outside_data_content;
1176 /* If we don't want any read-ahead, don't bother */
1177 if (VM_RandomReadHint(area))
1178 goto no_cached_page;
1181 * The "size" of the file, as far as mmap is concerned, isn't bigger
1188 * The readahead code wants to be told about each and every page
1189 * so it can build and shrink its windows appropriately
1191 * For sequential accesses, we use the generic readahead logic.
1193 if (VM_SequentialReadHint(area))
1194 page_cache_readahead(mapping, ra, file, pgoff, 1);
1197 * Do we have something in the page cache already?
1200 page = find_get_page(mapping, pgoff);
1202 unsigned long ra_pages;
1204 if (VM_SequentialReadHint(area)) {
1205 handle_ra_miss(mapping, ra, pgoff);
1206 goto no_cached_page;
1211 * Do we miss much more than hit in this file? If so,
1212 * stop bothering with read-ahead. It will only hurt.
1214 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1215 goto no_cached_page;
1218 * To keep the pgmajfault counter straight, we need to
1219 * check did_readaround, as this is an inner loop.
1221 if (!did_readaround) {
1222 majmin = VM_FAULT_MAJOR;
1223 inc_page_state(pgmajfault);
1226 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1230 if (pgoff > ra_pages / 2)
1231 start = pgoff - ra_pages / 2;
1232 do_page_cache_readahead(mapping, file, start, ra_pages);
1234 page = find_get_page(mapping, pgoff);
1236 goto no_cached_page;
1239 if (!did_readaround)
1243 * Ok, found a page in the page cache, now we need to check
1244 * that it's up-to-date.
1246 if (!PageUptodate(page))
1247 goto page_not_uptodate;
1251 * Found the page and have a reference on it.
1253 mark_page_accessed(page);
1258 outside_data_content:
1260 * An external ptracer can access pages that normally aren't
1263 if (area->vm_mm == current->mm)
1265 /* Fall through to the non-read-ahead case */
1268 * We're only likely to ever get here if MADV_RANDOM is in
1271 error = page_cache_read(file, pgoff);
1275 * The page we want has now been added to the page cache.
1276 * In the unlikely event that someone removed it in the
1277 * meantime, we'll just come back here and read it again.
1283 * An error return from page_cache_read can result if the
1284 * system is low on memory, or a problem occurs while trying
1287 if (error == -ENOMEM)
1292 if (!did_readaround) {
1293 majmin = VM_FAULT_MAJOR;
1294 inc_page_state(pgmajfault);
1298 /* Did it get unhashed while we waited for it? */
1299 if (!page->mapping) {
1301 page_cache_release(page);
1305 /* Did somebody else get it up-to-date? */
1306 if (PageUptodate(page)) {
1311 if (!mapping->a_ops->readpage(file, page)) {
1312 wait_on_page_locked(page);
1313 if (PageUptodate(page))
1318 * Umm, take care of errors if the page isn't up-to-date.
1319 * Try to re-read it _once_. We do this synchronously,
1320 * because there really aren't any performance issues here
1321 * and we need to check for errors.
1325 /* Somebody truncated the page on us? */
1326 if (!page->mapping) {
1328 page_cache_release(page);
1332 /* Somebody else successfully read it in? */
1333 if (PageUptodate(page)) {
1337 ClearPageError(page);
1338 if (!mapping->a_ops->readpage(file, page)) {
1339 wait_on_page_locked(page);
1340 if (PageUptodate(page))
1345 * Things didn't work out. Return zero to tell the
1346 * mm layer so, possibly freeing the page cache page first.
1348 page_cache_release(page);
1352 EXPORT_SYMBOL(filemap_nopage);
1354 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1357 struct address_space *mapping = file->f_mapping;
1362 * Do we have something in the page cache already?
1365 page = find_get_page(mapping, pgoff);
1369 goto no_cached_page;
1373 * Ok, found a page in the page cache, now we need to check
1374 * that it's up-to-date.
1376 if (!PageUptodate(page))
1377 goto page_not_uptodate;
1381 * Found the page and have a reference on it.
1383 mark_page_accessed(page);
1387 error = page_cache_read(file, pgoff);
1390 * The page we want has now been added to the page cache.
1391 * In the unlikely event that someone removed it in the
1392 * meantime, we'll just come back here and read it again.
1398 * An error return from page_cache_read can result if the
1399 * system is low on memory, or a problem occurs while trying
1407 /* Did it get unhashed while we waited for it? */
1408 if (!page->mapping) {
1413 /* Did somebody else get it up-to-date? */
1414 if (PageUptodate(page)) {
1419 if (!mapping->a_ops->readpage(file, page)) {
1420 wait_on_page_locked(page);
1421 if (PageUptodate(page))
1426 * Umm, take care of errors if the page isn't up-to-date.
1427 * Try to re-read it _once_. We do this synchronously,
1428 * because there really aren't any performance issues here
1429 * and we need to check for errors.
1433 /* Somebody truncated the page on us? */
1434 if (!page->mapping) {
1438 /* Somebody else successfully read it in? */
1439 if (PageUptodate(page)) {
1444 ClearPageError(page);
1445 if (!mapping->a_ops->readpage(file, page)) {
1446 wait_on_page_locked(page);
1447 if (PageUptodate(page))
1452 * Things didn't work out. Return zero to tell the
1453 * mm layer so, possibly freeing the page cache page first.
1456 page_cache_release(page);
1461 int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
1462 unsigned long len, pgprot_t prot, unsigned long pgoff,
1465 struct file *file = vma->vm_file;
1466 struct address_space *mapping = file->f_mapping;
1467 struct inode *inode = mapping->host;
1469 struct mm_struct *mm = vma->vm_mm;
1474 force_page_cache_readahead(mapping, vma->vm_file,
1475 pgoff, len >> PAGE_CACHE_SHIFT);
1478 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1479 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1482 page = filemap_getpage(file, pgoff, nonblock);
1483 if (!page && !nonblock)
1486 err = install_page(mm, vma, addr, page, prot);
1488 page_cache_release(page);
1492 err = install_file_pte(mm, vma, addr, pgoff, prot);
1506 struct vm_operations_struct generic_file_vm_ops = {
1507 .nopage = filemap_nopage,
1508 .populate = filemap_populate,
1511 /* This is used for a general mmap of a disk file */
1513 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1515 struct address_space *mapping = file->f_mapping;
1517 if (!mapping->a_ops->readpage)
1519 file_accessed(file);
1520 vma->vm_ops = &generic_file_vm_ops;
1523 EXPORT_SYMBOL(filemap_populate);
1526 * This is for filesystems which do not implement ->writepage.
1528 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1530 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1532 return generic_file_mmap(file, vma);
1535 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1539 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1543 #endif /* CONFIG_MMU */
1545 EXPORT_SYMBOL(generic_file_mmap);
1546 EXPORT_SYMBOL(generic_file_readonly_mmap);
1548 static inline struct page *__read_cache_page(struct address_space *mapping,
1549 unsigned long index,
1550 int (*filler)(void *,struct page*),
1553 struct page *page, *cached_page = NULL;
1556 page = find_get_page(mapping, index);
1559 cached_page = page_cache_alloc_cold(mapping);
1561 return ERR_PTR(-ENOMEM);
1563 err = add_to_page_cache_lru(cached_page, mapping,
1568 /* Presumably ENOMEM for radix tree node */
1569 page_cache_release(cached_page);
1570 return ERR_PTR(err);
1574 err = filler(data, page);
1576 page_cache_release(page);
1577 page = ERR_PTR(err);
1581 page_cache_release(cached_page);
1586 * Read into the page cache. If a page already exists,
1587 * and PageUptodate() is not set, try to fill the page.
1589 struct page *read_cache_page(struct address_space *mapping,
1590 unsigned long index,
1591 int (*filler)(void *,struct page*),
1598 page = __read_cache_page(mapping, index, filler, data);
1601 mark_page_accessed(page);
1602 if (PageUptodate(page))
1606 if (!page->mapping) {
1608 page_cache_release(page);
1611 if (PageUptodate(page)) {
1615 err = filler(data, page);
1617 page_cache_release(page);
1618 page = ERR_PTR(err);
1624 EXPORT_SYMBOL(read_cache_page);
1627 * If the page was newly created, increment its refcount and add it to the
1628 * caller's lru-buffering pagevec. This function is specifically for
1629 * generic_file_write().
1631 static inline struct page *
1632 __grab_cache_page(struct address_space *mapping, unsigned long index,
1633 struct page **cached_page, struct pagevec *lru_pvec)
1638 page = find_lock_page(mapping, index);
1640 if (!*cached_page) {
1641 *cached_page = page_cache_alloc(mapping);
1645 err = add_to_page_cache(*cached_page, mapping,
1650 page = *cached_page;
1651 page_cache_get(page);
1652 if (!pagevec_add(lru_pvec, page))
1653 __pagevec_lru_add(lru_pvec);
1654 *cached_page = NULL;
1661 * The logic we want is
1663 * if suid or (sgid and xgrp)
1666 int remove_suid(struct dentry *dentry)
1668 mode_t mode = dentry->d_inode->i_mode;
1672 /* suid always must be killed */
1673 if (unlikely(mode & S_ISUID))
1674 kill = ATTR_KILL_SUID;
1677 * sgid without any exec bits is just a mandatory locking mark; leave
1678 * it alone. If some exec bits are set, it's a real sgid; kill it.
1680 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1681 kill |= ATTR_KILL_SGID;
1683 if (unlikely(kill && !capable(CAP_FSETID))) {
1684 struct iattr newattrs;
1686 newattrs.ia_valid = ATTR_FORCE | kill;
1687 result = notify_change(dentry, &newattrs);
1691 EXPORT_SYMBOL(remove_suid);
1694 * Copy as much as we can into the page and return the number of bytes which
1695 * were sucessfully copied. If a fault is encountered then clear the page
1696 * out to (offset+bytes) and return the number of bytes which were copied.
1698 static inline size_t
1699 filemap_copy_from_user(struct page *page, unsigned long offset,
1700 const char __user *buf, unsigned bytes)
1705 kaddr = kmap_atomic(page, KM_USER0);
1706 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
1707 kunmap_atomic(kaddr, KM_USER0);
1710 /* Do it the slow way */
1712 left = __copy_from_user(kaddr + offset, buf, bytes);
1715 return bytes - left;
1719 __filemap_copy_from_user_iovec(char *vaddr,
1720 const struct iovec *iov, size_t base, size_t bytes)
1722 size_t copied = 0, left = 0;
1725 char __user *buf = iov->iov_base + base;
1726 int copy = min(bytes, iov->iov_len - base);
1729 left = __copy_from_user_inatomic(vaddr, buf, copy);
1735 if (unlikely(left)) {
1736 /* zero the rest of the target like __copy_from_user */
1738 memset(vaddr, 0, bytes);
1742 return copied - left;
1746 * This has the same sideeffects and return value as filemap_copy_from_user().
1747 * The difference is that on a fault we need to memset the remainder of the
1748 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1749 * single-segment behaviour.
1751 static inline size_t
1752 filemap_copy_from_user_iovec(struct page *page, unsigned long offset,
1753 const struct iovec *iov, size_t base, size_t bytes)
1758 kaddr = kmap_atomic(page, KM_USER0);
1759 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1761 kunmap_atomic(kaddr, KM_USER0);
1762 if (copied != bytes) {
1764 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1772 filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1774 const struct iovec *iov = *iovp;
1775 size_t base = *basep;
1778 int copy = min(bytes, iov->iov_len - base);
1782 if (iov->iov_len == base) {
1792 * Performs necessary checks before doing a write
1794 * Can adjust writing position aor amount of bytes to write.
1795 * Returns appropriate error code that caller should return or
1796 * zero in case that write should be allowed.
1798 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1800 struct inode *inode = file->f_mapping->host;
1801 unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1803 if (unlikely(*pos < 0))
1806 if (unlikely(file->f_error)) {
1807 int err = file->f_error;
1813 /* FIXME: this is for backwards compatibility with 2.4 */
1814 if (file->f_flags & O_APPEND)
1815 *pos = i_size_read(inode);
1817 if (limit != RLIM_INFINITY) {
1818 if (*pos >= limit) {
1819 send_sig(SIGXFSZ, current, 0);
1822 if (*count > limit - (typeof(limit))*pos) {
1823 *count = limit - (typeof(limit))*pos;
1831 if (unlikely(*pos + *count > MAX_NON_LFS &&
1832 !(file->f_flags & O_LARGEFILE))) {
1833 if (*pos >= MAX_NON_LFS) {
1834 send_sig(SIGXFSZ, current, 0);
1837 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1838 *count = MAX_NON_LFS - (unsigned long)*pos;
1843 * Are we about to exceed the fs block limit ?
1845 * If we have written data it becomes a short write. If we have
1846 * exceeded without writing data we send a signal and return EFBIG.
1847 * Linus frestrict idea will clean these up nicely..
1849 if (likely(!isblk)) {
1850 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1851 if (*count || *pos > inode->i_sb->s_maxbytes) {
1852 send_sig(SIGXFSZ, current, 0);
1855 /* zero-length writes at ->s_maxbytes are OK */
1858 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1859 *count = inode->i_sb->s_maxbytes - *pos;
1862 if (bdev_read_only(I_BDEV(inode)))
1864 isize = i_size_read(inode);
1865 if (*pos >= isize) {
1866 if (*count || *pos > isize)
1870 if (*pos + *count > isize)
1871 *count = isize - *pos;
1875 EXPORT_SYMBOL(generic_write_checks);
1878 generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1879 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1880 size_t count, size_t ocount)
1882 struct file *file = iocb->ki_filp;
1883 struct address_space *mapping = file->f_mapping;
1884 struct inode *inode = mapping->host;
1887 if (count != ocount)
1888 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1890 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1892 loff_t end = pos + written;
1893 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1894 i_size_write(inode, end);
1895 mark_inode_dirty(inode);
1901 * Sync the fs metadata but not the minor inode changes and
1902 * of course not the data as we did direct DMA for the IO.
1903 * i_sem is held, which protects generic_osync_inode() from
1906 if (written >= 0 && file->f_flags & O_SYNC)
1907 generic_osync_inode(inode, mapping, OSYNC_METADATA);
1908 if (written == count && !is_sync_kiocb(iocb))
1909 written = -EIOCBQUEUED;
1912 EXPORT_SYMBOL(generic_file_direct_write);
1915 generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
1916 unsigned long nr_segs, loff_t pos, loff_t *ppos,
1917 size_t count, ssize_t written)
1919 struct file *file = iocb->ki_filp;
1920 struct address_space * mapping = file->f_mapping;
1921 struct address_space_operations *a_ops = mapping->a_ops;
1922 struct inode *inode = mapping->host;
1925 struct page *cached_page = NULL;
1927 struct pagevec lru_pvec;
1928 const struct iovec *cur_iov = iov; /* current iovec */
1929 size_t iov_base = 0; /* offset in the current iovec */
1932 pagevec_init(&lru_pvec, 0);
1935 * handle partial DIO write. Adjust cur_iov if needed.
1937 if (likely(nr_segs == 1))
1938 buf = iov->iov_base + written;
1940 filemap_set_next_iovec(&cur_iov, &iov_base, written);
1941 buf = iov->iov_base + iov_base;
1945 unsigned long index;
1946 unsigned long offset;
1949 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1950 index = pos >> PAGE_CACHE_SHIFT;
1951 bytes = PAGE_CACHE_SIZE - offset;
1956 * Bring in the user page that we will copy from _first_.
1957 * Otherwise there's a nasty deadlock on copying from the
1958 * same page as we're writing to, without it being marked
1961 fault_in_pages_readable(buf, bytes);
1963 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1969 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1970 if (unlikely(status)) {
1971 loff_t isize = i_size_read(inode);
1973 * prepare_write() may have instantiated a few blocks
1974 * outside i_size. Trim these off again.
1977 page_cache_release(page);
1978 if (pos + bytes > isize)
1979 vmtruncate(inode, isize);
1982 if (likely(nr_segs == 1))
1983 copied = filemap_copy_from_user(page, offset,
1986 copied = filemap_copy_from_user_iovec(page, offset,
1987 cur_iov, iov_base, bytes);
1988 flush_dcache_page(page);
1989 status = a_ops->commit_write(file, page, offset, offset+bytes);
1990 if (likely(copied > 0)) {
1999 if (unlikely(nr_segs > 1))
2000 filemap_set_next_iovec(&cur_iov,
2004 if (unlikely(copied != bytes))
2008 mark_page_accessed(page);
2009 page_cache_release(page);
2012 balance_dirty_pages_ratelimited(mapping);
2018 page_cache_release(cached_page);
2021 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2023 if (likely(status >= 0)) {
2024 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2025 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2026 status = generic_osync_inode(inode, mapping,
2027 OSYNC_METADATA|OSYNC_DATA);
2032 * If we get here for O_DIRECT writes then we must have fallen through
2033 * to buffered writes (block instantiation inside i_size). So we sync
2034 * the file data here, to try to honour O_DIRECT expectations.
2036 if (unlikely(file->f_flags & O_DIRECT) && written)
2037 status = filemap_write_and_wait(mapping);
2039 pagevec_lru_add(&lru_pvec);
2040 return written ? written : status;
2042 EXPORT_SYMBOL(generic_file_buffered_write);
2045 __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2046 unsigned long nr_segs, loff_t *ppos)
2048 struct file *file = iocb->ki_filp;
2049 struct address_space * mapping = file->f_mapping;
2050 size_t ocount; /* original count */
2051 size_t count; /* after file limit checks */
2052 struct inode *inode = mapping->host;
2059 for (seg = 0; seg < nr_segs; seg++) {
2060 const struct iovec *iv = &iov[seg];
2063 * If any segment has a negative length, or the cumulative
2064 * length ever wraps negative then return -EINVAL.
2066 ocount += iv->iov_len;
2067 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2069 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2074 ocount -= iv->iov_len; /* This segment is no good */
2081 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2083 /* We can write back this queue in page reclaim */
2084 current->backing_dev_info = mapping->backing_dev_info;
2087 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2094 err = remove_suid(file->f_dentry);
2098 inode_update_time(inode, 1);
2100 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2101 if (unlikely(file->f_flags & O_DIRECT)) {
2102 written = generic_file_direct_write(iocb, iov,
2103 &nr_segs, pos, ppos, count, ocount);
2104 if (written < 0 || written == count)
2107 * direct-io write to a hole: fall through to buffered I/O
2108 * for completing the rest of the request.
2114 written = generic_file_buffered_write(iocb, iov, nr_segs,
2115 pos, ppos, count, written);
2117 current->backing_dev_info = NULL;
2118 return written ? written : err;
2120 EXPORT_SYMBOL(generic_file_aio_write_nolock);
2123 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2124 unsigned long nr_segs, loff_t *ppos)
2126 struct file *file = iocb->ki_filp;
2127 struct address_space *mapping = file->f_mapping;
2128 struct inode *inode = mapping->host;
2132 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
2134 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2137 err = sync_page_range_nolock(inode, mapping, pos, ret);
2145 __generic_file_write_nolock(struct file *file, const struct iovec *iov,
2146 unsigned long nr_segs, loff_t *ppos)
2151 init_sync_kiocb(&kiocb, file);
2152 ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2153 if (ret == -EIOCBQUEUED)
2154 ret = wait_on_sync_kiocb(&kiocb);
2159 generic_file_write_nolock(struct file *file, const struct iovec *iov,
2160 unsigned long nr_segs, loff_t *ppos)
2165 init_sync_kiocb(&kiocb, file);
2166 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2167 if (-EIOCBQUEUED == ret)
2168 ret = wait_on_sync_kiocb(&kiocb);
2171 EXPORT_SYMBOL(generic_file_write_nolock);
2173 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2174 size_t count, loff_t pos)
2176 struct file *file = iocb->ki_filp;
2177 struct address_space *mapping = file->f_mapping;
2178 struct inode *inode = mapping->host;
2180 struct iovec local_iov = { .iov_base = (void __user *)buf,
2183 BUG_ON(iocb->ki_pos != pos);
2185 down(&inode->i_sem);
2186 ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1,
2190 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2193 err = sync_page_range(inode, mapping, pos, ret);
2199 EXPORT_SYMBOL(generic_file_aio_write);
2201 ssize_t generic_file_write(struct file *file, const char __user *buf,
2202 size_t count, loff_t *ppos)
2204 struct address_space *mapping = file->f_mapping;
2205 struct inode *inode = mapping->host;
2207 struct iovec local_iov = { .iov_base = (void __user *)buf,
2210 down(&inode->i_sem);
2211 ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
2214 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2217 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2223 EXPORT_SYMBOL(generic_file_write);
2225 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2226 unsigned long nr_segs, loff_t *ppos)
2231 init_sync_kiocb(&kiocb, filp);
2232 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
2233 if (-EIOCBQUEUED == ret)
2234 ret = wait_on_sync_kiocb(&kiocb);
2237 EXPORT_SYMBOL(generic_file_readv);
2239 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2240 unsigned long nr_segs, loff_t *ppos)
2242 struct address_space *mapping = file->f_mapping;
2243 struct inode *inode = mapping->host;
2246 down(&inode->i_sem);
2247 ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
2250 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2253 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2259 EXPORT_SYMBOL(generic_file_writev);
2262 * Called under i_sem for writes to S_ISREG files. Returns -EIO if something
2263 * went wrong during pagecache shootdown.
2266 generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2267 loff_t offset, unsigned long nr_segs)
2269 struct file *file = iocb->ki_filp;
2270 struct address_space *mapping = file->f_mapping;
2274 * If it's a write, unmap all mmappings of the file up-front. This
2275 * will cause any pte dirty bits to be propagated into the pageframes
2276 * for the subsequent filemap_write_and_wait().
2278 if (rw == WRITE && mapping_mapped(mapping))
2279 unmap_mapping_range(mapping, 0, -1, 0);
2281 retval = filemap_write_and_wait(mapping);
2283 retval = mapping->a_ops->direct_IO(rw, iocb, iov,
2285 if (rw == WRITE && mapping->nrpages) {
2286 int err = invalidate_inode_pages2(mapping);
2293 EXPORT_SYMBOL_GPL(generic_file_direct_IO);