4 * Copyright (C) 1994-1999 Linus Torvalds
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/compiler.h>
17 #include <linux/aio.h>
18 #include <linux/kernel_stat.h>
20 #include <linux/swap.h>
21 #include <linux/mman.h>
22 #include <linux/pagemap.h>
23 #include <linux/file.h>
24 #include <linux/uio.h>
25 #include <linux/hash.h>
26 #include <linux/writeback.h>
27 #include <linux/pagevec.h>
28 #include <linux/blkdev.h>
29 #include <linux/security.h>
31 * This is needed for the following functions:
32 * - try_to_release_page
33 * - block_invalidatepage
34 * - generic_osync_inode
36 * FIXME: remove all knowledge of the buffer layer from the core VM
38 #include <linux/buffer_head.h> /* for generic_osync_inode */
40 #include <asm/uaccess.h>
44 * Shared mappings implemented 30.11.1994. It's not fully working yet,
47 * Shared mappings now work. 15.8.1995 Bruno.
49 * finished 'unifying' the page and buffer cache and SMP-threaded the
50 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
52 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
58 * ->i_shared_sem (vmtruncate)
59 * ->private_lock (__free_pte->__set_page_dirty_buffers)
61 * ->swap_device_lock (exclusive_swap_page, others)
62 * ->mapping->page_lock
64 * ->i_shared_sem (various places)
67 * ->sb_lock (fs/fs-writeback.c)
68 * ->mapping->page_lock (__sync_single_inode)
70 * ->swap_device_lock (try_to_unmap_one)
71 * ->private_lock (try_to_unmap_one)
72 * ->page_lock (try_to_unmap_one)
76 * Remove a page from the page cache and free it. Caller has to make
77 * sure the page is locked and that nobody else uses it - or that usage
78 * is safe. The caller must hold a write_lock on the mapping's page_lock.
80 void __remove_from_page_cache(struct page
*page
)
82 struct address_space
*mapping
= page
->mapping
;
84 radix_tree_delete(&mapping
->page_tree
, page
->index
);
85 list_del(&page
->list
);
92 void remove_from_page_cache(struct page
*page
)
94 struct address_space
*mapping
= page
->mapping
;
96 if (unlikely(!PageLocked(page
)))
99 spin_lock(&mapping
->page_lock
);
100 __remove_from_page_cache(page
);
101 spin_unlock(&mapping
->page_lock
);
104 static inline int sync_page(struct page
*page
)
106 struct address_space
*mapping
= page
->mapping
;
108 if (mapping
&& mapping
->a_ops
&& mapping
->a_ops
->sync_page
)
109 return mapping
->a_ops
->sync_page(page
);
114 * filemap_fdatawrite - start writeback against all of a mapping's dirty pages
115 * @mapping: address space structure to write
117 * This is a "data integrity" operation, as opposed to a regular memory
118 * cleansing writeback. The difference between these two operations is that
119 * if a dirty page/buffer is encountered, it must be waited upon, and not just
122 static int __filemap_fdatawrite(struct address_space
*mapping
, int sync_mode
)
125 struct writeback_control wbc
= {
126 .sync_mode
= sync_mode
,
127 .nr_to_write
= mapping
->nrpages
* 2,
130 if (mapping
->backing_dev_info
->memory_backed
)
133 spin_lock(&mapping
->page_lock
);
134 list_splice_init(&mapping
->dirty_pages
, &mapping
->io_pages
);
135 spin_unlock(&mapping
->page_lock
);
136 ret
= do_writepages(mapping
, &wbc
);
140 int filemap_fdatawrite(struct address_space
*mapping
)
142 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
146 * This is a mostly non-blocking flush. Not suitable for data-integrity
149 int filemap_flush(struct address_space
*mapping
)
151 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
155 * filemap_fdatawait - walk the list of locked pages of the given address
156 * space and wait for all of them.
157 * @mapping: address space structure to wait for
159 int filemap_fdatawait(struct address_space
* mapping
)
166 spin_lock(&mapping
->page_lock
);
167 while (!list_empty(&mapping
->locked_pages
)) {
170 page
= list_entry(mapping
->locked_pages
.next
,struct page
,list
);
171 list_del(&page
->list
);
173 list_add(&page
->list
, &mapping
->dirty_pages
);
175 list_add(&page
->list
, &mapping
->clean_pages
);
177 if (!PageWriteback(page
)) {
178 if (++progress
> 32) {
179 if (need_resched()) {
180 spin_unlock(&mapping
->page_lock
);
189 page_cache_get(page
);
190 spin_unlock(&mapping
->page_lock
);
192 wait_on_page_writeback(page
);
196 page_cache_release(page
);
197 spin_lock(&mapping
->page_lock
);
199 spin_unlock(&mapping
->page_lock
);
204 * This adds a page to the page cache, starting out as locked, unreferenced,
205 * not uptodate and with no errors.
207 * This function is used for two things: adding newly allocated pagecache
208 * pages and for moving existing anon pages into swapcache.
210 * In the case of pagecache pages, the page is new, so we can just run
211 * SetPageLocked() against it. The other page state flags were set by
214 * In the case of swapcache, try_to_swap_out() has already locked the page, so
215 * SetPageLocked() is ugly-but-OK there too. The required page state has been
216 * set up by swap_out_add_to_swap_cache().
218 * This function does not add the page to the LRU. The caller must do that.
220 int add_to_page_cache(struct page
*page
, struct address_space
*mapping
,
221 pgoff_t offset
, int gfp_mask
)
223 int error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
226 page_cache_get(page
);
227 spin_lock(&mapping
->page_lock
);
228 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
231 ___add_to_page_cache(page
, mapping
, offset
);
233 page_cache_release(page
);
235 spin_unlock(&mapping
->page_lock
);
236 radix_tree_preload_end();
240 EXPORT_SYMBOL(add_to_page_cache
);
242 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
243 pgoff_t offset
, int gfp_mask
)
245 int ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
252 * In order to wait for pages to become available there must be
253 * waitqueues associated with pages. By using a hash table of
254 * waitqueues where the bucket discipline is to maintain all
255 * waiters on the same queue and wake all when any of the pages
256 * become available, and for the woken contexts to check to be
257 * sure the appropriate page became available, this saves space
258 * at a cost of "thundering herd" phenomena during rare hash
261 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
263 const struct zone
*zone
= page_zone(page
);
265 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
268 void wait_on_page_bit(struct page
*page
, int bit_nr
)
270 wait_queue_head_t
*waitqueue
= page_waitqueue(page
);
274 prepare_to_wait(waitqueue
, &wait
, TASK_UNINTERRUPTIBLE
);
275 if (test_bit(bit_nr
, &page
->flags
)) {
279 } while (test_bit(bit_nr
, &page
->flags
));
280 finish_wait(waitqueue
, &wait
);
282 EXPORT_SYMBOL(wait_on_page_bit
);
285 * unlock_page() - unlock a locked page
289 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
290 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
291 * mechananism between PageLocked pages and PageWriteback pages is shared.
292 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
294 * The first mb is necessary to safely close the critical section opened by the
295 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
296 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
297 * parallel wait_on_page_locked()).
299 void unlock_page(struct page
*page
)
301 wait_queue_head_t
*waitqueue
= page_waitqueue(page
);
302 smp_mb__before_clear_bit();
303 if (!TestClearPageLocked(page
))
305 smp_mb__after_clear_bit();
306 if (waitqueue_active(waitqueue
))
307 wake_up_all(waitqueue
);
311 * End writeback against a page.
313 void end_page_writeback(struct page
*page
)
315 wait_queue_head_t
*waitqueue
= page_waitqueue(page
);
317 if (!TestClearPageReclaim(page
) || rotate_reclaimable_page(page
)) {
318 smp_mb__before_clear_bit();
319 if (!TestClearPageWriteback(page
))
321 smp_mb__after_clear_bit();
323 if (waitqueue_active(waitqueue
))
324 wake_up_all(waitqueue
);
326 EXPORT_SYMBOL(end_page_writeback
);
329 * Get a lock on the page, assuming we need to sleep to get it.
331 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
332 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
333 * chances are that on the second loop, the block layer's plug list is empty,
334 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
336 void __lock_page(struct page
*page
)
338 wait_queue_head_t
*wqh
= page_waitqueue(page
);
341 while (TestSetPageLocked(page
)) {
342 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
343 if (PageLocked(page
)) {
348 finish_wait(wqh
, &wait
);
350 EXPORT_SYMBOL(__lock_page
);
353 * a rather lightweight function, finding and getting a reference to a
354 * hashed page atomically.
356 struct page
* find_get_page(struct address_space
*mapping
, unsigned long offset
)
361 * We scan the hash list read-only. Addition to and removal from
362 * the hash-list needs a held write-lock.
364 spin_lock(&mapping
->page_lock
);
365 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
367 page_cache_get(page
);
368 spin_unlock(&mapping
->page_lock
);
373 * Same as above, but trylock it instead of incrementing the count.
375 struct page
*find_trylock_page(struct address_space
*mapping
, unsigned long offset
)
379 spin_lock(&mapping
->page_lock
);
380 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
381 if (page
&& TestSetPageLocked(page
))
383 spin_unlock(&mapping
->page_lock
);
388 * find_lock_page - locate, pin and lock a pagecache page
390 * @mapping - the address_space to search
391 * @offset - the page index
393 * Locates the desired pagecache page, locks it, increments its reference
394 * count and returns its address.
396 * Returns zero if the page was not present. find_lock_page() may sleep.
398 struct page
*find_lock_page(struct address_space
*mapping
,
399 unsigned long offset
)
403 spin_lock(&mapping
->page_lock
);
405 page
= radix_tree_lookup(&mapping
->page_tree
, offset
);
407 page_cache_get(page
);
408 if (TestSetPageLocked(page
)) {
409 spin_unlock(&mapping
->page_lock
);
411 spin_lock(&mapping
->page_lock
);
413 /* Has the page been truncated while we slept? */
414 if (page
->mapping
!= mapping
|| page
->index
!= offset
) {
416 page_cache_release(page
);
421 spin_unlock(&mapping
->page_lock
);
426 * find_or_create_page - locate or add a pagecache page
428 * @mapping - the page's address_space
429 * @index - the page's index into the mapping
430 * @gfp_mask - page allocation mode
432 * Locates a page in the pagecache. If the page is not present, a new page
433 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
434 * LRU list. The returned page is locked and has its reference count
437 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
440 * find_or_create_page() returns the desired page's address, or zero on
443 struct page
*find_or_create_page(struct address_space
*mapping
,
444 unsigned long index
, unsigned int gfp_mask
)
446 struct page
*page
, *cached_page
= NULL
;
449 page
= find_lock_page(mapping
, index
);
452 cached_page
= alloc_page(gfp_mask
);
456 err
= add_to_page_cache_lru(cached_page
, mapping
,
461 } else if (err
== -EEXIST
)
465 page_cache_release(cached_page
);
470 * find_get_pages - gang pagecache lookup
471 * @mapping: The address_space to search
472 * @start: The starting page index
473 * @nr_pages: The maximum number of pages
474 * @pages: Where the resulting pages are placed
476 * find_get_pages() will search for and return a group of up to
477 * @nr_pages pages in the mapping. The pages are placed at @pages.
478 * find_get_pages() takes a reference against the returned pages.
480 * The search returns a group of mapping-contiguous pages with ascending
481 * indexes. There may be holes in the indices due to not-present pages.
483 * find_get_pages() returns the number of pages which were found.
485 unsigned int find_get_pages(struct address_space
*mapping
, pgoff_t start
,
486 unsigned int nr_pages
, struct page
**pages
)
491 spin_lock(&mapping
->page_lock
);
492 ret
= radix_tree_gang_lookup(&mapping
->page_tree
,
493 (void **)pages
, start
, nr_pages
);
494 for (i
= 0; i
< ret
; i
++)
495 page_cache_get(pages
[i
]);
496 spin_unlock(&mapping
->page_lock
);
501 * Same as grab_cache_page, but do not wait if the page is unavailable.
502 * This is intended for speculative data generators, where the data can
503 * be regenerated if the page couldn't be grabbed. This routine should
504 * be safe to call while holding the lock for another page.
506 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
507 * and deadlock against the caller's locked page.
510 grab_cache_page_nowait(struct address_space
*mapping
, unsigned long index
)
512 struct page
*page
= find_get_page(mapping
, index
);
516 if (!TestSetPageLocked(page
))
518 page_cache_release(page
);
521 gfp_mask
= mapping
->gfp_mask
& ~__GFP_FS
;
522 page
= alloc_pages(gfp_mask
, 0);
523 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, gfp_mask
)) {
524 page_cache_release(page
);
531 * This is a generic file read routine, and uses the
532 * inode->i_op->readpage() function for the actual low-level
535 * This is really ugly. But the goto's actually try to clarify some
536 * of the logic when it comes to error handling etc.
537 * - note the struct file * is only passed for the use of readpage
539 void do_generic_mapping_read(struct address_space
*mapping
,
540 struct file_ra_state
*ra
,
543 read_descriptor_t
* desc
,
546 struct inode
*inode
= mapping
->host
;
547 unsigned long index
, offset
;
548 struct page
*cached_page
;
552 index
= *ppos
>> PAGE_CACHE_SHIFT
;
553 offset
= *ppos
& ~PAGE_CACHE_MASK
;
557 unsigned long end_index
, nr
, ret
;
558 loff_t isize
= i_size_read(inode
);
560 end_index
= isize
>> PAGE_CACHE_SHIFT
;
562 if (index
> end_index
)
564 nr
= PAGE_CACHE_SIZE
;
565 if (index
== end_index
) {
566 nr
= isize
& ~PAGE_CACHE_MASK
;
572 page_cache_readahead(mapping
, ra
, filp
, index
);
576 page
= find_get_page(mapping
, index
);
577 if (unlikely(page
== NULL
)) {
578 handle_ra_miss(mapping
, ra
, index
);
581 if (!PageUptodate(page
))
582 goto page_not_up_to_date
;
584 /* If users can be writing to this page using arbitrary
585 * virtual addresses, take care about potential aliasing
586 * before reading the page on the kernel side.
588 if (!list_empty(&mapping
->i_mmap_shared
))
589 flush_dcache_page(page
);
592 * Mark the page accessed if we read the beginning.
595 mark_page_accessed(page
);
598 * Ok, we have the page, and it's up-to-date, so
599 * now we can copy it to user space...
601 * The actor routine returns how many bytes were actually used..
602 * NOTE! This may not be the same as how much of a user buffer
603 * we filled up (we may be padding etc), so we can only update
604 * "pos" here (the actor routine has to update the user buffer
605 * pointers and the remaining count).
607 ret
= actor(desc
, page
, offset
, nr
);
609 index
+= offset
>> PAGE_CACHE_SHIFT
;
610 offset
&= ~PAGE_CACHE_MASK
;
612 page_cache_release(page
);
613 if (ret
== nr
&& desc
->count
)
618 if (PageUptodate(page
))
621 /* Get exclusive access to the page ... */
624 /* Did it get unhashed before we got the lock? */
625 if (!page
->mapping
) {
627 page_cache_release(page
);
631 /* Did somebody else fill it already? */
632 if (PageUptodate(page
)) {
638 /* ... and start the actual read. The read will unlock the page. */
639 error
= mapping
->a_ops
->readpage(filp
, page
);
642 if (PageUptodate(page
))
644 wait_on_page_locked(page
);
645 if (PageUptodate(page
))
650 /* UHHUH! A synchronous read error occurred. Report it */
652 page_cache_release(page
);
657 * Ok, it wasn't cached, so we need to create a new
661 cached_page
= page_cache_alloc_cold(mapping
);
663 desc
->error
= -ENOMEM
;
667 error
= add_to_page_cache_lru(cached_page
, mapping
,
670 if (error
== -EEXIST
)
680 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
682 page_cache_release(cached_page
);
686 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
687 unsigned long offset
, unsigned long size
)
690 unsigned long left
, count
= desc
->count
;
696 * Faults on the destination of a read are common, so do it before
699 if (!fault_in_pages_writeable(desc
->buf
, size
)) {
700 kaddr
= kmap_atomic(page
, KM_USER0
);
701 left
= __copy_to_user(desc
->buf
, kaddr
+ offset
, size
);
702 kunmap_atomic(kaddr
, KM_USER0
);
707 /* Do it the slow way */
709 left
= __copy_to_user(desc
->buf
, kaddr
+ offset
, size
);
714 desc
->error
= -EFAULT
;
717 desc
->count
= count
- size
;
718 desc
->written
+= size
;
724 * This is the "read()" routine for all filesystems
725 * that can use the page cache directly.
728 __generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
729 unsigned long nr_segs
, loff_t
*ppos
)
731 struct file
*filp
= iocb
->ki_filp
;
737 for (seg
= 0; seg
< nr_segs
; seg
++) {
738 const struct iovec
*iv
= &iov
[seg
];
741 * If any segment has a negative length, or the cumulative
742 * length ever wraps negative then return -EINVAL.
744 count
+= iv
->iov_len
;
745 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
747 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
752 count
-= iv
->iov_len
; /* This segment is no good */
756 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
757 if (filp
->f_flags
& O_DIRECT
) {
758 loff_t pos
= *ppos
, size
;
759 struct address_space
*mapping
;
762 mapping
= filp
->f_dentry
->d_inode
->i_mapping
;
763 inode
= mapping
->host
;
766 goto out
; /* skip atime */
767 size
= i_size_read(inode
);
769 retval
= generic_file_direct_IO(READ
, iocb
,
771 if (retval
>= 0 && !is_sync_kiocb(iocb
))
772 retval
= -EIOCBQUEUED
;
774 *ppos
= pos
+ retval
;
776 update_atime(filp
->f_dentry
->d_inode
);
782 for (seg
= 0; seg
< nr_segs
; seg
++) {
783 read_descriptor_t desc
;
786 desc
.buf
= iov
[seg
].iov_base
;
787 desc
.count
= iov
[seg
].iov_len
;
791 do_generic_file_read(filp
,ppos
,&desc
,file_read_actor
);
792 retval
+= desc
.written
;
804 generic_file_aio_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
806 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
808 BUG_ON(iocb
->ki_pos
!= pos
);
809 return __generic_file_aio_read(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
811 EXPORT_SYMBOL(generic_file_aio_read
);
814 generic_file_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
816 struct iovec local_iov
= { .iov_base
= buf
, .iov_len
= count
};
820 init_sync_kiocb(&kiocb
, filp
);
821 ret
= __generic_file_aio_read(&kiocb
, &local_iov
, 1, ppos
);
822 if (-EIOCBQUEUED
== ret
)
823 ret
= wait_on_sync_kiocb(&kiocb
);
827 int file_send_actor(read_descriptor_t
* desc
, struct page
*page
, unsigned long offset
, unsigned long size
)
830 unsigned long count
= desc
->count
;
831 struct file
*file
= (struct file
*) desc
->buf
;
836 written
= file
->f_op
->sendpage(file
, page
, offset
,
837 size
, &file
->f_pos
, size
<count
);
839 desc
->error
= written
;
842 desc
->count
= count
- written
;
843 desc
->written
+= written
;
847 ssize_t
generic_file_sendfile(struct file
*in_file
, loff_t
*ppos
,
848 size_t count
, read_actor_t actor
, void __user
*target
)
850 read_descriptor_t desc
;
860 do_generic_file_read(in_file
, ppos
, &desc
, actor
);
867 do_readahead(struct address_space
*mapping
, struct file
*filp
,
868 unsigned long index
, unsigned long nr
)
870 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
873 do_page_cache_readahead(mapping
, filp
, index
, max_sane_readahead(nr
));
877 asmlinkage ssize_t
sys_readahead(int fd
, loff_t offset
, size_t count
)
885 if (file
->f_mode
& FMODE_READ
) {
886 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
887 unsigned long start
= offset
>> PAGE_CACHE_SHIFT
;
888 unsigned long end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
889 unsigned long len
= end
- start
+ 1;
890 ret
= do_readahead(mapping
, file
, start
, len
);
899 * This adds the requested page to the page cache if it isn't already there,
900 * and schedules an I/O to read in its contents from disk.
902 static int FASTCALL(page_cache_read(struct file
* file
, unsigned long offset
));
903 static int page_cache_read(struct file
* file
, unsigned long offset
)
905 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
909 page
= page_cache_alloc_cold(mapping
);
913 error
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
915 error
= mapping
->a_ops
->readpage(file
, page
);
916 page_cache_release(page
);
921 * We arrive here in the unlikely event that someone
922 * raced with us and added our page to the cache first
923 * or we are out of memory for radix-tree nodes.
925 page_cache_release(page
);
926 return error
== -EEXIST
? 0 : error
;
929 #define MMAP_READAROUND (16UL)
930 #define MMAP_LOTSAMISS (100)
933 * filemap_nopage() is invoked via the vma operations vector for a
934 * mapped memory region to read in file data during a page fault.
936 * The goto's are kind of ugly, but this streamlines the normal case of having
937 * it in the page cache, and handles the special cases reasonably without
938 * having a lot of duplicated code.
940 struct page
* filemap_nopage(struct vm_area_struct
* area
, unsigned long address
, int unused
)
943 struct file
*file
= area
->vm_file
;
944 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
945 struct file_ra_state
*ra
= &file
->f_ra
;
946 struct inode
*inode
= mapping
->host
;
948 unsigned long size
, pgoff
, endoff
;
949 int did_readaround
= 0;
951 pgoff
= ((address
- area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
952 endoff
= ((area
->vm_end
- area
->vm_start
) >> PAGE_CACHE_SHIFT
) + area
->vm_pgoff
;
955 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
957 goto outside_data_content
;
959 /* If we don't want any read-ahead, don't bother */
960 if (VM_RandomReadHint(area
))
964 * The "size" of the file, as far as mmap is concerned, isn't bigger
971 * The readahead code wants to be told about each and every page
972 * so it can build and shrink its windows appropriately
974 * For sequential accesses, we use the generic readahead logic.
976 if (VM_SequentialReadHint(area
))
977 page_cache_readahead(mapping
, ra
, file
, pgoff
);
980 * Do we have something in the page cache already?
983 page
= find_get_page(mapping
, pgoff
);
985 if (VM_SequentialReadHint(area
)) {
986 handle_ra_miss(mapping
, ra
, pgoff
);
992 * Do we miss much more than hit in this file? If so,
993 * stop bothering with read-ahead. It will only hurt.
995 if (ra
->mmap_miss
> ra
->mmap_hit
+ MMAP_LOTSAMISS
)
999 do_page_cache_readahead(mapping
, file
, pgoff
& ~(MMAP_READAROUND
-1), MMAP_READAROUND
);
1003 if (!did_readaround
)
1007 * Ok, found a page in the page cache, now we need to check
1008 * that it's up-to-date.
1010 if (!PageUptodate(page
))
1011 goto page_not_uptodate
;
1015 * Found the page and have a reference on it.
1017 mark_page_accessed(page
);
1020 outside_data_content
:
1022 * An external ptracer can access pages that normally aren't
1025 if (area
->vm_mm
== current
->mm
)
1027 /* Fall through to the non-read-ahead case */
1030 * We're only likely to ever get here if MADV_RANDOM is in
1033 error
= page_cache_read(file
, pgoff
);
1036 * The page we want has now been added to the page cache.
1037 * In the unlikely event that someone removed it in the
1038 * meantime, we'll just come back here and read it again.
1044 * An error return from page_cache_read can result if the
1045 * system is low on memory, or a problem occurs while trying
1048 if (error
== -ENOMEM
)
1053 inc_page_state(pgmajfault
);
1056 /* Did it get unhashed while we waited for it? */
1057 if (!page
->mapping
) {
1059 page_cache_release(page
);
1063 /* Did somebody else get it up-to-date? */
1064 if (PageUptodate(page
)) {
1069 if (!mapping
->a_ops
->readpage(file
, page
)) {
1070 wait_on_page_locked(page
);
1071 if (PageUptodate(page
))
1076 * Umm, take care of errors if the page isn't up-to-date.
1077 * Try to re-read it _once_. We do this synchronously,
1078 * because there really aren't any performance issues here
1079 * and we need to check for errors.
1083 /* Somebody truncated the page on us? */
1084 if (!page
->mapping
) {
1086 page_cache_release(page
);
1090 /* Somebody else successfully read it in? */
1091 if (PageUptodate(page
)) {
1095 ClearPageError(page
);
1096 if (!mapping
->a_ops
->readpage(file
, page
)) {
1097 wait_on_page_locked(page
);
1098 if (PageUptodate(page
))
1103 * Things didn't work out. Return zero to tell the
1104 * mm layer so, possibly freeing the page cache page first.
1106 page_cache_release(page
);
1110 static struct page
* filemap_getpage(struct file
*file
, unsigned long pgoff
,
1113 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
1118 * Do we have something in the page cache already?
1121 page
= find_get_page(mapping
, pgoff
);
1125 goto no_cached_page
;
1129 * Ok, found a page in the page cache, now we need to check
1130 * that it's up-to-date.
1132 if (!PageUptodate(page
))
1133 goto page_not_uptodate
;
1137 * Found the page and have a reference on it.
1139 mark_page_accessed(page
);
1143 error
= page_cache_read(file
, pgoff
);
1146 * The page we want has now been added to the page cache.
1147 * In the unlikely event that someone removed it in the
1148 * meantime, we'll just come back here and read it again.
1154 * An error return from page_cache_read can result if the
1155 * system is low on memory, or a problem occurs while trying
1163 /* Did it get unhashed while we waited for it? */
1164 if (!page
->mapping
) {
1169 /* Did somebody else get it up-to-date? */
1170 if (PageUptodate(page
)) {
1175 if (!mapping
->a_ops
->readpage(file
, page
)) {
1176 wait_on_page_locked(page
);
1177 if (PageUptodate(page
))
1182 * Umm, take care of errors if the page isn't up-to-date.
1183 * Try to re-read it _once_. We do this synchronously,
1184 * because there really aren't any performance issues here
1185 * and we need to check for errors.
1189 /* Somebody truncated the page on us? */
1190 if (!page
->mapping
) {
1194 /* Somebody else successfully read it in? */
1195 if (PageUptodate(page
)) {
1200 ClearPageError(page
);
1201 if (!mapping
->a_ops
->readpage(file
, page
)) {
1202 wait_on_page_locked(page
);
1203 if (PageUptodate(page
))
1208 * Things didn't work out. Return zero to tell the
1209 * mm layer so, possibly freeing the page cache page first.
1212 page_cache_release(page
);
1217 static int filemap_populate(struct vm_area_struct
*vma
,
1221 unsigned long pgoff
,
1224 struct file
*file
= vma
->vm_file
;
1225 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
1226 struct inode
*inode
= mapping
->host
;
1228 struct mm_struct
*mm
= vma
->vm_mm
;
1233 do_page_cache_readahead(mapping
, vma
->vm_file
,
1234 pgoff
, len
>> PAGE_CACHE_SHIFT
);
1237 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1238 if (pgoff
+ (len
>> PAGE_CACHE_SHIFT
) > size
)
1241 page
= filemap_getpage(file
, pgoff
, nonblock
);
1242 if (!page
&& !nonblock
)
1245 err
= install_page(mm
, vma
, addr
, page
, prot
);
1247 page_cache_release(page
);
1261 static struct vm_operations_struct generic_file_vm_ops
= {
1262 .nopage
= filemap_nopage
,
1263 .populate
= filemap_populate
,
1266 /* This is used for a general mmap of a disk file */
1268 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1270 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
1271 struct inode
*inode
= mapping
->host
;
1273 if (!mapping
->a_ops
->readpage
)
1275 update_atime(inode
);
1276 vma
->vm_ops
= &generic_file_vm_ops
;
1281 * This is for filesystems which do not implement ->writepage.
1283 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1285 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1287 return generic_file_mmap(file
, vma
);
1290 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1294 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1298 #endif /* CONFIG_MMU */
1300 static inline struct page
*__read_cache_page(struct address_space
*mapping
,
1301 unsigned long index
,
1302 int (*filler
)(void *,struct page
*),
1305 struct page
*page
, *cached_page
= NULL
;
1308 page
= find_get_page(mapping
, index
);
1311 cached_page
= page_cache_alloc_cold(mapping
);
1313 return ERR_PTR(-ENOMEM
);
1315 err
= add_to_page_cache_lru(cached_page
, mapping
,
1320 /* Presumably ENOMEM for radix tree node */
1321 page_cache_release(cached_page
);
1322 return ERR_PTR(err
);
1326 err
= filler(data
, page
);
1328 page_cache_release(page
);
1329 page
= ERR_PTR(err
);
1333 page_cache_release(cached_page
);
1338 * Read into the page cache. If a page already exists,
1339 * and PageUptodate() is not set, try to fill the page.
1341 struct page
*read_cache_page(struct address_space
*mapping
,
1342 unsigned long index
,
1343 int (*filler
)(void *,struct page
*),
1350 page
= __read_cache_page(mapping
, index
, filler
, data
);
1353 mark_page_accessed(page
);
1354 if (PageUptodate(page
))
1358 if (!page
->mapping
) {
1360 page_cache_release(page
);
1363 if (PageUptodate(page
)) {
1367 err
= filler(data
, page
);
1369 page_cache_release(page
);
1370 page
= ERR_PTR(err
);
1377 * If the page was newly created, increment its refcount and add it to the
1378 * caller's lru-buffering pagevec. This function is specifically for
1379 * generic_file_write().
1381 static inline struct page
*
1382 __grab_cache_page(struct address_space
*mapping
, unsigned long index
,
1383 struct page
**cached_page
, struct pagevec
*lru_pvec
)
1388 page
= find_lock_page(mapping
, index
);
1390 if (!*cached_page
) {
1391 *cached_page
= page_cache_alloc(mapping
);
1395 err
= add_to_page_cache(*cached_page
, mapping
,
1400 page
= *cached_page
;
1401 page_cache_get(page
);
1402 if (!pagevec_add(lru_pvec
, page
))
1403 __pagevec_lru_add(lru_pvec
);
1404 *cached_page
= NULL
;
1410 void remove_suid(struct dentry
*dentry
)
1412 struct iattr newattrs
;
1413 struct inode
*inode
= dentry
->d_inode
;
1414 unsigned int mode
= inode
->i_mode
& (S_ISUID
|S_ISGID
|S_IXGRP
);
1416 if (!(mode
& S_IXGRP
))
1419 /* was any of the uid bits set? */
1420 if (mode
&& !capable(CAP_FSETID
)) {
1421 newattrs
.ia_valid
= ATTR_KILL_SUID
| ATTR_KILL_SGID
;
1422 notify_change(dentry
, &newattrs
);
1427 * Copy as much as we can into the page and return the number of bytes which
1428 * were sucessfully copied. If a fault is encountered then clear the page
1429 * out to (offset+bytes) and return the number of bytes which were copied.
1431 static inline size_t
1432 filemap_copy_from_user(struct page
*page
, unsigned long offset
,
1433 const char __user
*buf
, unsigned bytes
)
1438 kaddr
= kmap_atomic(page
, KM_USER0
);
1439 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
1440 kunmap_atomic(kaddr
, KM_USER0
);
1443 /* Do it the slow way */
1445 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
1448 return bytes
- left
;
1452 __filemap_copy_from_user_iovec(char *vaddr
,
1453 const struct iovec
*iov
, size_t base
, size_t bytes
)
1455 size_t copied
= 0, left
= 0;
1458 char __user
*buf
= iov
->iov_base
+ base
;
1459 int copy
= min(bytes
, iov
->iov_len
- base
);
1462 left
= __copy_from_user(vaddr
, buf
, copy
);
1468 if (unlikely(left
)) {
1469 /* zero the rest of the target like __copy_from_user */
1471 memset(vaddr
, 0, bytes
);
1475 return copied
- left
;
1479 * This has the same sideeffects and return value as filemap_copy_from_user().
1480 * The difference is that on a fault we need to memset the remainder of the
1481 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1482 * single-segment behaviour.
1484 static inline size_t
1485 filemap_copy_from_user_iovec(struct page
*page
, unsigned long offset
,
1486 const struct iovec
*iov
, size_t base
, size_t bytes
)
1491 kaddr
= kmap_atomic(page
, KM_USER0
);
1492 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1494 kunmap_atomic(kaddr
, KM_USER0
);
1495 if (copied
!= bytes
) {
1497 copied
= __filemap_copy_from_user_iovec(kaddr
+ offset
, iov
,
1505 filemap_set_next_iovec(const struct iovec
**iovp
, size_t *basep
, size_t bytes
)
1507 const struct iovec
*iov
= *iovp
;
1508 size_t base
= *basep
;
1511 int copy
= min(bytes
, iov
->iov_len
- base
);
1515 if (iov
->iov_len
== base
) {
1525 * Performs necessary checks before doing a write
1527 * Can adjust writing position aor amount of bytes to write.
1528 * Returns appropriate error code that caller should return or
1529 * zero in case that write should be allowed.
1531 inline int generic_write_checks(struct inode
*inode
,
1532 struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
1534 unsigned long limit
= current
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1536 if (unlikely(*pos
< 0))
1539 if (unlikely(file
->f_error
)) {
1540 int err
= file
->f_error
;
1546 /* FIXME: this is for backwards compatibility with 2.4 */
1547 if (file
->f_flags
& O_APPEND
)
1548 *pos
= i_size_read(inode
);
1550 if (limit
!= RLIM_INFINITY
) {
1551 if (*pos
>= limit
) {
1552 send_sig(SIGXFSZ
, current
, 0);
1555 if (*count
> limit
- (typeof(limit
))*pos
) {
1556 *count
= limit
- (typeof(limit
))*pos
;
1564 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
1565 !(file
->f_flags
& O_LARGEFILE
))) {
1566 if (*pos
>= MAX_NON_LFS
) {
1567 send_sig(SIGXFSZ
, current
, 0);
1570 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
1571 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
1576 * Are we about to exceed the fs block limit ?
1578 * If we have written data it becomes a short write. If we have
1579 * exceeded without writing data we send a signal and return EFBIG.
1580 * Linus frestrict idea will clean these up nicely..
1582 if (likely(!isblk
)) {
1583 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
1584 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
1585 send_sig(SIGXFSZ
, current
, 0);
1588 /* zero-length writes at ->s_maxbytes are OK */
1591 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
1592 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
1595 if (bdev_read_only(inode
->i_bdev
))
1597 isize
= i_size_read(inode
);
1598 if (*pos
>= isize
) {
1599 if (*count
|| *pos
> isize
)
1603 if (*pos
+ *count
> isize
)
1604 *count
= isize
- *pos
;
1608 EXPORT_SYMBOL(generic_write_checks
);
1611 * Write to a file through the page cache.
1613 * We put everything into the page cache prior to writing it. This is not a
1614 * problem when writing full pages. With partial pages, however, we first have
1615 * to read the data into the cache, then dirty the page, and finally schedule
1616 * it for writing by marking it dirty.
1620 generic_file_aio_write_nolock(struct kiocb
*iocb
, const struct iovec
*iov
,
1621 unsigned long nr_segs
, loff_t
*ppos
)
1623 struct file
*file
= iocb
->ki_filp
;
1624 struct address_space
* mapping
= file
->f_dentry
->d_inode
->i_mapping
;
1625 struct address_space_operations
*a_ops
= mapping
->a_ops
;
1626 size_t ocount
; /* original count */
1627 size_t count
; /* after file limit checks */
1628 struct inode
*inode
= mapping
->host
;
1632 struct page
*cached_page
= NULL
;
1633 const int isblk
= S_ISBLK(inode
->i_mode
);
1637 struct pagevec lru_pvec
;
1638 const struct iovec
*cur_iov
= iov
; /* current iovec */
1639 size_t iov_base
= 0; /* offset in the current iovec */
1644 for (seg
= 0; seg
< nr_segs
; seg
++) {
1645 const struct iovec
*iv
= &iov
[seg
];
1648 * If any segment has a negative length, or the cumulative
1649 * length ever wraps negative then return -EINVAL.
1651 ocount
+= iv
->iov_len
;
1652 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
1654 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
1659 ocount
-= iv
->iov_len
; /* This segment is no good */
1665 pagevec_init(&lru_pvec
, 0);
1667 /* We can write back this queue in page reclaim */
1668 current
->backing_dev_info
= mapping
->backing_dev_info
;
1671 err
= generic_write_checks(inode
, file
, &pos
, &count
, isblk
);
1679 remove_suid(file
->f_dentry
);
1680 inode_update_time(inode
, 1);
1682 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1683 if (unlikely(file
->f_flags
& O_DIRECT
)) {
1684 if (count
!= ocount
)
1685 nr_segs
= iov_shorten((struct iovec
*)iov
,
1687 written
= generic_file_direct_IO(WRITE
, iocb
,
1690 loff_t end
= pos
+ written
;
1691 if (end
> i_size_read(inode
) && !isblk
) {
1692 i_size_write(inode
, end
);
1693 mark_inode_dirty(inode
);
1698 * Sync the fs metadata but not the minor inode changes and
1699 * of course not the data as we did direct DMA for the IO.
1701 if (written
>= 0 && file
->f_flags
& O_SYNC
)
1702 status
= generic_osync_inode(inode
, OSYNC_METADATA
);
1703 if (written
>= 0 && !is_sync_kiocb(iocb
))
1704 written
= -EIOCBQUEUED
;
1708 buf
= iov
->iov_base
;
1710 unsigned long index
;
1711 unsigned long offset
;
1714 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
1715 index
= pos
>> PAGE_CACHE_SHIFT
;
1716 bytes
= PAGE_CACHE_SIZE
- offset
;
1721 * Bring in the user page that we will copy from _first_.
1722 * Otherwise there's a nasty deadlock on copying from the
1723 * same page as we're writing to, without it being marked
1726 fault_in_pages_readable(buf
, bytes
);
1728 page
= __grab_cache_page(mapping
,index
,&cached_page
,&lru_pvec
);
1734 status
= a_ops
->prepare_write(file
, page
, offset
, offset
+bytes
);
1735 if (unlikely(status
)) {
1736 loff_t isize
= i_size_read(inode
);
1738 * prepare_write() may have instantiated a few blocks
1739 * outside i_size. Trim these off again.
1742 page_cache_release(page
);
1743 if (pos
+ bytes
> isize
)
1744 vmtruncate(inode
, isize
);
1747 if (likely(nr_segs
== 1))
1748 copied
= filemap_copy_from_user(page
, offset
,
1751 copied
= filemap_copy_from_user_iovec(page
, offset
,
1752 cur_iov
, iov_base
, bytes
);
1753 flush_dcache_page(page
);
1754 status
= a_ops
->commit_write(file
, page
, offset
, offset
+bytes
);
1755 if (likely(copied
> 0)) {
1764 if (unlikely(nr_segs
> 1))
1765 filemap_set_next_iovec(&cur_iov
,
1769 if (unlikely(copied
!= bytes
))
1773 if (!PageReferenced(page
))
1774 SetPageReferenced(page
);
1776 page_cache_release(page
);
1779 balance_dirty_pages_ratelimited(mapping
);
1785 page_cache_release(cached_page
);
1788 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
1791 if ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))
1792 status
= generic_osync_inode(inode
,
1793 OSYNC_METADATA
|OSYNC_DATA
);
1797 err
= written
? written
: status
;
1799 pagevec_lru_add(&lru_pvec
);
1800 current
->backing_dev_info
= 0;
1805 generic_file_write_nolock(struct file
*file
, const struct iovec
*iov
,
1806 unsigned long nr_segs
, loff_t
*ppos
)
1811 init_sync_kiocb(&kiocb
, file
);
1812 ret
= generic_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
1813 if (-EIOCBQUEUED
== ret
)
1814 ret
= wait_on_sync_kiocb(&kiocb
);
1818 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
1819 size_t count
, loff_t pos
)
1821 struct file
*file
= iocb
->ki_filp
;
1822 struct inode
*inode
= file
->f_dentry
->d_inode
->i_mapping
->host
;
1824 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1826 BUG_ON(iocb
->ki_pos
!= pos
);
1828 down(&inode
->i_sem
);
1829 err
= generic_file_aio_write_nolock(iocb
, &local_iov
, 1,
1835 EXPORT_SYMBOL(generic_file_aio_write
);
1836 EXPORT_SYMBOL(generic_file_aio_write_nolock
);
1838 ssize_t
generic_file_write(struct file
*file
, const char __user
*buf
,
1839 size_t count
, loff_t
*ppos
)
1841 struct inode
*inode
= file
->f_dentry
->d_inode
->i_mapping
->host
;
1843 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1845 down(&inode
->i_sem
);
1846 err
= generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
1852 ssize_t
generic_file_readv(struct file
*filp
, const struct iovec
*iov
,
1853 unsigned long nr_segs
, loff_t
*ppos
)
1858 init_sync_kiocb(&kiocb
, filp
);
1859 ret
= __generic_file_aio_read(&kiocb
, iov
, nr_segs
, ppos
);
1860 if (-EIOCBQUEUED
== ret
)
1861 ret
= wait_on_sync_kiocb(&kiocb
);
1865 ssize_t
generic_file_writev(struct file
*file
, const struct iovec
*iov
,
1866 unsigned long nr_segs
, loff_t
* ppos
)
1868 struct inode
*inode
= file
->f_dentry
->d_inode
;
1871 down(&inode
->i_sem
);
1872 ret
= generic_file_write_nolock(file
, iov
, nr_segs
, ppos
);
1878 generic_file_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
1879 loff_t offset
, unsigned long nr_segs
)
1881 struct file
*file
= iocb
->ki_filp
;
1882 struct address_space
*mapping
= file
->f_dentry
->d_inode
->i_mapping
;
1885 if (mapping
->nrpages
) {
1886 retval
= filemap_fdatawrite(mapping
);
1888 retval
= filemap_fdatawait(mapping
);
1893 retval
= mapping
->a_ops
->direct_IO(rw
, iocb
, iov
, offset
, nr_segs
);
1894 if (rw
== WRITE
&& mapping
->nrpages
)
1895 invalidate_inode_pages2(mapping
);