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 write_lock_irq(&mapping
->tree_lock
);
130 __remove_from_page_cache(page
);
131 write_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_cap_writeback_dirty(mapping
))
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_cap_writeback_dirty(mapping
) || !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_cap_writeback_dirty(mapping
) || !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
);
339 int filemap_write_and_wait_range(struct address_space
*mapping
,
340 loff_t lstart
, loff_t lend
)
344 if (mapping
->nrpages
) {
345 retval
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
348 retval
= wait_on_page_writeback_range(mapping
,
349 lstart
>> PAGE_CACHE_SHIFT
,
350 lend
>> PAGE_CACHE_SHIFT
);
356 * This function is used to add newly allocated pagecache pages:
357 * the page is new, so we can just run SetPageLocked() against it.
358 * The other page state flags were set by rmqueue().
360 * This function does not add the page to the LRU. The caller must do that.
362 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
363 pgoff_t offset
, int gfp_mask
)
365 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
368 write_lock_irq(&mapping
->tree_lock
);
369 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
371 page_cache_get(page
);
373 page
->mapping
= mapping
;
374 page
->index
= offset
;
378 write_unlock_irq(&mapping
->tree_lock
);
379 radix_tree_preload_end();
384 EXPORT_SYMBOL(add_to_page_cache
);
386 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
387 pgoff_t offset
, int gfp_mask
)
389 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
396 * In order to wait for pages to become available there must be
397 * waitqueues associated with pages. By using a hash table of
398 * waitqueues where the bucket discipline is to maintain all
399 * waiters on the same queue and wake all when any of the pages
400 * become available, and for the woken contexts to check to be
401 * sure the appropriate page became available, this saves space
402 * at a cost of "thundering herd" phenomena during rare hash
405 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
407 const struct zone
*zone
= page_zone(page
);
409 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
412 static inline void wake_up_page(struct page
*page
, int bit
)
414 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
417 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
419 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
421 if (test_bit(bit_nr
, &page
->flags
))
422 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
423 TASK_UNINTERRUPTIBLE
);
425 EXPORT_SYMBOL(wait_on_page_bit
);
428 * unlock_page() - unlock a locked page
432 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
433 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
434 * mechananism between PageLocked pages and PageWriteback pages is shared.
435 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
437 * The first mb is necessary to safely close the critical section opened by the
438 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
439 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
440 * parallel wait_on_page_locked()).
442 void fastcall
unlock_page(struct page
*page
)
444 smp_mb__before_clear_bit();
445 if (!TestClearPageLocked(page
))
447 smp_mb__after_clear_bit();
448 wake_up_page(page
, PG_locked
);
450 EXPORT_SYMBOL(unlock_page
);
453 * End writeback against a page.
455 void end_page_writeback(struct page
*page
)
457 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
458 if (!test_clear_page_writeback(page
))
461 smp_mb__after_clear_bit();
462 wake_up_page(page
, PG_writeback
);
464 EXPORT_SYMBOL(end_page_writeback
);
467 * Get a lock on the page, assuming we need to sleep to get it.
469 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
470 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
471 * chances are that on the second loop, the block layer's plug list is empty,
472 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
474 void fastcall
__lock_page(struct page
*page
)
476 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
478 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
479 TASK_UNINTERRUPTIBLE
);
481 EXPORT_SYMBOL(__lock_page
);
484 * a rather lightweight function, finding and getting a reference to a
485 * hashed page atomically.
487 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
491 read_lock_irq(&mapping
->tree_lock
);
492 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
494 page_cache_get(page
);
495 read_unlock_irq(&mapping
->tree_lock
);
499 EXPORT_SYMBOL(find_get_page
);
502 * Same as above, but trylock it instead of incrementing the count.
504 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
508 read_lock_irq(&mapping
->tree_lock
);
509 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
510 if (page
&& TestSetPageLocked(page
))
512 read_unlock_irq(&mapping
->tree_lock
);
516 EXPORT_SYMBOL(find_trylock_page
);
519 * find_lock_page - locate, pin and lock a pagecache page
521 * @mapping - the address_space to search
522 * @offset - the page index
524 * Locates the desired pagecache page, locks it, increments its reference
525 * count and returns its address.
527 * Returns zero if the page was not present. find_lock_page() may sleep.
529 struct page
*find_lock_page(struct address_space
*mapping
,
530 unsigned long offset
)
534 read_lock_irq(&mapping
->tree_lock
);
536 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
538 page_cache_get(page
);
539 if (TestSetPageLocked(page
)) {
540 read_unlock_irq(&mapping
->tree_lock
);
542 read_lock_irq(&mapping
->tree_lock
);
544 /* Has the page been truncated while we slept? */
545 if (page
->mapping
!= mapping
|| page
->index
!= offset
) {
547 page_cache_release(page
);
552 read_unlock_irq(&mapping
->tree_lock
);
556 EXPORT_SYMBOL(find_lock_page
);
559 * find_or_create_page - locate or add a pagecache page
561 * @mapping - the page's address_space
562 * @index - the page's index into the mapping
563 * @gfp_mask - page allocation mode
565 * Locates a page in the pagecache. If the page is not present, a new page
566 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
567 * LRU list. The returned page is locked and has its reference count
570 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
573 * find_or_create_page() returns the desired page's address, or zero on
576 struct page
*find_or_create_page(struct address_space
*mapping
,
577 unsigned long index
, unsigned int gfp_mask
)
579 struct page
*page
, *cached_page
= NULL
;
582 page
= find_lock_page(mapping
, index
);
585 cached_page
= alloc_page(gfp_mask
);
589 err
= add_to_page_cache_lru(cached_page
, mapping
,
594 } else if (err
== -EEXIST
)
598 page_cache_release(cached_page
);
602 EXPORT_SYMBOL(find_or_create_page
);
605 * find_get_pages - gang pagecache lookup
606 * @mapping: The address_space to search
607 * @start: The starting page index
608 * @nr_pages: The maximum number of pages
609 * @pages: Where the resulting pages are placed
611 * find_get_pages() will search for and return a group of up to
612 * @nr_pages pages in the mapping. The pages are placed at @pages.
613 * find_get_pages() takes a reference against the returned pages.
615 * The search returns a group of mapping-contiguous pages with ascending
616 * indexes. There may be holes in the indices due to not-present pages.
618 * find_get_pages() returns the number of pages which were found.
620 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
621 unsigned int nr_pages
, struct page
**pages
)
626 read_lock_irq(&mapping
->tree_lock
);
627 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
628 (void **)pages
, start
, nr_pages
);
629 for (i
= 0; i
< ret
; i
++)
630 page_cache_get(pages
[i
]);
631 read_unlock_irq(&mapping
->tree_lock
);
636 * Like find_get_pages, except we only return pages which are tagged with
637 * `tag'. We update *index to index the next page for the traversal.
639 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
640 int tag
, unsigned int nr_pages
, struct page
**pages
)
645 read_lock_irq(&mapping
->tree_lock
);
646 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
647 (void **)pages
, *index
, nr_pages
, tag
);
648 for (i
= 0; i
< ret
; i
++)
649 page_cache_get(pages
[i
]);
651 *index
= pages
[ret
- 1]->index
+ 1;
652 read_unlock_irq(&mapping
->tree_lock
);
657 * Same as grab_cache_page, but do not wait if the page is unavailable.
658 * This is intended for speculative data generators, where the data can
659 * be regenerated if the page couldn't be grabbed. This routine should
660 * be safe to call while holding the lock for another page.
662 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
663 * and deadlock against the caller's locked page.
666 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
668 struct page
*page
= find_get_page(mapping
, index
);
669 unsigned int gfp_mask
;
672 if (!TestSetPageLocked(page
))
674 page_cache_release(page
);
677 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
678 page
= alloc_pages(gfp_mask
, 0);
679 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
680 page_cache_release(page
);
686 EXPORT_SYMBOL(grab_cache_page_nowait
);
689 * This is a generic file read routine, and uses the
690 * mapping->a_ops->readpage() function for the actual low-level
693 * This is really ugly. But the goto's actually try to clarify some
694 * of the logic when it comes to error handling etc.
696 * Note the struct file* is only passed for the use of readpage. It may be
699 void do_generic_mapping_read(struct address_space
*mapping
,
700 struct file_ra_state
*_ra
,
703 read_descriptor_t
*desc
,
706 struct inode
*inode
= mapping
->host
;
708 unsigned long end_index
;
709 unsigned long offset
;
710 unsigned long last_index
;
711 unsigned long next_index
;
712 unsigned long prev_index
;
714 struct page
*cached_page
;
716 struct file_ra_state ra
= *_ra
;
719 index
= *ppos
>> PAGE_CACHE_SHIFT
;
721 prev_index
= ra
.prev_page
;
722 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
723 offset
= *ppos
& ~PAGE_CACHE_MASK
;
725 isize
= i_size_read(inode
);
729 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
732 unsigned long nr
, ret
;
734 /* nr is the maximum number of bytes to copy from this page */
735 nr
= PAGE_CACHE_SIZE
;
736 if (index
>= end_index
) {
737 if (index
> end_index
)
739 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
747 if (index
== next_index
)
748 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
749 index
, last_index
- index
);
752 page
= find_get_page(mapping
, index
);
753 if (unlikely(page
== NULL
)) {
754 handle_ra_miss(mapping
, &ra
, index
);
757 if (!PageUptodate(page
))
758 goto page_not_up_to_date
;
761 /* If users can be writing to this page using arbitrary
762 * virtual addresses, take care about potential aliasing
763 * before reading the page on the kernel side.
765 if (mapping_writably_mapped(mapping
))
766 flush_dcache_page(page
);
769 * When (part of) the same page is read multiple times
770 * in succession, only mark it as accessed the first time.
772 if (prev_index
!= index
)
773 mark_page_accessed(page
);
777 * Ok, we have the page, and it's up-to-date, so
778 * now we can copy it to user space...
780 * The actor routine returns how many bytes were actually used..
781 * NOTE! This may not be the same as how much of a user buffer
782 * we filled up (we may be padding etc), so we can only update
783 * "pos" here (the actor routine has to update the user buffer
784 * pointers and the remaining count).
786 ret
= actor(desc
, page
, offset
, nr
);
788 index
+= offset
>> PAGE_CACHE_SHIFT
;
789 offset
&= ~PAGE_CACHE_MASK
;
791 page_cache_release(page
);
792 if (ret
== nr
&& desc
->count
)
797 /* Get exclusive access to the page ... */
800 /* Did it get unhashed before we got the lock? */
801 if (!page
->mapping
) {
803 page_cache_release(page
);
807 /* Did somebody else fill it already? */
808 if (PageUptodate(page
)) {
814 /* Start the actual read. The read will unlock the page. */
815 error
= mapping
->a_ops
->readpage(filp
, page
);
820 if (!PageUptodate(page
)) {
822 if (!PageUptodate(page
)) {
823 if (page
->mapping
== NULL
) {
825 * invalidate_inode_pages got it
828 page_cache_release(page
);
839 * i_size must be checked after we have done ->readpage.
841 * Checking i_size after the readpage allows us to calculate
842 * the correct value for "nr", which means the zero-filled
843 * part of the page is not copied back to userspace (unless
844 * another truncate extends the file - this is desired though).
846 isize
= i_size_read(inode
);
847 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
848 if (unlikely(!isize
|| index
> end_index
)) {
849 page_cache_release(page
);
853 /* nr is the maximum number of bytes to copy from this page */
854 nr
= PAGE_CACHE_SIZE
;
855 if (index
== end_index
) {
856 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
858 page_cache_release(page
);
866 /* UHHUH! A synchronous read error occurred. Report it */
868 page_cache_release(page
);
873 * Ok, it wasn't cached, so we need to create a new
877 cached_page
= page_cache_alloc_cold(mapping
);
879 desc
->error
= -ENOMEM
;
883 error
= add_to_page_cache_lru(cached_page
, mapping
,
886 if (error
== -EEXIST
)
899 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
901 page_cache_release(cached_page
);
906 EXPORT_SYMBOL(do_generic_mapping_read
);
908 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
909 unsigned long offset
, unsigned long size
)
912 unsigned long left
, count
= desc
->count
;
918 * Faults on the destination of a read are common, so do it before
921 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
922 kaddr
= kmap_atomic(page
, KM_USER0
);
923 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
924 kaddr
+ offset
, size
);
925 kunmap_atomic(kaddr
, KM_USER0
);
930 /* Do it the slow way */
932 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
937 desc
->error
= -EFAULT
;
940 desc
->count
= count
- size
;
941 desc
->written
+= size
;
942 desc
->arg
.buf
+= size
;
947 * This is the "read()" routine for all filesystems
948 * that can use the page cache directly.
951 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
952 unsigned long nr_segs
, loff_t
*ppos
)
954 struct file
*filp
= iocb
->ki_filp
;
960 for (seg
= 0; seg
< nr_segs
; seg
++) {
961 const struct iovec
*iv
= &iov
[seg
];
964 * If any segment has a negative length, or the cumulative
965 * length ever wraps negative then return -EINVAL.
967 count
+= iv
->iov_len
;
968 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
970 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
975 count
-= iv
->iov_len
; /* This segment is no good */
979 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
980 if (filp
->f_flags
& O_DIRECT
) {
981 loff_t pos
= *ppos
, size
;
982 struct address_space
*mapping
;
985 mapping
= filp
->f_mapping
;
986 inode
= mapping
->host
;
989 goto out
; /* skip atime */
990 size
= i_size_read(inode
);
992 retval
= generic_file_direct_IO(READ
, iocb
,
994 if (retval
>= 0 && !is_sync_kiocb(iocb
))
995 retval
= -EIOCBQUEUED
;
997 *ppos
= pos
+ retval
;
1005 for (seg
= 0; seg
< nr_segs
; seg
++) {
1006 read_descriptor_t desc
;
1009 desc
.arg
.buf
= iov
[seg
].iov_base
;
1010 desc
.count
= iov
[seg
].iov_len
;
1011 if (desc
.count
== 0)
1014 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1015 retval
+= desc
.written
;
1017 retval
= desc
.error
;
1026 EXPORT_SYMBOL(__generic_file_aio_read
);
1029 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1031 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1033 BUG_ON(iocb
->ki_pos
!= pos
);
1034 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1037 EXPORT_SYMBOL(generic_file_aio_read
);
1040 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1042 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1046 init_sync_kiocb(&kiocb
, filp
);
1047 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1048 if (-EIOCBQUEUED
== ret
)
1049 ret
= wait_on_sync_kiocb(&kiocb
);
1053 EXPORT_SYMBOL(generic_file_read
);
1055 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1058 unsigned long count
= desc
->count
;
1059 struct file
*file
= desc
->arg
.data
;
1064 written
= file
->f_op
->sendpage(file
, page
, offset
,
1065 size
, &file
->f_pos
, size
<count
);
1067 desc
->error
= written
;
1070 desc
->count
= count
- written
;
1071 desc
->written
+= written
;
1075 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1076 size_t count
, read_actor_t actor
, void *target
)
1078 read_descriptor_t desc
;
1085 desc
.arg
.data
= target
;
1088 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1090 return desc
.written
;
1094 EXPORT_SYMBOL(generic_file_sendfile
);
1097 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1098 unsigned long index
, unsigned long nr
)
1100 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1103 force_page_cache_readahead(mapping
, filp
, index
,
1104 max_sane_readahead(nr
));
1108 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1116 if (file
->f_mode
& FMODE_READ
) {
1117 struct address_space
*mapping
= file
->f_mapping
;
1118 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1119 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1120 unsigned long len
= end
- start
+ 1;
1121 ret
= do_readahead(mapping
, file
, start
, len
);
1130 * This adds the requested page to the page cache if it isn't already there,
1131 * and schedules an I/O to read in its contents from disk.
1133 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1134 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1136 struct address_space
*mapping
= file
->f_mapping
;
1140 page
= page_cache_alloc_cold(mapping
);
1144 error
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1146 error
= mapping
->a_ops
->readpage(file
, page
);
1147 page_cache_release(page
);
1152 * We arrive here in the unlikely event that someone
1153 * raced with us and added our page to the cache first
1154 * or we are out of memory for radix-tree nodes.
1156 page_cache_release(page
);
1157 return error
== -EEXIST
? 0 : error
;
1160 #define MMAP_LOTSAMISS (100)
1163 * filemap_nopage() is invoked via the vma operations vector for a
1164 * mapped memory region to read in file data during a page fault.
1166 * The goto's are kind of ugly, but this streamlines the normal case of having
1167 * it in the page cache, and handles the special cases reasonably without
1168 * having a lot of duplicated code.
1170 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1171 unsigned long address
, int *type
)
1174 struct file
*file
= area
->vm_file
;
1175 struct address_space
*mapping
= file
->f_mapping
;
1176 struct file_ra_state
*ra
= &file
->f_ra
;
1177 struct inode
*inode
= mapping
->host
;
1179 unsigned long size
, pgoff
;
1180 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1182 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1185 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1187 goto outside_data_content
;
1189 /* If we don't want any read-ahead, don't bother */
1190 if (VM_RandomReadHint(area
))
1191 goto no_cached_page
;
1194 * The readahead code wants to be told about each and every page
1195 * so it can build and shrink its windows appropriately
1197 * For sequential accesses, we use the generic readahead logic.
1199 if (VM_SequentialReadHint(area
))
1200 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1203 * Do we have something in the page cache already?
1206 page
= find_get_page(mapping
, pgoff
);
1208 unsigned long ra_pages
;
1210 if (VM_SequentialReadHint(area
)) {
1211 handle_ra_miss(mapping
, ra
, pgoff
);
1212 goto no_cached_page
;
1217 * Do we miss much more than hit in this file? If so,
1218 * stop bothering with read-ahead. It will only hurt.
1220 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1221 goto no_cached_page
;
1224 * To keep the pgmajfault counter straight, we need to
1225 * check did_readaround, as this is an inner loop.
1227 if (!did_readaround
) {
1228 majmin
= VM_FAULT_MAJOR
;
1229 inc_page_state(pgmajfault
);
1232 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1236 if (pgoff
> ra_pages
/ 2)
1237 start
= pgoff
- ra_pages
/ 2;
1238 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1240 page
= find_get_page(mapping
, pgoff
);
1242 goto no_cached_page
;
1245 if (!did_readaround
)
1249 * Ok, found a page in the page cache, now we need to check
1250 * that it's up-to-date.
1252 if (!PageUptodate(page
))
1253 goto page_not_uptodate
;
1257 * Found the page and have a reference on it.
1259 mark_page_accessed(page
);
1264 outside_data_content
:
1266 * An external ptracer can access pages that normally aren't
1269 if (area
->vm_mm
== current
->mm
)
1271 /* Fall through to the non-read-ahead case */
1274 * We're only likely to ever get here if MADV_RANDOM is in
1277 error
= page_cache_read(file
, pgoff
);
1281 * The page we want has now been added to the page cache.
1282 * In the unlikely event that someone removed it in the
1283 * meantime, we'll just come back here and read it again.
1289 * An error return from page_cache_read can result if the
1290 * system is low on memory, or a problem occurs while trying
1293 if (error
== -ENOMEM
)
1298 if (!did_readaround
) {
1299 majmin
= VM_FAULT_MAJOR
;
1300 inc_page_state(pgmajfault
);
1304 /* Did it get unhashed while we waited for it? */
1305 if (!page
->mapping
) {
1307 page_cache_release(page
);
1311 /* Did somebody else get it up-to-date? */
1312 if (PageUptodate(page
)) {
1317 if (!mapping
->a_ops
->readpage(file
, page
)) {
1318 wait_on_page_locked(page
);
1319 if (PageUptodate(page
))
1324 * Umm, take care of errors if the page isn't up-to-date.
1325 * Try to re-read it _once_. We do this synchronously,
1326 * because there really aren't any performance issues here
1327 * and we need to check for errors.
1331 /* Somebody truncated the page on us? */
1332 if (!page
->mapping
) {
1334 page_cache_release(page
);
1338 /* Somebody else successfully read it in? */
1339 if (PageUptodate(page
)) {
1343 ClearPageError(page
);
1344 if (!mapping
->a_ops
->readpage(file
, page
)) {
1345 wait_on_page_locked(page
);
1346 if (PageUptodate(page
))
1351 * Things didn't work out. Return zero to tell the
1352 * mm layer so, possibly freeing the page cache page first.
1354 page_cache_release(page
);
1358 EXPORT_SYMBOL(filemap_nopage
);
1360 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1363 struct address_space
*mapping
= file
->f_mapping
;
1368 * Do we have something in the page cache already?
1371 page
= find_get_page(mapping
, pgoff
);
1375 goto no_cached_page
;
1379 * Ok, found a page in the page cache, now we need to check
1380 * that it's up-to-date.
1382 if (!PageUptodate(page
)) {
1384 page_cache_release(page
);
1387 goto page_not_uptodate
;
1392 * Found the page and have a reference on it.
1394 mark_page_accessed(page
);
1398 error
= page_cache_read(file
, pgoff
);
1401 * The page we want has now been added to the page cache.
1402 * In the unlikely event that someone removed it in the
1403 * meantime, we'll just come back here and read it again.
1409 * An error return from page_cache_read can result if the
1410 * system is low on memory, or a problem occurs while trying
1418 /* Did it get unhashed while we waited for it? */
1419 if (!page
->mapping
) {
1424 /* Did somebody else get it up-to-date? */
1425 if (PageUptodate(page
)) {
1430 if (!mapping
->a_ops
->readpage(file
, page
)) {
1431 wait_on_page_locked(page
);
1432 if (PageUptodate(page
))
1437 * Umm, take care of errors if the page isn't up-to-date.
1438 * Try to re-read it _once_. We do this synchronously,
1439 * because there really aren't any performance issues here
1440 * and we need to check for errors.
1444 /* Somebody truncated the page on us? */
1445 if (!page
->mapping
) {
1449 /* Somebody else successfully read it in? */
1450 if (PageUptodate(page
)) {
1455 ClearPageError(page
);
1456 if (!mapping
->a_ops
->readpage(file
, page
)) {
1457 wait_on_page_locked(page
);
1458 if (PageUptodate(page
))
1463 * Things didn't work out. Return zero to tell the
1464 * mm layer so, possibly freeing the page cache page first.
1467 page_cache_release(page
);
1472 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1473 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1476 struct file
*file
= vma
->vm_file
;
1477 struct address_space
*mapping
= file
->f_mapping
;
1478 struct inode
*inode
= mapping
->host
;
1480 struct mm_struct
*mm
= vma
->vm_mm
;
1485 force_page_cache_readahead(mapping
, vma
->vm_file
,
1486 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1489 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1490 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1493 page
= filemap_getpage(file
, pgoff
, nonblock
);
1494 if (!page
&& !nonblock
)
1497 err
= install_page(mm
, vma
, addr
, page
, prot
);
1499 page_cache_release(page
);
1503 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1517 struct vm_operations_struct generic_file_vm_ops
= {
1518 .nopage
= filemap_nopage
,
1519 .populate
= filemap_populate
,
1522 /* This is used for a general mmap of a disk file */
1524 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1526 struct address_space
*mapping
= file
->f_mapping
;
1528 if (!mapping
->a_ops
->readpage
)
1530 file_accessed(file
);
1531 vma
->vm_ops
= &generic_file_vm_ops
;
1534 EXPORT_SYMBOL(filemap_populate
);
1537 * This is for filesystems which do not implement ->writepage.
1539 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1541 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1543 return generic_file_mmap(file
, vma
);
1546 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1550 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1554 #endif /* CONFIG_MMU */
1556 EXPORT_SYMBOL(generic_file_mmap
);
1557 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1559 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1560 unsigned long index
,
1561 int (*filler
)(void *,struct page
*),
1564 struct page
*page
, *cached_page
= NULL
;
1567 page
= find_get_page(mapping
, index
);
1570 cached_page
= page_cache_alloc_cold(mapping
);
1572 return ERR_PTR(-ENOMEM
);
1574 err
= add_to_page_cache_lru(cached_page
, mapping
,
1579 /* Presumably ENOMEM for radix tree node */
1580 page_cache_release(cached_page
);
1581 return ERR_PTR(err
);
1585 err
= filler(data
, page
);
1587 page_cache_release(page
);
1588 page
= ERR_PTR(err
);
1592 page_cache_release(cached_page
);
1597 * Read into the page cache. If a page already exists,
1598 * and PageUptodate() is not set, try to fill the page.
1600 struct page
*read_cache_page(struct address_space
*mapping
,
1601 unsigned long index
,
1602 int (*filler
)(void *,struct page
*),
1609 page
= __read_cache_page(mapping
, index
, filler
, data
);
1612 mark_page_accessed(page
);
1613 if (PageUptodate(page
))
1617 if (!page
->mapping
) {
1619 page_cache_release(page
);
1622 if (PageUptodate(page
)) {
1626 err
= filler(data
, page
);
1628 page_cache_release(page
);
1629 page
= ERR_PTR(err
);
1635 EXPORT_SYMBOL(read_cache_page
);
1638 * If the page was newly created, increment its refcount and add it to the
1639 * caller's lru-buffering pagevec. This function is specifically for
1640 * generic_file_write().
1642 static inline struct page
*
1643 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1644 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1649 page
= find_lock_page(mapping
, index
);
1651 if (!*cached_page
) {
1652 *cached_page
= page_cache_alloc(mapping
);
1656 err
= add_to_page_cache(*cached_page
, mapping
,
1661 page
= *cached_page
;
1662 page_cache_get(page
);
1663 if (!pagevec_add(lru_pvec
, page
))
1664 __pagevec_lru_add(lru_pvec
);
1665 *cached_page
= NULL
;
1672 * The logic we want is
1674 * if suid or (sgid and xgrp)
1677 int remove_suid(struct dentry
*dentry
)
1679 mode_t mode
= dentry
->d_inode
->i_mode
;
1683 /* suid always must be killed */
1684 if (unlikely(mode
& S_ISUID
))
1685 kill
= ATTR_KILL_SUID
;
1688 * sgid without any exec bits is just a mandatory locking mark; leave
1689 * it alone. If some exec bits are set, it's a real sgid; kill it.
1691 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1692 kill
|= ATTR_KILL_SGID
;
1694 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1695 struct iattr newattrs
;
1697 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1698 result
= notify_change(dentry
, &newattrs
);
1702 EXPORT_SYMBOL(remove_suid
);
1705 * Copy as much as we can into the page and return the number of bytes which
1706 * were sucessfully copied. If a fault is encountered then clear the page
1707 * out to (offset+bytes) and return the number of bytes which were copied.
1709 static inline size_t
1710 filemap_copy_from_user(struct page
*page
, unsigned long offset
,
1711 const char __user
*buf
, unsigned bytes
)
1716 kaddr
= kmap_atomic(page
, KM_USER0
);
1717 left
= __copy_from_user_inatomic(kaddr
+ offset
, buf
, bytes
);
1718 kunmap_atomic(kaddr
, KM_USER0
);
1721 /* Do it the slow way */
1723 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
1726 return bytes
- left
;
1730 __filemap_copy_from_user_iovec(char *vaddr
,
1731 const struct iovec
*iov
, size_t base
, size_t bytes
)
1733 size_t copied
= 0, left
= 0;
1736 char __user
*buf
= iov
->iov_base
+ base
;
1737 int copy
= min(bytes
, iov
->iov_len
- base
);
1740 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1746 if (unlikely(left
)) {
1747 /* zero the rest of the target like __copy_from_user */
1749 memset(vaddr
, 0, bytes
);
1753 return copied
- left
;
1757 * This has the same sideeffects and return value as filemap_copy_from_user().
1758 * The difference is that on a fault we need to memset the remainder of the
1759 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1760 * single-segment behaviour.
1762 static inline size_t
1763 filemap_copy_from_user_iovec(struct page
*page
, unsigned long offset
,
1764 const struct iovec
*iov
, size_t base
, size_t bytes
)
1769 kaddr
= kmap_atomic(page
, KM_USER0
);
1770 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1772 kunmap_atomic(kaddr
, KM_USER0
);
1773 if (copied
!= bytes
) {
1775 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1783 filemap_set_next_iovec(const struct iovec
**iovp
, size_t *basep
, size_t bytes
)
1785 const struct iovec
*iov
= *iovp
;
1786 size_t base
= *basep
;
1789 int copy
= min(bytes
, iov
->iov_len
- base
);
1793 if (iov
->iov_len
== base
) {
1803 * Performs necessary checks before doing a write
1805 * Can adjust writing position aor amount of bytes to write.
1806 * Returns appropriate error code that caller should return or
1807 * zero in case that write should be allowed.
1809 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1811 struct inode
*inode
= file
->f_mapping
->host
;
1812 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1814 if (unlikely(*pos
< 0))
1817 if (unlikely(file
->f_error
)) {
1818 int err
= file
->f_error
;
1824 /* FIXME: this is for backwards compatibility with 2.4 */
1825 if (file
->f_flags
& O_APPEND
)
1826 *pos
= i_size_read(inode
);
1828 if (limit
!= RLIM_INFINITY
) {
1829 if (*pos
>= limit
) {
1830 send_sig(SIGXFSZ
, current
, 0);
1833 if (*count
> limit
- (typeof(limit
))*pos
) {
1834 *count
= limit
- (typeof(limit
))*pos
;
1842 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1843 !(file
->f_flags
& O_LARGEFILE
))) {
1844 if (*pos
>= MAX_NON_LFS
) {
1845 send_sig(SIGXFSZ
, current
, 0);
1848 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1849 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1854 * Are we about to exceed the fs block limit ?
1856 * If we have written data it becomes a short write. If we have
1857 * exceeded without writing data we send a signal and return EFBIG.
1858 * Linus frestrict idea will clean these up nicely..
1860 if (likely(!isblk
)) {
1861 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1862 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1863 send_sig(SIGXFSZ
, current
, 0);
1866 /* zero-length writes at ->s_maxbytes are OK */
1869 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1870 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1873 if (bdev_read_only(I_BDEV(inode
)))
1875 isize
= i_size_read(inode
);
1876 if (*pos
>= isize
) {
1877 if (*count
|| *pos
> isize
)
1881 if (*pos
+ *count
> isize
)
1882 *count
= isize
- *pos
;
1886 EXPORT_SYMBOL(generic_write_checks
);
1889 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1890 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1891 size_t count
, size_t ocount
)
1893 struct file
*file
= iocb
->ki_filp
;
1894 struct address_space
*mapping
= file
->f_mapping
;
1895 struct inode
*inode
= mapping
->host
;
1898 if (count
!= ocount
)
1899 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1901 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1903 loff_t end
= pos
+ written
;
1904 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1905 i_size_write(inode
, end
);
1906 mark_inode_dirty(inode
);
1912 * Sync the fs metadata but not the minor inode changes and
1913 * of course not the data as we did direct DMA for the IO.
1914 * i_sem is held, which protects generic_osync_inode() from
1917 if (written
>= 0 && file
->f_flags
& O_SYNC
)
1918 generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1919 if (written
== count
&& !is_sync_kiocb(iocb
))
1920 written
= -EIOCBQUEUED
;
1923 EXPORT_SYMBOL(generic_file_direct_write
);
1926 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1927 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1928 size_t count
, ssize_t written
)
1930 struct file
*file
= iocb
->ki_filp
;
1931 struct address_space
* mapping
= file
->f_mapping
;
1932 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1933 struct inode
*inode
= mapping
->host
;
1936 struct page
*cached_page
= NULL
;
1938 struct pagevec lru_pvec
;
1939 const struct iovec
*cur_iov
= iov
; /* current iovec */
1940 size_t iov_base
= 0; /* offset in the current iovec */
1943 pagevec_init(&lru_pvec
, 0);
1946 * handle partial DIO write. Adjust cur_iov if needed.
1948 if (likely(nr_segs
== 1))
1949 buf
= iov
->iov_base
+ written
;
1951 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1952 buf
= iov
->iov_base
+ iov_base
;
1956 unsigned long index
;
1957 unsigned long offset
;
1960 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1961 index
= pos
>> PAGE_CACHE_SHIFT
;
1962 bytes
= PAGE_CACHE_SIZE
- offset
;
1967 * Bring in the user page that we will copy from _first_.
1968 * Otherwise there's a nasty deadlock on copying from the
1969 * same page as we're writing to, without it being marked
1972 fault_in_pages_readable(buf
, bytes
);
1974 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1980 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1981 if (unlikely(status
)) {
1982 loff_t isize
= i_size_read(inode
);
1984 * prepare_write() may have instantiated a few blocks
1985 * outside i_size. Trim these off again.
1988 page_cache_release(page
);
1989 if (pos
+ bytes
> isize
)
1990 vmtruncate(inode
, isize
);
1993 if (likely(nr_segs
== 1))
1994 copied
= filemap_copy_from_user(page
, offset
,
1997 copied
= filemap_copy_from_user_iovec(page
, offset
,
1998 cur_iov
, iov_base
, bytes
);
1999 flush_dcache_page(page
);
2000 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
2001 if (likely(copied
> 0)) {
2010 if (unlikely(nr_segs
> 1))
2011 filemap_set_next_iovec(&cur_iov
,
2015 if (unlikely(copied
!= bytes
))
2019 mark_page_accessed(page
);
2020 page_cache_release(page
);
2023 balance_dirty_pages_ratelimited(mapping
);
2029 page_cache_release(cached_page
);
2032 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2034 if (likely(status
>= 0)) {
2035 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2036 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2037 status
= generic_osync_inode(inode
, mapping
,
2038 OSYNC_METADATA
|OSYNC_DATA
);
2043 * If we get here for O_DIRECT writes then we must have fallen through
2044 * to buffered writes (block instantiation inside i_size). So we sync
2045 * the file data here, to try to honour O_DIRECT expectations.
2047 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2048 status
= filemap_write_and_wait(mapping
);
2050 pagevec_lru_add(&lru_pvec
);
2051 return written
? written
: status
;
2053 EXPORT_SYMBOL(generic_file_buffered_write
);
2056 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2057 unsigned long nr_segs
, loff_t
*ppos
)
2059 struct file
*file
= iocb
->ki_filp
;
2060 struct address_space
* mapping
= file
->f_mapping
;
2061 size_t ocount
; /* original count */
2062 size_t count
; /* after file limit checks */
2063 struct inode
*inode
= mapping
->host
;
2070 for (seg
= 0; seg
< nr_segs
; seg
++) {
2071 const struct iovec
*iv
= &iov
[seg
];
2074 * If any segment has a negative length, or the cumulative
2075 * length ever wraps negative then return -EINVAL.
2077 ocount
+= iv
->iov_len
;
2078 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2080 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2085 ocount
-= iv
->iov_len
; /* This segment is no good */
2092 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2094 /* We can write back this queue in page reclaim */
2095 current
->backing_dev_info
= mapping
->backing_dev_info
;
2098 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2105 err
= remove_suid(file
->f_dentry
);
2109 inode_update_time(inode
, 1);
2111 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2112 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2113 written
= generic_file_direct_write(iocb
, iov
,
2114 &nr_segs
, pos
, ppos
, count
, ocount
);
2115 if (written
< 0 || written
== count
)
2118 * direct-io write to a hole: fall through to buffered I/O
2119 * for completing the rest of the request.
2125 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2126 pos
, ppos
, count
, written
);
2128 current
->backing_dev_info
= NULL
;
2129 return written
? written
: err
;
2131 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2134 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2135 unsigned long nr_segs
, loff_t
*ppos
)
2137 struct file
*file
= iocb
->ki_filp
;
2138 struct address_space
*mapping
= file
->f_mapping
;
2139 struct inode
*inode
= mapping
->host
;
2143 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2145 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2148 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2156 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2157 unsigned long nr_segs
, loff_t
*ppos
)
2162 init_sync_kiocb(&kiocb
, file
);
2163 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2164 if (ret
== -EIOCBQUEUED
)
2165 ret
= wait_on_sync_kiocb(&kiocb
);
2170 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2171 unsigned long nr_segs
, loff_t
*ppos
)
2176 init_sync_kiocb(&kiocb
, file
);
2177 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2178 if (-EIOCBQUEUED
== ret
)
2179 ret
= wait_on_sync_kiocb(&kiocb
);
2182 EXPORT_SYMBOL(generic_file_write_nolock
);
2184 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2185 size_t count
, loff_t pos
)
2187 struct file
*file
= iocb
->ki_filp
;
2188 struct address_space
*mapping
= file
->f_mapping
;
2189 struct inode
*inode
= mapping
->host
;
2191 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2194 BUG_ON(iocb
->ki_pos
!= pos
);
2196 down(&inode
->i_sem
);
2197 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2201 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2204 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2210 EXPORT_SYMBOL(generic_file_aio_write
);
2212 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2213 size_t count
, loff_t
*ppos
)
2215 struct address_space
*mapping
= file
->f_mapping
;
2216 struct inode
*inode
= mapping
->host
;
2218 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2221 down(&inode
->i_sem
);
2222 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2225 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2228 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2234 EXPORT_SYMBOL(generic_file_write
);
2236 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2237 unsigned long nr_segs
, loff_t
*ppos
)
2242 init_sync_kiocb(&kiocb
, filp
);
2243 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2244 if (-EIOCBQUEUED
== ret
)
2245 ret
= wait_on_sync_kiocb(&kiocb
);
2248 EXPORT_SYMBOL(generic_file_readv
);
2250 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2251 unsigned long nr_segs
, loff_t
*ppos
)
2253 struct address_space
*mapping
= file
->f_mapping
;
2254 struct inode
*inode
= mapping
->host
;
2257 down(&inode
->i_sem
);
2258 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2261 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2264 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2270 EXPORT_SYMBOL(generic_file_writev
);
2273 * Called under i_sem for writes to S_ISREG files. Returns -EIO if something
2274 * went wrong during pagecache shootdown.
2277 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2278 loff_t offset
, unsigned long nr_segs
)
2280 struct file
*file
= iocb
->ki_filp
;
2281 struct address_space
*mapping
= file
->f_mapping
;
2283 size_t write_len
= 0;
2286 * If it's a write, unmap all mmappings of the file up-front. This
2287 * will cause any pte dirty bits to be propagated into the pageframes
2288 * for the subsequent filemap_write_and_wait().
2291 write_len
= iov_length(iov
, nr_segs
);
2292 if (mapping_mapped(mapping
))
2293 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2296 retval
= filemap_write_and_wait(mapping
);
2298 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2300 if (rw
== WRITE
&& mapping
->nrpages
) {
2301 pgoff_t end
= (offset
+ write_len
- 1)
2302 >> PAGE_CACHE_SHIFT
;
2303 int err
= invalidate_inode_pages2_range(mapping
,
2304 offset
>> PAGE_CACHE_SHIFT
, end
);
2311 EXPORT_SYMBOL_GPL(generic_file_direct_IO
);