4 * Copyright (C) 1994-1999 Linus Torvalds
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/compiler.h>
17 #include <linux/aio.h>
18 #include <linux/kernel_stat.h>
20 #include <linux/swap.h>
21 #include <linux/mman.h>
22 #include <linux/pagemap.h>
23 #include <linux/file.h>
24 #include <linux/uio.h>
25 #include <linux/hash.h>
26 #include <linux/writeback.h>
27 #include <linux/pagevec.h>
28 #include <linux/blkdev.h>
29 #include <linux/security.h>
30 #include <linux/syscalls.h>
32 * This is needed for the following functions:
33 * - try_to_release_page
34 * - block_invalidatepage
35 * - generic_osync_inode
37 * FIXME: remove all knowledge of the buffer layer from the core VM
39 #include <linux/buffer_head.h> /* for generic_osync_inode */
41 #include <asm/uaccess.h>
45 * Shared mappings implemented 30.11.1994. It's not fully working yet,
48 * Shared mappings now work. 15.8.1995 Bruno.
50 * finished 'unifying' the page and buffer cache and SMP-threaded the
51 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
53 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
59 * ->i_mmap_lock (vmtruncate)
60 * ->private_lock (__free_pte->__set_page_dirty_buffers)
62 * ->swap_device_lock (exclusive_swap_page, others)
63 * ->mapping->tree_lock
66 * ->i_mmap_lock (truncate->unmap_mapping_range)
70 * ->page_table_lock (various places, mainly in mmap.c)
71 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
74 * ->lock_page (access_process_vm)
80 * ->i_alloc_sem (various)
83 * ->sb_lock (fs/fs-writeback.c)
84 * ->mapping->tree_lock (__sync_single_inode)
87 * ->anon_vma.lock (vma_adjust)
90 * ->page_table_lock (anon_vma_prepare and various)
93 * ->swap_device_lock (try_to_unmap_one)
94 * ->private_lock (try_to_unmap_one)
95 * ->tree_lock (try_to_unmap_one)
96 * ->zone.lru_lock (follow_page->mark_page_accessed)
97 * ->private_lock (page_remove_rmap->set_page_dirty)
98 * ->tree_lock (page_remove_rmap->set_page_dirty)
99 * ->inode_lock (page_remove_rmap->set_page_dirty)
100 * ->inode_lock (zap_pte_range->set_page_dirty)
101 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
104 * ->dcache_lock (proc_pid_lookup)
108 * Remove a page from the page cache and free it. Caller has to make
109 * sure the page is locked and that nobody else uses it - or that usage
110 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
112 void __remove_from_page_cache(struct page
*page
)
114 struct address_space
*mapping
= page
->mapping
;
116 radix_tree_delete(&mapping
->page_tree
, page
->index
);
117 page
->mapping
= NULL
;
122 void remove_from_page_cache(struct page
*page
)
124 struct address_space
*mapping
= page
->mapping
;
126 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((page_flags_t
*)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 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
176 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
177 * opposed to a regular memory * cleansing writeback. The difference between
178 * these two operations is that if a dirty page/buffer is encountered, it must
179 * be waited upon, and not just skipped over.
181 static int __filemap_fdatawrite_range(struct address_space
*mapping
,
182 loff_t start
, loff_t end
, int sync_mode
)
185 struct writeback_control wbc
= {
186 .sync_mode
= sync_mode
,
187 .nr_to_write
= mapping
->nrpages
* 2,
192 if (!mapping_cap_writeback_dirty(mapping
))
195 ret
= do_writepages(mapping
, &wbc
);
199 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
202 return __filemap_fdatawrite_range(mapping
, 0, 0, sync_mode
);
205 int filemap_fdatawrite(struct address_space
*mapping
)
207 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
209 EXPORT_SYMBOL(filemap_fdatawrite
);
211 static int filemap_fdatawrite_range(struct address_space
*mapping
,
212 loff_t start
, loff_t end
)
214 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
218 * This is a mostly non-blocking flush. Not suitable for data-integrity
219 * purposes - I/O may not be started against all dirty pages.
221 int filemap_flush(struct address_space
*mapping
)
223 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
225 EXPORT_SYMBOL(filemap_flush
);
228 * Wait for writeback to complete against pages indexed by start->end
231 static int wait_on_page_writeback_range(struct address_space
*mapping
,
232 pgoff_t start
, pgoff_t end
)
242 pagevec_init(&pvec
, 0);
244 while ((index
<= end
) &&
245 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
246 PAGECACHE_TAG_WRITEBACK
,
247 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
250 for (i
= 0; i
< nr_pages
; i
++) {
251 struct page
*page
= pvec
.pages
[i
];
253 /* until radix tree lookup accepts end_index */
254 if (page
->index
> end
)
257 wait_on_page_writeback(page
);
261 pagevec_release(&pvec
);
265 /* Check for outstanding write errors */
266 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
268 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
275 * Write and wait upon all the pages in the passed range. This is a "data
276 * integrity" operation. It waits upon in-flight writeout before starting and
277 * waiting upon new writeout. If there was an IO error, return it.
279 * We need to re-take i_sem during the generic_osync_inode list walk because
280 * it is otherwise livelockable.
282 int sync_page_range(struct inode
*inode
, struct address_space
*mapping
,
283 loff_t pos
, size_t count
)
285 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
286 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
289 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
291 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
294 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
298 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
301 EXPORT_SYMBOL(sync_page_range
);
304 * Note: Holding i_sem across sync_page_range_nolock is not a good idea
305 * as it forces O_SYNC writers to different parts of the same file
306 * to be serialised right until io completion.
308 int sync_page_range_nolock(struct inode
*inode
, struct address_space
*mapping
,
309 loff_t pos
, size_t count
)
311 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
312 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
315 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
317 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
319 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
321 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
324 EXPORT_SYMBOL(sync_page_range_nolock
);
327 * filemap_fdatawait - walk the list of under-writeback pages of the given
328 * address space and wait for all of them.
330 * @mapping: address space structure to wait for
332 int filemap_fdatawait(struct address_space
*mapping
)
334 loff_t i_size
= i_size_read(mapping
->host
);
339 return wait_on_page_writeback_range(mapping
, 0,
340 (i_size
- 1) >> PAGE_CACHE_SHIFT
);
342 EXPORT_SYMBOL(filemap_fdatawait
);
344 int filemap_write_and_wait(struct address_space
*mapping
)
348 if (mapping
->nrpages
) {
349 retval
= filemap_fdatawrite(mapping
);
351 retval
= filemap_fdatawait(mapping
);
356 int filemap_write_and_wait_range(struct address_space
*mapping
,
357 loff_t lstart
, loff_t lend
)
361 if (mapping
->nrpages
) {
362 retval
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
365 retval
= wait_on_page_writeback_range(mapping
,
366 lstart
>> PAGE_CACHE_SHIFT
,
367 lend
>> PAGE_CACHE_SHIFT
);
373 * This function is used to add newly allocated pagecache pages:
374 * the page is new, so we can just run SetPageLocked() against it.
375 * The other page state flags were set by rmqueue().
377 * This function does not add the page to the LRU. The caller must do that.
379 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
380 pgoff_t offset
, int gfp_mask
)
382 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
385 write_lock_irq(&mapping
->tree_lock
);
386 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
388 page_cache_get(page
);
390 page
->mapping
= mapping
;
391 page
->index
= offset
;
395 write_unlock_irq(&mapping
->tree_lock
);
396 radix_tree_preload_end();
401 EXPORT_SYMBOL(add_to_page_cache
);
403 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
404 pgoff_t offset
, int gfp_mask
)
406 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
413 * In order to wait for pages to become available there must be
414 * waitqueues associated with pages. By using a hash table of
415 * waitqueues where the bucket discipline is to maintain all
416 * waiters on the same queue and wake all when any of the pages
417 * become available, and for the woken contexts to check to be
418 * sure the appropriate page became available, this saves space
419 * at a cost of "thundering herd" phenomena during rare hash
422 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
424 const struct zone
*zone
= page_zone(page
);
426 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
429 static inline void wake_up_page(struct page
*page
, int bit
)
431 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
434 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
436 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
438 if (test_bit(bit_nr
, &page
->flags
))
439 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
440 TASK_UNINTERRUPTIBLE
);
442 EXPORT_SYMBOL(wait_on_page_bit
);
445 * unlock_page() - unlock a locked page
449 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
450 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
451 * mechananism between PageLocked pages and PageWriteback pages is shared.
452 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
454 * The first mb is necessary to safely close the critical section opened by the
455 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
456 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
457 * parallel wait_on_page_locked()).
459 void fastcall
unlock_page(struct page
*page
)
461 smp_mb__before_clear_bit();
462 if (!TestClearPageLocked(page
))
464 smp_mb__after_clear_bit();
465 wake_up_page(page
, PG_locked
);
467 EXPORT_SYMBOL(unlock_page
);
470 * End writeback against a page.
472 void end_page_writeback(struct page
*page
)
474 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
475 if (!test_clear_page_writeback(page
))
478 smp_mb__after_clear_bit();
479 wake_up_page(page
, PG_writeback
);
481 EXPORT_SYMBOL(end_page_writeback
);
484 * Get a lock on the page, assuming we need to sleep to get it.
486 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
487 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
488 * chances are that on the second loop, the block layer's plug list is empty,
489 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
491 void fastcall
__lock_page(struct page
*page
)
493 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
495 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
496 TASK_UNINTERRUPTIBLE
);
498 EXPORT_SYMBOL(__lock_page
);
501 * a rather lightweight function, finding and getting a reference to a
502 * hashed page atomically.
504 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
508 read_lock_irq(&mapping
->tree_lock
);
509 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
511 page_cache_get(page
);
512 read_unlock_irq(&mapping
->tree_lock
);
516 EXPORT_SYMBOL(find_get_page
);
519 * Same as above, but trylock it instead of incrementing the count.
521 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
525 read_lock_irq(&mapping
->tree_lock
);
526 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
527 if (page
&& TestSetPageLocked(page
))
529 read_unlock_irq(&mapping
->tree_lock
);
533 EXPORT_SYMBOL(find_trylock_page
);
536 * find_lock_page - locate, pin and lock a pagecache page
538 * @mapping - the address_space to search
539 * @offset - the page index
541 * Locates the desired pagecache page, locks it, increments its reference
542 * count and returns its address.
544 * Returns zero if the page was not present. find_lock_page() may sleep.
546 struct page
*find_lock_page(struct address_space
*mapping
,
547 unsigned long offset
)
551 read_lock_irq(&mapping
->tree_lock
);
553 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
555 page_cache_get(page
);
556 if (TestSetPageLocked(page
)) {
557 read_unlock_irq(&mapping
->tree_lock
);
559 read_lock_irq(&mapping
->tree_lock
);
561 /* Has the page been truncated while we slept? */
562 if (page
->mapping
!= mapping
|| page
->index
!= offset
) {
564 page_cache_release(page
);
569 read_unlock_irq(&mapping
->tree_lock
);
573 EXPORT_SYMBOL(find_lock_page
);
576 * find_or_create_page - locate or add a pagecache page
578 * @mapping - the page's address_space
579 * @index - the page's index into the mapping
580 * @gfp_mask - page allocation mode
582 * Locates a page in the pagecache. If the page is not present, a new page
583 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
584 * LRU list. The returned page is locked and has its reference count
587 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
590 * find_or_create_page() returns the desired page's address, or zero on
593 struct page
*find_or_create_page(struct address_space
*mapping
,
594 unsigned long index
, unsigned int gfp_mask
)
596 struct page
*page
, *cached_page
= NULL
;
599 page
= find_lock_page(mapping
, index
);
602 cached_page
= alloc_page(gfp_mask
);
606 err
= add_to_page_cache_lru(cached_page
, mapping
,
611 } else if (err
== -EEXIST
)
615 page_cache_release(cached_page
);
619 EXPORT_SYMBOL(find_or_create_page
);
622 * find_get_pages - gang pagecache lookup
623 * @mapping: The address_space to search
624 * @start: The starting page index
625 * @nr_pages: The maximum number of pages
626 * @pages: Where the resulting pages are placed
628 * find_get_pages() will search for and return a group of up to
629 * @nr_pages pages in the mapping. The pages are placed at @pages.
630 * find_get_pages() takes a reference against the returned pages.
632 * The search returns a group of mapping-contiguous pages with ascending
633 * indexes. There may be holes in the indices due to not-present pages.
635 * find_get_pages() returns the number of pages which were found.
637 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
638 unsigned int nr_pages
, struct page
**pages
)
643 read_lock_irq(&mapping
->tree_lock
);
644 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
645 (void **)pages
, start
, nr_pages
);
646 for (i
= 0; i
< ret
; i
++)
647 page_cache_get(pages
[i
]);
648 read_unlock_irq(&mapping
->tree_lock
);
653 * Like find_get_pages, except we only return pages which are tagged with
654 * `tag'. We update *index to index the next page for the traversal.
656 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
657 int tag
, unsigned int nr_pages
, struct page
**pages
)
662 read_lock_irq(&mapping
->tree_lock
);
663 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
664 (void **)pages
, *index
, nr_pages
, tag
);
665 for (i
= 0; i
< ret
; i
++)
666 page_cache_get(pages
[i
]);
668 *index
= pages
[ret
- 1]->index
+ 1;
669 read_unlock_irq(&mapping
->tree_lock
);
674 * Same as grab_cache_page, but do not wait if the page is unavailable.
675 * This is intended for speculative data generators, where the data can
676 * be regenerated if the page couldn't be grabbed. This routine should
677 * be safe to call while holding the lock for another page.
679 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
680 * and deadlock against the caller's locked page.
683 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
685 struct page
*page
= find_get_page(mapping
, index
);
686 unsigned int gfp_mask
;
689 if (!TestSetPageLocked(page
))
691 page_cache_release(page
);
694 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
695 page
= alloc_pages(gfp_mask
, 0);
696 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
697 page_cache_release(page
);
703 EXPORT_SYMBOL(grab_cache_page_nowait
);
706 * This is a generic file read routine, and uses the
707 * mapping->a_ops->readpage() function for the actual low-level
710 * This is really ugly. But the goto's actually try to clarify some
711 * of the logic when it comes to error handling etc.
713 * Note the struct file* is only passed for the use of readpage. It may be
716 void do_generic_mapping_read(struct address_space
*mapping
,
717 struct file_ra_state
*_ra
,
720 read_descriptor_t
*desc
,
723 struct inode
*inode
= mapping
->host
;
725 unsigned long end_index
;
726 unsigned long offset
;
727 unsigned long last_index
;
728 unsigned long next_index
;
729 unsigned long prev_index
;
731 struct page
*cached_page
;
733 struct file_ra_state ra
= *_ra
;
736 index
= *ppos
>> PAGE_CACHE_SHIFT
;
738 prev_index
= ra
.prev_page
;
739 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
740 offset
= *ppos
& ~PAGE_CACHE_MASK
;
742 isize
= i_size_read(inode
);
746 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
749 unsigned long nr
, ret
;
751 /* nr is the maximum number of bytes to copy from this page */
752 nr
= PAGE_CACHE_SIZE
;
753 if (index
>= end_index
) {
754 if (index
> end_index
)
756 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
764 if (index
== next_index
)
765 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
766 index
, last_index
- index
);
769 page
= find_get_page(mapping
, index
);
770 if (unlikely(page
== NULL
)) {
771 handle_ra_miss(mapping
, &ra
, index
);
774 if (!PageUptodate(page
))
775 goto page_not_up_to_date
;
778 /* If users can be writing to this page using arbitrary
779 * virtual addresses, take care about potential aliasing
780 * before reading the page on the kernel side.
782 if (mapping_writably_mapped(mapping
))
783 flush_dcache_page(page
);
786 * When (part of) the same page is read multiple times
787 * in succession, only mark it as accessed the first time.
789 if (prev_index
!= index
)
790 mark_page_accessed(page
);
794 * Ok, we have the page, and it's up-to-date, so
795 * now we can copy it to user space...
797 * The actor routine returns how many bytes were actually used..
798 * NOTE! This may not be the same as how much of a user buffer
799 * we filled up (we may be padding etc), so we can only update
800 * "pos" here (the actor routine has to update the user buffer
801 * pointers and the remaining count).
803 ret
= actor(desc
, page
, offset
, nr
);
805 index
+= offset
>> PAGE_CACHE_SHIFT
;
806 offset
&= ~PAGE_CACHE_MASK
;
808 page_cache_release(page
);
809 if (ret
== nr
&& desc
->count
)
814 /* Get exclusive access to the page ... */
817 /* Did it get unhashed before we got the lock? */
818 if (!page
->mapping
) {
820 page_cache_release(page
);
824 /* Did somebody else fill it already? */
825 if (PageUptodate(page
)) {
831 /* Start the actual read. The read will unlock the page. */
832 error
= mapping
->a_ops
->readpage(filp
, page
);
837 if (!PageUptodate(page
)) {
839 if (!PageUptodate(page
)) {
840 if (page
->mapping
== NULL
) {
842 * invalidate_inode_pages got it
845 page_cache_release(page
);
856 * i_size must be checked after we have done ->readpage.
858 * Checking i_size after the readpage allows us to calculate
859 * the correct value for "nr", which means the zero-filled
860 * part of the page is not copied back to userspace (unless
861 * another truncate extends the file - this is desired though).
863 isize
= i_size_read(inode
);
864 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
865 if (unlikely(!isize
|| index
> end_index
)) {
866 page_cache_release(page
);
870 /* nr is the maximum number of bytes to copy from this page */
871 nr
= PAGE_CACHE_SIZE
;
872 if (index
== end_index
) {
873 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
875 page_cache_release(page
);
883 /* UHHUH! A synchronous read error occurred. Report it */
885 page_cache_release(page
);
890 * Ok, it wasn't cached, so we need to create a new
894 cached_page
= page_cache_alloc_cold(mapping
);
896 desc
->error
= -ENOMEM
;
900 error
= add_to_page_cache_lru(cached_page
, mapping
,
903 if (error
== -EEXIST
)
916 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
918 page_cache_release(cached_page
);
923 EXPORT_SYMBOL(do_generic_mapping_read
);
925 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
926 unsigned long offset
, unsigned long size
)
929 unsigned long left
, count
= desc
->count
;
935 * Faults on the destination of a read are common, so do it before
938 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
939 kaddr
= kmap_atomic(page
, KM_USER0
);
940 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
941 kaddr
+ offset
, size
);
942 kunmap_atomic(kaddr
, KM_USER0
);
947 /* Do it the slow way */
949 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
954 desc
->error
= -EFAULT
;
957 desc
->count
= count
- size
;
958 desc
->written
+= size
;
959 desc
->arg
.buf
+= size
;
964 * This is the "read()" routine for all filesystems
965 * that can use the page cache directly.
968 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
969 unsigned long nr_segs
, loff_t
*ppos
)
971 struct file
*filp
= iocb
->ki_filp
;
977 for (seg
= 0; seg
< nr_segs
; seg
++) {
978 const struct iovec
*iv
= &iov
[seg
];
981 * If any segment has a negative length, or the cumulative
982 * length ever wraps negative then return -EINVAL.
984 count
+= iv
->iov_len
;
985 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
987 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
992 count
-= iv
->iov_len
; /* This segment is no good */
996 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
997 if (filp
->f_flags
& O_DIRECT
) {
998 loff_t pos
= *ppos
, size
;
999 struct address_space
*mapping
;
1000 struct inode
*inode
;
1002 mapping
= filp
->f_mapping
;
1003 inode
= mapping
->host
;
1006 goto out
; /* skip atime */
1007 size
= i_size_read(inode
);
1009 retval
= generic_file_direct_IO(READ
, iocb
,
1011 if (retval
>= 0 && !is_sync_kiocb(iocb
))
1012 retval
= -EIOCBQUEUED
;
1014 *ppos
= pos
+ retval
;
1016 file_accessed(filp
);
1022 for (seg
= 0; seg
< nr_segs
; seg
++) {
1023 read_descriptor_t desc
;
1026 desc
.arg
.buf
= iov
[seg
].iov_base
;
1027 desc
.count
= iov
[seg
].iov_len
;
1028 if (desc
.count
== 0)
1031 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1032 retval
+= desc
.written
;
1034 retval
= desc
.error
;
1043 EXPORT_SYMBOL(__generic_file_aio_read
);
1046 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1048 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1050 BUG_ON(iocb
->ki_pos
!= pos
);
1051 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1054 EXPORT_SYMBOL(generic_file_aio_read
);
1057 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1059 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1063 init_sync_kiocb(&kiocb
, filp
);
1064 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1065 if (-EIOCBQUEUED
== ret
)
1066 ret
= wait_on_sync_kiocb(&kiocb
);
1070 EXPORT_SYMBOL(generic_file_read
);
1072 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1075 unsigned long count
= desc
->count
;
1076 struct file
*file
= desc
->arg
.data
;
1081 written
= file
->f_op
->sendpage(file
, page
, offset
,
1082 size
, &file
->f_pos
, size
<count
);
1084 desc
->error
= written
;
1087 desc
->count
= count
- written
;
1088 desc
->written
+= written
;
1092 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1093 size_t count
, read_actor_t actor
, void *target
)
1095 read_descriptor_t desc
;
1102 desc
.arg
.data
= target
;
1105 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1107 return desc
.written
;
1111 EXPORT_SYMBOL(generic_file_sendfile
);
1114 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1115 unsigned long index
, unsigned long nr
)
1117 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1120 force_page_cache_readahead(mapping
, filp
, index
,
1121 max_sane_readahead(nr
));
1125 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1133 if (file
->f_mode
& FMODE_READ
) {
1134 struct address_space
*mapping
= file
->f_mapping
;
1135 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1136 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1137 unsigned long len
= end
- start
+ 1;
1138 ret
= do_readahead(mapping
, file
, start
, len
);
1147 * This adds the requested page to the page cache if it isn't already there,
1148 * and schedules an I/O to read in its contents from disk.
1150 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1151 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1153 struct address_space
*mapping
= file
->f_mapping
;
1157 page
= page_cache_alloc_cold(mapping
);
1161 error
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1163 error
= mapping
->a_ops
->readpage(file
, page
);
1164 page_cache_release(page
);
1169 * We arrive here in the unlikely event that someone
1170 * raced with us and added our page to the cache first
1171 * or we are out of memory for radix-tree nodes.
1173 page_cache_release(page
);
1174 return error
== -EEXIST
? 0 : error
;
1177 #define MMAP_LOTSAMISS (100)
1180 * filemap_nopage() is invoked via the vma operations vector for a
1181 * mapped memory region to read in file data during a page fault.
1183 * The goto's are kind of ugly, but this streamlines the normal case of having
1184 * it in the page cache, and handles the special cases reasonably without
1185 * having a lot of duplicated code.
1187 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1188 unsigned long address
, int *type
)
1191 struct file
*file
= area
->vm_file
;
1192 struct address_space
*mapping
= file
->f_mapping
;
1193 struct file_ra_state
*ra
= &file
->f_ra
;
1194 struct inode
*inode
= mapping
->host
;
1196 unsigned long size
, pgoff
;
1197 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1199 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1202 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1204 goto outside_data_content
;
1206 /* If we don't want any read-ahead, don't bother */
1207 if (VM_RandomReadHint(area
))
1208 goto no_cached_page
;
1211 * The readahead code wants to be told about each and every page
1212 * so it can build and shrink its windows appropriately
1214 * For sequential accesses, we use the generic readahead logic.
1216 if (VM_SequentialReadHint(area
))
1217 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1220 * Do we have something in the page cache already?
1223 page
= find_get_page(mapping
, pgoff
);
1225 unsigned long ra_pages
;
1227 if (VM_SequentialReadHint(area
)) {
1228 handle_ra_miss(mapping
, ra
, pgoff
);
1229 goto no_cached_page
;
1234 * Do we miss much more than hit in this file? If so,
1235 * stop bothering with read-ahead. It will only hurt.
1237 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1238 goto no_cached_page
;
1241 * To keep the pgmajfault counter straight, we need to
1242 * check did_readaround, as this is an inner loop.
1244 if (!did_readaround
) {
1245 majmin
= VM_FAULT_MAJOR
;
1246 inc_page_state(pgmajfault
);
1249 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1253 if (pgoff
> ra_pages
/ 2)
1254 start
= pgoff
- ra_pages
/ 2;
1255 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1257 page
= find_get_page(mapping
, pgoff
);
1259 goto no_cached_page
;
1262 if (!did_readaround
)
1266 * Ok, found a page in the page cache, now we need to check
1267 * that it's up-to-date.
1269 if (!PageUptodate(page
))
1270 goto page_not_uptodate
;
1274 * Found the page and have a reference on it.
1276 mark_page_accessed(page
);
1281 outside_data_content
:
1283 * An external ptracer can access pages that normally aren't
1286 if (area
->vm_mm
== current
->mm
)
1288 /* Fall through to the non-read-ahead case */
1291 * We're only likely to ever get here if MADV_RANDOM is in
1294 error
= page_cache_read(file
, pgoff
);
1298 * The page we want has now been added to the page cache.
1299 * In the unlikely event that someone removed it in the
1300 * meantime, we'll just come back here and read it again.
1306 * An error return from page_cache_read can result if the
1307 * system is low on memory, or a problem occurs while trying
1310 if (error
== -ENOMEM
)
1315 if (!did_readaround
) {
1316 majmin
= VM_FAULT_MAJOR
;
1317 inc_page_state(pgmajfault
);
1321 /* Did it get unhashed while we waited for it? */
1322 if (!page
->mapping
) {
1324 page_cache_release(page
);
1328 /* Did somebody else get it up-to-date? */
1329 if (PageUptodate(page
)) {
1334 if (!mapping
->a_ops
->readpage(file
, page
)) {
1335 wait_on_page_locked(page
);
1336 if (PageUptodate(page
))
1341 * Umm, take care of errors if the page isn't up-to-date.
1342 * Try to re-read it _once_. We do this synchronously,
1343 * because there really aren't any performance issues here
1344 * and we need to check for errors.
1348 /* Somebody truncated the page on us? */
1349 if (!page
->mapping
) {
1351 page_cache_release(page
);
1355 /* Somebody else successfully read it in? */
1356 if (PageUptodate(page
)) {
1360 ClearPageError(page
);
1361 if (!mapping
->a_ops
->readpage(file
, page
)) {
1362 wait_on_page_locked(page
);
1363 if (PageUptodate(page
))
1368 * Things didn't work out. Return zero to tell the
1369 * mm layer so, possibly freeing the page cache page first.
1371 page_cache_release(page
);
1375 EXPORT_SYMBOL(filemap_nopage
);
1377 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1380 struct address_space
*mapping
= file
->f_mapping
;
1385 * Do we have something in the page cache already?
1388 page
= find_get_page(mapping
, pgoff
);
1392 goto no_cached_page
;
1396 * Ok, found a page in the page cache, now we need to check
1397 * that it's up-to-date.
1399 if (!PageUptodate(page
)) {
1401 page_cache_release(page
);
1404 goto page_not_uptodate
;
1409 * Found the page and have a reference on it.
1411 mark_page_accessed(page
);
1415 error
= page_cache_read(file
, pgoff
);
1418 * The page we want has now been added to the page cache.
1419 * In the unlikely event that someone removed it in the
1420 * meantime, we'll just come back here and read it again.
1426 * An error return from page_cache_read can result if the
1427 * system is low on memory, or a problem occurs while trying
1435 /* Did it get unhashed while we waited for it? */
1436 if (!page
->mapping
) {
1441 /* Did somebody else get it up-to-date? */
1442 if (PageUptodate(page
)) {
1447 if (!mapping
->a_ops
->readpage(file
, page
)) {
1448 wait_on_page_locked(page
);
1449 if (PageUptodate(page
))
1454 * Umm, take care of errors if the page isn't up-to-date.
1455 * Try to re-read it _once_. We do this synchronously,
1456 * because there really aren't any performance issues here
1457 * and we need to check for errors.
1461 /* Somebody truncated the page on us? */
1462 if (!page
->mapping
) {
1466 /* Somebody else successfully read it in? */
1467 if (PageUptodate(page
)) {
1472 ClearPageError(page
);
1473 if (!mapping
->a_ops
->readpage(file
, page
)) {
1474 wait_on_page_locked(page
);
1475 if (PageUptodate(page
))
1480 * Things didn't work out. Return zero to tell the
1481 * mm layer so, possibly freeing the page cache page first.
1484 page_cache_release(page
);
1489 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1490 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1493 struct file
*file
= vma
->vm_file
;
1494 struct address_space
*mapping
= file
->f_mapping
;
1495 struct inode
*inode
= mapping
->host
;
1497 struct mm_struct
*mm
= vma
->vm_mm
;
1502 force_page_cache_readahead(mapping
, vma
->vm_file
,
1503 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1506 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1507 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1510 page
= filemap_getpage(file
, pgoff
, nonblock
);
1511 if (!page
&& !nonblock
)
1514 err
= install_page(mm
, vma
, addr
, page
, prot
);
1516 page_cache_release(page
);
1520 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1534 struct vm_operations_struct generic_file_vm_ops
= {
1535 .nopage
= filemap_nopage
,
1536 .populate
= filemap_populate
,
1539 /* This is used for a general mmap of a disk file */
1541 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1543 struct address_space
*mapping
= file
->f_mapping
;
1545 if (!mapping
->a_ops
->readpage
)
1547 file_accessed(file
);
1548 vma
->vm_ops
= &generic_file_vm_ops
;
1551 EXPORT_SYMBOL(filemap_populate
);
1554 * This is for filesystems which do not implement ->writepage.
1556 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1558 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1560 return generic_file_mmap(file
, vma
);
1563 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1567 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1571 #endif /* CONFIG_MMU */
1573 EXPORT_SYMBOL(generic_file_mmap
);
1574 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1576 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1577 unsigned long index
,
1578 int (*filler
)(void *,struct page
*),
1581 struct page
*page
, *cached_page
= NULL
;
1584 page
= find_get_page(mapping
, index
);
1587 cached_page
= page_cache_alloc_cold(mapping
);
1589 return ERR_PTR(-ENOMEM
);
1591 err
= add_to_page_cache_lru(cached_page
, mapping
,
1596 /* Presumably ENOMEM for radix tree node */
1597 page_cache_release(cached_page
);
1598 return ERR_PTR(err
);
1602 err
= filler(data
, page
);
1604 page_cache_release(page
);
1605 page
= ERR_PTR(err
);
1609 page_cache_release(cached_page
);
1614 * Read into the page cache. If a page already exists,
1615 * and PageUptodate() is not set, try to fill the page.
1617 struct page
*read_cache_page(struct address_space
*mapping
,
1618 unsigned long index
,
1619 int (*filler
)(void *,struct page
*),
1626 page
= __read_cache_page(mapping
, index
, filler
, data
);
1629 mark_page_accessed(page
);
1630 if (PageUptodate(page
))
1634 if (!page
->mapping
) {
1636 page_cache_release(page
);
1639 if (PageUptodate(page
)) {
1643 err
= filler(data
, page
);
1645 page_cache_release(page
);
1646 page
= ERR_PTR(err
);
1652 EXPORT_SYMBOL(read_cache_page
);
1655 * If the page was newly created, increment its refcount and add it to the
1656 * caller's lru-buffering pagevec. This function is specifically for
1657 * generic_file_write().
1659 static inline struct page
*
1660 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1661 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1666 page
= find_lock_page(mapping
, index
);
1668 if (!*cached_page
) {
1669 *cached_page
= page_cache_alloc(mapping
);
1673 err
= add_to_page_cache(*cached_page
, mapping
,
1678 page
= *cached_page
;
1679 page_cache_get(page
);
1680 if (!pagevec_add(lru_pvec
, page
))
1681 __pagevec_lru_add(lru_pvec
);
1682 *cached_page
= NULL
;
1689 * The logic we want is
1691 * if suid or (sgid and xgrp)
1694 int remove_suid(struct dentry
*dentry
)
1696 mode_t mode
= dentry
->d_inode
->i_mode
;
1700 /* suid always must be killed */
1701 if (unlikely(mode
& S_ISUID
))
1702 kill
= ATTR_KILL_SUID
;
1705 * sgid without any exec bits is just a mandatory locking mark; leave
1706 * it alone. If some exec bits are set, it's a real sgid; kill it.
1708 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1709 kill
|= ATTR_KILL_SGID
;
1711 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1712 struct iattr newattrs
;
1714 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1715 result
= notify_change(dentry
, &newattrs
);
1719 EXPORT_SYMBOL(remove_suid
);
1722 * Copy as much as we can into the page and return the number of bytes which
1723 * were sucessfully copied. If a fault is encountered then clear the page
1724 * out to (offset+bytes) and return the number of bytes which were copied.
1726 static inline size_t
1727 filemap_copy_from_user(struct page
*page
, unsigned long offset
,
1728 const char __user
*buf
, unsigned bytes
)
1733 kaddr
= kmap_atomic(page
, KM_USER0
);
1734 left
= __copy_from_user_inatomic(kaddr
+ offset
, buf
, bytes
);
1735 kunmap_atomic(kaddr
, KM_USER0
);
1738 /* Do it the slow way */
1740 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
1743 return bytes
- left
;
1747 __filemap_copy_from_user_iovec(char *vaddr
,
1748 const struct iovec
*iov
, size_t base
, size_t bytes
)
1750 size_t copied
= 0, left
= 0;
1753 char __user
*buf
= iov
->iov_base
+ base
;
1754 int copy
= min(bytes
, iov
->iov_len
- base
);
1757 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1763 if (unlikely(left
)) {
1764 /* zero the rest of the target like __copy_from_user */
1766 memset(vaddr
, 0, bytes
);
1770 return copied
- left
;
1774 * This has the same sideeffects and return value as filemap_copy_from_user().
1775 * The difference is that on a fault we need to memset the remainder of the
1776 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1777 * single-segment behaviour.
1779 static inline size_t
1780 filemap_copy_from_user_iovec(struct page
*page
, unsigned long offset
,
1781 const struct iovec
*iov
, size_t base
, size_t bytes
)
1786 kaddr
= kmap_atomic(page
, KM_USER0
);
1787 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1789 kunmap_atomic(kaddr
, KM_USER0
);
1790 if (copied
!= bytes
) {
1792 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1800 filemap_set_next_iovec(const struct iovec
**iovp
, size_t *basep
, size_t bytes
)
1802 const struct iovec
*iov
= *iovp
;
1803 size_t base
= *basep
;
1806 int copy
= min(bytes
, iov
->iov_len
- base
);
1810 if (iov
->iov_len
== base
) {
1820 * Performs necessary checks before doing a write
1822 * Can adjust writing position aor amount of bytes to write.
1823 * Returns appropriate error code that caller should return or
1824 * zero in case that write should be allowed.
1826 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1828 struct inode
*inode
= file
->f_mapping
->host
;
1829 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1831 if (unlikely(*pos
< 0))
1834 if (unlikely(file
->f_error
)) {
1835 int err
= file
->f_error
;
1841 /* FIXME: this is for backwards compatibility with 2.4 */
1842 if (file
->f_flags
& O_APPEND
)
1843 *pos
= i_size_read(inode
);
1845 if (limit
!= RLIM_INFINITY
) {
1846 if (*pos
>= limit
) {
1847 send_sig(SIGXFSZ
, current
, 0);
1850 if (*count
> limit
- (typeof(limit
))*pos
) {
1851 *count
= limit
- (typeof(limit
))*pos
;
1859 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1860 !(file
->f_flags
& O_LARGEFILE
))) {
1861 if (*pos
>= MAX_NON_LFS
) {
1862 send_sig(SIGXFSZ
, current
, 0);
1865 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1866 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1871 * Are we about to exceed the fs block limit ?
1873 * If we have written data it becomes a short write. If we have
1874 * exceeded without writing data we send a signal and return EFBIG.
1875 * Linus frestrict idea will clean these up nicely..
1877 if (likely(!isblk
)) {
1878 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1879 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1880 send_sig(SIGXFSZ
, current
, 0);
1883 /* zero-length writes at ->s_maxbytes are OK */
1886 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1887 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1890 if (bdev_read_only(I_BDEV(inode
)))
1892 isize
= i_size_read(inode
);
1893 if (*pos
>= isize
) {
1894 if (*count
|| *pos
> isize
)
1898 if (*pos
+ *count
> isize
)
1899 *count
= isize
- *pos
;
1903 EXPORT_SYMBOL(generic_write_checks
);
1906 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1907 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1908 size_t count
, size_t ocount
)
1910 struct file
*file
= iocb
->ki_filp
;
1911 struct address_space
*mapping
= file
->f_mapping
;
1912 struct inode
*inode
= mapping
->host
;
1915 if (count
!= ocount
)
1916 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1918 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1920 loff_t end
= pos
+ written
;
1921 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1922 i_size_write(inode
, end
);
1923 mark_inode_dirty(inode
);
1929 * Sync the fs metadata but not the minor inode changes and
1930 * of course not the data as we did direct DMA for the IO.
1931 * i_sem is held, which protects generic_osync_inode() from
1934 if (written
>= 0 && file
->f_flags
& O_SYNC
)
1935 generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1936 if (written
== count
&& !is_sync_kiocb(iocb
))
1937 written
= -EIOCBQUEUED
;
1940 EXPORT_SYMBOL(generic_file_direct_write
);
1943 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1944 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1945 size_t count
, ssize_t written
)
1947 struct file
*file
= iocb
->ki_filp
;
1948 struct address_space
* mapping
= file
->f_mapping
;
1949 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1950 struct inode
*inode
= mapping
->host
;
1953 struct page
*cached_page
= NULL
;
1955 struct pagevec lru_pvec
;
1956 const struct iovec
*cur_iov
= iov
; /* current iovec */
1957 size_t iov_base
= 0; /* offset in the current iovec */
1960 pagevec_init(&lru_pvec
, 0);
1963 * handle partial DIO write. Adjust cur_iov if needed.
1965 if (likely(nr_segs
== 1))
1966 buf
= iov
->iov_base
+ written
;
1968 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1969 buf
= cur_iov
->iov_base
+ iov_base
;
1973 unsigned long index
;
1974 unsigned long offset
;
1977 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1978 index
= pos
>> PAGE_CACHE_SHIFT
;
1979 bytes
= PAGE_CACHE_SIZE
- offset
;
1984 * Bring in the user page that we will copy from _first_.
1985 * Otherwise there's a nasty deadlock on copying from the
1986 * same page as we're writing to, without it being marked
1989 fault_in_pages_readable(buf
, bytes
);
1991 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1997 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1998 if (unlikely(status
)) {
1999 loff_t isize
= i_size_read(inode
);
2001 * prepare_write() may have instantiated a few blocks
2002 * outside i_size. Trim these off again.
2005 page_cache_release(page
);
2006 if (pos
+ bytes
> isize
)
2007 vmtruncate(inode
, isize
);
2010 if (likely(nr_segs
== 1))
2011 copied
= filemap_copy_from_user(page
, offset
,
2014 copied
= filemap_copy_from_user_iovec(page
, offset
,
2015 cur_iov
, iov_base
, bytes
);
2016 flush_dcache_page(page
);
2017 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
2018 if (likely(copied
> 0)) {
2027 if (unlikely(nr_segs
> 1)) {
2028 filemap_set_next_iovec(&cur_iov
,
2030 buf
= cur_iov
->iov_base
+ iov_base
;
2034 if (unlikely(copied
!= bytes
))
2038 mark_page_accessed(page
);
2039 page_cache_release(page
);
2042 balance_dirty_pages_ratelimited(mapping
);
2048 page_cache_release(cached_page
);
2051 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2053 if (likely(status
>= 0)) {
2054 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2055 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2056 status
= generic_osync_inode(inode
, mapping
,
2057 OSYNC_METADATA
|OSYNC_DATA
);
2062 * If we get here for O_DIRECT writes then we must have fallen through
2063 * to buffered writes (block instantiation inside i_size). So we sync
2064 * the file data here, to try to honour O_DIRECT expectations.
2066 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2067 status
= filemap_write_and_wait(mapping
);
2069 pagevec_lru_add(&lru_pvec
);
2070 return written
? written
: status
;
2072 EXPORT_SYMBOL(generic_file_buffered_write
);
2075 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2076 unsigned long nr_segs
, loff_t
*ppos
)
2078 struct file
*file
= iocb
->ki_filp
;
2079 struct address_space
* mapping
= file
->f_mapping
;
2080 size_t ocount
; /* original count */
2081 size_t count
; /* after file limit checks */
2082 struct inode
*inode
= mapping
->host
;
2089 for (seg
= 0; seg
< nr_segs
; seg
++) {
2090 const struct iovec
*iv
= &iov
[seg
];
2093 * If any segment has a negative length, or the cumulative
2094 * length ever wraps negative then return -EINVAL.
2096 ocount
+= iv
->iov_len
;
2097 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2099 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2104 ocount
-= iv
->iov_len
; /* This segment is no good */
2111 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2113 /* We can write back this queue in page reclaim */
2114 current
->backing_dev_info
= mapping
->backing_dev_info
;
2117 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2124 err
= remove_suid(file
->f_dentry
);
2128 inode_update_time(inode
, 1);
2130 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2131 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2132 written
= generic_file_direct_write(iocb
, iov
,
2133 &nr_segs
, pos
, ppos
, count
, ocount
);
2134 if (written
< 0 || written
== count
)
2137 * direct-io write to a hole: fall through to buffered I/O
2138 * for completing the rest of the request.
2144 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2145 pos
, ppos
, count
, written
);
2147 current
->backing_dev_info
= NULL
;
2148 return written
? written
: err
;
2150 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2153 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2154 unsigned long nr_segs
, loff_t
*ppos
)
2156 struct file
*file
= iocb
->ki_filp
;
2157 struct address_space
*mapping
= file
->f_mapping
;
2158 struct inode
*inode
= mapping
->host
;
2162 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2164 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2167 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2175 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2176 unsigned long nr_segs
, loff_t
*ppos
)
2181 init_sync_kiocb(&kiocb
, file
);
2182 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2183 if (ret
== -EIOCBQUEUED
)
2184 ret
= wait_on_sync_kiocb(&kiocb
);
2189 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2190 unsigned long nr_segs
, loff_t
*ppos
)
2195 init_sync_kiocb(&kiocb
, file
);
2196 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2197 if (-EIOCBQUEUED
== ret
)
2198 ret
= wait_on_sync_kiocb(&kiocb
);
2201 EXPORT_SYMBOL(generic_file_write_nolock
);
2203 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2204 size_t count
, loff_t pos
)
2206 struct file
*file
= iocb
->ki_filp
;
2207 struct address_space
*mapping
= file
->f_mapping
;
2208 struct inode
*inode
= mapping
->host
;
2210 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2213 BUG_ON(iocb
->ki_pos
!= pos
);
2215 down(&inode
->i_sem
);
2216 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2220 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2223 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2229 EXPORT_SYMBOL(generic_file_aio_write
);
2231 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2232 size_t count
, loff_t
*ppos
)
2234 struct address_space
*mapping
= file
->f_mapping
;
2235 struct inode
*inode
= mapping
->host
;
2237 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2240 down(&inode
->i_sem
);
2241 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2244 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2247 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2253 EXPORT_SYMBOL(generic_file_write
);
2255 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2256 unsigned long nr_segs
, loff_t
*ppos
)
2261 init_sync_kiocb(&kiocb
, filp
);
2262 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2263 if (-EIOCBQUEUED
== ret
)
2264 ret
= wait_on_sync_kiocb(&kiocb
);
2267 EXPORT_SYMBOL(generic_file_readv
);
2269 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2270 unsigned long nr_segs
, loff_t
*ppos
)
2272 struct address_space
*mapping
= file
->f_mapping
;
2273 struct inode
*inode
= mapping
->host
;
2276 down(&inode
->i_sem
);
2277 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2280 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2283 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2289 EXPORT_SYMBOL(generic_file_writev
);
2292 * Called under i_sem for writes to S_ISREG files. Returns -EIO if something
2293 * went wrong during pagecache shootdown.
2296 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2297 loff_t offset
, unsigned long nr_segs
)
2299 struct file
*file
= iocb
->ki_filp
;
2300 struct address_space
*mapping
= file
->f_mapping
;
2302 size_t write_len
= 0;
2305 * If it's a write, unmap all mmappings of the file up-front. This
2306 * will cause any pte dirty bits to be propagated into the pageframes
2307 * for the subsequent filemap_write_and_wait().
2310 write_len
= iov_length(iov
, nr_segs
);
2311 if (mapping_mapped(mapping
))
2312 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2315 retval
= filemap_write_and_wait(mapping
);
2317 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2319 if (rw
== WRITE
&& mapping
->nrpages
) {
2320 pgoff_t end
= (offset
+ write_len
- 1)
2321 >> PAGE_CACHE_SHIFT
;
2322 int err
= invalidate_inode_pages2_range(mapping
,
2323 offset
>> PAGE_CACHE_SHIFT
, end
);
2330 EXPORT_SYMBOL_GPL(generic_file_direct_IO
);