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/module.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/syscalls.h>
33 #include <linux/cpuset.h>
34 #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
35 #include <linux/memcontrol.h>
36 #include <linux/mm_inline.h> /* for page_is_file_cache() */
37 #include <linux/cleancache.h>
41 * FIXME: remove all knowledge of the buffer layer from the core VM
43 #include <linux/buffer_head.h> /* for try_to_free_buffers */
48 * Shared mappings implemented 30.11.1994. It's not fully working yet,
51 * Shared mappings now work. 15.8.1995 Bruno.
53 * finished 'unifying' the page and buffer cache and SMP-threaded the
54 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
56 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
62 * ->i_mmap_mutex (truncate_pagecache)
63 * ->private_lock (__free_pte->__set_page_dirty_buffers)
64 * ->swap_lock (exclusive_swap_page, others)
65 * ->mapping->tree_lock
68 * ->i_mmap_mutex (truncate->unmap_mapping_range)
72 * ->page_table_lock or pte_lock (various, mainly in memory.c)
73 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
76 * ->lock_page (access_process_vm)
78 * ->i_mutex (generic_file_buffered_write)
79 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
82 * ->i_alloc_sem (various)
85 * sb_lock (fs/fs-writeback.c)
86 * ->mapping->tree_lock (__sync_single_inode)
89 * ->anon_vma.lock (vma_adjust)
92 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
94 * ->page_table_lock or pte_lock
95 * ->swap_lock (try_to_unmap_one)
96 * ->private_lock (try_to_unmap_one)
97 * ->tree_lock (try_to_unmap_one)
98 * ->zone.lru_lock (follow_page->mark_page_accessed)
99 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
100 * ->private_lock (page_remove_rmap->set_page_dirty)
101 * ->tree_lock (page_remove_rmap->set_page_dirty)
102 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
103 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
104 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
105 * ->inode->i_lock (zap_pte_range->set_page_dirty)
106 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 * (code doesn't rely on that order, so you could switch it around)
109 * ->tasklist_lock (memory_failure, collect_procs_ao)
114 * Delete a page from the page cache and free it. Caller has to make
115 * sure the page is locked and that nobody else uses it - or that usage
116 * is safe. The caller must hold the mapping's tree_lock.
118 void __delete_from_page_cache(struct page
*page
)
120 struct address_space
*mapping
= page
->mapping
;
123 * if we're uptodate, flush out into the cleancache, otherwise
124 * invalidate any existing cleancache entries. We can't leave
125 * stale data around in the cleancache once our page is gone
127 if (PageUptodate(page
) && PageMappedToDisk(page
))
128 cleancache_put_page(page
);
130 cleancache_flush_page(mapping
, page
);
132 radix_tree_delete(&mapping
->page_tree
, page
->index
);
133 page
->mapping
= NULL
;
134 /* Leave page->index set: truncation lookup relies upon it */
136 __dec_zone_page_state(page
, NR_FILE_PAGES
);
137 if (PageSwapBacked(page
))
138 __dec_zone_page_state(page
, NR_SHMEM
);
139 BUG_ON(page_mapped(page
));
142 * Some filesystems seem to re-dirty the page even after
143 * the VM has canceled the dirty bit (eg ext3 journaling).
145 * Fix it up by doing a final dirty accounting check after
146 * having removed the page entirely.
148 if (PageDirty(page
) && mapping_cap_account_dirty(mapping
)) {
149 dec_zone_page_state(page
, NR_FILE_DIRTY
);
150 dec_bdi_stat(mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
155 * delete_from_page_cache - delete page from page cache
156 * @page: the page which the kernel is trying to remove from page cache
158 * This must be called only on pages that have been verified to be in the page
159 * cache and locked. It will never put the page into the free list, the caller
160 * has a reference on the page.
162 void delete_from_page_cache(struct page
*page
)
164 struct address_space
*mapping
= page
->mapping
;
165 void (*freepage
)(struct page
*);
167 BUG_ON(!PageLocked(page
));
169 freepage
= mapping
->a_ops
->freepage
;
170 spin_lock_irq(&mapping
->tree_lock
);
171 __delete_from_page_cache(page
);
172 spin_unlock_irq(&mapping
->tree_lock
);
173 mem_cgroup_uncharge_cache_page(page
);
177 page_cache_release(page
);
179 EXPORT_SYMBOL(delete_from_page_cache
);
181 static int sleep_on_page(void *word
)
187 static int sleep_on_page_killable(void *word
)
190 return fatal_signal_pending(current
) ? -EINTR
: 0;
194 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
195 * @mapping: address space structure to write
196 * @start: offset in bytes where the range starts
197 * @end: offset in bytes where the range ends (inclusive)
198 * @sync_mode: enable synchronous operation
200 * Start writeback against all of a mapping's dirty pages that lie
201 * within the byte offsets <start, end> inclusive.
203 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
204 * opposed to a regular memory cleansing writeback. The difference between
205 * these two operations is that if a dirty page/buffer is encountered, it must
206 * be waited upon, and not just skipped over.
208 int __filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
209 loff_t end
, int sync_mode
)
212 struct writeback_control wbc
= {
213 .sync_mode
= sync_mode
,
214 .nr_to_write
= LONG_MAX
,
215 .range_start
= start
,
219 if (!mapping_cap_writeback_dirty(mapping
))
222 ret
= do_writepages(mapping
, &wbc
);
226 static inline int __filemap_fdatawrite(struct address_space
*mapping
,
229 return __filemap_fdatawrite_range(mapping
, 0, LLONG_MAX
, sync_mode
);
232 int filemap_fdatawrite(struct address_space
*mapping
)
234 return __filemap_fdatawrite(mapping
, WB_SYNC_ALL
);
236 EXPORT_SYMBOL(filemap_fdatawrite
);
238 int filemap_fdatawrite_range(struct address_space
*mapping
, loff_t start
,
241 return __filemap_fdatawrite_range(mapping
, start
, end
, WB_SYNC_ALL
);
243 EXPORT_SYMBOL(filemap_fdatawrite_range
);
246 * filemap_flush - mostly a non-blocking flush
247 * @mapping: target address_space
249 * This is a mostly non-blocking flush. Not suitable for data-integrity
250 * purposes - I/O may not be started against all dirty pages.
252 int filemap_flush(struct address_space
*mapping
)
254 return __filemap_fdatawrite(mapping
, WB_SYNC_NONE
);
256 EXPORT_SYMBOL(filemap_flush
);
259 * filemap_fdatawait_range - wait for writeback to complete
260 * @mapping: address space structure to wait for
261 * @start_byte: offset in bytes where the range starts
262 * @end_byte: offset in bytes where the range ends (inclusive)
264 * Walk the list of under-writeback pages of the given address space
265 * in the given range and wait for all of them.
267 int filemap_fdatawait_range(struct address_space
*mapping
, loff_t start_byte
,
270 pgoff_t index
= start_byte
>> PAGE_CACHE_SHIFT
;
271 pgoff_t end
= end_byte
>> PAGE_CACHE_SHIFT
;
276 if (end_byte
< start_byte
)
279 pagevec_init(&pvec
, 0);
280 while ((index
<= end
) &&
281 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
282 PAGECACHE_TAG_WRITEBACK
,
283 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1)) != 0) {
286 for (i
= 0; i
< nr_pages
; i
++) {
287 struct page
*page
= pvec
.pages
[i
];
289 /* until radix tree lookup accepts end_index */
290 if (page
->index
> end
)
293 wait_on_page_writeback(page
);
294 if (TestClearPageError(page
))
297 pagevec_release(&pvec
);
301 /* Check for outstanding write errors */
302 if (test_and_clear_bit(AS_ENOSPC
, &mapping
->flags
))
304 if (test_and_clear_bit(AS_EIO
, &mapping
->flags
))
309 EXPORT_SYMBOL(filemap_fdatawait_range
);
312 * filemap_fdatawait - wait for all under-writeback pages to complete
313 * @mapping: address space structure to wait for
315 * Walk the list of under-writeback pages of the given address space
316 * and wait for all of them.
318 int filemap_fdatawait(struct address_space
*mapping
)
320 loff_t i_size
= i_size_read(mapping
->host
);
325 return filemap_fdatawait_range(mapping
, 0, i_size
- 1);
327 EXPORT_SYMBOL(filemap_fdatawait
);
329 int filemap_write_and_wait(struct address_space
*mapping
)
333 if (mapping
->nrpages
) {
334 err
= filemap_fdatawrite(mapping
);
336 * Even if the above returned error, the pages may be
337 * written partially (e.g. -ENOSPC), so we wait for it.
338 * But the -EIO is special case, it may indicate the worst
339 * thing (e.g. bug) happened, so we avoid waiting for it.
342 int err2
= filemap_fdatawait(mapping
);
349 EXPORT_SYMBOL(filemap_write_and_wait
);
352 * filemap_write_and_wait_range - write out & wait on a file range
353 * @mapping: the address_space for the pages
354 * @lstart: offset in bytes where the range starts
355 * @lend: offset in bytes where the range ends (inclusive)
357 * Write out and wait upon file offsets lstart->lend, inclusive.
359 * Note that `lend' is inclusive (describes the last byte to be written) so
360 * that this function can be used to write to the very end-of-file (end = -1).
362 int filemap_write_and_wait_range(struct address_space
*mapping
,
363 loff_t lstart
, loff_t lend
)
367 if (mapping
->nrpages
) {
368 err
= __filemap_fdatawrite_range(mapping
, lstart
, lend
,
370 /* See comment of filemap_write_and_wait() */
372 int err2
= filemap_fdatawait_range(mapping
,
380 EXPORT_SYMBOL(filemap_write_and_wait_range
);
383 * replace_page_cache_page - replace a pagecache page with a new one
384 * @old: page to be replaced
385 * @new: page to replace with
386 * @gfp_mask: allocation mode
388 * This function replaces a page in the pagecache with a new one. On
389 * success it acquires the pagecache reference for the new page and
390 * drops it for the old page. Both the old and new pages must be
391 * locked. This function does not add the new page to the LRU, the
392 * caller must do that.
394 * The remove + add is atomic. The only way this function can fail is
395 * memory allocation failure.
397 int replace_page_cache_page(struct page
*old
, struct page
*new, gfp_t gfp_mask
)
400 struct mem_cgroup
*memcg
= NULL
;
402 VM_BUG_ON(!PageLocked(old
));
403 VM_BUG_ON(!PageLocked(new));
404 VM_BUG_ON(new->mapping
);
407 * This is not page migration, but prepare_migration and
408 * end_migration does enough work for charge replacement.
410 * In the longer term we probably want a specialized function
411 * for moving the charge from old to new in a more efficient
414 error
= mem_cgroup_prepare_migration(old
, new, &memcg
, gfp_mask
);
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 radix_tree_preload_end();
442 page_cache_release(old
);
443 mem_cgroup_end_migration(memcg
, old
, new, true);
445 mem_cgroup_end_migration(memcg
, old
, new, false);
450 EXPORT_SYMBOL_GPL(replace_page_cache_page
);
453 * add_to_page_cache_locked - add a locked page to the pagecache
455 * @mapping: the page's address_space
456 * @offset: page index
457 * @gfp_mask: page allocation mode
459 * This function is used to add a page to the pagecache. It must be locked.
460 * This function does not add the page to the LRU. The caller must do that.
462 int add_to_page_cache_locked(struct page
*page
, struct address_space
*mapping
,
463 pgoff_t offset
, gfp_t gfp_mask
)
467 VM_BUG_ON(!PageLocked(page
));
469 error
= mem_cgroup_cache_charge(page
, current
->mm
,
470 gfp_mask
& GFP_RECLAIM_MASK
);
474 error
= radix_tree_preload(gfp_mask
& ~__GFP_HIGHMEM
);
476 page_cache_get(page
);
477 page
->mapping
= mapping
;
478 page
->index
= offset
;
480 spin_lock_irq(&mapping
->tree_lock
);
481 error
= radix_tree_insert(&mapping
->page_tree
, offset
, page
);
482 if (likely(!error
)) {
484 __inc_zone_page_state(page
, NR_FILE_PAGES
);
485 if (PageSwapBacked(page
))
486 __inc_zone_page_state(page
, NR_SHMEM
);
487 spin_unlock_irq(&mapping
->tree_lock
);
489 page
->mapping
= NULL
;
490 /* Leave page->index set: truncation relies upon it */
491 spin_unlock_irq(&mapping
->tree_lock
);
492 mem_cgroup_uncharge_cache_page(page
);
493 page_cache_release(page
);
495 radix_tree_preload_end();
497 mem_cgroup_uncharge_cache_page(page
);
501 EXPORT_SYMBOL(add_to_page_cache_locked
);
503 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
504 pgoff_t offset
, gfp_t gfp_mask
)
509 * Splice_read and readahead add shmem/tmpfs pages into the page cache
510 * before shmem_readpage has a chance to mark them as SwapBacked: they
511 * need to go on the anon lru below, and mem_cgroup_cache_charge
512 * (called in add_to_page_cache) needs to know where they're going too.
514 if (mapping_cap_swap_backed(mapping
))
515 SetPageSwapBacked(page
);
517 ret
= add_to_page_cache(page
, mapping
, offset
, gfp_mask
);
519 if (page_is_file_cache(page
))
520 lru_cache_add_file(page
);
522 lru_cache_add_anon(page
);
526 EXPORT_SYMBOL_GPL(add_to_page_cache_lru
);
529 struct page
*__page_cache_alloc(gfp_t gfp
)
534 if (cpuset_do_page_mem_spread()) {
536 n
= cpuset_mem_spread_node();
537 page
= alloc_pages_exact_node(n
, gfp
, 0);
541 return alloc_pages(gfp
, 0);
543 EXPORT_SYMBOL(__page_cache_alloc
);
547 * In order to wait for pages to become available there must be
548 * waitqueues associated with pages. By using a hash table of
549 * waitqueues where the bucket discipline is to maintain all
550 * waiters on the same queue and wake all when any of the pages
551 * become available, and for the woken contexts to check to be
552 * sure the appropriate page became available, this saves space
553 * at a cost of "thundering herd" phenomena during rare hash
556 static wait_queue_head_t
*page_waitqueue(struct page
*page
)
558 const struct zone
*zone
= page_zone(page
);
560 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
563 static inline void wake_up_page(struct page
*page
, int bit
)
565 __wake_up_bit(page_waitqueue(page
), &page
->flags
, bit
);
568 void wait_on_page_bit(struct page
*page
, int bit_nr
)
570 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
572 if (test_bit(bit_nr
, &page
->flags
))
573 __wait_on_bit(page_waitqueue(page
), &wait
, sleep_on_page
,
574 TASK_UNINTERRUPTIBLE
);
576 EXPORT_SYMBOL(wait_on_page_bit
);
578 int wait_on_page_bit_killable(struct page
*page
, int bit_nr
)
580 DEFINE_WAIT_BIT(wait
, &page
->flags
, bit_nr
);
582 if (!test_bit(bit_nr
, &page
->flags
))
585 return __wait_on_bit(page_waitqueue(page
), &wait
,
586 sleep_on_page_killable
, TASK_KILLABLE
);
590 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
591 * @page: Page defining the wait queue of interest
592 * @waiter: Waiter to add to the queue
594 * Add an arbitrary @waiter to the wait queue for the nominated @page.
596 void add_page_wait_queue(struct page
*page
, wait_queue_t
*waiter
)
598 wait_queue_head_t
*q
= page_waitqueue(page
);
601 spin_lock_irqsave(&q
->lock
, flags
);
602 __add_wait_queue(q
, waiter
);
603 spin_unlock_irqrestore(&q
->lock
, flags
);
605 EXPORT_SYMBOL_GPL(add_page_wait_queue
);
608 * unlock_page - unlock a locked page
611 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
612 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
613 * mechananism between PageLocked pages and PageWriteback pages is shared.
614 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
616 * The mb is necessary to enforce ordering between the clear_bit and the read
617 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
619 void unlock_page(struct page
*page
)
621 VM_BUG_ON(!PageLocked(page
));
622 clear_bit_unlock(PG_locked
, &page
->flags
);
623 smp_mb__after_clear_bit();
624 wake_up_page(page
, PG_locked
);
626 EXPORT_SYMBOL(unlock_page
);
629 * end_page_writeback - end writeback against a page
632 void end_page_writeback(struct page
*page
)
634 if (TestClearPageReclaim(page
))
635 rotate_reclaimable_page(page
);
637 if (!test_clear_page_writeback(page
))
640 smp_mb__after_clear_bit();
641 wake_up_page(page
, PG_writeback
);
643 EXPORT_SYMBOL(end_page_writeback
);
646 * __lock_page - get a lock on the page, assuming we need to sleep to get it
647 * @page: the page to lock
649 void __lock_page(struct page
*page
)
651 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
653 __wait_on_bit_lock(page_waitqueue(page
), &wait
, sleep_on_page
,
654 TASK_UNINTERRUPTIBLE
);
656 EXPORT_SYMBOL(__lock_page
);
658 int __lock_page_killable(struct page
*page
)
660 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
662 return __wait_on_bit_lock(page_waitqueue(page
), &wait
,
663 sleep_on_page_killable
, TASK_KILLABLE
);
665 EXPORT_SYMBOL_GPL(__lock_page_killable
);
667 int __lock_page_or_retry(struct page
*page
, struct mm_struct
*mm
,
670 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
672 * CAUTION! In this case, mmap_sem is not released
673 * even though return 0.
675 if (flags
& FAULT_FLAG_RETRY_NOWAIT
)
678 up_read(&mm
->mmap_sem
);
679 if (flags
& FAULT_FLAG_KILLABLE
)
680 wait_on_page_locked_killable(page
);
682 wait_on_page_locked(page
);
685 if (flags
& FAULT_FLAG_KILLABLE
) {
688 ret
= __lock_page_killable(page
);
690 up_read(&mm
->mmap_sem
);
700 * find_get_page - find and get a page reference
701 * @mapping: the address_space to search
702 * @offset: the page index
704 * Is there a pagecache struct page at the given (mapping, offset) tuple?
705 * If yes, increment its refcount and return it; if no, return NULL.
707 struct page
*find_get_page(struct address_space
*mapping
, pgoff_t offset
)
715 pagep
= radix_tree_lookup_slot(&mapping
->page_tree
, offset
);
717 page
= radix_tree_deref_slot(pagep
);
720 if (radix_tree_deref_retry(page
))
723 if (!page_cache_get_speculative(page
))
727 * Has the page moved?
728 * This is part of the lockless pagecache protocol. See
729 * include/linux/pagemap.h for details.
731 if (unlikely(page
!= *pagep
)) {
732 page_cache_release(page
);
741 EXPORT_SYMBOL(find_get_page
);
744 * find_lock_page - locate, pin and lock a pagecache page
745 * @mapping: the address_space to search
746 * @offset: the page index
748 * Locates the desired pagecache page, locks it, increments its reference
749 * count and returns its address.
751 * Returns zero if the page was not present. find_lock_page() may sleep.
753 struct page
*find_lock_page(struct address_space
*mapping
, pgoff_t offset
)
758 page
= find_get_page(mapping
, offset
);
761 /* Has the page been truncated? */
762 if (unlikely(page
->mapping
!= mapping
)) {
764 page_cache_release(page
);
767 VM_BUG_ON(page
->index
!= offset
);
771 EXPORT_SYMBOL(find_lock_page
);
774 * find_or_create_page - locate or add a pagecache page
775 * @mapping: the page's address_space
776 * @index: the page's index into the mapping
777 * @gfp_mask: page allocation mode
779 * Locates a page in the pagecache. If the page is not present, a new page
780 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
781 * LRU list. The returned page is locked and has its reference count
784 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
787 * find_or_create_page() returns the desired page's address, or zero on
790 struct page
*find_or_create_page(struct address_space
*mapping
,
791 pgoff_t index
, gfp_t gfp_mask
)
796 page
= find_lock_page(mapping
, index
);
798 page
= __page_cache_alloc(gfp_mask
);
802 * We want a regular kernel memory (not highmem or DMA etc)
803 * allocation for the radix tree nodes, but we need to honour
804 * the context-specific requirements the caller has asked for.
805 * GFP_RECLAIM_MASK collects those requirements.
807 err
= add_to_page_cache_lru(page
, mapping
, index
,
808 (gfp_mask
& GFP_RECLAIM_MASK
));
810 page_cache_release(page
);
818 EXPORT_SYMBOL(find_or_create_page
);
821 * find_get_pages - gang pagecache lookup
822 * @mapping: The address_space to search
823 * @start: The starting page index
824 * @nr_pages: The maximum number of pages
825 * @pages: Where the resulting pages are placed
827 * find_get_pages() will search for and return a group of up to
828 * @nr_pages pages in the mapping. The pages are placed at @pages.
829 * find_get_pages() takes a reference against the returned pages.
831 * The search returns a group of mapping-contiguous pages with ascending
832 * indexes. There may be holes in the indices due to not-present pages.
834 * find_get_pages() returns the number of pages which were found.
836 unsigned find_get_pages(struct address_space
*mapping
, pgoff_t start
,
837 unsigned int nr_pages
, struct page
**pages
)
841 unsigned int nr_found
;
845 nr_found
= radix_tree_gang_lookup_slot(&mapping
->page_tree
,
846 (void ***)pages
, start
, nr_pages
);
848 for (i
= 0; i
< nr_found
; i
++) {
851 page
= radix_tree_deref_slot((void **)pages
[i
]);
856 * This can only trigger when the entry at index 0 moves out
857 * of or back to the root: none yet gotten, safe to restart.
859 if (radix_tree_deref_retry(page
)) {
864 if (!page_cache_get_speculative(page
))
867 /* Has the page moved? */
868 if (unlikely(page
!= *((void **)pages
[i
]))) {
869 page_cache_release(page
);
878 * If all entries were removed before we could secure them,
879 * try again, because callers stop trying once 0 is returned.
881 if (unlikely(!ret
&& nr_found
))
888 * find_get_pages_contig - gang contiguous pagecache lookup
889 * @mapping: The address_space to search
890 * @index: The starting page index
891 * @nr_pages: The maximum number of pages
892 * @pages: Where the resulting pages are placed
894 * find_get_pages_contig() works exactly like find_get_pages(), except
895 * that the returned number of pages are guaranteed to be contiguous.
897 * find_get_pages_contig() returns the number of pages which were found.
899 unsigned find_get_pages_contig(struct address_space
*mapping
, pgoff_t index
,
900 unsigned int nr_pages
, struct page
**pages
)
904 unsigned int nr_found
;
908 nr_found
= radix_tree_gang_lookup_slot(&mapping
->page_tree
,
909 (void ***)pages
, index
, nr_pages
);
911 for (i
= 0; i
< nr_found
; i
++) {
914 page
= radix_tree_deref_slot((void **)pages
[i
]);
919 * This can only trigger when the entry at index 0 moves out
920 * of or back to the root: none yet gotten, safe to restart.
922 if (radix_tree_deref_retry(page
))
925 if (!page_cache_get_speculative(page
))
928 /* Has the page moved? */
929 if (unlikely(page
!= *((void **)pages
[i
]))) {
930 page_cache_release(page
);
935 * must check mapping and index after taking the ref.
936 * otherwise we can get both false positives and false
937 * negatives, which is just confusing to the caller.
939 if (page
->mapping
== NULL
|| page
->index
!= index
) {
940 page_cache_release(page
);
951 EXPORT_SYMBOL(find_get_pages_contig
);
954 * find_get_pages_tag - find and return pages that match @tag
955 * @mapping: the address_space to search
956 * @index: the starting page index
957 * @tag: the tag index
958 * @nr_pages: the maximum number of pages
959 * @pages: where the resulting pages are placed
961 * Like find_get_pages, except we only return pages which are tagged with
962 * @tag. We update @index to index the next page for the traversal.
964 unsigned find_get_pages_tag(struct address_space
*mapping
, pgoff_t
*index
,
965 int tag
, unsigned int nr_pages
, struct page
**pages
)
969 unsigned int nr_found
;
973 nr_found
= radix_tree_gang_lookup_tag_slot(&mapping
->page_tree
,
974 (void ***)pages
, *index
, nr_pages
, tag
);
976 for (i
= 0; i
< nr_found
; i
++) {
979 page
= radix_tree_deref_slot((void **)pages
[i
]);
984 * This can only trigger when the entry at index 0 moves out
985 * of or back to the root: none yet gotten, safe to restart.
987 if (radix_tree_deref_retry(page
))
990 if (!page_cache_get_speculative(page
))
993 /* Has the page moved? */
994 if (unlikely(page
!= *((void **)pages
[i
]))) {
995 page_cache_release(page
);
1004 * If all entries were removed before we could secure them,
1005 * try again, because callers stop trying once 0 is returned.
1007 if (unlikely(!ret
&& nr_found
))
1012 *index
= pages
[ret
- 1]->index
+ 1;
1016 EXPORT_SYMBOL(find_get_pages_tag
);
1019 * grab_cache_page_nowait - returns locked page at given index in given cache
1020 * @mapping: target address_space
1021 * @index: the page index
1023 * Same as grab_cache_page(), but do not wait if the page is unavailable.
1024 * This is intended for speculative data generators, where the data can
1025 * be regenerated if the page couldn't be grabbed. This routine should
1026 * be safe to call while holding the lock for another page.
1028 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
1029 * and deadlock against the caller's locked page.
1032 grab_cache_page_nowait(struct address_space
*mapping
, pgoff_t index
)
1034 struct page
*page
= find_get_page(mapping
, index
);
1037 if (trylock_page(page
))
1039 page_cache_release(page
);
1042 page
= __page_cache_alloc(mapping_gfp_mask(mapping
) & ~__GFP_FS
);
1043 if (page
&& add_to_page_cache_lru(page
, mapping
, index
, GFP_NOFS
)) {
1044 page_cache_release(page
);
1049 EXPORT_SYMBOL(grab_cache_page_nowait
);
1052 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1053 * a _large_ part of the i/o request. Imagine the worst scenario:
1055 * ---R__________________________________________B__________
1056 * ^ reading here ^ bad block(assume 4k)
1058 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1059 * => failing the whole request => read(R) => read(R+1) =>
1060 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1061 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1062 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1064 * It is going insane. Fix it by quickly scaling down the readahead size.
1066 static void shrink_readahead_size_eio(struct file
*filp
,
1067 struct file_ra_state
*ra
)
1073 * do_generic_file_read - generic file read routine
1074 * @filp: the file to read
1075 * @ppos: current file position
1076 * @desc: read_descriptor
1077 * @actor: read method
1079 * This is a generic file read routine, and uses the
1080 * mapping->a_ops->readpage() function for the actual low-level stuff.
1082 * This is really ugly. But the goto's actually try to clarify some
1083 * of the logic when it comes to error handling etc.
1085 static void do_generic_file_read(struct file
*filp
, loff_t
*ppos
,
1086 read_descriptor_t
*desc
, read_actor_t actor
)
1088 struct address_space
*mapping
= filp
->f_mapping
;
1089 struct inode
*inode
= mapping
->host
;
1090 struct file_ra_state
*ra
= &filp
->f_ra
;
1094 unsigned long offset
; /* offset into pagecache page */
1095 unsigned int prev_offset
;
1098 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1099 prev_index
= ra
->prev_pos
>> PAGE_CACHE_SHIFT
;
1100 prev_offset
= ra
->prev_pos
& (PAGE_CACHE_SIZE
-1);
1101 last_index
= (*ppos
+ desc
->count
+ PAGE_CACHE_SIZE
-1) >> PAGE_CACHE_SHIFT
;
1102 offset
= *ppos
& ~PAGE_CACHE_MASK
;
1108 unsigned long nr
, ret
;
1112 page
= find_get_page(mapping
, index
);
1114 page_cache_sync_readahead(mapping
,
1116 index
, last_index
- index
);
1117 page
= find_get_page(mapping
, index
);
1118 if (unlikely(page
== NULL
))
1119 goto no_cached_page
;
1121 if (PageReadahead(page
)) {
1122 page_cache_async_readahead(mapping
,
1124 index
, last_index
- index
);
1126 if (!PageUptodate(page
)) {
1127 if (inode
->i_blkbits
== PAGE_CACHE_SHIFT
||
1128 !mapping
->a_ops
->is_partially_uptodate
)
1129 goto page_not_up_to_date
;
1130 if (!trylock_page(page
))
1131 goto page_not_up_to_date
;
1132 /* Did it get truncated before we got the lock? */
1134 goto page_not_up_to_date_locked
;
1135 if (!mapping
->a_ops
->is_partially_uptodate(page
,
1137 goto page_not_up_to_date_locked
;
1142 * i_size must be checked after we know the page is Uptodate.
1144 * Checking i_size after the check allows us to calculate
1145 * the correct value for "nr", which means the zero-filled
1146 * part of the page is not copied back to userspace (unless
1147 * another truncate extends the file - this is desired though).
1150 isize
= i_size_read(inode
);
1151 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1152 if (unlikely(!isize
|| index
> end_index
)) {
1153 page_cache_release(page
);
1157 /* nr is the maximum number of bytes to copy from this page */
1158 nr
= PAGE_CACHE_SIZE
;
1159 if (index
== end_index
) {
1160 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
1162 page_cache_release(page
);
1168 /* If users can be writing to this page using arbitrary
1169 * virtual addresses, take care about potential aliasing
1170 * before reading the page on the kernel side.
1172 if (mapping_writably_mapped(mapping
))
1173 flush_dcache_page(page
);
1176 * When a sequential read accesses a page several times,
1177 * only mark it as accessed the first time.
1179 if (prev_index
!= index
|| offset
!= prev_offset
)
1180 mark_page_accessed(page
);
1184 * Ok, we have the page, and it's up-to-date, so
1185 * now we can copy it to user space...
1187 * The actor routine returns how many bytes were actually used..
1188 * NOTE! This may not be the same as how much of a user buffer
1189 * we filled up (we may be padding etc), so we can only update
1190 * "pos" here (the actor routine has to update the user buffer
1191 * pointers and the remaining count).
1193 ret
= actor(desc
, page
, offset
, nr
);
1195 index
+= offset
>> PAGE_CACHE_SHIFT
;
1196 offset
&= ~PAGE_CACHE_MASK
;
1197 prev_offset
= offset
;
1199 page_cache_release(page
);
1200 if (ret
== nr
&& desc
->count
)
1204 page_not_up_to_date
:
1205 /* Get exclusive access to the page ... */
1206 error
= lock_page_killable(page
);
1207 if (unlikely(error
))
1208 goto readpage_error
;
1210 page_not_up_to_date_locked
:
1211 /* Did it get truncated before we got the lock? */
1212 if (!page
->mapping
) {
1214 page_cache_release(page
);
1218 /* Did somebody else fill it already? */
1219 if (PageUptodate(page
)) {
1226 * A previous I/O error may have been due to temporary
1227 * failures, eg. multipath errors.
1228 * PG_error will be set again if readpage fails.
1230 ClearPageError(page
);
1231 /* Start the actual read. The read will unlock the page. */
1232 error
= mapping
->a_ops
->readpage(filp
, page
);
1234 if (unlikely(error
)) {
1235 if (error
== AOP_TRUNCATED_PAGE
) {
1236 page_cache_release(page
);
1239 goto readpage_error
;
1242 if (!PageUptodate(page
)) {
1243 error
= lock_page_killable(page
);
1244 if (unlikely(error
))
1245 goto readpage_error
;
1246 if (!PageUptodate(page
)) {
1247 if (page
->mapping
== NULL
) {
1249 * invalidate_mapping_pages got it
1252 page_cache_release(page
);
1256 shrink_readahead_size_eio(filp
, ra
);
1258 goto readpage_error
;
1266 /* UHHUH! A synchronous read error occurred. Report it */
1267 desc
->error
= error
;
1268 page_cache_release(page
);
1273 * Ok, it wasn't cached, so we need to create a new
1276 page
= page_cache_alloc_cold(mapping
);
1278 desc
->error
= -ENOMEM
;
1281 error
= add_to_page_cache_lru(page
, mapping
,
1284 page_cache_release(page
);
1285 if (error
== -EEXIST
)
1287 desc
->error
= error
;
1294 ra
->prev_pos
= prev_index
;
1295 ra
->prev_pos
<<= PAGE_CACHE_SHIFT
;
1296 ra
->prev_pos
|= prev_offset
;
1298 *ppos
= ((loff_t
)index
<< PAGE_CACHE_SHIFT
) + offset
;
1299 file_accessed(filp
);
1302 int file_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1303 unsigned long offset
, unsigned long size
)
1306 unsigned long left
, count
= desc
->count
;
1312 * Faults on the destination of a read are common, so do it before
1315 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1316 kaddr
= kmap_atomic(page
, KM_USER0
);
1317 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1318 kaddr
+ offset
, size
);
1319 kunmap_atomic(kaddr
, KM_USER0
);
1324 /* Do it the slow way */
1326 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1331 desc
->error
= -EFAULT
;
1334 desc
->count
= count
- size
;
1335 desc
->written
+= size
;
1336 desc
->arg
.buf
+= size
;
1341 * Performs necessary checks before doing a write
1342 * @iov: io vector request
1343 * @nr_segs: number of segments in the iovec
1344 * @count: number of bytes to write
1345 * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
1347 * Adjust number of segments and amount of bytes to write (nr_segs should be
1348 * properly initialized first). Returns appropriate error code that caller
1349 * should return or zero in case that write should be allowed.
1351 int generic_segment_checks(const struct iovec
*iov
,
1352 unsigned long *nr_segs
, size_t *count
, int access_flags
)
1356 for (seg
= 0; seg
< *nr_segs
; seg
++) {
1357 const struct iovec
*iv
= &iov
[seg
];
1360 * If any segment has a negative length, or the cumulative
1361 * length ever wraps negative then return -EINVAL.
1364 if (unlikely((ssize_t
)(cnt
|iv
->iov_len
) < 0))
1366 if (access_ok(access_flags
, iv
->iov_base
, iv
->iov_len
))
1371 cnt
-= iv
->iov_len
; /* This segment is no good */
1377 EXPORT_SYMBOL(generic_segment_checks
);
1380 * generic_file_aio_read - generic filesystem read routine
1381 * @iocb: kernel I/O control block
1382 * @iov: io vector request
1383 * @nr_segs: number of segments in the iovec
1384 * @pos: current file position
1386 * This is the "read()" routine for all filesystems
1387 * that can use the page cache directly.
1390 generic_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1391 unsigned long nr_segs
, loff_t pos
)
1393 struct file
*filp
= iocb
->ki_filp
;
1395 unsigned long seg
= 0;
1397 loff_t
*ppos
= &iocb
->ki_pos
;
1398 struct blk_plug plug
;
1401 retval
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_WRITE
);
1405 blk_start_plug(&plug
);
1407 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1408 if (filp
->f_flags
& O_DIRECT
) {
1410 struct address_space
*mapping
;
1411 struct inode
*inode
;
1413 mapping
= filp
->f_mapping
;
1414 inode
= mapping
->host
;
1416 goto out
; /* skip atime */
1417 size
= i_size_read(inode
);
1419 retval
= filemap_write_and_wait_range(mapping
, pos
,
1420 pos
+ iov_length(iov
, nr_segs
) - 1);
1422 retval
= mapping
->a_ops
->direct_IO(READ
, iocb
,
1426 *ppos
= pos
+ retval
;
1431 * Btrfs can have a short DIO read if we encounter
1432 * compressed extents, so if there was an error, or if
1433 * we've already read everything we wanted to, or if
1434 * there was a short read because we hit EOF, go ahead
1435 * and return. Otherwise fallthrough to buffered io for
1436 * the rest of the read.
1438 if (retval
< 0 || !count
|| *ppos
>= size
) {
1439 file_accessed(filp
);
1446 for (seg
= 0; seg
< nr_segs
; seg
++) {
1447 read_descriptor_t desc
;
1451 * If we did a short DIO read we need to skip the section of the
1452 * iov that we've already read data into.
1455 if (count
> iov
[seg
].iov_len
) {
1456 count
-= iov
[seg
].iov_len
;
1464 desc
.arg
.buf
= iov
[seg
].iov_base
+ offset
;
1465 desc
.count
= iov
[seg
].iov_len
- offset
;
1466 if (desc
.count
== 0)
1469 do_generic_file_read(filp
, ppos
, &desc
, file_read_actor
);
1470 retval
+= desc
.written
;
1472 retval
= retval
?: desc
.error
;
1479 blk_finish_plug(&plug
);
1482 EXPORT_SYMBOL(generic_file_aio_read
);
1485 do_readahead(struct address_space
*mapping
, struct file
*filp
,
1486 pgoff_t index
, unsigned long nr
)
1488 if (!mapping
|| !mapping
->a_ops
|| !mapping
->a_ops
->readpage
)
1491 force_page_cache_readahead(mapping
, filp
, index
, nr
);
1495 SYSCALL_DEFINE(readahead
)(int fd
, loff_t offset
, size_t count
)
1503 if (file
->f_mode
& FMODE_READ
) {
1504 struct address_space
*mapping
= file
->f_mapping
;
1505 pgoff_t start
= offset
>> PAGE_CACHE_SHIFT
;
1506 pgoff_t end
= (offset
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1507 unsigned long len
= end
- start
+ 1;
1508 ret
= do_readahead(mapping
, file
, start
, len
);
1514 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
1515 asmlinkage
long SyS_readahead(long fd
, loff_t offset
, long count
)
1517 return SYSC_readahead((int) fd
, offset
, (size_t) count
);
1519 SYSCALL_ALIAS(sys_readahead
, SyS_readahead
);
1524 * page_cache_read - adds requested page to the page cache if not already there
1525 * @file: file to read
1526 * @offset: page index
1528 * This adds the requested page to the page cache if it isn't already there,
1529 * and schedules an I/O to read in its contents from disk.
1531 static int page_cache_read(struct file
*file
, pgoff_t offset
)
1533 struct address_space
*mapping
= file
->f_mapping
;
1538 page
= page_cache_alloc_cold(mapping
);
1542 ret
= add_to_page_cache_lru(page
, mapping
, offset
, GFP_KERNEL
);
1544 ret
= mapping
->a_ops
->readpage(file
, page
);
1545 else if (ret
== -EEXIST
)
1546 ret
= 0; /* losing race to add is OK */
1548 page_cache_release(page
);
1550 } while (ret
== AOP_TRUNCATED_PAGE
);
1555 #define MMAP_LOTSAMISS (100)
1558 * Synchronous readahead happens when we don't even find
1559 * a page in the page cache at all.
1561 static void do_sync_mmap_readahead(struct vm_area_struct
*vma
,
1562 struct file_ra_state
*ra
,
1566 unsigned long ra_pages
;
1567 struct address_space
*mapping
= file
->f_mapping
;
1569 /* If we don't want any read-ahead, don't bother */
1570 if (VM_RandomReadHint(vma
))
1575 if (VM_SequentialReadHint(vma
)) {
1576 page_cache_sync_readahead(mapping
, ra
, file
, offset
,
1581 /* Avoid banging the cache line if not needed */
1582 if (ra
->mmap_miss
< MMAP_LOTSAMISS
* 10)
1586 * Do we miss much more than hit in this file? If so,
1587 * stop bothering with read-ahead. It will only hurt.
1589 if (ra
->mmap_miss
> MMAP_LOTSAMISS
)
1595 ra_pages
= max_sane_readahead(ra
->ra_pages
);
1596 ra
->start
= max_t(long, 0, offset
- ra_pages
/ 2);
1597 ra
->size
= ra_pages
;
1598 ra
->async_size
= ra_pages
/ 4;
1599 ra_submit(ra
, mapping
, file
);
1603 * Asynchronous readahead happens when we find the page and PG_readahead,
1604 * so we want to possibly extend the readahead further..
1606 static void do_async_mmap_readahead(struct vm_area_struct
*vma
,
1607 struct file_ra_state
*ra
,
1612 struct address_space
*mapping
= file
->f_mapping
;
1614 /* If we don't want any read-ahead, don't bother */
1615 if (VM_RandomReadHint(vma
))
1617 if (ra
->mmap_miss
> 0)
1619 if (PageReadahead(page
))
1620 page_cache_async_readahead(mapping
, ra
, file
,
1621 page
, offset
, ra
->ra_pages
);
1625 * filemap_fault - read in file data for page fault handling
1626 * @vma: vma in which the fault was taken
1627 * @vmf: struct vm_fault containing details of the fault
1629 * filemap_fault() is invoked via the vma operations vector for a
1630 * mapped memory region to read in file data during a page fault.
1632 * The goto's are kind of ugly, but this streamlines the normal case of having
1633 * it in the page cache, and handles the special cases reasonably without
1634 * having a lot of duplicated code.
1636 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1639 struct file
*file
= vma
->vm_file
;
1640 struct address_space
*mapping
= file
->f_mapping
;
1641 struct file_ra_state
*ra
= &file
->f_ra
;
1642 struct inode
*inode
= mapping
->host
;
1643 pgoff_t offset
= vmf
->pgoff
;
1648 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1650 return VM_FAULT_SIGBUS
;
1653 * Do we have something in the page cache already?
1655 page
= find_get_page(mapping
, offset
);
1658 * We found the page, so try async readahead before
1659 * waiting for the lock.
1661 do_async_mmap_readahead(vma
, ra
, file
, page
, offset
);
1663 /* No page in the page cache at all */
1664 do_sync_mmap_readahead(vma
, ra
, file
, offset
);
1665 count_vm_event(PGMAJFAULT
);
1666 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1667 ret
= VM_FAULT_MAJOR
;
1669 page
= find_get_page(mapping
, offset
);
1671 goto no_cached_page
;
1674 if (!lock_page_or_retry(page
, vma
->vm_mm
, vmf
->flags
)) {
1675 page_cache_release(page
);
1676 return ret
| VM_FAULT_RETRY
;
1679 /* Did it get truncated? */
1680 if (unlikely(page
->mapping
!= mapping
)) {
1685 VM_BUG_ON(page
->index
!= offset
);
1688 * We have a locked page in the page cache, now we need to check
1689 * that it's up-to-date. If not, it is going to be due to an error.
1691 if (unlikely(!PageUptodate(page
)))
1692 goto page_not_uptodate
;
1695 * Found the page and have a reference on it.
1696 * We must recheck i_size under page lock.
1698 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1699 if (unlikely(offset
>= size
)) {
1701 page_cache_release(page
);
1702 return VM_FAULT_SIGBUS
;
1706 return ret
| VM_FAULT_LOCKED
;
1710 * We're only likely to ever get here if MADV_RANDOM is in
1713 error
= page_cache_read(file
, offset
);
1716 * The page we want has now been added to the page cache.
1717 * In the unlikely event that someone removed it in the
1718 * meantime, we'll just come back here and read it again.
1724 * An error return from page_cache_read can result if the
1725 * system is low on memory, or a problem occurs while trying
1728 if (error
== -ENOMEM
)
1729 return VM_FAULT_OOM
;
1730 return VM_FAULT_SIGBUS
;
1734 * Umm, take care of errors if the page isn't up-to-date.
1735 * Try to re-read it _once_. We do this synchronously,
1736 * because there really aren't any performance issues here
1737 * and we need to check for errors.
1739 ClearPageError(page
);
1740 error
= mapping
->a_ops
->readpage(file
, page
);
1742 wait_on_page_locked(page
);
1743 if (!PageUptodate(page
))
1746 page_cache_release(page
);
1748 if (!error
|| error
== AOP_TRUNCATED_PAGE
)
1751 /* Things didn't work out. Return zero to tell the mm layer so. */
1752 shrink_readahead_size_eio(file
, ra
);
1753 return VM_FAULT_SIGBUS
;
1755 EXPORT_SYMBOL(filemap_fault
);
1757 const struct vm_operations_struct generic_file_vm_ops
= {
1758 .fault
= filemap_fault
,
1761 /* This is used for a general mmap of a disk file */
1763 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1765 struct address_space
*mapping
= file
->f_mapping
;
1767 if (!mapping
->a_ops
->readpage
)
1769 file_accessed(file
);
1770 vma
->vm_ops
= &generic_file_vm_ops
;
1771 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1776 * This is for filesystems which do not implement ->writepage.
1778 int generic_file_readonly_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1780 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
1782 return generic_file_mmap(file
, vma
);
1785 int generic_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1789 int generic_file_readonly_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1793 #endif /* CONFIG_MMU */
1795 EXPORT_SYMBOL(generic_file_mmap
);
1796 EXPORT_SYMBOL(generic_file_readonly_mmap
);
1798 static struct page
*__read_cache_page(struct address_space
*mapping
,
1800 int (*filler
)(void *, struct page
*),
1807 page
= find_get_page(mapping
, index
);
1809 page
= __page_cache_alloc(gfp
| __GFP_COLD
);
1811 return ERR_PTR(-ENOMEM
);
1812 err
= add_to_page_cache_lru(page
, mapping
, index
, GFP_KERNEL
);
1813 if (unlikely(err
)) {
1814 page_cache_release(page
);
1817 /* Presumably ENOMEM for radix tree node */
1818 return ERR_PTR(err
);
1820 err
= filler(data
, page
);
1822 page_cache_release(page
);
1823 page
= ERR_PTR(err
);
1829 static struct page
*do_read_cache_page(struct address_space
*mapping
,
1831 int (*filler
)(void *, struct page
*),
1840 page
= __read_cache_page(mapping
, index
, filler
, data
, gfp
);
1843 if (PageUptodate(page
))
1847 if (!page
->mapping
) {
1849 page_cache_release(page
);
1852 if (PageUptodate(page
)) {
1856 err
= filler(data
, page
);
1858 page_cache_release(page
);
1859 return ERR_PTR(err
);
1862 mark_page_accessed(page
);
1867 * read_cache_page_async - read into page cache, fill it if needed
1868 * @mapping: the page's address_space
1869 * @index: the page index
1870 * @filler: function to perform the read
1871 * @data: first arg to filler(data, page) function, often left as NULL
1873 * Same as read_cache_page, but don't wait for page to become unlocked
1874 * after submitting it to the filler.
1876 * Read into the page cache. If a page already exists, and PageUptodate() is
1877 * not set, try to fill the page but don't wait for it to become unlocked.
1879 * If the page does not get brought uptodate, return -EIO.
1881 struct page
*read_cache_page_async(struct address_space
*mapping
,
1883 int (*filler
)(void *, struct page
*),
1886 return do_read_cache_page(mapping
, index
, filler
, data
, mapping_gfp_mask(mapping
));
1888 EXPORT_SYMBOL(read_cache_page_async
);
1890 static struct page
*wait_on_page_read(struct page
*page
)
1892 if (!IS_ERR(page
)) {
1893 wait_on_page_locked(page
);
1894 if (!PageUptodate(page
)) {
1895 page_cache_release(page
);
1896 page
= ERR_PTR(-EIO
);
1903 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
1904 * @mapping: the page's address_space
1905 * @index: the page index
1906 * @gfp: the page allocator flags to use if allocating
1908 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
1909 * any new page allocations done using the specified allocation flags. Note
1910 * that the Radix tree operations will still use GFP_KERNEL, so you can't
1911 * expect to do this atomically or anything like that - but you can pass in
1912 * other page requirements.
1914 * If the page does not get brought uptodate, return -EIO.
1916 struct page
*read_cache_page_gfp(struct address_space
*mapping
,
1920 filler_t
*filler
= (filler_t
*)mapping
->a_ops
->readpage
;
1922 return wait_on_page_read(do_read_cache_page(mapping
, index
, filler
, NULL
, gfp
));
1924 EXPORT_SYMBOL(read_cache_page_gfp
);
1927 * read_cache_page - read into page cache, fill it if needed
1928 * @mapping: the page's address_space
1929 * @index: the page index
1930 * @filler: function to perform the read
1931 * @data: first arg to filler(data, page) function, often left as NULL
1933 * Read into the page cache. If a page already exists, and PageUptodate() is
1934 * not set, try to fill the page then wait for it to become unlocked.
1936 * If the page does not get brought uptodate, return -EIO.
1938 struct page
*read_cache_page(struct address_space
*mapping
,
1940 int (*filler
)(void *, struct page
*),
1943 return wait_on_page_read(read_cache_page_async(mapping
, index
, filler
, data
));
1945 EXPORT_SYMBOL(read_cache_page
);
1948 * The logic we want is
1950 * if suid or (sgid and xgrp)
1953 int should_remove_suid(struct dentry
*dentry
)
1955 mode_t mode
= dentry
->d_inode
->i_mode
;
1958 /* suid always must be killed */
1959 if (unlikely(mode
& S_ISUID
))
1960 kill
= ATTR_KILL_SUID
;
1963 * sgid without any exec bits is just a mandatory locking mark; leave
1964 * it alone. If some exec bits are set, it's a real sgid; kill it.
1966 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1967 kill
|= ATTR_KILL_SGID
;
1969 if (unlikely(kill
&& !capable(CAP_FSETID
) && S_ISREG(mode
)))
1974 EXPORT_SYMBOL(should_remove_suid
);
1976 static int __remove_suid(struct dentry
*dentry
, int kill
)
1978 struct iattr newattrs
;
1980 newattrs
.ia_valid
= ATTR_FORCE
| kill
;
1981 return notify_change(dentry
, &newattrs
);
1984 int file_remove_suid(struct file
*file
)
1986 struct dentry
*dentry
= file
->f_path
.dentry
;
1987 struct inode
*inode
= dentry
->d_inode
;
1992 /* Fast path for nothing security related */
1993 if (IS_NOSEC(inode
))
1996 killsuid
= should_remove_suid(dentry
);
1997 killpriv
= security_inode_need_killpriv(dentry
);
2002 error
= security_inode_killpriv(dentry
);
2003 if (!error
&& killsuid
)
2004 error
= __remove_suid(dentry
, killsuid
);
2005 if (!error
&& (inode
->i_sb
->s_flags
& MS_NOSEC
))
2006 inode
->i_flags
|= S_NOSEC
;
2010 EXPORT_SYMBOL(file_remove_suid
);
2012 static size_t __iovec_copy_from_user_inatomic(char *vaddr
,
2013 const struct iovec
*iov
, size_t base
, size_t bytes
)
2015 size_t copied
= 0, left
= 0;
2018 char __user
*buf
= iov
->iov_base
+ base
;
2019 int copy
= min(bytes
, iov
->iov_len
- base
);
2022 left
= __copy_from_user_inatomic(vaddr
, buf
, copy
);
2031 return copied
- left
;
2035 * Copy as much as we can into the page and return the number of bytes which
2036 * were successfully copied. If a fault is encountered then return the number of
2037 * bytes which were copied.
2039 size_t iov_iter_copy_from_user_atomic(struct page
*page
,
2040 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
2045 BUG_ON(!in_atomic());
2046 kaddr
= kmap_atomic(page
, KM_USER0
);
2047 if (likely(i
->nr_segs
== 1)) {
2049 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
2050 left
= __copy_from_user_inatomic(kaddr
+ offset
, buf
, bytes
);
2051 copied
= bytes
- left
;
2053 copied
= __iovec_copy_from_user_inatomic(kaddr
+ offset
,
2054 i
->iov
, i
->iov_offset
, bytes
);
2056 kunmap_atomic(kaddr
, KM_USER0
);
2060 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic
);
2063 * This has the same sideeffects and return value as
2064 * iov_iter_copy_from_user_atomic().
2065 * The difference is that it attempts to resolve faults.
2066 * Page must not be locked.
2068 size_t iov_iter_copy_from_user(struct page
*page
,
2069 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
2075 if (likely(i
->nr_segs
== 1)) {
2077 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
2078 left
= __copy_from_user(kaddr
+ offset
, buf
, bytes
);
2079 copied
= bytes
- left
;
2081 copied
= __iovec_copy_from_user_inatomic(kaddr
+ offset
,
2082 i
->iov
, i
->iov_offset
, bytes
);
2087 EXPORT_SYMBOL(iov_iter_copy_from_user
);
2089 void iov_iter_advance(struct iov_iter
*i
, size_t bytes
)
2091 BUG_ON(i
->count
< bytes
);
2093 if (likely(i
->nr_segs
== 1)) {
2094 i
->iov_offset
+= bytes
;
2097 const struct iovec
*iov
= i
->iov
;
2098 size_t base
= i
->iov_offset
;
2101 * The !iov->iov_len check ensures we skip over unlikely
2102 * zero-length segments (without overruning the iovec).
2104 while (bytes
|| unlikely(i
->count
&& !iov
->iov_len
)) {
2107 copy
= min(bytes
, iov
->iov_len
- base
);
2108 BUG_ON(!i
->count
|| i
->count
< copy
);
2112 if (iov
->iov_len
== base
) {
2118 i
->iov_offset
= base
;
2121 EXPORT_SYMBOL(iov_iter_advance
);
2124 * Fault in the first iovec of the given iov_iter, to a maximum length
2125 * of bytes. Returns 0 on success, or non-zero if the memory could not be
2126 * accessed (ie. because it is an invalid address).
2128 * writev-intensive code may want this to prefault several iovecs -- that
2129 * would be possible (callers must not rely on the fact that _only_ the
2130 * first iovec will be faulted with the current implementation).
2132 int iov_iter_fault_in_readable(struct iov_iter
*i
, size_t bytes
)
2134 char __user
*buf
= i
->iov
->iov_base
+ i
->iov_offset
;
2135 bytes
= min(bytes
, i
->iov
->iov_len
- i
->iov_offset
);
2136 return fault_in_pages_readable(buf
, bytes
);
2138 EXPORT_SYMBOL(iov_iter_fault_in_readable
);
2141 * Return the count of just the current iov_iter segment.
2143 size_t iov_iter_single_seg_count(struct iov_iter
*i
)
2145 const struct iovec
*iov
= i
->iov
;
2146 if (i
->nr_segs
== 1)
2149 return min(i
->count
, iov
->iov_len
- i
->iov_offset
);
2151 EXPORT_SYMBOL(iov_iter_single_seg_count
);
2154 * Performs necessary checks before doing a write
2156 * Can adjust writing position or amount of bytes to write.
2157 * Returns appropriate error code that caller should return or
2158 * zero in case that write should be allowed.
2160 inline int generic_write_checks(struct file
*file
, loff_t
*pos
, size_t *count
, int isblk
)
2162 struct inode
*inode
= file
->f_mapping
->host
;
2163 unsigned long limit
= rlimit(RLIMIT_FSIZE
);
2165 if (unlikely(*pos
< 0))
2169 /* FIXME: this is for backwards compatibility with 2.4 */
2170 if (file
->f_flags
& O_APPEND
)
2171 *pos
= i_size_read(inode
);
2173 if (limit
!= RLIM_INFINITY
) {
2174 if (*pos
>= limit
) {
2175 send_sig(SIGXFSZ
, current
, 0);
2178 if (*count
> limit
- (typeof(limit
))*pos
) {
2179 *count
= limit
- (typeof(limit
))*pos
;
2187 if (unlikely(*pos
+ *count
> MAX_NON_LFS
&&
2188 !(file
->f_flags
& O_LARGEFILE
))) {
2189 if (*pos
>= MAX_NON_LFS
) {
2192 if (*count
> MAX_NON_LFS
- (unsigned long)*pos
) {
2193 *count
= MAX_NON_LFS
- (unsigned long)*pos
;
2198 * Are we about to exceed the fs block limit ?
2200 * If we have written data it becomes a short write. If we have
2201 * exceeded without writing data we send a signal and return EFBIG.
2202 * Linus frestrict idea will clean these up nicely..
2204 if (likely(!isblk
)) {
2205 if (unlikely(*pos
>= inode
->i_sb
->s_maxbytes
)) {
2206 if (*count
|| *pos
> inode
->i_sb
->s_maxbytes
) {
2209 /* zero-length writes at ->s_maxbytes are OK */
2212 if (unlikely(*pos
+ *count
> inode
->i_sb
->s_maxbytes
))
2213 *count
= inode
->i_sb
->s_maxbytes
- *pos
;
2217 if (bdev_read_only(I_BDEV(inode
)))
2219 isize
= i_size_read(inode
);
2220 if (*pos
>= isize
) {
2221 if (*count
|| *pos
> isize
)
2225 if (*pos
+ *count
> isize
)
2226 *count
= isize
- *pos
;
2233 EXPORT_SYMBOL(generic_write_checks
);
2235 int pagecache_write_begin(struct file
*file
, struct address_space
*mapping
,
2236 loff_t pos
, unsigned len
, unsigned flags
,
2237 struct page
**pagep
, void **fsdata
)
2239 const struct address_space_operations
*aops
= mapping
->a_ops
;
2241 return aops
->write_begin(file
, mapping
, pos
, len
, flags
,
2244 EXPORT_SYMBOL(pagecache_write_begin
);
2246 int pagecache_write_end(struct file
*file
, struct address_space
*mapping
,
2247 loff_t pos
, unsigned len
, unsigned copied
,
2248 struct page
*page
, void *fsdata
)
2250 const struct address_space_operations
*aops
= mapping
->a_ops
;
2252 mark_page_accessed(page
);
2253 return aops
->write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
2255 EXPORT_SYMBOL(pagecache_write_end
);
2258 generic_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2259 unsigned long *nr_segs
, loff_t pos
, loff_t
*ppos
,
2260 size_t count
, size_t ocount
)
2262 struct file
*file
= iocb
->ki_filp
;
2263 struct address_space
*mapping
= file
->f_mapping
;
2264 struct inode
*inode
= mapping
->host
;
2269 if (count
!= ocount
)
2270 *nr_segs
= iov_shorten((struct iovec
*)iov
, *nr_segs
, count
);
2272 write_len
= iov_length(iov
, *nr_segs
);
2273 end
= (pos
+ write_len
- 1) >> PAGE_CACHE_SHIFT
;
2275 written
= filemap_write_and_wait_range(mapping
, pos
, pos
+ write_len
- 1);
2280 * After a write we want buffered reads to be sure to go to disk to get
2281 * the new data. We invalidate clean cached page from the region we're
2282 * about to write. We do this *before* the write so that we can return
2283 * without clobbering -EIOCBQUEUED from ->direct_IO().
2285 if (mapping
->nrpages
) {
2286 written
= invalidate_inode_pages2_range(mapping
,
2287 pos
>> PAGE_CACHE_SHIFT
, end
);
2289 * If a page can not be invalidated, return 0 to fall back
2290 * to buffered write.
2293 if (written
== -EBUSY
)
2299 written
= mapping
->a_ops
->direct_IO(WRITE
, iocb
, iov
, pos
, *nr_segs
);
2302 * Finally, try again to invalidate clean pages which might have been
2303 * cached by non-direct readahead, or faulted in by get_user_pages()
2304 * if the source of the write was an mmap'ed region of the file
2305 * we're writing. Either one is a pretty crazy thing to do,
2306 * so we don't support it 100%. If this invalidation
2307 * fails, tough, the write still worked...
2309 if (mapping
->nrpages
) {
2310 invalidate_inode_pages2_range(mapping
,
2311 pos
>> PAGE_CACHE_SHIFT
, end
);
2316 if (pos
> i_size_read(inode
) && !S_ISBLK(inode
->i_mode
)) {
2317 i_size_write(inode
, pos
);
2318 mark_inode_dirty(inode
);
2325 EXPORT_SYMBOL(generic_file_direct_write
);
2328 * Find or create a page at the given pagecache position. Return the locked
2329 * page. This function is specifically for buffered writes.
2331 struct page
*grab_cache_page_write_begin(struct address_space
*mapping
,
2332 pgoff_t index
, unsigned flags
)
2336 gfp_t gfp_notmask
= 0;
2337 if (flags
& AOP_FLAG_NOFS
)
2338 gfp_notmask
= __GFP_FS
;
2340 page
= find_lock_page(mapping
, index
);
2344 page
= __page_cache_alloc(mapping_gfp_mask(mapping
) & ~gfp_notmask
);
2347 status
= add_to_page_cache_lru(page
, mapping
, index
,
2348 GFP_KERNEL
& ~gfp_notmask
);
2349 if (unlikely(status
)) {
2350 page_cache_release(page
);
2351 if (status
== -EEXIST
)
2356 wait_on_page_writeback(page
);
2359 EXPORT_SYMBOL(grab_cache_page_write_begin
);
2361 static ssize_t
generic_perform_write(struct file
*file
,
2362 struct iov_iter
*i
, loff_t pos
)
2364 struct address_space
*mapping
= file
->f_mapping
;
2365 const struct address_space_operations
*a_ops
= mapping
->a_ops
;
2367 ssize_t written
= 0;
2368 unsigned int flags
= 0;
2371 * Copies from kernel address space cannot fail (NFSD is a big user).
2373 if (segment_eq(get_fs(), KERNEL_DS
))
2374 flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
2378 unsigned long offset
; /* Offset into pagecache page */
2379 unsigned long bytes
; /* Bytes to write to page */
2380 size_t copied
; /* Bytes copied from user */
2383 offset
= (pos
& (PAGE_CACHE_SIZE
- 1));
2384 bytes
= min_t(unsigned long, PAGE_CACHE_SIZE
- offset
,
2390 * Bring in the user page that we will copy from _first_.
2391 * Otherwise there's a nasty deadlock on copying from the
2392 * same page as we're writing to, without it being marked
2395 * Not only is this an optimisation, but it is also required
2396 * to check that the address is actually valid, when atomic
2397 * usercopies are used, below.
2399 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
2404 status
= a_ops
->write_begin(file
, mapping
, pos
, bytes
, flags
,
2406 if (unlikely(status
))
2409 if (mapping_writably_mapped(mapping
))
2410 flush_dcache_page(page
);
2412 pagefault_disable();
2413 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
2415 flush_dcache_page(page
);
2417 mark_page_accessed(page
);
2418 status
= a_ops
->write_end(file
, mapping
, pos
, bytes
, copied
,
2420 if (unlikely(status
< 0))
2426 iov_iter_advance(i
, copied
);
2427 if (unlikely(copied
== 0)) {
2429 * If we were unable to copy any data at all, we must
2430 * fall back to a single segment length write.
2432 * If we didn't fallback here, we could livelock
2433 * because not all segments in the iov can be copied at
2434 * once without a pagefault.
2436 bytes
= min_t(unsigned long, PAGE_CACHE_SIZE
- offset
,
2437 iov_iter_single_seg_count(i
));
2443 balance_dirty_pages_ratelimited(mapping
);
2445 } while (iov_iter_count(i
));
2447 return written
? written
: status
;
2451 generic_file_buffered_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2452 unsigned long nr_segs
, loff_t pos
, loff_t
*ppos
,
2453 size_t count
, ssize_t written
)
2455 struct file
*file
= iocb
->ki_filp
;
2459 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
2460 status
= generic_perform_write(file
, &i
, pos
);
2462 if (likely(status
>= 0)) {
2464 *ppos
= pos
+ status
;
2467 return written
? written
: status
;
2469 EXPORT_SYMBOL(generic_file_buffered_write
);
2472 * __generic_file_aio_write - write data to a file
2473 * @iocb: IO state structure (file, offset, etc.)
2474 * @iov: vector with data to write
2475 * @nr_segs: number of segments in the vector
2476 * @ppos: position where to write
2478 * This function does all the work needed for actually writing data to a
2479 * file. It does all basic checks, removes SUID from the file, updates
2480 * modification times and calls proper subroutines depending on whether we
2481 * do direct IO or a standard buffered write.
2483 * It expects i_mutex to be grabbed unless we work on a block device or similar
2484 * object which does not need locking at all.
2486 * This function does *not* take care of syncing data in case of O_SYNC write.
2487 * A caller has to handle it. This is mainly due to the fact that we want to
2488 * avoid syncing under i_mutex.
2490 ssize_t
__generic_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2491 unsigned long nr_segs
, loff_t
*ppos
)
2493 struct file
*file
= iocb
->ki_filp
;
2494 struct address_space
* mapping
= file
->f_mapping
;
2495 size_t ocount
; /* original count */
2496 size_t count
; /* after file limit checks */
2497 struct inode
*inode
= mapping
->host
;
2503 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
2510 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2512 /* We can write back this queue in page reclaim */
2513 current
->backing_dev_info
= mapping
->backing_dev_info
;
2516 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2523 err
= file_remove_suid(file
);
2527 file_update_time(file
);
2529 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
2530 if (unlikely(file
->f_flags
& O_DIRECT
)) {
2532 ssize_t written_buffered
;
2534 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
, pos
,
2535 ppos
, count
, ocount
);
2536 if (written
< 0 || written
== count
)
2539 * direct-io write to a hole: fall through to buffered I/O
2540 * for completing the rest of the request.
2544 written_buffered
= generic_file_buffered_write(iocb
, iov
,
2545 nr_segs
, pos
, ppos
, count
,
2548 * If generic_file_buffered_write() retuned a synchronous error
2549 * then we want to return the number of bytes which were
2550 * direct-written, or the error code if that was zero. Note
2551 * that this differs from normal direct-io semantics, which
2552 * will return -EFOO even if some bytes were written.
2554 if (written_buffered
< 0) {
2555 err
= written_buffered
;
2560 * We need to ensure that the page cache pages are written to
2561 * disk and invalidated to preserve the expected O_DIRECT
2564 endbyte
= pos
+ written_buffered
- written
- 1;
2565 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
, endbyte
);
2567 written
= written_buffered
;
2568 invalidate_mapping_pages(mapping
,
2569 pos
>> PAGE_CACHE_SHIFT
,
2570 endbyte
>> PAGE_CACHE_SHIFT
);
2573 * We don't know how much we wrote, so just return
2574 * the number of bytes which were direct-written
2578 written
= generic_file_buffered_write(iocb
, iov
, nr_segs
,
2579 pos
, ppos
, count
, written
);
2582 current
->backing_dev_info
= NULL
;
2583 return written
? written
: err
;
2585 EXPORT_SYMBOL(__generic_file_aio_write
);
2588 * generic_file_aio_write - write data to a file
2589 * @iocb: IO state structure
2590 * @iov: vector with data to write
2591 * @nr_segs: number of segments in the vector
2592 * @pos: position in file where to write
2594 * This is a wrapper around __generic_file_aio_write() to be used by most
2595 * filesystems. It takes care of syncing the file in case of O_SYNC file
2596 * and acquires i_mutex as needed.
2598 ssize_t
generic_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2599 unsigned long nr_segs
, loff_t pos
)
2601 struct file
*file
= iocb
->ki_filp
;
2602 struct inode
*inode
= file
->f_mapping
->host
;
2603 struct blk_plug plug
;
2606 BUG_ON(iocb
->ki_pos
!= pos
);
2608 mutex_lock(&inode
->i_mutex
);
2609 blk_start_plug(&plug
);
2610 ret
= __generic_file_aio_write(iocb
, iov
, nr_segs
, &iocb
->ki_pos
);
2611 mutex_unlock(&inode
->i_mutex
);
2613 if (ret
> 0 || ret
== -EIOCBQUEUED
) {
2616 err
= generic_write_sync(file
, pos
, ret
);
2617 if (err
< 0 && ret
> 0)
2620 blk_finish_plug(&plug
);
2623 EXPORT_SYMBOL(generic_file_aio_write
);
2626 * try_to_release_page() - release old fs-specific metadata on a page
2628 * @page: the page which the kernel is trying to free
2629 * @gfp_mask: memory allocation flags (and I/O mode)
2631 * The address_space is to try to release any data against the page
2632 * (presumably at page->private). If the release was successful, return `1'.
2633 * Otherwise return zero.
2635 * This may also be called if PG_fscache is set on a page, indicating that the
2636 * page is known to the local caching routines.
2638 * The @gfp_mask argument specifies whether I/O may be performed to release
2639 * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
2642 int try_to_release_page(struct page
*page
, gfp_t gfp_mask
)
2644 struct address_space
* const mapping
= page
->mapping
;
2646 BUG_ON(!PageLocked(page
));
2647 if (PageWriteback(page
))
2650 if (mapping
&& mapping
->a_ops
->releasepage
)
2651 return mapping
->a_ops
->releasepage(page
, gfp_mask
);
2652 return try_to_free_buffers(page
);
2655 EXPORT_SYMBOL(try_to_release_page
);