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 * ->private_lock (page_remove_rmap->set_page_dirty)
98 * ->tree_lock (page_remove_rmap->set_page_dirty)
99 * ->inode_lock (page_remove_rmap->set_page_dirty)
100 * ->inode_lock (zap_pte_range->set_page_dirty)
101 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
104 * ->dcache_lock (proc_pid_lookup)
108 * Remove a page from the page cache and free it. Caller has to make
109 * sure the page is locked and that nobody else uses it - or that usage
110 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
112 void __remove_from_page_cache(struct page
*page
)
114 struct address_space
*mapping
= page
->mapping
;
116 radix_tree_delete(&mapping
->page_tree
, page
->index
);
117 page
->mapping
= NULL
;
122 void remove_from_page_cache(struct page
*page
)
124 struct address_space
*mapping
= page
->mapping
;
126 BUG_ON(!PageLocked(page
));
128 write_lock_irq(&mapping
->tree_lock
);
129 __remove_from_page_cache(page
);
130 write_unlock_irq(&mapping
->tree_lock
);
133 static int sync_page(void *word
)
135 struct address_space
*mapping
;
138 page
= container_of((unsigned long *)word
, struct page
, flags
);
141 * page_mapping() is being called without PG_locked held.
142 * Some knowledge of the state and use of the page is used to
143 * reduce the requirements down to a memory barrier.
144 * The danger here is of a stale page_mapping() return value
145 * indicating a struct address_space different from the one it's
146 * associated with when it is associated with one.
147 * After smp_mb(), it's either the correct page_mapping() for
148 * the page, or an old page_mapping() and the page's own
149 * page_mapping() has gone NULL.
150 * The ->sync_page() address_space operation must tolerate
151 * page_mapping() going NULL. By an amazing coincidence,
152 * this comes about because none of the users of the page
153 * in the ->sync_page() methods make essential use of the
154 * page_mapping(), merely passing the page down to the backing
155 * device's unplug functions when it's non-NULL, which in turn
156 * ignore it for all cases but swap, where only page_private(page) is
157 * of interest. When page_mapping() does go NULL, the entire
158 * call stack gracefully ignores the page and returns.
162 mapping
= page_mapping(page
);
163 if (mapping
&& mapping
->a_ops
&& mapping
->a_ops
->sync_page
)
164 mapping
->a_ops
->sync_page(page
);
170 * filemap_fdatawrite_range - start writeback against all of a mapping's
171 * dirty pages that lie within the byte offsets <start, end>
172 * @mapping: address space structure to write
173 * @start: offset in bytes where the range starts
174 * @end: offset in bytes where the range ends
175 * @sync_mode: enable synchronous operation
177 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
178 * opposed to a regular memory * cleansing writeback. The difference between
179 * these two operations is that if a dirty page/buffer is encountered, it must
180 * be waited upon, and not just skipped over.
182 static int __filemap_fdatawrite_range(struct address_space
*mapping
,
183 loff_t start
, loff_t end
, int sync_mode
)
186 struct writeback_control wbc
= {
187 .sync_mode
= sync_mode
,
188 .nr_to_write
= mapping
->nrpages
* 2,
193 if (!mapping_cap_writeback_dirty(mapping
))
196 ret
= do_writepages(mapping
, &wbc
);
200 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
203 return __filemap_fdatawrite_range(mapping
, 0, 0, sync_mode
);
206 int filemap_fdatawrite(struct address_space
*mapping
)
208 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
210 EXPORT_SYMBOL(filemap_fdatawrite
);
212 static int filemap_fdatawrite_range(struct address_space
*mapping
,
213 loff_t start
, loff_t end
)
215 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
219 * This is a mostly non-blocking flush. Not suitable for data-integrity
220 * purposes - I/O may not be started against all dirty pages.
222 int filemap_flush(struct address_space
*mapping
)
224 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
226 EXPORT_SYMBOL(filemap_flush
);
229 * Wait for writeback to complete against pages indexed by start->end
232 static int wait_on_page_writeback_range(struct address_space
*mapping
,
233 pgoff_t start
, pgoff_t end
)
243 pagevec_init(&pvec
, 0);
245 while ((index
<= end
) &&
246 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
247 PAGECACHE_TAG_WRITEBACK
,
248 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
251 for (i
= 0; i
< nr_pages
; i
++) {
252 struct page
*page
= pvec
.pages
[i
];
254 /* until radix tree lookup accepts end_index */
255 if (page
->index
> end
)
258 wait_on_page_writeback(page
);
262 pagevec_release(&pvec
);
266 /* Check for outstanding write errors */
267 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
269 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
276 * Write and wait upon all the pages in the passed range. This is a "data
277 * integrity" operation. It waits upon in-flight writeout before starting and
278 * waiting upon new writeout. If there was an IO error, return it.
280 * We need to re-take i_mutex during the generic_osync_inode list walk because
281 * it is otherwise livelockable.
283 int sync_page_range(struct inode
*inode
, struct address_space
*mapping
,
284 loff_t pos
, loff_t count
)
286 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
287 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
290 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
292 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
294 mutex_lock(&inode
->i_mutex
);
295 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
296 mutex_unlock(&inode
->i_mutex
);
299 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
302 EXPORT_SYMBOL(sync_page_range
);
305 * Note: Holding i_mutex across sync_page_range_nolock is not a good idea
306 * as it forces O_SYNC writers to different parts of the same file
307 * to be serialised right until io completion.
309 int sync_page_range_nolock(struct inode
*inode
, struct address_space
*mapping
,
310 loff_t pos
, loff_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
);
325 EXPORT_SYMBOL(sync_page_range_nolock
);
328 * filemap_fdatawait - walk the list of under-writeback pages of the given
329 * address space and wait for all of them.
331 * @mapping: address space structure to wait for
333 int filemap_fdatawait(struct address_space
*mapping
)
335 loff_t i_size
= i_size_read(mapping
->host
);
340 return wait_on_page_writeback_range(mapping
, 0,
341 (i_size
- 1) >> PAGE_CACHE_SHIFT
);
343 EXPORT_SYMBOL(filemap_fdatawait
);
345 int filemap_write_and_wait(struct address_space
*mapping
)
349 if (mapping
->nrpages
) {
350 err
= filemap_fdatawrite(mapping
);
352 * Even if the above returned error, the pages may be
353 * written partially (e.g. -ENOSPC), so we wait for it.
354 * But the -EIO is special case, it may indicate the worst
355 * thing (e.g. bug) happened, so we avoid waiting for it.
358 int err2
= filemap_fdatawait(mapping
);
365 EXPORT_SYMBOL(filemap_write_and_wait
);
367 int filemap_write_and_wait_range(struct address_space
*mapping
,
368 loff_t lstart
, loff_t lend
)
372 if (mapping
->nrpages
) {
373 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
375 /* See comment of filemap_write_and_wait() */
377 int err2
= wait_on_page_writeback_range(mapping
,
378 lstart
>> PAGE_CACHE_SHIFT
,
379 lend
>> PAGE_CACHE_SHIFT
);
388 * This function is used to add newly allocated pagecache pages:
389 * the page is new, so we can just run SetPageLocked() against it.
390 * The other page state flags were set by rmqueue().
392 * This function does not add the page to the LRU. The caller must do that.
394 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
395 pgoff_t offset
, gfp_t gfp_mask
)
397 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
400 write_lock_irq(&mapping
->tree_lock
);
401 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
403 page_cache_get(page
);
405 page
->mapping
= mapping
;
406 page
->index
= offset
;
410 write_unlock_irq(&mapping
->tree_lock
);
411 radix_tree_preload_end();
416 EXPORT_SYMBOL(add_to_page_cache
);
418 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
419 pgoff_t offset
, gfp_t gfp_mask
)
421 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
428 * In order to wait for pages to become available there must be
429 * waitqueues associated with pages. By using a hash table of
430 * waitqueues where the bucket discipline is to maintain all
431 * waiters on the same queue and wake all when any of the pages
432 * become available, and for the woken contexts to check to be
433 * sure the appropriate page became available, this saves space
434 * at a cost of "thundering herd" phenomena during rare hash
437 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
439 const struct zone
*zone
= page_zone(page
);
441 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
444 static inline void wake_up_page(struct page
*page
, int bit
)
446 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
449 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
451 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
453 if (test_bit(bit_nr
, &page
->flags
))
454 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
455 TASK_UNINTERRUPTIBLE
);
457 EXPORT_SYMBOL(wait_on_page_bit
);
460 * unlock_page() - unlock a locked page
464 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
465 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
466 * mechananism between PageLocked pages and PageWriteback pages is shared.
467 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
469 * The first mb is necessary to safely close the critical section opened by the
470 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
471 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
472 * parallel wait_on_page_locked()).
474 void fastcall
unlock_page(struct page
*page
)
476 smp_mb__before_clear_bit();
477 if (!TestClearPageLocked(page
))
479 smp_mb__after_clear_bit();
480 wake_up_page(page
, PG_locked
);
482 EXPORT_SYMBOL(unlock_page
);
485 * End writeback against a page.
487 void end_page_writeback(struct page
*page
)
489 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
490 if (!test_clear_page_writeback(page
))
493 smp_mb__after_clear_bit();
494 wake_up_page(page
, PG_writeback
);
496 EXPORT_SYMBOL(end_page_writeback
);
499 * Get a lock on the page, assuming we need to sleep to get it.
501 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
502 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
503 * chances are that on the second loop, the block layer's plug list is empty,
504 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
506 void fastcall
__lock_page(struct page
*page
)
508 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
510 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
511 TASK_UNINTERRUPTIBLE
);
513 EXPORT_SYMBOL(__lock_page
);
516 * a rather lightweight function, finding and getting a reference to a
517 * hashed page atomically.
519 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
523 read_lock_irq(&mapping
->tree_lock
);
524 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
526 page_cache_get(page
);
527 read_unlock_irq(&mapping
->tree_lock
);
531 EXPORT_SYMBOL(find_get_page
);
534 * Same as above, but trylock it instead of incrementing the count.
536 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
540 read_lock_irq(&mapping
->tree_lock
);
541 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
542 if (page
&& TestSetPageLocked(page
))
544 read_unlock_irq(&mapping
->tree_lock
);
548 EXPORT_SYMBOL(find_trylock_page
);
551 * find_lock_page - locate, pin and lock a pagecache page
553 * @mapping: the address_space to search
554 * @offset: the page index
556 * Locates the desired pagecache page, locks it, increments its reference
557 * count and returns its address.
559 * Returns zero if the page was not present. find_lock_page() may sleep.
561 struct page
*find_lock_page(struct address_space
*mapping
,
562 unsigned long offset
)
566 read_lock_irq(&mapping
->tree_lock
);
568 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
570 page_cache_get(page
);
571 if (TestSetPageLocked(page
)) {
572 read_unlock_irq(&mapping
->tree_lock
);
574 read_lock_irq(&mapping
->tree_lock
);
576 /* Has the page been truncated while we slept? */
577 if (unlikely(page
->mapping
!= mapping
||
578 page
->index
!= offset
)) {
580 page_cache_release(page
);
585 read_unlock_irq(&mapping
->tree_lock
);
589 EXPORT_SYMBOL(find_lock_page
);
592 * find_or_create_page - locate or add a pagecache page
594 * @mapping: the page's address_space
595 * @index: the page's index into the mapping
596 * @gfp_mask: page allocation mode
598 * Locates a page in the pagecache. If the page is not present, a new page
599 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
600 * LRU list. The returned page is locked and has its reference count
603 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
606 * find_or_create_page() returns the desired page's address, or zero on
609 struct page
*find_or_create_page(struct address_space
*mapping
,
610 unsigned long index
, gfp_t gfp_mask
)
612 struct page
*page
, *cached_page
= NULL
;
615 page
= find_lock_page(mapping
, index
);
618 cached_page
= alloc_page(gfp_mask
);
622 err
= add_to_page_cache_lru(cached_page
, mapping
,
627 } else if (err
== -EEXIST
)
631 page_cache_release(cached_page
);
635 EXPORT_SYMBOL(find_or_create_page
);
638 * find_get_pages - gang pagecache lookup
639 * @mapping: The address_space to search
640 * @start: The starting page index
641 * @nr_pages: The maximum number of pages
642 * @pages: Where the resulting pages are placed
644 * find_get_pages() will search for and return a group of up to
645 * @nr_pages pages in the mapping. The pages are placed at @pages.
646 * find_get_pages() takes a reference against the returned pages.
648 * The search returns a group of mapping-contiguous pages with ascending
649 * indexes. There may be holes in the indices due to not-present pages.
651 * find_get_pages() returns the number of pages which were found.
653 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
654 unsigned int nr_pages
, struct page
**pages
)
659 read_lock_irq(&mapping
->tree_lock
);
660 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
661 (void **)pages
, start
, nr_pages
);
662 for (i
= 0; i
< ret
; i
++)
663 page_cache_get(pages
[i
]);
664 read_unlock_irq(&mapping
->tree_lock
);
669 * Like find_get_pages, except we only return pages which are tagged with
670 * `tag'. We update *index to index the next page for the traversal.
672 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
673 int tag
, unsigned int nr_pages
, struct page
**pages
)
678 read_lock_irq(&mapping
->tree_lock
);
679 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
680 (void **)pages
, *index
, nr_pages
, tag
);
681 for (i
= 0; i
< ret
; i
++)
682 page_cache_get(pages
[i
]);
684 *index
= pages
[ret
- 1]->index
+ 1;
685 read_unlock_irq(&mapping
->tree_lock
);
690 * Same as grab_cache_page, but do not wait if the page is unavailable.
691 * This is intended for speculative data generators, where the data can
692 * be regenerated if the page couldn't be grabbed. This routine should
693 * be safe to call while holding the lock for another page.
695 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
696 * and deadlock against the caller's locked page.
699 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
701 struct page
*page
= find_get_page(mapping
, index
);
705 if (!TestSetPageLocked(page
))
707 page_cache_release(page
);
710 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
711 page
= alloc_pages(gfp_mask
, 0);
712 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
713 page_cache_release(page
);
719 EXPORT_SYMBOL(grab_cache_page_nowait
);
722 * This is a generic file read routine, and uses the
723 * mapping->a_ops->readpage() function for the actual low-level
726 * This is really ugly. But the goto's actually try to clarify some
727 * of the logic when it comes to error handling etc.
729 * Note the struct file* is only passed for the use of readpage. It may be
732 void do_generic_mapping_read(struct address_space
*mapping
,
733 struct file_ra_state
*_ra
,
736 read_descriptor_t
*desc
,
739 struct inode
*inode
= mapping
->host
;
741 unsigned long end_index
;
742 unsigned long offset
;
743 unsigned long last_index
;
744 unsigned long next_index
;
745 unsigned long prev_index
;
747 struct page
*cached_page
;
749 struct file_ra_state ra
= *_ra
;
752 index
= *ppos
>> PAGE_CACHE_SHIFT
;
754 prev_index
= ra
.prev_page
;
755 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
756 offset
= *ppos
& ~PAGE_CACHE_MASK
;
758 isize
= i_size_read(inode
);
762 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
765 unsigned long nr
, ret
;
767 /* nr is the maximum number of bytes to copy from this page */
768 nr
= PAGE_CACHE_SIZE
;
769 if (index
>= end_index
) {
770 if (index
> end_index
)
772 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
780 if (index
== next_index
)
781 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
782 index
, last_index
- index
);
785 page
= find_get_page(mapping
, index
);
786 if (unlikely(page
== NULL
)) {
787 handle_ra_miss(mapping
, &ra
, index
);
790 if (!PageUptodate(page
))
791 goto page_not_up_to_date
;
794 /* If users can be writing to this page using arbitrary
795 * virtual addresses, take care about potential aliasing
796 * before reading the page on the kernel side.
798 if (mapping_writably_mapped(mapping
))
799 flush_dcache_page(page
);
802 * When (part of) the same page is read multiple times
803 * in succession, only mark it as accessed the first time.
805 if (prev_index
!= index
)
806 mark_page_accessed(page
);
810 * Ok, we have the page, and it's up-to-date, so
811 * now we can copy it to user space...
813 * The actor routine returns how many bytes were actually used..
814 * NOTE! This may not be the same as how much of a user buffer
815 * we filled up (we may be padding etc), so we can only update
816 * "pos" here (the actor routine has to update the user buffer
817 * pointers and the remaining count).
819 ret
= actor(desc
, page
, offset
, nr
);
821 index
+= offset
>> PAGE_CACHE_SHIFT
;
822 offset
&= ~PAGE_CACHE_MASK
;
824 page_cache_release(page
);
825 if (ret
== nr
&& desc
->count
)
830 /* Get exclusive access to the page ... */
833 /* Did it get unhashed before we got the lock? */
834 if (!page
->mapping
) {
836 page_cache_release(page
);
840 /* Did somebody else fill it already? */
841 if (PageUptodate(page
)) {
847 /* Start the actual read. The read will unlock the page. */
848 error
= mapping
->a_ops
->readpage(filp
, page
);
850 if (unlikely(error
)) {
851 if (error
== AOP_TRUNCATED_PAGE
) {
852 page_cache_release(page
);
858 if (!PageUptodate(page
)) {
860 if (!PageUptodate(page
)) {
861 if (page
->mapping
== NULL
) {
863 * invalidate_inode_pages got it
866 page_cache_release(page
);
877 * i_size must be checked after we have done ->readpage.
879 * Checking i_size after the readpage allows us to calculate
880 * the correct value for "nr", which means the zero-filled
881 * part of the page is not copied back to userspace (unless
882 * another truncate extends the file - this is desired though).
884 isize
= i_size_read(inode
);
885 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
886 if (unlikely(!isize
|| index
> end_index
)) {
887 page_cache_release(page
);
891 /* nr is the maximum number of bytes to copy from this page */
892 nr
= PAGE_CACHE_SIZE
;
893 if (index
== end_index
) {
894 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
896 page_cache_release(page
);
904 /* UHHUH! A synchronous read error occurred. Report it */
906 page_cache_release(page
);
911 * Ok, it wasn't cached, so we need to create a new
915 cached_page
= page_cache_alloc_cold(mapping
);
917 desc
->error
= -ENOMEM
;
921 error
= add_to_page_cache_lru(cached_page
, mapping
,
924 if (error
== -EEXIST
)
937 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
939 page_cache_release(cached_page
);
944 EXPORT_SYMBOL(do_generic_mapping_read
);
946 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
947 unsigned long offset
, unsigned long size
)
950 unsigned long left
, count
= desc
->count
;
956 * Faults on the destination of a read are common, so do it before
959 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
960 kaddr
= kmap_atomic(page
, KM_USER0
);
961 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
962 kaddr
+ offset
, size
);
963 kunmap_atomic(kaddr
, KM_USER0
);
968 /* Do it the slow way */
970 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
975 desc
->error
= -EFAULT
;
978 desc
->count
= count
- size
;
979 desc
->written
+= size
;
980 desc
->arg
.buf
+= size
;
985 * This is the "read()" routine for all filesystems
986 * that can use the page cache directly.
989 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
990 unsigned long nr_segs
, loff_t
*ppos
)
992 struct file
*filp
= iocb
->ki_filp
;
998 for (seg
= 0; seg
< nr_segs
; seg
++) {
999 const struct iovec
*iv
= &iov
[seg
];
1002 * If any segment has a negative length, or the cumulative
1003 * length ever wraps negative then return -EINVAL.
1005 count
+= iv
->iov_len
;
1006 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1008 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1013 count
-= iv
->iov_len
; /* This segment is no good */
1017 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1018 if (filp
->f_flags
& O_DIRECT
) {
1019 loff_t pos
= *ppos
, size
;
1020 struct address_space
*mapping
;
1021 struct inode
*inode
;
1023 mapping
= filp
->f_mapping
;
1024 inode
= mapping
->host
;
1027 goto out
; /* skip atime */
1028 size
= i_size_read(inode
);
1030 retval
= generic_file_direct_IO(READ
, iocb
,
1032 if (retval
> 0 && !is_sync_kiocb(iocb
))
1033 retval
= -EIOCBQUEUED
;
1035 *ppos
= pos
+ retval
;
1037 file_accessed(filp
);
1043 for (seg
= 0; seg
< nr_segs
; seg
++) {
1044 read_descriptor_t desc
;
1047 desc
.arg
.buf
= iov
[seg
].iov_base
;
1048 desc
.count
= iov
[seg
].iov_len
;
1049 if (desc
.count
== 0)
1052 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1053 retval
+= desc
.written
;
1055 retval
= retval
?: desc
.error
;
1064 EXPORT_SYMBOL(__generic_file_aio_read
);
1067 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1069 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1071 BUG_ON(iocb
->ki_pos
!= pos
);
1072 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1075 EXPORT_SYMBOL(generic_file_aio_read
);
1078 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1080 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1084 init_sync_kiocb(&kiocb
, filp
);
1085 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1086 if (-EIOCBQUEUED
== ret
)
1087 ret
= wait_on_sync_kiocb(&kiocb
);
1091 EXPORT_SYMBOL(generic_file_read
);
1093 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1096 unsigned long count
= desc
->count
;
1097 struct file
*file
= desc
->arg
.data
;
1102 written
= file
->f_op
->sendpage(file
, page
, offset
,
1103 size
, &file
->f_pos
, size
<count
);
1105 desc
->error
= written
;
1108 desc
->count
= count
- written
;
1109 desc
->written
+= written
;
1113 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1114 size_t count
, read_actor_t actor
, void *target
)
1116 read_descriptor_t desc
;
1123 desc
.arg
.data
= target
;
1126 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1128 return desc
.written
;
1132 EXPORT_SYMBOL(generic_file_sendfile
);
1135 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1136 unsigned long index
, unsigned long nr
)
1138 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1141 force_page_cache_readahead(mapping
, filp
, index
,
1142 max_sane_readahead(nr
));
1146 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1154 if (file
->f_mode
& FMODE_READ
) {
1155 struct address_space
*mapping
= file
->f_mapping
;
1156 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1157 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1158 unsigned long len
= end
- start
+ 1;
1159 ret
= do_readahead(mapping
, file
, start
, len
);
1168 * This adds the requested page to the page cache if it isn't already there,
1169 * and schedules an I/O to read in its contents from disk.
1171 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1172 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1174 struct address_space
*mapping
= file
->f_mapping
;
1179 page
= page_cache_alloc_cold(mapping
);
1183 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1185 ret
= mapping
->a_ops
->readpage(file
, page
);
1186 else if (ret
== -EEXIST
)
1187 ret
= 0; /* losing race to add is OK */
1189 page_cache_release(page
);
1191 } while (ret
== AOP_TRUNCATED_PAGE
);
1196 #define MMAP_LOTSAMISS (100)
1199 * filemap_nopage() is invoked via the vma operations vector for a
1200 * mapped memory region to read in file data during a page fault.
1202 * The goto's are kind of ugly, but this streamlines the normal case of having
1203 * it in the page cache, and handles the special cases reasonably without
1204 * having a lot of duplicated code.
1206 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1207 unsigned long address
, int *type
)
1210 struct file
*file
= area
->vm_file
;
1211 struct address_space
*mapping
= file
->f_mapping
;
1212 struct file_ra_state
*ra
= &file
->f_ra
;
1213 struct inode
*inode
= mapping
->host
;
1215 unsigned long size
, pgoff
;
1216 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1218 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1221 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1223 goto outside_data_content
;
1225 /* If we don't want any read-ahead, don't bother */
1226 if (VM_RandomReadHint(area
))
1227 goto no_cached_page
;
1230 * The readahead code wants to be told about each and every page
1231 * so it can build and shrink its windows appropriately
1233 * For sequential accesses, we use the generic readahead logic.
1235 if (VM_SequentialReadHint(area
))
1236 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1239 * Do we have something in the page cache already?
1242 page
= find_get_page(mapping
, pgoff
);
1244 unsigned long ra_pages
;
1246 if (VM_SequentialReadHint(area
)) {
1247 handle_ra_miss(mapping
, ra
, pgoff
);
1248 goto no_cached_page
;
1253 * Do we miss much more than hit in this file? If so,
1254 * stop bothering with read-ahead. It will only hurt.
1256 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1257 goto no_cached_page
;
1260 * To keep the pgmajfault counter straight, we need to
1261 * check did_readaround, as this is an inner loop.
1263 if (!did_readaround
) {
1264 majmin
= VM_FAULT_MAJOR
;
1265 inc_page_state(pgmajfault
);
1268 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1272 if (pgoff
> ra_pages
/ 2)
1273 start
= pgoff
- ra_pages
/ 2;
1274 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1276 page
= find_get_page(mapping
, pgoff
);
1278 goto no_cached_page
;
1281 if (!did_readaround
)
1285 * Ok, found a page in the page cache, now we need to check
1286 * that it's up-to-date.
1288 if (!PageUptodate(page
))
1289 goto page_not_uptodate
;
1293 * Found the page and have a reference on it.
1295 mark_page_accessed(page
);
1300 outside_data_content
:
1302 * An external ptracer can access pages that normally aren't
1305 if (area
->vm_mm
== current
->mm
)
1307 /* Fall through to the non-read-ahead case */
1310 * We're only likely to ever get here if MADV_RANDOM is in
1313 error
= page_cache_read(file
, pgoff
);
1317 * The page we want has now been added to the page cache.
1318 * In the unlikely event that someone removed it in the
1319 * meantime, we'll just come back here and read it again.
1325 * An error return from page_cache_read can result if the
1326 * system is low on memory, or a problem occurs while trying
1329 if (error
== -ENOMEM
)
1334 if (!did_readaround
) {
1335 majmin
= VM_FAULT_MAJOR
;
1336 inc_page_state(pgmajfault
);
1340 /* Did it get unhashed while we waited for it? */
1341 if (!page
->mapping
) {
1343 page_cache_release(page
);
1347 /* Did somebody else get it up-to-date? */
1348 if (PageUptodate(page
)) {
1353 error
= mapping
->a_ops
->readpage(file
, page
);
1355 wait_on_page_locked(page
);
1356 if (PageUptodate(page
))
1358 } else if (error
== AOP_TRUNCATED_PAGE
) {
1359 page_cache_release(page
);
1364 * Umm, take care of errors if the page isn't up-to-date.
1365 * Try to re-read it _once_. We do this synchronously,
1366 * because there really aren't any performance issues here
1367 * and we need to check for errors.
1371 /* Somebody truncated the page on us? */
1372 if (!page
->mapping
) {
1374 page_cache_release(page
);
1378 /* Somebody else successfully read it in? */
1379 if (PageUptodate(page
)) {
1383 ClearPageError(page
);
1384 error
= mapping
->a_ops
->readpage(file
, page
);
1386 wait_on_page_locked(page
);
1387 if (PageUptodate(page
))
1389 } else if (error
== AOP_TRUNCATED_PAGE
) {
1390 page_cache_release(page
);
1395 * Things didn't work out. Return zero to tell the
1396 * mm layer so, possibly freeing the page cache page first.
1398 page_cache_release(page
);
1402 EXPORT_SYMBOL(filemap_nopage
);
1404 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1407 struct address_space
*mapping
= file
->f_mapping
;
1412 * Do we have something in the page cache already?
1415 page
= find_get_page(mapping
, pgoff
);
1419 goto no_cached_page
;
1423 * Ok, found a page in the page cache, now we need to check
1424 * that it's up-to-date.
1426 if (!PageUptodate(page
)) {
1428 page_cache_release(page
);
1431 goto page_not_uptodate
;
1436 * Found the page and have a reference on it.
1438 mark_page_accessed(page
);
1442 error
= page_cache_read(file
, pgoff
);
1445 * The page we want has now been added to the page cache.
1446 * In the unlikely event that someone removed it in the
1447 * meantime, we'll just come back here and read it again.
1453 * An error return from page_cache_read can result if the
1454 * system is low on memory, or a problem occurs while trying
1462 /* Did it get unhashed while we waited for it? */
1463 if (!page
->mapping
) {
1468 /* Did somebody else get it up-to-date? */
1469 if (PageUptodate(page
)) {
1474 error
= mapping
->a_ops
->readpage(file
, page
);
1476 wait_on_page_locked(page
);
1477 if (PageUptodate(page
))
1479 } else if (error
== AOP_TRUNCATED_PAGE
) {
1480 page_cache_release(page
);
1485 * Umm, take care of errors if the page isn't up-to-date.
1486 * Try to re-read it _once_. We do this synchronously,
1487 * because there really aren't any performance issues here
1488 * and we need to check for errors.
1492 /* Somebody truncated the page on us? */
1493 if (!page
->mapping
) {
1497 /* Somebody else successfully read it in? */
1498 if (PageUptodate(page
)) {
1503 ClearPageError(page
);
1504 error
= mapping
->a_ops
->readpage(file
, page
);
1506 wait_on_page_locked(page
);
1507 if (PageUptodate(page
))
1509 } else if (error
== AOP_TRUNCATED_PAGE
) {
1510 page_cache_release(page
);
1515 * Things didn't work out. Return zero to tell the
1516 * mm layer so, possibly freeing the page cache page first.
1519 page_cache_release(page
);
1524 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1525 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1528 struct file
*file
= vma
->vm_file
;
1529 struct address_space
*mapping
= file
->f_mapping
;
1530 struct inode
*inode
= mapping
->host
;
1532 struct mm_struct
*mm
= vma
->vm_mm
;
1537 force_page_cache_readahead(mapping
, vma
->vm_file
,
1538 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1541 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1542 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1545 page
= filemap_getpage(file
, pgoff
, nonblock
);
1547 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1548 * done in shmem_populate calling shmem_getpage */
1549 if (!page
&& !nonblock
)
1553 err
= install_page(mm
, vma
, addr
, page
, prot
);
1555 page_cache_release(page
);
1558 } else if (vma
->vm_flags
& VM_NONLINEAR
) {
1559 /* No page was found just because we can't read it in now (being
1560 * here implies nonblock != 0), but the page may exist, so set
1561 * the PTE to fault it in later. */
1562 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1575 EXPORT_SYMBOL(filemap_populate
);
1577 struct vm_operations_struct generic_file_vm_ops
= {
1578 .nopage
= filemap_nopage
,
1579 .populate
= filemap_populate
,
1582 /* This is used for a general mmap of a disk file */
1584 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1586 struct address_space
*mapping
= file
->f_mapping
;
1588 if (!mapping
->a_ops
->readpage
)
1590 file_accessed(file
);
1591 vma
->vm_ops
= &generic_file_vm_ops
;
1596 * This is for filesystems which do not implement ->writepage.
1598 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1600 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1602 return generic_file_mmap(file
, vma
);
1605 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1609 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1613 #endif /* CONFIG_MMU */
1615 EXPORT_SYMBOL(generic_file_mmap
);
1616 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1618 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1619 unsigned long index
,
1620 int (*filler
)(void *,struct page
*),
1623 struct page
*page
, *cached_page
= NULL
;
1626 page
= find_get_page(mapping
, index
);
1629 cached_page
= page_cache_alloc_cold(mapping
);
1631 return ERR_PTR(-ENOMEM
);
1633 err
= add_to_page_cache_lru(cached_page
, mapping
,
1638 /* Presumably ENOMEM for radix tree node */
1639 page_cache_release(cached_page
);
1640 return ERR_PTR(err
);
1644 err
= filler(data
, page
);
1646 page_cache_release(page
);
1647 page
= ERR_PTR(err
);
1651 page_cache_release(cached_page
);
1656 * Read into the page cache. If a page already exists,
1657 * and PageUptodate() is not set, try to fill the page.
1659 struct page
*read_cache_page(struct address_space
*mapping
,
1660 unsigned long index
,
1661 int (*filler
)(void *,struct page
*),
1668 page
= __read_cache_page(mapping
, index
, filler
, data
);
1671 mark_page_accessed(page
);
1672 if (PageUptodate(page
))
1676 if (!page
->mapping
) {
1678 page_cache_release(page
);
1681 if (PageUptodate(page
)) {
1685 err
= filler(data
, page
);
1687 page_cache_release(page
);
1688 page
= ERR_PTR(err
);
1694 EXPORT_SYMBOL(read_cache_page
);
1697 * If the page was newly created, increment its refcount and add it to the
1698 * caller's lru-buffering pagevec. This function is specifically for
1699 * generic_file_write().
1701 static inline struct page
*
1702 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1703 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1708 page
= find_lock_page(mapping
, index
);
1710 if (!*cached_page
) {
1711 *cached_page
= page_cache_alloc(mapping
);
1715 err
= add_to_page_cache(*cached_page
, mapping
,
1720 page
= *cached_page
;
1721 page_cache_get(page
);
1722 if (!pagevec_add(lru_pvec
, page
))
1723 __pagevec_lru_add(lru_pvec
);
1724 *cached_page
= NULL
;
1731 * The logic we want is
1733 * if suid or (sgid and xgrp)
1736 int remove_suid(struct dentry
*dentry
)
1738 mode_t mode
= dentry
->d_inode
->i_mode
;
1742 /* suid always must be killed */
1743 if (unlikely(mode
& S_ISUID
))
1744 kill
= ATTR_KILL_SUID
;
1747 * sgid without any exec bits is just a mandatory locking mark; leave
1748 * it alone. If some exec bits are set, it's a real sgid; kill it.
1750 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1751 kill
|= ATTR_KILL_SGID
;
1753 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1754 struct iattr newattrs
;
1756 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1757 result
= notify_change(dentry
, &newattrs
);
1761 EXPORT_SYMBOL(remove_suid
);
1764 __filemap_copy_from_user_iovec(char *vaddr
,
1765 const struct iovec
*iov
, size_t base
, size_t bytes
)
1767 size_t copied
= 0, left
= 0;
1770 char __user
*buf
= iov
->iov_base
+ base
;
1771 int copy
= min(bytes
, iov
->iov_len
- base
);
1774 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1780 if (unlikely(left
)) {
1781 /* zero the rest of the target like __copy_from_user */
1783 memset(vaddr
, 0, bytes
);
1787 return copied
- left
;
1791 * Performs necessary checks before doing a write
1793 * Can adjust writing position aor amount of bytes to write.
1794 * Returns appropriate error code that caller should return or
1795 * zero in case that write should be allowed.
1797 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1799 struct inode
*inode
= file
->f_mapping
->host
;
1800 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1802 if (unlikely(*pos
< 0))
1806 /* FIXME: this is for backwards compatibility with 2.4 */
1807 if (file
->f_flags
& O_APPEND
)
1808 *pos
= i_size_read(inode
);
1810 if (limit
!= RLIM_INFINITY
) {
1811 if (*pos
>= limit
) {
1812 send_sig(SIGXFSZ
, current
, 0);
1815 if (*count
> limit
- (typeof(limit
))*pos
) {
1816 *count
= limit
- (typeof(limit
))*pos
;
1824 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1825 !(file
->f_flags
& O_LARGEFILE
))) {
1826 if (*pos
>= MAX_NON_LFS
) {
1827 send_sig(SIGXFSZ
, current
, 0);
1830 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1831 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1836 * Are we about to exceed the fs block limit ?
1838 * If we have written data it becomes a short write. If we have
1839 * exceeded without writing data we send a signal and return EFBIG.
1840 * Linus frestrict idea will clean these up nicely..
1842 if (likely(!isblk
)) {
1843 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1844 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1845 send_sig(SIGXFSZ
, current
, 0);
1848 /* zero-length writes at ->s_maxbytes are OK */
1851 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1852 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1855 if (bdev_read_only(I_BDEV(inode
)))
1857 isize
= i_size_read(inode
);
1858 if (*pos
>= isize
) {
1859 if (*count
|| *pos
> isize
)
1863 if (*pos
+ *count
> isize
)
1864 *count
= isize
- *pos
;
1868 EXPORT_SYMBOL(generic_write_checks
);
1871 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1872 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1873 size_t count
, size_t ocount
)
1875 struct file
*file
= iocb
->ki_filp
;
1876 struct address_space
*mapping
= file
->f_mapping
;
1877 struct inode
*inode
= mapping
->host
;
1880 if (count
!= ocount
)
1881 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1883 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1885 loff_t end
= pos
+ written
;
1886 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1887 i_size_write(inode
, end
);
1888 mark_inode_dirty(inode
);
1894 * Sync the fs metadata but not the minor inode changes and
1895 * of course not the data as we did direct DMA for the IO.
1896 * i_mutex is held, which protects generic_osync_inode() from
1899 if (written
>= 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
1900 int err
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1904 if (written
== count
&& !is_sync_kiocb(iocb
))
1905 written
= -EIOCBQUEUED
;
1908 EXPORT_SYMBOL(generic_file_direct_write
);
1911 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1912 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1913 size_t count
, ssize_t written
)
1915 struct file
*file
= iocb
->ki_filp
;
1916 struct address_space
* mapping
= file
->f_mapping
;
1917 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1918 struct inode
*inode
= mapping
->host
;
1921 struct page
*cached_page
= NULL
;
1923 struct pagevec lru_pvec
;
1924 const struct iovec
*cur_iov
= iov
; /* current iovec */
1925 size_t iov_base
= 0; /* offset in the current iovec */
1928 pagevec_init(&lru_pvec
, 0);
1931 * handle partial DIO write. Adjust cur_iov if needed.
1933 if (likely(nr_segs
== 1))
1934 buf
= iov
->iov_base
+ written
;
1936 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1937 buf
= cur_iov
->iov_base
+ iov_base
;
1941 unsigned long index
;
1942 unsigned long offset
;
1943 unsigned long maxlen
;
1946 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1947 index
= pos
>> PAGE_CACHE_SHIFT
;
1948 bytes
= PAGE_CACHE_SIZE
- offset
;
1953 * Bring in the user page that we will copy from _first_.
1954 * Otherwise there's a nasty deadlock on copying from the
1955 * same page as we're writing to, without it being marked
1958 maxlen
= cur_iov
->iov_len
- iov_base
;
1961 fault_in_pages_readable(buf
, maxlen
);
1963 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1969 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1970 if (unlikely(status
)) {
1971 loff_t isize
= i_size_read(inode
);
1973 if (status
!= AOP_TRUNCATED_PAGE
)
1975 page_cache_release(page
);
1976 if (status
== AOP_TRUNCATED_PAGE
)
1979 * prepare_write() may have instantiated a few blocks
1980 * outside i_size. Trim these off again.
1982 if (pos
+ bytes
> isize
)
1983 vmtruncate(inode
, isize
);
1986 if (likely(nr_segs
== 1))
1987 copied
= filemap_copy_from_user(page
, offset
,
1990 copied
= filemap_copy_from_user_iovec(page
, offset
,
1991 cur_iov
, iov_base
, bytes
);
1992 flush_dcache_page(page
);
1993 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
1994 if (status
== AOP_TRUNCATED_PAGE
) {
1995 page_cache_release(page
);
1998 if (likely(copied
> 0)) {
2007 if (unlikely(nr_segs
> 1)) {
2008 filemap_set_next_iovec(&cur_iov
,
2011 buf
= cur_iov
->iov_base
+
2018 if (unlikely(copied
!= bytes
))
2022 mark_page_accessed(page
);
2023 page_cache_release(page
);
2026 balance_dirty_pages_ratelimited(mapping
);
2032 page_cache_release(cached_page
);
2035 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2037 if (likely(status
>= 0)) {
2038 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2039 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2040 status
= generic_osync_inode(inode
, mapping
,
2041 OSYNC_METADATA
|OSYNC_DATA
);
2046 * If we get here for O_DIRECT writes then we must have fallen through
2047 * to buffered writes (block instantiation inside i_size). So we sync
2048 * the file data here, to try to honour O_DIRECT expectations.
2050 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2051 status
= filemap_write_and_wait(mapping
);
2053 pagevec_lru_add(&lru_pvec
);
2054 return written
? written
: status
;
2056 EXPORT_SYMBOL(generic_file_buffered_write
);
2059 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2060 unsigned long nr_segs
, loff_t
*ppos
)
2062 struct file
*file
= iocb
->ki_filp
;
2063 struct address_space
* mapping
= file
->f_mapping
;
2064 size_t ocount
; /* original count */
2065 size_t count
; /* after file limit checks */
2066 struct inode
*inode
= mapping
->host
;
2073 for (seg
= 0; seg
< nr_segs
; seg
++) {
2074 const struct iovec
*iv
= &iov
[seg
];
2077 * If any segment has a negative length, or the cumulative
2078 * length ever wraps negative then return -EINVAL.
2080 ocount
+= iv
->iov_len
;
2081 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2083 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2088 ocount
-= iv
->iov_len
; /* This segment is no good */
2095 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2097 /* We can write back this queue in page reclaim */
2098 current
->backing_dev_info
= mapping
->backing_dev_info
;
2101 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2108 err
= remove_suid(file
->f_dentry
);
2112 file_update_time(file
);
2114 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2115 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2116 written
= generic_file_direct_write(iocb
, iov
,
2117 &nr_segs
, pos
, ppos
, count
, ocount
);
2118 if (written
< 0 || written
== count
)
2121 * direct-io write to a hole: fall through to buffered I/O
2122 * for completing the rest of the request.
2128 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2129 pos
, ppos
, count
, written
);
2131 current
->backing_dev_info
= NULL
;
2132 return written
? written
: err
;
2134 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2137 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2138 unsigned long nr_segs
, loff_t
*ppos
)
2140 struct file
*file
= iocb
->ki_filp
;
2141 struct address_space
*mapping
= file
->f_mapping
;
2142 struct inode
*inode
= mapping
->host
;
2146 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2148 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2151 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2159 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2160 unsigned long nr_segs
, loff_t
*ppos
)
2165 init_sync_kiocb(&kiocb
, file
);
2166 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2167 if (ret
== -EIOCBQUEUED
)
2168 ret
= wait_on_sync_kiocb(&kiocb
);
2173 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2174 unsigned long nr_segs
, loff_t
*ppos
)
2179 init_sync_kiocb(&kiocb
, file
);
2180 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2181 if (-EIOCBQUEUED
== ret
)
2182 ret
= wait_on_sync_kiocb(&kiocb
);
2185 EXPORT_SYMBOL(generic_file_write_nolock
);
2187 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2188 size_t count
, loff_t pos
)
2190 struct file
*file
= iocb
->ki_filp
;
2191 struct address_space
*mapping
= file
->f_mapping
;
2192 struct inode
*inode
= mapping
->host
;
2194 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2197 BUG_ON(iocb
->ki_pos
!= pos
);
2199 mutex_lock(&inode
->i_mutex
);
2200 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2202 mutex_unlock(&inode
->i_mutex
);
2204 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2207 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2213 EXPORT_SYMBOL(generic_file_aio_write
);
2215 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2216 size_t count
, loff_t
*ppos
)
2218 struct address_space
*mapping
= file
->f_mapping
;
2219 struct inode
*inode
= mapping
->host
;
2221 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2224 mutex_lock(&inode
->i_mutex
);
2225 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2226 mutex_unlock(&inode
->i_mutex
);
2228 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2231 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2237 EXPORT_SYMBOL(generic_file_write
);
2239 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2240 unsigned long nr_segs
, loff_t
*ppos
)
2245 init_sync_kiocb(&kiocb
, filp
);
2246 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2247 if (-EIOCBQUEUED
== ret
)
2248 ret
= wait_on_sync_kiocb(&kiocb
);
2251 EXPORT_SYMBOL(generic_file_readv
);
2253 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2254 unsigned long nr_segs
, loff_t
*ppos
)
2256 struct address_space
*mapping
= file
->f_mapping
;
2257 struct inode
*inode
= mapping
->host
;
2260 mutex_lock(&inode
->i_mutex
);
2261 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2262 mutex_unlock(&inode
->i_mutex
);
2264 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2267 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2273 EXPORT_SYMBOL(generic_file_writev
);
2276 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
2277 * went wrong during pagecache shootdown.
2280 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2281 loff_t offset
, unsigned long nr_segs
)
2283 struct file
*file
= iocb
->ki_filp
;
2284 struct address_space
*mapping
= file
->f_mapping
;
2286 size_t write_len
= 0;
2289 * If it's a write, unmap all mmappings of the file up-front. This
2290 * will cause any pte dirty bits to be propagated into the pageframes
2291 * for the subsequent filemap_write_and_wait().
2294 write_len
= iov_length(iov
, nr_segs
);
2295 if (mapping_mapped(mapping
))
2296 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2299 retval
= filemap_write_and_wait(mapping
);
2301 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2303 if (rw
== WRITE
&& mapping
->nrpages
) {
2304 pgoff_t end
= (offset
+ write_len
- 1)
2305 >> PAGE_CACHE_SHIFT
;
2306 int err
= invalidate_inode_pages2_range(mapping
,
2307 offset
>> PAGE_CACHE_SHIFT
, end
);