Lines Matching +full:wait +full:- +full:on +full:- +full:read
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1994-1999 Linus Torvalds
28 #include <linux/error-injection.h>
31 #include <linux/backing-dev.h>
63 * finished 'unifying' the page and buffer cache and SMP-threaded the
64 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
66 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
72 * ->i_mmap_rwsem (truncate_pagecache)
73 * ->private_lock (__free_pte->__set_page_dirty_buffers)
74 * ->swap_lock (exclusive_swap_page, others)
75 * ->i_pages lock
77 * ->i_mutex
78 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
80 * ->mmap_lock
81 * ->i_mmap_rwsem
82 * ->page_table_lock or pte_lock (various, mainly in memory.c)
83 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
85 * ->mmap_lock
86 * ->lock_page (access_process_vm)
88 * ->i_mutex (generic_perform_write)
89 * ->mmap_lock (fault_in_pages_readable->do_page_fault)
91 * bdi->wb.list_lock
92 * sb_lock (fs/fs-writeback.c)
93 * ->i_pages lock (__sync_single_inode)
95 * ->i_mmap_rwsem
96 * ->anon_vma.lock (vma_adjust)
98 * ->anon_vma.lock
99 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
101 * ->page_table_lock or pte_lock
102 * ->swap_lock (try_to_unmap_one)
103 * ->private_lock (try_to_unmap_one)
104 * ->i_pages lock (try_to_unmap_one)
105 * ->pgdat->lru_lock (follow_page->mark_page_accessed)
106 * ->pgdat->lru_lock (check_pte_range->isolate_lru_page)
107 * ->private_lock (page_remove_rmap->set_page_dirty)
108 * ->i_pages lock (page_remove_rmap->set_page_dirty)
109 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
110 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
111 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
112 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
113 * ->inode->i_lock (zap_pte_range->set_page_dirty)
114 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
116 * ->i_mmap_rwsem
117 * ->tasklist_lock (memory_failure, collect_procs_ao)
123 XA_STATE(xas, &mapping->i_pages, page->index); in page_cache_delete()
130 xas_set_order(&xas, page->index, compound_order(page)); in page_cache_delete()
141 page->mapping = NULL; in page_cache_delete()
142 /* Leave page->index set: truncation lookup relies upon it */ in page_cache_delete()
145 mapping->nrexceptional += nr; in page_cache_delete()
154 mapping->nrpages -= nr; in page_cache_delete()
178 current->comm, page_to_pfn(page)); in unaccount_page_cache_page()
203 __mod_lruvec_page_state(page, NR_FILE_PAGES, -nr); in unaccount_page_cache_page()
205 __mod_lruvec_page_state(page, NR_SHMEM, -nr); in unaccount_page_cache_page()
224 account_page_cleaned(page, mapping, inode_to_wb(mapping->host)); in unaccount_page_cache_page()
229 * sure the page is locked and that nobody else uses it - or that usage
234 struct address_space *mapping = page->mapping; in __delete_from_page_cache()
247 freepage = mapping->a_ops->freepage; in page_cache_free_page()
260 * delete_from_page_cache - delete page from page cache
263 * This must be called only on pages that have been verified to be in the page
265 * has a reference on the page.
273 xa_lock_irqsave(&mapping->i_pages, flags); in delete_from_page_cache()
275 xa_unlock_irqrestore(&mapping->i_pages, flags); in delete_from_page_cache()
282 * page_cache_delete_batch - delete several pages from page cache
286 * The function walks over mapping->i_pages and removes pages passed in @pvec
298 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index); in page_cache_delete_batch()
318 if (page != pvec->pages[i]) { in page_cache_delete_batch()
319 VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index, in page_cache_delete_batch()
326 if (page->index == xas.xa_index) in page_cache_delete_batch()
327 page->mapping = NULL; in page_cache_delete_batch()
328 /* Leave page->index set: truncation lookup relies on it */ in page_cache_delete_batch()
332 * page or the index is of the last sub-page of this compound in page_cache_delete_batch()
335 if (page->index + compound_nr(page) - 1 == xas.xa_index) in page_cache_delete_batch()
340 mapping->nrpages -= total_pages; in page_cache_delete_batch()
352 xa_lock_irqsave(&mapping->i_pages, flags); in delete_from_page_cache_batch()
354 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]); in delete_from_page_cache_batch()
356 unaccount_page_cache_page(mapping, pvec->pages[i]); in delete_from_page_cache_batch()
359 xa_unlock_irqrestore(&mapping->i_pages, flags); in delete_from_page_cache_batch()
362 page_cache_free_page(mapping, pvec->pages[i]); in delete_from_page_cache_batch()
369 if (test_bit(AS_ENOSPC, &mapping->flags) && in filemap_check_errors()
370 test_and_clear_bit(AS_ENOSPC, &mapping->flags)) in filemap_check_errors()
371 ret = -ENOSPC; in filemap_check_errors()
372 if (test_bit(AS_EIO, &mapping->flags) && in filemap_check_errors()
373 test_and_clear_bit(AS_EIO, &mapping->flags)) in filemap_check_errors()
374 ret = -EIO; in filemap_check_errors()
382 if (test_bit(AS_EIO, &mapping->flags)) in filemap_check_and_keep_errors()
383 return -EIO; in filemap_check_and_keep_errors()
384 if (test_bit(AS_ENOSPC, &mapping->flags)) in filemap_check_and_keep_errors()
385 return -ENOSPC; in filemap_check_and_keep_errors()
390 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
404 * Return: %0 on success, negative error code otherwise.
421 wbc_attach_fdatawrite_inode(&wbc, mapping->host); in __filemap_fdatawrite_range()
447 * filemap_flush - mostly a non-blocking flush
450 * This is a mostly non-blocking flush. Not suitable for data-integrity
451 * purposes - I/O may not be started against all dirty pages.
453 * Return: %0 on success, negative error code otherwise.
462 * filemap_range_has_page - check if a page exists in range.
477 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); in filemap_range_has_page()
536 * filemap_fdatawait_range - wait for writeback to complete
537 * @mapping: address space structure to wait for
541 * Walk the list of under-writeback pages of the given address space
542 * in the given range and wait for all of them. Check error status of
560 * filemap_fdatawait_range_keep_errors - wait for writeback to complete
561 * @mapping: address space structure to wait for
565 * Walk the list of under-writeback pages of the given address space in the
566 * given range and wait for all of them. Unlike filemap_fdatawait_range(),
570 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
582 * file_fdatawait_range - wait for writeback to complete
583 * @file: file pointing to address space structure to wait for
587 * Walk the list of under-writeback pages of the address space that file
588 * refers to, in the given range and wait for all of them. Check error
589 * status of the address space vs. the file->f_wb_err cursor and return it.
595 * Return: error status of the address space vs. the file->f_wb_err cursor.
599 struct address_space *mapping = file->f_mapping; in file_fdatawait_range()
607 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
608 * @mapping: address space structure to wait for
610 * Walk the list of under-writeback pages of the given address space
611 * and wait for all of them. Unlike filemap_fdatawait(), this function
615 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
631 return mapping->nrexceptional; in mapping_needs_writeback()
633 return mapping->nrpages; in mapping_needs_writeback()
637 * filemap_write_and_wait_range - write out & wait on a file range
642 * Write out and wait upon file offsets lstart->lend, inclusive.
645 * that this function can be used to write to the very end-of-file (end = -1).
659 * written partially (e.g. -ENOSPC), so we wait for it. in filemap_write_and_wait_range()
660 * But the -EIO is special case, it may indicate the worst in filemap_write_and_wait_range()
663 if (err != -EIO) { in filemap_write_and_wait_range()
681 errseq_t eseq = errseq_set(&mapping->wb_err, err); in __filemap_set_wb_err()
688 * file_check_and_advance_wb_err - report wb error (if any) that was previously
690 * @file: struct file on which the error is being reported
705 * While we handle mapping->wb_err with atomic operations, the f_wb_err
709 * Return: %0 on success, negative error code otherwise.
714 errseq_t old = READ_ONCE(file->f_wb_err); in file_check_and_advance_wb_err()
715 struct address_space *mapping = file->f_mapping; in file_check_and_advance_wb_err()
718 if (errseq_check(&mapping->wb_err, old)) { in file_check_and_advance_wb_err()
720 spin_lock(&file->f_lock); in file_check_and_advance_wb_err()
721 old = file->f_wb_err; in file_check_and_advance_wb_err()
722 err = errseq_check_and_advance(&mapping->wb_err, in file_check_and_advance_wb_err()
723 &file->f_wb_err); in file_check_and_advance_wb_err()
725 spin_unlock(&file->f_lock); in file_check_and_advance_wb_err()
731 * that the legacy code would have had on these flags. in file_check_and_advance_wb_err()
733 clear_bit(AS_EIO, &mapping->flags); in file_check_and_advance_wb_err()
734 clear_bit(AS_ENOSPC, &mapping->flags); in file_check_and_advance_wb_err()
740 * file_write_and_wait_range - write out & wait on a file range
745 * Write out and wait upon file offsets lstart->lend, inclusive.
748 * that this function can be used to write to the very end-of-file (end = -1).
750 * After writing out and waiting on the data, we check and advance the
753 * Return: %0 on success, negative error code otherwise.
758 struct address_space *mapping = file->f_mapping; in file_write_and_wait_range()
764 if (err != -EIO) in file_write_and_wait_range()
775 * replace_page_cache_page - replace a pagecache page with a new one
780 * This function replaces a page in the pagecache with a new one. On
792 struct address_space *mapping = old->mapping; in replace_page_cache_page()
793 void (*freepage)(struct page *) = mapping->a_ops->freepage; in replace_page_cache_page()
794 pgoff_t offset = old->index; in replace_page_cache_page()
795 XA_STATE(xas, &mapping->i_pages, offset); in replace_page_cache_page()
800 VM_BUG_ON_PAGE(new->mapping, new); in replace_page_cache_page()
803 new->mapping = mapping; in replace_page_cache_page()
804 new->index = offset; in replace_page_cache_page()
811 old->mapping = NULL; in replace_page_cache_page()
835 XA_STATE(xas, &mapping->i_pages, offset); in __add_to_page_cache_locked()
845 page->mapping = mapping; in __add_to_page_cache_locked()
846 page->index = offset; in __add_to_page_cache_locked()
849 error = mem_cgroup_charge(page, current->mm, gfp); in __add_to_page_cache_locked()
868 xas_set_err(&xas, -EEXIST); in __add_to_page_cache_locked()
889 mapping->nrexceptional--; in __add_to_page_cache_locked()
890 mapping->nrpages++; in __add_to_page_cache_locked()
909 page->mapping = NULL; in __add_to_page_cache_locked()
910 /* Leave page->index set: truncation relies upon it */ in __add_to_page_cache_locked()
917 * add_to_page_cache_locked - add a locked page to the pagecache
926 * Return: %0 on success, negative error code otherwise.
987 * In order to wait for pages to become available there must be
990 * waiters on the same queue and wake all when any of the pages
1016 * The page wait code treats the "wait->flags" somewhat unusually, because
1026 * and remove it from the wait queue.
1034 * WQ_FLAG_WOKEN bit, wake it up, and remove it from the wait queue.
1036 * This is the traditional exclusive wait.
1042 * cannot be taken, we stop walking the wait queue without waking
1049 static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg) in wake_page_function() argument
1054 = container_of(wait, struct wait_page_queue, wait); in wake_page_function()
1060 * If it's a lock handoff wait, we get the bit for it, and in wake_page_function()
1063 flags = wait->flags; in wake_page_function()
1065 if (test_bit(key->bit_nr, &key->page->flags)) in wake_page_function()
1066 return -1; in wake_page_function()
1068 if (test_and_set_bit(key->bit_nr, &key->page->flags)) in wake_page_function()
1069 return -1; in wake_page_function()
1075 * We are holding the wait-queue lock, but the waiter that in wake_page_function()
1080 * afterwards to avoid any races. This store-release pairs in wake_page_function()
1081 * with the load-acquire in wait_on_page_bit_common(). in wake_page_function()
1083 smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN); in wake_page_function()
1084 wake_up_state(wait->private, mode); in wake_page_function()
1088 * and we can unconditionally remove the wait entry. in wake_page_function()
1092 * After this list_del_init(&wait->entry) the wait entry in wake_page_function()
1093 * might be de-allocated and the process might even have in wake_page_function()
1096 list_del_init_careful(&wait->entry); in wake_page_function()
1116 spin_lock_irqsave(&q->lock, flags); in wake_up_page_bit()
1124 * from wait queue in wake_up_page_bit()
1126 spin_unlock_irqrestore(&q->lock, flags); in wake_up_page_bit()
1128 spin_lock_irqsave(&q->lock, flags); in wake_up_page_bit()
1133 * It is possible for other pages to have collided on the waitqueue in wake_up_page_bit()
1134 * hash, so in that case check for a page match. That prevents a long- in wake_up_page_bit()
1146 * other pages on it. in wake_up_page_bit()
1151 spin_unlock_irqrestore(&q->lock, flags); in wake_up_page_bit()
1166 * __lock_page() waiting on then setting PG_locked.
1169 * wait_on_page_writeback() waiting on PG_writeback.
1171 DROP, /* Drop ref to page before wait, no check when woken,
1172 * like put_and_wait_on_page_locked() on PG_locked.
1181 struct wait_queue_entry *wait) in trylock_page_bit_common() argument
1183 if (wait->flags & WQ_FLAG_EXCLUSIVE) { in trylock_page_bit_common()
1184 if (test_and_set_bit(bit_nr, &page->flags)) in trylock_page_bit_common()
1186 } else if (test_bit(bit_nr, &page->flags)) in trylock_page_bit_common()
1189 wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE; in trylock_page_bit_common()
1201 wait_queue_entry_t *wait = &wait_page.wait; in wait_on_page_bit_common() local
1216 init_wait(wait); in wait_on_page_bit_common()
1217 wait->func = wake_page_function; in wait_on_page_bit_common()
1222 wait->flags = 0; in wait_on_page_bit_common()
1224 wait->flags = WQ_FLAG_EXCLUSIVE; in wait_on_page_bit_common()
1225 if (--unfairness < 0) in wait_on_page_bit_common()
1226 wait->flags |= WQ_FLAG_CUSTOM; in wait_on_page_bit_common()
1237 * page queue), and add ourselves to the wait in wait_on_page_bit_common()
1243 spin_lock_irq(&q->lock); in wait_on_page_bit_common()
1245 if (!trylock_page_bit_common(page, bit_nr, wait)) in wait_on_page_bit_common()
1246 __add_wait_queue_entry_tail(q, wait); in wait_on_page_bit_common()
1247 spin_unlock_irq(&q->lock); in wait_on_page_bit_common()
1250 * From now on, all the logic will be based on in wait_on_page_bit_common()
1263 * be very careful with the 'wait->flags', because in wait_on_page_bit_common()
1272 flags = smp_load_acquire(&wait->flags); in wait_on_page_bit_common()
1281 /* If we were non-exclusive, we're done */ in wait_on_page_bit_common()
1295 if (unlikely(test_and_set_bit(bit_nr, &page->flags))) in wait_on_page_bit_common()
1298 wait->flags |= WQ_FLAG_DONE; in wait_on_page_bit_common()
1304 * waiter from the wait-queues, but the PageWaiters bit will remain in wait_on_page_bit_common()
1308 finish_wait(q, wait); in wait_on_page_bit_common()
1317 * NOTE! The wait->flags weren't stable until we've done the in wait_on_page_bit_common()
1324 * return value based on that state without races. in wait_on_page_bit_common()
1326 * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive in wait_on_page_bit_common()
1330 return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR; in wait_on_page_bit_common()
1332 return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR; in wait_on_page_bit_common()
1350 struct wait_page_queue *wait, bool set) in __wait_on_page_locked_async() argument
1355 wait->page = page; in __wait_on_page_locked_async()
1356 wait->bit_nr = PG_locked; in __wait_on_page_locked_async()
1358 spin_lock_irq(&q->lock); in __wait_on_page_locked_async()
1359 __add_wait_queue_entry_tail(q, &wait->wait); in __wait_on_page_locked_async()
1366 * If we were succesful now, we know we're still on the in __wait_on_page_locked_async()
1372 __remove_wait_queue(q, &wait->wait); in __wait_on_page_locked_async()
1374 ret = -EIOCBQUEUED; in __wait_on_page_locked_async()
1375 spin_unlock_irq(&q->lock); in __wait_on_page_locked_async()
1380 struct wait_page_queue *wait) in wait_on_page_locked_async() argument
1384 return __wait_on_page_locked_async(compound_head(page), wait, false); in wait_on_page_locked_async()
1388 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1389 * @page: The page to wait for.
1391 * The caller should hold a reference on @page. They expect the page to
1407 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
1408 * @page: Page defining the wait queue of interest
1411 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1418 spin_lock_irqsave(&q->lock, flags); in add_page_wait_queue()
1421 spin_unlock_irqrestore(&q->lock, flags); in add_page_wait_queue()
1430 * On x86 (and on many other architectures), we can clear PG_lock and
1435 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1449 * unlock_page - unlock a locked page
1455 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1457 * Note that this depends on PG_waiters being the sign bit in the byte
1458 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1468 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags)) in unlock_page()
1474 * end_page_writeback - end writeback against a page
1493 * on truncation to wait for the clearing of PG_writeback. in end_page_writeback()
1508 * After completing I/O on a page, call this routine to update the page
1536 * __lock_page - get a lock on the page, assuming we need to sleep to get it
1557 int __lock_page_async(struct page *page, struct wait_page_queue *wait) in __lock_page_async() argument
1559 return __wait_on_page_locked_async(page, wait, true); in __lock_page_async()
1564 * 1 - page is locked; mmap_lock is still held.
1565 * 0 - page is not locked.
1606 * page_cache_next_miss() - Find the next gap in the page cache.
1611 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1621 * range specified (in which case 'return - index >= max_scan' will be true).
1622 * In the rare case of index wrap-around, 0 will be returned.
1627 XA_STATE(xas, &mapping->i_pages, index); in page_cache_next_miss()
1629 while (max_scan--) { in page_cache_next_miss()
1642 * page_cache_prev_miss() - Find the previous gap in the page cache.
1647 * Search the range [max(index - max_scan + 1, 0), index] for the
1657 * range specified (in which case 'index - return >= max_scan' will be true).
1658 * In the rare case of wrap-around, ULONG_MAX will be returned.
1663 XA_STATE(xas, &mapping->i_pages, index); in page_cache_prev_miss()
1665 while (max_scan--) { in page_cache_prev_miss()
1678 * find_get_entry - find and get a page cache entry
1692 XA_STATE(xas, &mapping->i_pages, index); in find_get_entry()
1727 * find_lock_entry - Locate and lock a page cache entry.
1749 if (unlikely(page->mapping != mapping)) { in find_lock_entry()
1760 * pagecache_get_page - Find and get a reference to a page.
1770 * * %FGP_ACCESSED - The page will be marked accessed.
1771 * * %FGP_LOCK - The page is returned locked.
1772 * * %FGP_HEAD - If the page is present and a THP, return the head page
1774 * * %FGP_CREAT - If no page is present then a new page is allocated using
1777 * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the
1780 * * %FGP_WRITE - The page will be written
1781 * * %FGP_NOFS - __GFP_FS will get cleared in gfp mask
1782 * * %FGP_NOWAIT - Don't get blocked by page lock
1814 if (unlikely(page->mapping != mapping)) { in pagecache_get_page()
1855 if (err == -EEXIST) in pagecache_get_page()
1872 * find_get_entries - gang pagecache lookup
1884 * The search returns a group of mapping-contiguous page cache entries
1886 * not-present pages.
1902 XA_STATE(xas, &mapping->i_pages, start); in find_get_entries()
1929 * Terminate early on finding a THP, to allow the caller to in find_get_entries()
1952 * find_get_pages_range - gang pagecache lookup
1964 * The search returns a group of mapping-contiguous pages with ascending
1965 * indexes. There may be holes in the indices due to not-present pages.
1976 XA_STATE(xas, &mapping->i_pages, *start); in find_get_pages_range()
2013 * breaks the iteration when there is a page at index -1 but that is in find_get_pages_range()
2016 if (end == (pgoff_t)-1) in find_get_pages_range()
2017 *start = (pgoff_t)-1; in find_get_pages_range()
2027 * find_get_pages_contig - gang contiguous pagecache lookup
2041 XA_STATE(xas, &mapping->i_pages, index); in find_get_pages_contig()
2081 * find_get_pages_range_tag - find and return pages in given range matching @tag
2098 XA_STATE(xas, &mapping->i_pages, *index); in find_get_pages_range_tag()
2139 * iteration when there is a page at index -1 but that is already in find_get_pages_range_tag()
2142 if (end == (pgoff_t)-1) in find_get_pages_range_tag()
2143 *index = (pgoff_t)-1; in find_get_pages_range_tag()
2157 * ---R__________________________________________B__________
2160 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2161 * => failing the whole request => read(R) => read(R+1) =>
2162 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2163 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2164 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2170 ra->ra_pages /= 4; in shrink_readahead_size_eio()
2174 * generic_file_buffered_read - generic file read routine
2175 * @iocb: the iocb to read
2179 * This is a generic file read routine, and uses the
2180 * mapping->a_ops->readpage() function for the actual low-level stuff.
2192 struct file *filp = iocb->ki_filp; in generic_file_buffered_read()
2193 struct address_space *mapping = filp->f_mapping; in generic_file_buffered_read()
2194 struct inode *inode = mapping->host; in generic_file_buffered_read()
2195 struct file_ra_state *ra = &filp->f_ra; in generic_file_buffered_read()
2196 loff_t *ppos = &iocb->ki_pos; in generic_file_buffered_read()
2204 if (unlikely(*ppos >= inode->i_sb->s_maxbytes)) in generic_file_buffered_read()
2209 iov_iter_truncate(iter, inode->i_sb->s_maxbytes); in generic_file_buffered_read()
2212 prev_index = ra->prev_pos >> PAGE_SHIFT; in generic_file_buffered_read()
2213 prev_offset = ra->prev_pos & (PAGE_SIZE-1); in generic_file_buffered_read()
2214 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT; in generic_file_buffered_read()
2219 * can no longer safely return -EIOCBQUEUED. Hence mark in generic_file_buffered_read()
2220 * an async read NOWAIT at that point. in generic_file_buffered_read()
2222 if (written && (iocb->ki_flags & IOCB_WAITQ)) in generic_file_buffered_read()
2223 iocb->ki_flags |= IOCB_NOWAIT; in generic_file_buffered_read()
2234 error = -EINTR; in generic_file_buffered_read()
2240 if (iocb->ki_flags & IOCB_NOIO) in generic_file_buffered_read()
2244 index, last_index - index); in generic_file_buffered_read()
2250 if (iocb->ki_flags & IOCB_NOIO) { in generic_file_buffered_read()
2256 index, last_index - index); in generic_file_buffered_read()
2260 * See comment in do_read_cache_page on why in generic_file_buffered_read()
2264 if (iocb->ki_flags & IOCB_WAITQ) { in generic_file_buffered_read()
2270 iocb->ki_waitq); in generic_file_buffered_read()
2272 if (iocb->ki_flags & IOCB_NOWAIT) { in generic_file_buffered_read()
2283 if (inode->i_blkbits == PAGE_SHIFT || in generic_file_buffered_read()
2284 !mapping->a_ops->is_partially_uptodate) in generic_file_buffered_read()
2292 if (!page->mapping) in generic_file_buffered_read()
2294 if (!mapping->a_ops->is_partially_uptodate(page, in generic_file_buffered_read()
2295 offset, iter->count)) in generic_file_buffered_read()
2304 * the correct value for "nr", which means the zero-filled in generic_file_buffered_read()
2306 * another truncate extends the file - this is desired though). in generic_file_buffered_read()
2310 end_index = (isize - 1) >> PAGE_SHIFT; in generic_file_buffered_read()
2319 nr = ((isize - 1) & ~PAGE_MASK) + 1; in generic_file_buffered_read()
2325 nr = nr - offset; in generic_file_buffered_read()
2329 * before reading the page on the kernel side. in generic_file_buffered_read()
2335 * When a sequential read accesses a page several times, in generic_file_buffered_read()
2343 * Ok, we have the page, and it's up-to-date, so in generic_file_buffered_read()
2358 error = -EFAULT; in generic_file_buffered_read()
2365 if (iocb->ki_flags & IOCB_WAITQ) { in generic_file_buffered_read()
2370 error = lock_page_async(page, iocb->ki_waitq); in generic_file_buffered_read()
2379 if (!page->mapping) { in generic_file_buffered_read()
2392 if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT)) { in generic_file_buffered_read()
2403 /* Start the actual read. The read will unlock the page. */ in generic_file_buffered_read()
2404 error = mapping->a_ops->readpage(filp, page); in generic_file_buffered_read()
2416 if (iocb->ki_flags & IOCB_WAITQ) { in generic_file_buffered_read()
2421 error = lock_page_async(page, iocb->ki_waitq); in generic_file_buffered_read()
2429 if (page->mapping == NULL) { in generic_file_buffered_read()
2439 error = -EIO; in generic_file_buffered_read()
2448 /* UHHUH! A synchronous read error occurred. Report it */ in generic_file_buffered_read()
2459 error = -ENOMEM; in generic_file_buffered_read()
2466 if (error == -EEXIST) { in generic_file_buffered_read()
2476 error = -EAGAIN; in generic_file_buffered_read()
2478 ra->prev_pos = prev_index; in generic_file_buffered_read()
2479 ra->prev_pos <<= PAGE_SHIFT; in generic_file_buffered_read()
2480 ra->prev_pos |= prev_offset; in generic_file_buffered_read()
2489 * generic_file_read_iter - generic filesystem read routine
2491 * @iter: destination for the data read
2496 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2497 * be returned when no data can be read without waiting for I/O requests
2500 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2501 * requests shall be made for the read or for readahead. When no data
2502 * can be read, -EAGAIN shall be returned. When readahead would be
2503 * triggered, a partial, possibly empty read shall be returned.
2507 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
2518 if (iocb->ki_flags & IOCB_DIRECT) { in generic_file_read_iter()
2519 struct file *file = iocb->ki_filp; in generic_file_read_iter()
2520 struct address_space *mapping = file->f_mapping; in generic_file_read_iter()
2521 struct inode *inode = mapping->host; in generic_file_read_iter()
2525 if (iocb->ki_flags & IOCB_NOWAIT) { in generic_file_read_iter()
2526 if (filemap_range_has_page(mapping, iocb->ki_pos, in generic_file_read_iter()
2527 iocb->ki_pos + count - 1)) in generic_file_read_iter()
2528 return -EAGAIN; in generic_file_read_iter()
2531 iocb->ki_pos, in generic_file_read_iter()
2532 iocb->ki_pos + count - 1); in generic_file_read_iter()
2539 retval = mapping->a_ops->direct_IO(iocb, iter); in generic_file_read_iter()
2541 iocb->ki_pos += retval; in generic_file_read_iter()
2542 count -= retval; in generic_file_read_iter()
2544 iov_iter_revert(iter, count - iov_iter_count(iter)); in generic_file_read_iter()
2547 * Btrfs can have a short DIO read if we encounter in generic_file_read_iter()
2549 * we've already read everything we wanted to, or if in generic_file_read_iter()
2550 * there was a short read because we hit EOF, go ahead in generic_file_read_iter()
2552 * the rest of the read. Buffered reads will not work for in generic_file_read_iter()
2555 if (retval < 0 || !count || iocb->ki_pos >= size || in generic_file_read_iter()
2569 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
2570 * @vmf - the vm_fault for this fault.
2571 * @page - the page to lock.
2572 * @fpin - the pointer to the file we may pin (or is already pinned).
2590 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) in lock_page_maybe_drop_mmap()
2594 if (vmf->flags & FAULT_FLAG_KILLABLE) { in lock_page_maybe_drop_mmap()
2603 mmap_read_unlock(vmf->vma->vm_mm); in lock_page_maybe_drop_mmap()
2621 struct file *file = vmf->vma->vm_file; in do_sync_mmap_readahead()
2622 struct file_ra_state *ra = &file->f_ra; in do_sync_mmap_readahead()
2623 struct address_space *mapping = file->f_mapping; in do_sync_mmap_readahead()
2624 DEFINE_READAHEAD(ractl, file, mapping, vmf->pgoff); in do_sync_mmap_readahead()
2628 /* If we don't want any read-ahead, don't bother */ in do_sync_mmap_readahead()
2629 if (vmf->vma->vm_flags & VM_RAND_READ) in do_sync_mmap_readahead()
2631 if (!ra->ra_pages) in do_sync_mmap_readahead()
2634 if (vmf->vma->vm_flags & VM_SEQ_READ) { in do_sync_mmap_readahead()
2636 page_cache_sync_ra(&ractl, ra, ra->ra_pages); in do_sync_mmap_readahead()
2641 mmap_miss = READ_ONCE(ra->mmap_miss); in do_sync_mmap_readahead()
2643 WRITE_ONCE(ra->mmap_miss, ++mmap_miss); in do_sync_mmap_readahead()
2647 * stop bothering with read-ahead. It will only hurt. in do_sync_mmap_readahead()
2653 * mmap read-around in do_sync_mmap_readahead()
2656 ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2); in do_sync_mmap_readahead()
2657 ra->size = ra->ra_pages; in do_sync_mmap_readahead()
2658 ra->async_size = ra->ra_pages / 4; in do_sync_mmap_readahead()
2659 ractl._index = ra->start; in do_sync_mmap_readahead()
2660 do_page_cache_ra(&ractl, ra->size, ra->async_size); in do_sync_mmap_readahead()
2672 struct file *file = vmf->vma->vm_file; in do_async_mmap_readahead()
2673 struct file_ra_state *ra = &file->f_ra; in do_async_mmap_readahead()
2674 struct address_space *mapping = file->f_mapping; in do_async_mmap_readahead()
2677 pgoff_t offset = vmf->pgoff; in do_async_mmap_readahead()
2679 /* If we don't want any read-ahead, don't bother */ in do_async_mmap_readahead()
2680 if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages) in do_async_mmap_readahead()
2682 mmap_miss = READ_ONCE(ra->mmap_miss); in do_async_mmap_readahead()
2684 WRITE_ONCE(ra->mmap_miss, --mmap_miss); in do_async_mmap_readahead()
2688 page, offset, ra->ra_pages); in do_async_mmap_readahead()
2694 * filemap_fault - read in file data for page fault handling
2698 * mapped memory region to read in file data during a page fault.
2704 * vma->vm_mm->mmap_lock must be held on entry.
2714 * Return: bitwise-OR of %VM_FAULT_ codes.
2719 struct file *file = vmf->vma->vm_file; in filemap_fault()
2721 struct address_space *mapping = file->f_mapping; in filemap_fault()
2722 struct file_ra_state *ra = &file->f_ra; in filemap_fault()
2723 struct inode *inode = mapping->host; in filemap_fault()
2724 pgoff_t offset = vmf->pgoff; in filemap_fault()
2737 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) { in filemap_fault()
2746 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); in filemap_fault()
2752 vmf->gfp_mask); in filemap_fault()
2764 if (unlikely(compound_head(page)->mapping != mapping)) { in filemap_fault()
2773 * that it's up-to-date. If not, it is going to be due to an error. in filemap_fault()
2780 * time to return to the upper layer and have it re-find the vma and in filemap_fault()
2789 * Found the page and have a reference on it. in filemap_fault()
2799 vmf->page = page; in filemap_fault()
2804 * Umm, take care of errors if the page isn't up-to-date. in filemap_fault()
2805 * Try to re-read it _once_. We do this synchronously, in filemap_fault()
2811 error = mapping->a_ops->readpage(file, page); in filemap_fault()
2815 error = -EIO; in filemap_fault()
2830 * re-find the vma and come back and find our hopefully still populated in filemap_fault()
2844 struct file *file = vmf->vma->vm_file; in filemap_map_pages()
2845 struct address_space *mapping = file->f_mapping; in filemap_map_pages()
2848 XA_STATE(xas, &mapping->i_pages, start_pgoff); in filemap_map_pages()
2850 unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss); in filemap_map_pages()
2880 if (head->mapping != mapping || !PageUptodate(head)) in filemap_map_pages()
2883 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); in filemap_map_pages()
2888 mmap_miss--; in filemap_map_pages()
2890 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT; in filemap_map_pages()
2891 if (vmf->pte) in filemap_map_pages()
2892 vmf->pte += xas.xa_index - last_pgoff; in filemap_map_pages()
2904 if (pmd_trans_huge(*vmf->pmd)) in filemap_map_pages()
2908 WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss); in filemap_map_pages()
2914 struct page *page = vmf->page; in filemap_page_mkwrite()
2915 struct inode *inode = file_inode(vmf->vma->vm_file); in filemap_page_mkwrite()
2918 sb_start_pagefault(inode->i_sb); in filemap_page_mkwrite()
2919 file_update_time(vmf->vma->vm_file); in filemap_page_mkwrite()
2921 if (page->mapping != inode->i_mapping) { in filemap_page_mkwrite()
2934 sb_end_pagefault(inode->i_sb); in filemap_page_mkwrite()
2948 struct address_space *mapping = file->f_mapping; in generic_file_mmap()
2950 if (!mapping->a_ops->readpage) in generic_file_mmap()
2951 return -ENOEXEC; in generic_file_mmap()
2953 vma->vm_ops = &generic_file_vm_ops; in generic_file_mmap()
2958 * This is for filesystems which do not implement ->writepage.
2962 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) in generic_file_readonly_mmap()
2963 return -EINVAL; in generic_file_readonly_mmap()
2973 return -ENOSYS; in generic_file_mmap()
2977 return -ENOSYS; in generic_file_readonly_mmap()
2991 page = ERR_PTR(-EIO); in wait_on_page_read()
3010 return ERR_PTR(-ENOMEM); in do_read_cache_page()
3014 if (err == -EEXIST) in do_read_cache_page()
3024 err = mapping->a_ops->readpage(data, page); in do_read_cache_page()
3042 * case b: Read/write error clearing the page uptodate status in do_read_cache_page()
3047 * There is no need to serialise on the page lock here as the page in do_read_cache_page()
3053 * be valid after IO completes as it's a read vs truncate race. The in do_read_cache_page()
3054 * operation must restart if the page is not uptodate on unlock but in do_read_cache_page()
3055 * otherwise serialising on page lock to stabilise the mapping gives in do_read_cache_page()
3060 * is valid on unlock but otherwise the data is valid and there is in do_read_cache_page()
3064 * wait on the page to be unlocked and check if it's up to date and in do_read_cache_page()
3068 * wait on the same page for IO to complete. in do_read_cache_page()
3078 if (!page->mapping) { in do_read_cache_page()
3093 * Clear page error before actual read, PG_error will be in do_read_cache_page()
3094 * set again if read page fails. in do_read_cache_page()
3105 * read_cache_page - read into page cache, fill it if needed
3108 * @filler: function to perform the read
3111 * Read into the page cache. If a page already exists, and PageUptodate() is
3112 * not set, try to fill the page and wait for it to become unlocked.
3114 * If the page does not get brought uptodate, return -EIO.
3116 * Return: up to date page on success, ERR_PTR() on failure.
3129 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
3137 * If the page does not get brought uptodate, return -EIO.
3139 * Return: up to date page on success, ERR_PTR() on failure.
3153 const struct address_space_operations *aops = mapping->a_ops; in pagecache_write_begin()
3155 return aops->write_begin(file, mapping, pos, len, flags, in pagecache_write_begin()
3164 const struct address_space_operations *aops = mapping->a_ops; in pagecache_write_end()
3166 return aops->write_end(file, mapping, pos, len, copied, page, fsdata); in pagecache_write_end()
3180 errseq_set(&inode->i_mapping->wb_err, -EIO); in dio_warn_stale_pagecache()
3185 …pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision… in dio_warn_stale_pagecache()
3186 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid, in dio_warn_stale_pagecache()
3187 current->comm); in dio_warn_stale_pagecache()
3194 struct file *file = iocb->ki_filp; in generic_file_direct_write()
3195 struct address_space *mapping = file->f_mapping; in generic_file_direct_write()
3196 struct inode *inode = mapping->host; in generic_file_direct_write()
3197 loff_t pos = iocb->ki_pos; in generic_file_direct_write()
3203 end = (pos + write_len - 1) >> PAGE_SHIFT; in generic_file_direct_write()
3205 if (iocb->ki_flags & IOCB_NOWAIT) { in generic_file_direct_write()
3207 if (filemap_range_has_page(inode->i_mapping, pos, in generic_file_direct_write()
3208 pos + write_len - 1)) in generic_file_direct_write()
3209 return -EAGAIN; in generic_file_direct_write()
3212 pos + write_len - 1); in generic_file_direct_write()
3221 * without clobbering -EIOCBQUEUED from ->direct_IO(). in generic_file_direct_write()
3230 if (written == -EBUSY) in generic_file_direct_write()
3235 written = mapping->a_ops->direct_IO(iocb, from); in generic_file_direct_write()
3239 * cached by non-direct readahead, or faulted in by get_user_pages() in generic_file_direct_write()
3254 if (written > 0 && mapping->nrpages && in generic_file_direct_write()
3260 write_len -= written; in generic_file_direct_write()
3261 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { in generic_file_direct_write()
3265 iocb->ki_pos = pos; in generic_file_direct_write()
3267 iov_iter_revert(from, write_len - iov_iter_count(from)); in generic_file_direct_write()
3298 struct address_space *mapping = file->f_mapping; in generic_perform_write()
3299 const struct address_space_operations *a_ops = mapping->a_ops; in generic_perform_write()
3311 offset = (pos & (PAGE_SIZE - 1)); in generic_perform_write()
3312 bytes = min_t(unsigned long, PAGE_SIZE - offset, in generic_perform_write()
3318 * Otherwise there's a nasty deadlock on copying from the in generic_perform_write()
3320 * up-to-date. in generic_perform_write()
3327 status = -EFAULT; in generic_perform_write()
3332 status = -EINTR; in generic_perform_write()
3336 status = a_ops->write_begin(file, mapping, pos, bytes, flags, in generic_perform_write()
3347 status = a_ops->write_end(file, mapping, pos, bytes, copied, in generic_perform_write()
3365 bytes = min_t(unsigned long, PAGE_SIZE - offset, in generic_perform_write()
3380 * __generic_file_write_iter - write data to a file
3386 * modification times and calls proper subroutines depending on whether we
3389 * It expects i_mutex to be grabbed unless we work on a block device or similar
3402 struct file *file = iocb->ki_filp; in __generic_file_write_iter()
3403 struct address_space * mapping = file->f_mapping; in __generic_file_write_iter()
3404 struct inode *inode = mapping->host; in __generic_file_write_iter()
3410 current->backing_dev_info = inode_to_bdi(inode); in __generic_file_write_iter()
3419 if (iocb->ki_flags & IOCB_DIRECT) { in __generic_file_write_iter()
3428 * page-cache pages correctly). in __generic_file_write_iter()
3433 status = generic_perform_write(file, from, pos = iocb->ki_pos); in __generic_file_write_iter()
3437 * direct-written, or the error code if that was zero. Note in __generic_file_write_iter()
3438 * that this differs from normal direct-io semantics, which in __generic_file_write_iter()
3439 * will return -EFOO even if some bytes were written. in __generic_file_write_iter()
3450 endbyte = pos + status - 1; in __generic_file_write_iter()
3453 iocb->ki_pos = endbyte + 1; in __generic_file_write_iter()
3461 * the number of bytes which were direct-written in __generic_file_write_iter()
3465 written = generic_perform_write(file, from, iocb->ki_pos); in __generic_file_write_iter()
3467 iocb->ki_pos += written; in __generic_file_write_iter()
3470 current->backing_dev_info = NULL; in __generic_file_write_iter()
3476 * generic_file_write_iter - write data to a file
3490 struct file *file = iocb->ki_filp; in generic_file_write_iter()
3491 struct inode *inode = file->f_mapping->host; in generic_file_write_iter()
3507 * try_to_release_page() - release old fs-specific metadata on a page
3513 * (presumably at page->private).
3515 * This may also be called if PG_fscache is set on a page, indicating that the
3525 struct address_space * const mapping = page->mapping; in try_to_release_page()
3531 if (mapping && mapping->a_ops->releasepage) in try_to_release_page()
3532 return mapping->a_ops->releasepage(page, gfp_mask); in try_to_release_page()