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
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 static int __filemap_fdatawrite_range(struct address_space
*mapping
,
187 loff_t start
, 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
,
217 loff_t start
, loff_t end
)
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 static 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
);
371 int filemap_write_and_wait_range(struct address_space
*mapping
,
372 loff_t lstart
, loff_t lend
)
376 if (mapping
->nrpages
) {
377 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
379 /* See comment of filemap_write_and_wait() */
381 int err2
= wait_on_page_writeback_range(mapping
,
382 lstart
>> PAGE_CACHE_SHIFT
,
383 lend
>> PAGE_CACHE_SHIFT
);
392 * This function is used to add newly allocated pagecache pages:
393 * the page is new, so we can just run SetPageLocked() against it.
394 * The other page state flags were set by rmqueue().
396 * This function does not add the page to the LRU. The caller must do that.
398 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
399 pgoff_t offset
, gfp_t gfp_mask
)
401 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
404 write_lock_irq(&mapping
->tree_lock
);
405 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
407 page_cache_get(page
);
409 page
->mapping
= mapping
;
410 page
->index
= offset
;
414 write_unlock_irq(&mapping
->tree_lock
);
415 radix_tree_preload_end();
420 EXPORT_SYMBOL(add_to_page_cache
);
422 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
423 pgoff_t offset
, gfp_t gfp_mask
)
425 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
432 struct page
*page_cache_alloc(struct address_space
*x
)
434 if (cpuset_do_page_mem_spread()) {
435 int n
= cpuset_mem_spread_node();
436 return alloc_pages_node(n
, mapping_gfp_mask(x
), 0);
438 return alloc_pages(mapping_gfp_mask(x
), 0);
440 EXPORT_SYMBOL(page_cache_alloc
);
442 struct page
*page_cache_alloc_cold(struct address_space
*x
)
444 if (cpuset_do_page_mem_spread()) {
445 int n
= cpuset_mem_spread_node();
446 return alloc_pages_node(n
, mapping_gfp_mask(x
)|__GFP_COLD
, 0);
448 return alloc_pages(mapping_gfp_mask(x
)|__GFP_COLD
, 0);
450 EXPORT_SYMBOL(page_cache_alloc_cold
);
454 * In order to wait for pages to become available there must be
455 * waitqueues associated with pages. By using a hash table of
456 * waitqueues where the bucket discipline is to maintain all
457 * waiters on the same queue and wake all when any of the pages
458 * become available, and for the woken contexts to check to be
459 * sure the appropriate page became available, this saves space
460 * at a cost of "thundering herd" phenomena during rare hash
463 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
465 const struct zone
*zone
= page_zone(page
);
467 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
470 static inline void wake_up_page(struct page
*page
, int bit
)
472 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
475 void fastcall
wait_on_page_bit(struct page
*page
, int bit_nr
)
477 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
479 if (test_bit(bit_nr
, &page
->flags
))
480 __wait_on_bit(page_waitqueue(page
), &wait
, sync_page
,
481 TASK_UNINTERRUPTIBLE
);
483 EXPORT_SYMBOL(wait_on_page_bit
);
486 * unlock_page() - unlock a locked page
490 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
491 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
492 * mechananism between PageLocked pages and PageWriteback pages is shared.
493 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
495 * The first mb is necessary to safely close the critical section opened by the
496 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
497 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
498 * parallel wait_on_page_locked()).
500 void fastcall
unlock_page(struct page
*page
)
502 smp_mb__before_clear_bit();
503 if (!TestClearPageLocked(page
))
505 smp_mb__after_clear_bit();
506 wake_up_page(page
, PG_locked
);
508 EXPORT_SYMBOL(unlock_page
);
511 * End writeback against a page.
513 void end_page_writeback(struct page
*page
)
515 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
516 if (!test_clear_page_writeback(page
))
519 smp_mb__after_clear_bit();
520 wake_up_page(page
, PG_writeback
);
522 EXPORT_SYMBOL(end_page_writeback
);
525 * Get a lock on the page, assuming we need to sleep to get it.
527 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
528 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
529 * chances are that on the second loop, the block layer's plug list is empty,
530 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
532 void fastcall
__lock_page(struct page
*page
)
534 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
536 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sync_page
,
537 TASK_UNINTERRUPTIBLE
);
539 EXPORT_SYMBOL(__lock_page
);
542 * a rather lightweight function, finding and getting a reference to a
543 * hashed page atomically.
545 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
549 read_lock_irq(&mapping
->tree_lock
);
550 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
552 page_cache_get(page
);
553 read_unlock_irq(&mapping
->tree_lock
);
557 EXPORT_SYMBOL(find_get_page
);
560 * Same as above, but trylock it instead of incrementing the count.
562 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
566 read_lock_irq(&mapping
->tree_lock
);
567 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
568 if (page
&& TestSetPageLocked(page
))
570 read_unlock_irq(&mapping
->tree_lock
);
574 EXPORT_SYMBOL(find_trylock_page
);
577 * find_lock_page - locate, pin and lock a pagecache page
579 * @mapping: the address_space to search
580 * @offset: the page index
582 * Locates the desired pagecache page, locks it, increments its reference
583 * count and returns its address.
585 * Returns zero if the page was not present. find_lock_page() may sleep.
587 struct page
*find_lock_page(struct address_space
*mapping
,
588 unsigned long offset
)
592 read_lock_irq(&mapping
->tree_lock
);
594 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
596 page_cache_get(page
);
597 if (TestSetPageLocked(page
)) {
598 read_unlock_irq(&mapping
->tree_lock
);
600 read_lock_irq(&mapping
->tree_lock
);
602 /* Has the page been truncated while we slept? */
603 if (unlikely(page
->mapping
!= mapping
||
604 page
->index
!= offset
)) {
606 page_cache_release(page
);
611 read_unlock_irq(&mapping
->tree_lock
);
615 EXPORT_SYMBOL(find_lock_page
);
618 * find_or_create_page - locate or add a pagecache page
620 * @mapping: the page's address_space
621 * @index: the page's index into the mapping
622 * @gfp_mask: page allocation mode
624 * Locates a page in the pagecache. If the page is not present, a new page
625 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
626 * LRU list. The returned page is locked and has its reference count
629 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
632 * find_or_create_page() returns the desired page's address, or zero on
635 struct page
*find_or_create_page(struct address_space
*mapping
,
636 unsigned long index
, gfp_t gfp_mask
)
638 struct page
*page
, *cached_page
= NULL
;
641 page
= find_lock_page(mapping
, index
);
644 cached_page
= alloc_page(gfp_mask
);
648 err
= add_to_page_cache_lru(cached_page
, mapping
,
653 } else if (err
== -EEXIST
)
657 page_cache_release(cached_page
);
661 EXPORT_SYMBOL(find_or_create_page
);
664 * find_get_pages - gang pagecache lookup
665 * @mapping: The address_space to search
666 * @start: The starting page index
667 * @nr_pages: The maximum number of pages
668 * @pages: Where the resulting pages are placed
670 * find_get_pages() will search for and return a group of up to
671 * @nr_pages pages in the mapping. The pages are placed at @pages.
672 * find_get_pages() takes a reference against the returned pages.
674 * The search returns a group of mapping-contiguous pages with ascending
675 * indexes. There may be holes in the indices due to not-present pages.
677 * find_get_pages() returns the number of pages which were found.
679 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
680 unsigned int nr_pages
, struct page
**pages
)
685 read_lock_irq(&mapping
->tree_lock
);
686 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
687 (void **)pages
, start
, nr_pages
);
688 for (i
= 0; i
< ret
; i
++)
689 page_cache_get(pages
[i
]);
690 read_unlock_irq(&mapping
->tree_lock
);
695 * Like find_get_pages, except we only return pages which are tagged with
696 * `tag'. We update *index to index the next page for the traversal.
698 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
699 int tag
, unsigned int nr_pages
, struct page
**pages
)
704 read_lock_irq(&mapping
->tree_lock
);
705 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
706 (void **)pages
, *index
, nr_pages
, tag
);
707 for (i
= 0; i
< ret
; i
++)
708 page_cache_get(pages
[i
]);
710 *index
= pages
[ret
- 1]->index
+ 1;
711 read_unlock_irq(&mapping
->tree_lock
);
716 * Same as grab_cache_page, but do not wait if the page is unavailable.
717 * This is intended for speculative data generators, where the data can
718 * be regenerated if the page couldn't be grabbed. This routine should
719 * be safe to call while holding the lock for another page.
721 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
722 * and deadlock against the caller's locked page.
725 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
727 struct page
*page
= find_get_page(mapping
, index
);
731 if (!TestSetPageLocked(page
))
733 page_cache_release(page
);
736 gfp_mask
= mapping_gfp_mask(mapping
) & ~__GFP_FS
;
737 page
= alloc_pages(gfp_mask
, 0);
738 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
739 page_cache_release(page
);
745 EXPORT_SYMBOL(grab_cache_page_nowait
);
748 * This is a generic file read routine, and uses the
749 * mapping->a_ops->readpage() function for the actual low-level
752 * This is really ugly. But the goto's actually try to clarify some
753 * of the logic when it comes to error handling etc.
755 * Note the struct file* is only passed for the use of readpage. It may be
758 void do_generic_mapping_read(struct address_space
*mapping
,
759 struct file_ra_state
*_ra
,
762 read_descriptor_t
*desc
,
765 struct inode
*inode
= mapping
->host
;
767 unsigned long end_index
;
768 unsigned long offset
;
769 unsigned long last_index
;
770 unsigned long next_index
;
771 unsigned long prev_index
;
773 struct page
*cached_page
;
775 struct file_ra_state ra
= *_ra
;
778 index
= *ppos
>> PAGE_CACHE_SHIFT
;
780 prev_index
= ra
.prev_page
;
781 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
782 offset
= *ppos
& ~PAGE_CACHE_MASK
;
784 isize
= i_size_read(inode
);
788 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
791 unsigned long nr
, ret
;
793 /* nr is the maximum number of bytes to copy from this page */
794 nr
= PAGE_CACHE_SIZE
;
795 if (index
>= end_index
) {
796 if (index
> end_index
)
798 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
806 if (index
== next_index
)
807 next_index
= page_cache_readahead(mapping
, &ra
, filp
,
808 index
, last_index
- index
);
811 page
= find_get_page(mapping
, index
);
812 if (unlikely(page
== NULL
)) {
813 handle_ra_miss(mapping
, &ra
, index
);
816 if (!PageUptodate(page
))
817 goto page_not_up_to_date
;
820 /* If users can be writing to this page using arbitrary
821 * virtual addresses, take care about potential aliasing
822 * before reading the page on the kernel side.
824 if (mapping_writably_mapped(mapping
))
825 flush_dcache_page(page
);
828 * When (part of) the same page is read multiple times
829 * in succession, only mark it as accessed the first time.
831 if (prev_index
!= index
)
832 mark_page_accessed(page
);
836 * Ok, we have the page, and it's up-to-date, so
837 * now we can copy it to user space...
839 * The actor routine returns how many bytes were actually used..
840 * NOTE! This may not be the same as how much of a user buffer
841 * we filled up (we may be padding etc), so we can only update
842 * "pos" here (the actor routine has to update the user buffer
843 * pointers and the remaining count).
845 ret
= actor(desc
, page
, offset
, nr
);
847 index
+= offset
>> PAGE_CACHE_SHIFT
;
848 offset
&= ~PAGE_CACHE_MASK
;
850 page_cache_release(page
);
851 if (ret
== nr
&& desc
->count
)
856 /* Get exclusive access to the page ... */
859 /* Did it get unhashed before we got the lock? */
860 if (!page
->mapping
) {
862 page_cache_release(page
);
866 /* Did somebody else fill it already? */
867 if (PageUptodate(page
)) {
873 /* Start the actual read. The read will unlock the page. */
874 error
= mapping
->a_ops
->readpage(filp
, page
);
876 if (unlikely(error
)) {
877 if (error
== AOP_TRUNCATED_PAGE
) {
878 page_cache_release(page
);
884 if (!PageUptodate(page
)) {
886 if (!PageUptodate(page
)) {
887 if (page
->mapping
== NULL
) {
889 * invalidate_inode_pages got it
892 page_cache_release(page
);
903 * i_size must be checked after we have done ->readpage.
905 * Checking i_size after the readpage allows us to calculate
906 * the correct value for "nr", which means the zero-filled
907 * part of the page is not copied back to userspace (unless
908 * another truncate extends the file - this is desired though).
910 isize
= i_size_read(inode
);
911 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
912 if (unlikely(!isize
|| index
> end_index
)) {
913 page_cache_release(page
);
917 /* nr is the maximum number of bytes to copy from this page */
918 nr
= PAGE_CACHE_SIZE
;
919 if (index
== end_index
) {
920 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
922 page_cache_release(page
);
930 /* UHHUH! A synchronous read error occurred. Report it */
932 page_cache_release(page
);
937 * Ok, it wasn't cached, so we need to create a new
941 cached_page
= page_cache_alloc_cold(mapping
);
943 desc
->error
= -ENOMEM
;
947 error
= add_to_page_cache_lru(cached_page
, mapping
,
950 if (error
== -EEXIST
)
963 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
965 page_cache_release(cached_page
);
970 EXPORT_SYMBOL(do_generic_mapping_read
);
972 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
973 unsigned long offset
, unsigned long size
)
976 unsigned long left
, count
= desc
->count
;
982 * Faults on the destination of a read are common, so do it before
985 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
986 kaddr
= kmap_atomic(page
, KM_USER0
);
987 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
988 kaddr
+ offset
, size
);
989 kunmap_atomic(kaddr
, KM_USER0
);
994 /* Do it the slow way */
996 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1001 desc
->error
= -EFAULT
;
1004 desc
->count
= count
- size
;
1005 desc
->written
+= size
;
1006 desc
->arg
.buf
+= size
;
1011 * This is the "read()" routine for all filesystems
1012 * that can use the page cache directly.
1015 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1016 unsigned long nr_segs
, loff_t
*ppos
)
1018 struct file
*filp
= iocb
->ki_filp
;
1024 for (seg
= 0; seg
< nr_segs
; seg
++) {
1025 const struct iovec
*iv
= &iov
[seg
];
1028 * If any segment has a negative length, or the cumulative
1029 * length ever wraps negative then return -EINVAL.
1031 count
+= iv
->iov_len
;
1032 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1034 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1039 count
-= iv
->iov_len
; /* This segment is no good */
1043 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1044 if (filp
->f_flags
& O_DIRECT
) {
1045 loff_t pos
= *ppos
, size
;
1046 struct address_space
*mapping
;
1047 struct inode
*inode
;
1049 mapping
= filp
->f_mapping
;
1050 inode
= mapping
->host
;
1053 goto out
; /* skip atime */
1054 size
= i_size_read(inode
);
1056 retval
= generic_file_direct_IO(READ
, iocb
,
1058 if (retval
> 0 && !is_sync_kiocb(iocb
))
1059 retval
= -EIOCBQUEUED
;
1061 *ppos
= pos
+ retval
;
1063 file_accessed(filp
);
1069 for (seg
= 0; seg
< nr_segs
; seg
++) {
1070 read_descriptor_t desc
;
1073 desc
.arg
.buf
= iov
[seg
].iov_base
;
1074 desc
.count
= iov
[seg
].iov_len
;
1075 if (desc
.count
== 0)
1078 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
1079 retval
+= desc
.written
;
1081 retval
= retval
?: desc
.error
;
1090 EXPORT_SYMBOL(__generic_file_aio_read
);
1093 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
1095 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1097 BUG_ON(iocb
->ki_pos
!= pos
);
1098 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1101 EXPORT_SYMBOL(generic_file_aio_read
);
1104 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1106 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
1110 init_sync_kiocb(&kiocb
, filp
);
1111 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
1112 if (-EIOCBQUEUED
== ret
)
1113 ret
= wait_on_sync_kiocb(&kiocb
);
1117 EXPORT_SYMBOL(generic_file_read
);
1119 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
1122 unsigned long count
= desc
->count
;
1123 struct file
*file
= desc
->arg
.data
;
1128 written
= file
->f_op
->sendpage(file
, page
, offset
,
1129 size
, &file
->f_pos
, size
<count
);
1131 desc
->error
= written
;
1134 desc
->count
= count
- written
;
1135 desc
->written
+= written
;
1139 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
1140 size_t count
, read_actor_t actor
, void *target
)
1142 read_descriptor_t desc
;
1149 desc
.arg
.data
= target
;
1152 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
1154 return desc
.written
;
1158 EXPORT_SYMBOL(generic_file_sendfile
);
1161 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1162 unsigned long index
, unsigned long nr
)
1164 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1167 force_page_cache_readahead(mapping
, filp
, index
,
1168 max_sane_readahead(nr
));
1172 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
1180 if (file
->f_mode
& FMODE_READ
) {
1181 struct address_space
*mapping
= file
->f_mapping
;
1182 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
1183 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1184 unsigned long len
= end
- start
+ 1;
1185 ret
= do_readahead(mapping
, file
, start
, len
);
1194 * This adds the requested page to the page cache if it isn't already there,
1195 * and schedules an I/O to read in its contents from disk.
1197 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
1198 static int fastcall
page_cache_read(struct file
* file
, unsigned long offset
)
1200 struct address_space
*mapping
= file
->f_mapping
;
1205 page
= page_cache_alloc_cold(mapping
);
1209 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1211 ret
= mapping
->a_ops
->readpage(file
, page
);
1212 else if (ret
== -EEXIST
)
1213 ret
= 0; /* losing race to add is OK */
1215 page_cache_release(page
);
1217 } while (ret
== AOP_TRUNCATED_PAGE
);
1222 #define MMAP_LOTSAMISS (100)
1225 * filemap_nopage() is invoked via the vma operations vector for a
1226 * mapped memory region to read in file data during a page fault.
1228 * The goto's are kind of ugly, but this streamlines the normal case of having
1229 * it in the page cache, and handles the special cases reasonably without
1230 * having a lot of duplicated code.
1232 struct page
*filemap_nopage(struct vm_area_struct
*area
,
1233 unsigned long address
, int *type
)
1236 struct file
*file
= area
->vm_file
;
1237 struct address_space
*mapping
= file
->f_mapping
;
1238 struct file_ra_state
*ra
= &file
->f_ra
;
1239 struct inode
*inode
= mapping
->host
;
1241 unsigned long size
, pgoff
;
1242 int did_readaround
= 0, majmin
= VM_FAULT_MINOR
;
1244 pgoff
= ((address
-area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
1247 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1249 goto outside_data_content
;
1251 /* If we don't want any read-ahead, don't bother */
1252 if (VM_RandomReadHint(area
))
1253 goto no_cached_page
;
1256 * The readahead code wants to be told about each and every page
1257 * so it can build and shrink its windows appropriately
1259 * For sequential accesses, we use the generic readahead logic.
1261 if (VM_SequentialReadHint(area
))
1262 page_cache_readahead(mapping
, ra
, file
, pgoff
, 1);
1265 * Do we have something in the page cache already?
1268 page
= find_get_page(mapping
, pgoff
);
1270 unsigned long ra_pages
;
1272 if (VM_SequentialReadHint(area
)) {
1273 handle_ra_miss(mapping
, ra
, pgoff
);
1274 goto no_cached_page
;
1279 * Do we miss much more than hit in this file? If so,
1280 * stop bothering with read-ahead. It will only hurt.
1282 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
1283 goto no_cached_page
;
1286 * To keep the pgmajfault counter straight, we need to
1287 * check did_readaround, as this is an inner loop.
1289 if (!did_readaround
) {
1290 majmin
= VM_FAULT_MAJOR
;
1291 inc_page_state(pgmajfault
);
1294 ra_pages
= max_sane_readahead(file
->f_ra
.ra_pages
);
1298 if (pgoff
> ra_pages
/ 2)
1299 start
= pgoff
- ra_pages
/ 2;
1300 do_page_cache_readahead(mapping
, file
, start
, ra_pages
);
1302 page
= find_get_page(mapping
, pgoff
);
1304 goto no_cached_page
;
1307 if (!did_readaround
)
1311 * Ok, found a page in the page cache, now we need to check
1312 * that it's up-to-date.
1314 if (!PageUptodate(page
))
1315 goto page_not_uptodate
;
1319 * Found the page and have a reference on it.
1321 mark_page_accessed(page
);
1326 outside_data_content
:
1328 * An external ptracer can access pages that normally aren't
1331 if (area
->vm_mm
== current
->mm
)
1333 /* Fall through to the non-read-ahead case */
1336 * We're only likely to ever get here if MADV_RANDOM is in
1339 error
= page_cache_read(file
, pgoff
);
1343 * The page we want has now been added to the page cache.
1344 * In the unlikely event that someone removed it in the
1345 * meantime, we'll just come back here and read it again.
1351 * An error return from page_cache_read can result if the
1352 * system is low on memory, or a problem occurs while trying
1355 if (error
== -ENOMEM
)
1360 if (!did_readaround
) {
1361 majmin
= VM_FAULT_MAJOR
;
1362 inc_page_state(pgmajfault
);
1366 /* Did it get unhashed while we waited for it? */
1367 if (!page
->mapping
) {
1369 page_cache_release(page
);
1373 /* Did somebody else get it up-to-date? */
1374 if (PageUptodate(page
)) {
1379 error
= mapping
->a_ops
->readpage(file
, page
);
1381 wait_on_page_locked(page
);
1382 if (PageUptodate(page
))
1384 } else if (error
== AOP_TRUNCATED_PAGE
) {
1385 page_cache_release(page
);
1390 * Umm, take care of errors if the page isn't up-to-date.
1391 * Try to re-read it _once_. We do this synchronously,
1392 * because there really aren't any performance issues here
1393 * and we need to check for errors.
1397 /* Somebody truncated the page on us? */
1398 if (!page
->mapping
) {
1400 page_cache_release(page
);
1404 /* Somebody else successfully read it in? */
1405 if (PageUptodate(page
)) {
1409 ClearPageError(page
);
1410 error
= mapping
->a_ops
->readpage(file
, page
);
1412 wait_on_page_locked(page
);
1413 if (PageUptodate(page
))
1415 } else if (error
== AOP_TRUNCATED_PAGE
) {
1416 page_cache_release(page
);
1421 * Things didn't work out. Return zero to tell the
1422 * mm layer so, possibly freeing the page cache page first.
1424 page_cache_release(page
);
1428 EXPORT_SYMBOL(filemap_nopage
);
1430 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1433 struct address_space
*mapping
= file
->f_mapping
;
1438 * Do we have something in the page cache already?
1441 page
= find_get_page(mapping
, pgoff
);
1445 goto no_cached_page
;
1449 * Ok, found a page in the page cache, now we need to check
1450 * that it's up-to-date.
1452 if (!PageUptodate(page
)) {
1454 page_cache_release(page
);
1457 goto page_not_uptodate
;
1462 * Found the page and have a reference on it.
1464 mark_page_accessed(page
);
1468 error
= page_cache_read(file
, pgoff
);
1471 * The page we want has now been added to the page cache.
1472 * In the unlikely event that someone removed it in the
1473 * meantime, we'll just come back here and read it again.
1479 * An error return from page_cache_read can result if the
1480 * system is low on memory, or a problem occurs while trying
1488 /* Did it get unhashed while we waited for it? */
1489 if (!page
->mapping
) {
1494 /* Did somebody else get it up-to-date? */
1495 if (PageUptodate(page
)) {
1500 error
= mapping
->a_ops
->readpage(file
, page
);
1502 wait_on_page_locked(page
);
1503 if (PageUptodate(page
))
1505 } else if (error
== AOP_TRUNCATED_PAGE
) {
1506 page_cache_release(page
);
1511 * Umm, take care of errors if the page isn't up-to-date.
1512 * Try to re-read it _once_. We do this synchronously,
1513 * because there really aren't any performance issues here
1514 * and we need to check for errors.
1518 /* Somebody truncated the page on us? */
1519 if (!page
->mapping
) {
1523 /* Somebody else successfully read it in? */
1524 if (PageUptodate(page
)) {
1529 ClearPageError(page
);
1530 error
= mapping
->a_ops
->readpage(file
, page
);
1532 wait_on_page_locked(page
);
1533 if (PageUptodate(page
))
1535 } else if (error
== AOP_TRUNCATED_PAGE
) {
1536 page_cache_release(page
);
1541 * Things didn't work out. Return zero to tell the
1542 * mm layer so, possibly freeing the page cache page first.
1545 page_cache_release(page
);
1550 int filemap_populate(struct vm_area_struct
*vma
, unsigned long addr
,
1551 unsigned long len
, pgprot_t prot
, unsigned long pgoff
,
1554 struct file
*file
= vma
->vm_file
;
1555 struct address_space
*mapping
= file
->f_mapping
;
1556 struct inode
*inode
= mapping
->host
;
1558 struct mm_struct
*mm
= vma
->vm_mm
;
1563 force_page_cache_readahead(mapping
, vma
->vm_file
,
1564 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1567 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1568 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1571 page
= filemap_getpage(file
, pgoff
, nonblock
);
1573 /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
1574 * done in shmem_populate calling shmem_getpage */
1575 if (!page
&& !nonblock
)
1579 err
= install_page(mm
, vma
, addr
, page
, prot
);
1581 page_cache_release(page
);
1584 } else if (vma
->vm_flags
& VM_NONLINEAR
) {
1585 /* No page was found just because we can't read it in now (being
1586 * here implies nonblock != 0), but the page may exist, so set
1587 * the PTE to fault it in later. */
1588 err
= install_file_pte(mm
, vma
, addr
, pgoff
, prot
);
1601 EXPORT_SYMBOL(filemap_populate
);
1603 struct vm_operations_struct generic_file_vm_ops
= {
1604 .nopage
= filemap_nopage
,
1605 .populate
= filemap_populate
,
1608 /* This is used for a general mmap of a disk file */
1610 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1612 struct address_space
*mapping
= file
->f_mapping
;
1614 if (!mapping
->a_ops
->readpage
)
1616 file_accessed(file
);
1617 vma
->vm_ops
= &generic_file_vm_ops
;
1622 * This is for filesystems which do not implement ->writepage.
1624 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1626 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1628 return generic_file_mmap(file
, vma
);
1631 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1635 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1639 #endif /* CONFIG_MMU */
1641 EXPORT_SYMBOL(generic_file_mmap
);
1642 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1644 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1645 unsigned long index
,
1646 int (*filler
)(void *,struct page
*),
1649 struct page
*page
, *cached_page
= NULL
;
1652 page
= find_get_page(mapping
, index
);
1655 cached_page
= page_cache_alloc_cold(mapping
);
1657 return ERR_PTR(-ENOMEM
);
1659 err
= add_to_page_cache_lru(cached_page
, mapping
,
1664 /* Presumably ENOMEM for radix tree node */
1665 page_cache_release(cached_page
);
1666 return ERR_PTR(err
);
1670 err
= filler(data
, page
);
1672 page_cache_release(page
);
1673 page
= ERR_PTR(err
);
1677 page_cache_release(cached_page
);
1682 * Read into the page cache. If a page already exists,
1683 * and PageUptodate() is not set, try to fill the page.
1685 struct page
*read_cache_page(struct address_space
*mapping
,
1686 unsigned long index
,
1687 int (*filler
)(void *,struct page
*),
1694 page
= __read_cache_page(mapping
, index
, filler
, data
);
1697 mark_page_accessed(page
);
1698 if (PageUptodate(page
))
1702 if (!page
->mapping
) {
1704 page_cache_release(page
);
1707 if (PageUptodate(page
)) {
1711 err
= filler(data
, page
);
1713 page_cache_release(page
);
1714 page
= ERR_PTR(err
);
1720 EXPORT_SYMBOL(read_cache_page
);
1723 * If the page was newly created, increment its refcount and add it to the
1724 * caller's lru-buffering pagevec. This function is specifically for
1725 * generic_file_write().
1727 static inline struct page
*
1728 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1729 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1734 page
= find_lock_page(mapping
, index
);
1736 if (!*cached_page
) {
1737 *cached_page
= page_cache_alloc(mapping
);
1741 err
= add_to_page_cache(*cached_page
, mapping
,
1746 page
= *cached_page
;
1747 page_cache_get(page
);
1748 if (!pagevec_add(lru_pvec
, page
))
1749 __pagevec_lru_add(lru_pvec
);
1750 *cached_page
= NULL
;
1757 * The logic we want is
1759 * if suid or (sgid and xgrp)
1762 int remove_suid(struct dentry
*dentry
)
1764 mode_t mode
= dentry
->d_inode
->i_mode
;
1768 /* suid always must be killed */
1769 if (unlikely(mode
& S_ISUID
))
1770 kill
= ATTR_KILL_SUID
;
1773 * sgid without any exec bits is just a mandatory locking mark; leave
1774 * it alone. If some exec bits are set, it's a real sgid; kill it.
1776 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1777 kill
|= ATTR_KILL_SGID
;
1779 if (unlikely(kill
&& !capable(CAP_FSETID
))) {
1780 struct iattr newattrs
;
1782 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1783 result
= notify_change(dentry
, &newattrs
);
1787 EXPORT_SYMBOL(remove_suid
);
1790 __filemap_copy_from_user_iovec(char *vaddr
,
1791 const struct iovec
*iov
, size_t base
, size_t bytes
)
1793 size_t copied
= 0, left
= 0;
1796 char __user
*buf
= iov
->iov_base
+ base
;
1797 int copy
= min(bytes
, iov
->iov_len
- base
);
1800 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
1806 if (unlikely(left
)) {
1807 /* zero the rest of the target like __copy_from_user */
1809 memset(vaddr
, 0, bytes
);
1813 return copied
- left
;
1817 * Performs necessary checks before doing a write
1819 * Can adjust writing position aor amount of bytes to write.
1820 * Returns appropriate error code that caller should return or
1821 * zero in case that write should be allowed.
1823 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1825 struct inode
*inode
= file
->f_mapping
->host
;
1826 unsigned long limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1828 if (unlikely(*pos
< 0))
1832 /* FIXME: this is for backwards compatibility with 2.4 */
1833 if (file
->f_flags
& O_APPEND
)
1834 *pos
= i_size_read(inode
);
1836 if (limit
!= RLIM_INFINITY
) {
1837 if (*pos
>= limit
) {
1838 send_sig(SIGXFSZ
, current
, 0);
1841 if (*count
> limit
- (typeof(limit
))*pos
) {
1842 *count
= limit
- (typeof(limit
))*pos
;
1850 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1851 !(file
->f_flags
& O_LARGEFILE
))) {
1852 if (*pos
>= MAX_NON_LFS
) {
1853 send_sig(SIGXFSZ
, current
, 0);
1856 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1857 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1862 * Are we about to exceed the fs block limit ?
1864 * If we have written data it becomes a short write. If we have
1865 * exceeded without writing data we send a signal and return EFBIG.
1866 * Linus frestrict idea will clean these up nicely..
1868 if (likely(!isblk
)) {
1869 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1870 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1871 send_sig(SIGXFSZ
, current
, 0);
1874 /* zero-length writes at ->s_maxbytes are OK */
1877 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1878 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1881 if (bdev_read_only(I_BDEV(inode
)))
1883 isize
= i_size_read(inode
);
1884 if (*pos
>= isize
) {
1885 if (*count
|| *pos
> isize
)
1889 if (*pos
+ *count
> isize
)
1890 *count
= isize
- *pos
;
1894 EXPORT_SYMBOL(generic_write_checks
);
1897 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1898 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
1899 size_t count
, size_t ocount
)
1901 struct file
*file
= iocb
->ki_filp
;
1902 struct address_space
*mapping
= file
->f_mapping
;
1903 struct inode
*inode
= mapping
->host
;
1906 if (count
!= ocount
)
1907 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
1909 written
= generic_file_direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
1911 loff_t end
= pos
+ written
;
1912 if (end
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
1913 i_size_write(inode
, end
);
1914 mark_inode_dirty(inode
);
1920 * Sync the fs metadata but not the minor inode changes and
1921 * of course not the data as we did direct DMA for the IO.
1922 * i_mutex is held, which protects generic_osync_inode() from
1925 if (written
>= 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
1926 int err
= generic_osync_inode(inode
, mapping
, OSYNC_METADATA
);
1930 if (written
== count
&& !is_sync_kiocb(iocb
))
1931 written
= -EIOCBQUEUED
;
1934 EXPORT_SYMBOL(generic_file_direct_write
);
1937 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1938 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
1939 size_t count
, ssize_t written
)
1941 struct file
*file
= iocb
->ki_filp
;
1942 struct address_space
* mapping
= file
->f_mapping
;
1943 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1944 struct inode
*inode
= mapping
->host
;
1947 struct page
*cached_page
= NULL
;
1949 struct pagevec lru_pvec
;
1950 const struct iovec
*cur_iov
= iov
; /* current iovec */
1951 size_t iov_base
= 0; /* offset in the current iovec */
1954 pagevec_init(&lru_pvec
, 0);
1957 * handle partial DIO write. Adjust cur_iov if needed.
1959 if (likely(nr_segs
== 1))
1960 buf
= iov
->iov_base
+ written
;
1962 filemap_set_next_iovec(&cur_iov
, &iov_base
, written
);
1963 buf
= cur_iov
->iov_base
+ iov_base
;
1967 unsigned long index
;
1968 unsigned long offset
;
1969 unsigned long maxlen
;
1972 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1973 index
= pos
>> PAGE_CACHE_SHIFT
;
1974 bytes
= PAGE_CACHE_SIZE
- offset
;
1979 * Bring in the user page that we will copy from _first_.
1980 * Otherwise there's a nasty deadlock on copying from the
1981 * same page as we're writing to, without it being marked
1984 maxlen
= cur_iov
->iov_len
- iov_base
;
1987 fault_in_pages_readable(buf
, maxlen
);
1989 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1995 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1996 if (unlikely(status
)) {
1997 loff_t isize
= i_size_read(inode
);
1999 if (status
!= AOP_TRUNCATED_PAGE
)
2001 page_cache_release(page
);
2002 if (status
== AOP_TRUNCATED_PAGE
)
2005 * prepare_write() may have instantiated a few blocks
2006 * outside i_size. Trim these off again.
2008 if (pos
+ bytes
> isize
)
2009 vmtruncate(inode
, isize
);
2012 if (likely(nr_segs
== 1))
2013 copied
= filemap_copy_from_user(page
, offset
,
2016 copied
= filemap_copy_from_user_iovec(page
, offset
,
2017 cur_iov
, iov_base
, bytes
);
2018 flush_dcache_page(page
);
2019 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
2020 if (status
== AOP_TRUNCATED_PAGE
) {
2021 page_cache_release(page
);
2024 if (likely(copied
> 0)) {
2033 if (unlikely(nr_segs
> 1)) {
2034 filemap_set_next_iovec(&cur_iov
,
2037 buf
= cur_iov
->iov_base
+
2044 if (unlikely(copied
!= bytes
))
2048 mark_page_accessed(page
);
2049 page_cache_release(page
);
2052 balance_dirty_pages_ratelimited(mapping
);
2058 page_cache_release(cached_page
);
2061 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
2063 if (likely(status
>= 0)) {
2064 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2065 if (!a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2066 status
= generic_osync_inode(inode
, mapping
,
2067 OSYNC_METADATA
|OSYNC_DATA
);
2072 * If we get here for O_DIRECT writes then we must have fallen through
2073 * to buffered writes (block instantiation inside i_size). So we sync
2074 * the file data here, to try to honour O_DIRECT expectations.
2076 if (unlikely(file
->f_flags
& O_DIRECT
) && written
)
2077 status
= filemap_write_and_wait(mapping
);
2079 pagevec_lru_add(&lru_pvec
);
2080 return written
? written
: status
;
2082 EXPORT_SYMBOL(generic_file_buffered_write
);
2085 __generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2086 unsigned long nr_segs
, loff_t
*ppos
)
2088 struct file
*file
= iocb
->ki_filp
;
2089 struct address_space
* mapping
= file
->f_mapping
;
2090 size_t ocount
; /* original count */
2091 size_t count
; /* after file limit checks */
2092 struct inode
*inode
= mapping
->host
;
2099 for (seg
= 0; seg
< nr_segs
; seg
++) {
2100 const struct iovec
*iv
= &iov
[seg
];
2103 * If any segment has a negative length, or the cumulative
2104 * length ever wraps negative then return -EINVAL.
2106 ocount
+= iv
->iov_len
;
2107 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
2109 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2114 ocount
-= iv
->iov_len
; /* This segment is no good */
2121 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2123 /* We can write back this queue in page reclaim */
2124 current
->backing_dev_info
= mapping
->backing_dev_info
;
2127 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2134 err
= remove_suid(file
->f_dentry
);
2138 file_update_time(file
);
2140 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2141 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2142 written
= generic_file_direct_write(iocb
, iov
,
2143 &nr_segs
, pos
, ppos
, count
, ocount
);
2144 if (written
< 0 || written
== count
)
2147 * direct-io write to a hole: fall through to buffered I/O
2148 * for completing the rest of the request.
2154 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2155 pos
, ppos
, count
, written
);
2157 current
->backing_dev_info
= NULL
;
2158 return written
? written
: err
;
2160 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
2163 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
2164 unsigned long nr_segs
, loff_t
*ppos
)
2166 struct file
*file
= iocb
->ki_filp
;
2167 struct address_space
*mapping
= file
->f_mapping
;
2168 struct inode
*inode
= mapping
->host
;
2172 ret
= __generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, ppos
);
2174 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2177 err
= sync_page_range_nolock(inode
, mapping
, pos
, ret
);
2185 __generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2186 unsigned long nr_segs
, loff_t
*ppos
)
2191 init_sync_kiocb(&kiocb
, file
);
2192 ret
= __generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2193 if (ret
== -EIOCBQUEUED
)
2194 ret
= wait_on_sync_kiocb(&kiocb
);
2199 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
2200 unsigned long nr_segs
, loff_t
*ppos
)
2205 init_sync_kiocb(&kiocb
, file
);
2206 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2207 if (-EIOCBQUEUED
== ret
)
2208 ret
= wait_on_sync_kiocb(&kiocb
);
2211 EXPORT_SYMBOL(generic_file_write_nolock
);
2213 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2214 size_t count
, loff_t pos
)
2216 struct file
*file
= iocb
->ki_filp
;
2217 struct address_space
*mapping
= file
->f_mapping
;
2218 struct inode
*inode
= mapping
->host
;
2220 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2223 BUG_ON(iocb
->ki_pos
!= pos
);
2225 mutex_lock(&inode
->i_mutex
);
2226 ret
= __generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
2228 mutex_unlock(&inode
->i_mutex
);
2230 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2233 err
= sync_page_range(inode
, mapping
, pos
, ret
);
2239 EXPORT_SYMBOL(generic_file_aio_write
);
2241 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
2242 size_t count
, loff_t
*ppos
)
2244 struct address_space
*mapping
= file
->f_mapping
;
2245 struct inode
*inode
= mapping
->host
;
2247 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2250 mutex_lock(&inode
->i_mutex
);
2251 ret
= __generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
2252 mutex_unlock(&inode
->i_mutex
);
2254 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2257 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2263 EXPORT_SYMBOL(generic_file_write
);
2265 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
2266 unsigned long nr_segs
, loff_t
*ppos
)
2271 init_sync_kiocb(&kiocb
, filp
);
2272 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
2273 if (-EIOCBQUEUED
== ret
)
2274 ret
= wait_on_sync_kiocb(&kiocb
);
2277 EXPORT_SYMBOL(generic_file_readv
);
2279 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
2280 unsigned long nr_segs
, loff_t
*ppos
)
2282 struct address_space
*mapping
= file
->f_mapping
;
2283 struct inode
*inode
= mapping
->host
;
2286 mutex_lock(&inode
->i_mutex
);
2287 ret
= __generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
2288 mutex_unlock(&inode
->i_mutex
);
2290 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2293 err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2299 EXPORT_SYMBOL(generic_file_writev
);
2302 * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
2303 * went wrong during pagecache shootdown.
2306 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2307 loff_t offset
, unsigned long nr_segs
)
2309 struct file
*file
= iocb
->ki_filp
;
2310 struct address_space
*mapping
= file
->f_mapping
;
2312 size_t write_len
= 0;
2315 * If it's a write, unmap all mmappings of the file up-front. This
2316 * will cause any pte dirty bits to be propagated into the pageframes
2317 * for the subsequent filemap_write_and_wait().
2320 write_len
= iov_length(iov
, nr_segs
);
2321 if (mapping_mapped(mapping
))
2322 unmap_mapping_range(mapping
, offset
, write_len
, 0);
2325 retval
= filemap_write_and_wait(mapping
);
2327 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
,
2329 if (rw
== WRITE
&& mapping
->nrpages
) {
2330 pgoff_t end
= (offset
+ write_len
- 1)
2331 >> PAGE_CACHE_SHIFT
;
2332 int err
= invalidate_inode_pages2_range(mapping
,
2333 offset
>> PAGE_CACHE_SHIFT
, end
);