4 * Copyright (C) 1994-1999 Linus Torvalds
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/compiler.h>
17 #include <linux/aio.h>
18 #include <linux/capability.h>
19 #include <linux/kernel_stat.h>
21 #include <linux/swap.h>
22 #include <linux/mman.h>
23 #include <linux/pagemap.h>
24 #include <linux/file.h>
25 #include <linux/uio.h>
26 #include <linux/hash.h>
27 #include <linux/writeback.h>
28 #include <linux/pagevec.h>
29 #include <linux/blkdev.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
34 * FIXME: remove all knowledge of the buffer layer from the core VM
36 #include <linux/buffer_head.h> /* for generic_osync_inode */
38 #include <asm/uaccess.h>
42 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
43 loff_t offset
, unsigned long nr_segs
);
46 * Shared mappings implemented 30.11.1994. It's not fully working yet,
49 * Shared mappings now work. 15.8.1995 Bruno.
51 * finished 'unifying' the page and buffer cache and SMP-threaded the
52 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
54 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
60 * ->i_mmap_lock (vmtruncate)
61 * ->private_lock (__free_pte->__set_page_dirty_buffers)
62 * ->swap_lock (exclusive_swap_page, others)
63 * ->mapping->tree_lock
66 * ->i_mmap_lock (truncate->unmap_mapping_range)
70 * ->page_table_lock or pte_lock (various, mainly in memory.c)
71 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
74 * ->lock_page (access_process_vm)
80 * ->i_alloc_sem (various)
83 * ->sb_lock (fs/fs-writeback.c)
84 * ->mapping->tree_lock (__sync_single_inode)
87 * ->anon_vma.lock (vma_adjust)
90 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
92 * ->page_table_lock or pte_lock
93 * ->swap_lock (try_to_unmap_one)
94 * ->private_lock (try_to_unmap_one)
95 * ->tree_lock (try_to_unmap_one)
96 * ->zone.lru_lock (follow_page->mark_page_accessed)
97 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
98 * ->private_lock (page_remove_rmap->set_page_dirty)
99 * ->tree_lock (page_remove_rmap->set_page_dirty)
100 * ->inode_lock (page_remove_rmap->set_page_dirty)
101 * ->inode_lock (zap_pte_range->set_page_dirty)
102 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
105 * ->dcache_lock (proc_pid_lookup)
109 * Remove a page from the page cache and free it. Caller has to make
110 * sure the page is locked and that nobody else uses it - or that usage
111 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
113 void __remove_from_page_cache(struct page
*page
)
115 struct address_space
*mapping
= page
->mapping
;
117 radix_tree_delete(&mapping
->page_tree
, page
->index
);
118 page
->mapping
= NULL
;
123 void remove_from_page_cache(struct page
*page
)
125 struct address_space
*mapping
= page
->mapping
;
127 BUG_ON(!PageLocked(page
));
129 write_lock_irq(&mapping
->tree_lock
);
130 __remove_from_page_cache(page
);
131 write_unlock_irq(&mapping
->tree_lock
);
134 static int sync_page(void *word
)
136 struct address_space
*mapping
;
139 page
= container_of((unsigned long *)word
, struct page
, flags
);
142 * page_mapping() is being called without PG_locked held.
143 * Some knowledge of the state and use of the page is used to
144 * reduce the requirements down to a memory barrier.
145 * The danger here is of a stale page_mapping() return value
146 * indicating a struct address_space different from the one it's
147 * associated with when it is associated with one.
148 * After smp_mb(), it's either the correct page_mapping() for
149 * the page, or an old page_mapping() and the page's own
150 * page_mapping() has gone NULL.
151 * The ->sync_page() address_space operation must tolerate
152 * page_mapping() going NULL. By an amazing coincidence,
153 * this comes about because none of the users of the page
154 * in the ->sync_page() methods make essential use of the
155 * page_mapping(), merely passing the page down to the backing
156 * device's unplug functions when it's non-NULL, which in turn
157 * ignore it for all cases but swap, where only page_private(page) is
158 * of interest. When page_mapping() does go NULL, the entire
159 * call stack gracefully ignores the page and returns.
163 mapping
= page_mapping(page
);
164 if (mapping
&& mapping
->a_ops
&& mapping
->a_ops
->sync_page
)
165 mapping
->a_ops
->sync_page(page
);
171 * filemap_fdatawrite_range - start writeback against all of a mapping's
172 * dirty pages that lie within the byte offsets <start, end>
173 * @mapping: address space structure to write
174 * @start: offset in bytes where the range starts
175 * @end: offset in bytes where the range ends
176 * @sync_mode: enable synchronous operation
178 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
179 * opposed to a regular memory * cleansing writeback. The difference between
180 * these two operations is that if a dirty page/buffer is encountered, it must
181 * be waited upon, and not just skipped over.
183 static int __filemap_fdatawrite_range(struct address_space
*mapping
,
184 loff_t start
, loff_t end
, int sync_mode
)
187 struct writeback_control wbc
= {
188 .sync_mode
= sync_mode
,
189 .nr_to_write
= mapping
->nrpages
* 2,
194 if (!mapping_cap_writeback_dirty(mapping
))
197 ret
= do_writepages(mapping
, &wbc
);
201 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
204 return __filemap_fdatawrite_range(mapping
, 0, 0, sync_mode
);
207 int filemap_fdatawrite(struct address_space
*mapping
)
209 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
211 EXPORT_SYMBOL(filemap_fdatawrite
);
213 static int filemap_fdatawrite_range(struct address_space
*mapping
,
214 loff_t start
, loff_t end
)
216 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
220 * This is a mostly non-blocking flush. Not suitable for data-integrity
221 * purposes - I/O may not be started against all dirty pages.
223 int filemap_flush(struct address_space
*mapping
)
225 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
227 EXPORT_SYMBOL(filemap_flush
);
230 * Wait for writeback to complete against pages indexed by start->end
233 static int wait_on_page_writeback_range(struct address_space
*mapping
,
234 pgoff_t start
, pgoff_t end
)
244 pagevec_init(&pvec
, 0);
246 while ((index
<= end
) &&
247 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
248 PAGECACHE_TAG_WRITEBACK
,
249 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
252 for (i
= 0; i
< nr_pages
; i
++) {
253 struct page
*page
= pvec
.pages
[i
];
255 /* until radix tree lookup accepts end_index */
256 if (page
->index
> end
)
259 wait_on_page_writeback(page
);
263 pagevec_release(&pvec
);
267 /* Check for outstanding write errors */
268 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
270 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
277 * Write and wait upon all the pages in the passed range. This is a "data
278 * integrity" operation. It waits upon in-flight writeout before starting and
279 * waiting upon new writeout. If there was an IO error, return it.
281 * We need to re-take i_mutex during the generic_osync_inode list walk because
282 * it is otherwise livelockable.
284 int sync_page_range(struct inode
*inode
, struct address_space
*mapping
,
285 loff_t pos
, loff_t count
)
287 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
288 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
291 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
293 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
295 mutex_lock(&inode
->i_mutex
);
296 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
297 mutex_unlock(&inode
->i_mutex
);
300 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
303 EXPORT_SYMBOL(sync_page_range
);
306 * Note: Holding i_mutex across sync_page_range_nolock is not a good idea
307 * as it forces O_SYNC writers to different parts of the same file
308 * to be serialised right until io completion.
310 int sync_page_range_nolock(struct inode
*inode
, struct address_space
*mapping
,
311 loff_t pos
, loff_t count
)
313 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
314 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
317 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
319 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
321 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
323 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
326 EXPORT_SYMBOL(sync_page_range_nolock
);
329 * filemap_fdatawait - walk the list of under-writeback pages of the given
330 * address space and wait for all of them.
332 * @mapping: address space structure to wait for
334 int filemap_fdatawait(struct address_space
*mapping
)
336 loff_t i_size
= i_size_read(mapping
->host
);
341 return wait_on_page_writeback_range(mapping
, 0,
342 (i_size
- 1) >> PAGE_CACHE_SHIFT
);
344 EXPORT_SYMBOL(filemap_fdatawait
);
346 int filemap_write_and_wait(struct address_space
*mapping
)
350 if (mapping
->nrpages
) {
351 err
= filemap_fdatawrite(mapping
);
353 * Even if the above returned error, the pages may be
354 * written partially (e.g. -ENOSPC), so we wait for it.
355 * But the -EIO is special case, it may indicate the worst
356 * thing (e.g. bug) happened, so we avoid waiting for it.
359 int err2
= filemap_fdatawait(mapping
);
366 EXPORT_SYMBOL(filemap_write_and_wait
);
368 int filemap_write_and_wait_range(struct address_space
*mapping
,
369 loff_t lstart
, loff_t lend
)
373 if (mapping
->nrpages
) {
374 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
376 /* See comment of filemap_write_and_wait() */
378 int err2
= wait_on_page_writeback_range(mapping
,
379 lstart
>> PAGE_CACHE_SHIFT
,
380 lend
>> PAGE_CACHE_SHIFT
);
389 * This function is used to add newly allocated pagecache pages:
390 * the page is new, so we can just run SetPageLocked() against it.
391 * The other page state flags were set by rmqueue().
393 * This function does not add the page to the LRU. The caller must do that.
395 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
396 pgoff_t offset
, gfp_t gfp_mask
)
398 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
401 write_lock_irq(&mapping
->tree_lock
);
402 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
404 page_cache_get(page
);
406 page
->mapping
= mapping
;
407 page
->index
= offset
;
411 write_unlock_irq(&mapping
->tree_lock
);
412 radix_tree_preload_end();
417 EXPORT_SYMBOL(add_to_page_cache
);
419 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
420 pgoff_t offset
, gfp_t gfp_mask
)
422 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
429 * In order to wait for pages to become available there must be
430 * waitqueues associated with pages. By using a hash table of
431 * waitqueues where the bucket discipline is to maintain all
432 * waiters on the same queue and wake all when any of the pages
433 * become available, and for the woken contexts to check to be
434 * sure the appropriate page became available, this saves space
435 * at a cost of "thundering herd" phenomena during rare hash
438 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
440 const struct zone
*zone
= page_zone(page
);
442 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
445 static inline void wake_up_page(struct page
*page
, int bit
)
447 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
450 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
452 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
454 if (test_bit(bit_nr
, &page
->flags
))
455 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
456 TASK_UNINTERRUPTIBLE
);
458 EXPORT_SYMBOL(wait_on_page_bit
);
461 * unlock_page() - unlock a locked page
465 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
466 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
467 * mechananism between PageLocked pages and PageWriteback pages is shared.
468 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
470 * The first mb is necessary to safely close the critical section opened by the
471 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
472 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
473 * parallel wait_on_page_locked()).
475 void fastcall
unlock_page(struct page
*page
)
477 smp_mb__before_clear_bit();
478 if (!TestClearPageLocked(page
))
480 smp_mb__after_clear_bit();
481 wake_up_page(page
, PG_locked
);
483 EXPORT_SYMBOL(unlock_page
);
486 * End writeback against a page.
488 void end_page_writeback(struct page
*page
)
490 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
491 if (!test_clear_page_writeback(page
))
494 smp_mb__after_clear_bit();
495 wake_up_page(page
, PG_writeback
);
497 EXPORT_SYMBOL(end_page_writeback
);
500 * Get a lock on the page, assuming we need to sleep to get it.
502 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
503 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
504 * chances are that on the second loop, the block layer's plug list is empty,
505 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
507 void fastcall
__lock_page(struct page
*page
)
509 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
511 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
512 TASK_UNINTERRUPTIBLE
);
514 EXPORT_SYMBOL(__lock_page
);
517 * a rather lightweight function, finding and getting a reference to a
518 * hashed page atomically.
520 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
524 read_lock_irq(&mapping
->tree_lock
);
525 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
527 page_cache_get(page
);
528 read_unlock_irq(&mapping
->tree_lock
);
532 EXPORT_SYMBOL(find_get_page
);
535 * Same as above, but trylock it instead of incrementing the count.
537 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
541 read_lock_irq(&mapping
->tree_lock
);
542 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
543 if (page
&& TestSetPageLocked(page
))
545 read_unlock_irq(&mapping
->tree_lock
);
549 EXPORT_SYMBOL(find_trylock_page
);
552 * find_lock_page - locate, pin and lock a pagecache page
554 * @mapping: the address_space to search
555 * @offset: the page index
557 * Locates the desired pagecache page, locks it, increments its reference
558 * count and returns its address.
560 * Returns zero if the page was not present. find_lock_page() may sleep.
562 struct page
*find_lock_page(struct address_space
*mapping
,
563 unsigned long offset
)
567 read_lock_irq(&mapping
->tree_lock
);
569 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
571 page_cache_get(page
);
572 if (TestSetPageLocked(page
)) {
573 read_unlock_irq(&mapping
->tree_lock
);
575 read_lock_irq(&mapping
->tree_lock
);
577 /* Has the page been truncated while we slept? */
578 if (unlikely(page
->mapping
!= mapping
||
579 page
->index
!= offset
)) {
581 page_cache_release(page
);
586 read_unlock_irq(&mapping
->tree_lock
);
590 EXPORT_SYMBOL(find_lock_page
);
593 * find_or_create_page - locate or add a pagecache page
595 * @mapping: the page's address_space
596 * @index: the page's index into the mapping
597 * @gfp_mask: page allocation mode
599 * Locates a page in the pagecache. If the page is not present, a new page
600 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
601 * LRU list. The returned page is locked and has its reference count
604 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
607 * find_or_create_page() returns the desired page's address, or zero on
610 struct page
*find_or_create_page(struct address_space
*mapping
,
611 unsigned long index
, gfp_t gfp_mask
)
613 struct page
*page
, *cached_page
= NULL
;
616 page
= find_lock_page(mapping
, index
);
619 cached_page
= alloc_page(gfp_mask
);
623 err
= add_to_page_cache_lru(cached_page
, mapping
,
628 } else if (err
== -EEXIST
)
632 page_cache_release(cached_page
);
636 EXPORT_SYMBOL(find_or_create_page
);
639 * find_get_pages - gang pagecache lookup
640 * @mapping: The address_space to search
641 * @start: The starting page index
642 * @nr_pages: The maximum number of pages
643 * @pages: Where the resulting pages are placed
645 * find_get_pages() will search for and return a group of up to
646 * @nr_pages pages in the mapping. The pages are placed at @pages.
647 * find_get_pages() takes a reference against the returned pages.
649 * The search returns a group of mapping-contiguous pages with ascending
650 * indexes. There may be holes in the indices due to not-present pages.
652 * find_get_pages() returns the number of pages which were found.
654 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
655 unsigned int nr_pages
, struct page
**pages
)
660 read_lock_irq(&mapping
->tree_lock
);
661 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
662 (void **)pages
, start
, nr_pages
);
663 for (i
= 0; i
< ret
; i
++)
664 page_cache_get(pages
[i
]);
665 read_unlock_irq(&mapping
->tree_lock
);
670 * Like find_get_pages, except we only return pages which are tagged with
671 * `tag'. We update *index to index the next page for the traversal.
673 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
674 int tag
, unsigned int nr_pages
, struct page
**pages
)
679 read_lock_irq(&mapping
->tree_lock
);
680 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
681 (void **)pages
, *index
, nr_pages
, tag
);
682 for (i
= 0; i
< ret
; i
++)
683 page_cache_get(pages
[i
]);
685 *index
= pages
[ret
- 1]->index
+ 1;
686 read_unlock_irq(&mapping
->tree_lock
);
691 * Same as grab_cache_page, but do not wait if the page is unavailable.
692 * This is intended for speculative data generators, where the data can
693 * be regenerated if the page couldn't be grabbed. This routine should
694 * be safe to call while holding the lock for another page.
696 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
697 * and deadlock against the caller's locked page.
700 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
702 struct page
*page
= find_get_page(mapping
, index
);
706 if (!TestSetPageLocked(page
))
708 page_cache_release(page
);
711 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
712 page
= alloc_pages(gfp_mask
, 0);
713 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
714 page_cache_release(page
);
720 EXPORT_SYMBOL(grab_cache_page_nowait
);
723 * This is a generic file read routine, and uses the
724 * mapping->a_ops->readpage() function for the actual low-level
727 * This is really ugly. But the goto's actually try to clarify some
728 * of the logic when it comes to error handling etc.
730 * Note the struct file* is only passed for the use of readpage. It may be
733 void do_generic_mapping_read(struct address_space
*mapping
,
734 struct file_ra_state
*_ra
,
737 read_descriptor_t
*desc
,
740 struct inode
*inode
= mapping
->host
;
742 unsigned long end_index
;
743 unsigned long offset
;
744 unsigned long last_index
;
745 unsigned long next_index
;
746 unsigned long prev_index
;
748 struct page
*cached_page
;
750 struct file_ra_state ra
= *_ra
;
753 index
= *ppos
>> PAGE_CACHE_SHIFT
;
755 prev_index
= ra
.prev_page
;
756 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
757 offset
= *ppos
& ~PAGE_CACHE_MASK
;
759 isize
= i_size_read(inode
);
763 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
766 unsigned long nr
, ret
;
768 /* nr is the maximum number of bytes to copy from this page */
769 nr
= PAGE_CACHE_SIZE
;
770 if (index
>= end_index
) {
771 if (index
> end_index
)
773 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
781 if (index
== next_index
)
782 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
783 index
, last_index
- index
);
786 page
= find_get_page(mapping
, index
);
787 if (unlikely(page
== NULL
)) {
788 handle_ra_miss(mapping
, &ra
, index
);
791 if (!PageUptodate(page
))
792 goto page_not_up_to_date
;
795 /* If users can be writing to this page using arbitrary
796 * virtual addresses, take care about potential aliasing
797 * before reading the page on the kernel side.
799 if (mapping_writably_mapped(mapping
))
800 flush_dcache_page(page
);
803 * When (part of) the same page is read multiple times
804 * in succession, only mark it as accessed the first time.
806 if (prev_index
!= index
)
807 mark_page_accessed(page
);
811 * Ok, we have the page, and it's up-to-date, so
812 * now we can copy it to user space...
814 * The actor routine returns how many bytes were actually used..
815 * NOTE! This may not be the same as how much of a user buffer
816 * we filled up (we may be padding etc), so we can only update
817 * "pos" here (the actor routine has to update the user buffer
818 * pointers and the remaining count).
820 ret
= actor(desc
, page
, offset
, nr
);
822 index
+= offset
>> PAGE_CACHE_SHIFT
;
823 offset
&= ~PAGE_CACHE_MASK
;
825 page_cache_release(page
);
826 if (ret
== nr
&& desc
->count
)
831 /* Get exclusive access to the page ... */
834 /* Did it get unhashed before we got the lock? */
835 if (!page
->mapping
) {
837 page_cache_release(page
);
841 /* Did somebody else fill it already? */
842 if (PageUptodate(page
)) {
848 /* Start the actual read. The read will unlock the page. */
849 error
= mapping
->a_ops
->readpage(filp
, page
);
851 if (unlikely(error
)) {
852 if (error
== AOP_TRUNCATED_PAGE
) {
853 page_cache_release(page
);
859 if (!PageUptodate(page
)) {
861 if (!PageUptodate(page
)) {
862 if (page
->mapping
== NULL
) {
864 * invalidate_inode_pages got it
867 page_cache_release(page
);
878 * i_size must be checked after we have done ->readpage.
880 * Checking i_size after the readpage allows us to calculate
881 * the correct value for "nr", which means the zero-filled
882 * part of the page is not copied back to userspace (unless
883 * another truncate extends the file - this is desired though).
885 isize
= i_size_read(inode
);
886 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
887 if (unlikely(!isize
|| index
> end_index
)) {
888 page_cache_release(page
);
892 /* nr is the maximum number of bytes to copy from this page */
893 nr
= PAGE_CACHE_SIZE
;
894 if (index
== end_index
) {
895 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
897 page_cache_release(page
);
905 /* UHHUH! A synchronous read error occurred. Report it */
907 page_cache_release(page
);
912 * Ok, it wasn't cached, so we need to create a new
916 cached_page
= page_cache_alloc_cold(mapping
);
918 desc
->error
= -ENOMEM
;
922 error
= add_to_page_cache_lru(cached_page
, mapping
,
925 if (error
== -EEXIST
)
938 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
940 page_cache_release(cached_page
);
945 EXPORT_SYMBOL(do_generic_mapping_read
);
947 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
948 unsigned long offset
, unsigned long size
)
951 unsigned long left
, count
= desc
->count
;
957 * Faults on the destination of a read are common, so do it before
960 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
961 kaddr
= kmap_atomic(page
, KM_USER0
);
962 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
963 kaddr
+ offset
, size
);
964 kunmap_atomic(kaddr
, KM_USER0
);
969 /* Do it the slow way */
971 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
976 desc
->error
= -EFAULT
;
979 desc
->count
= count
- size
;
980 desc
->written
+= size
;
981 desc
->arg
.buf
+= size
;
986 * This is the "read()" routine for all filesystems
987 * that can use the page cache directly.
990 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
991 unsigned long nr_segs
, loff_t
*ppos
)
993 struct file
*filp
= iocb
->ki_filp
;
999 for (seg
= 0; seg
< nr_segs
; seg
++) {
1000 const struct iovec
*iv
= &iov
[seg
];
1003 * If any segment has a negative length, or the cumulative
1004 * length ever wraps negative then return -EINVAL.
1006 count
+= iv
->iov_len
;
1007 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1009 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1014 count
-= iv
->iov_len
; /* This segment is no good */
1018 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1019 if (filp
->f_flags
& O_DIRECT
) {
1020 loff_t pos
= *ppos
, size
;
1021 struct address_space
*mapping
;
1022 struct inode
*inode
;
1024 mapping
= filp
->f_mapping
;
1025 inode
= mapping
->host
;
1028 goto out
; /* skip atime */
1029 size
= i_size_read(inode
);
1031 retval
= generic_file_direct_IO(READ
, iocb
,
1033 if (retval
> 0 && !is_sync_kiocb(iocb
))
1034 retval
= -EIOCBQUEUED
;
1036 *ppos
= pos
+ retval
;
1038 file_accessed(filp
);
1044 for (seg
= 0; seg
< nr_segs
; seg
++) {
1045 read_descriptor_t desc
;
1048 desc
.arg
.buf
= iov
[seg
].iov_base
;
1049 desc
.count
= iov
[seg
].iov_len
;
1050 if (desc
.count
== 0)
1053 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1054 retval
+= desc
.written
;
1056 retval
= retval
?: desc
.error
;
1065 EXPORT_SYMBOL(__generic_file_aio_read
);
1068 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1070 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1072 BUG_ON(iocb
->ki_pos
!= pos
);
1073 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1076 EXPORT_SYMBOL(generic_file_aio_read
);
1079 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1081 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1085 init_sync_kiocb(&kiocb
, filp
);
1086 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1087 if (-EIOCBQUEUED
== ret
)
1088 ret
= wait_on_sync_kiocb(&kiocb
);
1092 EXPORT_SYMBOL(generic_file_read
);
1094 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1097 unsigned long count
= desc
->count
;
1098 struct file
*file
= desc
->arg
.data
;
1103 written
= file
->f_op
->sendpage(file
, page
, offset
,
1104 size
, &file
->f_pos
, size
<count
);
1106 desc
->error
= written
;
1109 desc
->count
= count
- written
;
1110 desc
->written
+= written
;
1114 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1115 size_t count
, read_actor_t actor
, void *target
)
1117 read_descriptor_t desc
;
1124 desc
.arg
.data
= target
;
1127 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1129 return desc
.written
;
1133 EXPORT_SYMBOL(generic_file_sendfile
);
1136 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1137 unsigned long index
, unsigned long nr
)
1139 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1142 force_page_cache_readahead(mapping
, filp
, index
,
1143 max_sane_readahead(nr
));
1147 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1155 if (file
->f_mode
& FMODE_READ
) {
1156 struct address_space
*mapping
= file
->f_mapping
;
1157 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1158 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1159 unsigned long len
= end
- start
+ 1;
1160 ret
= do_readahead(mapping
, file
, start
, len
);
1169 * This adds the requested page to the page cache if it isn't already there,
1170 * and schedules an I/O to read in its contents from disk.
1172 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1173 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1175 struct address_space
*mapping
= file
->f_mapping
;
1180 page
= page_cache_alloc_cold(mapping
);
1184 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1186 ret
= mapping
->a_ops
->readpage(file
, page
);
1187 else if (ret
== -EEXIST
)
1188 ret
= 0; /* losing race to add is OK */
1190 page_cache_release(page
);
1192 } while (ret
== AOP_TRUNCATED_PAGE
);
1197 #define MMAP_LOTSAMISS (100)
1200 * filemap_nopage() is invoked via the vma operations vector for a
1201 * mapped memory region to read in file data during a page fault.
1203 * The goto's are kind of ugly, but this streamlines the normal case of having
1204 * it in the page cache, and handles the special cases reasonably without
1205 * having a lot of duplicated code.
1207 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1208 unsigned long address
, int *type
)
1211 struct file
*file
= area
->vm_file
;
1212 struct address_space
*mapping
= file
->f_mapping
;
1213 struct file_ra_state
*ra
= &file
->f_ra
;
1214 struct inode
*inode
= mapping
->host
;
1216 unsigned long size
, pgoff
;
1217 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1219 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1222 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1224 goto outside_data_content
;
1226 /* If we don't want any read-ahead, don't bother */
1227 if (VM_RandomReadHint(area
))
1228 goto no_cached_page
;
1231 * The readahead code wants to be told about each and every page
1232 * so it can build and shrink its windows appropriately
1234 * For sequential accesses, we use the generic readahead logic.
1236 if (VM_SequentialReadHint(area
))
1237 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1240 * Do we have something in the page cache already?
1243 page
= find_get_page(mapping
, pgoff
);
1245 unsigned long ra_pages
;
1247 if (VM_SequentialReadHint(area
)) {
1248 handle_ra_miss(mapping
, ra
, pgoff
);
1249 goto no_cached_page
;
1254 * Do we miss much more than hit in this file? If so,
1255 * stop bothering with read-ahead. It will only hurt.
1257 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1258 goto no_cached_page
;
1261 * To keep the pgmajfault counter straight, we need to
1262 * check did_readaround, as this is an inner loop.
1264 if (!did_readaround
) {
1265 majmin
= VM_FAULT_MAJOR
;
1266 inc_page_state(pgmajfault
);
1269 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1273 if (pgoff
> ra_pages
/ 2)
1274 start
= pgoff
- ra_pages
/ 2;
1275 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1277 page
= find_get_page(mapping
, pgoff
);
1279 goto no_cached_page
;
1282 if (!did_readaround
)
1286 * Ok, found a page in the page cache, now we need to check
1287 * that it's up-to-date.
1289 if (!PageUptodate(page
))
1290 goto page_not_uptodate
;
1294 * Found the page and have a reference on it.
1296 mark_page_accessed(page
);
1301 outside_data_content
:
1303 * An external ptracer can access pages that normally aren't
1306 if (area
->vm_mm
== current
->mm
)
1308 /* Fall through to the non-read-ahead case */
1311 * We're only likely to ever get here if MADV_RANDOM is in
1314 error
= page_cache_read(file
, pgoff
);
1318 * The page we want has now been added to the page cache.
1319 * In the unlikely event that someone removed it in the
1320 * meantime, we'll just come back here and read it again.
1326 * An error return from page_cache_read can result if the
1327 * system is low on memory, or a problem occurs while trying
1330 if (error
== -ENOMEM
)
1335 if (!did_readaround
) {
1336 majmin
= VM_FAULT_MAJOR
;
1337 inc_page_state(pgmajfault
);
1341 /* Did it get unhashed while we waited for it? */
1342 if (!page
->mapping
) {
1344 page_cache_release(page
);
1348 /* Did somebody else get it up-to-date? */
1349 if (PageUptodate(page
)) {
1354 error
= mapping
->a_ops
->readpage(file
, page
);
1356 wait_on_page_locked(page
);
1357 if (PageUptodate(page
))
1359 } else if (error
== AOP_TRUNCATED_PAGE
) {
1360 page_cache_release(page
);
1365 * Umm, take care of errors if the page isn't up-to-date.
1366 * Try to re-read it _once_. We do this synchronously,
1367 * because there really aren't any performance issues here
1368 * and we need to check for errors.
1372 /* Somebody truncated the page on us? */
1373 if (!page
->mapping
) {
1375 page_cache_release(page
);
1379 /* Somebody else successfully read it in? */
1380 if (PageUptodate(page
)) {
1384 ClearPageError(page
);
1385 error
= mapping
->a_ops
->readpage(file
, page
);
1387 wait_on_page_locked(page
);
1388 if (PageUptodate(page
))
1390 } else if (error
== AOP_TRUNCATED_PAGE
) {
1391 page_cache_release(page
);
1396 * Things didn't work out. Return zero to tell the
1397 * mm layer so, possibly freeing the page cache page first.
1399 page_cache_release(page
);
1403 EXPORT_SYMBOL(filemap_nopage
);
1405 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1408 struct address_space
*mapping
= file
->f_mapping
;
1413 * Do we have something in the page cache already?
1416 page
= find_get_page(mapping
, pgoff
);
1420 goto no_cached_page
;
1424 * Ok, found a page in the page cache, now we need to check
1425 * that it's up-to-date.
1427 if (!PageUptodate(page
)) {
1429 page_cache_release(page
);
1432 goto page_not_uptodate
;
1437 * Found the page and have a reference on it.
1439 mark_page_accessed(page
);
1443 error
= page_cache_read(file
, pgoff
);
1446 * The page we want has now been added to the page cache.
1447 * In the unlikely event that someone removed it in the
1448 * meantime, we'll just come back here and read it again.
1454 * An error return from page_cache_read can result if the
1455 * system is low on memory, or a problem occurs while trying
1463 /* Did it get unhashed while we waited for it? */
1464 if (!page
->mapping
) {
1469 /* Did somebody else get it up-to-date? */
1470 if (PageUptodate(page
)) {
1475 error
= mapping
->a_ops
->readpage(file
, page
);
1477 wait_on_page_locked(page
);
1478 if (PageUptodate(page
))
1480 } else if (error
== AOP_TRUNCATED_PAGE
) {
1481 page_cache_release(page
);
1486 * Umm, take care of errors if the page isn't up-to-date.
1487 * Try to re-read it _once_. We do this synchronously,
1488 * because there really aren't any performance issues here
1489 * and we need to check for errors.
1493 /* Somebody truncated the page on us? */
1494 if (!page
->mapping
) {
1498 /* Somebody else successfully read it in? */
1499 if (PageUptodate(page
)) {
1504 ClearPageError(page
);
1505 error
= mapping
->a_ops
->readpage(file
, page
);
1507 wait_on_page_locked(page
);
1508 if (PageUptodate(page
))
1510 } else if (error
== AOP_TRUNCATED_PAGE
) {
1511 page_cache_release(page
);
1516 * Things didn't work out. Return zero to tell the
1517 * mm layer so, possibly freeing the page cache page first.
1520 page_cache_release(page
);
1525 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1526 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1529 struct file
*file
= vma
->vm_file
;
1530 struct address_space
*mapping
= file
->f_mapping
;
1531 struct inode
*inode
= mapping
->host
;
1533 struct mm_struct
*mm
= vma
->vm_mm
;
1538 force_page_cache_readahead(mapping
, vma
->vm_file
,
1539 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1542 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1543 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1546 page
= filemap_getpage(file
, pgoff
, nonblock
);
1548 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1549 * done in shmem_populate calling shmem_getpage */
1550 if (!page
&& !nonblock
)
1554 err
= install_page(mm
, vma
, addr
, page
, prot
);
1556 page_cache_release(page
);
1559 } else if (vma
->vm_flags
& VM_NONLINEAR
) {
1560 /* No page was found just because we can't read it in now (being
1561 * here implies nonblock != 0), but the page may exist, so set
1562 * the PTE to fault it in later. */
1563 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1576 EXPORT_SYMBOL(filemap_populate
);
1578 struct vm_operations_struct generic_file_vm_ops
= {
1579 .nopage
= filemap_nopage
,
1580 .populate
= filemap_populate
,
1583 /* This is used for a general mmap of a disk file */
1585 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1587 struct address_space
*mapping
= file
->f_mapping
;
1589 if (!mapping
->a_ops
->readpage
)
1591 file_accessed(file
);
1592 vma
->vm_ops
= &generic_file_vm_ops
;
1597 * This is for filesystems which do not implement ->writepage.
1599 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1601 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1603 return generic_file_mmap(file
, vma
);
1606 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1610 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1614 #endif /* CONFIG_MMU */
1616 EXPORT_SYMBOL(generic_file_mmap
);
1617 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1619 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1620 unsigned long index
,
1621 int (*filler
)(void *,struct page
*),
1624 struct page
*page
, *cached_page
= NULL
;
1627 page
= find_get_page(mapping
, index
);
1630 cached_page
= page_cache_alloc_cold(mapping
);
1632 return ERR_PTR(-ENOMEM
);
1634 err
= add_to_page_cache_lru(cached_page
, mapping
,
1639 /* Presumably ENOMEM for radix tree node */
1640 page_cache_release(cached_page
);
1641 return ERR_PTR(err
);
1645 err
= filler(data
, page
);
1647 page_cache_release(page
);
1648 page
= ERR_PTR(err
);
1652 page_cache_release(cached_page
);
1657 * Read into the page cache. If a page already exists,
1658 * and PageUptodate() is not set, try to fill the page.
1660 struct page
*read_cache_page(struct address_space
*mapping
,
1661 unsigned long index
,
1662 int (*filler
)(void *,struct page
*),
1669 page
= __read_cache_page(mapping
, index
, filler
, data
);
1672 mark_page_accessed(page
);
1673 if (PageUptodate(page
))
1677 if (!page
->mapping
) {
1679 page_cache_release(page
);
1682 if (PageUptodate(page
)) {
1686 err
= filler(data
, page
);
1688 page_cache_release(page
);
1689 page
= ERR_PTR(err
);
1695 EXPORT_SYMBOL(read_cache_page
);
1698 * If the page was newly created, increment its refcount and add it to the
1699 * caller's lru-buffering pagevec. This function is specifically for
1700 * generic_file_write().
1702 static inline struct page
*
1703 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1704 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1709 page
= find_lock_page(mapping
, index
);
1711 if (!*cached_page
) {
1712 *cached_page
= page_cache_alloc(mapping
);
1716 err
= add_to_page_cache(*cached_page
, mapping
,
1721 page
= *cached_page
;
1722 page_cache_get(page
);
1723 if (!pagevec_add(lru_pvec
, page
))
1724 __pagevec_lru_add(lru_pvec
);
1725 *cached_page
= NULL
;
1732 * The logic we want is
1734 * if suid or (sgid and xgrp)
1737 int remove_suid(struct dentry
*dentry
)
1739 mode_t mode
= dentry
->d_inode
->i_mode
;
1743 /* suid always must be killed */
1744 if (unlikely(mode
& S_ISUID
))
1745 kill
= ATTR_KILL_SUID
;
1748 * sgid without any exec bits is just a mandatory locking mark; leave
1749 * it alone. If some exec bits are set, it's a real sgid; kill it.
1751 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1752 kill
|= ATTR_KILL_SGID
;
1754 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1755 struct iattr newattrs
;
1757 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1758 result
= notify_change(dentry
, &newattrs
);
1762 EXPORT_SYMBOL(remove_suid
);
1765 __filemap_copy_from_user_iovec(char *vaddr
,
1766 const struct iovec
*iov
, size_t base
, size_t bytes
)
1768 size_t copied
= 0, left
= 0;
1771 char __user
*buf
= iov
->iov_base
+ base
;
1772 int copy
= min(bytes
, iov
->iov_len
- base
);
1775 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1781 if (unlikely(left
)) {
1782 /* zero the rest of the target like __copy_from_user */
1784 memset(vaddr
, 0, bytes
);
1788 return copied
- left
;
1792 * Performs necessary checks before doing a write
1794 * Can adjust writing position aor amount of bytes to write.
1795 * Returns appropriate error code that caller should return or
1796 * zero in case that write should be allowed.
1798 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1800 struct inode
*inode
= file
->f_mapping
->host
;
1801 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1803 if (unlikely(*pos
< 0))
1807 /* FIXME: this is for backwards compatibility with 2.4 */
1808 if (file
->f_flags
& O_APPEND
)
1809 *pos
= i_size_read(inode
);
1811 if (limit
!= RLIM_INFINITY
) {
1812 if (*pos
>= limit
) {
1813 send_sig(SIGXFSZ
, current
, 0);
1816 if (*count
> limit
- (typeof(limit
))*pos
) {
1817 *count
= limit
- (typeof(limit
))*pos
;
1825 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1826 !(file
->f_flags
& O_LARGEFILE
))) {
1827 if (*pos
>= MAX_NON_LFS
) {
1828 send_sig(SIGXFSZ
, current
, 0);
1831 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1832 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1837 * Are we about to exceed the fs block limit ?
1839 * If we have written data it becomes a short write. If we have
1840 * exceeded without writing data we send a signal and return EFBIG.
1841 * Linus frestrict idea will clean these up nicely..
1843 if (likely(!isblk
)) {
1844 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1845 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1846 send_sig(SIGXFSZ
, current
, 0);
1849 /* zero-length writes at ->s_maxbytes are OK */
1852 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1853 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1856 if (bdev_read_only(I_BDEV(inode
)))
1858 isize
= i_size_read(inode
);
1859 if (*pos
>= isize
) {
1860 if (*count
|| *pos
> isize
)
1864 if (*pos
+ *count
> isize
)
1865 *count
= isize
- *pos
;
1869 EXPORT_SYMBOL(generic_write_checks
);
1872 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1873 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1874 size_t count
, size_t ocount
)
1876 struct file
*file
= iocb
->ki_filp
;
1877 struct address_space
*mapping
= file
->f_mapping
;
1878 struct inode
*inode
= mapping
->host
;
1881 if (count
!= ocount
)
1882 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1884 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1886 loff_t end
= pos
+ written
;
1887 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1888 i_size_write(inode
, end
);
1889 mark_inode_dirty(inode
);
1895 * Sync the fs metadata but not the minor inode changes and
1896 * of course not the data as we did direct DMA for the IO.
1897 * i_mutex is held, which protects generic_osync_inode() from
1900 if (written
>= 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
1901 int err
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1905 if (written
== count
&& !is_sync_kiocb(iocb
))
1906 written
= -EIOCBQUEUED
;
1909 EXPORT_SYMBOL(generic_file_direct_write
);
1912 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1913 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1914 size_t count
, ssize_t written
)
1916 struct file
*file
= iocb
->ki_filp
;
1917 struct address_space
* mapping
= file
->f_mapping
;
1918 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1919 struct inode
*inode
= mapping
->host
;
1922 struct page
*cached_page
= NULL
;
1924 struct pagevec lru_pvec
;
1925 const struct iovec
*cur_iov
= iov
; /* current iovec */
1926 size_t iov_base
= 0; /* offset in the current iovec */
1929 pagevec_init(&lru_pvec
, 0);
1932 * handle partial DIO write. Adjust cur_iov if needed.
1934 if (likely(nr_segs
== 1))
1935 buf
= iov
->iov_base
+ written
;
1937 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1938 buf
= cur_iov
->iov_base
+ iov_base
;
1942 unsigned long index
;
1943 unsigned long offset
;
1944 unsigned long maxlen
;
1947 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1948 index
= pos
>> PAGE_CACHE_SHIFT
;
1949 bytes
= PAGE_CACHE_SIZE
- offset
;
1954 * Bring in the user page that we will copy from _first_.
1955 * Otherwise there's a nasty deadlock on copying from the
1956 * same page as we're writing to, without it being marked
1959 maxlen
= cur_iov
->iov_len
- iov_base
;
1962 fault_in_pages_readable(buf
, maxlen
);
1964 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1970 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1971 if (unlikely(status
)) {
1972 loff_t isize
= i_size_read(inode
);
1974 if (status
!= AOP_TRUNCATED_PAGE
)
1976 page_cache_release(page
);
1977 if (status
== AOP_TRUNCATED_PAGE
)
1980 * prepare_write() may have instantiated a few blocks
1981 * outside i_size. Trim these off again.
1983 if (pos
+ bytes
> isize
)
1984 vmtruncate(inode
, isize
);
1987 if (likely(nr_segs
== 1))
1988 copied
= filemap_copy_from_user(page
, offset
,
1991 copied
= filemap_copy_from_user_iovec(page
, offset
,
1992 cur_iov
, iov_base
, bytes
);
1993 flush_dcache_page(page
);
1994 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
1995 if (status
== AOP_TRUNCATED_PAGE
) {
1996 page_cache_release(page
);
1999 if (likely(copied
> 0)) {
2008 if (unlikely(nr_segs
> 1)) {
2009 filemap_set_next_iovec(&cur_iov
,
2012 buf
= cur_iov
->iov_base
+
2019 if (unlikely(copied
!= bytes
))
2023 mark_page_accessed(page
);
2024 page_cache_release(page
);
2027 balance_dirty_pages_ratelimited(mapping
);
2033 page_cache_release(cached_page
);
2036 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2038 if (likely(status
>= 0)) {
2039 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2040 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2041 status
= generic_osync_inode(inode
, mapping
,
2042 OSYNC_METADATA
|OSYNC_DATA
);
2047 * If we get here for O_DIRECT writes then we must have fallen through
2048 * to buffered writes (block instantiation inside i_size). So we sync
2049 * the file data here, to try to honour O_DIRECT expectations.
2051 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2052 status
= filemap_write_and_wait(mapping
);
2054 pagevec_lru_add(&lru_pvec
);
2055 return written
? written
: status
;
2057 EXPORT_SYMBOL(generic_file_buffered_write
);
2060 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2061 unsigned long nr_segs
, loff_t
*ppos
)
2063 struct file
*file
= iocb
->ki_filp
;
2064 struct address_space
* mapping
= file
->f_mapping
;
2065 size_t ocount
; /* original count */
2066 size_t count
; /* after file limit checks */
2067 struct inode
*inode
= mapping
->host
;
2074 for (seg
= 0; seg
< nr_segs
; seg
++) {
2075 const struct iovec
*iv
= &iov
[seg
];
2078 * If any segment has a negative length, or the cumulative
2079 * length ever wraps negative then return -EINVAL.
2081 ocount
+= iv
->iov_len
;
2082 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2084 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2089 ocount
-= iv
->iov_len
; /* This segment is no good */
2096 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2098 /* We can write back this queue in page reclaim */
2099 current
->backing_dev_info
= mapping
->backing_dev_info
;
2102 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2109 err
= remove_suid(file
->f_dentry
);
2113 file_update_time(file
);
2115 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2116 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2117 written
= generic_file_direct_write(iocb
, iov
,
2118 &nr_segs
, pos
, ppos
, count
, ocount
);
2119 if (written
< 0 || written
== count
)
2122 * direct-io write to a hole: fall through to buffered I/O
2123 * for completing the rest of the request.
2129 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2130 pos
, ppos
, count
, written
);
2132 current
->backing_dev_info
= NULL
;
2133 return written
? written
: err
;
2135 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2138 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2139 unsigned long nr_segs
, loff_t
*ppos
)
2141 struct file
*file
= iocb
->ki_filp
;
2142 struct address_space
*mapping
= file
->f_mapping
;
2143 struct inode
*inode
= mapping
->host
;
2147 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2149 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2152 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2160 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2161 unsigned long nr_segs
, loff_t
*ppos
)
2166 init_sync_kiocb(&kiocb
, file
);
2167 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2168 if (ret
== -EIOCBQUEUED
)
2169 ret
= wait_on_sync_kiocb(&kiocb
);
2174 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2175 unsigned long nr_segs
, loff_t
*ppos
)
2180 init_sync_kiocb(&kiocb
, file
);
2181 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2182 if (-EIOCBQUEUED
== ret
)
2183 ret
= wait_on_sync_kiocb(&kiocb
);
2186 EXPORT_SYMBOL(generic_file_write_nolock
);
2188 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2189 size_t count
, loff_t pos
)
2191 struct file
*file
= iocb
->ki_filp
;
2192 struct address_space
*mapping
= file
->f_mapping
;
2193 struct inode
*inode
= mapping
->host
;
2195 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2198 BUG_ON(iocb
->ki_pos
!= pos
);
2200 mutex_lock(&inode
->i_mutex
);
2201 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2203 mutex_unlock(&inode
->i_mutex
);
2205 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2208 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2214 EXPORT_SYMBOL(generic_file_aio_write
);
2216 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2217 size_t count
, loff_t
*ppos
)
2219 struct address_space
*mapping
= file
->f_mapping
;
2220 struct inode
*inode
= mapping
->host
;
2222 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2225 mutex_lock(&inode
->i_mutex
);
2226 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2227 mutex_unlock(&inode
->i_mutex
);
2229 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2232 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2238 EXPORT_SYMBOL(generic_file_write
);
2240 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2241 unsigned long nr_segs
, loff_t
*ppos
)
2246 init_sync_kiocb(&kiocb
, filp
);
2247 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2248 if (-EIOCBQUEUED
== ret
)
2249 ret
= wait_on_sync_kiocb(&kiocb
);
2252 EXPORT_SYMBOL(generic_file_readv
);
2254 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2255 unsigned long nr_segs
, loff_t
*ppos
)
2257 struct address_space
*mapping
= file
->f_mapping
;
2258 struct inode
*inode
= mapping
->host
;
2261 mutex_lock(&inode
->i_mutex
);
2262 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2263 mutex_unlock(&inode
->i_mutex
);
2265 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2268 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2274 EXPORT_SYMBOL(generic_file_writev
);
2277 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
2278 * went wrong during pagecache shootdown.
2281 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2282 loff_t offset
, unsigned long nr_segs
)
2284 struct file
*file
= iocb
->ki_filp
;
2285 struct address_space
*mapping
= file
->f_mapping
;
2287 size_t write_len
= 0;
2290 * If it's a write, unmap all mmappings of the file up-front. This
2291 * will cause any pte dirty bits to be propagated into the pageframes
2292 * for the subsequent filemap_write_and_wait().
2295 write_len
= iov_length(iov
, nr_segs
);
2296 if (mapping_mapped(mapping
))
2297 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2300 retval
= filemap_write_and_wait(mapping
);
2302 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2304 if (rw
== WRITE
&& mapping
->nrpages
) {
2305 pgoff_t end
= (offset
+ write_len
- 1)
2306 >> PAGE_CACHE_SHIFT
;
2307 int err
= invalidate_inode_pages2_range(mapping
,
2308 offset
>> PAGE_CACHE_SHIFT
, end
);