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>
32 #include <linux/cpuset.h>
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 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
46 loff_t offset
, unsigned long nr_segs
);
49 * Shared mappings implemented 30.11.1994. It's not fully working yet,
52 * Shared mappings now work. 15.8.1995 Bruno.
54 * finished 'unifying' the page and buffer cache and SMP-threaded the
55 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
57 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
63 * ->i_mmap_lock (vmtruncate)
64 * ->private_lock (__free_pte->__set_page_dirty_buffers)
65 * ->swap_lock (exclusive_swap_page, others)
66 * ->mapping->tree_lock
69 * ->i_mmap_lock (truncate->unmap_mapping_range)
73 * ->page_table_lock or pte_lock (various, mainly in memory.c)
74 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
77 * ->lock_page (access_process_vm)
83 * ->i_alloc_sem (various)
86 * ->sb_lock (fs/fs-writeback.c)
87 * ->mapping->tree_lock (__sync_single_inode)
90 * ->anon_vma.lock (vma_adjust)
93 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
95 * ->page_table_lock or pte_lock
96 * ->swap_lock (try_to_unmap_one)
97 * ->private_lock (try_to_unmap_one)
98 * ->tree_lock (try_to_unmap_one)
99 * ->zone.lru_lock (follow_page->mark_page_accessed)
100 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
101 * ->private_lock (page_remove_rmap->set_page_dirty)
102 * ->tree_lock (page_remove_rmap->set_page_dirty)
103 * ->inode_lock (page_remove_rmap->set_page_dirty)
104 * ->inode_lock (zap_pte_range->set_page_dirty)
105 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 * ->dcache_lock (proc_pid_lookup)
112 * Remove a page from the page cache and free it. Caller has to make
113 * sure the page is locked and that nobody else uses it - or that usage
114 * is safe. The caller must hold a write_lock on the mapping's tree_lock.
116 void __remove_from_page_cache(struct page
*page
)
118 struct address_space
*mapping
= page
->mapping
;
120 radix_tree_delete(&mapping
->page_tree
, page
->index
);
121 page
->mapping
= NULL
;
126 void remove_from_page_cache(struct page
*page
)
128 struct address_space
*mapping
= page
->mapping
;
130 BUG_ON(!PageLocked(page
));
132 write_lock_irq(&mapping
->tree_lock
);
133 __remove_from_page_cache(page
);
134 write_unlock_irq(&mapping
->tree_lock
);
137 static int sync_page(void *word
)
139 struct address_space
*mapping
;
142 page
= container_of((unsigned long *)word
, struct page
, flags
);
145 * page_mapping() is being called without PG_locked held.
146 * Some knowledge of the state and use of the page is used to
147 * reduce the requirements down to a memory barrier.
148 * The danger here is of a stale page_mapping() return value
149 * indicating a struct address_space different from the one it's
150 * associated with when it is associated with one.
151 * After smp_mb(), it's either the correct page_mapping() for
152 * the page, or an old page_mapping() and the page's own
153 * page_mapping() has gone NULL.
154 * The ->sync_page() address_space operation must tolerate
155 * page_mapping() going NULL. By an amazing coincidence,
156 * this comes about because none of the users of the page
157 * in the ->sync_page() methods make essential use of the
158 * page_mapping(), merely passing the page down to the backing
159 * device's unplug functions when it's non-NULL, which in turn
160 * ignore it for all cases but swap, where only page_private(page) is
161 * of interest. When page_mapping() does go NULL, the entire
162 * call stack gracefully ignores the page and returns.
166 mapping
= page_mapping(page
);
167 if (mapping
&& mapping
->a_ops
&& mapping
->a_ops
->sync_page
)
168 mapping
->a_ops
->sync_page(page
);
174 * filemap_fdatawrite_range - start writeback against all of a mapping's
175 * dirty pages that lie within the byte offsets <start, end>
176 * @mapping: address space structure to write
177 * @start: offset in bytes where the range starts
178 * @end: offset in bytes where the range ends (inclusive)
179 * @sync_mode: enable synchronous operation
181 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
182 * opposed to a regular memory * cleansing writeback. The difference between
183 * these two operations is that if a dirty page/buffer is encountered, it must
184 * be waited upon, and not just skipped over.
186 int __filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
187 loff_t end
, int sync_mode
)
190 struct writeback_control wbc
= {
191 .sync_mode
= sync_mode
,
192 .nr_to_write
= mapping
->nrpages
* 2,
197 if (!mapping_cap_writeback_dirty(mapping
))
200 ret
= do_writepages(mapping
, &wbc
);
204 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
207 return __filemap_fdatawrite_range(mapping
, 0, 0, sync_mode
);
210 int filemap_fdatawrite(struct address_space
*mapping
)
212 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
214 EXPORT_SYMBOL(filemap_fdatawrite
);
216 static int filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
219 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
223 * This is a mostly non-blocking flush. Not suitable for data-integrity
224 * purposes - I/O may not be started against all dirty pages.
226 int filemap_flush(struct address_space
*mapping
)
228 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
230 EXPORT_SYMBOL(filemap_flush
);
233 * Wait for writeback to complete against pages indexed by start->end
236 int wait_on_page_writeback_range(struct address_space
*mapping
,
237 pgoff_t start
, pgoff_t end
)
247 pagevec_init(&pvec
, 0);
249 while ((index
<= end
) &&
250 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
251 PAGECACHE_TAG_WRITEBACK
,
252 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
255 for (i
= 0; i
< nr_pages
; i
++) {
256 struct page
*page
= pvec
.pages
[i
];
258 /* until radix tree lookup accepts end_index */
259 if (page
->index
> end
)
262 wait_on_page_writeback(page
);
266 pagevec_release(&pvec
);
270 /* Check for outstanding write errors */
271 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
273 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
280 * Write and wait upon all the pages in the passed range. This is a "data
281 * integrity" operation. It waits upon in-flight writeout before starting and
282 * waiting upon new writeout. If there was an IO error, return it.
284 * We need to re-take i_mutex during the generic_osync_inode list walk because
285 * it is otherwise livelockable.
287 int sync_page_range(struct inode
*inode
, struct address_space
*mapping
,
288 loff_t pos
, loff_t count
)
290 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
291 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
294 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
296 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
298 mutex_lock(&inode
->i_mutex
);
299 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
300 mutex_unlock(&inode
->i_mutex
);
303 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
306 EXPORT_SYMBOL(sync_page_range
);
309 * Note: Holding i_mutex across sync_page_range_nolock is not a good idea
310 * as it forces O_SYNC writers to different parts of the same file
311 * to be serialised right until io completion.
313 int sync_page_range_nolock(struct inode
*inode
, struct address_space
*mapping
,
314 loff_t pos
, loff_t count
)
316 pgoff_t start
= pos
>> PAGE_CACHE_SHIFT
;
317 pgoff_t end
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
320 if (!mapping_cap_writeback_dirty(mapping
) || !count
)
322 ret
= filemap_fdatawrite_range(mapping
, pos
, pos
+ count
- 1);
324 ret
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
326 ret
= wait_on_page_writeback_range(mapping
, start
, end
);
329 EXPORT_SYMBOL(sync_page_range_nolock
);
332 * filemap_fdatawait - walk the list of under-writeback pages of the given
333 * address space and wait for all of them.
335 * @mapping: address space structure to wait for
337 int filemap_fdatawait(struct address_space
*mapping
)
339 loff_t i_size
= i_size_read(mapping
->host
);
344 return wait_on_page_writeback_range(mapping
, 0,
345 (i_size
- 1) >> PAGE_CACHE_SHIFT
);
347 EXPORT_SYMBOL(filemap_fdatawait
);
349 int filemap_write_and_wait(struct address_space
*mapping
)
353 if (mapping
->nrpages
) {
354 err
= filemap_fdatawrite(mapping
);
356 * Even if the above returned error, the pages may be
357 * written partially (e.g. -ENOSPC), so we wait for it.
358 * But the -EIO is special case, it may indicate the worst
359 * thing (e.g. bug) happened, so we avoid waiting for it.
362 int err2
= filemap_fdatawait(mapping
);
369 EXPORT_SYMBOL(filemap_write_and_wait
);
372 * Write out and wait upon file offsets lstart->lend, inclusive.
374 * Note that `lend' is inclusive (describes the last byte to be written) so
375 * that this function can be used to write to the very end-of-file (end = -1).
377 int filemap_write_and_wait_range(struct address_space
*mapping
,
378 loff_t lstart
, loff_t lend
)
382 if (mapping
->nrpages
) {
383 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
385 /* See comment of filemap_write_and_wait() */
387 int err2
= wait_on_page_writeback_range(mapping
,
388 lstart
>> PAGE_CACHE_SHIFT
,
389 lend
>> PAGE_CACHE_SHIFT
);
398 * This function is used to add newly allocated pagecache pages:
399 * the page is new, so we can just run SetPageLocked() against it.
400 * The other page state flags were set by rmqueue().
402 * This function does not add the page to the LRU. The caller must do that.
404 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
405 pgoff_t offset
, gfp_t gfp_mask
)
407 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
410 write_lock_irq(&mapping
->tree_lock
);
411 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
413 page_cache_get(page
);
415 page
->mapping
= mapping
;
416 page
->index
= offset
;
420 write_unlock_irq(&mapping
->tree_lock
);
421 radix_tree_preload_end();
426 EXPORT_SYMBOL(add_to_page_cache
);
428 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
429 pgoff_t offset
, gfp_t gfp_mask
)
431 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
438 struct page
*page_cache_alloc(struct address_space
*x
)
440 if (cpuset_do_page_mem_spread()) {
441 int n
= cpuset_mem_spread_node();
442 return alloc_pages_node(n
, mapping_gfp_mask(x
), 0);
444 return alloc_pages(mapping_gfp_mask(x
), 0);
446 EXPORT_SYMBOL(page_cache_alloc
);
448 struct page
*page_cache_alloc_cold(struct address_space
*x
)
450 if (cpuset_do_page_mem_spread()) {
451 int n
= cpuset_mem_spread_node();
452 return alloc_pages_node(n
, mapping_gfp_mask(x
)|__GFP_COLD
, 0);
454 return alloc_pages(mapping_gfp_mask(x
)|__GFP_COLD
, 0);
456 EXPORT_SYMBOL(page_cache_alloc_cold
);
460 * In order to wait for pages to become available there must be
461 * waitqueues associated with pages. By using a hash table of
462 * waitqueues where the bucket discipline is to maintain all
463 * waiters on the same queue and wake all when any of the pages
464 * become available, and for the woken contexts to check to be
465 * sure the appropriate page became available, this saves space
466 * at a cost of "thundering herd" phenomena during rare hash
469 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
471 const struct zone
*zone
= page_zone(page
);
473 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
476 static inline void wake_up_page(struct page
*page
, int bit
)
478 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
481 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
483 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
485 if (test_bit(bit_nr
, &page
->flags
))
486 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
487 TASK_UNINTERRUPTIBLE
);
489 EXPORT_SYMBOL(wait_on_page_bit
);
492 * unlock_page() - unlock a locked page
496 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
497 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
498 * mechananism between PageLocked pages and PageWriteback pages is shared.
499 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
501 * The first mb is necessary to safely close the critical section opened by the
502 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
503 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
504 * parallel wait_on_page_locked()).
506 void fastcall
unlock_page(struct page
*page
)
508 smp_mb__before_clear_bit();
509 if (!TestClearPageLocked(page
))
511 smp_mb__after_clear_bit();
512 wake_up_page(page
, PG_locked
);
514 EXPORT_SYMBOL(unlock_page
);
517 * End writeback against a page.
519 void end_page_writeback(struct page
*page
)
521 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
522 if (!test_clear_page_writeback(page
))
525 smp_mb__after_clear_bit();
526 wake_up_page(page
, PG_writeback
);
528 EXPORT_SYMBOL(end_page_writeback
);
531 * Get a lock on the page, assuming we need to sleep to get it.
533 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
534 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
535 * chances are that on the second loop, the block layer's plug list is empty,
536 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
538 void fastcall
__lock_page(struct page
*page
)
540 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
542 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
543 TASK_UNINTERRUPTIBLE
);
545 EXPORT_SYMBOL(__lock_page
);
548 * a rather lightweight function, finding and getting a reference to a
549 * hashed page atomically.
551 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
555 read_lock_irq(&mapping
->tree_lock
);
556 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
558 page_cache_get(page
);
559 read_unlock_irq(&mapping
->tree_lock
);
563 EXPORT_SYMBOL(find_get_page
);
566 * Same as above, but trylock it instead of incrementing the count.
568 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
572 read_lock_irq(&mapping
->tree_lock
);
573 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
574 if (page
&& TestSetPageLocked(page
))
576 read_unlock_irq(&mapping
->tree_lock
);
580 EXPORT_SYMBOL(find_trylock_page
);
583 * find_lock_page - locate, pin and lock a pagecache page
585 * @mapping: the address_space to search
586 * @offset: the page index
588 * Locates the desired pagecache page, locks it, increments its reference
589 * count and returns its address.
591 * Returns zero if the page was not present. find_lock_page() may sleep.
593 struct page
*find_lock_page(struct address_space
*mapping
,
594 unsigned long offset
)
598 read_lock_irq(&mapping
->tree_lock
);
600 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
602 page_cache_get(page
);
603 if (TestSetPageLocked(page
)) {
604 read_unlock_irq(&mapping
->tree_lock
);
606 read_lock_irq(&mapping
->tree_lock
);
608 /* Has the page been truncated while we slept? */
609 if (unlikely(page
->mapping
!= mapping
||
610 page
->index
!= offset
)) {
612 page_cache_release(page
);
617 read_unlock_irq(&mapping
->tree_lock
);
621 EXPORT_SYMBOL(find_lock_page
);
624 * find_or_create_page - locate or add a pagecache page
626 * @mapping: the page's address_space
627 * @index: the page's index into the mapping
628 * @gfp_mask: page allocation mode
630 * Locates a page in the pagecache. If the page is not present, a new page
631 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
632 * LRU list. The returned page is locked and has its reference count
635 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
638 * find_or_create_page() returns the desired page's address, or zero on
641 struct page
*find_or_create_page(struct address_space
*mapping
,
642 unsigned long index
, gfp_t gfp_mask
)
644 struct page
*page
, *cached_page
= NULL
;
647 page
= find_lock_page(mapping
, index
);
650 cached_page
= alloc_page(gfp_mask
);
654 err
= add_to_page_cache_lru(cached_page
, mapping
,
659 } else if (err
== -EEXIST
)
663 page_cache_release(cached_page
);
667 EXPORT_SYMBOL(find_or_create_page
);
670 * find_get_pages - gang pagecache lookup
671 * @mapping: The address_space to search
672 * @start: The starting page index
673 * @nr_pages: The maximum number of pages
674 * @pages: Where the resulting pages are placed
676 * find_get_pages() will search for and return a group of up to
677 * @nr_pages pages in the mapping. The pages are placed at @pages.
678 * find_get_pages() takes a reference against the returned pages.
680 * The search returns a group of mapping-contiguous pages with ascending
681 * indexes. There may be holes in the indices due to not-present pages.
683 * find_get_pages() returns the number of pages which were found.
685 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
686 unsigned int nr_pages
, struct page
**pages
)
691 read_lock_irq(&mapping
->tree_lock
);
692 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
693 (void **)pages
, start
, nr_pages
);
694 for (i
= 0; i
< ret
; i
++)
695 page_cache_get(pages
[i
]);
696 read_unlock_irq(&mapping
->tree_lock
);
701 * Like find_get_pages, except we only return pages which are tagged with
702 * `tag'. We update *index to index the next page for the traversal.
704 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
705 int tag
, unsigned int nr_pages
, struct page
**pages
)
710 read_lock_irq(&mapping
->tree_lock
);
711 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
712 (void **)pages
, *index
, nr_pages
, tag
);
713 for (i
= 0; i
< ret
; i
++)
714 page_cache_get(pages
[i
]);
716 *index
= pages
[ret
- 1]->index
+ 1;
717 read_unlock_irq(&mapping
->tree_lock
);
722 * Same as grab_cache_page, but do not wait if the page is unavailable.
723 * This is intended for speculative data generators, where the data can
724 * be regenerated if the page couldn't be grabbed. This routine should
725 * be safe to call while holding the lock for another page.
727 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
728 * and deadlock against the caller's locked page.
731 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
733 struct page
*page
= find_get_page(mapping
, index
);
737 if (!TestSetPageLocked(page
))
739 page_cache_release(page
);
742 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
743 page
= alloc_pages(gfp_mask
, 0);
744 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
745 page_cache_release(page
);
751 EXPORT_SYMBOL(grab_cache_page_nowait
);
754 * This is a generic file read routine, and uses the
755 * mapping->a_ops->readpage() function for the actual low-level
758 * This is really ugly. But the goto's actually try to clarify some
759 * of the logic when it comes to error handling etc.
761 * Note the struct file* is only passed for the use of readpage. It may be
764 void do_generic_mapping_read(struct address_space
*mapping
,
765 struct file_ra_state
*_ra
,
768 read_descriptor_t
*desc
,
771 struct inode
*inode
= mapping
->host
;
773 unsigned long end_index
;
774 unsigned long offset
;
775 unsigned long last_index
;
776 unsigned long next_index
;
777 unsigned long prev_index
;
779 struct page
*cached_page
;
781 struct file_ra_state ra
= *_ra
;
784 index
= *ppos
>> PAGE_CACHE_SHIFT
;
786 prev_index
= ra
.prev_page
;
787 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
788 offset
= *ppos
& ~PAGE_CACHE_MASK
;
790 isize
= i_size_read(inode
);
794 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
797 unsigned long nr
, ret
;
799 /* nr is the maximum number of bytes to copy from this page */
800 nr
= PAGE_CACHE_SIZE
;
801 if (index
>= end_index
) {
802 if (index
> end_index
)
804 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
812 if (index
== next_index
)
813 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
814 index
, last_index
- index
);
817 page
= find_get_page(mapping
, index
);
818 if (unlikely(page
== NULL
)) {
819 handle_ra_miss(mapping
, &ra
, index
);
822 if (!PageUptodate(page
))
823 goto page_not_up_to_date
;
826 /* If users can be writing to this page using arbitrary
827 * virtual addresses, take care about potential aliasing
828 * before reading the page on the kernel side.
830 if (mapping_writably_mapped(mapping
))
831 flush_dcache_page(page
);
834 * When (part of) the same page is read multiple times
835 * in succession, only mark it as accessed the first time.
837 if (prev_index
!= index
)
838 mark_page_accessed(page
);
842 * Ok, we have the page, and it's up-to-date, so
843 * now we can copy it to user space...
845 * The actor routine returns how many bytes were actually used..
846 * NOTE! This may not be the same as how much of a user buffer
847 * we filled up (we may be padding etc), so we can only update
848 * "pos" here (the actor routine has to update the user buffer
849 * pointers and the remaining count).
851 ret
= actor(desc
, page
, offset
, nr
);
853 index
+= offset
>> PAGE_CACHE_SHIFT
;
854 offset
&= ~PAGE_CACHE_MASK
;
856 page_cache_release(page
);
857 if (ret
== nr
&& desc
->count
)
862 /* Get exclusive access to the page ... */
865 /* Did it get unhashed before we got the lock? */
866 if (!page
->mapping
) {
868 page_cache_release(page
);
872 /* Did somebody else fill it already? */
873 if (PageUptodate(page
)) {
879 /* Start the actual read. The read will unlock the page. */
880 error
= mapping
->a_ops
->readpage(filp
, page
);
882 if (unlikely(error
)) {
883 if (error
== AOP_TRUNCATED_PAGE
) {
884 page_cache_release(page
);
890 if (!PageUptodate(page
)) {
892 if (!PageUptodate(page
)) {
893 if (page
->mapping
== NULL
) {
895 * invalidate_inode_pages got it
898 page_cache_release(page
);
909 * i_size must be checked after we have done ->readpage.
911 * Checking i_size after the readpage allows us to calculate
912 * the correct value for "nr", which means the zero-filled
913 * part of the page is not copied back to userspace (unless
914 * another truncate extends the file - this is desired though).
916 isize
= i_size_read(inode
);
917 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
918 if (unlikely(!isize
|| index
> end_index
)) {
919 page_cache_release(page
);
923 /* nr is the maximum number of bytes to copy from this page */
924 nr
= PAGE_CACHE_SIZE
;
925 if (index
== end_index
) {
926 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
928 page_cache_release(page
);
936 /* UHHUH! A synchronous read error occurred. Report it */
938 page_cache_release(page
);
943 * Ok, it wasn't cached, so we need to create a new
947 cached_page
= page_cache_alloc_cold(mapping
);
949 desc
->error
= -ENOMEM
;
953 error
= add_to_page_cache_lru(cached_page
, mapping
,
956 if (error
== -EEXIST
)
969 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
971 page_cache_release(cached_page
);
976 EXPORT_SYMBOL(do_generic_mapping_read
);
978 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
979 unsigned long offset
, unsigned long size
)
982 unsigned long left
, count
= desc
->count
;
988 * Faults on the destination of a read are common, so do it before
991 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
992 kaddr
= kmap_atomic(page
, KM_USER0
);
993 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
994 kaddr
+ offset
, size
);
995 kunmap_atomic(kaddr
, KM_USER0
);
1000 /* Do it the slow way */
1002 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1007 desc
->error
= -EFAULT
;
1010 desc
->count
= count
- size
;
1011 desc
->written
+= size
;
1012 desc
->arg
.buf
+= size
;
1017 * This is the "read()" routine for all filesystems
1018 * that can use the page cache directly.
1021 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1022 unsigned long nr_segs
, loff_t
*ppos
)
1024 struct file
*filp
= iocb
->ki_filp
;
1030 for (seg
= 0; seg
< nr_segs
; seg
++) {
1031 const struct iovec
*iv
= &iov
[seg
];
1034 * If any segment has a negative length, or the cumulative
1035 * length ever wraps negative then return -EINVAL.
1037 count
+= iv
->iov_len
;
1038 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1040 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1045 count
-= iv
->iov_len
; /* This segment is no good */
1049 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1050 if (filp
->f_flags
& O_DIRECT
) {
1051 loff_t pos
= *ppos
, size
;
1052 struct address_space
*mapping
;
1053 struct inode
*inode
;
1055 mapping
= filp
->f_mapping
;
1056 inode
= mapping
->host
;
1059 goto out
; /* skip atime */
1060 size
= i_size_read(inode
);
1062 retval
= generic_file_direct_IO(READ
, iocb
,
1064 if (retval
> 0 && !is_sync_kiocb(iocb
))
1065 retval
= -EIOCBQUEUED
;
1067 *ppos
= pos
+ retval
;
1069 file_accessed(filp
);
1075 for (seg
= 0; seg
< nr_segs
; seg
++) {
1076 read_descriptor_t desc
;
1079 desc
.arg
.buf
= iov
[seg
].iov_base
;
1080 desc
.count
= iov
[seg
].iov_len
;
1081 if (desc
.count
== 0)
1084 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1085 retval
+= desc
.written
;
1087 retval
= retval
?: desc
.error
;
1096 EXPORT_SYMBOL(__generic_file_aio_read
);
1099 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1101 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1103 BUG_ON(iocb
->ki_pos
!= pos
);
1104 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1107 EXPORT_SYMBOL(generic_file_aio_read
);
1110 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1112 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1116 init_sync_kiocb(&kiocb
, filp
);
1117 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1118 if (-EIOCBQUEUED
== ret
)
1119 ret
= wait_on_sync_kiocb(&kiocb
);
1123 EXPORT_SYMBOL(generic_file_read
);
1125 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1128 unsigned long count
= desc
->count
;
1129 struct file
*file
= desc
->arg
.data
;
1134 written
= file
->f_op
->sendpage(file
, page
, offset
,
1135 size
, &file
->f_pos
, size
<count
);
1137 desc
->error
= written
;
1140 desc
->count
= count
- written
;
1141 desc
->written
+= written
;
1145 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1146 size_t count
, read_actor_t actor
, void *target
)
1148 read_descriptor_t desc
;
1155 desc
.arg
.data
= target
;
1158 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1160 return desc
.written
;
1164 EXPORT_SYMBOL(generic_file_sendfile
);
1167 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1168 unsigned long index
, unsigned long nr
)
1170 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1173 force_page_cache_readahead(mapping
, filp
, index
,
1174 max_sane_readahead(nr
));
1178 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1186 if (file
->f_mode
& FMODE_READ
) {
1187 struct address_space
*mapping
= file
->f_mapping
;
1188 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1189 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1190 unsigned long len
= end
- start
+ 1;
1191 ret
= do_readahead(mapping
, file
, start
, len
);
1200 * This adds the requested page to the page cache if it isn't already there,
1201 * and schedules an I/O to read in its contents from disk.
1203 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1204 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1206 struct address_space
*mapping
= file
->f_mapping
;
1211 page
= page_cache_alloc_cold(mapping
);
1215 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1217 ret
= mapping
->a_ops
->readpage(file
, page
);
1218 else if (ret
== -EEXIST
)
1219 ret
= 0; /* losing race to add is OK */
1221 page_cache_release(page
);
1223 } while (ret
== AOP_TRUNCATED_PAGE
);
1228 #define MMAP_LOTSAMISS (100)
1231 * filemap_nopage() is invoked via the vma operations vector for a
1232 * mapped memory region to read in file data during a page fault.
1234 * The goto's are kind of ugly, but this streamlines the normal case of having
1235 * it in the page cache, and handles the special cases reasonably without
1236 * having a lot of duplicated code.
1238 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1239 unsigned long address
, int *type
)
1242 struct file
*file
= area
->vm_file
;
1243 struct address_space
*mapping
= file
->f_mapping
;
1244 struct file_ra_state
*ra
= &file
->f_ra
;
1245 struct inode
*inode
= mapping
->host
;
1247 unsigned long size
, pgoff
;
1248 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1250 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1253 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1255 goto outside_data_content
;
1257 /* If we don't want any read-ahead, don't bother */
1258 if (VM_RandomReadHint(area
))
1259 goto no_cached_page
;
1262 * The readahead code wants to be told about each and every page
1263 * so it can build and shrink its windows appropriately
1265 * For sequential accesses, we use the generic readahead logic.
1267 if (VM_SequentialReadHint(area
))
1268 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1271 * Do we have something in the page cache already?
1274 page
= find_get_page(mapping
, pgoff
);
1276 unsigned long ra_pages
;
1278 if (VM_SequentialReadHint(area
)) {
1279 handle_ra_miss(mapping
, ra
, pgoff
);
1280 goto no_cached_page
;
1285 * Do we miss much more than hit in this file? If so,
1286 * stop bothering with read-ahead. It will only hurt.
1288 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1289 goto no_cached_page
;
1292 * To keep the pgmajfault counter straight, we need to
1293 * check did_readaround, as this is an inner loop.
1295 if (!did_readaround
) {
1296 majmin
= VM_FAULT_MAJOR
;
1297 inc_page_state(pgmajfault
);
1300 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1304 if (pgoff
> ra_pages
/ 2)
1305 start
= pgoff
- ra_pages
/ 2;
1306 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1308 page
= find_get_page(mapping
, pgoff
);
1310 goto no_cached_page
;
1313 if (!did_readaround
)
1317 * Ok, found a page in the page cache, now we need to check
1318 * that it's up-to-date.
1320 if (!PageUptodate(page
))
1321 goto page_not_uptodate
;
1325 * Found the page and have a reference on it.
1327 mark_page_accessed(page
);
1332 outside_data_content
:
1334 * An external ptracer can access pages that normally aren't
1337 if (area
->vm_mm
== current
->mm
)
1339 /* Fall through to the non-read-ahead case */
1342 * We're only likely to ever get here if MADV_RANDOM is in
1345 error
= page_cache_read(file
, pgoff
);
1349 * The page we want has now been added to the page cache.
1350 * In the unlikely event that someone removed it in the
1351 * meantime, we'll just come back here and read it again.
1357 * An error return from page_cache_read can result if the
1358 * system is low on memory, or a problem occurs while trying
1361 if (error
== -ENOMEM
)
1366 if (!did_readaround
) {
1367 majmin
= VM_FAULT_MAJOR
;
1368 inc_page_state(pgmajfault
);
1372 /* Did it get unhashed while we waited for it? */
1373 if (!page
->mapping
) {
1375 page_cache_release(page
);
1379 /* Did somebody else get it up-to-date? */
1380 if (PageUptodate(page
)) {
1385 error
= mapping
->a_ops
->readpage(file
, page
);
1387 wait_on_page_locked(page
);
1388 if (PageUptodate(page
))
1390 } else if (error
== AOP_TRUNCATED_PAGE
) {
1391 page_cache_release(page
);
1396 * Umm, take care of errors if the page isn't up-to-date.
1397 * Try to re-read it _once_. We do this synchronously,
1398 * because there really aren't any performance issues here
1399 * and we need to check for errors.
1403 /* Somebody truncated the page on us? */
1404 if (!page
->mapping
) {
1406 page_cache_release(page
);
1410 /* Somebody else successfully read it in? */
1411 if (PageUptodate(page
)) {
1415 ClearPageError(page
);
1416 error
= mapping
->a_ops
->readpage(file
, page
);
1418 wait_on_page_locked(page
);
1419 if (PageUptodate(page
))
1421 } else if (error
== AOP_TRUNCATED_PAGE
) {
1422 page_cache_release(page
);
1427 * Things didn't work out. Return zero to tell the
1428 * mm layer so, possibly freeing the page cache page first.
1430 page_cache_release(page
);
1434 EXPORT_SYMBOL(filemap_nopage
);
1436 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1439 struct address_space
*mapping
= file
->f_mapping
;
1444 * Do we have something in the page cache already?
1447 page
= find_get_page(mapping
, pgoff
);
1451 goto no_cached_page
;
1455 * Ok, found a page in the page cache, now we need to check
1456 * that it's up-to-date.
1458 if (!PageUptodate(page
)) {
1460 page_cache_release(page
);
1463 goto page_not_uptodate
;
1468 * Found the page and have a reference on it.
1470 mark_page_accessed(page
);
1474 error
= page_cache_read(file
, pgoff
);
1477 * The page we want has now been added to the page cache.
1478 * In the unlikely event that someone removed it in the
1479 * meantime, we'll just come back here and read it again.
1485 * An error return from page_cache_read can result if the
1486 * system is low on memory, or a problem occurs while trying
1494 /* Did it get unhashed while we waited for it? */
1495 if (!page
->mapping
) {
1500 /* Did somebody else get it up-to-date? */
1501 if (PageUptodate(page
)) {
1506 error
= mapping
->a_ops
->readpage(file
, page
);
1508 wait_on_page_locked(page
);
1509 if (PageUptodate(page
))
1511 } else if (error
== AOP_TRUNCATED_PAGE
) {
1512 page_cache_release(page
);
1517 * Umm, take care of errors if the page isn't up-to-date.
1518 * Try to re-read it _once_. We do this synchronously,
1519 * because there really aren't any performance issues here
1520 * and we need to check for errors.
1524 /* Somebody truncated the page on us? */
1525 if (!page
->mapping
) {
1529 /* Somebody else successfully read it in? */
1530 if (PageUptodate(page
)) {
1535 ClearPageError(page
);
1536 error
= mapping
->a_ops
->readpage(file
, page
);
1538 wait_on_page_locked(page
);
1539 if (PageUptodate(page
))
1541 } else if (error
== AOP_TRUNCATED_PAGE
) {
1542 page_cache_release(page
);
1547 * Things didn't work out. Return zero to tell the
1548 * mm layer so, possibly freeing the page cache page first.
1551 page_cache_release(page
);
1556 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1557 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1560 struct file
*file
= vma
->vm_file
;
1561 struct address_space
*mapping
= file
->f_mapping
;
1562 struct inode
*inode
= mapping
->host
;
1564 struct mm_struct
*mm
= vma
->vm_mm
;
1569 force_page_cache_readahead(mapping
, vma
->vm_file
,
1570 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1573 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1574 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1577 page
= filemap_getpage(file
, pgoff
, nonblock
);
1579 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1580 * done in shmem_populate calling shmem_getpage */
1581 if (!page
&& !nonblock
)
1585 err
= install_page(mm
, vma
, addr
, page
, prot
);
1587 page_cache_release(page
);
1590 } else if (vma
->vm_flags
& VM_NONLINEAR
) {
1591 /* No page was found just because we can't read it in now (being
1592 * here implies nonblock != 0), but the page may exist, so set
1593 * the PTE to fault it in later. */
1594 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1607 EXPORT_SYMBOL(filemap_populate
);
1609 struct vm_operations_struct generic_file_vm_ops
= {
1610 .nopage
= filemap_nopage
,
1611 .populate
= filemap_populate
,
1614 /* This is used for a general mmap of a disk file */
1616 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1618 struct address_space
*mapping
= file
->f_mapping
;
1620 if (!mapping
->a_ops
->readpage
)
1622 file_accessed(file
);
1623 vma
->vm_ops
= &generic_file_vm_ops
;
1628 * This is for filesystems which do not implement ->writepage.
1630 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1632 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1634 return generic_file_mmap(file
, vma
);
1637 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1641 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1645 #endif /* CONFIG_MMU */
1647 EXPORT_SYMBOL(generic_file_mmap
);
1648 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1650 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1651 unsigned long index
,
1652 int (*filler
)(void *,struct page
*),
1655 struct page
*page
, *cached_page
= NULL
;
1658 page
= find_get_page(mapping
, index
);
1661 cached_page
= page_cache_alloc_cold(mapping
);
1663 return ERR_PTR(-ENOMEM
);
1665 err
= add_to_page_cache_lru(cached_page
, mapping
,
1670 /* Presumably ENOMEM for radix tree node */
1671 page_cache_release(cached_page
);
1672 return ERR_PTR(err
);
1676 err
= filler(data
, page
);
1678 page_cache_release(page
);
1679 page
= ERR_PTR(err
);
1683 page_cache_release(cached_page
);
1688 * Read into the page cache. If a page already exists,
1689 * and PageUptodate() is not set, try to fill the page.
1691 struct page
*read_cache_page(struct address_space
*mapping
,
1692 unsigned long index
,
1693 int (*filler
)(void *,struct page
*),
1700 page
= __read_cache_page(mapping
, index
, filler
, data
);
1703 mark_page_accessed(page
);
1704 if (PageUptodate(page
))
1708 if (!page
->mapping
) {
1710 page_cache_release(page
);
1713 if (PageUptodate(page
)) {
1717 err
= filler(data
, page
);
1719 page_cache_release(page
);
1720 page
= ERR_PTR(err
);
1726 EXPORT_SYMBOL(read_cache_page
);
1729 * If the page was newly created, increment its refcount and add it to the
1730 * caller's lru-buffering pagevec. This function is specifically for
1731 * generic_file_write().
1733 static inline struct page
*
1734 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1735 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1740 page
= find_lock_page(mapping
, index
);
1742 if (!*cached_page
) {
1743 *cached_page
= page_cache_alloc(mapping
);
1747 err
= add_to_page_cache(*cached_page
, mapping
,
1752 page
= *cached_page
;
1753 page_cache_get(page
);
1754 if (!pagevec_add(lru_pvec
, page
))
1755 __pagevec_lru_add(lru_pvec
);
1756 *cached_page
= NULL
;
1763 * The logic we want is
1765 * if suid or (sgid and xgrp)
1768 int remove_suid(struct dentry
*dentry
)
1770 mode_t mode
= dentry
->d_inode
->i_mode
;
1774 /* suid always must be killed */
1775 if (unlikely(mode
& S_ISUID
))
1776 kill
= ATTR_KILL_SUID
;
1779 * sgid without any exec bits is just a mandatory locking mark; leave
1780 * it alone. If some exec bits are set, it's a real sgid; kill it.
1782 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1783 kill
|= ATTR_KILL_SGID
;
1785 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1786 struct iattr newattrs
;
1788 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1789 result
= notify_change(dentry
, &newattrs
);
1793 EXPORT_SYMBOL(remove_suid
);
1796 __filemap_copy_from_user_iovec(char *vaddr
,
1797 const struct iovec
*iov
, size_t base
, size_t bytes
)
1799 size_t copied
= 0, left
= 0;
1802 char __user
*buf
= iov
->iov_base
+ base
;
1803 int copy
= min(bytes
, iov
->iov_len
- base
);
1806 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1812 if (unlikely(left
)) {
1813 /* zero the rest of the target like __copy_from_user */
1815 memset(vaddr
, 0, bytes
);
1819 return copied
- left
;
1823 * Performs necessary checks before doing a write
1825 * Can adjust writing position aor amount of bytes to write.
1826 * Returns appropriate error code that caller should return or
1827 * zero in case that write should be allowed.
1829 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1831 struct inode
*inode
= file
->f_mapping
->host
;
1832 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1834 if (unlikely(*pos
< 0))
1838 /* FIXME: this is for backwards compatibility with 2.4 */
1839 if (file
->f_flags
& O_APPEND
)
1840 *pos
= i_size_read(inode
);
1842 if (limit
!= RLIM_INFINITY
) {
1843 if (*pos
>= limit
) {
1844 send_sig(SIGXFSZ
, current
, 0);
1847 if (*count
> limit
- (typeof(limit
))*pos
) {
1848 *count
= limit
- (typeof(limit
))*pos
;
1856 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1857 !(file
->f_flags
& O_LARGEFILE
))) {
1858 if (*pos
>= MAX_NON_LFS
) {
1859 send_sig(SIGXFSZ
, current
, 0);
1862 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1863 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1868 * Are we about to exceed the fs block limit ?
1870 * If we have written data it becomes a short write. If we have
1871 * exceeded without writing data we send a signal and return EFBIG.
1872 * Linus frestrict idea will clean these up nicely..
1874 if (likely(!isblk
)) {
1875 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1876 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1877 send_sig(SIGXFSZ
, current
, 0);
1880 /* zero-length writes at ->s_maxbytes are OK */
1883 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1884 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1887 if (bdev_read_only(I_BDEV(inode
)))
1889 isize
= i_size_read(inode
);
1890 if (*pos
>= isize
) {
1891 if (*count
|| *pos
> isize
)
1895 if (*pos
+ *count
> isize
)
1896 *count
= isize
- *pos
;
1900 EXPORT_SYMBOL(generic_write_checks
);
1903 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1904 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1905 size_t count
, size_t ocount
)
1907 struct file
*file
= iocb
->ki_filp
;
1908 struct address_space
*mapping
= file
->f_mapping
;
1909 struct inode
*inode
= mapping
->host
;
1912 if (count
!= ocount
)
1913 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1915 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1917 loff_t end
= pos
+ written
;
1918 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1919 i_size_write(inode
, end
);
1920 mark_inode_dirty(inode
);
1926 * Sync the fs metadata but not the minor inode changes and
1927 * of course not the data as we did direct DMA for the IO.
1928 * i_mutex is held, which protects generic_osync_inode() from
1931 if (written
>= 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
1932 int err
= 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
;
1975 unsigned long maxlen
;
1978 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1979 index
= pos
>> PAGE_CACHE_SHIFT
;
1980 bytes
= PAGE_CACHE_SIZE
- offset
;
1985 * Bring in the user page that we will copy from _first_.
1986 * Otherwise there's a nasty deadlock on copying from the
1987 * same page as we're writing to, without it being marked
1990 maxlen
= cur_iov
->iov_len
- iov_base
;
1993 fault_in_pages_readable(buf
, maxlen
);
1995 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
2001 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
2002 if (unlikely(status
)) {
2003 loff_t isize
= i_size_read(inode
);
2005 if (status
!= AOP_TRUNCATED_PAGE
)
2007 page_cache_release(page
);
2008 if (status
== AOP_TRUNCATED_PAGE
)
2011 * prepare_write() may have instantiated a few blocks
2012 * outside i_size. Trim these off again.
2014 if (pos
+ bytes
> isize
)
2015 vmtruncate(inode
, isize
);
2018 if (likely(nr_segs
== 1))
2019 copied
= filemap_copy_from_user(page
, offset
,
2022 copied
= filemap_copy_from_user_iovec(page
, offset
,
2023 cur_iov
, iov_base
, bytes
);
2024 flush_dcache_page(page
);
2025 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
2026 if (status
== AOP_TRUNCATED_PAGE
) {
2027 page_cache_release(page
);
2030 if (likely(copied
> 0)) {
2039 if (unlikely(nr_segs
> 1)) {
2040 filemap_set_next_iovec(&cur_iov
,
2043 buf
= cur_iov
->iov_base
+
2050 if (unlikely(copied
!= bytes
))
2054 mark_page_accessed(page
);
2055 page_cache_release(page
);
2058 balance_dirty_pages_ratelimited(mapping
);
2064 page_cache_release(cached_page
);
2067 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2069 if (likely(status
>= 0)) {
2070 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2071 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2072 status
= generic_osync_inode(inode
, mapping
,
2073 OSYNC_METADATA
|OSYNC_DATA
);
2078 * If we get here for O_DIRECT writes then we must have fallen through
2079 * to buffered writes (block instantiation inside i_size). So we sync
2080 * the file data here, to try to honour O_DIRECT expectations.
2082 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2083 status
= filemap_write_and_wait(mapping
);
2085 pagevec_lru_add(&lru_pvec
);
2086 return written
? written
: status
;
2088 EXPORT_SYMBOL(generic_file_buffered_write
);
2091 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2092 unsigned long nr_segs
, loff_t
*ppos
)
2094 struct file
*file
= iocb
->ki_filp
;
2095 struct address_space
* mapping
= file
->f_mapping
;
2096 size_t ocount
; /* original count */
2097 size_t count
; /* after file limit checks */
2098 struct inode
*inode
= mapping
->host
;
2105 for (seg
= 0; seg
< nr_segs
; seg
++) {
2106 const struct iovec
*iv
= &iov
[seg
];
2109 * If any segment has a negative length, or the cumulative
2110 * length ever wraps negative then return -EINVAL.
2112 ocount
+= iv
->iov_len
;
2113 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2115 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2120 ocount
-= iv
->iov_len
; /* This segment is no good */
2127 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2129 /* We can write back this queue in page reclaim */
2130 current
->backing_dev_info
= mapping
->backing_dev_info
;
2133 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2140 err
= remove_suid(file
->f_dentry
);
2144 file_update_time(file
);
2146 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2147 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2148 written
= generic_file_direct_write(iocb
, iov
,
2149 &nr_segs
, pos
, ppos
, count
, ocount
);
2150 if (written
< 0 || written
== count
)
2153 * direct-io write to a hole: fall through to buffered I/O
2154 * for completing the rest of the request.
2160 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2161 pos
, ppos
, count
, written
);
2163 current
->backing_dev_info
= NULL
;
2164 return written
? written
: err
;
2166 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2169 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2170 unsigned long nr_segs
, loff_t
*ppos
)
2172 struct file
*file
= iocb
->ki_filp
;
2173 struct address_space
*mapping
= file
->f_mapping
;
2174 struct inode
*inode
= mapping
->host
;
2178 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2180 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2183 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2191 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2192 unsigned long nr_segs
, loff_t
*ppos
)
2197 init_sync_kiocb(&kiocb
, file
);
2198 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2199 if (ret
== -EIOCBQUEUED
)
2200 ret
= wait_on_sync_kiocb(&kiocb
);
2205 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2206 unsigned long nr_segs
, loff_t
*ppos
)
2211 init_sync_kiocb(&kiocb
, file
);
2212 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2213 if (-EIOCBQUEUED
== ret
)
2214 ret
= wait_on_sync_kiocb(&kiocb
);
2217 EXPORT_SYMBOL(generic_file_write_nolock
);
2219 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2220 size_t count
, loff_t pos
)
2222 struct file
*file
= iocb
->ki_filp
;
2223 struct address_space
*mapping
= file
->f_mapping
;
2224 struct inode
*inode
= mapping
->host
;
2226 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2229 BUG_ON(iocb
->ki_pos
!= pos
);
2231 mutex_lock(&inode
->i_mutex
);
2232 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2234 mutex_unlock(&inode
->i_mutex
);
2236 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2239 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2245 EXPORT_SYMBOL(generic_file_aio_write
);
2247 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2248 size_t count
, loff_t
*ppos
)
2250 struct address_space
*mapping
= file
->f_mapping
;
2251 struct inode
*inode
= mapping
->host
;
2253 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2256 mutex_lock(&inode
->i_mutex
);
2257 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2258 mutex_unlock(&inode
->i_mutex
);
2260 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2263 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2269 EXPORT_SYMBOL(generic_file_write
);
2271 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2272 unsigned long nr_segs
, loff_t
*ppos
)
2277 init_sync_kiocb(&kiocb
, filp
);
2278 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2279 if (-EIOCBQUEUED
== ret
)
2280 ret
= wait_on_sync_kiocb(&kiocb
);
2283 EXPORT_SYMBOL(generic_file_readv
);
2285 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2286 unsigned long nr_segs
, loff_t
*ppos
)
2288 struct address_space
*mapping
= file
->f_mapping
;
2289 struct inode
*inode
= mapping
->host
;
2292 mutex_lock(&inode
->i_mutex
);
2293 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2294 mutex_unlock(&inode
->i_mutex
);
2296 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2299 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2305 EXPORT_SYMBOL(generic_file_writev
);
2308 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
2309 * went wrong during pagecache shootdown.
2312 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2313 loff_t offset
, unsigned long nr_segs
)
2315 struct file
*file
= iocb
->ki_filp
;
2316 struct address_space
*mapping
= file
->f_mapping
;
2318 size_t write_len
= 0;
2321 * If it's a write, unmap all mmappings of the file up-front. This
2322 * will cause any pte dirty bits to be propagated into the pageframes
2323 * for the subsequent filemap_write_and_wait().
2326 write_len
= iov_length(iov
, nr_segs
);
2327 if (mapping_mapped(mapping
))
2328 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2331 retval
= filemap_write_and_wait(mapping
);
2333 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2335 if (rw
== WRITE
&& mapping
->nrpages
) {
2336 pgoff_t end
= (offset
+ write_len
- 1)
2337 >> PAGE_CACHE_SHIFT
;
2338 int err
= invalidate_inode_pages2_range(mapping
,
2339 offset
>> PAGE_CACHE_SHIFT
, end
);