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
, size_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 static int sync_page_range_nolock(struct inode
*inode
,
309 struct address_space
*mapping
,
310 loff_t pos
, size_t count
)
312 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
313 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
316 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
318 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
320 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
322 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
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 retval
= filemap_fdatawrite(mapping
);
351 retval
= filemap_fdatawait(mapping
);
356 int filemap_write_and_wait_range(struct address_space
*mapping
,
357 loff_t lstart
, loff_t lend
)
361 if (mapping
->nrpages
) {
362 retval
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
365 retval
= wait_on_page_writeback_range(mapping
,
366 lstart
>> PAGE_CACHE_SHIFT
,
367 lend
>> PAGE_CACHE_SHIFT
);
373 * This function is used to add newly allocated pagecache pages:
374 * the page is new, so we can just run SetPageLocked() against it.
375 * The other page state flags were set by rmqueue().
377 * This function does not add the page to the LRU. The caller must do that.
379 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
380 pgoff_t offset
, gfp_t gfp_mask
)
382 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
385 write_lock_irq(&mapping
->tree_lock
);
386 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
388 page_cache_get(page
);
390 page
->mapping
= mapping
;
391 page
->index
= offset
;
395 write_unlock_irq(&mapping
->tree_lock
);
396 radix_tree_preload_end();
401 EXPORT_SYMBOL(add_to_page_cache
);
403 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
404 pgoff_t offset
, gfp_t gfp_mask
)
406 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
413 * In order to wait for pages to become available there must be
414 * waitqueues associated with pages. By using a hash table of
415 * waitqueues where the bucket discipline is to maintain all
416 * waiters on the same queue and wake all when any of the pages
417 * become available, and for the woken contexts to check to be
418 * sure the appropriate page became available, this saves space
419 * at a cost of "thundering herd" phenomena during rare hash
422 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
424 const struct zone
*zone
= page_zone(page
);
426 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
429 static inline void wake_up_page(struct page
*page
, int bit
)
431 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
434 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
436 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
438 if (test_bit(bit_nr
, &page
->flags
))
439 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
440 TASK_UNINTERRUPTIBLE
);
442 EXPORT_SYMBOL(wait_on_page_bit
);
445 * unlock_page() - unlock a locked page
449 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
450 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
451 * mechananism between PageLocked pages and PageWriteback pages is shared.
452 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
454 * The first mb is necessary to safely close the critical section opened by the
455 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
456 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
457 * parallel wait_on_page_locked()).
459 void fastcall
unlock_page(struct page
*page
)
461 smp_mb__before_clear_bit();
462 if (!TestClearPageLocked(page
))
464 smp_mb__after_clear_bit();
465 wake_up_page(page
, PG_locked
);
467 EXPORT_SYMBOL(unlock_page
);
470 * End writeback against a page.
472 void end_page_writeback(struct page
*page
)
474 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
475 if (!test_clear_page_writeback(page
))
478 smp_mb__after_clear_bit();
479 wake_up_page(page
, PG_writeback
);
481 EXPORT_SYMBOL(end_page_writeback
);
484 * Get a lock on the page, assuming we need to sleep to get it.
486 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
487 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
488 * chances are that on the second loop, the block layer's plug list is empty,
489 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
491 void fastcall
__lock_page(struct page
*page
)
493 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
495 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
496 TASK_UNINTERRUPTIBLE
);
498 EXPORT_SYMBOL(__lock_page
);
501 * a rather lightweight function, finding and getting a reference to a
502 * hashed page atomically.
504 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
508 read_lock_irq(&mapping
->tree_lock
);
509 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
511 page_cache_get(page
);
512 read_unlock_irq(&mapping
->tree_lock
);
516 EXPORT_SYMBOL(find_get_page
);
519 * Same as above, but trylock it instead of incrementing the count.
521 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
525 read_lock_irq(&mapping
->tree_lock
);
526 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
527 if (page
&& TestSetPageLocked(page
))
529 read_unlock_irq(&mapping
->tree_lock
);
533 EXPORT_SYMBOL(find_trylock_page
);
536 * find_lock_page - locate, pin and lock a pagecache page
538 * @mapping: the address_space to search
539 * @offset: the page index
541 * Locates the desired pagecache page, locks it, increments its reference
542 * count and returns its address.
544 * Returns zero if the page was not present. find_lock_page() may sleep.
546 struct page
*find_lock_page(struct address_space
*mapping
,
547 unsigned long offset
)
551 read_lock_irq(&mapping
->tree_lock
);
553 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
555 page_cache_get(page
);
556 if (TestSetPageLocked(page
)) {
557 read_unlock_irq(&mapping
->tree_lock
);
559 read_lock_irq(&mapping
->tree_lock
);
561 /* Has the page been truncated while we slept? */
562 if (unlikely(page
->mapping
!= mapping
||
563 page
->index
!= offset
)) {
565 page_cache_release(page
);
570 read_unlock_irq(&mapping
->tree_lock
);
574 EXPORT_SYMBOL(find_lock_page
);
577 * find_or_create_page - locate or add a pagecache page
579 * @mapping: the page's address_space
580 * @index: the page's index into the mapping
581 * @gfp_mask: page allocation mode
583 * Locates a page in the pagecache. If the page is not present, a new page
584 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
585 * LRU list. The returned page is locked and has its reference count
588 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
591 * find_or_create_page() returns the desired page's address, or zero on
594 struct page
*find_or_create_page(struct address_space
*mapping
,
595 unsigned long index
, gfp_t gfp_mask
)
597 struct page
*page
, *cached_page
= NULL
;
600 page
= find_lock_page(mapping
, index
);
603 cached_page
= alloc_page(gfp_mask
);
607 err
= add_to_page_cache_lru(cached_page
, mapping
,
612 } else if (err
== -EEXIST
)
616 page_cache_release(cached_page
);
620 EXPORT_SYMBOL(find_or_create_page
);
623 * find_get_pages - gang pagecache lookup
624 * @mapping: The address_space to search
625 * @start: The starting page index
626 * @nr_pages: The maximum number of pages
627 * @pages: Where the resulting pages are placed
629 * find_get_pages() will search for and return a group of up to
630 * @nr_pages pages in the mapping. The pages are placed at @pages.
631 * find_get_pages() takes a reference against the returned pages.
633 * The search returns a group of mapping-contiguous pages with ascending
634 * indexes. There may be holes in the indices due to not-present pages.
636 * find_get_pages() returns the number of pages which were found.
638 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
639 unsigned int nr_pages
, struct page
**pages
)
644 read_lock_irq(&mapping
->tree_lock
);
645 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
646 (void **)pages
, start
, nr_pages
);
647 for (i
= 0; i
< ret
; i
++)
648 page_cache_get(pages
[i
]);
649 read_unlock_irq(&mapping
->tree_lock
);
654 * Like find_get_pages, except we only return pages which are tagged with
655 * `tag'. We update *index to index the next page for the traversal.
657 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
658 int tag
, unsigned int nr_pages
, struct page
**pages
)
663 read_lock_irq(&mapping
->tree_lock
);
664 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
665 (void **)pages
, *index
, nr_pages
, tag
);
666 for (i
= 0; i
< ret
; i
++)
667 page_cache_get(pages
[i
]);
669 *index
= pages
[ret
- 1]->index
+ 1;
670 read_unlock_irq(&mapping
->tree_lock
);
675 * Same as grab_cache_page, but do not wait if the page is unavailable.
676 * This is intended for speculative data generators, where the data can
677 * be regenerated if the page couldn't be grabbed. This routine should
678 * be safe to call while holding the lock for another page.
680 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
681 * and deadlock against the caller's locked page.
684 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
686 struct page
*page
= find_get_page(mapping
, index
);
690 if (!TestSetPageLocked(page
))
692 page_cache_release(page
);
695 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
696 page
= alloc_pages(gfp_mask
, 0);
697 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
698 page_cache_release(page
);
704 EXPORT_SYMBOL(grab_cache_page_nowait
);
707 * This is a generic file read routine, and uses the
708 * mapping->a_ops->readpage() function for the actual low-level
711 * This is really ugly. But the goto's actually try to clarify some
712 * of the logic when it comes to error handling etc.
714 * Note the struct file* is only passed for the use of readpage. It may be
717 void do_generic_mapping_read(struct address_space
*mapping
,
718 struct file_ra_state
*_ra
,
721 read_descriptor_t
*desc
,
724 struct inode
*inode
= mapping
->host
;
726 unsigned long end_index
;
727 unsigned long offset
;
728 unsigned long last_index
;
729 unsigned long next_index
;
730 unsigned long prev_index
;
732 struct page
*cached_page
;
734 struct file_ra_state ra
= *_ra
;
737 index
= *ppos
>> PAGE_CACHE_SHIFT
;
739 prev_index
= ra
.prev_page
;
740 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
741 offset
= *ppos
& ~PAGE_CACHE_MASK
;
743 isize
= i_size_read(inode
);
747 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
750 unsigned long nr
, ret
;
752 /* nr is the maximum number of bytes to copy from this page */
753 nr
= PAGE_CACHE_SIZE
;
754 if (index
>= end_index
) {
755 if (index
> end_index
)
757 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
765 if (index
== next_index
)
766 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
767 index
, last_index
- index
);
770 page
= find_get_page(mapping
, index
);
771 if (unlikely(page
== NULL
)) {
772 handle_ra_miss(mapping
, &ra
, index
);
775 if (!PageUptodate(page
))
776 goto page_not_up_to_date
;
779 /* If users can be writing to this page using arbitrary
780 * virtual addresses, take care about potential aliasing
781 * before reading the page on the kernel side.
783 if (mapping_writably_mapped(mapping
))
784 flush_dcache_page(page
);
787 * When (part of) the same page is read multiple times
788 * in succession, only mark it as accessed the first time.
790 if (prev_index
!= index
)
791 mark_page_accessed(page
);
795 * Ok, we have the page, and it's up-to-date, so
796 * now we can copy it to user space...
798 * The actor routine returns how many bytes were actually used..
799 * NOTE! This may not be the same as how much of a user buffer
800 * we filled up (we may be padding etc), so we can only update
801 * "pos" here (the actor routine has to update the user buffer
802 * pointers and the remaining count).
804 ret
= actor(desc
, page
, offset
, nr
);
806 index
+= offset
>> PAGE_CACHE_SHIFT
;
807 offset
&= ~PAGE_CACHE_MASK
;
809 page_cache_release(page
);
810 if (ret
== nr
&& desc
->count
)
815 /* Get exclusive access to the page ... */
818 /* Did it get unhashed before we got the lock? */
819 if (!page
->mapping
) {
821 page_cache_release(page
);
825 /* Did somebody else fill it already? */
826 if (PageUptodate(page
)) {
832 /* Start the actual read. The read will unlock the page. */
833 error
= mapping
->a_ops
->readpage(filp
, page
);
835 if (unlikely(error
)) {
836 if (error
== AOP_TRUNCATED_PAGE
) {
837 page_cache_release(page
);
843 if (!PageUptodate(page
)) {
845 if (!PageUptodate(page
)) {
846 if (page
->mapping
== NULL
) {
848 * invalidate_inode_pages got it
851 page_cache_release(page
);
862 * i_size must be checked after we have done ->readpage.
864 * Checking i_size after the readpage allows us to calculate
865 * the correct value for "nr", which means the zero-filled
866 * part of the page is not copied back to userspace (unless
867 * another truncate extends the file - this is desired though).
869 isize
= i_size_read(inode
);
870 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
871 if (unlikely(!isize
|| index
> end_index
)) {
872 page_cache_release(page
);
876 /* nr is the maximum number of bytes to copy from this page */
877 nr
= PAGE_CACHE_SIZE
;
878 if (index
== end_index
) {
879 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
881 page_cache_release(page
);
889 /* UHHUH! A synchronous read error occurred. Report it */
891 page_cache_release(page
);
896 * Ok, it wasn't cached, so we need to create a new
900 cached_page
= page_cache_alloc_cold(mapping
);
902 desc
->error
= -ENOMEM
;
906 error
= add_to_page_cache_lru(cached_page
, mapping
,
909 if (error
== -EEXIST
)
922 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
924 page_cache_release(cached_page
);
929 EXPORT_SYMBOL(do_generic_mapping_read
);
931 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
932 unsigned long offset
, unsigned long size
)
935 unsigned long left
, count
= desc
->count
;
941 * Faults on the destination of a read are common, so do it before
944 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
945 kaddr
= kmap_atomic(page
, KM_USER0
);
946 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
947 kaddr
+ offset
, size
);
948 kunmap_atomic(kaddr
, KM_USER0
);
953 /* Do it the slow way */
955 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
960 desc
->error
= -EFAULT
;
963 desc
->count
= count
- size
;
964 desc
->written
+= size
;
965 desc
->arg
.buf
+= size
;
970 * This is the "read()" routine for all filesystems
971 * that can use the page cache directly.
974 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
975 unsigned long nr_segs
, loff_t
*ppos
)
977 struct file
*filp
= iocb
->ki_filp
;
983 for (seg
= 0; seg
< nr_segs
; seg
++) {
984 const struct iovec
*iv
= &iov
[seg
];
987 * If any segment has a negative length, or the cumulative
988 * length ever wraps negative then return -EINVAL.
990 count
+= iv
->iov_len
;
991 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
993 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
998 count
-= iv
->iov_len
; /* This segment is no good */
1002 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1003 if (filp
->f_flags
& O_DIRECT
) {
1004 loff_t pos
= *ppos
, size
;
1005 struct address_space
*mapping
;
1006 struct inode
*inode
;
1008 mapping
= filp
->f_mapping
;
1009 inode
= mapping
->host
;
1012 goto out
; /* skip atime */
1013 size
= i_size_read(inode
);
1015 retval
= generic_file_direct_IO(READ
, iocb
,
1017 if (retval
> 0 && !is_sync_kiocb(iocb
))
1018 retval
= -EIOCBQUEUED
;
1020 *ppos
= pos
+ retval
;
1022 file_accessed(filp
);
1028 for (seg
= 0; seg
< nr_segs
; seg
++) {
1029 read_descriptor_t desc
;
1032 desc
.arg
.buf
= iov
[seg
].iov_base
;
1033 desc
.count
= iov
[seg
].iov_len
;
1034 if (desc
.count
== 0)
1037 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1038 retval
+= desc
.written
;
1040 retval
= retval
?: desc
.error
;
1049 EXPORT_SYMBOL(__generic_file_aio_read
);
1052 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1054 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1056 BUG_ON(iocb
->ki_pos
!= pos
);
1057 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1060 EXPORT_SYMBOL(generic_file_aio_read
);
1063 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1065 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1069 init_sync_kiocb(&kiocb
, filp
);
1070 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1071 if (-EIOCBQUEUED
== ret
)
1072 ret
= wait_on_sync_kiocb(&kiocb
);
1076 EXPORT_SYMBOL(generic_file_read
);
1078 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1081 unsigned long count
= desc
->count
;
1082 struct file
*file
= desc
->arg
.data
;
1087 written
= file
->f_op
->sendpage(file
, page
, offset
,
1088 size
, &file
->f_pos
, size
<count
);
1090 desc
->error
= written
;
1093 desc
->count
= count
- written
;
1094 desc
->written
+= written
;
1098 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1099 size_t count
, read_actor_t actor
, void *target
)
1101 read_descriptor_t desc
;
1108 desc
.arg
.data
= target
;
1111 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1113 return desc
.written
;
1117 EXPORT_SYMBOL(generic_file_sendfile
);
1120 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1121 unsigned long index
, unsigned long nr
)
1123 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1126 force_page_cache_readahead(mapping
, filp
, index
,
1127 max_sane_readahead(nr
));
1131 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1139 if (file
->f_mode
& FMODE_READ
) {
1140 struct address_space
*mapping
= file
->f_mapping
;
1141 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1142 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1143 unsigned long len
= end
- start
+ 1;
1144 ret
= do_readahead(mapping
, file
, start
, len
);
1153 * This adds the requested page to the page cache if it isn't already there,
1154 * and schedules an I/O to read in its contents from disk.
1156 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1157 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1159 struct address_space
*mapping
= file
->f_mapping
;
1164 page
= page_cache_alloc_cold(mapping
);
1168 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1170 ret
= mapping
->a_ops
->readpage(file
, page
);
1171 else if (ret
== -EEXIST
)
1172 ret
= 0; /* losing race to add is OK */
1174 page_cache_release(page
);
1176 } while (ret
== AOP_TRUNCATED_PAGE
);
1181 #define MMAP_LOTSAMISS (100)
1184 * filemap_nopage() is invoked via the vma operations vector for a
1185 * mapped memory region to read in file data during a page fault.
1187 * The goto's are kind of ugly, but this streamlines the normal case of having
1188 * it in the page cache, and handles the special cases reasonably without
1189 * having a lot of duplicated code.
1191 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1192 unsigned long address
, int *type
)
1195 struct file
*file
= area
->vm_file
;
1196 struct address_space
*mapping
= file
->f_mapping
;
1197 struct file_ra_state
*ra
= &file
->f_ra
;
1198 struct inode
*inode
= mapping
->host
;
1200 unsigned long size
, pgoff
;
1201 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1203 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1206 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1208 goto outside_data_content
;
1210 /* If we don't want any read-ahead, don't bother */
1211 if (VM_RandomReadHint(area
))
1212 goto no_cached_page
;
1215 * The readahead code wants to be told about each and every page
1216 * so it can build and shrink its windows appropriately
1218 * For sequential accesses, we use the generic readahead logic.
1220 if (VM_SequentialReadHint(area
))
1221 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1224 * Do we have something in the page cache already?
1227 page
= find_get_page(mapping
, pgoff
);
1229 unsigned long ra_pages
;
1231 if (VM_SequentialReadHint(area
)) {
1232 handle_ra_miss(mapping
, ra
, pgoff
);
1233 goto no_cached_page
;
1238 * Do we miss much more than hit in this file? If so,
1239 * stop bothering with read-ahead. It will only hurt.
1241 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1242 goto no_cached_page
;
1245 * To keep the pgmajfault counter straight, we need to
1246 * check did_readaround, as this is an inner loop.
1248 if (!did_readaround
) {
1249 majmin
= VM_FAULT_MAJOR
;
1250 inc_page_state(pgmajfault
);
1253 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1257 if (pgoff
> ra_pages
/ 2)
1258 start
= pgoff
- ra_pages
/ 2;
1259 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1261 page
= find_get_page(mapping
, pgoff
);
1263 goto no_cached_page
;
1266 if (!did_readaround
)
1270 * Ok, found a page in the page cache, now we need to check
1271 * that it's up-to-date.
1273 if (!PageUptodate(page
))
1274 goto page_not_uptodate
;
1278 * Found the page and have a reference on it.
1280 mark_page_accessed(page
);
1285 outside_data_content
:
1287 * An external ptracer can access pages that normally aren't
1290 if (area
->vm_mm
== current
->mm
)
1292 /* Fall through to the non-read-ahead case */
1295 * We're only likely to ever get here if MADV_RANDOM is in
1298 error
= page_cache_read(file
, pgoff
);
1302 * The page we want has now been added to the page cache.
1303 * In the unlikely event that someone removed it in the
1304 * meantime, we'll just come back here and read it again.
1310 * An error return from page_cache_read can result if the
1311 * system is low on memory, or a problem occurs while trying
1314 if (error
== -ENOMEM
)
1319 if (!did_readaround
) {
1320 majmin
= VM_FAULT_MAJOR
;
1321 inc_page_state(pgmajfault
);
1325 /* Did it get unhashed while we waited for it? */
1326 if (!page
->mapping
) {
1328 page_cache_release(page
);
1332 /* Did somebody else get it up-to-date? */
1333 if (PageUptodate(page
)) {
1338 error
= mapping
->a_ops
->readpage(file
, page
);
1340 wait_on_page_locked(page
);
1341 if (PageUptodate(page
))
1343 } else if (error
== AOP_TRUNCATED_PAGE
) {
1344 page_cache_release(page
);
1349 * Umm, take care of errors if the page isn't up-to-date.
1350 * Try to re-read it _once_. We do this synchronously,
1351 * because there really aren't any performance issues here
1352 * and we need to check for errors.
1356 /* Somebody truncated the page on us? */
1357 if (!page
->mapping
) {
1359 page_cache_release(page
);
1363 /* Somebody else successfully read it in? */
1364 if (PageUptodate(page
)) {
1368 ClearPageError(page
);
1369 error
= mapping
->a_ops
->readpage(file
, page
);
1371 wait_on_page_locked(page
);
1372 if (PageUptodate(page
))
1374 } else if (error
== AOP_TRUNCATED_PAGE
) {
1375 page_cache_release(page
);
1380 * Things didn't work out. Return zero to tell the
1381 * mm layer so, possibly freeing the page cache page first.
1383 page_cache_release(page
);
1387 EXPORT_SYMBOL(filemap_nopage
);
1389 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1392 struct address_space
*mapping
= file
->f_mapping
;
1397 * Do we have something in the page cache already?
1400 page
= find_get_page(mapping
, pgoff
);
1404 goto no_cached_page
;
1408 * Ok, found a page in the page cache, now we need to check
1409 * that it's up-to-date.
1411 if (!PageUptodate(page
)) {
1413 page_cache_release(page
);
1416 goto page_not_uptodate
;
1421 * Found the page and have a reference on it.
1423 mark_page_accessed(page
);
1427 error
= page_cache_read(file
, pgoff
);
1430 * The page we want has now been added to the page cache.
1431 * In the unlikely event that someone removed it in the
1432 * meantime, we'll just come back here and read it again.
1438 * An error return from page_cache_read can result if the
1439 * system is low on memory, or a problem occurs while trying
1447 /* Did it get unhashed while we waited for it? */
1448 if (!page
->mapping
) {
1453 /* Did somebody else get it up-to-date? */
1454 if (PageUptodate(page
)) {
1459 error
= mapping
->a_ops
->readpage(file
, page
);
1461 wait_on_page_locked(page
);
1462 if (PageUptodate(page
))
1464 } else if (error
== AOP_TRUNCATED_PAGE
) {
1465 page_cache_release(page
);
1470 * Umm, take care of errors if the page isn't up-to-date.
1471 * Try to re-read it _once_. We do this synchronously,
1472 * because there really aren't any performance issues here
1473 * and we need to check for errors.
1477 /* Somebody truncated the page on us? */
1478 if (!page
->mapping
) {
1482 /* Somebody else successfully read it in? */
1483 if (PageUptodate(page
)) {
1488 ClearPageError(page
);
1489 error
= mapping
->a_ops
->readpage(file
, page
);
1491 wait_on_page_locked(page
);
1492 if (PageUptodate(page
))
1494 } else if (error
== AOP_TRUNCATED_PAGE
) {
1495 page_cache_release(page
);
1500 * Things didn't work out. Return zero to tell the
1501 * mm layer so, possibly freeing the page cache page first.
1504 page_cache_release(page
);
1509 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1510 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1513 struct file
*file
= vma
->vm_file
;
1514 struct address_space
*mapping
= file
->f_mapping
;
1515 struct inode
*inode
= mapping
->host
;
1517 struct mm_struct
*mm
= vma
->vm_mm
;
1522 force_page_cache_readahead(mapping
, vma
->vm_file
,
1523 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1526 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1527 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1530 page
= filemap_getpage(file
, pgoff
, nonblock
);
1532 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1533 * done in shmem_populate calling shmem_getpage */
1534 if (!page
&& !nonblock
)
1538 err
= install_page(mm
, vma
, addr
, page
, prot
);
1540 page_cache_release(page
);
1543 } else if (vma
->vm_flags
& VM_NONLINEAR
) {
1544 /* No page was found just because we can't read it in now (being
1545 * here implies nonblock != 0), but the page may exist, so set
1546 * the PTE to fault it in later. */
1547 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1560 EXPORT_SYMBOL(filemap_populate
);
1562 struct vm_operations_struct generic_file_vm_ops
= {
1563 .nopage
= filemap_nopage
,
1564 .populate
= filemap_populate
,
1567 /* This is used for a general mmap of a disk file */
1569 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1571 struct address_space
*mapping
= file
->f_mapping
;
1573 if (!mapping
->a_ops
->readpage
)
1575 file_accessed(file
);
1576 vma
->vm_ops
= &generic_file_vm_ops
;
1581 * This is for filesystems which do not implement ->writepage.
1583 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1585 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1587 return generic_file_mmap(file
, vma
);
1590 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1594 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1598 #endif /* CONFIG_MMU */
1600 EXPORT_SYMBOL(generic_file_mmap
);
1601 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1603 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1604 unsigned long index
,
1605 int (*filler
)(void *,struct page
*),
1608 struct page
*page
, *cached_page
= NULL
;
1611 page
= find_get_page(mapping
, index
);
1614 cached_page
= page_cache_alloc_cold(mapping
);
1616 return ERR_PTR(-ENOMEM
);
1618 err
= add_to_page_cache_lru(cached_page
, mapping
,
1623 /* Presumably ENOMEM for radix tree node */
1624 page_cache_release(cached_page
);
1625 return ERR_PTR(err
);
1629 err
= filler(data
, page
);
1631 page_cache_release(page
);
1632 page
= ERR_PTR(err
);
1636 page_cache_release(cached_page
);
1641 * Read into the page cache. If a page already exists,
1642 * and PageUptodate() is not set, try to fill the page.
1644 struct page
*read_cache_page(struct address_space
*mapping
,
1645 unsigned long index
,
1646 int (*filler
)(void *,struct page
*),
1653 page
= __read_cache_page(mapping
, index
, filler
, data
);
1656 mark_page_accessed(page
);
1657 if (PageUptodate(page
))
1661 if (!page
->mapping
) {
1663 page_cache_release(page
);
1666 if (PageUptodate(page
)) {
1670 err
= filler(data
, page
);
1672 page_cache_release(page
);
1673 page
= ERR_PTR(err
);
1679 EXPORT_SYMBOL(read_cache_page
);
1682 * If the page was newly created, increment its refcount and add it to the
1683 * caller's lru-buffering pagevec. This function is specifically for
1684 * generic_file_write().
1686 static inline struct page
*
1687 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1688 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1693 page
= find_lock_page(mapping
, index
);
1695 if (!*cached_page
) {
1696 *cached_page
= page_cache_alloc(mapping
);
1700 err
= add_to_page_cache(*cached_page
, mapping
,
1705 page
= *cached_page
;
1706 page_cache_get(page
);
1707 if (!pagevec_add(lru_pvec
, page
))
1708 __pagevec_lru_add(lru_pvec
);
1709 *cached_page
= NULL
;
1716 * The logic we want is
1718 * if suid or (sgid and xgrp)
1721 int remove_suid(struct dentry
*dentry
)
1723 mode_t mode
= dentry
->d_inode
->i_mode
;
1727 /* suid always must be killed */
1728 if (unlikely(mode
& S_ISUID
))
1729 kill
= ATTR_KILL_SUID
;
1732 * sgid without any exec bits is just a mandatory locking mark; leave
1733 * it alone. If some exec bits are set, it's a real sgid; kill it.
1735 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1736 kill
|= ATTR_KILL_SGID
;
1738 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1739 struct iattr newattrs
;
1741 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1742 result
= notify_change(dentry
, &newattrs
);
1746 EXPORT_SYMBOL(remove_suid
);
1749 __filemap_copy_from_user_iovec(char *vaddr
,
1750 const struct iovec
*iov
, size_t base
, size_t bytes
)
1752 size_t copied
= 0, left
= 0;
1755 char __user
*buf
= iov
->iov_base
+ base
;
1756 int copy
= min(bytes
, iov
->iov_len
- base
);
1759 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1765 if (unlikely(left
)) {
1766 /* zero the rest of the target like __copy_from_user */
1768 memset(vaddr
, 0, bytes
);
1772 return copied
- left
;
1776 * Performs necessary checks before doing a write
1778 * Can adjust writing position aor amount of bytes to write.
1779 * Returns appropriate error code that caller should return or
1780 * zero in case that write should be allowed.
1782 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1784 struct inode
*inode
= file
->f_mapping
->host
;
1785 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1787 if (unlikely(*pos
< 0))
1791 /* FIXME: this is for backwards compatibility with 2.4 */
1792 if (file
->f_flags
& O_APPEND
)
1793 *pos
= i_size_read(inode
);
1795 if (limit
!= RLIM_INFINITY
) {
1796 if (*pos
>= limit
) {
1797 send_sig(SIGXFSZ
, current
, 0);
1800 if (*count
> limit
- (typeof(limit
))*pos
) {
1801 *count
= limit
- (typeof(limit
))*pos
;
1809 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1810 !(file
->f_flags
& O_LARGEFILE
))) {
1811 if (*pos
>= MAX_NON_LFS
) {
1812 send_sig(SIGXFSZ
, current
, 0);
1815 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1816 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1821 * Are we about to exceed the fs block limit ?
1823 * If we have written data it becomes a short write. If we have
1824 * exceeded without writing data we send a signal and return EFBIG.
1825 * Linus frestrict idea will clean these up nicely..
1827 if (likely(!isblk
)) {
1828 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1829 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1830 send_sig(SIGXFSZ
, current
, 0);
1833 /* zero-length writes at ->s_maxbytes are OK */
1836 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1837 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1840 if (bdev_read_only(I_BDEV(inode
)))
1842 isize
= i_size_read(inode
);
1843 if (*pos
>= isize
) {
1844 if (*count
|| *pos
> isize
)
1848 if (*pos
+ *count
> isize
)
1849 *count
= isize
- *pos
;
1853 EXPORT_SYMBOL(generic_write_checks
);
1856 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1857 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1858 size_t count
, size_t ocount
)
1860 struct file
*file
= iocb
->ki_filp
;
1861 struct address_space
*mapping
= file
->f_mapping
;
1862 struct inode
*inode
= mapping
->host
;
1865 if (count
!= ocount
)
1866 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1868 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1870 loff_t end
= pos
+ written
;
1871 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1872 i_size_write(inode
, end
);
1873 mark_inode_dirty(inode
);
1879 * Sync the fs metadata but not the minor inode changes and
1880 * of course not the data as we did direct DMA for the IO.
1881 * i_sem is held, which protects generic_osync_inode() from
1884 if (written
>= 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
1885 int err
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1889 if (written
== count
&& !is_sync_kiocb(iocb
))
1890 written
= -EIOCBQUEUED
;
1893 EXPORT_SYMBOL(generic_file_direct_write
);
1896 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1897 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1898 size_t count
, ssize_t written
)
1900 struct file
*file
= iocb
->ki_filp
;
1901 struct address_space
* mapping
= file
->f_mapping
;
1902 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1903 struct inode
*inode
= mapping
->host
;
1906 struct page
*cached_page
= NULL
;
1908 struct pagevec lru_pvec
;
1909 const struct iovec
*cur_iov
= iov
; /* current iovec */
1910 size_t iov_base
= 0; /* offset in the current iovec */
1913 pagevec_init(&lru_pvec
, 0);
1916 * handle partial DIO write. Adjust cur_iov if needed.
1918 if (likely(nr_segs
== 1))
1919 buf
= iov
->iov_base
+ written
;
1921 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1922 buf
= cur_iov
->iov_base
+ iov_base
;
1926 unsigned long index
;
1927 unsigned long offset
;
1928 unsigned long maxlen
;
1931 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1932 index
= pos
>> PAGE_CACHE_SHIFT
;
1933 bytes
= PAGE_CACHE_SIZE
- offset
;
1938 * Bring in the user page that we will copy from _first_.
1939 * Otherwise there's a nasty deadlock on copying from the
1940 * same page as we're writing to, without it being marked
1943 maxlen
= cur_iov
->iov_len
- iov_base
;
1946 fault_in_pages_readable(buf
, maxlen
);
1948 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1954 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1955 if (unlikely(status
)) {
1956 loff_t isize
= i_size_read(inode
);
1958 if (status
!= AOP_TRUNCATED_PAGE
)
1960 page_cache_release(page
);
1961 if (status
== AOP_TRUNCATED_PAGE
)
1964 * prepare_write() may have instantiated a few blocks
1965 * outside i_size. Trim these off again.
1967 if (pos
+ bytes
> isize
)
1968 vmtruncate(inode
, isize
);
1971 if (likely(nr_segs
== 1))
1972 copied
= filemap_copy_from_user(page
, offset
,
1975 copied
= filemap_copy_from_user_iovec(page
, offset
,
1976 cur_iov
, iov_base
, bytes
);
1977 flush_dcache_page(page
);
1978 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
1979 if (status
== AOP_TRUNCATED_PAGE
) {
1980 page_cache_release(page
);
1983 if (likely(copied
> 0)) {
1992 if (unlikely(nr_segs
> 1)) {
1993 filemap_set_next_iovec(&cur_iov
,
1996 buf
= cur_iov
->iov_base
+
2003 if (unlikely(copied
!= bytes
))
2007 mark_page_accessed(page
);
2008 page_cache_release(page
);
2011 balance_dirty_pages_ratelimited(mapping
);
2017 page_cache_release(cached_page
);
2020 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2022 if (likely(status
>= 0)) {
2023 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2024 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2025 status
= generic_osync_inode(inode
, mapping
,
2026 OSYNC_METADATA
|OSYNC_DATA
);
2031 * If we get here for O_DIRECT writes then we must have fallen through
2032 * to buffered writes (block instantiation inside i_size). So we sync
2033 * the file data here, to try to honour O_DIRECT expectations.
2035 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2036 status
= filemap_write_and_wait(mapping
);
2038 pagevec_lru_add(&lru_pvec
);
2039 return written
? written
: status
;
2041 EXPORT_SYMBOL(generic_file_buffered_write
);
2044 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2045 unsigned long nr_segs
, loff_t
*ppos
)
2047 struct file
*file
= iocb
->ki_filp
;
2048 struct address_space
* mapping
= file
->f_mapping
;
2049 size_t ocount
; /* original count */
2050 size_t count
; /* after file limit checks */
2051 struct inode
*inode
= mapping
->host
;
2058 for (seg
= 0; seg
< nr_segs
; seg
++) {
2059 const struct iovec
*iv
= &iov
[seg
];
2062 * If any segment has a negative length, or the cumulative
2063 * length ever wraps negative then return -EINVAL.
2065 ocount
+= iv
->iov_len
;
2066 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2068 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2073 ocount
-= iv
->iov_len
; /* This segment is no good */
2080 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2082 /* We can write back this queue in page reclaim */
2083 current
->backing_dev_info
= mapping
->backing_dev_info
;
2086 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2093 err
= remove_suid(file
->f_dentry
);
2097 inode_update_time(inode
, 1);
2099 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2100 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2101 written
= generic_file_direct_write(iocb
, iov
,
2102 &nr_segs
, pos
, ppos
, count
, ocount
);
2103 if (written
< 0 || written
== count
)
2106 * direct-io write to a hole: fall through to buffered I/O
2107 * for completing the rest of the request.
2113 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2114 pos
, ppos
, count
, written
);
2116 current
->backing_dev_info
= NULL
;
2117 return written
? written
: err
;
2119 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2122 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2123 unsigned long nr_segs
, loff_t
*ppos
)
2125 struct file
*file
= iocb
->ki_filp
;
2126 struct address_space
*mapping
= file
->f_mapping
;
2127 struct inode
*inode
= mapping
->host
;
2131 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2133 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2136 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2144 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2145 unsigned long nr_segs
, loff_t
*ppos
)
2150 init_sync_kiocb(&kiocb
, file
);
2151 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2152 if (ret
== -EIOCBQUEUED
)
2153 ret
= wait_on_sync_kiocb(&kiocb
);
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 (-EIOCBQUEUED
== ret
)
2167 ret
= wait_on_sync_kiocb(&kiocb
);
2170 EXPORT_SYMBOL(generic_file_write_nolock
);
2172 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2173 size_t count
, loff_t pos
)
2175 struct file
*file
= iocb
->ki_filp
;
2176 struct address_space
*mapping
= file
->f_mapping
;
2177 struct inode
*inode
= mapping
->host
;
2179 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2182 BUG_ON(iocb
->ki_pos
!= pos
);
2184 down(&inode
->i_sem
);
2185 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2189 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2192 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2198 EXPORT_SYMBOL(generic_file_aio_write
);
2200 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2201 size_t count
, loff_t
*ppos
)
2203 struct address_space
*mapping
= file
->f_mapping
;
2204 struct inode
*inode
= mapping
->host
;
2206 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2209 down(&inode
->i_sem
);
2210 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2213 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2216 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2222 EXPORT_SYMBOL(generic_file_write
);
2224 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2225 unsigned long nr_segs
, loff_t
*ppos
)
2230 init_sync_kiocb(&kiocb
, filp
);
2231 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2232 if (-EIOCBQUEUED
== ret
)
2233 ret
= wait_on_sync_kiocb(&kiocb
);
2236 EXPORT_SYMBOL(generic_file_readv
);
2238 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2239 unsigned long nr_segs
, loff_t
*ppos
)
2241 struct address_space
*mapping
= file
->f_mapping
;
2242 struct inode
*inode
= mapping
->host
;
2245 down(&inode
->i_sem
);
2246 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2249 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2252 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2258 EXPORT_SYMBOL(generic_file_writev
);
2261 * Called under i_sem for writes to S_ISREG files. Returns -EIO if something
2262 * went wrong during pagecache shootdown.
2265 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2266 loff_t offset
, unsigned long nr_segs
)
2268 struct file
*file
= iocb
->ki_filp
;
2269 struct address_space
*mapping
= file
->f_mapping
;
2271 size_t write_len
= 0;
2274 * If it's a write, unmap all mmappings of the file up-front. This
2275 * will cause any pte dirty bits to be propagated into the pageframes
2276 * for the subsequent filemap_write_and_wait().
2279 write_len
= iov_length(iov
, nr_segs
);
2280 if (mapping_mapped(mapping
))
2281 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2284 retval
= filemap_write_and_wait(mapping
);
2286 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2288 if (rw
== WRITE
&& mapping
->nrpages
) {
2289 pgoff_t end
= (offset
+ write_len
- 1)
2290 >> PAGE_CACHE_SHIFT
;
2291 int err
= invalidate_inode_pages2_range(mapping
,
2292 offset
>> PAGE_CACHE_SHIFT
, end
);