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/capability.h>
19 #include <linux/kernel_stat.h>
21 #include <linux/swap.h>
22 #include <linux/mman.h>
23 #include <linux/pagemap.h>
24 #include <linux/file.h>
25 #include <linux/uio.h>
26 #include <linux/hash.h>
27 #include <linux/writeback.h>
28 #include <linux/pagevec.h>
29 #include <linux/blkdev.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
32 #include <linux/cpuset.h>
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 generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
46 loff_t offset, unsigned long nr_segs);
49 * Shared mappings implemented 30.11.1994. It's not fully working yet,
52 * Shared mappings now work. 15.8.1995 Bruno.
54 * finished 'unifying' the page and buffer cache and SMP-threaded the
55 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
57 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
63 * ->i_mmap_lock (vmtruncate)
64 * ->private_lock (__free_pte->__set_page_dirty_buffers)
65 * ->swap_lock (exclusive_swap_page, others)
66 * ->mapping->tree_lock
69 * ->i_mmap_lock (truncate->unmap_mapping_range)
73 * ->page_table_lock or pte_lock (various, mainly in memory.c)
74 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
77 * ->lock_page (access_process_vm)
83 * ->i_alloc_sem (various)
86 * ->sb_lock (fs/fs-writeback.c)
87 * ->mapping->tree_lock (__sync_single_inode)
90 * ->anon_vma.lock (vma_adjust)
93 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
95 * ->page_table_lock or pte_lock
96 * ->swap_lock (try_to_unmap_one)
97 * ->private_lock (try_to_unmap_one)
98 * ->tree_lock (try_to_unmap_one)
99 * ->zone.lru_lock (follow_page->mark_page_accessed)
100 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
101 * ->private_lock (page_remove_rmap->set_page_dirty)
102 * ->tree_lock (page_remove_rmap->set_page_dirty)
103 * ->inode_lock (page_remove_rmap->set_page_dirty)
104 * ->inode_lock (zap_pte_range->set_page_dirty)
105 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 * ->dcache_lock (proc_pid_lookup)
112 * Remove a page from the page cache and free it. Caller has to make
113 * sure the page is locked and that nobody else uses it - or that usage
114 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
116 void __remove_from_page_cache(struct page *page)
118 struct address_space *mapping = page->mapping;
120 radix_tree_delete(&mapping->page_tree, page->index);
121 page->mapping = NULL;
126 void remove_from_page_cache(struct page *page)
128 struct address_space *mapping = page->mapping;
130 BUG_ON(!PageLocked(page));
132 write_lock_irq(&mapping->tree_lock);
133 __remove_from_page_cache(page);
134 write_unlock_irq(&mapping->tree_lock);
137 static int sync_page(void *word)
139 struct address_space *mapping;
142 page = container_of((unsigned long *)word, struct page, flags);
145 * page_mapping() is being called without PG_locked held.
146 * Some knowledge of the state and use of the page is used to
147 * reduce the requirements down to a memory barrier.
148 * The danger here is of a stale page_mapping() return value
149 * indicating a struct address_space different from the one it's
150 * associated with when it is associated with one.
151 * After smp_mb(), it's either the correct page_mapping() for
152 * the page, or an old page_mapping() and the page's own
153 * page_mapping() has gone NULL.
154 * The ->sync_page() address_space operation must tolerate
155 * page_mapping() going NULL. By an amazing coincidence,
156 * this comes about because none of the users of the page
157 * in the ->sync_page() methods make essential use of the
158 * page_mapping(), merely passing the page down to the backing
159 * device's unplug functions when it's non-NULL, which in turn
160 * ignore it for all cases but swap, where only page_private(page) is
161 * of interest. When page_mapping() does go NULL, the entire
162 * call stack gracefully ignores the page and returns.
166 mapping = page_mapping(page);
167 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
168 mapping->a_ops->sync_page(page);
174 * filemap_fdatawrite_range - start writeback against all of a mapping's
175 * dirty pages that lie within the byte offsets <start, end>
176 * @mapping: address space structure to write
177 * @start: offset in bytes where the range starts
178 * @end: offset in bytes where the range ends (inclusive)
179 * @sync_mode: enable synchronous operation
181 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
182 * opposed to a regular memory * cleansing writeback. The difference between
183 * these two operations is that if a dirty page/buffer is encountered, it must
184 * be waited upon, and not just skipped over.
186 int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
187 loff_t end, int sync_mode)
190 struct writeback_control wbc = {
191 .sync_mode = sync_mode,
192 .nr_to_write = mapping->nrpages * 2,
197 if (!mapping_cap_writeback_dirty(mapping))
200 ret = do_writepages(mapping, &wbc);
204 static inline int __filemap_fdatawrite(struct address_space *mapping,
207 return __filemap_fdatawrite_range(mapping, 0, 0, sync_mode);
210 int filemap_fdatawrite(struct address_space *mapping)
212 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
214 EXPORT_SYMBOL(filemap_fdatawrite);
216 static int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
219 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
223 * This is a mostly non-blocking flush. Not suitable for data-integrity
224 * purposes - I/O may not be started against all dirty pages.
226 int filemap_flush(struct address_space *mapping)
228 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
230 EXPORT_SYMBOL(filemap_flush);
233 * Wait for writeback to complete against pages indexed by start->end
236 int wait_on_page_writeback_range(struct address_space *mapping,
237 pgoff_t start, pgoff_t end)
247 pagevec_init(&pvec, 0);
249 while ((index <= end) &&
250 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
251 PAGECACHE_TAG_WRITEBACK,
252 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
255 for (i = 0; i < nr_pages; i++) {
256 struct page *page = pvec.pages[i];
258 /* until radix tree lookup accepts end_index */
259 if (page->index > end)
262 wait_on_page_writeback(page);
266 pagevec_release(&pvec);
270 /* Check for outstanding write errors */
271 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
273 if (test_and_clear_bit(AS_EIO, &mapping->flags))
280 * Write and wait upon all the pages in the passed range. This is a "data
281 * integrity" operation. It waits upon in-flight writeout before starting and
282 * waiting upon new writeout. If there was an IO error, return it.
284 * We need to re-take i_mutex during the generic_osync_inode list walk because
285 * it is otherwise livelockable.
287 int sync_page_range(struct inode *inode, struct address_space *mapping,
288 loff_t pos, loff_t count)
290 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
291 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
294 if (!mapping_cap_writeback_dirty(mapping) || !count)
296 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
298 mutex_lock(&inode->i_mutex);
299 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
300 mutex_unlock(&inode->i_mutex);
303 ret = wait_on_page_writeback_range(mapping, start, end);
306 EXPORT_SYMBOL(sync_page_range);
309 * Note: Holding i_mutex across sync_page_range_nolock is not a good idea
310 * as it forces O_SYNC writers to different parts of the same file
311 * to be serialised right until io completion.
313 int sync_page_range_nolock(struct inode *inode, struct address_space *mapping,
314 loff_t pos, loff_t count)
316 pgoff_t start = pos >> PAGE_CACHE_SHIFT;
317 pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
320 if (!mapping_cap_writeback_dirty(mapping) || !count)
322 ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1);
324 ret = generic_osync_inode(inode, mapping, OSYNC_METADATA);
326 ret = wait_on_page_writeback_range(mapping, start, end);
329 EXPORT_SYMBOL(sync_page_range_nolock);
332 * filemap_fdatawait - walk the list of under-writeback pages of the given
333 * address space and wait for all of them.
335 * @mapping: address space structure to wait for
337 int filemap_fdatawait(struct address_space *mapping)
339 loff_t i_size = i_size_read(mapping->host);
344 return wait_on_page_writeback_range(mapping, 0,
345 (i_size - 1) >> PAGE_CACHE_SHIFT);
347 EXPORT_SYMBOL(filemap_fdatawait);
349 int filemap_write_and_wait(struct address_space *mapping)
353 if (mapping->nrpages) {
354 err = filemap_fdatawrite(mapping);
356 * Even if the above returned error, the pages may be
357 * written partially (e.g. -ENOSPC), so we wait for it.
358 * But the -EIO is special case, it may indicate the worst
359 * thing (e.g. bug) happened, so we avoid waiting for it.
362 int err2 = filemap_fdatawait(mapping);
369 EXPORT_SYMBOL(filemap_write_and_wait);
372 * Write out and wait upon file offsets lstart->lend, inclusive.
374 * Note that `lend' is inclusive (describes the last byte to be written) so
375 * that this function can be used to write to the very end-of-file (end = -1).
377 int filemap_write_and_wait_range(struct address_space *mapping,
378 loff_t lstart, loff_t lend)
382 if (mapping->nrpages) {
383 err = __filemap_fdatawrite_range(mapping, lstart, lend,
385 /* See comment of filemap_write_and_wait() */
387 int err2 = wait_on_page_writeback_range(mapping,
388 lstart >> PAGE_CACHE_SHIFT,
389 lend >> PAGE_CACHE_SHIFT);
398 * This function is used to add newly allocated pagecache pages:
399 * the page is new, so we can just run SetPageLocked() against it.
400 * The other page state flags were set by rmqueue().
402 * This function does not add the page to the LRU. The caller must do that.
404 int add_to_page_cache(struct page *page, struct address_space *mapping,
405 pgoff_t offset, gfp_t gfp_mask)
407 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
410 write_lock_irq(&mapping->tree_lock);
411 error = radix_tree_insert(&mapping->page_tree, offset, page);
413 page_cache_get(page);
415 page->mapping = mapping;
416 page->index = offset;
420 write_unlock_irq(&mapping->tree_lock);
421 radix_tree_preload_end();
426 EXPORT_SYMBOL(add_to_page_cache);
428 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
429 pgoff_t offset, gfp_t gfp_mask)
431 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
438 struct page *page_cache_alloc(struct address_space *x)
440 if (cpuset_do_page_mem_spread()) {
441 int n = cpuset_mem_spread_node();
442 return alloc_pages_node(n, mapping_gfp_mask(x), 0);
444 return alloc_pages(mapping_gfp_mask(x), 0);
446 EXPORT_SYMBOL(page_cache_alloc);
448 struct page *page_cache_alloc_cold(struct address_space *x)
450 if (cpuset_do_page_mem_spread()) {
451 int n = cpuset_mem_spread_node();
452 return alloc_pages_node(n, mapping_gfp_mask(x)|__GFP_COLD, 0);
454 return alloc_pages(mapping_gfp_mask(x)|__GFP_COLD, 0);
456 EXPORT_SYMBOL(page_cache_alloc_cold);
460 * In order to wait for pages to become available there must be
461 * waitqueues associated with pages. By using a hash table of
462 * waitqueues where the bucket discipline is to maintain all
463 * waiters on the same queue and wake all when any of the pages
464 * become available, and for the woken contexts to check to be
465 * sure the appropriate page became available, this saves space
466 * at a cost of "thundering herd" phenomena during rare hash
469 static wait_queue_head_t *page_waitqueue(struct page *page)
471 const struct zone *zone = page_zone(page);
473 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
476 static inline void wake_up_page(struct page *page, int bit)
478 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
481 void fastcall wait_on_page_bit(struct page *page, int bit_nr)
483 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
485 if (test_bit(bit_nr, &page->flags))
486 __wait_on_bit(page_waitqueue(page), &wait, sync_page,
487 TASK_UNINTERRUPTIBLE);
489 EXPORT_SYMBOL(wait_on_page_bit);
492 * unlock_page() - unlock a locked page
496 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
497 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
498 * mechananism between PageLocked pages and PageWriteback pages is shared.
499 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
501 * The first mb is necessary to safely close the critical section opened by the
502 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
503 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
504 * parallel wait_on_page_locked()).
506 void fastcall unlock_page(struct page *page)
508 smp_mb__before_clear_bit();
509 if (!TestClearPageLocked(page))
511 smp_mb__after_clear_bit();
512 wake_up_page(page, PG_locked);
514 EXPORT_SYMBOL(unlock_page);
517 * End writeback against a page.
519 void end_page_writeback(struct page *page)
521 struct zone *zone = page_zone(page);
522 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
523 if (!test_clear_page_writeback(page))
526 smp_mb__after_clear_bit();
527 if (zone->all_unreclaimable) {
528 spin_lock(&zone->lock);
529 zone->all_unreclaimable = 0;
530 zone->pages_scanned = 0;
531 spin_unlock(&zone->lock);
533 wake_up_page(page, PG_writeback);
535 EXPORT_SYMBOL(end_page_writeback);
538 * Get a lock on the page, assuming we need to sleep to get it.
540 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
541 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
542 * chances are that on the second loop, the block layer's plug list is empty,
543 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
545 void fastcall __lock_page(struct page *page)
547 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
549 __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
550 TASK_UNINTERRUPTIBLE);
552 EXPORT_SYMBOL(__lock_page);
555 * a rather lightweight function, finding and getting a reference to a
556 * hashed page atomically.
558 struct page * find_get_page(struct address_space *mapping, unsigned long offset)
562 read_lock_irq(&mapping->tree_lock);
563 page = radix_tree_lookup(&mapping->page_tree, offset);
565 page_cache_get(page);
566 read_unlock_irq(&mapping->tree_lock);
570 EXPORT_SYMBOL(find_get_page);
573 * Same as above, but trylock it instead of incrementing the count.
575 struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
579 read_lock_irq(&mapping->tree_lock);
580 page = radix_tree_lookup(&mapping->page_tree, offset);
581 if (page && TestSetPageLocked(page))
583 read_unlock_irq(&mapping->tree_lock);
587 EXPORT_SYMBOL(find_trylock_page);
590 * find_lock_page - locate, pin and lock a pagecache page
592 * @mapping: the address_space to search
593 * @offset: the page index
595 * Locates the desired pagecache page, locks it, increments its reference
596 * count and returns its address.
598 * Returns zero if the page was not present. find_lock_page() may sleep.
600 struct page *find_lock_page(struct address_space *mapping,
601 unsigned long offset)
605 read_lock_irq(&mapping->tree_lock);
607 page = radix_tree_lookup(&mapping->page_tree, offset);
609 page_cache_get(page);
610 if (TestSetPageLocked(page)) {
611 read_unlock_irq(&mapping->tree_lock);
613 read_lock_irq(&mapping->tree_lock);
615 /* Has the page been truncated while we slept? */
616 if (unlikely(page->mapping != mapping ||
617 page->index != offset)) {
619 page_cache_release(page);
624 read_unlock_irq(&mapping->tree_lock);
628 EXPORT_SYMBOL(find_lock_page);
631 * find_or_create_page - locate or add a pagecache page
633 * @mapping: the page's address_space
634 * @index: the page's index into the mapping
635 * @gfp_mask: page allocation mode
637 * Locates a page in the pagecache. If the page is not present, a new page
638 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
639 * LRU list. The returned page is locked and has its reference count
642 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
645 * find_or_create_page() returns the desired page's address, or zero on
648 struct page *find_or_create_page(struct address_space *mapping,
649 unsigned long index, gfp_t gfp_mask)
651 struct page *page, *cached_page = NULL;
654 page = find_lock_page(mapping, index);
657 cached_page = alloc_page(gfp_mask);
661 err = add_to_page_cache_lru(cached_page, mapping,
666 } else if (err == -EEXIST)
670 page_cache_release(cached_page);
674 EXPORT_SYMBOL(find_or_create_page);
677 * find_get_pages - gang pagecache lookup
678 * @mapping: The address_space to search
679 * @start: The starting page index
680 * @nr_pages: The maximum number of pages
681 * @pages: Where the resulting pages are placed
683 * find_get_pages() will search for and return a group of up to
684 * @nr_pages pages in the mapping. The pages are placed at @pages.
685 * find_get_pages() takes a reference against the returned pages.
687 * The search returns a group of mapping-contiguous pages with ascending
688 * indexes. There may be holes in the indices due to not-present pages.
690 * find_get_pages() returns the number of pages which were found.
692 unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
693 unsigned int nr_pages, struct page **pages)
698 read_lock_irq(&mapping->tree_lock);
699 ret = radix_tree_gang_lookup(&mapping->page_tree,
700 (void **)pages, start, nr_pages);
701 for (i = 0; i < ret; i++)
702 page_cache_get(pages[i]);
703 read_unlock_irq(&mapping->tree_lock);
708 * find_get_pages_contig - gang contiguous pagecache lookup
709 * @mapping: The address_space to search
710 * @index: The starting page index
711 * @nr_pages: The maximum number of pages
712 * @pages: Where the resulting pages are placed
714 * find_get_pages_contig() works exactly like find_get_pages(), except
715 * that the returned number of pages are guaranteed to be contiguous.
717 * find_get_pages_contig() returns the number of pages which were found.
719 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
720 unsigned int nr_pages, struct page **pages)
725 read_lock_irq(&mapping->tree_lock);
726 ret = radix_tree_gang_lookup(&mapping->page_tree,
727 (void **)pages, index, nr_pages);
728 for (i = 0; i < ret; i++) {
729 if (pages[i]->mapping == NULL || pages[i]->index != index)
732 page_cache_get(pages[i]);
735 read_unlock_irq(&mapping->tree_lock);
740 * Like find_get_pages, except we only return pages which are tagged with
741 * `tag'. We update *index to index the next page for the traversal.
743 unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
744 int tag, unsigned int nr_pages, struct page **pages)
749 read_lock_irq(&mapping->tree_lock);
750 ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
751 (void **)pages, *index, nr_pages, tag);
752 for (i = 0; i < ret; i++)
753 page_cache_get(pages[i]);
755 *index = pages[ret - 1]->index + 1;
756 read_unlock_irq(&mapping->tree_lock);
761 * Same as grab_cache_page, but do not wait if the page is unavailable.
762 * This is intended for speculative data generators, where the data can
763 * be regenerated if the page couldn't be grabbed. This routine should
764 * be safe to call while holding the lock for another page.
766 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
767 * and deadlock against the caller's locked page.
770 grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
772 struct page *page = find_get_page(mapping, index);
776 if (!TestSetPageLocked(page))
778 page_cache_release(page);
781 gfp_mask = mapping_gfp_mask(mapping) & ~__GFP_FS;
782 page = alloc_pages(gfp_mask, 0);
783 if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
784 page_cache_release(page);
790 EXPORT_SYMBOL(grab_cache_page_nowait);
793 * This is a generic file read routine, and uses the
794 * mapping->a_ops->readpage() function for the actual low-level
797 * This is really ugly. But the goto's actually try to clarify some
798 * of the logic when it comes to error handling etc.
800 * Note the struct file* is only passed for the use of readpage. It may be
803 void do_generic_mapping_read(struct address_space *mapping,
804 struct file_ra_state *_ra,
807 read_descriptor_t *desc,
811 struct inode *inode = mapping->host;
813 unsigned long end_index;
814 unsigned long offset;
815 unsigned long last_index;
816 unsigned long next_index;
817 unsigned long prev_index;
819 struct page *cached_page;
821 struct file_ra_state ra = *_ra;
824 index = *ppos >> PAGE_CACHE_SHIFT;
826 prev_index = ra.prev_page;
827 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
828 offset = *ppos & ~PAGE_CACHE_MASK;
830 isize = i_size_read(inode);
834 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
837 unsigned long nr, ret;
839 /* nr is the maximum number of bytes to copy from this page */
840 nr = PAGE_CACHE_SIZE;
841 if (index >= end_index) {
842 if (index > end_index)
844 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
852 if (index == next_index)
853 next_index = page_cache_readahead(mapping, &ra, filp,
854 index, last_index - index);
857 page = find_get_page(mapping, index);
858 if (unlikely(page == NULL)) {
860 desc->error = -EWOULDBLOCKIO;
863 handle_ra_miss(mapping, &ra, index);
866 if (!PageUptodate(page)) {
868 page_cache_release(page);
869 desc->error = -EWOULDBLOCKIO;
872 goto page_not_up_to_date;
876 /* If users can be writing to this page using arbitrary
877 * virtual addresses, take care about potential aliasing
878 * before reading the page on the kernel side.
880 if (mapping_writably_mapped(mapping))
881 flush_dcache_page(page);
884 * When (part of) the same page is read multiple times
885 * in succession, only mark it as accessed the first time.
887 if (prev_index != index)
888 mark_page_accessed(page);
892 * Ok, we have the page, and it's up-to-date, so
893 * now we can copy it to user space...
895 * The actor routine returns how many bytes were actually used..
896 * NOTE! This may not be the same as how much of a user buffer
897 * we filled up (we may be padding etc), so we can only update
898 * "pos" here (the actor routine has to update the user buffer
899 * pointers and the remaining count).
901 ret = actor(desc, page, offset, nr);
903 index += offset >> PAGE_CACHE_SHIFT;
904 offset &= ~PAGE_CACHE_MASK;
906 page_cache_release(page);
907 if (ret == nr && desc->count)
912 /* Get exclusive access to the page ... */
915 /* Did it get unhashed before we got the lock? */
916 if (!page->mapping) {
918 page_cache_release(page);
922 /* Did somebody else fill it already? */
923 if (PageUptodate(page)) {
929 /* Start the actual read. The read will unlock the page. */
930 error = mapping->a_ops->readpage(filp, page);
932 if (unlikely(error)) {
933 if (error == AOP_TRUNCATED_PAGE) {
934 page_cache_release(page);
940 if (!PageUptodate(page)) {
942 if (!PageUptodate(page)) {
943 if (page->mapping == NULL) {
945 * invalidate_inode_pages got it
948 page_cache_release(page);
959 * i_size must be checked after we have done ->readpage.
961 * Checking i_size after the readpage allows us to calculate
962 * the correct value for "nr", which means the zero-filled
963 * part of the page is not copied back to userspace (unless
964 * another truncate extends the file - this is desired though).
966 isize = i_size_read(inode);
967 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
968 if (unlikely(!isize || index > end_index)) {
969 page_cache_release(page);
973 /* nr is the maximum number of bytes to copy from this page */
974 nr = PAGE_CACHE_SIZE;
975 if (index == end_index) {
976 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
978 page_cache_release(page);
986 /* UHHUH! A synchronous read error occurred. Report it */
988 page_cache_release(page);
993 * Ok, it wasn't cached, so we need to create a new
997 cached_page = page_cache_alloc_cold(mapping);
999 desc->error = -ENOMEM;
1003 error = add_to_page_cache_lru(cached_page, mapping,
1006 if (error == -EEXIST)
1008 desc->error = error;
1019 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1021 page_cache_release(cached_page);
1023 file_accessed(filp);
1026 EXPORT_SYMBOL(do_generic_mapping_read);
1028 int file_read_actor(read_descriptor_t *desc, struct page *page,
1029 unsigned long offset, unsigned long size)
1032 unsigned long left, count = desc->count;
1038 * Faults on the destination of a read are common, so do it before
1041 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1042 kaddr = kmap_atomic(page, KM_USER0);
1043 left = __copy_to_user_inatomic(desc->arg.buf,
1044 kaddr + offset, size);
1045 kunmap_atomic(kaddr, KM_USER0);
1050 /* Do it the slow way */
1052 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1057 desc->error = -EFAULT;
1060 desc->count = count - size;
1061 desc->written += size;
1062 desc->arg.buf += size;
1067 * This is the "read()" routine for all filesystems
1068 * that can use the page cache directly.
1071 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1072 unsigned long nr_segs, loff_t *ppos)
1074 struct file *filp = iocb->ki_filp;
1080 for (seg = 0; seg < nr_segs; seg++) {
1081 const struct iovec *iv = &iov[seg];
1084 * If any segment has a negative length, or the cumulative
1085 * length ever wraps negative then return -EINVAL.
1087 count += iv->iov_len;
1088 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1090 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1095 count -= iv->iov_len; /* This segment is no good */
1099 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1100 if (filp->f_flags & O_DIRECT) {
1101 loff_t pos = *ppos, size;
1102 struct address_space *mapping;
1103 struct inode *inode;
1105 mapping = filp->f_mapping;
1106 inode = mapping->host;
1109 goto out; /* skip atime */
1110 size = i_size_read(inode);
1112 retval = generic_file_direct_IO(READ, iocb,
1114 if (retval > 0 && !is_sync_kiocb(iocb))
1115 retval = -EIOCBQUEUED;
1117 *ppos = pos + retval;
1119 file_accessed(filp);
1125 for (seg = 0; seg < nr_segs; seg++) {
1126 read_descriptor_t desc;
1129 desc.arg.buf = iov[seg].iov_base;
1130 desc.count = iov[seg].iov_len;
1131 if (desc.count == 0)
1134 do_generic_file_read(filp,ppos,&desc,file_read_actor,0);
1135 retval += desc.written;
1137 retval = retval ?: desc.error;
1146 EXPORT_SYMBOL(__generic_file_aio_read);
1149 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1151 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1153 BUG_ON(iocb->ki_pos != pos);
1154 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
1157 EXPORT_SYMBOL(generic_file_aio_read);
1160 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1162 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
1166 init_sync_kiocb(&kiocb, filp);
1167 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
1168 if (-EIOCBQUEUED == ret)
1169 ret = wait_on_sync_kiocb(&kiocb);
1173 EXPORT_SYMBOL(generic_file_read);
1175 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1178 unsigned long count = desc->count;
1179 struct file *file = desc->arg.data;
1184 written = file->f_op->sendpage(file, page, offset,
1185 size, &file->f_pos, size<count);
1187 desc->error = written;
1190 desc->count = count - written;
1191 desc->written += written;
1195 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
1196 size_t count, read_actor_t actor, void *target)
1198 read_descriptor_t desc;
1205 desc.arg.data = target;
1208 do_generic_file_read(in_file, ppos, &desc, actor, 0);
1210 return desc.written;
1214 EXPORT_SYMBOL(generic_file_sendfile);
1217 do_readahead(struct address_space *mapping, struct file *filp,
1218 unsigned long index, unsigned long nr)
1220 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1223 force_page_cache_readahead(mapping, filp, index,
1224 max_sane_readahead(nr));
1228 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1236 if (file->f_mode & FMODE_READ) {
1237 struct address_space *mapping = file->f_mapping;
1238 unsigned long start = offset >> PAGE_CACHE_SHIFT;
1239 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1240 unsigned long len = end - start + 1;
1241 ret = do_readahead(mapping, file, start, len);
1250 * This adds the requested page to the page cache if it isn't already there,
1251 * and schedules an I/O to read in its contents from disk.
1253 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
1254 static int fastcall page_cache_read(struct file * file, unsigned long offset)
1256 struct address_space *mapping = file->f_mapping;
1261 page = page_cache_alloc_cold(mapping);
1265 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1267 ret = mapping->a_ops->readpage(file, page);
1268 else if (ret == -EEXIST)
1269 ret = 0; /* losing race to add is OK */
1271 page_cache_release(page);
1273 } while (ret == AOP_TRUNCATED_PAGE);
1278 #define MMAP_LOTSAMISS (100)
1281 * filemap_nopage() is invoked via the vma operations vector for a
1282 * mapped memory region to read in file data during a page fault.
1284 * The goto's are kind of ugly, but this streamlines the normal case of having
1285 * it in the page cache, and handles the special cases reasonably without
1286 * having a lot of duplicated code.
1288 struct page *filemap_nopage(struct vm_area_struct *area,
1289 unsigned long address, int *type)
1292 struct file *file = area->vm_file;
1293 struct address_space *mapping = file->f_mapping;
1294 struct file_ra_state *ra = &file->f_ra;
1295 struct inode *inode = mapping->host;
1297 unsigned long size, pgoff;
1298 int did_readaround = 0, majmin = VM_FAULT_MINOR;
1300 pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1303 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1305 goto outside_data_content;
1307 /* If we don't want any read-ahead, don't bother */
1308 if (VM_RandomReadHint(area))
1309 goto no_cached_page;
1312 * The readahead code wants to be told about each and every page
1313 * so it can build and shrink its windows appropriately
1315 * For sequential accesses, we use the generic readahead logic.
1317 if (VM_SequentialReadHint(area))
1318 page_cache_readahead(mapping, ra, file, pgoff, 1);
1321 * Do we have something in the page cache already?
1324 page = find_get_page(mapping, pgoff);
1326 unsigned long ra_pages;
1328 if (VM_SequentialReadHint(area)) {
1329 handle_ra_miss(mapping, ra, pgoff);
1330 goto no_cached_page;
1335 * Do we miss much more than hit in this file? If so,
1336 * stop bothering with read-ahead. It will only hurt.
1338 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1339 goto no_cached_page;
1342 * To keep the pgmajfault counter straight, we need to
1343 * check did_readaround, as this is an inner loop.
1345 if (!did_readaround) {
1346 majmin = VM_FAULT_MAJOR;
1347 inc_page_state(pgmajfault);
1350 ra_pages = max_sane_readahead(file->f_ra.ra_pages);
1354 if (pgoff > ra_pages / 2)
1355 start = pgoff - ra_pages / 2;
1356 do_page_cache_readahead(mapping, file, start, ra_pages);
1358 page = find_get_page(mapping, pgoff);
1360 goto no_cached_page;
1363 if (!did_readaround)
1367 * Ok, found a page in the page cache, now we need to check
1368 * that it's up-to-date.
1370 if (!PageUptodate(page))
1371 goto page_not_uptodate;
1375 * Found the page and have a reference on it.
1377 mark_page_accessed(page);
1382 outside_data_content:
1384 * An external ptracer can access pages that normally aren't
1387 if (area->vm_mm == current->mm)
1389 /* Fall through to the non-read-ahead case */
1392 * We're only likely to ever get here if MADV_RANDOM is in
1395 error = page_cache_read(file, pgoff);
1399 * The page we want has now been added to the page cache.
1400 * In the unlikely event that someone removed it in the
1401 * meantime, we'll just come back here and read it again.
1407 * An error return from page_cache_read can result if the
1408 * system is low on memory, or a problem occurs while trying
1411 if (error == -ENOMEM)
1416 if (!did_readaround) {
1417 majmin = VM_FAULT_MAJOR;
1418 inc_page_state(pgmajfault);
1422 /* Did it get unhashed while we waited for it? */
1423 if (!page->mapping) {
1425 page_cache_release(page);
1429 /* Did somebody else get it up-to-date? */
1430 if (PageUptodate(page)) {
1435 error = mapping->a_ops->readpage(file, page);
1437 wait_on_page_locked(page);
1438 if (PageUptodate(page))
1440 } else if (error == AOP_TRUNCATED_PAGE) {
1441 page_cache_release(page);
1446 * Umm, take care of errors if the page isn't up-to-date.
1447 * Try to re-read it _once_. We do this synchronously,
1448 * because there really aren't any performance issues here
1449 * and we need to check for errors.
1453 /* Somebody truncated the page on us? */
1454 if (!page->mapping) {
1456 page_cache_release(page);
1460 /* Somebody else successfully read it in? */
1461 if (PageUptodate(page)) {
1465 ClearPageError(page);
1466 error = mapping->a_ops->readpage(file, page);
1468 wait_on_page_locked(page);
1469 if (PageUptodate(page))
1471 } else if (error == AOP_TRUNCATED_PAGE) {
1472 page_cache_release(page);
1477 * Things didn't work out. Return zero to tell the
1478 * mm layer so, possibly freeing the page cache page first.
1480 page_cache_release(page);
1484 EXPORT_SYMBOL(filemap_nopage);
1486 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1489 struct address_space *mapping = file->f_mapping;
1494 * Do we have something in the page cache already?
1497 page = find_get_page(mapping, pgoff);
1501 goto no_cached_page;
1505 * Ok, found a page in the page cache, now we need to check
1506 * that it's up-to-date.
1508 if (!PageUptodate(page)) {
1510 page_cache_release(page);
1513 goto page_not_uptodate;
1518 * Found the page and have a reference on it.
1520 mark_page_accessed(page);
1524 error = page_cache_read(file, pgoff);
1527 * The page we want has now been added to the page cache.
1528 * In the unlikely event that someone removed it in the
1529 * meantime, we'll just come back here and read it again.
1535 * An error return from page_cache_read can result if the
1536 * system is low on memory, or a problem occurs while trying
1544 /* Did it get unhashed while we waited for it? */
1545 if (!page->mapping) {
1550 /* Did somebody else get it up-to-date? */
1551 if (PageUptodate(page)) {
1556 error = mapping->a_ops->readpage(file, page);
1558 wait_on_page_locked(page);
1559 if (PageUptodate(page))
1561 } else if (error == AOP_TRUNCATED_PAGE) {
1562 page_cache_release(page);
1567 * Umm, take care of errors if the page isn't up-to-date.
1568 * Try to re-read it _once_. We do this synchronously,
1569 * because there really aren't any performance issues here
1570 * and we need to check for errors.
1574 /* Somebody truncated the page on us? */
1575 if (!page->mapping) {
1579 /* Somebody else successfully read it in? */
1580 if (PageUptodate(page)) {
1585 ClearPageError(page);
1586 error = mapping->a_ops->readpage(file, page);
1588 wait_on_page_locked(page);
1589 if (PageUptodate(page))
1591 } else if (error == AOP_TRUNCATED_PAGE) {
1592 page_cache_release(page);
1597 * Things didn't work out. Return zero to tell the
1598 * mm layer so, possibly freeing the page cache page first.
1601 page_cache_release(page);
1606 int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
1607 unsigned long len, pgprot_t prot, unsigned long pgoff,
1610 struct file *file = vma->vm_file;
1611 struct address_space *mapping = file->f_mapping;
1612 struct inode *inode = mapping->host;
1614 struct mm_struct *mm = vma->vm_mm;
1619 force_page_cache_readahead(mapping, vma->vm_file,
1620 pgoff, len >> PAGE_CACHE_SHIFT);
1623 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1624 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1627 page = filemap_getpage(file, pgoff, nonblock);
1629 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1630 * done in shmem_populate calling shmem_getpage */
1631 if (!page && !nonblock)
1635 err = install_page(mm, vma, addr, page, prot);
1637 page_cache_release(page);
1640 } else if (vma->vm_flags & VM_NONLINEAR) {
1641 /* No page was found just because we can't read it in now (being
1642 * here implies nonblock != 0), but the page may exist, so set
1643 * the PTE to fault it in later. */
1644 err = install_file_pte(mm, vma, addr, pgoff, prot);
1657 EXPORT_SYMBOL(filemap_populate);
1659 struct vm_operations_struct generic_file_vm_ops = {
1660 .nopage = filemap_nopage,
1661 .populate = filemap_populate,
1664 /* This is used for a general mmap of a disk file */
1666 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1668 struct address_space *mapping = file->f_mapping;
1670 if (!mapping->a_ops->readpage)
1672 file_accessed(file);
1673 vma->vm_ops = &generic_file_vm_ops;
1678 * This is for filesystems which do not implement ->writepage.
1680 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1682 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1684 return generic_file_mmap(file, vma);
1687 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1691 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1695 #endif /* CONFIG_MMU */
1697 EXPORT_SYMBOL(generic_file_mmap);
1698 EXPORT_SYMBOL(generic_file_readonly_mmap);
1700 static inline struct page *__read_cache_page(struct address_space *mapping,
1701 unsigned long index,
1702 int (*filler)(void *,struct page*),
1705 struct page *page, *cached_page = NULL;
1708 page = find_get_page(mapping, index);
1711 cached_page = page_cache_alloc_cold(mapping);
1713 return ERR_PTR(-ENOMEM);
1715 err = add_to_page_cache_lru(cached_page, mapping,
1720 /* Presumably ENOMEM for radix tree node */
1721 page_cache_release(cached_page);
1722 return ERR_PTR(err);
1726 err = filler(data, page);
1728 page_cache_release(page);
1729 page = ERR_PTR(err);
1733 page_cache_release(cached_page);
1738 * Read into the page cache. If a page already exists,
1739 * and PageUptodate() is not set, try to fill the page.
1741 struct page *read_cache_page(struct address_space *mapping,
1742 unsigned long index,
1743 int (*filler)(void *,struct page*),
1750 page = __read_cache_page(mapping, index, filler, data);
1753 mark_page_accessed(page);
1754 if (PageUptodate(page))
1758 if (!page->mapping) {
1760 page_cache_release(page);
1763 if (PageUptodate(page)) {
1767 err = filler(data, page);
1769 page_cache_release(page);
1770 page = ERR_PTR(err);
1776 EXPORT_SYMBOL(read_cache_page);
1779 * If the page was newly created, increment its refcount and add it to the
1780 * caller's lru-buffering pagevec. This function is specifically for
1781 * generic_file_write().
1783 static inline struct page *
1784 __grab_cache_page(struct address_space *mapping, unsigned long index,
1785 struct page **cached_page, struct pagevec *lru_pvec)
1790 page = find_lock_page(mapping, index);
1792 if (!*cached_page) {
1793 *cached_page = page_cache_alloc(mapping);
1797 err = add_to_page_cache(*cached_page, mapping,
1802 page = *cached_page;
1803 page_cache_get(page);
1804 if (!pagevec_add(lru_pvec, page))
1805 __pagevec_lru_add(lru_pvec);
1806 *cached_page = NULL;
1813 * The logic we want is
1815 * if suid or (sgid and xgrp)
1818 int remove_suid(struct dentry *dentry)
1820 mode_t mode = dentry->d_inode->i_mode;
1824 /* suid always must be killed */
1825 if (unlikely(mode & S_ISUID))
1826 kill = ATTR_KILL_SUID;
1829 * sgid without any exec bits is just a mandatory locking mark; leave
1830 * it alone. If some exec bits are set, it's a real sgid; kill it.
1832 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1833 kill |= ATTR_KILL_SGID;
1835 if (unlikely(kill && !capable(CAP_FSETID))) {
1836 struct iattr newattrs;
1838 newattrs.ia_valid = ATTR_FORCE | kill;
1839 result = notify_change(dentry, &newattrs);
1843 EXPORT_SYMBOL(remove_suid);
1846 __filemap_copy_from_user_iovec(char *vaddr,
1847 const struct iovec *iov, size_t base, size_t bytes)
1849 size_t copied = 0, left = 0;
1852 char __user *buf = iov->iov_base + base;
1853 int copy = min(bytes, iov->iov_len - base);
1856 left = __copy_from_user_inatomic(vaddr, buf, copy);
1862 if (unlikely(left)) {
1863 /* zero the rest of the target like __copy_from_user */
1865 memset(vaddr, 0, bytes);
1869 return copied - left;
1873 * Performs necessary checks before doing a write
1875 * Can adjust writing position aor amount of bytes to write.
1876 * Returns appropriate error code that caller should return or
1877 * zero in case that write should be allowed.
1879 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1881 struct inode *inode = file->f_mapping->host;
1882 unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1884 if (unlikely(*pos < 0))
1888 /* FIXME: this is for backwards compatibility with 2.4 */
1889 if (file->f_flags & O_APPEND)
1890 *pos = i_size_read(inode);
1892 if (limit != RLIM_INFINITY) {
1893 if (*pos >= limit) {
1894 send_sig(SIGXFSZ, current, 0);
1897 if (*count > limit - (typeof(limit))*pos) {
1898 *count = limit - (typeof(limit))*pos;
1906 if (unlikely(*pos + *count > MAX_NON_LFS &&
1907 !(file->f_flags & O_LARGEFILE))) {
1908 if (*pos >= MAX_NON_LFS) {
1909 send_sig(SIGXFSZ, current, 0);
1912 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1913 *count = MAX_NON_LFS - (unsigned long)*pos;
1918 * Are we about to exceed the fs block limit ?
1920 * If we have written data it becomes a short write. If we have
1921 * exceeded without writing data we send a signal and return EFBIG.
1922 * Linus frestrict idea will clean these up nicely..
1924 if (likely(!isblk)) {
1925 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1926 if (*count || *pos > inode->i_sb->s_maxbytes) {
1927 send_sig(SIGXFSZ, current, 0);
1930 /* zero-length writes at ->s_maxbytes are OK */
1933 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1934 *count = inode->i_sb->s_maxbytes - *pos;
1937 if (bdev_read_only(I_BDEV(inode)))
1939 isize = i_size_read(inode);
1940 if (*pos >= isize) {
1941 if (*count || *pos > isize)
1945 if (*pos + *count > isize)
1946 *count = isize - *pos;
1950 EXPORT_SYMBOL(generic_write_checks);
1953 generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1954 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
1955 size_t count, size_t ocount)
1957 struct file *file = iocb->ki_filp;
1958 struct address_space *mapping = file->f_mapping;
1959 struct inode *inode = mapping->host;
1962 if (count != ocount)
1963 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
1965 written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
1967 loff_t end = pos + written;
1968 if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
1969 i_size_write(inode, end);
1970 mark_inode_dirty(inode);
1976 * Sync the fs metadata but not the minor inode changes and
1977 * of course not the data as we did direct DMA for the IO.
1978 * i_mutex is held, which protects generic_osync_inode() from
1981 if (written >= 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
1982 int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
1986 if (written == count && !is_sync_kiocb(iocb))
1987 written = -EIOCBQUEUED;
1990 EXPORT_SYMBOL(generic_file_direct_write);
1993 generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
1994 unsigned long nr_segs, loff_t pos, loff_t *ppos,
1995 size_t count, ssize_t written)
1997 struct file *file = iocb->ki_filp;
1998 struct address_space * mapping = file->f_mapping;
1999 struct address_space_operations *a_ops = mapping->a_ops;
2000 struct inode *inode = mapping->host;
2003 struct page *cached_page = NULL;
2005 struct pagevec lru_pvec;
2006 const struct iovec *cur_iov = iov; /* current iovec */
2007 size_t iov_base = 0; /* offset in the current iovec */
2010 pagevec_init(&lru_pvec, 0);
2013 * handle partial DIO write. Adjust cur_iov if needed.
2015 if (likely(nr_segs == 1))
2016 buf = iov->iov_base + written;
2018 filemap_set_next_iovec(&cur_iov, &iov_base, written);
2019 buf = cur_iov->iov_base + iov_base;
2023 unsigned long index;
2024 unsigned long offset;
2025 unsigned long maxlen;
2028 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
2029 index = pos >> PAGE_CACHE_SHIFT;
2030 bytes = PAGE_CACHE_SIZE - offset;
2035 * Bring in the user page that we will copy from _first_.
2036 * Otherwise there's a nasty deadlock on copying from the
2037 * same page as we're writing to, without it being marked
2040 maxlen = cur_iov->iov_len - iov_base;
2043 fault_in_pages_readable(buf, maxlen);
2045 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
2051 status = a_ops->prepare_write(file, page, offset, offset+bytes);
2052 if (unlikely(status)) {
2053 loff_t isize = i_size_read(inode);
2055 if (status != AOP_TRUNCATED_PAGE)
2057 page_cache_release(page);
2058 if (status == AOP_TRUNCATED_PAGE)
2061 * prepare_write() may have instantiated a few blocks
2062 * outside i_size. Trim these off again.
2064 if (pos + bytes > isize)
2065 vmtruncate(inode, isize);
2068 if (likely(nr_segs == 1))
2069 copied = filemap_copy_from_user(page, offset,
2072 copied = filemap_copy_from_user_iovec(page, offset,
2073 cur_iov, iov_base, bytes);
2074 flush_dcache_page(page);
2075 status = a_ops->commit_write(file, page, offset, offset+bytes);
2076 if (status == AOP_TRUNCATED_PAGE) {
2077 page_cache_release(page);
2080 if (likely(copied > 0)) {
2089 if (unlikely(nr_segs > 1)) {
2090 filemap_set_next_iovec(&cur_iov,
2093 buf = cur_iov->iov_base +
2100 if (unlikely(copied != bytes))
2104 mark_page_accessed(page);
2105 page_cache_release(page);
2108 balance_dirty_pages_ratelimited(mapping);
2114 page_cache_release(cached_page);
2117 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2119 if (likely(status >= 0)) {
2120 if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2121 if (!a_ops->writepage || !is_sync_kiocb(iocb))
2122 status = generic_osync_inode(inode, mapping,
2123 OSYNC_METADATA|OSYNC_DATA);
2128 * If we get here for O_DIRECT writes then we must have fallen through
2129 * to buffered writes (block instantiation inside i_size). So we sync
2130 * the file data here, to try to honour O_DIRECT expectations.
2132 if (unlikely(file->f_flags & O_DIRECT) && written)
2133 status = filemap_write_and_wait(mapping);
2135 pagevec_lru_add(&lru_pvec);
2136 return written ? written : status;
2138 EXPORT_SYMBOL(generic_file_buffered_write);
2141 __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2142 unsigned long nr_segs, loff_t *ppos)
2144 struct file *file = iocb->ki_filp;
2145 struct address_space * mapping = file->f_mapping;
2146 size_t ocount; /* original count */
2147 size_t count; /* after file limit checks */
2148 struct inode *inode = mapping->host;
2155 for (seg = 0; seg < nr_segs; seg++) {
2156 const struct iovec *iv = &iov[seg];
2159 * If any segment has a negative length, or the cumulative
2160 * length ever wraps negative then return -EINVAL.
2162 ocount += iv->iov_len;
2163 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
2165 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
2170 ocount -= iv->iov_len; /* This segment is no good */
2177 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2179 /* We can write back this queue in page reclaim */
2180 current->backing_dev_info = mapping->backing_dev_info;
2183 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2190 err = remove_suid(file->f_dentry);
2194 file_update_time(file);
2196 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2197 if (unlikely(file->f_flags & O_DIRECT)) {
2198 written = generic_file_direct_write(iocb, iov,
2199 &nr_segs, pos, ppos, count, ocount);
2200 if (written < 0 || written == count)
2203 * direct-io write to a hole: fall through to buffered I/O
2204 * for completing the rest of the request.
2210 written = generic_file_buffered_write(iocb, iov, nr_segs,
2211 pos, ppos, count, written);
2213 current->backing_dev_info = NULL;
2214 return written ? written : err;
2216 EXPORT_SYMBOL(generic_file_aio_write_nolock);
2219 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
2220 unsigned long nr_segs, loff_t *ppos)
2222 struct file *file = iocb->ki_filp;
2223 struct address_space *mapping = file->f_mapping;
2224 struct inode *inode = mapping->host;
2228 ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
2230 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2233 err = sync_page_range_nolock(inode, mapping, pos, ret);
2241 __generic_file_write_nolock(struct file *file, const struct iovec *iov,
2242 unsigned long nr_segs, loff_t *ppos)
2247 init_sync_kiocb(&kiocb, file);
2248 ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2249 if (ret == -EIOCBQUEUED)
2250 ret = wait_on_sync_kiocb(&kiocb);
2255 generic_file_write_nolock(struct file *file, const struct iovec *iov,
2256 unsigned long nr_segs, loff_t *ppos)
2261 init_sync_kiocb(&kiocb, file);
2262 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
2263 if (-EIOCBQUEUED == ret)
2264 ret = wait_on_sync_kiocb(&kiocb);
2267 EXPORT_SYMBOL(generic_file_write_nolock);
2269 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
2270 size_t count, loff_t pos)
2272 struct file *file = iocb->ki_filp;
2273 struct address_space *mapping = file->f_mapping;
2274 struct inode *inode = mapping->host;
2276 struct iovec local_iov = { .iov_base = (void __user *)buf,
2279 BUG_ON(iocb->ki_pos != pos);
2281 mutex_lock(&inode->i_mutex);
2282 ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1,
2284 mutex_unlock(&inode->i_mutex);
2286 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2289 err = sync_page_range(inode, mapping, pos, ret);
2295 EXPORT_SYMBOL(generic_file_aio_write);
2297 ssize_t generic_file_write(struct file *file, const char __user *buf,
2298 size_t count, loff_t *ppos)
2300 struct address_space *mapping = file->f_mapping;
2301 struct inode *inode = mapping->host;
2303 struct iovec local_iov = { .iov_base = (void __user *)buf,
2306 mutex_lock(&inode->i_mutex);
2307 ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
2308 mutex_unlock(&inode->i_mutex);
2310 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2313 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2319 EXPORT_SYMBOL(generic_file_write);
2321 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
2322 unsigned long nr_segs, loff_t *ppos)
2327 init_sync_kiocb(&kiocb, filp);
2328 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
2329 if (-EIOCBQUEUED == ret)
2330 ret = wait_on_sync_kiocb(&kiocb);
2333 EXPORT_SYMBOL(generic_file_readv);
2335 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
2336 unsigned long nr_segs, loff_t *ppos)
2338 struct address_space *mapping = file->f_mapping;
2339 struct inode *inode = mapping->host;
2342 mutex_lock(&inode->i_mutex);
2343 ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
2344 mutex_unlock(&inode->i_mutex);
2346 if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2349 err = sync_page_range(inode, mapping, *ppos - ret, ret);
2355 EXPORT_SYMBOL(generic_file_writev);
2358 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
2359 * went wrong during pagecache shootdown.
2362 generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2363 loff_t offset, unsigned long nr_segs)
2365 struct file *file = iocb->ki_filp;
2366 struct address_space *mapping = file->f_mapping;
2368 size_t write_len = 0;
2371 * If it's a write, unmap all mmappings of the file up-front. This
2372 * will cause any pte dirty bits to be propagated into the pageframes
2373 * for the subsequent filemap_write_and_wait().
2376 write_len = iov_length(iov, nr_segs);
2377 if (mapping_mapped(mapping))
2378 unmap_mapping_range(mapping, offset, write_len, 0);
2381 retval = filemap_write_and_wait(mapping);
2383 retval = mapping->a_ops->direct_IO(rw, iocb, iov,
2385 if (rw == WRITE && mapping->nrpages) {
2386 pgoff_t end = (offset + write_len - 1)
2387 >> PAGE_CACHE_SHIFT;
2388 int err = invalidate_inode_pages2_range(mapping,
2389 offset >> PAGE_CACHE_SHIFT, end);