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/export.h>
13 #include <linux/compiler.h>
15 #include <linux/uaccess.h>
16 #include <linux/aio.h>
17 #include <linux/capability.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/gfp.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/backing-dev.h>
29 #include <linux/pagevec.h>
30 #include <linux/blkdev.h>
31 #include <linux/security.h>
32 #include <linux/cpuset.h>
33 #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
34 #include <linux/memcontrol.h>
35 #include <linux/cleancache.h>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/filemap.h>
42 * FIXME: remove all knowledge of the buffer layer from the core VM
44 #include <linux/buffer_head.h> /* for try_to_free_buffers */
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_mutex (truncate_pagecache)
64 * ->private_lock (__free_pte->__set_page_dirty_buffers)
65 * ->swap_lock (exclusive_swap_page, others)
66 * ->mapping->tree_lock
69 * ->i_mmap_mutex (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)
79 * ->i_mutex (generic_file_buffered_write)
80 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
83 * sb_lock (fs/fs-writeback.c)
84 * ->mapping->tree_lock (__sync_single_inode)
87 * ->anon_vma.lock (vma_adjust)
90 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
92 * ->page_table_lock or pte_lock
93 * ->swap_lock (try_to_unmap_one)
94 * ->private_lock (try_to_unmap_one)
95 * ->tree_lock (try_to_unmap_one)
96 * ->zone.lru_lock (follow_page->mark_page_accessed)
97 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
98 * ->private_lock (page_remove_rmap->set_page_dirty)
99 * ->tree_lock (page_remove_rmap->set_page_dirty)
100 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
101 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
102 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
103 * ->inode->i_lock (zap_pte_range->set_page_dirty)
104 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
107 * ->tasklist_lock (memory_failure, collect_procs_ao)
111 * Delete a page from the page cache and free it. Caller has to make
112 * sure the page is locked and that nobody else uses it - or that usage
113 * is safe. The caller must hold the mapping's tree_lock.
115 void __delete_from_page_cache(struct page
*page
)
117 struct address_space
*mapping
= page
->mapping
;
119 trace_mm_filemap_delete_from_page_cache(page
);
121 * if we're uptodate, flush out into the cleancache, otherwise
122 * invalidate any existing cleancache entries. We can't leave
123 * stale data around in the cleancache once our page is gone
125 if (PageUptodate(page
) && PageMappedToDisk(page
))
126 cleancache_put_page(page
);
128 cleancache_invalidate_page(mapping
, page
);
130 radix_tree_delete(&mapping
->page_tree
, page
->index
);
131 page
->mapping
= NULL
;
132 /* Leave page->index set: truncation lookup relies upon it */
134 __dec_zone_page_state(page
, NR_FILE_PAGES
);
135 if (PageSwapBacked(page
))
136 __dec_zone_page_state(page
, NR_SHMEM
);
137 BUG_ON(page_mapped(page
));
140 * Some filesystems seem to re-dirty the page even after
141 * the VM has canceled the dirty bit (eg ext3 journaling).
143 * Fix it up by doing a final dirty accounting check after
144 * having removed the page entirely.
146 if (PageDirty(page
) && mapping_cap_account_dirty(mapping
)) {
147 dec_zone_page_state(page
, NR_FILE_DIRTY
);
148 dec_bdi_stat(mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
153 * delete_from_page_cache - delete page from page cache
154 * @page: the page which the kernel is trying to remove from page cache
156 * This must be called only on pages that have been verified to be in the page
157 * cache and locked. It will never put the page into the free list, the caller
158 * has a reference on the page.
160 void delete_from_page_cache(struct page
*page
)
162 struct address_space
*mapping
= page
->mapping
;
163 void (*freepage
)(struct page
*);
165 BUG_ON(!PageLocked(page
));
167 freepage
= mapping
->a_ops
->freepage
;
168 spin_lock_irq(&mapping
->tree_lock
);
169 __delete_from_page_cache(page
);
170 spin_unlock_irq(&mapping
->tree_lock
);
171 mem_cgroup_uncharge_cache_page(page
);
175 page_cache_release(page
);
177 EXPORT_SYMBOL(delete_from_page_cache
);
179 static int sleep_on_page(void *word
)
185 static int sleep_on_page_killable(void *word
)
188 return fatal_signal_pending(current
) ? -EINTR
: 0;
191 static int filemap_check_errors(struct address_space
*mapping
)
194 /* Check for outstanding write errors */
195 if (test_bit(AS_ENOSPC
, &mapping
->flags
) &&
196 test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
198 if (test_bit(AS_EIO
, &mapping
->flags
) &&
199 test_and_clear_bit(AS_EIO
, &mapping
->flags
))
205 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
206 * @mapping: address space structure to write
207 * @start: offset in bytes where the range starts
208 * @end: offset in bytes where the range ends (inclusive)
209 * @sync_mode: enable synchronous operation
211 * Start writeback against all of a mapping's dirty pages that lie
212 * within the byte offsets <start, end> inclusive.
214 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
215 * opposed to a regular memory cleansing writeback. The difference between
216 * these two operations is that if a dirty page/buffer is encountered, it must
217 * be waited upon, and not just skipped over.
219 int __filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
220 loff_t end
, int sync_mode
)
223 struct writeback_control wbc
= {
224 .sync_mode
= sync_mode
,
225 .nr_to_write
= LONG_MAX
,
226 .range_start
= start
,
230 if (!mapping_cap_writeback_dirty(mapping
))
233 ret
= do_writepages(mapping
, &wbc
);
237 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
240 return __filemap_fdatawrite_range(mapping
, 0, LLONG_MAX
, sync_mode
);
243 int filemap_fdatawrite(struct address_space
*mapping
)
245 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
247 EXPORT_SYMBOL(filemap_fdatawrite
);
249 int filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
252 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
254 EXPORT_SYMBOL(filemap_fdatawrite_range
);
257 * filemap_flush - mostly a non-blocking flush
258 * @mapping: target address_space
260 * This is a mostly non-blocking flush. Not suitable for data-integrity
261 * purposes - I/O may not be started against all dirty pages.
263 int filemap_flush(struct address_space
*mapping
)
265 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
267 EXPORT_SYMBOL(filemap_flush
);
270 * filemap_fdatawait_range - wait for writeback to complete
271 * @mapping: address space structure to wait for
272 * @start_byte: offset in bytes where the range starts
273 * @end_byte: offset in bytes where the range ends (inclusive)
275 * Walk the list of under-writeback pages of the given address space
276 * in the given range and wait for all of them.
278 int filemap_fdatawait_range(struct address_space
*mapping
, loff_t start_byte
,
281 pgoff_t index
= start_byte
>> PAGE_CACHE_SHIFT
;
282 pgoff_t end
= end_byte
>> PAGE_CACHE_SHIFT
;
287 if (end_byte
< start_byte
)
290 pagevec_init(&pvec
, 0);
291 while ((index
<= end
) &&
292 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
293 PAGECACHE_TAG_WRITEBACK
,
294 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
297 for (i
= 0; i
< nr_pages
; i
++) {
298 struct page
*page
= pvec
.pages
[i
];
300 /* until radix tree lookup accepts end_index */
301 if (page
->index
> end
)
304 wait_on_page_writeback(page
);
305 if (TestClearPageError(page
))
308 pagevec_release(&pvec
);
312 ret2
= filemap_check_errors(mapping
);
318 EXPORT_SYMBOL(filemap_fdatawait_range
);
321 * filemap_fdatawait - wait for all under-writeback pages to complete
322 * @mapping: address space structure to wait for
324 * Walk the list of under-writeback pages of the given address space
325 * and wait for all of them.
327 int filemap_fdatawait(struct address_space
*mapping
)
329 loff_t i_size
= i_size_read(mapping
->host
);
334 return filemap_fdatawait_range(mapping
, 0, i_size
- 1);
336 EXPORT_SYMBOL(filemap_fdatawait
);
338 int filemap_write_and_wait(struct address_space
*mapping
)
342 if (mapping
->nrpages
) {
343 err
= filemap_fdatawrite(mapping
);
345 * Even if the above returned error, the pages may be
346 * written partially (e.g. -ENOSPC), so we wait for it.
347 * But the -EIO is special case, it may indicate the worst
348 * thing (e.g. bug) happened, so we avoid waiting for it.
351 int err2
= filemap_fdatawait(mapping
);
356 err
= filemap_check_errors(mapping
);
360 EXPORT_SYMBOL(filemap_write_and_wait
);
363 * filemap_write_and_wait_range - write out & wait on a file range
364 * @mapping: the address_space for the pages
365 * @lstart: offset in bytes where the range starts
366 * @lend: offset in bytes where the range ends (inclusive)
368 * Write out and wait upon file offsets lstart->lend, inclusive.
370 * Note that `lend' is inclusive (describes the last byte to be written) so
371 * that this function can be used to write to the very end-of-file (end = -1).
373 int filemap_write_and_wait_range(struct address_space
*mapping
,
374 loff_t lstart
, loff_t lend
)
378 if (mapping
->nrpages
) {
379 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
381 /* See comment of filemap_write_and_wait() */
383 int err2
= filemap_fdatawait_range(mapping
,
389 err
= filemap_check_errors(mapping
);
393 EXPORT_SYMBOL(filemap_write_and_wait_range
);
396 * replace_page_cache_page - replace a pagecache page with a new one
397 * @old: page to be replaced
398 * @new: page to replace with
399 * @gfp_mask: allocation mode
401 * This function replaces a page in the pagecache with a new one. On
402 * success it acquires the pagecache reference for the new page and
403 * drops it for the old page. Both the old and new pages must be
404 * locked. This function does not add the new page to the LRU, the
405 * caller must do that.
407 * The remove + add is atomic. The only way this function can fail is
408 * memory allocation failure.
410 int replace_page_cache_page(struct page
*old
, struct page
*new, gfp_t gfp_mask
)
414 VM_BUG_ON_PAGE(!PageLocked(old
), old
);
415 VM_BUG_ON_PAGE(!PageLocked(new), new);
416 VM_BUG_ON_PAGE(new->mapping
, new);
418 error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
420 struct address_space
*mapping
= old
->mapping
;
421 void (*freepage
)(struct page
*);
423 pgoff_t offset
= old
->index
;
424 freepage
= mapping
->a_ops
->freepage
;
427 new->mapping
= mapping
;
430 spin_lock_irq(&mapping
->tree_lock
);
431 __delete_from_page_cache(old
);
432 error
= radix_tree_insert(&mapping
->page_tree
, offset
, new);
435 __inc_zone_page_state(new, NR_FILE_PAGES
);
436 if (PageSwapBacked(new))
437 __inc_zone_page_state(new, NR_SHMEM
);
438 spin_unlock_irq(&mapping
->tree_lock
);
439 /* mem_cgroup codes must not be called under tree_lock */
440 mem_cgroup_replace_page_cache(old
, new);
441 radix_tree_preload_end();
444 page_cache_release(old
);
449 EXPORT_SYMBOL_GPL(replace_page_cache_page
);
451 static int page_cache_tree_insert(struct address_space
*mapping
,
457 slot
= radix_tree_lookup_slot(&mapping
->page_tree
, page
->index
);
461 p
= radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
462 if (!radix_tree_exceptional_entry(p
))
464 radix_tree_replace_slot(slot
, page
);
468 error
= radix_tree_insert(&mapping
->page_tree
, page
->index
, page
);
475 * add_to_page_cache_locked - add a locked page to the pagecache
477 * @mapping: the page's address_space
478 * @offset: page index
479 * @gfp_mask: page allocation mode
481 * This function is used to add a page to the pagecache. It must be locked.
482 * This function does not add the page to the LRU. The caller must do that.
484 int add_to_page_cache_locked(struct page
*page
, struct address_space
*mapping
,
485 pgoff_t offset
, gfp_t gfp_mask
)
489 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
490 VM_BUG_ON_PAGE(PageSwapBacked(page
), page
);
492 error
= mem_cgroup_cache_charge(page
, current
->mm
,
493 gfp_mask
& GFP_RECLAIM_MASK
);
497 error
= radix_tree_maybe_preload(gfp_mask
& ~__GFP_HIGHMEM
);
499 mem_cgroup_uncharge_cache_page(page
);
503 page_cache_get(page
);
504 page
->mapping
= mapping
;
505 page
->index
= offset
;
507 spin_lock_irq(&mapping
->tree_lock
);
508 error
= page_cache_tree_insert(mapping
, page
);
509 radix_tree_preload_end();
512 __inc_zone_page_state(page
, NR_FILE_PAGES
);
513 spin_unlock_irq(&mapping
->tree_lock
);
514 trace_mm_filemap_add_to_page_cache(page
);
517 page
->mapping
= NULL
;
518 /* Leave page->index set: truncation relies upon it */
519 spin_unlock_irq(&mapping
->tree_lock
);
520 mem_cgroup_uncharge_cache_page(page
);
521 page_cache_release(page
);
524 EXPORT_SYMBOL(add_to_page_cache_locked
);
526 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
527 pgoff_t offset
, gfp_t gfp_mask
)
531 ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
533 lru_cache_add_file(page
);
536 EXPORT_SYMBOL_GPL(add_to_page_cache_lru
);
539 struct page
*__page_cache_alloc(gfp_t gfp
)
544 if (cpuset_do_page_mem_spread()) {
545 unsigned int cpuset_mems_cookie
;
547 cpuset_mems_cookie
= read_mems_allowed_begin();
548 n
= cpuset_mem_spread_node();
549 page
= alloc_pages_exact_node(n
, gfp
, 0);
550 } while (!page
&& read_mems_allowed_retry(cpuset_mems_cookie
));
554 return alloc_pages(gfp
, 0);
556 EXPORT_SYMBOL(__page_cache_alloc
);
560 * In order to wait for pages to become available there must be
561 * waitqueues associated with pages. By using a hash table of
562 * waitqueues where the bucket discipline is to maintain all
563 * waiters on the same queue and wake all when any of the pages
564 * become available, and for the woken contexts to check to be
565 * sure the appropriate page became available, this saves space
566 * at a cost of "thundering herd" phenomena during rare hash
569 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
571 const struct zone
*zone
= page_zone(page
);
573 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
576 static inline void wake_up_page(struct page
*page
, int bit
)
578 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
581 void wait_on_page_bit(struct page
*page
, int bit_nr
)
583 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
585 if (test_bit(bit_nr
, &page
->flags
))
586 __wait_on_bit(page_waitqueue(page
), &wait
, sleep_on_page
,
587 TASK_UNINTERRUPTIBLE
);
589 EXPORT_SYMBOL(wait_on_page_bit
);
591 int wait_on_page_bit_killable(struct page
*page
, int bit_nr
)
593 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
595 if (!test_bit(bit_nr
, &page
->flags
))
598 return __wait_on_bit(page_waitqueue(page
), &wait
,
599 sleep_on_page_killable
, TASK_KILLABLE
);
603 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
604 * @page: Page defining the wait queue of interest
605 * @waiter: Waiter to add to the queue
607 * Add an arbitrary @waiter to the wait queue for the nominated @page.
609 void add_page_wait_queue(struct page
*page
, wait_queue_t
*waiter
)
611 wait_queue_head_t
*q
= page_waitqueue(page
);
614 spin_lock_irqsave(&q
->lock
, flags
);
615 __add_wait_queue(q
, waiter
);
616 spin_unlock_irqrestore(&q
->lock
, flags
);
618 EXPORT_SYMBOL_GPL(add_page_wait_queue
);
621 * unlock_page - unlock a locked page
624 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
625 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
626 * mechananism between PageLocked pages and PageWriteback pages is shared.
627 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
629 * The mb is necessary to enforce ordering between the clear_bit and the read
630 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
632 void unlock_page(struct page
*page
)
634 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
635 clear_bit_unlock(PG_locked
, &page
->flags
);
636 smp_mb__after_clear_bit();
637 wake_up_page(page
, PG_locked
);
639 EXPORT_SYMBOL(unlock_page
);
642 * end_page_writeback - end writeback against a page
645 void end_page_writeback(struct page
*page
)
647 if (TestClearPageReclaim(page
))
648 rotate_reclaimable_page(page
);
650 if (!test_clear_page_writeback(page
))
653 smp_mb__after_clear_bit();
654 wake_up_page(page
, PG_writeback
);
656 EXPORT_SYMBOL(end_page_writeback
);
659 * __lock_page - get a lock on the page, assuming we need to sleep to get it
660 * @page: the page to lock
662 void __lock_page(struct page
*page
)
664 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
666 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sleep_on_page
,
667 TASK_UNINTERRUPTIBLE
);
669 EXPORT_SYMBOL(__lock_page
);
671 int __lock_page_killable(struct page
*page
)
673 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
675 return __wait_on_bit_lock(page_waitqueue(page
), &wait
,
676 sleep_on_page_killable
, TASK_KILLABLE
);
678 EXPORT_SYMBOL_GPL(__lock_page_killable
);
680 int __lock_page_or_retry(struct page
*page
, struct mm_struct
*mm
,
683 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
685 * CAUTION! In this case, mmap_sem is not released
686 * even though return 0.
688 if (flags
& FAULT_FLAG_RETRY_NOWAIT
)
691 up_read(&mm
->mmap_sem
);
692 if (flags
& FAULT_FLAG_KILLABLE
)
693 wait_on_page_locked_killable(page
);
695 wait_on_page_locked(page
);
698 if (flags
& FAULT_FLAG_KILLABLE
) {
701 ret
= __lock_page_killable(page
);
703 up_read(&mm
->mmap_sem
);
713 * page_cache_next_hole - find the next hole (not-present entry)
716 * @max_scan: maximum range to search
718 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
719 * lowest indexed hole.
721 * Returns: the index of the hole if found, otherwise returns an index
722 * outside of the set specified (in which case 'return - index >=
723 * max_scan' will be true). In rare cases of index wrap-around, 0 will
726 * page_cache_next_hole may be called under rcu_read_lock. However,
727 * like radix_tree_gang_lookup, this will not atomically search a
728 * snapshot of the tree at a single point in time. For example, if a
729 * hole is created at index 5, then subsequently a hole is created at
730 * index 10, page_cache_next_hole covering both indexes may return 10
731 * if called under rcu_read_lock.
733 pgoff_t
page_cache_next_hole(struct address_space
*mapping
,
734 pgoff_t index
, unsigned long max_scan
)
738 for (i
= 0; i
< max_scan
; i
++) {
741 page
= radix_tree_lookup(&mapping
->page_tree
, index
);
742 if (!page
|| radix_tree_exceptional_entry(page
))
751 EXPORT_SYMBOL(page_cache_next_hole
);
754 * page_cache_prev_hole - find the prev hole (not-present entry)
757 * @max_scan: maximum range to search
759 * Search backwards in the range [max(index-max_scan+1, 0), index] for
762 * Returns: the index of the hole if found, otherwise returns an index
763 * outside of the set specified (in which case 'index - return >=
764 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
767 * page_cache_prev_hole may be called under rcu_read_lock. However,
768 * like radix_tree_gang_lookup, this will not atomically search a
769 * snapshot of the tree at a single point in time. For example, if a
770 * hole is created at index 10, then subsequently a hole is created at
771 * index 5, page_cache_prev_hole covering both indexes may return 5 if
772 * called under rcu_read_lock.
774 pgoff_t
page_cache_prev_hole(struct address_space
*mapping
,
775 pgoff_t index
, unsigned long max_scan
)
779 for (i
= 0; i
< max_scan
; i
++) {
782 page
= radix_tree_lookup(&mapping
->page_tree
, index
);
783 if (!page
|| radix_tree_exceptional_entry(page
))
786 if (index
== ULONG_MAX
)
792 EXPORT_SYMBOL(page_cache_prev_hole
);
795 * find_get_entry - find and get a page cache entry
796 * @mapping: the address_space to search
797 * @offset: the page cache index
799 * Looks up the page cache slot at @mapping & @offset. If there is a
800 * page cache page, it is returned with an increased refcount.
802 * If the slot holds a shadow entry of a previously evicted page, it
805 * Otherwise, %NULL is returned.
807 struct page
*find_get_entry(struct address_space
*mapping
, pgoff_t offset
)
815 pagep
= radix_tree_lookup_slot(&mapping
->page_tree
, offset
);
817 page
= radix_tree_deref_slot(pagep
);
820 if (radix_tree_exception(page
)) {
821 if (radix_tree_deref_retry(page
))
824 * Otherwise, shmem/tmpfs must be storing a swap entry
825 * here as an exceptional entry: so return it without
826 * attempting to raise page count.
830 if (!page_cache_get_speculative(page
))
834 * Has the page moved?
835 * This is part of the lockless pagecache protocol. See
836 * include/linux/pagemap.h for details.
838 if (unlikely(page
!= *pagep
)) {
839 page_cache_release(page
);
848 EXPORT_SYMBOL(find_get_entry
);
851 * find_get_page - find and get a page reference
852 * @mapping: the address_space to search
853 * @offset: the page index
855 * Looks up the page cache slot at @mapping & @offset. If there is a
856 * page cache page, it is returned with an increased refcount.
858 * Otherwise, %NULL is returned.
860 struct page
*find_get_page(struct address_space
*mapping
, pgoff_t offset
)
862 struct page
*page
= find_get_entry(mapping
, offset
);
864 if (radix_tree_exceptional_entry(page
))
868 EXPORT_SYMBOL(find_get_page
);
871 * find_lock_entry - locate, pin and lock a page cache entry
872 * @mapping: the address_space to search
873 * @offset: the page cache index
875 * Looks up the page cache slot at @mapping & @offset. If there is a
876 * page cache page, it is returned locked and with an increased
879 * If the slot holds a shadow entry of a previously evicted page, it
882 * Otherwise, %NULL is returned.
884 * find_lock_entry() may sleep.
886 struct page
*find_lock_entry(struct address_space
*mapping
, pgoff_t offset
)
891 page
= find_get_entry(mapping
, offset
);
892 if (page
&& !radix_tree_exception(page
)) {
894 /* Has the page been truncated? */
895 if (unlikely(page
->mapping
!= mapping
)) {
897 page_cache_release(page
);
900 VM_BUG_ON_PAGE(page
->index
!= offset
, page
);
904 EXPORT_SYMBOL(find_lock_entry
);
907 * find_lock_page - locate, pin and lock a pagecache page
908 * @mapping: the address_space to search
909 * @offset: the page index
911 * Looks up the page cache slot at @mapping & @offset. If there is a
912 * page cache page, it is returned locked and with an increased
915 * Otherwise, %NULL is returned.
917 * find_lock_page() may sleep.
919 struct page
*find_lock_page(struct address_space
*mapping
, pgoff_t offset
)
921 struct page
*page
= find_lock_entry(mapping
, offset
);
923 if (radix_tree_exceptional_entry(page
))
927 EXPORT_SYMBOL(find_lock_page
);
930 * find_or_create_page - locate or add a pagecache page
931 * @mapping: the page's address_space
932 * @index: the page's index into the mapping
933 * @gfp_mask: page allocation mode
935 * Looks up the page cache slot at @mapping & @offset. If there is a
936 * page cache page, it is returned locked and with an increased
939 * If the page is not present, a new page is allocated using @gfp_mask
940 * and added to the page cache and the VM's LRU list. The page is
941 * returned locked and with an increased refcount.
943 * On memory exhaustion, %NULL is returned.
945 * find_or_create_page() may sleep, even if @gfp_flags specifies an
948 struct page
*find_or_create_page(struct address_space
*mapping
,
949 pgoff_t index
, gfp_t gfp_mask
)
954 page
= find_lock_page(mapping
, index
);
956 page
= __page_cache_alloc(gfp_mask
);
960 * We want a regular kernel memory (not highmem or DMA etc)
961 * allocation for the radix tree nodes, but we need to honour
962 * the context-specific requirements the caller has asked for.
963 * GFP_RECLAIM_MASK collects those requirements.
965 err
= add_to_page_cache_lru(page
, mapping
, index
,
966 (gfp_mask
& GFP_RECLAIM_MASK
));
968 page_cache_release(page
);
976 EXPORT_SYMBOL(find_or_create_page
);
979 * find_get_entries - gang pagecache lookup
980 * @mapping: The address_space to search
981 * @start: The starting page cache index
982 * @nr_entries: The maximum number of entries
983 * @entries: Where the resulting entries are placed
984 * @indices: The cache indices corresponding to the entries in @entries
986 * find_get_entries() will search for and return a group of up to
987 * @nr_entries entries in the mapping. The entries are placed at
988 * @entries. find_get_entries() takes a reference against any actual
991 * The search returns a group of mapping-contiguous page cache entries
992 * with ascending indexes. There may be holes in the indices due to
995 * Any shadow entries of evicted pages are included in the returned
998 * find_get_entries() returns the number of pages and shadow entries
1001 unsigned find_get_entries(struct address_space
*mapping
,
1002 pgoff_t start
, unsigned int nr_entries
,
1003 struct page
**entries
, pgoff_t
*indices
)
1006 unsigned int ret
= 0;
1007 struct radix_tree_iter iter
;
1014 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, start
) {
1017 page
= radix_tree_deref_slot(slot
);
1018 if (unlikely(!page
))
1020 if (radix_tree_exception(page
)) {
1021 if (radix_tree_deref_retry(page
))
1024 * Otherwise, we must be storing a swap entry
1025 * here as an exceptional entry: so return it
1026 * without attempting to raise page count.
1030 if (!page_cache_get_speculative(page
))
1033 /* Has the page moved? */
1034 if (unlikely(page
!= *slot
)) {
1035 page_cache_release(page
);
1039 indices
[ret
] = iter
.index
;
1040 entries
[ret
] = page
;
1041 if (++ret
== nr_entries
)
1049 * find_get_pages - gang pagecache lookup
1050 * @mapping: The address_space to search
1051 * @start: The starting page index
1052 * @nr_pages: The maximum number of pages
1053 * @pages: Where the resulting pages are placed
1055 * find_get_pages() will search for and return a group of up to
1056 * @nr_pages pages in the mapping. The pages are placed at @pages.
1057 * find_get_pages() takes a reference against the returned pages.
1059 * The search returns a group of mapping-contiguous pages with ascending
1060 * indexes. There may be holes in the indices due to not-present pages.
1062 * find_get_pages() returns the number of pages which were found.
1064 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
1065 unsigned int nr_pages
, struct page
**pages
)
1067 struct radix_tree_iter iter
;
1071 if (unlikely(!nr_pages
))
1076 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, start
) {
1079 page
= radix_tree_deref_slot(slot
);
1080 if (unlikely(!page
))
1083 if (radix_tree_exception(page
)) {
1084 if (radix_tree_deref_retry(page
)) {
1086 * Transient condition which can only trigger
1087 * when entry at index 0 moves out of or back
1088 * to root: none yet gotten, safe to restart.
1090 WARN_ON(iter
.index
);
1094 * Otherwise, shmem/tmpfs must be storing a swap entry
1095 * here as an exceptional entry: so skip over it -
1096 * we only reach this from invalidate_mapping_pages().
1101 if (!page_cache_get_speculative(page
))
1104 /* Has the page moved? */
1105 if (unlikely(page
!= *slot
)) {
1106 page_cache_release(page
);
1111 if (++ret
== nr_pages
)
1120 * find_get_pages_contig - gang contiguous pagecache lookup
1121 * @mapping: The address_space to search
1122 * @index: The starting page index
1123 * @nr_pages: The maximum number of pages
1124 * @pages: Where the resulting pages are placed
1126 * find_get_pages_contig() works exactly like find_get_pages(), except
1127 * that the returned number of pages are guaranteed to be contiguous.
1129 * find_get_pages_contig() returns the number of pages which were found.
1131 unsigned find_get_pages_contig(struct address_space
*mapping
, pgoff_t index
,
1132 unsigned int nr_pages
, struct page
**pages
)
1134 struct radix_tree_iter iter
;
1136 unsigned int ret
= 0;
1138 if (unlikely(!nr_pages
))
1143 radix_tree_for_each_contig(slot
, &mapping
->page_tree
, &iter
, index
) {
1146 page
= radix_tree_deref_slot(slot
);
1147 /* The hole, there no reason to continue */
1148 if (unlikely(!page
))
1151 if (radix_tree_exception(page
)) {
1152 if (radix_tree_deref_retry(page
)) {
1154 * Transient condition which can only trigger
1155 * when entry at index 0 moves out of or back
1156 * to root: none yet gotten, safe to restart.
1161 * Otherwise, shmem/tmpfs must be storing a swap entry
1162 * here as an exceptional entry: so stop looking for
1168 if (!page_cache_get_speculative(page
))
1171 /* Has the page moved? */
1172 if (unlikely(page
!= *slot
)) {
1173 page_cache_release(page
);
1178 * must check mapping and index after taking the ref.
1179 * otherwise we can get both false positives and false
1180 * negatives, which is just confusing to the caller.
1182 if (page
->mapping
== NULL
|| page
->index
!= iter
.index
) {
1183 page_cache_release(page
);
1188 if (++ret
== nr_pages
)
1194 EXPORT_SYMBOL(find_get_pages_contig
);
1197 * find_get_pages_tag - find and return pages that match @tag
1198 * @mapping: the address_space to search
1199 * @index: the starting page index
1200 * @tag: the tag index
1201 * @nr_pages: the maximum number of pages
1202 * @pages: where the resulting pages are placed
1204 * Like find_get_pages, except we only return pages which are tagged with
1205 * @tag. We update @index to index the next page for the traversal.
1207 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
1208 int tag
, unsigned int nr_pages
, struct page
**pages
)
1210 struct radix_tree_iter iter
;
1214 if (unlikely(!nr_pages
))
1219 radix_tree_for_each_tagged(slot
, &mapping
->page_tree
,
1220 &iter
, *index
, tag
) {
1223 page
= radix_tree_deref_slot(slot
);
1224 if (unlikely(!page
))
1227 if (radix_tree_exception(page
)) {
1228 if (radix_tree_deref_retry(page
)) {
1230 * Transient condition which can only trigger
1231 * when entry at index 0 moves out of or back
1232 * to root: none yet gotten, safe to restart.
1237 * This function is never used on a shmem/tmpfs
1238 * mapping, so a swap entry won't be found here.
1243 if (!page_cache_get_speculative(page
))
1246 /* Has the page moved? */
1247 if (unlikely(page
!= *slot
)) {
1248 page_cache_release(page
);
1253 if (++ret
== nr_pages
)
1260 *index
= pages
[ret
- 1]->index
+ 1;
1264 EXPORT_SYMBOL(find_get_pages_tag
);
1267 * grab_cache_page_nowait - returns locked page at given index in given cache
1268 * @mapping: target address_space
1269 * @index: the page index
1271 * Same as grab_cache_page(), but do not wait if the page is unavailable.
1272 * This is intended for speculative data generators, where the data can
1273 * be regenerated if the page couldn't be grabbed. This routine should
1274 * be safe to call while holding the lock for another page.
1276 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
1277 * and deadlock against the caller's locked page.
1280 grab_cache_page_nowait(struct address_space
*mapping
, pgoff_t index
)
1282 struct page
*page
= find_get_page(mapping
, index
);
1285 if (trylock_page(page
))
1287 page_cache_release(page
);
1290 page
= __page_cache_alloc(mapping_gfp_mask(mapping
) & ~__GFP_FS
);
1291 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, GFP_NOFS
)) {
1292 page_cache_release(page
);
1297 EXPORT_SYMBOL(grab_cache_page_nowait
);
1300 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1301 * a _large_ part of the i/o request. Imagine the worst scenario:
1303 * ---R__________________________________________B__________
1304 * ^ reading here ^ bad block(assume 4k)
1306 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1307 * => failing the whole request => read(R) => read(R+1) =>
1308 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1309 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1310 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1312 * It is going insane. Fix it by quickly scaling down the readahead size.
1314 static void shrink_readahead_size_eio(struct file
*filp
,
1315 struct file_ra_state
*ra
)
1321 * do_generic_file_read - generic file read routine
1322 * @filp: the file to read
1323 * @ppos: current file position
1324 * @desc: read_descriptor
1326 * This is a generic file read routine, and uses the
1327 * mapping->a_ops->readpage() function for the actual low-level stuff.
1329 * This is really ugly. But the goto's actually try to clarify some
1330 * of the logic when it comes to error handling etc.
1332 static void do_generic_file_read(struct file
*filp
, loff_t
*ppos
,
1333 read_descriptor_t
*desc
)
1335 struct address_space
*mapping
= filp
->f_mapping
;
1336 struct inode
*inode
= mapping
->host
;
1337 struct file_ra_state
*ra
= &filp
->f_ra
;
1341 unsigned long offset
; /* offset into pagecache page */
1342 unsigned int prev_offset
;
1345 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1346 prev_index
= ra
->prev_pos
>> PAGE_CACHE_SHIFT
;
1347 prev_offset
= ra
->prev_pos
& (PAGE_CACHE_SIZE
-1);
1348 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
1349 offset
= *ppos
& ~PAGE_CACHE_MASK
;
1355 unsigned long nr
, ret
;
1359 page
= find_get_page(mapping
, index
);
1361 page_cache_sync_readahead(mapping
,
1363 index
, last_index
- index
);
1364 page
= find_get_page(mapping
, index
);
1365 if (unlikely(page
== NULL
))
1366 goto no_cached_page
;
1368 if (PageReadahead(page
)) {
1369 page_cache_async_readahead(mapping
,
1371 index
, last_index
- index
);
1373 if (!PageUptodate(page
)) {
1374 if (inode
->i_blkbits
== PAGE_CACHE_SHIFT
||
1375 !mapping
->a_ops
->is_partially_uptodate
)
1376 goto page_not_up_to_date
;
1377 if (!trylock_page(page
))
1378 goto page_not_up_to_date
;
1379 /* Did it get truncated before we got the lock? */
1381 goto page_not_up_to_date_locked
;
1382 if (!mapping
->a_ops
->is_partially_uptodate(page
,
1384 goto page_not_up_to_date_locked
;
1389 * i_size must be checked after we know the page is Uptodate.
1391 * Checking i_size after the check allows us to calculate
1392 * the correct value for "nr", which means the zero-filled
1393 * part of the page is not copied back to userspace (unless
1394 * another truncate extends the file - this is desired though).
1397 isize
= i_size_read(inode
);
1398 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1399 if (unlikely(!isize
|| index
> end_index
)) {
1400 page_cache_release(page
);
1404 /* nr is the maximum number of bytes to copy from this page */
1405 nr
= PAGE_CACHE_SIZE
;
1406 if (index
== end_index
) {
1407 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
1409 page_cache_release(page
);
1415 /* If users can be writing to this page using arbitrary
1416 * virtual addresses, take care about potential aliasing
1417 * before reading the page on the kernel side.
1419 if (mapping_writably_mapped(mapping
))
1420 flush_dcache_page(page
);
1423 * When a sequential read accesses a page several times,
1424 * only mark it as accessed the first time.
1426 if (prev_index
!= index
|| offset
!= prev_offset
)
1427 mark_page_accessed(page
);
1431 * Ok, we have the page, and it's up-to-date, so
1432 * now we can copy it to user space...
1434 * The file_read_actor routine returns how many bytes were
1436 * NOTE! This may not be the same as how much of a user buffer
1437 * we filled up (we may be padding etc), so we can only update
1438 * "pos" here (the actor routine has to update the user buffer
1439 * pointers and the remaining count).
1441 ret
= file_read_actor(desc
, page
, offset
, nr
);
1443 index
+= offset
>> PAGE_CACHE_SHIFT
;
1444 offset
&= ~PAGE_CACHE_MASK
;
1445 prev_offset
= offset
;
1447 page_cache_release(page
);
1448 if (ret
== nr
&& desc
->count
)
1452 page_not_up_to_date
:
1453 /* Get exclusive access to the page ... */
1454 error
= lock_page_killable(page
);
1455 if (unlikely(error
))
1456 goto readpage_error
;
1458 page_not_up_to_date_locked
:
1459 /* Did it get truncated before we got the lock? */
1460 if (!page
->mapping
) {
1462 page_cache_release(page
);
1466 /* Did somebody else fill it already? */
1467 if (PageUptodate(page
)) {
1474 * A previous I/O error may have been due to temporary
1475 * failures, eg. multipath errors.
1476 * PG_error will be set again if readpage fails.
1478 ClearPageError(page
);
1479 /* Start the actual read. The read will unlock the page. */
1480 error
= mapping
->a_ops
->readpage(filp
, page
);
1482 if (unlikely(error
)) {
1483 if (error
== AOP_TRUNCATED_PAGE
) {
1484 page_cache_release(page
);
1487 goto readpage_error
;
1490 if (!PageUptodate(page
)) {
1491 error
= lock_page_killable(page
);
1492 if (unlikely(error
))
1493 goto readpage_error
;
1494 if (!PageUptodate(page
)) {
1495 if (page
->mapping
== NULL
) {
1497 * invalidate_mapping_pages got it
1500 page_cache_release(page
);
1504 shrink_readahead_size_eio(filp
, ra
);
1506 goto readpage_error
;
1514 /* UHHUH! A synchronous read error occurred. Report it */
1515 desc
->error
= error
;
1516 page_cache_release(page
);
1521 * Ok, it wasn't cached, so we need to create a new
1524 page
= page_cache_alloc_cold(mapping
);
1526 desc
->error
= -ENOMEM
;
1529 error
= add_to_page_cache_lru(page
, mapping
,
1532 page_cache_release(page
);
1533 if (error
== -EEXIST
)
1535 desc
->error
= error
;
1542 ra
->prev_pos
= prev_index
;
1543 ra
->prev_pos
<<= PAGE_CACHE_SHIFT
;
1544 ra
->prev_pos
|= prev_offset
;
1546 *ppos
= ((loff_t
)index
<< PAGE_CACHE_SHIFT
) + offset
;
1547 file_accessed(filp
);
1550 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1551 unsigned long offset
, unsigned long size
)
1554 unsigned long left
, count
= desc
->count
;
1560 * Faults on the destination of a read are common, so do it before
1563 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1564 kaddr
= kmap_atomic(page
);
1565 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1566 kaddr
+ offset
, size
);
1567 kunmap_atomic(kaddr
);
1572 /* Do it the slow way */
1574 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1579 desc
->error
= -EFAULT
;
1582 desc
->count
= count
- size
;
1583 desc
->written
+= size
;
1584 desc
->arg
.buf
+= size
;
1589 * Performs necessary checks before doing a write
1590 * @iov: io vector request
1591 * @nr_segs: number of segments in the iovec
1592 * @count: number of bytes to write
1593 * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1595 * Adjust number of segments and amount of bytes to write (nr_segs should be
1596 * properly initialized first). Returns appropriate error code that caller
1597 * should return or zero in case that write should be allowed.
1599 int generic_segment_checks(const struct iovec
*iov
,
1600 unsigned long *nr_segs
, size_t *count
, int access_flags
)
1604 for (seg
= 0; seg
< *nr_segs
; seg
++) {
1605 const struct iovec
*iv
= &iov
[seg
];
1608 * If any segment has a negative length, or the cumulative
1609 * length ever wraps negative then return -EINVAL.
1612 if (unlikely((ssize_t
)(cnt
|iv
->iov_len
) < 0))
1614 if (access_ok(access_flags
, iv
->iov_base
, iv
->iov_len
))
1619 cnt
-= iv
->iov_len
; /* This segment is no good */
1625 EXPORT_SYMBOL(generic_segment_checks
);
1628 * generic_file_aio_read - generic filesystem read routine
1629 * @iocb: kernel I/O control block
1630 * @iov: io vector request
1631 * @nr_segs: number of segments in the iovec
1632 * @pos: current file position
1634 * This is the "read()" routine for all filesystems
1635 * that can use the page cache directly.
1638 generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1639 unsigned long nr_segs
, loff_t pos
)
1641 struct file
*filp
= iocb
->ki_filp
;
1643 unsigned long seg
= 0;
1645 loff_t
*ppos
= &iocb
->ki_pos
;
1648 retval
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_WRITE
);
1652 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1653 if (filp
->f_flags
& O_DIRECT
) {
1655 struct address_space
*mapping
;
1656 struct inode
*inode
;
1658 mapping
= filp
->f_mapping
;
1659 inode
= mapping
->host
;
1661 goto out
; /* skip atime */
1662 size
= i_size_read(inode
);
1663 retval
= filemap_write_and_wait_range(mapping
, pos
,
1664 pos
+ iov_length(iov
, nr_segs
) - 1);
1666 retval
= mapping
->a_ops
->direct_IO(READ
, iocb
,
1670 *ppos
= pos
+ retval
;
1675 * Btrfs can have a short DIO read if we encounter
1676 * compressed extents, so if there was an error, or if
1677 * we've already read everything we wanted to, or if
1678 * there was a short read because we hit EOF, go ahead
1679 * and return. Otherwise fallthrough to buffered io for
1680 * the rest of the read.
1682 if (retval
< 0 || !count
|| *ppos
>= size
) {
1683 file_accessed(filp
);
1689 for (seg
= 0; seg
< nr_segs
; seg
++) {
1690 read_descriptor_t desc
;
1694 * If we did a short DIO read we need to skip the section of the
1695 * iov that we've already read data into.
1698 if (count
> iov
[seg
].iov_len
) {
1699 count
-= iov
[seg
].iov_len
;
1707 desc
.arg
.buf
= iov
[seg
].iov_base
+ offset
;
1708 desc
.count
= iov
[seg
].iov_len
- offset
;
1709 if (desc
.count
== 0)
1712 do_generic_file_read(filp
, ppos
, &desc
);
1713 retval
+= desc
.written
;
1715 retval
= retval
?: desc
.error
;
1724 EXPORT_SYMBOL(generic_file_aio_read
);
1728 * page_cache_read - adds requested page to the page cache if not already there
1729 * @file: file to read
1730 * @offset: page index
1732 * This adds the requested page to the page cache if it isn't already there,
1733 * and schedules an I/O to read in its contents from disk.
1735 static int page_cache_read(struct file
*file
, pgoff_t offset
)
1737 struct address_space
*mapping
= file
->f_mapping
;
1742 page
= page_cache_alloc_cold(mapping
);
1746 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1748 ret
= mapping
->a_ops
->readpage(file
, page
);
1749 else if (ret
== -EEXIST
)
1750 ret
= 0; /* losing race to add is OK */
1752 page_cache_release(page
);
1754 } while (ret
== AOP_TRUNCATED_PAGE
);
1759 #define MMAP_LOTSAMISS (100)
1762 * Synchronous readahead happens when we don't even find
1763 * a page in the page cache at all.
1765 static void do_sync_mmap_readahead(struct vm_area_struct
*vma
,
1766 struct file_ra_state
*ra
,
1770 unsigned long ra_pages
;
1771 struct address_space
*mapping
= file
->f_mapping
;
1773 /* If we don't want any read-ahead, don't bother */
1774 if (vma
->vm_flags
& VM_RAND_READ
)
1779 if (vma
->vm_flags
& VM_SEQ_READ
) {
1780 page_cache_sync_readahead(mapping
, ra
, file
, offset
,
1785 /* Avoid banging the cache line if not needed */
1786 if (ra
->mmap_miss
< MMAP_LOTSAMISS
* 10)
1790 * Do we miss much more than hit in this file? If so,
1791 * stop bothering with read-ahead. It will only hurt.
1793 if (ra
->mmap_miss
> MMAP_LOTSAMISS
)
1799 ra_pages
= max_sane_readahead(ra
->ra_pages
);
1800 ra
->start
= max_t(long, 0, offset
- ra_pages
/ 2);
1801 ra
->size
= ra_pages
;
1802 ra
->async_size
= ra_pages
/ 4;
1803 ra_submit(ra
, mapping
, file
);
1807 * Asynchronous readahead happens when we find the page and PG_readahead,
1808 * so we want to possibly extend the readahead further..
1810 static void do_async_mmap_readahead(struct vm_area_struct
*vma
,
1811 struct file_ra_state
*ra
,
1816 struct address_space
*mapping
= file
->f_mapping
;
1818 /* If we don't want any read-ahead, don't bother */
1819 if (vma
->vm_flags
& VM_RAND_READ
)
1821 if (ra
->mmap_miss
> 0)
1823 if (PageReadahead(page
))
1824 page_cache_async_readahead(mapping
, ra
, file
,
1825 page
, offset
, ra
->ra_pages
);
1829 * filemap_fault - read in file data for page fault handling
1830 * @vma: vma in which the fault was taken
1831 * @vmf: struct vm_fault containing details of the fault
1833 * filemap_fault() is invoked via the vma operations vector for a
1834 * mapped memory region to read in file data during a page fault.
1836 * The goto's are kind of ugly, but this streamlines the normal case of having
1837 * it in the page cache, and handles the special cases reasonably without
1838 * having a lot of duplicated code.
1840 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1843 struct file
*file
= vma
->vm_file
;
1844 struct address_space
*mapping
= file
->f_mapping
;
1845 struct file_ra_state
*ra
= &file
->f_ra
;
1846 struct inode
*inode
= mapping
->host
;
1847 pgoff_t offset
= vmf
->pgoff
;
1852 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1854 return VM_FAULT_SIGBUS
;
1857 * Do we have something in the page cache already?
1859 page
= find_get_page(mapping
, offset
);
1860 if (likely(page
) && !(vmf
->flags
& FAULT_FLAG_TRIED
)) {
1862 * We found the page, so try async readahead before
1863 * waiting for the lock.
1865 do_async_mmap_readahead(vma
, ra
, file
, page
, offset
);
1867 /* No page in the page cache at all */
1868 do_sync_mmap_readahead(vma
, ra
, file
, offset
);
1869 count_vm_event(PGMAJFAULT
);
1870 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1871 ret
= VM_FAULT_MAJOR
;
1873 page
= find_get_page(mapping
, offset
);
1875 goto no_cached_page
;
1878 if (!lock_page_or_retry(page
, vma
->vm_mm
, vmf
->flags
)) {
1879 page_cache_release(page
);
1880 return ret
| VM_FAULT_RETRY
;
1883 /* Did it get truncated? */
1884 if (unlikely(page
->mapping
!= mapping
)) {
1889 VM_BUG_ON_PAGE(page
->index
!= offset
, page
);
1892 * We have a locked page in the page cache, now we need to check
1893 * that it's up-to-date. If not, it is going to be due to an error.
1895 if (unlikely(!PageUptodate(page
)))
1896 goto page_not_uptodate
;
1899 * Found the page and have a reference on it.
1900 * We must recheck i_size under page lock.
1902 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1903 if (unlikely(offset
>= size
)) {
1905 page_cache_release(page
);
1906 return VM_FAULT_SIGBUS
;
1910 return ret
| VM_FAULT_LOCKED
;
1914 * We're only likely to ever get here if MADV_RANDOM is in
1917 error
= page_cache_read(file
, offset
);
1920 * The page we want has now been added to the page cache.
1921 * In the unlikely event that someone removed it in the
1922 * meantime, we'll just come back here and read it again.
1928 * An error return from page_cache_read can result if the
1929 * system is low on memory, or a problem occurs while trying
1932 if (error
== -ENOMEM
)
1933 return VM_FAULT_OOM
;
1934 return VM_FAULT_SIGBUS
;
1938 * Umm, take care of errors if the page isn't up-to-date.
1939 * Try to re-read it _once_. We do this synchronously,
1940 * because there really aren't any performance issues here
1941 * and we need to check for errors.
1943 ClearPageError(page
);
1944 error
= mapping
->a_ops
->readpage(file
, page
);
1946 wait_on_page_locked(page
);
1947 if (!PageUptodate(page
))
1950 page_cache_release(page
);
1952 if (!error
|| error
== AOP_TRUNCATED_PAGE
)
1955 /* Things didn't work out. Return zero to tell the mm layer so. */
1956 shrink_readahead_size_eio(file
, ra
);
1957 return VM_FAULT_SIGBUS
;
1959 EXPORT_SYMBOL(filemap_fault
);
1961 int filemap_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1963 struct page
*page
= vmf
->page
;
1964 struct inode
*inode
= file_inode(vma
->vm_file
);
1965 int ret
= VM_FAULT_LOCKED
;
1967 sb_start_pagefault(inode
->i_sb
);
1968 file_update_time(vma
->vm_file
);
1970 if (page
->mapping
!= inode
->i_mapping
) {
1972 ret
= VM_FAULT_NOPAGE
;
1976 * We mark the page dirty already here so that when freeze is in
1977 * progress, we are guaranteed that writeback during freezing will
1978 * see the dirty page and writeprotect it again.
1980 set_page_dirty(page
);
1981 wait_for_stable_page(page
);
1983 sb_end_pagefault(inode
->i_sb
);
1986 EXPORT_SYMBOL(filemap_page_mkwrite
);
1988 const struct vm_operations_struct generic_file_vm_ops
= {
1989 .fault
= filemap_fault
,
1990 .page_mkwrite
= filemap_page_mkwrite
,
1991 .remap_pages
= generic_file_remap_pages
,
1994 /* This is used for a general mmap of a disk file */
1996 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1998 struct address_space
*mapping
= file
->f_mapping
;
2000 if (!mapping
->a_ops
->readpage
)
2002 file_accessed(file
);
2003 vma
->vm_ops
= &generic_file_vm_ops
;
2008 * This is for filesystems which do not implement ->writepage.
2010 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2012 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2014 return generic_file_mmap(file
, vma
);
2017 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
2021 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
2025 #endif /* CONFIG_MMU */
2027 EXPORT_SYMBOL(generic_file_mmap
);
2028 EXPORT_SYMBOL(generic_file_readonly_mmap
);
2030 static struct page
*wait_on_page_read(struct page
*page
)
2032 if (!IS_ERR(page
)) {
2033 wait_on_page_locked(page
);
2034 if (!PageUptodate(page
)) {
2035 page_cache_release(page
);
2036 page
= ERR_PTR(-EIO
);
2042 static struct page
*__read_cache_page(struct address_space
*mapping
,
2044 int (*filler
)(void *, struct page
*),
2051 page
= find_get_page(mapping
, index
);
2053 page
= __page_cache_alloc(gfp
| __GFP_COLD
);
2055 return ERR_PTR(-ENOMEM
);
2056 err
= add_to_page_cache_lru(page
, mapping
, index
, gfp
);
2057 if (unlikely(err
)) {
2058 page_cache_release(page
);
2061 /* Presumably ENOMEM for radix tree node */
2062 return ERR_PTR(err
);
2064 err
= filler(data
, page
);
2066 page_cache_release(page
);
2067 page
= ERR_PTR(err
);
2069 page
= wait_on_page_read(page
);
2075 static struct page
*do_read_cache_page(struct address_space
*mapping
,
2077 int (*filler
)(void *, struct page
*),
2086 page
= __read_cache_page(mapping
, index
, filler
, data
, gfp
);
2089 if (PageUptodate(page
))
2093 if (!page
->mapping
) {
2095 page_cache_release(page
);
2098 if (PageUptodate(page
)) {
2102 err
= filler(data
, page
);
2104 page_cache_release(page
);
2105 return ERR_PTR(err
);
2107 page
= wait_on_page_read(page
);
2112 mark_page_accessed(page
);
2117 * read_cache_page - read into page cache, fill it if needed
2118 * @mapping: the page's address_space
2119 * @index: the page index
2120 * @filler: function to perform the read
2121 * @data: first arg to filler(data, page) function, often left as NULL
2123 * Read into the page cache. If a page already exists, and PageUptodate() is
2124 * not set, try to fill the page and wait for it to become unlocked.
2126 * If the page does not get brought uptodate, return -EIO.
2128 struct page
*read_cache_page(struct address_space
*mapping
,
2130 int (*filler
)(void *, struct page
*),
2133 return do_read_cache_page(mapping
, index
, filler
, data
, mapping_gfp_mask(mapping
));
2135 EXPORT_SYMBOL(read_cache_page
);
2138 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2139 * @mapping: the page's address_space
2140 * @index: the page index
2141 * @gfp: the page allocator flags to use if allocating
2143 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
2144 * any new page allocations done using the specified allocation flags.
2146 * If the page does not get brought uptodate, return -EIO.
2148 struct page
*read_cache_page_gfp(struct address_space
*mapping
,
2152 filler_t
*filler
= (filler_t
*)mapping
->a_ops
->readpage
;
2154 return do_read_cache_page(mapping
, index
, filler
, NULL
, gfp
);
2156 EXPORT_SYMBOL(read_cache_page_gfp
);
2158 static size_t __iovec_copy_from_user_inatomic(char *vaddr
,
2159 const struct iovec
*iov
, size_t base
, size_t bytes
)
2161 size_t copied
= 0, left
= 0;
2164 char __user
*buf
= iov
->iov_base
+ base
;
2165 int copy
= min(bytes
, iov
->iov_len
- base
);
2168 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
2177 return copied
- left
;
2181 * Copy as much as we can into the page and return the number of bytes which
2182 * were successfully copied. If a fault is encountered then return the number of
2183 * bytes which were copied.
2185 size_t iov_iter_copy_from_user_atomic(struct page
*page
,
2186 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
2191 kaddr
= kmap_atomic(page
);
2192 if (likely(i
->nr_segs
== 1)) {
2194 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
2195 left
= __copy_from_user_inatomic(kaddr
+ offset
, buf
, bytes
);
2196 copied
= bytes
- left
;
2198 copied
= __iovec_copy_from_user_inatomic(kaddr
+ offset
,
2199 i
->iov
, i
->iov_offset
, bytes
);
2201 kunmap_atomic(kaddr
);
2205 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic
);
2208 * This has the same sideeffects and return value as
2209 * iov_iter_copy_from_user_atomic().
2210 * The difference is that it attempts to resolve faults.
2211 * Page must not be locked.
2213 size_t iov_iter_copy_from_user(struct page
*page
,
2214 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
2220 if (likely(i
->nr_segs
== 1)) {
2222 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
2223 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
2224 copied
= bytes
- left
;
2226 copied
= __iovec_copy_from_user_inatomic(kaddr
+ offset
,
2227 i
->iov
, i
->iov_offset
, bytes
);
2232 EXPORT_SYMBOL(iov_iter_copy_from_user
);
2234 void iov_iter_advance(struct iov_iter
*i
, size_t bytes
)
2236 BUG_ON(i
->count
< bytes
);
2238 if (likely(i
->nr_segs
== 1)) {
2239 i
->iov_offset
+= bytes
;
2242 const struct iovec
*iov
= i
->iov
;
2243 size_t base
= i
->iov_offset
;
2244 unsigned long nr_segs
= i
->nr_segs
;
2247 * The !iov->iov_len check ensures we skip over unlikely
2248 * zero-length segments (without overruning the iovec).
2250 while (bytes
|| unlikely(i
->count
&& !iov
->iov_len
)) {
2253 copy
= min(bytes
, iov
->iov_len
- base
);
2254 BUG_ON(!i
->count
|| i
->count
< copy
);
2258 if (iov
->iov_len
== base
) {
2265 i
->iov_offset
= base
;
2266 i
->nr_segs
= nr_segs
;
2269 EXPORT_SYMBOL(iov_iter_advance
);
2272 * Fault in the first iovec of the given iov_iter, to a maximum length
2273 * of bytes. Returns 0 on success, or non-zero if the memory could not be
2274 * accessed (ie. because it is an invalid address).
2276 * writev-intensive code may want this to prefault several iovecs -- that
2277 * would be possible (callers must not rely on the fact that _only_ the
2278 * first iovec will be faulted with the current implementation).
2280 int iov_iter_fault_in_readable(struct iov_iter
*i
, size_t bytes
)
2282 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
2283 bytes
= min(bytes
, i
->iov
->iov_len
- i
->iov_offset
);
2284 return fault_in_pages_readable(buf
, bytes
);
2286 EXPORT_SYMBOL(iov_iter_fault_in_readable
);
2289 * Return the count of just the current iov_iter segment.
2291 size_t iov_iter_single_seg_count(const struct iov_iter
*i
)
2293 const struct iovec
*iov
= i
->iov
;
2294 if (i
->nr_segs
== 1)
2297 return min(i
->count
, iov
->iov_len
- i
->iov_offset
);
2299 EXPORT_SYMBOL(iov_iter_single_seg_count
);
2302 * Performs necessary checks before doing a write
2304 * Can adjust writing position or amount of bytes to write.
2305 * Returns appropriate error code that caller should return or
2306 * zero in case that write should be allowed.
2308 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
2310 struct inode
*inode
= file
->f_mapping
->host
;
2311 unsigned long limit
= rlimit(RLIMIT_FSIZE
);
2313 if (unlikely(*pos
< 0))
2317 /* FIXME: this is for backwards compatibility with 2.4 */
2318 if (file
->f_flags
& O_APPEND
)
2319 *pos
= i_size_read(inode
);
2321 if (limit
!= RLIM_INFINITY
) {
2322 if (*pos
>= limit
) {
2323 send_sig(SIGXFSZ
, current
, 0);
2326 if (*count
> limit
- (typeof(limit
))*pos
) {
2327 *count
= limit
- (typeof(limit
))*pos
;
2335 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
2336 !(file
->f_flags
& O_LARGEFILE
))) {
2337 if (*pos
>= MAX_NON_LFS
) {
2340 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
2341 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
2346 * Are we about to exceed the fs block limit ?
2348 * If we have written data it becomes a short write. If we have
2349 * exceeded without writing data we send a signal and return EFBIG.
2350 * Linus frestrict idea will clean these up nicely..
2352 if (likely(!isblk
)) {
2353 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
2354 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
2357 /* zero-length writes at ->s_maxbytes are OK */
2360 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
2361 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
2365 if (bdev_read_only(I_BDEV(inode
)))
2367 isize
= i_size_read(inode
);
2368 if (*pos
>= isize
) {
2369 if (*count
|| *pos
> isize
)
2373 if (*pos
+ *count
> isize
)
2374 *count
= isize
- *pos
;
2381 EXPORT_SYMBOL(generic_write_checks
);
2383 int pagecache_write_begin(struct file
*file
, struct address_space
*mapping
,
2384 loff_t pos
, unsigned len
, unsigned flags
,
2385 struct page
**pagep
, void **fsdata
)
2387 const struct address_space_operations
*aops
= mapping
->a_ops
;
2389 return aops
->write_begin(file
, mapping
, pos
, len
, flags
,
2392 EXPORT_SYMBOL(pagecache_write_begin
);
2394 int pagecache_write_end(struct file
*file
, struct address_space
*mapping
,
2395 loff_t pos
, unsigned len
, unsigned copied
,
2396 struct page
*page
, void *fsdata
)
2398 const struct address_space_operations
*aops
= mapping
->a_ops
;
2400 mark_page_accessed(page
);
2401 return aops
->write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
2403 EXPORT_SYMBOL(pagecache_write_end
);
2406 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2407 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
2408 size_t count
, size_t ocount
)
2410 struct file
*file
= iocb
->ki_filp
;
2411 struct address_space
*mapping
= file
->f_mapping
;
2412 struct inode
*inode
= mapping
->host
;
2417 if (count
!= ocount
)
2418 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
2420 write_len
= iov_length(iov
, *nr_segs
);
2421 end
= (pos
+ write_len
- 1) >> PAGE_CACHE_SHIFT
;
2423 written
= filemap_write_and_wait_range(mapping
, pos
, pos
+ write_len
- 1);
2428 * After a write we want buffered reads to be sure to go to disk to get
2429 * the new data. We invalidate clean cached page from the region we're
2430 * about to write. We do this *before* the write so that we can return
2431 * without clobbering -EIOCBQUEUED from ->direct_IO().
2433 if (mapping
->nrpages
) {
2434 written
= invalidate_inode_pages2_range(mapping
,
2435 pos
>> PAGE_CACHE_SHIFT
, end
);
2437 * If a page can not be invalidated, return 0 to fall back
2438 * to buffered write.
2441 if (written
== -EBUSY
)
2447 written
= mapping
->a_ops
->direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
2450 * Finally, try again to invalidate clean pages which might have been
2451 * cached by non-direct readahead, or faulted in by get_user_pages()
2452 * if the source of the write was an mmap'ed region of the file
2453 * we're writing. Either one is a pretty crazy thing to do,
2454 * so we don't support it 100%. If this invalidation
2455 * fails, tough, the write still worked...
2457 if (mapping
->nrpages
) {
2458 invalidate_inode_pages2_range(mapping
,
2459 pos
>> PAGE_CACHE_SHIFT
, end
);
2464 if (pos
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
2465 i_size_write(inode
, pos
);
2466 mark_inode_dirty(inode
);
2473 EXPORT_SYMBOL(generic_file_direct_write
);
2476 * Find or create a page at the given pagecache position. Return the locked
2477 * page. This function is specifically for buffered writes.
2479 struct page
*grab_cache_page_write_begin(struct address_space
*mapping
,
2480 pgoff_t index
, unsigned flags
)
2485 gfp_t gfp_notmask
= 0;
2487 gfp_mask
= mapping_gfp_mask(mapping
);
2488 if (mapping_cap_account_dirty(mapping
))
2489 gfp_mask
|= __GFP_WRITE
;
2490 if (flags
& AOP_FLAG_NOFS
)
2491 gfp_notmask
= __GFP_FS
;
2493 page
= find_lock_page(mapping
, index
);
2497 page
= __page_cache_alloc(gfp_mask
& ~gfp_notmask
);
2500 status
= add_to_page_cache_lru(page
, mapping
, index
,
2501 GFP_KERNEL
& ~gfp_notmask
);
2502 if (unlikely(status
)) {
2503 page_cache_release(page
);
2504 if (status
== -EEXIST
)
2509 wait_for_stable_page(page
);
2512 EXPORT_SYMBOL(grab_cache_page_write_begin
);
2514 static ssize_t
generic_perform_write(struct file
*file
,
2515 struct iov_iter
*i
, loff_t pos
)
2517 struct address_space
*mapping
= file
->f_mapping
;
2518 const struct address_space_operations
*a_ops
= mapping
->a_ops
;
2520 ssize_t written
= 0;
2521 unsigned int flags
= 0;
2524 * Copies from kernel address space cannot fail (NFSD is a big user).
2526 if (segment_eq(get_fs(), KERNEL_DS
))
2527 flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
2531 unsigned long offset
; /* Offset into pagecache page */
2532 unsigned long bytes
; /* Bytes to write to page */
2533 size_t copied
; /* Bytes copied from user */
2536 offset
= (pos
& (PAGE_CACHE_SIZE
- 1));
2537 bytes
= min_t(unsigned long, PAGE_CACHE_SIZE
- offset
,
2542 * Bring in the user page that we will copy from _first_.
2543 * Otherwise there's a nasty deadlock on copying from the
2544 * same page as we're writing to, without it being marked
2547 * Not only is this an optimisation, but it is also required
2548 * to check that the address is actually valid, when atomic
2549 * usercopies are used, below.
2551 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
2556 status
= a_ops
->write_begin(file
, mapping
, pos
, bytes
, flags
,
2558 if (unlikely(status
))
2561 if (mapping_writably_mapped(mapping
))
2562 flush_dcache_page(page
);
2564 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
2565 flush_dcache_page(page
);
2567 mark_page_accessed(page
);
2568 status
= a_ops
->write_end(file
, mapping
, pos
, bytes
, copied
,
2570 if (unlikely(status
< 0))
2576 iov_iter_advance(i
, copied
);
2577 if (unlikely(copied
== 0)) {
2579 * If we were unable to copy any data at all, we must
2580 * fall back to a single segment length write.
2582 * If we didn't fallback here, we could livelock
2583 * because not all segments in the iov can be copied at
2584 * once without a pagefault.
2586 bytes
= min_t(unsigned long, PAGE_CACHE_SIZE
- offset
,
2587 iov_iter_single_seg_count(i
));
2593 balance_dirty_pages_ratelimited(mapping
);
2594 if (fatal_signal_pending(current
)) {
2598 } while (iov_iter_count(i
));
2600 return written
? written
: status
;
2604 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2605 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
2606 size_t count
, ssize_t written
)
2608 struct file
*file
= iocb
->ki_filp
;
2612 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
2613 status
= generic_perform_write(file
, &i
, pos
);
2615 if (likely(status
>= 0)) {
2617 *ppos
= pos
+ status
;
2620 return written
? written
: status
;
2622 EXPORT_SYMBOL(generic_file_buffered_write
);
2625 * __generic_file_aio_write - write data to a file
2626 * @iocb: IO state structure (file, offset, etc.)
2627 * @iov: vector with data to write
2628 * @nr_segs: number of segments in the vector
2629 * @ppos: position where to write
2631 * This function does all the work needed for actually writing data to a
2632 * file. It does all basic checks, removes SUID from the file, updates
2633 * modification times and calls proper subroutines depending on whether we
2634 * do direct IO or a standard buffered write.
2636 * It expects i_mutex to be grabbed unless we work on a block device or similar
2637 * object which does not need locking at all.
2639 * This function does *not* take care of syncing data in case of O_SYNC write.
2640 * A caller has to handle it. This is mainly due to the fact that we want to
2641 * avoid syncing under i_mutex.
2643 ssize_t
__generic_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2644 unsigned long nr_segs
, loff_t
*ppos
)
2646 struct file
*file
= iocb
->ki_filp
;
2647 struct address_space
* mapping
= file
->f_mapping
;
2648 size_t ocount
; /* original count */
2649 size_t count
; /* after file limit checks */
2650 struct inode
*inode
= mapping
->host
;
2656 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
2663 /* We can write back this queue in page reclaim */
2664 current
->backing_dev_info
= mapping
->backing_dev_info
;
2667 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2674 err
= file_remove_suid(file
);
2678 err
= file_update_time(file
);
2682 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2683 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2685 ssize_t written_buffered
;
2687 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
, pos
,
2688 ppos
, count
, ocount
);
2689 if (written
< 0 || written
== count
)
2692 * direct-io write to a hole: fall through to buffered I/O
2693 * for completing the rest of the request.
2697 written_buffered
= generic_file_buffered_write(iocb
, iov
,
2698 nr_segs
, pos
, ppos
, count
,
2701 * If generic_file_buffered_write() retuned a synchronous error
2702 * then we want to return the number of bytes which were
2703 * direct-written, or the error code if that was zero. Note
2704 * that this differs from normal direct-io semantics, which
2705 * will return -EFOO even if some bytes were written.
2707 if (written_buffered
< 0) {
2708 err
= written_buffered
;
2713 * We need to ensure that the page cache pages are written to
2714 * disk and invalidated to preserve the expected O_DIRECT
2717 endbyte
= pos
+ written_buffered
- written
- 1;
2718 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
, endbyte
);
2720 written
= written_buffered
;
2721 invalidate_mapping_pages(mapping
,
2722 pos
>> PAGE_CACHE_SHIFT
,
2723 endbyte
>> PAGE_CACHE_SHIFT
);
2726 * We don't know how much we wrote, so just return
2727 * the number of bytes which were direct-written
2731 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2732 pos
, ppos
, count
, written
);
2735 current
->backing_dev_info
= NULL
;
2736 return written
? written
: err
;
2738 EXPORT_SYMBOL(__generic_file_aio_write
);
2741 * generic_file_aio_write - write data to a file
2742 * @iocb: IO state structure
2743 * @iov: vector with data to write
2744 * @nr_segs: number of segments in the vector
2745 * @pos: position in file where to write
2747 * This is a wrapper around __generic_file_aio_write() to be used by most
2748 * filesystems. It takes care of syncing the file in case of O_SYNC file
2749 * and acquires i_mutex as needed.
2751 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2752 unsigned long nr_segs
, loff_t pos
)
2754 struct file
*file
= iocb
->ki_filp
;
2755 struct inode
*inode
= file
->f_mapping
->host
;
2758 BUG_ON(iocb
->ki_pos
!= pos
);
2760 mutex_lock(&inode
->i_mutex
);
2761 ret
= __generic_file_aio_write(iocb
, iov
, nr_segs
, &iocb
->ki_pos
);
2762 mutex_unlock(&inode
->i_mutex
);
2767 err
= generic_write_sync(file
, iocb
->ki_pos
- ret
, ret
);
2773 EXPORT_SYMBOL(generic_file_aio_write
);
2776 * try_to_release_page() - release old fs-specific metadata on a page
2778 * @page: the page which the kernel is trying to free
2779 * @gfp_mask: memory allocation flags (and I/O mode)
2781 * The address_space is to try to release any data against the page
2782 * (presumably at page->private). If the release was successful, return `1'.
2783 * Otherwise return zero.
2785 * This may also be called if PG_fscache is set on a page, indicating that the
2786 * page is known to the local caching routines.
2788 * The @gfp_mask argument specifies whether I/O may be performed to release
2789 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
2792 int try_to_release_page(struct page
*page
, gfp_t gfp_mask
)
2794 struct address_space
* const mapping
= page
->mapping
;
2796 BUG_ON(!PageLocked(page
));
2797 if (PageWriteback(page
))
2800 if (mapping
&& mapping
->a_ops
->releasepage
)
2801 return mapping
->a_ops
->releasepage(page
, gfp_mask
);
2802 return try_to_free_buffers(page
);
2805 EXPORT_SYMBOL(try_to_release_page
);