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/capability.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/gfp.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/backing-dev.h>
28 #include <linux/pagevec.h>
29 #include <linux/blkdev.h>
30 #include <linux/security.h>
31 #include <linux/cpuset.h>
32 #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
33 #include <linux/hugetlb.h>
34 #include <linux/memcontrol.h>
35 #include <linux/cleancache.h>
36 #include <linux/rmap.h>
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/filemap.h>
43 * FIXME: remove all knowledge of the buffer layer from the core VM
45 #include <linux/buffer_head.h> /* for try_to_free_buffers */
50 * Shared mappings implemented 30.11.1994. It's not fully working yet,
53 * Shared mappings now work. 15.8.1995 Bruno.
55 * finished 'unifying' the page and buffer cache and SMP-threaded the
56 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
58 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
64 * ->i_mmap_rwsem (truncate_pagecache)
65 * ->private_lock (__free_pte->__set_page_dirty_buffers)
66 * ->swap_lock (exclusive_swap_page, others)
67 * ->mapping->tree_lock
70 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
74 * ->page_table_lock or pte_lock (various, mainly in memory.c)
75 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
78 * ->lock_page (access_process_vm)
80 * ->i_mutex (generic_perform_write)
81 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
84 * sb_lock (fs/fs-writeback.c)
85 * ->mapping->tree_lock (__sync_single_inode)
88 * ->anon_vma.lock (vma_adjust)
91 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
93 * ->page_table_lock or pte_lock
94 * ->swap_lock (try_to_unmap_one)
95 * ->private_lock (try_to_unmap_one)
96 * ->tree_lock (try_to_unmap_one)
97 * ->zone.lru_lock (follow_page->mark_page_accessed)
98 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
99 * ->private_lock (page_remove_rmap->set_page_dirty)
100 * ->tree_lock (page_remove_rmap->set_page_dirty)
101 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
102 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
103 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
104 * ->inode->i_lock (zap_pte_range->set_page_dirty)
105 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 * ->tasklist_lock (memory_failure, collect_procs_ao)
111 static void page_cache_tree_delete(struct address_space
*mapping
,
112 struct page
*page
, void *shadow
)
114 struct radix_tree_node
*node
;
120 VM_BUG_ON(!PageLocked(page
));
122 __radix_tree_lookup(&mapping
->page_tree
, page
->index
, &node
, &slot
);
125 mapping
->nrshadows
++;
127 * Make sure the nrshadows update is committed before
128 * the nrpages update so that final truncate racing
129 * with reclaim does not see both counters 0 at the
130 * same time and miss a shadow entry.
137 /* Clear direct pointer tags in root node */
138 mapping
->page_tree
.gfp_mask
&= __GFP_BITS_MASK
;
139 radix_tree_replace_slot(slot
, shadow
);
143 /* Clear tree tags for the removed page */
145 offset
= index
& RADIX_TREE_MAP_MASK
;
146 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
147 if (test_bit(offset
, node
->tags
[tag
]))
148 radix_tree_tag_clear(&mapping
->page_tree
, index
, tag
);
151 /* Delete page, swap shadow entry */
152 radix_tree_replace_slot(slot
, shadow
);
153 workingset_node_pages_dec(node
);
155 workingset_node_shadows_inc(node
);
157 if (__radix_tree_delete_node(&mapping
->page_tree
, node
))
161 * Track node that only contains shadow entries.
163 * Avoid acquiring the list_lru lock if already tracked. The
164 * list_empty() test is safe as node->private_list is
165 * protected by mapping->tree_lock.
167 if (!workingset_node_pages(node
) &&
168 list_empty(&node
->private_list
)) {
169 node
->private_data
= mapping
;
170 list_lru_add(&workingset_shadow_nodes
, &node
->private_list
);
175 * Delete a page from the page cache and free it. Caller has to make
176 * sure the page is locked and that nobody else uses it - or that usage
177 * is safe. The caller must hold the mapping's tree_lock.
179 void __delete_from_page_cache(struct page
*page
, void *shadow
)
181 struct address_space
*mapping
= page
->mapping
;
183 trace_mm_filemap_delete_from_page_cache(page
);
185 * if we're uptodate, flush out into the cleancache, otherwise
186 * invalidate any existing cleancache entries. We can't leave
187 * stale data around in the cleancache once our page is gone
189 if (PageUptodate(page
) && PageMappedToDisk(page
))
190 cleancache_put_page(page
);
192 cleancache_invalidate_page(mapping
, page
);
194 page_cache_tree_delete(mapping
, page
, shadow
);
196 page
->mapping
= NULL
;
197 /* Leave page->index set: truncation lookup relies upon it */
199 __dec_zone_page_state(page
, NR_FILE_PAGES
);
200 if (PageSwapBacked(page
))
201 __dec_zone_page_state(page
, NR_SHMEM
);
202 BUG_ON(page_mapped(page
));
205 * At this point page must be either written or cleaned by truncate.
206 * Dirty page here signals a bug and loss of unwritten data.
208 * This fixes dirty accounting after removing the page entirely but
209 * leaves PageDirty set: it has no effect for truncated page and
210 * anyway will be cleared before returning page into buddy allocator.
212 if (WARN_ON_ONCE(PageDirty(page
)))
213 account_page_cleaned(page
, mapping
);
217 * delete_from_page_cache - delete page from page cache
218 * @page: the page which the kernel is trying to remove from page cache
220 * This must be called only on pages that have been verified to be in the page
221 * cache and locked. It will never put the page into the free list, the caller
222 * has a reference on the page.
224 void delete_from_page_cache(struct page
*page
)
226 struct address_space
*mapping
= page
->mapping
;
227 void (*freepage
)(struct page
*);
229 BUG_ON(!PageLocked(page
));
231 freepage
= mapping
->a_ops
->freepage
;
232 spin_lock_irq(&mapping
->tree_lock
);
233 __delete_from_page_cache(page
, NULL
);
234 spin_unlock_irq(&mapping
->tree_lock
);
238 page_cache_release(page
);
240 EXPORT_SYMBOL(delete_from_page_cache
);
242 static int filemap_check_errors(struct address_space
*mapping
)
245 /* Check for outstanding write errors */
246 if (test_bit(AS_ENOSPC
, &mapping
->flags
) &&
247 test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
249 if (test_bit(AS_EIO
, &mapping
->flags
) &&
250 test_and_clear_bit(AS_EIO
, &mapping
->flags
))
256 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
257 * @mapping: address space structure to write
258 * @start: offset in bytes where the range starts
259 * @end: offset in bytes where the range ends (inclusive)
260 * @sync_mode: enable synchronous operation
262 * Start writeback against all of a mapping's dirty pages that lie
263 * within the byte offsets <start, end> inclusive.
265 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
266 * opposed to a regular memory cleansing writeback. The difference between
267 * these two operations is that if a dirty page/buffer is encountered, it must
268 * be waited upon, and not just skipped over.
270 int __filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
271 loff_t end
, int sync_mode
)
274 struct writeback_control wbc
= {
275 .sync_mode
= sync_mode
,
276 .nr_to_write
= LONG_MAX
,
277 .range_start
= start
,
281 if (!mapping_cap_writeback_dirty(mapping
))
284 ret
= do_writepages(mapping
, &wbc
);
288 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
291 return __filemap_fdatawrite_range(mapping
, 0, LLONG_MAX
, sync_mode
);
294 int filemap_fdatawrite(struct address_space
*mapping
)
296 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
298 EXPORT_SYMBOL(filemap_fdatawrite
);
300 int filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
303 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
305 EXPORT_SYMBOL(filemap_fdatawrite_range
);
308 * filemap_flush - mostly a non-blocking flush
309 * @mapping: target address_space
311 * This is a mostly non-blocking flush. Not suitable for data-integrity
312 * purposes - I/O may not be started against all dirty pages.
314 int filemap_flush(struct address_space
*mapping
)
316 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
318 EXPORT_SYMBOL(filemap_flush
);
321 * filemap_fdatawait_range - wait for writeback to complete
322 * @mapping: address space structure to wait for
323 * @start_byte: offset in bytes where the range starts
324 * @end_byte: offset in bytes where the range ends (inclusive)
326 * Walk the list of under-writeback pages of the given address space
327 * in the given range and wait for all of them.
329 int filemap_fdatawait_range(struct address_space
*mapping
, loff_t start_byte
,
332 pgoff_t index
= start_byte
>> PAGE_CACHE_SHIFT
;
333 pgoff_t end
= end_byte
>> PAGE_CACHE_SHIFT
;
338 if (end_byte
< start_byte
)
341 pagevec_init(&pvec
, 0);
342 while ((index
<= end
) &&
343 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
344 PAGECACHE_TAG_WRITEBACK
,
345 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
348 for (i
= 0; i
< nr_pages
; i
++) {
349 struct page
*page
= pvec
.pages
[i
];
351 /* until radix tree lookup accepts end_index */
352 if (page
->index
> end
)
355 wait_on_page_writeback(page
);
356 if (TestClearPageError(page
))
359 pagevec_release(&pvec
);
363 ret2
= filemap_check_errors(mapping
);
369 EXPORT_SYMBOL(filemap_fdatawait_range
);
372 * filemap_fdatawait - wait for all under-writeback pages to complete
373 * @mapping: address space structure to wait for
375 * Walk the list of under-writeback pages of the given address space
376 * and wait for all of them.
378 int filemap_fdatawait(struct address_space
*mapping
)
380 loff_t i_size
= i_size_read(mapping
->host
);
385 return filemap_fdatawait_range(mapping
, 0, i_size
- 1);
387 EXPORT_SYMBOL(filemap_fdatawait
);
389 int filemap_write_and_wait(struct address_space
*mapping
)
393 if (mapping
->nrpages
) {
394 err
= filemap_fdatawrite(mapping
);
396 * Even if the above returned error, the pages may be
397 * written partially (e.g. -ENOSPC), so we wait for it.
398 * But the -EIO is special case, it may indicate the worst
399 * thing (e.g. bug) happened, so we avoid waiting for it.
402 int err2
= filemap_fdatawait(mapping
);
407 err
= filemap_check_errors(mapping
);
411 EXPORT_SYMBOL(filemap_write_and_wait
);
414 * filemap_write_and_wait_range - write out & wait on a file range
415 * @mapping: the address_space for the pages
416 * @lstart: offset in bytes where the range starts
417 * @lend: offset in bytes where the range ends (inclusive)
419 * Write out and wait upon file offsets lstart->lend, inclusive.
421 * Note that `lend' is inclusive (describes the last byte to be written) so
422 * that this function can be used to write to the very end-of-file (end = -1).
424 int filemap_write_and_wait_range(struct address_space
*mapping
,
425 loff_t lstart
, loff_t lend
)
429 if (mapping
->nrpages
) {
430 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
432 /* See comment of filemap_write_and_wait() */
434 int err2
= filemap_fdatawait_range(mapping
,
440 err
= filemap_check_errors(mapping
);
444 EXPORT_SYMBOL(filemap_write_and_wait_range
);
447 * replace_page_cache_page - replace a pagecache page with a new one
448 * @old: page to be replaced
449 * @new: page to replace with
450 * @gfp_mask: allocation mode
452 * This function replaces a page in the pagecache with a new one. On
453 * success it acquires the pagecache reference for the new page and
454 * drops it for the old page. Both the old and new pages must be
455 * locked. This function does not add the new page to the LRU, the
456 * caller must do that.
458 * The remove + add is atomic. The only way this function can fail is
459 * memory allocation failure.
461 int replace_page_cache_page(struct page
*old
, struct page
*new, gfp_t gfp_mask
)
465 VM_BUG_ON_PAGE(!PageLocked(old
), old
);
466 VM_BUG_ON_PAGE(!PageLocked(new), new);
467 VM_BUG_ON_PAGE(new->mapping
, new);
469 error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
471 struct address_space
*mapping
= old
->mapping
;
472 void (*freepage
)(struct page
*);
474 pgoff_t offset
= old
->index
;
475 freepage
= mapping
->a_ops
->freepage
;
478 new->mapping
= mapping
;
481 spin_lock_irq(&mapping
->tree_lock
);
482 __delete_from_page_cache(old
, NULL
);
483 error
= radix_tree_insert(&mapping
->page_tree
, offset
, new);
486 __inc_zone_page_state(new, NR_FILE_PAGES
);
487 if (PageSwapBacked(new))
488 __inc_zone_page_state(new, NR_SHMEM
);
489 spin_unlock_irq(&mapping
->tree_lock
);
490 mem_cgroup_migrate(old
, new, true);
491 radix_tree_preload_end();
494 page_cache_release(old
);
499 EXPORT_SYMBOL_GPL(replace_page_cache_page
);
501 static int page_cache_tree_insert(struct address_space
*mapping
,
502 struct page
*page
, void **shadowp
)
504 struct radix_tree_node
*node
;
508 error
= __radix_tree_create(&mapping
->page_tree
, page
->index
,
515 p
= radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
516 if (!radix_tree_exceptional_entry(p
))
520 mapping
->nrshadows
--;
522 workingset_node_shadows_dec(node
);
524 radix_tree_replace_slot(slot
, page
);
527 workingset_node_pages_inc(node
);
529 * Don't track node that contains actual pages.
531 * Avoid acquiring the list_lru lock if already
532 * untracked. The list_empty() test is safe as
533 * node->private_list is protected by
534 * mapping->tree_lock.
536 if (!list_empty(&node
->private_list
))
537 list_lru_del(&workingset_shadow_nodes
,
538 &node
->private_list
);
543 static int __add_to_page_cache_locked(struct page
*page
,
544 struct address_space
*mapping
,
545 pgoff_t offset
, gfp_t gfp_mask
,
548 int huge
= PageHuge(page
);
549 struct mem_cgroup
*memcg
;
552 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
553 VM_BUG_ON_PAGE(PageSwapBacked(page
), page
);
556 error
= mem_cgroup_try_charge(page
, current
->mm
,
562 error
= radix_tree_maybe_preload(gfp_mask
& ~__GFP_HIGHMEM
);
565 mem_cgroup_cancel_charge(page
, memcg
);
569 page_cache_get(page
);
570 page
->mapping
= mapping
;
571 page
->index
= offset
;
573 spin_lock_irq(&mapping
->tree_lock
);
574 error
= page_cache_tree_insert(mapping
, page
, shadowp
);
575 radix_tree_preload_end();
578 __inc_zone_page_state(page
, NR_FILE_PAGES
);
579 spin_unlock_irq(&mapping
->tree_lock
);
581 mem_cgroup_commit_charge(page
, memcg
, false);
582 trace_mm_filemap_add_to_page_cache(page
);
585 page
->mapping
= NULL
;
586 /* Leave page->index set: truncation relies upon it */
587 spin_unlock_irq(&mapping
->tree_lock
);
589 mem_cgroup_cancel_charge(page
, memcg
);
590 page_cache_release(page
);
595 * add_to_page_cache_locked - add a locked page to the pagecache
597 * @mapping: the page's address_space
598 * @offset: page index
599 * @gfp_mask: page allocation mode
601 * This function is used to add a page to the pagecache. It must be locked.
602 * This function does not add the page to the LRU. The caller must do that.
604 int add_to_page_cache_locked(struct page
*page
, struct address_space
*mapping
,
605 pgoff_t offset
, gfp_t gfp_mask
)
607 return __add_to_page_cache_locked(page
, mapping
, offset
,
610 EXPORT_SYMBOL(add_to_page_cache_locked
);
612 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
613 pgoff_t offset
, gfp_t gfp_mask
)
618 __set_page_locked(page
);
619 ret
= __add_to_page_cache_locked(page
, mapping
, offset
,
622 __clear_page_locked(page
);
625 * The page might have been evicted from cache only
626 * recently, in which case it should be activated like
627 * any other repeatedly accessed page.
629 if (shadow
&& workingset_refault(shadow
)) {
631 workingset_activation(page
);
633 ClearPageActive(page
);
638 EXPORT_SYMBOL_GPL(add_to_page_cache_lru
);
641 struct page
*__page_cache_alloc(gfp_t gfp
)
646 if (cpuset_do_page_mem_spread()) {
647 unsigned int cpuset_mems_cookie
;
649 cpuset_mems_cookie
= read_mems_allowed_begin();
650 n
= cpuset_mem_spread_node();
651 page
= alloc_pages_exact_node(n
, gfp
, 0);
652 } while (!page
&& read_mems_allowed_retry(cpuset_mems_cookie
));
656 return alloc_pages(gfp
, 0);
658 EXPORT_SYMBOL(__page_cache_alloc
);
662 * In order to wait for pages to become available there must be
663 * waitqueues associated with pages. By using a hash table of
664 * waitqueues where the bucket discipline is to maintain all
665 * waiters on the same queue and wake all when any of the pages
666 * become available, and for the woken contexts to check to be
667 * sure the appropriate page became available, this saves space
668 * at a cost of "thundering herd" phenomena during rare hash
671 wait_queue_head_t
*page_waitqueue(struct page
*page
)
673 const struct zone
*zone
= page_zone(page
);
675 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
677 EXPORT_SYMBOL(page_waitqueue
);
679 void wait_on_page_bit(struct page
*page
, int bit_nr
)
681 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
683 if (test_bit(bit_nr
, &page
->flags
))
684 __wait_on_bit(page_waitqueue(page
), &wait
, bit_wait_io
,
685 TASK_UNINTERRUPTIBLE
);
687 EXPORT_SYMBOL(wait_on_page_bit
);
689 int wait_on_page_bit_killable(struct page
*page
, int bit_nr
)
691 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
693 if (!test_bit(bit_nr
, &page
->flags
))
696 return __wait_on_bit(page_waitqueue(page
), &wait
,
697 bit_wait_io
, TASK_KILLABLE
);
700 int wait_on_page_bit_killable_timeout(struct page
*page
,
701 int bit_nr
, unsigned long timeout
)
703 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
705 wait
.key
.timeout
= jiffies
+ timeout
;
706 if (!test_bit(bit_nr
, &page
->flags
))
708 return __wait_on_bit(page_waitqueue(page
), &wait
,
709 bit_wait_io_timeout
, TASK_KILLABLE
);
711 EXPORT_SYMBOL_GPL(wait_on_page_bit_killable_timeout
);
714 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
715 * @page: Page defining the wait queue of interest
716 * @waiter: Waiter to add to the queue
718 * Add an arbitrary @waiter to the wait queue for the nominated @page.
720 void add_page_wait_queue(struct page
*page
, wait_queue_t
*waiter
)
722 wait_queue_head_t
*q
= page_waitqueue(page
);
725 spin_lock_irqsave(&q
->lock
, flags
);
726 __add_wait_queue(q
, waiter
);
727 spin_unlock_irqrestore(&q
->lock
, flags
);
729 EXPORT_SYMBOL_GPL(add_page_wait_queue
);
732 * unlock_page - unlock a locked page
735 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
736 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
737 * mechanism between PageLocked pages and PageWriteback pages is shared.
738 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
740 * The mb is necessary to enforce ordering between the clear_bit and the read
741 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
743 void unlock_page(struct page
*page
)
745 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
746 clear_bit_unlock(PG_locked
, &page
->flags
);
747 smp_mb__after_atomic();
748 wake_up_page(page
, PG_locked
);
750 EXPORT_SYMBOL(unlock_page
);
753 * end_page_writeback - end writeback against a page
756 void end_page_writeback(struct page
*page
)
759 * TestClearPageReclaim could be used here but it is an atomic
760 * operation and overkill in this particular case. Failing to
761 * shuffle a page marked for immediate reclaim is too mild to
762 * justify taking an atomic operation penalty at the end of
763 * ever page writeback.
765 if (PageReclaim(page
)) {
766 ClearPageReclaim(page
);
767 rotate_reclaimable_page(page
);
770 if (!test_clear_page_writeback(page
))
773 smp_mb__after_atomic();
774 wake_up_page(page
, PG_writeback
);
776 EXPORT_SYMBOL(end_page_writeback
);
779 * After completing I/O on a page, call this routine to update the page
780 * flags appropriately
782 void page_endio(struct page
*page
, int rw
, int err
)
786 SetPageUptodate(page
);
788 ClearPageUptodate(page
);
792 } else { /* rw == WRITE */
796 mapping_set_error(page
->mapping
, err
);
798 end_page_writeback(page
);
801 EXPORT_SYMBOL_GPL(page_endio
);
804 * __lock_page - get a lock on the page, assuming we need to sleep to get it
805 * @page: the page to lock
807 void __lock_page(struct page
*page
)
809 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
811 __wait_on_bit_lock(page_waitqueue(page
), &wait
, bit_wait_io
,
812 TASK_UNINTERRUPTIBLE
);
814 EXPORT_SYMBOL(__lock_page
);
816 int __lock_page_killable(struct page
*page
)
818 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
820 return __wait_on_bit_lock(page_waitqueue(page
), &wait
,
821 bit_wait_io
, TASK_KILLABLE
);
823 EXPORT_SYMBOL_GPL(__lock_page_killable
);
827 * 1 - page is locked; mmap_sem is still held.
828 * 0 - page is not locked.
829 * mmap_sem has been released (up_read()), unless flags had both
830 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
831 * which case mmap_sem is still held.
833 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
834 * with the page locked and the mmap_sem unperturbed.
836 int __lock_page_or_retry(struct page
*page
, struct mm_struct
*mm
,
839 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
841 * CAUTION! In this case, mmap_sem is not released
842 * even though return 0.
844 if (flags
& FAULT_FLAG_RETRY_NOWAIT
)
847 up_read(&mm
->mmap_sem
);
848 if (flags
& FAULT_FLAG_KILLABLE
)
849 wait_on_page_locked_killable(page
);
851 wait_on_page_locked(page
);
854 if (flags
& FAULT_FLAG_KILLABLE
) {
857 ret
= __lock_page_killable(page
);
859 up_read(&mm
->mmap_sem
);
869 * page_cache_next_hole - find the next hole (not-present entry)
872 * @max_scan: maximum range to search
874 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
875 * lowest indexed hole.
877 * Returns: the index of the hole if found, otherwise returns an index
878 * outside of the set specified (in which case 'return - index >=
879 * max_scan' will be true). In rare cases of index wrap-around, 0 will
882 * page_cache_next_hole may be called under rcu_read_lock. However,
883 * like radix_tree_gang_lookup, this will not atomically search a
884 * snapshot of the tree at a single point in time. For example, if a
885 * hole is created at index 5, then subsequently a hole is created at
886 * index 10, page_cache_next_hole covering both indexes may return 10
887 * if called under rcu_read_lock.
889 pgoff_t
page_cache_next_hole(struct address_space
*mapping
,
890 pgoff_t index
, unsigned long max_scan
)
894 for (i
= 0; i
< max_scan
; i
++) {
897 page
= radix_tree_lookup(&mapping
->page_tree
, index
);
898 if (!page
|| radix_tree_exceptional_entry(page
))
907 EXPORT_SYMBOL(page_cache_next_hole
);
910 * page_cache_prev_hole - find the prev hole (not-present entry)
913 * @max_scan: maximum range to search
915 * Search backwards in the range [max(index-max_scan+1, 0), index] for
918 * Returns: the index of the hole if found, otherwise returns an index
919 * outside of the set specified (in which case 'index - return >=
920 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
923 * page_cache_prev_hole may be called under rcu_read_lock. However,
924 * like radix_tree_gang_lookup, this will not atomically search a
925 * snapshot of the tree at a single point in time. For example, if a
926 * hole is created at index 10, then subsequently a hole is created at
927 * index 5, page_cache_prev_hole covering both indexes may return 5 if
928 * called under rcu_read_lock.
930 pgoff_t
page_cache_prev_hole(struct address_space
*mapping
,
931 pgoff_t index
, unsigned long max_scan
)
935 for (i
= 0; i
< max_scan
; i
++) {
938 page
= radix_tree_lookup(&mapping
->page_tree
, index
);
939 if (!page
|| radix_tree_exceptional_entry(page
))
942 if (index
== ULONG_MAX
)
948 EXPORT_SYMBOL(page_cache_prev_hole
);
951 * find_get_entry - find and get a page cache entry
952 * @mapping: the address_space to search
953 * @offset: the page cache index
955 * Looks up the page cache slot at @mapping & @offset. If there is a
956 * page cache page, it is returned with an increased refcount.
958 * If the slot holds a shadow entry of a previously evicted page, or a
959 * swap entry from shmem/tmpfs, it is returned.
961 * Otherwise, %NULL is returned.
963 struct page
*find_get_entry(struct address_space
*mapping
, pgoff_t offset
)
971 pagep
= radix_tree_lookup_slot(&mapping
->page_tree
, offset
);
973 page
= radix_tree_deref_slot(pagep
);
976 if (radix_tree_exception(page
)) {
977 if (radix_tree_deref_retry(page
))
980 * A shadow entry of a recently evicted page,
981 * or a swap entry from shmem/tmpfs. Return
982 * it without attempting to raise page count.
986 if (!page_cache_get_speculative(page
))
990 * Has the page moved?
991 * This is part of the lockless pagecache protocol. See
992 * include/linux/pagemap.h for details.
994 if (unlikely(page
!= *pagep
)) {
995 page_cache_release(page
);
1004 EXPORT_SYMBOL(find_get_entry
);
1007 * find_lock_entry - locate, pin and lock a page cache entry
1008 * @mapping: the address_space to search
1009 * @offset: the page cache index
1011 * Looks up the page cache slot at @mapping & @offset. If there is a
1012 * page cache page, it is returned locked and with an increased
1015 * If the slot holds a shadow entry of a previously evicted page, or a
1016 * swap entry from shmem/tmpfs, it is returned.
1018 * Otherwise, %NULL is returned.
1020 * find_lock_entry() may sleep.
1022 struct page
*find_lock_entry(struct address_space
*mapping
, pgoff_t offset
)
1027 page
= find_get_entry(mapping
, offset
);
1028 if (page
&& !radix_tree_exception(page
)) {
1030 /* Has the page been truncated? */
1031 if (unlikely(page
->mapping
!= mapping
)) {
1033 page_cache_release(page
);
1036 VM_BUG_ON_PAGE(page
->index
!= offset
, page
);
1040 EXPORT_SYMBOL(find_lock_entry
);
1043 * pagecache_get_page - find and get a page reference
1044 * @mapping: the address_space to search
1045 * @offset: the page index
1046 * @fgp_flags: PCG flags
1047 * @gfp_mask: gfp mask to use for the page cache data page allocation
1049 * Looks up the page cache slot at @mapping & @offset.
1051 * PCG flags modify how the page is returned.
1053 * FGP_ACCESSED: the page will be marked accessed
1054 * FGP_LOCK: Page is return locked
1055 * FGP_CREAT: If page is not present then a new page is allocated using
1056 * @gfp_mask and added to the page cache and the VM's LRU
1057 * list. The page is returned locked and with an increased
1058 * refcount. Otherwise, %NULL is returned.
1060 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1061 * if the GFP flags specified for FGP_CREAT are atomic.
1063 * If there is a page cache page, it is returned with an increased refcount.
1065 struct page
*pagecache_get_page(struct address_space
*mapping
, pgoff_t offset
,
1066 int fgp_flags
, gfp_t gfp_mask
)
1071 page
= find_get_entry(mapping
, offset
);
1072 if (radix_tree_exceptional_entry(page
))
1077 if (fgp_flags
& FGP_LOCK
) {
1078 if (fgp_flags
& FGP_NOWAIT
) {
1079 if (!trylock_page(page
)) {
1080 page_cache_release(page
);
1087 /* Has the page been truncated? */
1088 if (unlikely(page
->mapping
!= mapping
)) {
1090 page_cache_release(page
);
1093 VM_BUG_ON_PAGE(page
->index
!= offset
, page
);
1096 if (page
&& (fgp_flags
& FGP_ACCESSED
))
1097 mark_page_accessed(page
);
1100 if (!page
&& (fgp_flags
& FGP_CREAT
)) {
1102 if ((fgp_flags
& FGP_WRITE
) && mapping_cap_account_dirty(mapping
))
1103 gfp_mask
|= __GFP_WRITE
;
1104 if (fgp_flags
& FGP_NOFS
)
1105 gfp_mask
&= ~__GFP_FS
;
1107 page
= __page_cache_alloc(gfp_mask
);
1111 if (WARN_ON_ONCE(!(fgp_flags
& FGP_LOCK
)))
1112 fgp_flags
|= FGP_LOCK
;
1114 /* Init accessed so avoid atomic mark_page_accessed later */
1115 if (fgp_flags
& FGP_ACCESSED
)
1116 __SetPageReferenced(page
);
1118 err
= add_to_page_cache_lru(page
, mapping
, offset
,
1119 gfp_mask
& GFP_RECLAIM_MASK
);
1120 if (unlikely(err
)) {
1121 page_cache_release(page
);
1130 EXPORT_SYMBOL(pagecache_get_page
);
1133 * find_get_entries - gang pagecache lookup
1134 * @mapping: The address_space to search
1135 * @start: The starting page cache index
1136 * @nr_entries: The maximum number of entries
1137 * @entries: Where the resulting entries are placed
1138 * @indices: The cache indices corresponding to the entries in @entries
1140 * find_get_entries() will search for and return a group of up to
1141 * @nr_entries entries in the mapping. The entries are placed at
1142 * @entries. find_get_entries() takes a reference against any actual
1145 * The search returns a group of mapping-contiguous page cache entries
1146 * with ascending indexes. There may be holes in the indices due to
1147 * not-present pages.
1149 * Any shadow entries of evicted pages, or swap entries from
1150 * shmem/tmpfs, are included in the returned array.
1152 * find_get_entries() returns the number of pages and shadow entries
1155 unsigned find_get_entries(struct address_space
*mapping
,
1156 pgoff_t start
, unsigned int nr_entries
,
1157 struct page
**entries
, pgoff_t
*indices
)
1160 unsigned int ret
= 0;
1161 struct radix_tree_iter iter
;
1168 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, start
) {
1171 page
= radix_tree_deref_slot(slot
);
1172 if (unlikely(!page
))
1174 if (radix_tree_exception(page
)) {
1175 if (radix_tree_deref_retry(page
))
1178 * A shadow entry of a recently evicted page,
1179 * or a swap entry from shmem/tmpfs. Return
1180 * it without attempting to raise page count.
1184 if (!page_cache_get_speculative(page
))
1187 /* Has the page moved? */
1188 if (unlikely(page
!= *slot
)) {
1189 page_cache_release(page
);
1193 indices
[ret
] = iter
.index
;
1194 entries
[ret
] = page
;
1195 if (++ret
== nr_entries
)
1203 * find_get_pages - gang pagecache lookup
1204 * @mapping: The address_space to search
1205 * @start: The starting page index
1206 * @nr_pages: The maximum number of pages
1207 * @pages: Where the resulting pages are placed
1209 * find_get_pages() will search for and return a group of up to
1210 * @nr_pages pages in the mapping. The pages are placed at @pages.
1211 * find_get_pages() takes a reference against the returned pages.
1213 * The search returns a group of mapping-contiguous pages with ascending
1214 * indexes. There may be holes in the indices due to not-present pages.
1216 * find_get_pages() returns the number of pages which were found.
1218 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
1219 unsigned int nr_pages
, struct page
**pages
)
1221 struct radix_tree_iter iter
;
1225 if (unlikely(!nr_pages
))
1230 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, start
) {
1233 page
= radix_tree_deref_slot(slot
);
1234 if (unlikely(!page
))
1237 if (radix_tree_exception(page
)) {
1238 if (radix_tree_deref_retry(page
)) {
1240 * Transient condition which can only trigger
1241 * when entry at index 0 moves out of or back
1242 * to root: none yet gotten, safe to restart.
1244 WARN_ON(iter
.index
);
1248 * A shadow entry of a recently evicted page,
1249 * or a swap entry from shmem/tmpfs. Skip
1255 if (!page_cache_get_speculative(page
))
1258 /* Has the page moved? */
1259 if (unlikely(page
!= *slot
)) {
1260 page_cache_release(page
);
1265 if (++ret
== nr_pages
)
1274 * find_get_pages_contig - gang contiguous pagecache lookup
1275 * @mapping: The address_space to search
1276 * @index: The starting page index
1277 * @nr_pages: The maximum number of pages
1278 * @pages: Where the resulting pages are placed
1280 * find_get_pages_contig() works exactly like find_get_pages(), except
1281 * that the returned number of pages are guaranteed to be contiguous.
1283 * find_get_pages_contig() returns the number of pages which were found.
1285 unsigned find_get_pages_contig(struct address_space
*mapping
, pgoff_t index
,
1286 unsigned int nr_pages
, struct page
**pages
)
1288 struct radix_tree_iter iter
;
1290 unsigned int ret
= 0;
1292 if (unlikely(!nr_pages
))
1297 radix_tree_for_each_contig(slot
, &mapping
->page_tree
, &iter
, index
) {
1300 page
= radix_tree_deref_slot(slot
);
1301 /* The hole, there no reason to continue */
1302 if (unlikely(!page
))
1305 if (radix_tree_exception(page
)) {
1306 if (radix_tree_deref_retry(page
)) {
1308 * Transient condition which can only trigger
1309 * when entry at index 0 moves out of or back
1310 * to root: none yet gotten, safe to restart.
1315 * A shadow entry of a recently evicted page,
1316 * or a swap entry from shmem/tmpfs. Stop
1317 * looking for contiguous pages.
1322 if (!page_cache_get_speculative(page
))
1325 /* Has the page moved? */
1326 if (unlikely(page
!= *slot
)) {
1327 page_cache_release(page
);
1332 * must check mapping and index after taking the ref.
1333 * otherwise we can get both false positives and false
1334 * negatives, which is just confusing to the caller.
1336 if (page
->mapping
== NULL
|| page
->index
!= iter
.index
) {
1337 page_cache_release(page
);
1342 if (++ret
== nr_pages
)
1348 EXPORT_SYMBOL(find_get_pages_contig
);
1351 * find_get_pages_tag - find and return pages that match @tag
1352 * @mapping: the address_space to search
1353 * @index: the starting page index
1354 * @tag: the tag index
1355 * @nr_pages: the maximum number of pages
1356 * @pages: where the resulting pages are placed
1358 * Like find_get_pages, except we only return pages which are tagged with
1359 * @tag. We update @index to index the next page for the traversal.
1361 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
1362 int tag
, unsigned int nr_pages
, struct page
**pages
)
1364 struct radix_tree_iter iter
;
1368 if (unlikely(!nr_pages
))
1373 radix_tree_for_each_tagged(slot
, &mapping
->page_tree
,
1374 &iter
, *index
, tag
) {
1377 page
= radix_tree_deref_slot(slot
);
1378 if (unlikely(!page
))
1381 if (radix_tree_exception(page
)) {
1382 if (radix_tree_deref_retry(page
)) {
1384 * Transient condition which can only trigger
1385 * when entry at index 0 moves out of or back
1386 * to root: none yet gotten, safe to restart.
1391 * A shadow entry of a recently evicted page.
1393 * Those entries should never be tagged, but
1394 * this tree walk is lockless and the tags are
1395 * looked up in bulk, one radix tree node at a
1396 * time, so there is a sizable window for page
1397 * reclaim to evict a page we saw tagged.
1404 if (!page_cache_get_speculative(page
))
1407 /* Has the page moved? */
1408 if (unlikely(page
!= *slot
)) {
1409 page_cache_release(page
);
1414 if (++ret
== nr_pages
)
1421 *index
= pages
[ret
- 1]->index
+ 1;
1425 EXPORT_SYMBOL(find_get_pages_tag
);
1428 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1429 * a _large_ part of the i/o request. Imagine the worst scenario:
1431 * ---R__________________________________________B__________
1432 * ^ reading here ^ bad block(assume 4k)
1434 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1435 * => failing the whole request => read(R) => read(R+1) =>
1436 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1437 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1438 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1440 * It is going insane. Fix it by quickly scaling down the readahead size.
1442 static void shrink_readahead_size_eio(struct file
*filp
,
1443 struct file_ra_state
*ra
)
1449 * do_generic_file_read - generic file read routine
1450 * @filp: the file to read
1451 * @ppos: current file position
1452 * @iter: data destination
1453 * @written: already copied
1455 * This is a generic file read routine, and uses the
1456 * mapping->a_ops->readpage() function for the actual low-level stuff.
1458 * This is really ugly. But the goto's actually try to clarify some
1459 * of the logic when it comes to error handling etc.
1461 static ssize_t
do_generic_file_read(struct file
*filp
, loff_t
*ppos
,
1462 struct iov_iter
*iter
, ssize_t written
)
1464 struct address_space
*mapping
= filp
->f_mapping
;
1465 struct inode
*inode
= mapping
->host
;
1466 struct file_ra_state
*ra
= &filp
->f_ra
;
1470 unsigned long offset
; /* offset into pagecache page */
1471 unsigned int prev_offset
;
1474 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1475 prev_index
= ra
->prev_pos
>> PAGE_CACHE_SHIFT
;
1476 prev_offset
= ra
->prev_pos
& (PAGE_CACHE_SIZE
-1);
1477 last_index
= (*ppos
+ iter
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
1478 offset
= *ppos
& ~PAGE_CACHE_MASK
;
1484 unsigned long nr
, ret
;
1488 page
= find_get_page(mapping
, index
);
1490 page_cache_sync_readahead(mapping
,
1492 index
, last_index
- index
);
1493 page
= find_get_page(mapping
, index
);
1494 if (unlikely(page
== NULL
))
1495 goto no_cached_page
;
1497 if (PageReadahead(page
)) {
1498 page_cache_async_readahead(mapping
,
1500 index
, last_index
- index
);
1502 if (!PageUptodate(page
)) {
1503 if (inode
->i_blkbits
== PAGE_CACHE_SHIFT
||
1504 !mapping
->a_ops
->is_partially_uptodate
)
1505 goto page_not_up_to_date
;
1506 if (!trylock_page(page
))
1507 goto page_not_up_to_date
;
1508 /* Did it get truncated before we got the lock? */
1510 goto page_not_up_to_date_locked
;
1511 if (!mapping
->a_ops
->is_partially_uptodate(page
,
1512 offset
, iter
->count
))
1513 goto page_not_up_to_date_locked
;
1518 * i_size must be checked after we know the page is Uptodate.
1520 * Checking i_size after the check allows us to calculate
1521 * the correct value for "nr", which means the zero-filled
1522 * part of the page is not copied back to userspace (unless
1523 * another truncate extends the file - this is desired though).
1526 isize
= i_size_read(inode
);
1527 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1528 if (unlikely(!isize
|| index
> end_index
)) {
1529 page_cache_release(page
);
1533 /* nr is the maximum number of bytes to copy from this page */
1534 nr
= PAGE_CACHE_SIZE
;
1535 if (index
== end_index
) {
1536 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
1538 page_cache_release(page
);
1544 /* If users can be writing to this page using arbitrary
1545 * virtual addresses, take care about potential aliasing
1546 * before reading the page on the kernel side.
1548 if (mapping_writably_mapped(mapping
))
1549 flush_dcache_page(page
);
1552 * When a sequential read accesses a page several times,
1553 * only mark it as accessed the first time.
1555 if (prev_index
!= index
|| offset
!= prev_offset
)
1556 mark_page_accessed(page
);
1560 * Ok, we have the page, and it's up-to-date, so
1561 * now we can copy it to user space...
1564 ret
= copy_page_to_iter(page
, offset
, nr
, iter
);
1566 index
+= offset
>> PAGE_CACHE_SHIFT
;
1567 offset
&= ~PAGE_CACHE_MASK
;
1568 prev_offset
= offset
;
1570 page_cache_release(page
);
1572 if (!iov_iter_count(iter
))
1580 page_not_up_to_date
:
1581 /* Get exclusive access to the page ... */
1582 error
= lock_page_killable(page
);
1583 if (unlikely(error
))
1584 goto readpage_error
;
1586 page_not_up_to_date_locked
:
1587 /* Did it get truncated before we got the lock? */
1588 if (!page
->mapping
) {
1590 page_cache_release(page
);
1594 /* Did somebody else fill it already? */
1595 if (PageUptodate(page
)) {
1602 * A previous I/O error may have been due to temporary
1603 * failures, eg. multipath errors.
1604 * PG_error will be set again if readpage fails.
1606 ClearPageError(page
);
1607 /* Start the actual read. The read will unlock the page. */
1608 error
= mapping
->a_ops
->readpage(filp
, page
);
1610 if (unlikely(error
)) {
1611 if (error
== AOP_TRUNCATED_PAGE
) {
1612 page_cache_release(page
);
1616 goto readpage_error
;
1619 if (!PageUptodate(page
)) {
1620 error
= lock_page_killable(page
);
1621 if (unlikely(error
))
1622 goto readpage_error
;
1623 if (!PageUptodate(page
)) {
1624 if (page
->mapping
== NULL
) {
1626 * invalidate_mapping_pages got it
1629 page_cache_release(page
);
1633 shrink_readahead_size_eio(filp
, ra
);
1635 goto readpage_error
;
1643 /* UHHUH! A synchronous read error occurred. Report it */
1644 page_cache_release(page
);
1649 * Ok, it wasn't cached, so we need to create a new
1652 page
= page_cache_alloc_cold(mapping
);
1657 error
= add_to_page_cache_lru(page
, mapping
,
1660 page_cache_release(page
);
1661 if (error
== -EEXIST
) {
1671 ra
->prev_pos
= prev_index
;
1672 ra
->prev_pos
<<= PAGE_CACHE_SHIFT
;
1673 ra
->prev_pos
|= prev_offset
;
1675 *ppos
= ((loff_t
)index
<< PAGE_CACHE_SHIFT
) + offset
;
1676 file_accessed(filp
);
1677 return written
? written
: error
;
1681 * generic_file_read_iter - generic filesystem read routine
1682 * @iocb: kernel I/O control block
1683 * @iter: destination for the data read
1685 * This is the "read_iter()" routine for all filesystems
1686 * that can use the page cache directly.
1689 generic_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1691 struct file
*file
= iocb
->ki_filp
;
1693 loff_t
*ppos
= &iocb
->ki_pos
;
1696 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1697 struct address_space
*mapping
= file
->f_mapping
;
1698 struct inode
*inode
= mapping
->host
;
1699 size_t count
= iov_iter_count(iter
);
1703 goto out
; /* skip atime */
1704 size
= i_size_read(inode
);
1705 retval
= filemap_write_and_wait_range(mapping
, pos
,
1708 struct iov_iter data
= *iter
;
1709 retval
= mapping
->a_ops
->direct_IO(iocb
, &data
, pos
);
1713 *ppos
= pos
+ retval
;
1714 iov_iter_advance(iter
, retval
);
1718 * Btrfs can have a short DIO read if we encounter
1719 * compressed extents, so if there was an error, or if
1720 * we've already read everything we wanted to, or if
1721 * there was a short read because we hit EOF, go ahead
1722 * and return. Otherwise fallthrough to buffered io for
1723 * the rest of the read. Buffered reads will not work for
1724 * DAX files, so don't bother trying.
1726 if (retval
< 0 || !iov_iter_count(iter
) || *ppos
>= size
||
1728 file_accessed(file
);
1733 retval
= do_generic_file_read(file
, ppos
, iter
, retval
);
1737 EXPORT_SYMBOL(generic_file_read_iter
);
1741 * page_cache_read - adds requested page to the page cache if not already there
1742 * @file: file to read
1743 * @offset: page index
1745 * This adds the requested page to the page cache if it isn't already there,
1746 * and schedules an I/O to read in its contents from disk.
1748 static int page_cache_read(struct file
*file
, pgoff_t offset
)
1750 struct address_space
*mapping
= file
->f_mapping
;
1755 page
= page_cache_alloc_cold(mapping
);
1759 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1761 ret
= mapping
->a_ops
->readpage(file
, page
);
1762 else if (ret
== -EEXIST
)
1763 ret
= 0; /* losing race to add is OK */
1765 page_cache_release(page
);
1767 } while (ret
== AOP_TRUNCATED_PAGE
);
1772 #define MMAP_LOTSAMISS (100)
1775 * Synchronous readahead happens when we don't even find
1776 * a page in the page cache at all.
1778 static void do_sync_mmap_readahead(struct vm_area_struct
*vma
,
1779 struct file_ra_state
*ra
,
1783 unsigned long ra_pages
;
1784 struct address_space
*mapping
= file
->f_mapping
;
1786 /* If we don't want any read-ahead, don't bother */
1787 if (vma
->vm_flags
& VM_RAND_READ
)
1792 if (vma
->vm_flags
& VM_SEQ_READ
) {
1793 page_cache_sync_readahead(mapping
, ra
, file
, offset
,
1798 /* Avoid banging the cache line if not needed */
1799 if (ra
->mmap_miss
< MMAP_LOTSAMISS
* 10)
1803 * Do we miss much more than hit in this file? If so,
1804 * stop bothering with read-ahead. It will only hurt.
1806 if (ra
->mmap_miss
> MMAP_LOTSAMISS
)
1812 ra_pages
= max_sane_readahead(ra
->ra_pages
);
1813 ra
->start
= max_t(long, 0, offset
- ra_pages
/ 2);
1814 ra
->size
= ra_pages
;
1815 ra
->async_size
= ra_pages
/ 4;
1816 ra_submit(ra
, mapping
, file
);
1820 * Asynchronous readahead happens when we find the page and PG_readahead,
1821 * so we want to possibly extend the readahead further..
1823 static void do_async_mmap_readahead(struct vm_area_struct
*vma
,
1824 struct file_ra_state
*ra
,
1829 struct address_space
*mapping
= file
->f_mapping
;
1831 /* If we don't want any read-ahead, don't bother */
1832 if (vma
->vm_flags
& VM_RAND_READ
)
1834 if (ra
->mmap_miss
> 0)
1836 if (PageReadahead(page
))
1837 page_cache_async_readahead(mapping
, ra
, file
,
1838 page
, offset
, ra
->ra_pages
);
1842 * filemap_fault - read in file data for page fault handling
1843 * @vma: vma in which the fault was taken
1844 * @vmf: struct vm_fault containing details of the fault
1846 * filemap_fault() is invoked via the vma operations vector for a
1847 * mapped memory region to read in file data during a page fault.
1849 * The goto's are kind of ugly, but this streamlines the normal case of having
1850 * it in the page cache, and handles the special cases reasonably without
1851 * having a lot of duplicated code.
1853 * vma->vm_mm->mmap_sem must be held on entry.
1855 * If our return value has VM_FAULT_RETRY set, it's because
1856 * lock_page_or_retry() returned 0.
1857 * The mmap_sem has usually been released in this case.
1858 * See __lock_page_or_retry() for the exception.
1860 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
1861 * has not been released.
1863 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
1865 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1868 struct file
*file
= vma
->vm_file
;
1869 struct address_space
*mapping
= file
->f_mapping
;
1870 struct file_ra_state
*ra
= &file
->f_ra
;
1871 struct inode
*inode
= mapping
->host
;
1872 pgoff_t offset
= vmf
->pgoff
;
1877 size
= round_up(i_size_read(inode
), PAGE_CACHE_SIZE
);
1878 if (offset
>= size
>> PAGE_CACHE_SHIFT
)
1879 return VM_FAULT_SIGBUS
;
1882 * Do we have something in the page cache already?
1884 page
= find_get_page(mapping
, offset
);
1885 if (likely(page
) && !(vmf
->flags
& FAULT_FLAG_TRIED
)) {
1887 * We found the page, so try async readahead before
1888 * waiting for the lock.
1890 do_async_mmap_readahead(vma
, ra
, file
, page
, offset
);
1892 /* No page in the page cache at all */
1893 do_sync_mmap_readahead(vma
, ra
, file
, offset
);
1894 count_vm_event(PGMAJFAULT
);
1895 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1896 ret
= VM_FAULT_MAJOR
;
1898 page
= find_get_page(mapping
, offset
);
1900 goto no_cached_page
;
1903 if (!lock_page_or_retry(page
, vma
->vm_mm
, vmf
->flags
)) {
1904 page_cache_release(page
);
1905 return ret
| VM_FAULT_RETRY
;
1908 /* Did it get truncated? */
1909 if (unlikely(page
->mapping
!= mapping
)) {
1914 VM_BUG_ON_PAGE(page
->index
!= offset
, page
);
1917 * We have a locked page in the page cache, now we need to check
1918 * that it's up-to-date. If not, it is going to be due to an error.
1920 if (unlikely(!PageUptodate(page
)))
1921 goto page_not_uptodate
;
1924 * Found the page and have a reference on it.
1925 * We must recheck i_size under page lock.
1927 size
= round_up(i_size_read(inode
), PAGE_CACHE_SIZE
);
1928 if (unlikely(offset
>= size
>> PAGE_CACHE_SHIFT
)) {
1930 page_cache_release(page
);
1931 return VM_FAULT_SIGBUS
;
1935 return ret
| VM_FAULT_LOCKED
;
1939 * We're only likely to ever get here if MADV_RANDOM is in
1942 error
= page_cache_read(file
, offset
);
1945 * The page we want has now been added to the page cache.
1946 * In the unlikely event that someone removed it in the
1947 * meantime, we'll just come back here and read it again.
1953 * An error return from page_cache_read can result if the
1954 * system is low on memory, or a problem occurs while trying
1957 if (error
== -ENOMEM
)
1958 return VM_FAULT_OOM
;
1959 return VM_FAULT_SIGBUS
;
1963 * Umm, take care of errors if the page isn't up-to-date.
1964 * Try to re-read it _once_. We do this synchronously,
1965 * because there really aren't any performance issues here
1966 * and we need to check for errors.
1968 ClearPageError(page
);
1969 error
= mapping
->a_ops
->readpage(file
, page
);
1971 wait_on_page_locked(page
);
1972 if (!PageUptodate(page
))
1975 page_cache_release(page
);
1977 if (!error
|| error
== AOP_TRUNCATED_PAGE
)
1980 /* Things didn't work out. Return zero to tell the mm layer so. */
1981 shrink_readahead_size_eio(file
, ra
);
1982 return VM_FAULT_SIGBUS
;
1984 EXPORT_SYMBOL(filemap_fault
);
1986 void filemap_map_pages(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1988 struct radix_tree_iter iter
;
1990 struct file
*file
= vma
->vm_file
;
1991 struct address_space
*mapping
= file
->f_mapping
;
1994 unsigned long address
= (unsigned long) vmf
->virtual_address
;
1999 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, vmf
->pgoff
) {
2000 if (iter
.index
> vmf
->max_pgoff
)
2003 page
= radix_tree_deref_slot(slot
);
2004 if (unlikely(!page
))
2006 if (radix_tree_exception(page
)) {
2007 if (radix_tree_deref_retry(page
))
2013 if (!page_cache_get_speculative(page
))
2016 /* Has the page moved? */
2017 if (unlikely(page
!= *slot
)) {
2018 page_cache_release(page
);
2022 if (!PageUptodate(page
) ||
2023 PageReadahead(page
) ||
2026 if (!trylock_page(page
))
2029 if (page
->mapping
!= mapping
|| !PageUptodate(page
))
2032 size
= round_up(i_size_read(mapping
->host
), PAGE_CACHE_SIZE
);
2033 if (page
->index
>= size
>> PAGE_CACHE_SHIFT
)
2036 pte
= vmf
->pte
+ page
->index
- vmf
->pgoff
;
2037 if (!pte_none(*pte
))
2040 if (file
->f_ra
.mmap_miss
> 0)
2041 file
->f_ra
.mmap_miss
--;
2042 addr
= address
+ (page
->index
- vmf
->pgoff
) * PAGE_SIZE
;
2043 do_set_pte(vma
, addr
, page
, pte
, false, false);
2049 page_cache_release(page
);
2051 if (iter
.index
== vmf
->max_pgoff
)
2056 EXPORT_SYMBOL(filemap_map_pages
);
2058 int filemap_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2060 struct page
*page
= vmf
->page
;
2061 struct inode
*inode
= file_inode(vma
->vm_file
);
2062 int ret
= VM_FAULT_LOCKED
;
2064 sb_start_pagefault(inode
->i_sb
);
2065 file_update_time(vma
->vm_file
);
2067 if (page
->mapping
!= inode
->i_mapping
) {
2069 ret
= VM_FAULT_NOPAGE
;
2073 * We mark the page dirty already here so that when freeze is in
2074 * progress, we are guaranteed that writeback during freezing will
2075 * see the dirty page and writeprotect it again.
2077 set_page_dirty(page
);
2078 wait_for_stable_page(page
);
2080 sb_end_pagefault(inode
->i_sb
);
2083 EXPORT_SYMBOL(filemap_page_mkwrite
);
2085 const struct vm_operations_struct generic_file_vm_ops
= {
2086 .fault
= filemap_fault
,
2087 .map_pages
= filemap_map_pages
,
2088 .page_mkwrite
= filemap_page_mkwrite
,
2091 /* This is used for a general mmap of a disk file */
2093 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
2095 struct address_space
*mapping
= file
->f_mapping
;
2097 if (!mapping
->a_ops
->readpage
)
2099 file_accessed(file
);
2100 vma
->vm_ops
= &generic_file_vm_ops
;
2105 * This is for filesystems which do not implement ->writepage.
2107 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2109 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2111 return generic_file_mmap(file
, vma
);
2114 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
2118 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
2122 #endif /* CONFIG_MMU */
2124 EXPORT_SYMBOL(generic_file_mmap
);
2125 EXPORT_SYMBOL(generic_file_readonly_mmap
);
2127 static struct page
*wait_on_page_read(struct page
*page
)
2129 if (!IS_ERR(page
)) {
2130 wait_on_page_locked(page
);
2131 if (!PageUptodate(page
)) {
2132 page_cache_release(page
);
2133 page
= ERR_PTR(-EIO
);
2139 static struct page
*__read_cache_page(struct address_space
*mapping
,
2141 int (*filler
)(void *, struct page
*),
2148 page
= find_get_page(mapping
, index
);
2150 page
= __page_cache_alloc(gfp
| __GFP_COLD
);
2152 return ERR_PTR(-ENOMEM
);
2153 err
= add_to_page_cache_lru(page
, mapping
, index
, gfp
);
2154 if (unlikely(err
)) {
2155 page_cache_release(page
);
2158 /* Presumably ENOMEM for radix tree node */
2159 return ERR_PTR(err
);
2161 err
= filler(data
, page
);
2163 page_cache_release(page
);
2164 page
= ERR_PTR(err
);
2166 page
= wait_on_page_read(page
);
2172 static struct page
*do_read_cache_page(struct address_space
*mapping
,
2174 int (*filler
)(void *, struct page
*),
2183 page
= __read_cache_page(mapping
, index
, filler
, data
, gfp
);
2186 if (PageUptodate(page
))
2190 if (!page
->mapping
) {
2192 page_cache_release(page
);
2195 if (PageUptodate(page
)) {
2199 err
= filler(data
, page
);
2201 page_cache_release(page
);
2202 return ERR_PTR(err
);
2204 page
= wait_on_page_read(page
);
2209 mark_page_accessed(page
);
2214 * read_cache_page - read into page cache, fill it if needed
2215 * @mapping: the page's address_space
2216 * @index: the page index
2217 * @filler: function to perform the read
2218 * @data: first arg to filler(data, page) function, often left as NULL
2220 * Read into the page cache. If a page already exists, and PageUptodate() is
2221 * not set, try to fill the page and wait for it to become unlocked.
2223 * If the page does not get brought uptodate, return -EIO.
2225 struct page
*read_cache_page(struct address_space
*mapping
,
2227 int (*filler
)(void *, struct page
*),
2230 return do_read_cache_page(mapping
, index
, filler
, data
, mapping_gfp_mask(mapping
));
2232 EXPORT_SYMBOL(read_cache_page
);
2235 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2236 * @mapping: the page's address_space
2237 * @index: the page index
2238 * @gfp: the page allocator flags to use if allocating
2240 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
2241 * any new page allocations done using the specified allocation flags.
2243 * If the page does not get brought uptodate, return -EIO.
2245 struct page
*read_cache_page_gfp(struct address_space
*mapping
,
2249 filler_t
*filler
= (filler_t
*)mapping
->a_ops
->readpage
;
2251 return do_read_cache_page(mapping
, index
, filler
, NULL
, gfp
);
2253 EXPORT_SYMBOL(read_cache_page_gfp
);
2256 * Performs necessary checks before doing a write
2258 * Can adjust writing position or amount of bytes to write.
2259 * Returns appropriate error code that caller should return or
2260 * zero in case that write should be allowed.
2262 inline ssize_t
generic_write_checks(struct kiocb
*iocb
, struct iov_iter
*from
)
2264 struct file
*file
= iocb
->ki_filp
;
2265 struct inode
*inode
= file
->f_mapping
->host
;
2266 unsigned long limit
= rlimit(RLIMIT_FSIZE
);
2269 if (!iov_iter_count(from
))
2272 /* FIXME: this is for backwards compatibility with 2.4 */
2273 if (iocb
->ki_flags
& IOCB_APPEND
)
2274 iocb
->ki_pos
= i_size_read(inode
);
2278 if (limit
!= RLIM_INFINITY
) {
2279 if (iocb
->ki_pos
>= limit
) {
2280 send_sig(SIGXFSZ
, current
, 0);
2283 iov_iter_truncate(from
, limit
- (unsigned long)pos
);
2289 if (unlikely(pos
+ iov_iter_count(from
) > MAX_NON_LFS
&&
2290 !(file
->f_flags
& O_LARGEFILE
))) {
2291 if (pos
>= MAX_NON_LFS
)
2293 iov_iter_truncate(from
, MAX_NON_LFS
- (unsigned long)pos
);
2297 * Are we about to exceed the fs block limit ?
2299 * If we have written data it becomes a short write. If we have
2300 * exceeded without writing data we send a signal and return EFBIG.
2301 * Linus frestrict idea will clean these up nicely..
2303 if (unlikely(pos
>= inode
->i_sb
->s_maxbytes
))
2306 iov_iter_truncate(from
, inode
->i_sb
->s_maxbytes
- pos
);
2307 return iov_iter_count(from
);
2309 EXPORT_SYMBOL(generic_write_checks
);
2311 int pagecache_write_begin(struct file
*file
, struct address_space
*mapping
,
2312 loff_t pos
, unsigned len
, unsigned flags
,
2313 struct page
**pagep
, void **fsdata
)
2315 const struct address_space_operations
*aops
= mapping
->a_ops
;
2317 return aops
->write_begin(file
, mapping
, pos
, len
, flags
,
2320 EXPORT_SYMBOL(pagecache_write_begin
);
2322 int pagecache_write_end(struct file
*file
, struct address_space
*mapping
,
2323 loff_t pos
, unsigned len
, unsigned copied
,
2324 struct page
*page
, void *fsdata
)
2326 const struct address_space_operations
*aops
= mapping
->a_ops
;
2328 return aops
->write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
2330 EXPORT_SYMBOL(pagecache_write_end
);
2333 generic_file_direct_write(struct kiocb
*iocb
, struct iov_iter
*from
, loff_t pos
)
2335 struct file
*file
= iocb
->ki_filp
;
2336 struct address_space
*mapping
= file
->f_mapping
;
2337 struct inode
*inode
= mapping
->host
;
2341 struct iov_iter data
;
2343 write_len
= iov_iter_count(from
);
2344 end
= (pos
+ write_len
- 1) >> PAGE_CACHE_SHIFT
;
2346 written
= filemap_write_and_wait_range(mapping
, pos
, pos
+ write_len
- 1);
2351 * After a write we want buffered reads to be sure to go to disk to get
2352 * the new data. We invalidate clean cached page from the region we're
2353 * about to write. We do this *before* the write so that we can return
2354 * without clobbering -EIOCBQUEUED from ->direct_IO().
2356 if (mapping
->nrpages
) {
2357 written
= invalidate_inode_pages2_range(mapping
,
2358 pos
>> PAGE_CACHE_SHIFT
, end
);
2360 * If a page can not be invalidated, return 0 to fall back
2361 * to buffered write.
2364 if (written
== -EBUSY
)
2371 written
= mapping
->a_ops
->direct_IO(iocb
, &data
, pos
);
2374 * Finally, try again to invalidate clean pages which might have been
2375 * cached by non-direct readahead, or faulted in by get_user_pages()
2376 * if the source of the write was an mmap'ed region of the file
2377 * we're writing. Either one is a pretty crazy thing to do,
2378 * so we don't support it 100%. If this invalidation
2379 * fails, tough, the write still worked...
2381 if (mapping
->nrpages
) {
2382 invalidate_inode_pages2_range(mapping
,
2383 pos
>> PAGE_CACHE_SHIFT
, end
);
2388 iov_iter_advance(from
, written
);
2389 if (pos
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
2390 i_size_write(inode
, pos
);
2391 mark_inode_dirty(inode
);
2398 EXPORT_SYMBOL(generic_file_direct_write
);
2401 * Find or create a page at the given pagecache position. Return the locked
2402 * page. This function is specifically for buffered writes.
2404 struct page
*grab_cache_page_write_begin(struct address_space
*mapping
,
2405 pgoff_t index
, unsigned flags
)
2408 int fgp_flags
= FGP_LOCK
|FGP_ACCESSED
|FGP_WRITE
|FGP_CREAT
;
2410 if (flags
& AOP_FLAG_NOFS
)
2411 fgp_flags
|= FGP_NOFS
;
2413 page
= pagecache_get_page(mapping
, index
, fgp_flags
,
2414 mapping_gfp_mask(mapping
));
2416 wait_for_stable_page(page
);
2420 EXPORT_SYMBOL(grab_cache_page_write_begin
);
2422 ssize_t
generic_perform_write(struct file
*file
,
2423 struct iov_iter
*i
, loff_t pos
)
2425 struct address_space
*mapping
= file
->f_mapping
;
2426 const struct address_space_operations
*a_ops
= mapping
->a_ops
;
2428 ssize_t written
= 0;
2429 unsigned int flags
= 0;
2432 * Copies from kernel address space cannot fail (NFSD is a big user).
2434 if (!iter_is_iovec(i
))
2435 flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
2439 unsigned long offset
; /* Offset into pagecache page */
2440 unsigned long bytes
; /* Bytes to write to page */
2441 size_t copied
; /* Bytes copied from user */
2444 offset
= (pos
& (PAGE_CACHE_SIZE
- 1));
2445 bytes
= min_t(unsigned long, PAGE_CACHE_SIZE
- offset
,
2450 * Bring in the user page that we will copy from _first_.
2451 * Otherwise there's a nasty deadlock on copying from the
2452 * same page as we're writing to, without it being marked
2455 * Not only is this an optimisation, but it is also required
2456 * to check that the address is actually valid, when atomic
2457 * usercopies are used, below.
2459 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
2464 status
= a_ops
->write_begin(file
, mapping
, pos
, bytes
, flags
,
2466 if (unlikely(status
< 0))
2469 if (mapping_writably_mapped(mapping
))
2470 flush_dcache_page(page
);
2472 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
2473 flush_dcache_page(page
);
2475 status
= a_ops
->write_end(file
, mapping
, pos
, bytes
, copied
,
2477 if (unlikely(status
< 0))
2483 iov_iter_advance(i
, copied
);
2484 if (unlikely(copied
== 0)) {
2486 * If we were unable to copy any data at all, we must
2487 * fall back to a single segment length write.
2489 * If we didn't fallback here, we could livelock
2490 * because not all segments in the iov can be copied at
2491 * once without a pagefault.
2493 bytes
= min_t(unsigned long, PAGE_CACHE_SIZE
- offset
,
2494 iov_iter_single_seg_count(i
));
2500 balance_dirty_pages_ratelimited(mapping
);
2501 if (fatal_signal_pending(current
)) {
2505 } while (iov_iter_count(i
));
2507 return written
? written
: status
;
2509 EXPORT_SYMBOL(generic_perform_write
);
2512 * __generic_file_write_iter - write data to a file
2513 * @iocb: IO state structure (file, offset, etc.)
2514 * @from: iov_iter with data to write
2516 * This function does all the work needed for actually writing data to a
2517 * file. It does all basic checks, removes SUID from the file, updates
2518 * modification times and calls proper subroutines depending on whether we
2519 * do direct IO or a standard buffered write.
2521 * It expects i_mutex to be grabbed unless we work on a block device or similar
2522 * object which does not need locking at all.
2524 * This function does *not* take care of syncing data in case of O_SYNC write.
2525 * A caller has to handle it. This is mainly due to the fact that we want to
2526 * avoid syncing under i_mutex.
2528 ssize_t
__generic_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
2530 struct file
*file
= iocb
->ki_filp
;
2531 struct address_space
* mapping
= file
->f_mapping
;
2532 struct inode
*inode
= mapping
->host
;
2533 ssize_t written
= 0;
2537 /* We can write back this queue in page reclaim */
2538 current
->backing_dev_info
= inode_to_bdi(inode
);
2539 err
= file_remove_suid(file
);
2543 err
= file_update_time(file
);
2547 if (iocb
->ki_flags
& IOCB_DIRECT
) {
2548 loff_t pos
, endbyte
;
2550 written
= generic_file_direct_write(iocb
, from
, iocb
->ki_pos
);
2552 * If the write stopped short of completing, fall back to
2553 * buffered writes. Some filesystems do this for writes to
2554 * holes, for example. For DAX files, a buffered write will
2555 * not succeed (even if it did, DAX does not handle dirty
2556 * page-cache pages correctly).
2558 if (written
< 0 || !iov_iter_count(from
) || IS_DAX(inode
))
2561 status
= generic_perform_write(file
, from
, pos
= iocb
->ki_pos
);
2563 * If generic_perform_write() returned a synchronous error
2564 * then we want to return the number of bytes which were
2565 * direct-written, or the error code if that was zero. Note
2566 * that this differs from normal direct-io semantics, which
2567 * will return -EFOO even if some bytes were written.
2569 if (unlikely(status
< 0)) {
2574 * We need to ensure that the page cache pages are written to
2575 * disk and invalidated to preserve the expected O_DIRECT
2578 endbyte
= pos
+ status
- 1;
2579 err
= filemap_write_and_wait_range(mapping
, pos
, endbyte
);
2581 iocb
->ki_pos
= endbyte
+ 1;
2583 invalidate_mapping_pages(mapping
,
2584 pos
>> PAGE_CACHE_SHIFT
,
2585 endbyte
>> PAGE_CACHE_SHIFT
);
2588 * We don't know how much we wrote, so just return
2589 * the number of bytes which were direct-written
2593 written
= generic_perform_write(file
, from
, iocb
->ki_pos
);
2594 if (likely(written
> 0))
2595 iocb
->ki_pos
+= written
;
2598 current
->backing_dev_info
= NULL
;
2599 return written
? written
: err
;
2601 EXPORT_SYMBOL(__generic_file_write_iter
);
2604 * generic_file_write_iter - write data to a file
2605 * @iocb: IO state structure
2606 * @from: iov_iter with data to write
2608 * This is a wrapper around __generic_file_write_iter() to be used by most
2609 * filesystems. It takes care of syncing the file in case of O_SYNC file
2610 * and acquires i_mutex as needed.
2612 ssize_t
generic_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
2614 struct file
*file
= iocb
->ki_filp
;
2615 struct inode
*inode
= file
->f_mapping
->host
;
2618 mutex_lock(&inode
->i_mutex
);
2619 ret
= generic_write_checks(iocb
, from
);
2621 ret
= __generic_file_write_iter(iocb
, from
);
2622 mutex_unlock(&inode
->i_mutex
);
2627 err
= generic_write_sync(file
, iocb
->ki_pos
- ret
, ret
);
2633 EXPORT_SYMBOL(generic_file_write_iter
);
2636 * try_to_release_page() - release old fs-specific metadata on a page
2638 * @page: the page which the kernel is trying to free
2639 * @gfp_mask: memory allocation flags (and I/O mode)
2641 * The address_space is to try to release any data against the page
2642 * (presumably at page->private). If the release was successful, return `1'.
2643 * Otherwise return zero.
2645 * This may also be called if PG_fscache is set on a page, indicating that the
2646 * page is known to the local caching routines.
2648 * The @gfp_mask argument specifies whether I/O may be performed to release
2649 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
2652 int try_to_release_page(struct page
*page
, gfp_t gfp_mask
)
2654 struct address_space
* const mapping
= page
->mapping
;
2656 BUG_ON(!PageLocked(page
));
2657 if (PageWriteback(page
))
2660 if (mapping
&& mapping
->a_ops
->releasepage
)
2661 return mapping
->a_ops
->releasepage(page
, gfp_mask
);
2662 return try_to_free_buffers(page
);
2665 EXPORT_SYMBOL(try_to_release_page
);