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>
33 * FIXME: remove all knowledge of the buffer layer from the core VM
35 #include <linux/buffer_head.h> /* for generic_osync_inode */
37 #include <asm/uaccess.h>
41 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
42 loff_t offset
, unsigned long nr_segs
);
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)
61 * ->swap_lock (exclusive_swap_page, others)
62 * ->mapping->tree_lock
65 * ->i_mmap_lock (truncate->unmap_mapping_range)
69 * ->page_table_lock or pte_lock (various, mainly in memory.c)
70 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
73 * ->lock_page (access_process_vm)
79 * ->i_alloc_sem (various)
82 * ->sb_lock (fs/fs-writeback.c)
83 * ->mapping->tree_lock (__sync_single_inode)
86 * ->anon_vma.lock (vma_adjust)
89 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
91 * ->page_table_lock or pte_lock
92 * ->swap_lock (try_to_unmap_one)
93 * ->private_lock (try_to_unmap_one)
94 * ->tree_lock (try_to_unmap_one)
95 * ->zone.lru_lock (follow_page->mark_page_accessed)
96 * ->private_lock (page_remove_rmap->set_page_dirty)
97 * ->tree_lock (page_remove_rmap->set_page_dirty)
98 * ->inode_lock (page_remove_rmap->set_page_dirty)
99 * ->inode_lock (zap_pte_range->set_page_dirty)
100 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
103 * ->dcache_lock (proc_pid_lookup)
107 * Remove a page from the page cache and free it. Caller has to make
108 * sure the page is locked and that nobody else uses it - or that usage
109 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
111 void __remove_from_page_cache(struct page
*page
)
113 struct address_space
*mapping
= page
->mapping
;
115 radix_tree_delete(&mapping
->page_tree
, page
->index
);
116 page
->mapping
= NULL
;
121 void remove_from_page_cache(struct page
*page
)
123 struct address_space
*mapping
= page
->mapping
;
125 BUG_ON(!PageLocked(page
));
127 write_lock_irq(&mapping
->tree_lock
);
128 __remove_from_page_cache(page
);
129 write_unlock_irq(&mapping
->tree_lock
);
132 static int sync_page(void *word
)
134 struct address_space
*mapping
;
137 page
= container_of((unsigned long *)word
, struct page
, flags
);
140 * page_mapping() is being called without PG_locked held.
141 * Some knowledge of the state and use of the page is used to
142 * reduce the requirements down to a memory barrier.
143 * The danger here is of a stale page_mapping() return value
144 * indicating a struct address_space different from the one it's
145 * associated with when it is associated with one.
146 * After smp_mb(), it's either the correct page_mapping() for
147 * the page, or an old page_mapping() and the page's own
148 * page_mapping() has gone NULL.
149 * The ->sync_page() address_space operation must tolerate
150 * page_mapping() going NULL. By an amazing coincidence,
151 * this comes about because none of the users of the page
152 * in the ->sync_page() methods make essential use of the
153 * page_mapping(), merely passing the page down to the backing
154 * device's unplug functions when it's non-NULL, which in turn
155 * ignore it for all cases but swap, where only page_private(page) is
156 * of interest. When page_mapping() does go NULL, the entire
157 * call stack gracefully ignores the page and returns.
161 mapping
= page_mapping(page
);
162 if (mapping
&& mapping
->a_ops
&& mapping
->a_ops
->sync_page
)
163 mapping
->a_ops
->sync_page(page
);
169 * filemap_fdatawrite_range - start writeback against all of a mapping's
170 * dirty pages that lie within the byte offsets <start, end>
171 * @mapping: address space structure to write
172 * @start: offset in bytes where the range starts
173 * @end: offset in bytes where the range ends
174 * @sync_mode: enable synchronous operation
176 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
177 * opposed to a regular memory * cleansing writeback. The difference between
178 * these two operations is that if a dirty page/buffer is encountered, it must
179 * be waited upon, and not just skipped over.
181 static int __filemap_fdatawrite_range(struct address_space
*mapping
,
182 loff_t start
, loff_t end
, int sync_mode
)
185 struct writeback_control wbc
= {
186 .sync_mode
= sync_mode
,
187 .nr_to_write
= mapping
->nrpages
* 2,
192 if (!mapping_cap_writeback_dirty(mapping
))
195 ret
= do_writepages(mapping
, &wbc
);
199 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
202 return __filemap_fdatawrite_range(mapping
, 0, 0, sync_mode
);
205 int filemap_fdatawrite(struct address_space
*mapping
)
207 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
209 EXPORT_SYMBOL(filemap_fdatawrite
);
211 static int filemap_fdatawrite_range(struct address_space
*mapping
,
212 loff_t start
, loff_t end
)
214 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
218 * This is a mostly non-blocking flush. Not suitable for data-integrity
219 * purposes - I/O may not be started against all dirty pages.
221 int filemap_flush(struct address_space
*mapping
)
223 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
225 EXPORT_SYMBOL(filemap_flush
);
228 * Wait for writeback to complete against pages indexed by start->end
231 static int wait_on_page_writeback_range(struct address_space
*mapping
,
232 pgoff_t start
, pgoff_t end
)
242 pagevec_init(&pvec
, 0);
244 while ((index
<= end
) &&
245 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
246 PAGECACHE_TAG_WRITEBACK
,
247 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
250 for (i
= 0; i
< nr_pages
; i
++) {
251 struct page
*page
= pvec
.pages
[i
];
253 /* until radix tree lookup accepts end_index */
254 if (page
->index
> end
)
257 wait_on_page_writeback(page
);
261 pagevec_release(&pvec
);
265 /* Check for outstanding write errors */
266 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
268 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
275 * Write and wait upon all the pages in the passed range. This is a "data
276 * integrity" operation. It waits upon in-flight writeout before starting and
277 * waiting upon new writeout. If there was an IO error, return it.
279 * We need to re-take i_sem during the generic_osync_inode list walk because
280 * it is otherwise livelockable.
282 int sync_page_range(struct inode
*inode
, struct address_space
*mapping
,
283 loff_t pos
, loff_t count
)
285 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
286 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
289 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
291 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
294 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
298 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
301 EXPORT_SYMBOL(sync_page_range
);
304 * Note: Holding i_sem across sync_page_range_nolock is not a good idea
305 * as it forces O_SYNC writers to different parts of the same file
306 * to be serialised right until io completion.
308 int sync_page_range_nolock(struct inode
*inode
, struct address_space
*mapping
,
309 loff_t pos
, loff_t count
)
311 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
312 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
315 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
317 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
319 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
321 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
324 EXPORT_SYMBOL(sync_page_range_nolock
);
327 * filemap_fdatawait - walk the list of under-writeback pages of the given
328 * address space and wait for all of them.
330 * @mapping: address space structure to wait for
332 int filemap_fdatawait(struct address_space
*mapping
)
334 loff_t i_size
= i_size_read(mapping
->host
);
339 return wait_on_page_writeback_range(mapping
, 0,
340 (i_size
- 1) >> PAGE_CACHE_SHIFT
);
342 EXPORT_SYMBOL(filemap_fdatawait
);
344 int filemap_write_and_wait(struct address_space
*mapping
)
348 if (mapping
->nrpages
) {
349 err
= filemap_fdatawrite(mapping
);
351 * Even if the above returned error, the pages may be
352 * written partially (e.g. -ENOSPC), so we wait for it.
353 * But the -EIO is special case, it may indicate the worst
354 * thing (e.g. bug) happened, so we avoid waiting for it.
357 int err2
= filemap_fdatawait(mapping
);
364 EXPORT_SYMBOL(filemap_write_and_wait
);
366 int filemap_write_and_wait_range(struct address_space
*mapping
,
367 loff_t lstart
, loff_t lend
)
371 if (mapping
->nrpages
) {
372 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
374 /* See comment of filemap_write_and_wait() */
376 int err2
= wait_on_page_writeback_range(mapping
,
377 lstart
>> PAGE_CACHE_SHIFT
,
378 lend
>> PAGE_CACHE_SHIFT
);
387 * This function is used to add newly allocated pagecache pages:
388 * the page is new, so we can just run SetPageLocked() against it.
389 * The other page state flags were set by rmqueue().
391 * This function does not add the page to the LRU. The caller must do that.
393 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
394 pgoff_t offset
, gfp_t gfp_mask
)
396 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
399 write_lock_irq(&mapping
->tree_lock
);
400 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
402 page_cache_get(page
);
404 page
->mapping
= mapping
;
405 page
->index
= offset
;
409 write_unlock_irq(&mapping
->tree_lock
);
410 radix_tree_preload_end();
415 EXPORT_SYMBOL(add_to_page_cache
);
417 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
418 pgoff_t offset
, gfp_t gfp_mask
)
420 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
427 * In order to wait for pages to become available there must be
428 * waitqueues associated with pages. By using a hash table of
429 * waitqueues where the bucket discipline is to maintain all
430 * waiters on the same queue and wake all when any of the pages
431 * become available, and for the woken contexts to check to be
432 * sure the appropriate page became available, this saves space
433 * at a cost of "thundering herd" phenomena during rare hash
436 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
438 const struct zone
*zone
= page_zone(page
);
440 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
443 static inline void wake_up_page(struct page
*page
, int bit
)
445 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
448 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
450 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
452 if (test_bit(bit_nr
, &page
->flags
))
453 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
454 TASK_UNINTERRUPTIBLE
);
456 EXPORT_SYMBOL(wait_on_page_bit
);
459 * unlock_page() - unlock a locked page
463 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
464 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
465 * mechananism between PageLocked pages and PageWriteback pages is shared.
466 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
468 * The first mb is necessary to safely close the critical section opened by the
469 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
470 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
471 * parallel wait_on_page_locked()).
473 void fastcall
unlock_page(struct page
*page
)
475 smp_mb__before_clear_bit();
476 if (!TestClearPageLocked(page
))
478 smp_mb__after_clear_bit();
479 wake_up_page(page
, PG_locked
);
481 EXPORT_SYMBOL(unlock_page
);
484 * End writeback against a page.
486 void end_page_writeback(struct page
*page
)
488 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
489 if (!test_clear_page_writeback(page
))
492 smp_mb__after_clear_bit();
493 wake_up_page(page
, PG_writeback
);
495 EXPORT_SYMBOL(end_page_writeback
);
498 * Get a lock on the page, assuming we need to sleep to get it.
500 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
501 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
502 * chances are that on the second loop, the block layer's plug list is empty,
503 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
505 void fastcall
__lock_page(struct page
*page
)
507 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
509 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
510 TASK_UNINTERRUPTIBLE
);
512 EXPORT_SYMBOL(__lock_page
);
515 * a rather lightweight function, finding and getting a reference to a
516 * hashed page atomically.
518 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
522 read_lock_irq(&mapping
->tree_lock
);
523 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
525 page_cache_get(page
);
526 read_unlock_irq(&mapping
->tree_lock
);
530 EXPORT_SYMBOL(find_get_page
);
533 * Same as above, but trylock it instead of incrementing the count.
535 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
539 read_lock_irq(&mapping
->tree_lock
);
540 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
541 if (page
&& TestSetPageLocked(page
))
543 read_unlock_irq(&mapping
->tree_lock
);
547 EXPORT_SYMBOL(find_trylock_page
);
550 * find_lock_page - locate, pin and lock a pagecache page
552 * @mapping: the address_space to search
553 * @offset: the page index
555 * Locates the desired pagecache page, locks it, increments its reference
556 * count and returns its address.
558 * Returns zero if the page was not present. find_lock_page() may sleep.
560 struct page
*find_lock_page(struct address_space
*mapping
,
561 unsigned long offset
)
565 read_lock_irq(&mapping
->tree_lock
);
567 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
569 page_cache_get(page
);
570 if (TestSetPageLocked(page
)) {
571 read_unlock_irq(&mapping
->tree_lock
);
573 read_lock_irq(&mapping
->tree_lock
);
575 /* Has the page been truncated while we slept? */
576 if (unlikely(page
->mapping
!= mapping
||
577 page
->index
!= offset
)) {
579 page_cache_release(page
);
584 read_unlock_irq(&mapping
->tree_lock
);
588 EXPORT_SYMBOL(find_lock_page
);
591 * find_or_create_page - locate or add a pagecache page
593 * @mapping: the page's address_space
594 * @index: the page's index into the mapping
595 * @gfp_mask: page allocation mode
597 * Locates a page in the pagecache. If the page is not present, a new page
598 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
599 * LRU list. The returned page is locked and has its reference count
602 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
605 * find_or_create_page() returns the desired page's address, or zero on
608 struct page
*find_or_create_page(struct address_space
*mapping
,
609 unsigned long index
, gfp_t gfp_mask
)
611 struct page
*page
, *cached_page
= NULL
;
614 page
= find_lock_page(mapping
, index
);
617 cached_page
= alloc_page(gfp_mask
);
621 err
= add_to_page_cache_lru(cached_page
, mapping
,
626 } else if (err
== -EEXIST
)
630 page_cache_release(cached_page
);
634 EXPORT_SYMBOL(find_or_create_page
);
637 * find_get_pages - gang pagecache lookup
638 * @mapping: The address_space to search
639 * @start: The starting page index
640 * @nr_pages: The maximum number of pages
641 * @pages: Where the resulting pages are placed
643 * find_get_pages() will search for and return a group of up to
644 * @nr_pages pages in the mapping. The pages are placed at @pages.
645 * find_get_pages() takes a reference against the returned pages.
647 * The search returns a group of mapping-contiguous pages with ascending
648 * indexes. There may be holes in the indices due to not-present pages.
650 * find_get_pages() returns the number of pages which were found.
652 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
653 unsigned int nr_pages
, struct page
**pages
)
658 read_lock_irq(&mapping
->tree_lock
);
659 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
660 (void **)pages
, start
, nr_pages
);
661 for (i
= 0; i
< ret
; i
++)
662 page_cache_get(pages
[i
]);
663 read_unlock_irq(&mapping
->tree_lock
);
668 * Like find_get_pages, except we only return pages which are tagged with
669 * `tag'. We update *index to index the next page for the traversal.
671 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
672 int tag
, unsigned int nr_pages
, struct page
**pages
)
677 read_lock_irq(&mapping
->tree_lock
);
678 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
679 (void **)pages
, *index
, nr_pages
, tag
);
680 for (i
= 0; i
< ret
; i
++)
681 page_cache_get(pages
[i
]);
683 *index
= pages
[ret
- 1]->index
+ 1;
684 read_unlock_irq(&mapping
->tree_lock
);
689 * Same as grab_cache_page, but do not wait if the page is unavailable.
690 * This is intended for speculative data generators, where the data can
691 * be regenerated if the page couldn't be grabbed. This routine should
692 * be safe to call while holding the lock for another page.
694 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
695 * and deadlock against the caller's locked page.
698 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
700 struct page
*page
= find_get_page(mapping
, index
);
704 if (!TestSetPageLocked(page
))
706 page_cache_release(page
);
709 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
710 page
= alloc_pages(gfp_mask
, 0);
711 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
712 page_cache_release(page
);
718 EXPORT_SYMBOL(grab_cache_page_nowait
);
721 * This is a generic file read routine, and uses the
722 * mapping->a_ops->readpage() function for the actual low-level
725 * This is really ugly. But the goto's actually try to clarify some
726 * of the logic when it comes to error handling etc.
728 * Note the struct file* is only passed for the use of readpage. It may be
731 void do_generic_mapping_read(struct address_space
*mapping
,
732 struct file_ra_state
*_ra
,
735 read_descriptor_t
*desc
,
738 struct inode
*inode
= mapping
->host
;
740 unsigned long end_index
;
741 unsigned long offset
;
742 unsigned long last_index
;
743 unsigned long next_index
;
744 unsigned long prev_index
;
746 struct page
*cached_page
;
748 struct file_ra_state ra
= *_ra
;
751 index
= *ppos
>> PAGE_CACHE_SHIFT
;
753 prev_index
= ra
.prev_page
;
754 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
755 offset
= *ppos
& ~PAGE_CACHE_MASK
;
757 isize
= i_size_read(inode
);
761 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
764 unsigned long nr
, ret
;
766 /* nr is the maximum number of bytes to copy from this page */
767 nr
= PAGE_CACHE_SIZE
;
768 if (index
>= end_index
) {
769 if (index
> end_index
)
771 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
779 if (index
== next_index
)
780 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
781 index
, last_index
- index
);
784 page
= find_get_page(mapping
, index
);
785 if (unlikely(page
== NULL
)) {
786 handle_ra_miss(mapping
, &ra
, index
);
789 if (!PageUptodate(page
))
790 goto page_not_up_to_date
;
793 /* If users can be writing to this page using arbitrary
794 * virtual addresses, take care about potential aliasing
795 * before reading the page on the kernel side.
797 if (mapping_writably_mapped(mapping
))
798 flush_dcache_page(page
);
801 * When (part of) the same page is read multiple times
802 * in succession, only mark it as accessed the first time.
804 if (prev_index
!= index
)
805 mark_page_accessed(page
);
809 * Ok, we have the page, and it's up-to-date, so
810 * now we can copy it to user space...
812 * The actor routine returns how many bytes were actually used..
813 * NOTE! This may not be the same as how much of a user buffer
814 * we filled up (we may be padding etc), so we can only update
815 * "pos" here (the actor routine has to update the user buffer
816 * pointers and the remaining count).
818 ret
= actor(desc
, page
, offset
, nr
);
820 index
+= offset
>> PAGE_CACHE_SHIFT
;
821 offset
&= ~PAGE_CACHE_MASK
;
823 page_cache_release(page
);
824 if (ret
== nr
&& desc
->count
)
829 /* Get exclusive access to the page ... */
832 /* Did it get unhashed before we got the lock? */
833 if (!page
->mapping
) {
835 page_cache_release(page
);
839 /* Did somebody else fill it already? */
840 if (PageUptodate(page
)) {
846 /* Start the actual read. The read will unlock the page. */
847 error
= mapping
->a_ops
->readpage(filp
, page
);
849 if (unlikely(error
)) {
850 if (error
== AOP_TRUNCATED_PAGE
) {
851 page_cache_release(page
);
857 if (!PageUptodate(page
)) {
859 if (!PageUptodate(page
)) {
860 if (page
->mapping
== NULL
) {
862 * invalidate_inode_pages got it
865 page_cache_release(page
);
876 * i_size must be checked after we have done ->readpage.
878 * Checking i_size after the readpage allows us to calculate
879 * the correct value for "nr", which means the zero-filled
880 * part of the page is not copied back to userspace (unless
881 * another truncate extends the file - this is desired though).
883 isize
= i_size_read(inode
);
884 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
885 if (unlikely(!isize
|| index
> end_index
)) {
886 page_cache_release(page
);
890 /* nr is the maximum number of bytes to copy from this page */
891 nr
= PAGE_CACHE_SIZE
;
892 if (index
== end_index
) {
893 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
895 page_cache_release(page
);
903 /* UHHUH! A synchronous read error occurred. Report it */
905 page_cache_release(page
);
910 * Ok, it wasn't cached, so we need to create a new
914 cached_page
= page_cache_alloc_cold(mapping
);
916 desc
->error
= -ENOMEM
;
920 error
= add_to_page_cache_lru(cached_page
, mapping
,
923 if (error
== -EEXIST
)
936 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
938 page_cache_release(cached_page
);
943 EXPORT_SYMBOL(do_generic_mapping_read
);
945 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
946 unsigned long offset
, unsigned long size
)
949 unsigned long left
, count
= desc
->count
;
955 * Faults on the destination of a read are common, so do it before
958 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
959 kaddr
= kmap_atomic(page
, KM_USER0
);
960 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
961 kaddr
+ offset
, size
);
962 kunmap_atomic(kaddr
, KM_USER0
);
967 /* Do it the slow way */
969 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
974 desc
->error
= -EFAULT
;
977 desc
->count
= count
- size
;
978 desc
->written
+= size
;
979 desc
->arg
.buf
+= size
;
984 * This is the "read()" routine for all filesystems
985 * that can use the page cache directly.
988 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
989 unsigned long nr_segs
, loff_t
*ppos
)
991 struct file
*filp
= iocb
->ki_filp
;
997 for (seg
= 0; seg
< nr_segs
; seg
++) {
998 const struct iovec
*iv
= &iov
[seg
];
1001 * If any segment has a negative length, or the cumulative
1002 * length ever wraps negative then return -EINVAL.
1004 count
+= iv
->iov_len
;
1005 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1007 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1012 count
-= iv
->iov_len
; /* This segment is no good */
1016 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1017 if (filp
->f_flags
& O_DIRECT
) {
1018 loff_t pos
= *ppos
, size
;
1019 struct address_space
*mapping
;
1020 struct inode
*inode
;
1022 mapping
= filp
->f_mapping
;
1023 inode
= mapping
->host
;
1026 goto out
; /* skip atime */
1027 size
= i_size_read(inode
);
1029 retval
= generic_file_direct_IO(READ
, iocb
,
1031 if (retval
> 0 && !is_sync_kiocb(iocb
))
1032 retval
= -EIOCBQUEUED
;
1034 *ppos
= pos
+ retval
;
1036 file_accessed(filp
);
1042 for (seg
= 0; seg
< nr_segs
; seg
++) {
1043 read_descriptor_t desc
;
1046 desc
.arg
.buf
= iov
[seg
].iov_base
;
1047 desc
.count
= iov
[seg
].iov_len
;
1048 if (desc
.count
== 0)
1051 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1052 retval
+= desc
.written
;
1054 retval
= retval
?: desc
.error
;
1063 EXPORT_SYMBOL(__generic_file_aio_read
);
1066 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1068 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1070 BUG_ON(iocb
->ki_pos
!= pos
);
1071 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1074 EXPORT_SYMBOL(generic_file_aio_read
);
1077 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1079 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1083 init_sync_kiocb(&kiocb
, filp
);
1084 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1085 if (-EIOCBQUEUED
== ret
)
1086 ret
= wait_on_sync_kiocb(&kiocb
);
1090 EXPORT_SYMBOL(generic_file_read
);
1092 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1095 unsigned long count
= desc
->count
;
1096 struct file
*file
= desc
->arg
.data
;
1101 written
= file
->f_op
->sendpage(file
, page
, offset
,
1102 size
, &file
->f_pos
, size
<count
);
1104 desc
->error
= written
;
1107 desc
->count
= count
- written
;
1108 desc
->written
+= written
;
1112 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1113 size_t count
, read_actor_t actor
, void *target
)
1115 read_descriptor_t desc
;
1122 desc
.arg
.data
= target
;
1125 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1127 return desc
.written
;
1131 EXPORT_SYMBOL(generic_file_sendfile
);
1134 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1135 unsigned long index
, unsigned long nr
)
1137 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1140 force_page_cache_readahead(mapping
, filp
, index
,
1141 max_sane_readahead(nr
));
1145 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1153 if (file
->f_mode
& FMODE_READ
) {
1154 struct address_space
*mapping
= file
->f_mapping
;
1155 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1156 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1157 unsigned long len
= end
- start
+ 1;
1158 ret
= do_readahead(mapping
, file
, start
, len
);
1167 * This adds the requested page to the page cache if it isn't already there,
1168 * and schedules an I/O to read in its contents from disk.
1170 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1171 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1173 struct address_space
*mapping
= file
->f_mapping
;
1178 page
= page_cache_alloc_cold(mapping
);
1182 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1184 ret
= mapping
->a_ops
->readpage(file
, page
);
1185 else if (ret
== -EEXIST
)
1186 ret
= 0; /* losing race to add is OK */
1188 page_cache_release(page
);
1190 } while (ret
== AOP_TRUNCATED_PAGE
);
1195 #define MMAP_LOTSAMISS (100)
1198 * filemap_nopage() is invoked via the vma operations vector for a
1199 * mapped memory region to read in file data during a page fault.
1201 * The goto's are kind of ugly, but this streamlines the normal case of having
1202 * it in the page cache, and handles the special cases reasonably without
1203 * having a lot of duplicated code.
1205 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1206 unsigned long address
, int *type
)
1209 struct file
*file
= area
->vm_file
;
1210 struct address_space
*mapping
= file
->f_mapping
;
1211 struct file_ra_state
*ra
= &file
->f_ra
;
1212 struct inode
*inode
= mapping
->host
;
1214 unsigned long size
, pgoff
;
1215 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1217 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1220 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1222 goto outside_data_content
;
1224 /* If we don't want any read-ahead, don't bother */
1225 if (VM_RandomReadHint(area
))
1226 goto no_cached_page
;
1229 * The readahead code wants to be told about each and every page
1230 * so it can build and shrink its windows appropriately
1232 * For sequential accesses, we use the generic readahead logic.
1234 if (VM_SequentialReadHint(area
))
1235 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1238 * Do we have something in the page cache already?
1241 page
= find_get_page(mapping
, pgoff
);
1243 unsigned long ra_pages
;
1245 if (VM_SequentialReadHint(area
)) {
1246 handle_ra_miss(mapping
, ra
, pgoff
);
1247 goto no_cached_page
;
1252 * Do we miss much more than hit in this file? If so,
1253 * stop bothering with read-ahead. It will only hurt.
1255 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1256 goto no_cached_page
;
1259 * To keep the pgmajfault counter straight, we need to
1260 * check did_readaround, as this is an inner loop.
1262 if (!did_readaround
) {
1263 majmin
= VM_FAULT_MAJOR
;
1264 inc_page_state(pgmajfault
);
1267 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1271 if (pgoff
> ra_pages
/ 2)
1272 start
= pgoff
- ra_pages
/ 2;
1273 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1275 page
= find_get_page(mapping
, pgoff
);
1277 goto no_cached_page
;
1280 if (!did_readaround
)
1284 * Ok, found a page in the page cache, now we need to check
1285 * that it's up-to-date.
1287 if (!PageUptodate(page
))
1288 goto page_not_uptodate
;
1292 * Found the page and have a reference on it.
1294 mark_page_accessed(page
);
1299 outside_data_content
:
1301 * An external ptracer can access pages that normally aren't
1304 if (area
->vm_mm
== current
->mm
)
1306 /* Fall through to the non-read-ahead case */
1309 * We're only likely to ever get here if MADV_RANDOM is in
1312 error
= page_cache_read(file
, pgoff
);
1316 * The page we want has now been added to the page cache.
1317 * In the unlikely event that someone removed it in the
1318 * meantime, we'll just come back here and read it again.
1324 * An error return from page_cache_read can result if the
1325 * system is low on memory, or a problem occurs while trying
1328 if (error
== -ENOMEM
)
1333 if (!did_readaround
) {
1334 majmin
= VM_FAULT_MAJOR
;
1335 inc_page_state(pgmajfault
);
1339 /* Did it get unhashed while we waited for it? */
1340 if (!page
->mapping
) {
1342 page_cache_release(page
);
1346 /* Did somebody else get it up-to-date? */
1347 if (PageUptodate(page
)) {
1352 error
= mapping
->a_ops
->readpage(file
, page
);
1354 wait_on_page_locked(page
);
1355 if (PageUptodate(page
))
1357 } else if (error
== AOP_TRUNCATED_PAGE
) {
1358 page_cache_release(page
);
1363 * Umm, take care of errors if the page isn't up-to-date.
1364 * Try to re-read it _once_. We do this synchronously,
1365 * because there really aren't any performance issues here
1366 * and we need to check for errors.
1370 /* Somebody truncated the page on us? */
1371 if (!page
->mapping
) {
1373 page_cache_release(page
);
1377 /* Somebody else successfully read it in? */
1378 if (PageUptodate(page
)) {
1382 ClearPageError(page
);
1383 error
= mapping
->a_ops
->readpage(file
, page
);
1385 wait_on_page_locked(page
);
1386 if (PageUptodate(page
))
1388 } else if (error
== AOP_TRUNCATED_PAGE
) {
1389 page_cache_release(page
);
1394 * Things didn't work out. Return zero to tell the
1395 * mm layer so, possibly freeing the page cache page first.
1397 page_cache_release(page
);
1401 EXPORT_SYMBOL(filemap_nopage
);
1403 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1406 struct address_space
*mapping
= file
->f_mapping
;
1411 * Do we have something in the page cache already?
1414 page
= find_get_page(mapping
, pgoff
);
1418 goto no_cached_page
;
1422 * Ok, found a page in the page cache, now we need to check
1423 * that it's up-to-date.
1425 if (!PageUptodate(page
)) {
1427 page_cache_release(page
);
1430 goto page_not_uptodate
;
1435 * Found the page and have a reference on it.
1437 mark_page_accessed(page
);
1441 error
= page_cache_read(file
, pgoff
);
1444 * The page we want has now been added to the page cache.
1445 * In the unlikely event that someone removed it in the
1446 * meantime, we'll just come back here and read it again.
1452 * An error return from page_cache_read can result if the
1453 * system is low on memory, or a problem occurs while trying
1461 /* Did it get unhashed while we waited for it? */
1462 if (!page
->mapping
) {
1467 /* Did somebody else get it up-to-date? */
1468 if (PageUptodate(page
)) {
1473 error
= mapping
->a_ops
->readpage(file
, page
);
1475 wait_on_page_locked(page
);
1476 if (PageUptodate(page
))
1478 } else if (error
== AOP_TRUNCATED_PAGE
) {
1479 page_cache_release(page
);
1484 * Umm, take care of errors if the page isn't up-to-date.
1485 * Try to re-read it _once_. We do this synchronously,
1486 * because there really aren't any performance issues here
1487 * and we need to check for errors.
1491 /* Somebody truncated the page on us? */
1492 if (!page
->mapping
) {
1496 /* Somebody else successfully read it in? */
1497 if (PageUptodate(page
)) {
1502 ClearPageError(page
);
1503 error
= mapping
->a_ops
->readpage(file
, page
);
1505 wait_on_page_locked(page
);
1506 if (PageUptodate(page
))
1508 } else if (error
== AOP_TRUNCATED_PAGE
) {
1509 page_cache_release(page
);
1514 * Things didn't work out. Return zero to tell the
1515 * mm layer so, possibly freeing the page cache page first.
1518 page_cache_release(page
);
1523 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1524 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1527 struct file
*file
= vma
->vm_file
;
1528 struct address_space
*mapping
= file
->f_mapping
;
1529 struct inode
*inode
= mapping
->host
;
1531 struct mm_struct
*mm
= vma
->vm_mm
;
1536 force_page_cache_readahead(mapping
, vma
->vm_file
,
1537 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1540 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1541 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1544 page
= filemap_getpage(file
, pgoff
, nonblock
);
1546 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1547 * done in shmem_populate calling shmem_getpage */
1548 if (!page
&& !nonblock
)
1552 err
= install_page(mm
, vma
, addr
, page
, prot
);
1554 page_cache_release(page
);
1557 } else if (vma
->vm_flags
& VM_NONLINEAR
) {
1558 /* No page was found just because we can't read it in now (being
1559 * here implies nonblock != 0), but the page may exist, so set
1560 * the PTE to fault it in later. */
1561 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1574 EXPORT_SYMBOL(filemap_populate
);
1576 struct vm_operations_struct generic_file_vm_ops
= {
1577 .nopage
= filemap_nopage
,
1578 .populate
= filemap_populate
,
1581 /* This is used for a general mmap of a disk file */
1583 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1585 struct address_space
*mapping
= file
->f_mapping
;
1587 if (!mapping
->a_ops
->readpage
)
1589 file_accessed(file
);
1590 vma
->vm_ops
= &generic_file_vm_ops
;
1595 * This is for filesystems which do not implement ->writepage.
1597 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1599 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1601 return generic_file_mmap(file
, vma
);
1604 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1608 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1612 #endif /* CONFIG_MMU */
1614 EXPORT_SYMBOL(generic_file_mmap
);
1615 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1617 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1618 unsigned long index
,
1619 int (*filler
)(void *,struct page
*),
1622 struct page
*page
, *cached_page
= NULL
;
1625 page
= find_get_page(mapping
, index
);
1628 cached_page
= page_cache_alloc_cold(mapping
);
1630 return ERR_PTR(-ENOMEM
);
1632 err
= add_to_page_cache_lru(cached_page
, mapping
,
1637 /* Presumably ENOMEM for radix tree node */
1638 page_cache_release(cached_page
);
1639 return ERR_PTR(err
);
1643 err
= filler(data
, page
);
1645 page_cache_release(page
);
1646 page
= ERR_PTR(err
);
1650 page_cache_release(cached_page
);
1655 * Read into the page cache. If a page already exists,
1656 * and PageUptodate() is not set, try to fill the page.
1658 struct page
*read_cache_page(struct address_space
*mapping
,
1659 unsigned long index
,
1660 int (*filler
)(void *,struct page
*),
1667 page
= __read_cache_page(mapping
, index
, filler
, data
);
1670 mark_page_accessed(page
);
1671 if (PageUptodate(page
))
1675 if (!page
->mapping
) {
1677 page_cache_release(page
);
1680 if (PageUptodate(page
)) {
1684 err
= filler(data
, page
);
1686 page_cache_release(page
);
1687 page
= ERR_PTR(err
);
1693 EXPORT_SYMBOL(read_cache_page
);
1696 * If the page was newly created, increment its refcount and add it to the
1697 * caller's lru-buffering pagevec. This function is specifically for
1698 * generic_file_write().
1700 static inline struct page
*
1701 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1702 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1707 page
= find_lock_page(mapping
, index
);
1709 if (!*cached_page
) {
1710 *cached_page
= page_cache_alloc(mapping
);
1714 err
= add_to_page_cache(*cached_page
, mapping
,
1719 page
= *cached_page
;
1720 page_cache_get(page
);
1721 if (!pagevec_add(lru_pvec
, page
))
1722 __pagevec_lru_add(lru_pvec
);
1723 *cached_page
= NULL
;
1730 * The logic we want is
1732 * if suid or (sgid and xgrp)
1735 int remove_suid(struct dentry
*dentry
)
1737 mode_t mode
= dentry
->d_inode
->i_mode
;
1741 /* suid always must be killed */
1742 if (unlikely(mode
& S_ISUID
))
1743 kill
= ATTR_KILL_SUID
;
1746 * sgid without any exec bits is just a mandatory locking mark; leave
1747 * it alone. If some exec bits are set, it's a real sgid; kill it.
1749 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1750 kill
|= ATTR_KILL_SGID
;
1752 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1753 struct iattr newattrs
;
1755 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1756 result
= notify_change(dentry
, &newattrs
);
1760 EXPORT_SYMBOL(remove_suid
);
1763 __filemap_copy_from_user_iovec(char *vaddr
,
1764 const struct iovec
*iov
, size_t base
, size_t bytes
)
1766 size_t copied
= 0, left
= 0;
1769 char __user
*buf
= iov
->iov_base
+ base
;
1770 int copy
= min(bytes
, iov
->iov_len
- base
);
1773 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1779 if (unlikely(left
)) {
1780 /* zero the rest of the target like __copy_from_user */
1782 memset(vaddr
, 0, bytes
);
1786 return copied
- left
;
1790 * Performs necessary checks before doing a write
1792 * Can adjust writing position aor amount of bytes to write.
1793 * Returns appropriate error code that caller should return or
1794 * zero in case that write should be allowed.
1796 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1798 struct inode
*inode
= file
->f_mapping
->host
;
1799 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1801 if (unlikely(*pos
< 0))
1805 /* FIXME: this is for backwards compatibility with 2.4 */
1806 if (file
->f_flags
& O_APPEND
)
1807 *pos
= i_size_read(inode
);
1809 if (limit
!= RLIM_INFINITY
) {
1810 if (*pos
>= limit
) {
1811 send_sig(SIGXFSZ
, current
, 0);
1814 if (*count
> limit
- (typeof(limit
))*pos
) {
1815 *count
= limit
- (typeof(limit
))*pos
;
1823 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1824 !(file
->f_flags
& O_LARGEFILE
))) {
1825 if (*pos
>= MAX_NON_LFS
) {
1826 send_sig(SIGXFSZ
, current
, 0);
1829 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1830 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1835 * Are we about to exceed the fs block limit ?
1837 * If we have written data it becomes a short write. If we have
1838 * exceeded without writing data we send a signal and return EFBIG.
1839 * Linus frestrict idea will clean these up nicely..
1841 if (likely(!isblk
)) {
1842 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1843 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1844 send_sig(SIGXFSZ
, current
, 0);
1847 /* zero-length writes at ->s_maxbytes are OK */
1850 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1851 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1854 if (bdev_read_only(I_BDEV(inode
)))
1856 isize
= i_size_read(inode
);
1857 if (*pos
>= isize
) {
1858 if (*count
|| *pos
> isize
)
1862 if (*pos
+ *count
> isize
)
1863 *count
= isize
- *pos
;
1867 EXPORT_SYMBOL(generic_write_checks
);
1870 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1871 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1872 size_t count
, size_t ocount
)
1874 struct file
*file
= iocb
->ki_filp
;
1875 struct address_space
*mapping
= file
->f_mapping
;
1876 struct inode
*inode
= mapping
->host
;
1879 if (count
!= ocount
)
1880 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1882 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1884 loff_t end
= pos
+ written
;
1885 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1886 i_size_write(inode
, end
);
1887 mark_inode_dirty(inode
);
1893 * Sync the fs metadata but not the minor inode changes and
1894 * of course not the data as we did direct DMA for the IO.
1895 * i_sem is held, which protects generic_osync_inode() from
1898 if (written
>= 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
1899 int err
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1903 if (written
== count
&& !is_sync_kiocb(iocb
))
1904 written
= -EIOCBQUEUED
;
1907 EXPORT_SYMBOL(generic_file_direct_write
);
1910 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1911 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1912 size_t count
, ssize_t written
)
1914 struct file
*file
= iocb
->ki_filp
;
1915 struct address_space
* mapping
= file
->f_mapping
;
1916 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1917 struct inode
*inode
= mapping
->host
;
1920 struct page
*cached_page
= NULL
;
1922 struct pagevec lru_pvec
;
1923 const struct iovec
*cur_iov
= iov
; /* current iovec */
1924 size_t iov_base
= 0; /* offset in the current iovec */
1927 pagevec_init(&lru_pvec
, 0);
1930 * handle partial DIO write. Adjust cur_iov if needed.
1932 if (likely(nr_segs
== 1))
1933 buf
= iov
->iov_base
+ written
;
1935 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1936 buf
= cur_iov
->iov_base
+ iov_base
;
1940 unsigned long index
;
1941 unsigned long offset
;
1942 unsigned long maxlen
;
1945 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1946 index
= pos
>> PAGE_CACHE_SHIFT
;
1947 bytes
= PAGE_CACHE_SIZE
- offset
;
1952 * Bring in the user page that we will copy from _first_.
1953 * Otherwise there's a nasty deadlock on copying from the
1954 * same page as we're writing to, without it being marked
1957 maxlen
= cur_iov
->iov_len
- iov_base
;
1960 fault_in_pages_readable(buf
, maxlen
);
1962 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1968 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1969 if (unlikely(status
)) {
1970 loff_t isize
= i_size_read(inode
);
1972 if (status
!= AOP_TRUNCATED_PAGE
)
1974 page_cache_release(page
);
1975 if (status
== AOP_TRUNCATED_PAGE
)
1978 * prepare_write() may have instantiated a few blocks
1979 * outside i_size. Trim these off again.
1981 if (pos
+ bytes
> isize
)
1982 vmtruncate(inode
, isize
);
1985 if (likely(nr_segs
== 1))
1986 copied
= filemap_copy_from_user(page
, offset
,
1989 copied
= filemap_copy_from_user_iovec(page
, offset
,
1990 cur_iov
, iov_base
, bytes
);
1991 flush_dcache_page(page
);
1992 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
1993 if (status
== AOP_TRUNCATED_PAGE
) {
1994 page_cache_release(page
);
1997 if (likely(copied
> 0)) {
2006 if (unlikely(nr_segs
> 1)) {
2007 filemap_set_next_iovec(&cur_iov
,
2010 buf
= cur_iov
->iov_base
+
2017 if (unlikely(copied
!= bytes
))
2021 mark_page_accessed(page
);
2022 page_cache_release(page
);
2025 balance_dirty_pages_ratelimited(mapping
);
2031 page_cache_release(cached_page
);
2034 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2036 if (likely(status
>= 0)) {
2037 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2038 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2039 status
= generic_osync_inode(inode
, mapping
,
2040 OSYNC_METADATA
|OSYNC_DATA
);
2045 * If we get here for O_DIRECT writes then we must have fallen through
2046 * to buffered writes (block instantiation inside i_size). So we sync
2047 * the file data here, to try to honour O_DIRECT expectations.
2049 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2050 status
= filemap_write_and_wait(mapping
);
2052 pagevec_lru_add(&lru_pvec
);
2053 return written
? written
: status
;
2055 EXPORT_SYMBOL(generic_file_buffered_write
);
2058 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2059 unsigned long nr_segs
, loff_t
*ppos
)
2061 struct file
*file
= iocb
->ki_filp
;
2062 struct address_space
* mapping
= file
->f_mapping
;
2063 size_t ocount
; /* original count */
2064 size_t count
; /* after file limit checks */
2065 struct inode
*inode
= mapping
->host
;
2072 for (seg
= 0; seg
< nr_segs
; seg
++) {
2073 const struct iovec
*iv
= &iov
[seg
];
2076 * If any segment has a negative length, or the cumulative
2077 * length ever wraps negative then return -EINVAL.
2079 ocount
+= iv
->iov_len
;
2080 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2082 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2087 ocount
-= iv
->iov_len
; /* This segment is no good */
2094 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2096 /* We can write back this queue in page reclaim */
2097 current
->backing_dev_info
= mapping
->backing_dev_info
;
2100 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2107 err
= remove_suid(file
->f_dentry
);
2111 inode_update_time(inode
, 1);
2113 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2114 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2115 written
= generic_file_direct_write(iocb
, iov
,
2116 &nr_segs
, pos
, ppos
, count
, ocount
);
2117 if (written
< 0 || written
== count
)
2120 * direct-io write to a hole: fall through to buffered I/O
2121 * for completing the rest of the request.
2127 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2128 pos
, ppos
, count
, written
);
2130 current
->backing_dev_info
= NULL
;
2131 return written
? written
: err
;
2133 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2136 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2137 unsigned long nr_segs
, loff_t
*ppos
)
2139 struct file
*file
= iocb
->ki_filp
;
2140 struct address_space
*mapping
= file
->f_mapping
;
2141 struct inode
*inode
= mapping
->host
;
2145 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2147 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2150 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2158 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2159 unsigned long nr_segs
, loff_t
*ppos
)
2164 init_sync_kiocb(&kiocb
, file
);
2165 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2166 if (ret
== -EIOCBQUEUED
)
2167 ret
= wait_on_sync_kiocb(&kiocb
);
2172 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2173 unsigned long nr_segs
, loff_t
*ppos
)
2178 init_sync_kiocb(&kiocb
, file
);
2179 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2180 if (-EIOCBQUEUED
== ret
)
2181 ret
= wait_on_sync_kiocb(&kiocb
);
2184 EXPORT_SYMBOL(generic_file_write_nolock
);
2186 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2187 size_t count
, loff_t pos
)
2189 struct file
*file
= iocb
->ki_filp
;
2190 struct address_space
*mapping
= file
->f_mapping
;
2191 struct inode
*inode
= mapping
->host
;
2193 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2196 BUG_ON(iocb
->ki_pos
!= pos
);
2198 down(&inode
->i_sem
);
2199 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2203 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2206 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2212 EXPORT_SYMBOL(generic_file_aio_write
);
2214 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2215 size_t count
, loff_t
*ppos
)
2217 struct address_space
*mapping
= file
->f_mapping
;
2218 struct inode
*inode
= mapping
->host
;
2220 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2223 down(&inode
->i_sem
);
2224 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2227 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2230 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2236 EXPORT_SYMBOL(generic_file_write
);
2238 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2239 unsigned long nr_segs
, loff_t
*ppos
)
2244 init_sync_kiocb(&kiocb
, filp
);
2245 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2246 if (-EIOCBQUEUED
== ret
)
2247 ret
= wait_on_sync_kiocb(&kiocb
);
2250 EXPORT_SYMBOL(generic_file_readv
);
2252 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2253 unsigned long nr_segs
, loff_t
*ppos
)
2255 struct address_space
*mapping
= file
->f_mapping
;
2256 struct inode
*inode
= mapping
->host
;
2259 down(&inode
->i_sem
);
2260 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2263 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2266 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2272 EXPORT_SYMBOL(generic_file_writev
);
2275 * Called under i_sem for writes to S_ISREG files. Returns -EIO if something
2276 * went wrong during pagecache shootdown.
2279 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2280 loff_t offset
, unsigned long nr_segs
)
2282 struct file
*file
= iocb
->ki_filp
;
2283 struct address_space
*mapping
= file
->f_mapping
;
2285 size_t write_len
= 0;
2288 * If it's a write, unmap all mmappings of the file up-front. This
2289 * will cause any pte dirty bits to be propagated into the pageframes
2290 * for the subsequent filemap_write_and_wait().
2293 write_len
= iov_length(iov
, nr_segs
);
2294 if (mapping_mapped(mapping
))
2295 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2298 retval
= filemap_write_and_wait(mapping
);
2300 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2302 if (rw
== WRITE
&& mapping
->nrpages
) {
2303 pgoff_t end
= (offset
+ write_len
- 1)
2304 >> PAGE_CACHE_SHIFT
;
2305 int err
= invalidate_inode_pages2_range(mapping
,
2306 offset
>> PAGE_CACHE_SHIFT
, end
);