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
30 #include <linux/error-injection.h>
33 #include <linux/backing-dev.h>
70 * finished 'unifying' the page and buffer cache and SMP-threaded the
71 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
73 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
79 * ->i_mmap_rwsem (truncate_pagecache)
80 * ->private_lock (__free_pte->block_dirty_folio)
81 * ->swap_lock (exclusive_swap_page, others)
82 * ->i_pages lock
84 * ->i_rwsem
85 * ->invalidate_lock (acquired by fs in truncate path)
86 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
88 * ->mmap_lock
89 * ->i_mmap_rwsem
90 * ->page_table_lock or pte_lock (various, mainly in memory.c)
91 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
93 * ->mmap_lock
94 * ->invalidate_lock (filemap_fault)
95 * ->lock_page (filemap_fault, access_process_vm)
97 * ->i_rwsem (generic_perform_write)
98 * ->mmap_lock (fault_in_readable->do_page_fault)
100 * bdi->wb.list_lock
101 * sb_lock (fs/fs-writeback.c)
102 * ->i_pages lock (__sync_single_inode)
104 * ->i_mmap_rwsem
105 * ->anon_vma.lock (vma_merge)
107 * ->anon_vma.lock
108 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
110 * ->page_table_lock or pte_lock
111 * ->swap_lock (try_to_unmap_one)
112 * ->private_lock (try_to_unmap_one)
113 * ->i_pages lock (try_to_unmap_one)
114 * ->lruvec->lru_lock (follow_page->mark_page_accessed)
115 * ->lruvec->lru_lock (check_pte_range->isolate_lru_page)
116 * ->private_lock (page_remove_rmap->set_page_dirty)
117 * ->i_pages lock (page_remove_rmap->set_page_dirty)
118 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
119 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
120 * ->memcg->move_lock (page_remove_rmap->folio_memcg_lock)
121 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
122 * ->inode->i_lock (zap_pte_range->set_page_dirty)
123 * ->private_lock (zap_pte_range->block_dirty_folio)
129 XA_STATE(xas, &mapping->i_pages, folio->index); in page_cache_delete()
136 xas_set_order(&xas, folio->index, folio_order(folio)); in page_cache_delete()
145 folio->mapping = NULL; in page_cache_delete()
146 /* Leave page->index set: truncation lookup relies upon it */ in page_cache_delete()
147 mapping->nrpages -= nr; in page_cache_delete()
158 current->comm, folio_pfn(folio)); in filemap_unaccount_folio()
159 dump_page(&folio->page, "still mapped when deleted"); in filemap_unaccount_folio()
164 int mapcount = page_mapcount(&folio->page); in filemap_unaccount_folio()
173 page_mapcount_reset(&folio->page); in filemap_unaccount_folio()
185 __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr); in filemap_unaccount_folio()
187 __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr); in filemap_unaccount_folio()
189 __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, -nr); in filemap_unaccount_folio()
191 __lruvec_stat_mod_folio(folio, NR_FILE_THPS, -nr); in filemap_unaccount_folio()
198 * unwritten data - on ordinary filesystems. in filemap_unaccount_folio()
200 * But it's harmless on in-memory filesystems like tmpfs; and can in filemap_unaccount_folio()
211 folio_account_cleaned(folio, inode_to_wb(mapping->host)); in filemap_unaccount_folio()
216 * sure the page is locked and that nobody else uses it - or that usage
221 struct address_space *mapping = folio->mapping; in __filemap_remove_folio()
233 free_folio = mapping->a_ops->free_folio; in filemap_free_folio()
243 * filemap_remove_folio - Remove folio from page cache.
246 * This must be called only on folios that are locked and have been
248 * the free list because the caller has a reference on the page.
252 struct address_space *mapping = folio->mapping; in filemap_remove_folio()
255 spin_lock(&mapping->host->i_lock); in filemap_remove_folio()
256 xa_lock_irq(&mapping->i_pages); in filemap_remove_folio()
258 xa_unlock_irq(&mapping->i_pages); in filemap_remove_folio()
260 inode_add_lru(mapping->host); in filemap_remove_folio()
261 spin_unlock(&mapping->host->i_lock); in filemap_remove_folio()
267 * page_cache_delete_batch - delete several folios from page cache
271 * The function walks over mapping->i_pages and removes folios passed in
282 XA_STATE(xas, &mapping->i_pages, fbatch->folios[0]->index); in page_cache_delete_batch()
302 if (folio != fbatch->folios[i]) { in page_cache_delete_batch()
303 VM_BUG_ON_FOLIO(folio->index > in page_cache_delete_batch()
304 fbatch->folios[i]->index, folio); in page_cache_delete_batch()
310 folio->mapping = NULL; in page_cache_delete_batch()
311 /* Leave folio->index set: truncation lookup relies on it */ in page_cache_delete_batch()
317 mapping->nrpages -= total_pages; in page_cache_delete_batch()
328 spin_lock(&mapping->host->i_lock); in delete_from_page_cache_batch()
329 xa_lock_irq(&mapping->i_pages); in delete_from_page_cache_batch()
331 struct folio *folio = fbatch->folios[i]; in delete_from_page_cache_batch()
337 xa_unlock_irq(&mapping->i_pages); in delete_from_page_cache_batch()
339 inode_add_lru(mapping->host); in delete_from_page_cache_batch()
340 spin_unlock(&mapping->host->i_lock); in delete_from_page_cache_batch()
343 filemap_free_folio(mapping, fbatch->folios[i]); in delete_from_page_cache_batch()
350 if (test_bit(AS_ENOSPC, &mapping->flags) && in filemap_check_errors()
351 test_and_clear_bit(AS_ENOSPC, &mapping->flags)) in filemap_check_errors()
352 ret = -ENOSPC; in filemap_check_errors()
353 if (test_bit(AS_EIO, &mapping->flags) && in filemap_check_errors()
354 test_and_clear_bit(AS_EIO, &mapping->flags)) in filemap_check_errors()
355 ret = -EIO; in filemap_check_errors()
363 if (test_bit(AS_EIO, &mapping->flags)) in filemap_check_and_keep_errors()
364 return -EIO; in filemap_check_and_keep_errors()
365 if (test_bit(AS_ENOSPC, &mapping->flags)) in filemap_check_and_keep_errors()
366 return -ENOSPC; in filemap_check_and_keep_errors()
371 * filemap_fdatawrite_wbc - start writeback on mapping dirty pages in range
375 * Call writepages on the mapping using the provided wbc to control the
378 * Return: %0 on success, negative error code otherwise.
389 wbc_attach_fdatawrite_inode(wbc, mapping->host); in filemap_fdatawrite_wbc()
397 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
411 * Return: %0 on success, negative error code otherwise.
446 * filemap_flush - mostly a non-blocking flush
449 * This is a mostly non-blocking flush. Not suitable for data-integrity
450 * purposes - I/O may not be started against all dirty pages.
452 * Return: %0 on success, negative error code otherwise.
461 * filemap_range_has_page - check if a page exists in range.
476 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); in filemap_range_has_page()
534 * filemap_fdatawait_range - wait for writeback to complete
535 * @mapping: address space structure to wait for
539 * Walk the list of under-writeback pages of the given address space
540 * in the given range and wait for all of them. Check error status of
558 * filemap_fdatawait_range_keep_errors - wait for writeback to complete
559 * @mapping: address space structure to wait for
563 * Walk the list of under-writeback pages of the given address space in the
564 * given range and wait for all of them. Unlike filemap_fdatawait_range(),
568 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
580 * file_fdatawait_range - wait for writeback to complete
581 * @file: file pointing to address space structure to wait for
585 * Walk the list of under-writeback pages of the address space that file
586 * refers to, in the given range and wait for all of them. Check error
587 * status of the address space vs. the file->f_wb_err cursor and return it.
593 * Return: error status of the address space vs. the file->f_wb_err cursor.
597 struct address_space *mapping = file->f_mapping; in file_fdatawait_range()
605 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
606 * @mapping: address space structure to wait for
608 * Walk the list of under-writeback pages of the given address space
609 * and wait for all of them. Unlike filemap_fdatawait(), this function
613 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
628 return mapping->nrpages; in mapping_needs_writeback()
634 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); in filemap_range_has_writeback()
657 * filemap_write_and_wait_range - write out & wait on a file range
662 * Write out and wait upon file offsets lstart->lend, inclusive.
665 * that this function can be used to write to the very end-of-file (end = -1).
682 * written partially (e.g. -ENOSPC), so we wait for it. in filemap_write_and_wait_range()
683 * But the -EIO is special case, it may indicate the worst in filemap_write_and_wait_range()
686 if (err != -EIO) in filemap_write_and_wait_range()
698 errseq_t eseq = errseq_set(&mapping->wb_err, err); in __filemap_set_wb_err()
705 * file_check_and_advance_wb_err - report wb error (if any) that was previously
707 * @file: struct file on which the error is being reported
722 * While we handle mapping->wb_err with atomic operations, the f_wb_err
726 * Return: %0 on success, negative error code otherwise.
731 errseq_t old = READ_ONCE(file->f_wb_err); in file_check_and_advance_wb_err()
732 struct address_space *mapping = file->f_mapping; in file_check_and_advance_wb_err()
735 if (errseq_check(&mapping->wb_err, old)) { in file_check_and_advance_wb_err()
737 spin_lock(&file->f_lock); in file_check_and_advance_wb_err()
738 old = file->f_wb_err; in file_check_and_advance_wb_err()
739 err = errseq_check_and_advance(&mapping->wb_err, in file_check_and_advance_wb_err()
740 &file->f_wb_err); in file_check_and_advance_wb_err()
742 spin_unlock(&file->f_lock); in file_check_and_advance_wb_err()
748 * that the legacy code would have had on these flags. in file_check_and_advance_wb_err()
750 clear_bit(AS_EIO, &mapping->flags); in file_check_and_advance_wb_err()
751 clear_bit(AS_ENOSPC, &mapping->flags); in file_check_and_advance_wb_err()
757 * file_write_and_wait_range - write out & wait on a file range
762 * Write out and wait upon file offsets lstart->lend, inclusive.
765 * that this function can be used to write to the very end-of-file (end = -1).
767 * After writing out and waiting on the data, we check and advance the
770 * Return: %0 on success, negative error code otherwise.
775 struct address_space *mapping = file->f_mapping; in file_write_and_wait_range()
784 if (err != -EIO) in file_write_and_wait_range()
795 * replace_page_cache_folio - replace a pagecache folio with a new one
799 * This function replaces a folio in the pagecache with a new one. On
809 struct address_space *mapping = old->mapping; in replace_page_cache_folio()
810 void (*free_folio)(struct folio *) = mapping->a_ops->free_folio; in replace_page_cache_folio()
811 pgoff_t offset = old->index; in replace_page_cache_folio()
812 XA_STATE(xas, &mapping->i_pages, offset); in replace_page_cache_folio()
816 VM_BUG_ON_FOLIO(new->mapping, new); in replace_page_cache_folio()
819 new->mapping = mapping; in replace_page_cache_folio()
820 new->index = offset; in replace_page_cache_folio()
827 old->mapping = NULL; in replace_page_cache_folio()
847 XA_STATE(xas, &mapping->i_pages, index); in __filemap_add_folio()
860 VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio); in __filemap_add_folio()
870 folio->mapping = mapping; in __filemap_add_folio()
871 folio->index = xas.xa_index; in __filemap_add_folio()
874 int order = -1, split_order = 0; in __filemap_add_folio()
881 xas_set_err(&xas, -EEXIST); in __filemap_add_folio()
888 if (order == -1) in __filemap_add_folio()
892 /* entry may have changed before we re-acquire the lock */ in __filemap_add_folio()
917 mapping->nrpages += nr; in __filemap_add_folio()
953 folio->mapping = NULL; in __filemap_add_folio()
954 /* Leave page->index set: truncation relies upon it */ in __filemap_add_folio()
1010 * filemap_invalidate_lock_two - lock invalidate_lock for two mappings
1023 down_write(&mapping1->invalidate_lock); in filemap_invalidate_lock_two()
1025 down_write_nested(&mapping2->invalidate_lock, 1); in filemap_invalidate_lock_two()
1030 * filemap_invalidate_unlock_two - unlock invalidate_lock for two mappings
1041 up_write(&mapping1->invalidate_lock); in filemap_invalidate_unlock_two()
1043 up_write(&mapping2->invalidate_lock); in filemap_invalidate_unlock_two()
1048 * In order to wait for pages to become available there must be
1051 * waiters on the same queue and wake all when any of the pages
1077 * The page wait code treats the "wait->flags" somewhat unusually, because
1087 * and remove it from the wait queue.
1095 * WQ_FLAG_WOKEN bit, wake it up, and remove it from the wait queue.
1097 * This is the traditional exclusive wait.
1103 * cannot be taken, we stop walking the wait queue without waking
1110 static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg) in wake_page_function() argument
1115 = container_of(wait, struct wait_page_queue, wait); in wake_page_function()
1121 * If it's a lock handoff wait, we get the bit for it, and in wake_page_function()
1124 flags = wait->flags; in wake_page_function()
1126 if (test_bit(key->bit_nr, &key->folio->flags)) in wake_page_function()
1127 return -1; in wake_page_function()
1129 if (test_and_set_bit(key->bit_nr, &key->folio->flags)) in wake_page_function()
1130 return -1; in wake_page_function()
1136 * We are holding the wait-queue lock, but the waiter that in wake_page_function()
1141 * afterwards to avoid any races. This store-release pairs in wake_page_function()
1142 * with the load-acquire in folio_wait_bit_common(). in wake_page_function()
1144 smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN); in wake_page_function()
1145 wake_up_state(wait->private, mode); in wake_page_function()
1149 * and we can unconditionally remove the wait entry. in wake_page_function()
1153 * After this list_del_init(&wait->entry) the wait entry in wake_page_function()
1154 * might be de-allocated and the process might even have in wake_page_function()
1157 list_del_init_careful(&wait->entry); in wake_page_function()
1177 spin_lock_irqsave(&q->lock, flags); in folio_wake_bit()
1185 * from wait queue in folio_wake_bit()
1187 spin_unlock_irqrestore(&q->lock, flags); in folio_wake_bit()
1189 spin_lock_irqsave(&q->lock, flags); in folio_wake_bit()
1195 * waiters, but the hashed waitqueue has waiters for other pages on it. in folio_wake_bit()
1198 * Note that, depending on the page pool (buddy, hugetlb, ZONE_DEVICE, in folio_wake_bit()
1205 spin_unlock_irqrestore(&q->lock, flags); in folio_wake_bit()
1220 * __folio_lock() waiting on then setting PG_locked.
1223 * folio_wait_writeback() waiting on PG_writeback.
1225 DROP, /* Drop ref to page before wait, no check when woken,
1226 * like folio_put_wait_locked() on PG_locked.
1235 struct wait_queue_entry *wait) in folio_trylock_flag() argument
1237 if (wait->flags & WQ_FLAG_EXCLUSIVE) { in folio_trylock_flag()
1238 if (test_and_set_bit(bit_nr, &folio->flags)) in folio_trylock_flag()
1240 } else if (test_bit(bit_nr, &folio->flags)) in folio_trylock_flag()
1243 wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE; in folio_trylock_flag()
1256 wait_queue_entry_t *wait = &wait_page.wait; in folio_wait_bit_common() local
1268 init_wait(wait); in folio_wait_bit_common()
1269 wait->func = wake_page_function; in folio_wait_bit_common()
1274 wait->flags = 0; in folio_wait_bit_common()
1276 wait->flags = WQ_FLAG_EXCLUSIVE; in folio_wait_bit_common()
1277 if (--unfairness < 0) in folio_wait_bit_common()
1278 wait->flags |= WQ_FLAG_CUSTOM; in folio_wait_bit_common()
1289 * page queue), and add ourselves to the wait in folio_wait_bit_common()
1295 spin_lock_irq(&q->lock); in folio_wait_bit_common()
1297 if (!folio_trylock_flag(folio, bit_nr, wait)) in folio_wait_bit_common()
1298 __add_wait_queue_entry_tail(q, wait); in folio_wait_bit_common()
1299 spin_unlock_irq(&q->lock); in folio_wait_bit_common()
1302 * From now on, all the logic will be based on in folio_wait_bit_common()
1315 * be very careful with the 'wait->flags', because in folio_wait_bit_common()
1324 flags = smp_load_acquire(&wait->flags); in folio_wait_bit_common()
1333 /* If we were non-exclusive, we're done */ in folio_wait_bit_common()
1350 wait->flags |= WQ_FLAG_DONE; in folio_wait_bit_common()
1356 * waiter from the wait-queues, but the folio waiters bit will remain in folio_wait_bit_common()
1360 finish_wait(q, wait); in folio_wait_bit_common()
1368 * NOTE! The wait->flags weren't stable until we've done the in folio_wait_bit_common()
1375 * return value based on that state without races. in folio_wait_bit_common()
1377 * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive in folio_wait_bit_common()
1381 return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR; in folio_wait_bit_common()
1383 return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR; in folio_wait_bit_common()
1388 * migration_entry_wait_on_locked - Wait for a migration entry to be removed
1392 * Wait for a migration entry referencing the given page to be removed. This is
1394 * this can be called without taking a reference on the page. Instead this
1407 wait_queue_entry_t *wait = &wait_page.wait; in migration_entry_wait_on_locked() local
1421 init_wait(wait); in migration_entry_wait_on_locked()
1422 wait->func = wake_page_function; in migration_entry_wait_on_locked()
1425 wait->flags = 0; in migration_entry_wait_on_locked()
1427 spin_lock_irq(&q->lock); in migration_entry_wait_on_locked()
1429 if (!folio_trylock_flag(folio, PG_locked, wait)) in migration_entry_wait_on_locked()
1430 __add_wait_queue_entry_tail(q, wait); in migration_entry_wait_on_locked()
1431 spin_unlock_irq(&q->lock); in migration_entry_wait_on_locked()
1446 flags = smp_load_acquire(&wait->flags); in migration_entry_wait_on_locked()
1457 finish_wait(q, wait); in migration_entry_wait_on_locked()
1479 * folio_put_wait_locked - Drop a reference and wait for it to be unlocked
1480 * @folio: The folio to wait for.
1483 * The caller should hold a reference on @folio. They expect the page to
1489 * Return: 0 if the folio was unlocked or -EINTR if interrupted by a signal.
1497 * folio_add_wait_queue - Add an arbitrary waiter to a folio's wait queue
1498 * @folio: Folio defining the wait queue of interest
1501 * Add an arbitrary @waiter to the wait queue for the nominated @folio.
1508 spin_lock_irqsave(&q->lock, flags); in folio_add_wait_queue()
1511 spin_unlock_irqrestore(&q->lock, flags); in folio_add_wait_queue()
1520 * On x86 (and on many other architectures), we can clear PG_lock and
1525 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1539 * folio_unlock - Unlock a locked folio.
1542 * Unlocks the folio and wakes up any thread sleeping on the page lock.
1559 * folio_end_private_2 - Clear PG_private_2 and wake any waiters.
1562 * Clear the PG_private_2 bit on a folio and wake up any sleepers waiting for
1579 * folio_wait_private_2 - Wait for PG_private_2 to be cleared on a folio.
1580 * @folio: The folio to wait on.
1582 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio.
1592 * folio_wait_private_2_killable - Wait for PG_private_2 to be cleared on a folio.
1593 * @folio: The folio to wait on.
1595 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio or until a
1599 * - 0 if successful.
1600 * - -EINTR if a fatal signal was encountered.
1617 * folio_end_writeback - End writeback against a folio.
1636 * on truncation to wait for the clearing of PG_writeback. in folio_end_writeback()
1652 * __folio_lock - Get a lock on the folio, assuming we need to sleep to get it.
1669 static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) in __folio_lock_async() argument
1674 wait->folio = folio; in __folio_lock_async()
1675 wait->bit_nr = PG_locked; in __folio_lock_async()
1677 spin_lock_irq(&q->lock); in __folio_lock_async()
1678 __add_wait_queue_entry_tail(q, &wait->wait); in __folio_lock_async()
1682 * If we were successful now, we know we're still on the in __folio_lock_async()
1688 __remove_wait_queue(q, &wait->wait); in __folio_lock_async()
1690 ret = -EIOCBQUEUED; in __folio_lock_async()
1691 spin_unlock_irq(&q->lock); in __folio_lock_async()
1697 * 0 - folio is locked.
1698 * non-zero - folio is not locked.
1699 * mmap_lock or per-VMA lock has been released (mmap_read_unlock() or
1704 * with the folio locked and the mmap_lock/per-VMA lock is left unperturbed.
1708 unsigned int flags = vmf->flags; in __folio_lock_or_retry()
1712 * CAUTION! In this case, mmap_lock/per-VMA lock is not in __folio_lock_or_retry()
1741 * page_cache_next_miss() - Find the next gap in the page cache.
1746 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1756 * range specified (in which case 'return - index >= max_scan' will be true).
1757 * In the rare case of index wrap-around, 0 will be returned.
1762 XA_STATE(xas, &mapping->i_pages, index); in page_cache_next_miss()
1764 while (max_scan--) { in page_cache_next_miss()
1777 * page_cache_prev_miss() - Find the previous gap in the page cache.
1782 * Search the range [max(index - max_scan + 1, 0), index] for the
1792 * range specified (in which case 'index - return >= max_scan' will be true).
1793 * In the rare case of wrap-around, ULONG_MAX will be returned.
1798 XA_STATE(xas, &mapping->i_pages, index); in page_cache_prev_miss()
1800 while (max_scan--) { in page_cache_prev_miss()
1814 * On the lookup side:
1819 * On the removal side:
1828 * last refcount on the page, any page allocation must be freeable by
1833 * filemap_get_entry - Get a page cache entry.
1846 XA_STATE(xas, &mapping->i_pages, index); in filemap_get_entry()
1876 * __filemap_get_folio - Find and get a reference to a folio.
1907 return ERR_PTR(-EAGAIN); in __filemap_get_folio()
1914 if (unlikely(folio->mapping != mapping)) { in __filemap_get_folio()
1953 if (index & ((1UL << order) - 1)) in __filemap_get_folio()
1959 err = -ENOMEM; in __filemap_get_folio()
1975 } while (order-- > 0); in __filemap_get_folio()
1977 if (err == -EEXIST) in __filemap_get_folio()
1984 * Return -EAGAIN so that there caller retries in a in __filemap_get_folio()
1985 * blocking fashion instead of propagating -ENOMEM in __filemap_get_folio()
1988 if ((fgp_flags & FGP_NOWAIT) && err == -ENOMEM) in __filemap_get_folio()
1989 err = -EAGAIN; in __filemap_get_folio()
2001 return ERR_PTR(-ENOENT); in __filemap_get_folio()
2042 * find_get_entries - gang pagecache lookup
2051 * takes a reference on any actual folios it returns.
2054 * due to not-present entries or large folios.
2064 XA_STATE(xas, &mapping->i_pages, *start); in find_get_entries()
2069 indices[fbatch->nr] = xas.xa_index; in find_get_entries()
2077 int idx = folio_batch_count(fbatch) - 1; in find_get_entries()
2079 folio = fbatch->folios[idx]; in find_get_entries()
2088 * find_lock_entries - Find a batch of pagecache entries.
2102 * due to not-present entries, large folios, folios which could not be
2110 XA_STATE(xas, &mapping->i_pages, *start); in find_lock_entries()
2116 if (folio->index < *start) in find_lock_entries()
2118 if (folio_next_index(folio) - 1 > end) in find_lock_entries()
2122 if (folio->mapping != mapping || in find_lock_entries()
2128 indices[fbatch->nr] = xas.xa_index; in find_lock_entries()
2141 int idx = folio_batch_count(fbatch) - 1; in find_lock_entries()
2143 folio = fbatch->folios[idx]; in find_lock_entries()
2152 * filemap_get_folios - Get a batch of folios
2175 XA_STATE(xas, &mapping->i_pages, *start); in filemap_get_folios()
2188 *start = folio->index + nr; in filemap_get_folios()
2196 * breaks the iteration when there is a page at index -1 but that is in filemap_get_folios()
2199 if (end == (pgoff_t)-1) in filemap_get_folios()
2200 *start = (pgoff_t)-1; in filemap_get_folios()
2211 * filemap_get_folios_contig - Get a batch of contiguous folios
2228 XA_STATE(xas, &mapping->i_pages, *start); in filemap_get_folios_contig()
2256 *start = folio->index + nr; in filemap_get_folios_contig()
2259 xas_advance(&xas, folio_next_index(folio) - 1); in filemap_get_folios_contig()
2272 folio = fbatch->folios[nr - 1]; in filemap_get_folios_contig()
2274 *start = folio->index + 1; in filemap_get_folios_contig()
2285 * filemap_get_folios_tag - Get a batch of folios matching @tag
2300 XA_STATE(xas, &mapping->i_pages, *start); in filemap_get_folios_tag()
2317 *start = folio->index + nr; in filemap_get_folios_tag()
2324 * breaks the iteration when there is a page at index -1 but that is in filemap_get_folios_tag()
2327 if (end == (pgoff_t)-1) in filemap_get_folios_tag()
2328 *start = (pgoff_t)-1; in filemap_get_folios_tag()
2342 * ---R__________________________________________B__________
2345 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2346 * => failing the whole request => read(R) => read(R+1) =>
2347 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2348 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2349 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2355 ra->ra_pages /= 4; in shrink_readahead_size_eio()
2359 * filemap_get_read_batch - Get a batch of folios for read
2370 XA_STATE(xas, &mapping->i_pages, index); in filemap_get_read_batch()
2393 xas_advance(&xas, folio_next_index(folio) - 1); in filemap_get_read_batch()
2417 /* Start the actual read. The read will unlock the page. */ in filemap_read_folio()
2432 shrink_readahead_size_eio(&file->f_ra); in filemap_read_folio()
2433 return -EIO; in filemap_read_folio()
2445 if (!mapping->a_ops->is_partially_uptodate) in filemap_range_uptodate()
2447 if (mapping->host->i_blkbits >= folio_shift(folio)) in filemap_range_uptodate()
2451 count -= folio_pos(folio) - pos; in filemap_range_uptodate()
2454 pos -= folio_pos(folio); in filemap_range_uptodate()
2457 return mapping->a_ops->is_partially_uptodate(folio, pos, count); in filemap_range_uptodate()
2466 if (iocb->ki_flags & IOCB_NOWAIT) { in filemap_update_page()
2468 return -EAGAIN; in filemap_update_page()
2474 error = -EAGAIN; in filemap_update_page()
2475 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO)) in filemap_update_page()
2477 if (!(iocb->ki_flags & IOCB_WAITQ)) { in filemap_update_page()
2486 error = __folio_lock_async(folio, iocb->ki_waitq); in filemap_update_page()
2492 if (!folio->mapping) in filemap_update_page()
2496 if (filemap_range_uptodate(mapping, iocb->ki_pos, count, folio, in filemap_update_page()
2500 error = -EAGAIN; in filemap_update_page()
2501 if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ)) in filemap_update_page()
2504 error = filemap_read_folio(iocb->ki_filp, mapping->a_ops->read_folio, in filemap_update_page()
2525 return -ENOMEM; in filemap_create_folio()
2536 * pages or ->readahead() that need to hold invalidate_lock in filemap_create_folio()
2543 if (error == -EEXIST) in filemap_create_folio()
2548 error = filemap_read_folio(file, mapping->a_ops->read_folio, folio); in filemap_create_folio()
2565 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, folio->index); in filemap_readahead()
2567 if (iocb->ki_flags & IOCB_NOIO) in filemap_readahead()
2568 return -EAGAIN; in filemap_readahead()
2569 page_cache_async_ra(&ractl, folio, last_index - folio->index); in filemap_readahead()
2576 struct file *filp = iocb->ki_filp; in filemap_get_pages()
2577 struct address_space *mapping = filp->f_mapping; in filemap_get_pages()
2578 struct file_ra_state *ra = &filp->f_ra; in filemap_get_pages()
2579 pgoff_t index = iocb->ki_pos >> PAGE_SHIFT; in filemap_get_pages()
2584 /* "last_index" is the index of the page beyond the end of the read */ in filemap_get_pages()
2585 last_index = DIV_ROUND_UP(iocb->ki_pos + count, PAGE_SIZE); in filemap_get_pages()
2588 return -EINTR; in filemap_get_pages()
2590 filemap_get_read_batch(mapping, index, last_index - 1, fbatch); in filemap_get_pages()
2592 if (iocb->ki_flags & IOCB_NOIO) in filemap_get_pages()
2593 return -EAGAIN; in filemap_get_pages()
2595 last_index - index); in filemap_get_pages()
2596 filemap_get_read_batch(mapping, index, last_index - 1, fbatch); in filemap_get_pages()
2599 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ)) in filemap_get_pages()
2600 return -EAGAIN; in filemap_get_pages()
2602 iocb->ki_pos >> PAGE_SHIFT, fbatch); in filemap_get_pages()
2608 folio = fbatch->folios[folio_batch_count(fbatch) - 1]; in filemap_get_pages()
2615 if ((iocb->ki_flags & IOCB_WAITQ) && in filemap_get_pages()
2617 iocb->ki_flags |= IOCB_NOWAIT; in filemap_get_pages()
2628 if (likely(--fbatch->nr)) in filemap_get_pages()
2643 * filemap_read - Read data from the page cache.
2644 * @iocb: The iocb to read.
2646 * @already_read: Number of bytes already read by the caller.
2651 * Return: Total number of bytes copied, including those already read by
2658 struct file *filp = iocb->ki_filp; in filemap_read()
2659 struct file_ra_state *ra = &filp->f_ra; in filemap_read()
2660 struct address_space *mapping = filp->f_mapping; in filemap_read()
2661 struct inode *inode = mapping->host; in filemap_read()
2666 loff_t last_pos = ra->prev_pos; in filemap_read()
2668 if (unlikely(iocb->ki_pos >= inode->i_sb->s_maxbytes)) in filemap_read()
2673 iov_iter_truncate(iter, inode->i_sb->s_maxbytes - iocb->ki_pos); in filemap_read()
2681 * can no longer safely return -EIOCBQUEUED. Hence mark in filemap_read()
2682 * an async read NOWAIT at that point. in filemap_read()
2684 if ((iocb->ki_flags & IOCB_WAITQ) && already_read) in filemap_read()
2685 iocb->ki_flags |= IOCB_NOWAIT; in filemap_read()
2687 if (unlikely(iocb->ki_pos >= i_size_read(inode))) in filemap_read()
2690 error = filemap_get_pages(iocb, iter->count, &fbatch, false); in filemap_read()
2698 * the correct value for "nr", which means the zero-filled in filemap_read()
2700 * another truncate extends the file - this is desired though). in filemap_read()
2703 if (unlikely(iocb->ki_pos >= isize)) in filemap_read()
2705 end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count); in filemap_read()
2709 * block_write_end()->mark_buffer_dirty() or other page in filemap_read()
2723 * When a read accesses the same folio several times, only in filemap_read()
2726 if (!pos_same_folio(iocb->ki_pos, last_pos - 1, in filemap_read()
2733 size_t offset = iocb->ki_pos & (fsize - 1); in filemap_read()
2734 size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos, in filemap_read()
2735 fsize - offset); in filemap_read()
2745 * before reading the folio on the kernel side. in filemap_read()
2753 iocb->ki_pos += copied; in filemap_read()
2754 last_pos = iocb->ki_pos; in filemap_read()
2757 error = -EFAULT; in filemap_read()
2765 } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error); in filemap_read()
2768 ra->prev_pos = last_pos; in filemap_read()
2775 struct address_space *mapping = iocb->ki_filp->f_mapping; in kiocb_write_and_wait()
2776 loff_t pos = iocb->ki_pos; in kiocb_write_and_wait()
2777 loff_t end = pos + count - 1; in kiocb_write_and_wait()
2779 if (iocb->ki_flags & IOCB_NOWAIT) { in kiocb_write_and_wait()
2781 return -EAGAIN; in kiocb_write_and_wait()
2790 struct address_space *mapping = iocb->ki_filp->f_mapping; in kiocb_invalidate_pages()
2791 loff_t pos = iocb->ki_pos; in kiocb_invalidate_pages()
2792 loff_t end = pos + count - 1; in kiocb_invalidate_pages()
2795 if (iocb->ki_flags & IOCB_NOWAIT) { in kiocb_invalidate_pages()
2798 return -EAGAIN; in kiocb_invalidate_pages()
2809 * without clobbering -EIOCBQUEUED from ->direct_IO(). in kiocb_invalidate_pages()
2816 * generic_file_read_iter - generic filesystem read routine
2818 * @iter: destination for the data read
2823 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2824 * be returned when no data can be read without waiting for I/O requests
2827 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2828 * requests shall be made for the read or for readahead. When no data
2829 * can be read, -EAGAIN shall be returned. When readahead would be
2830 * triggered, a partial, possibly empty read shall be returned.
2834 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
2845 if (iocb->ki_flags & IOCB_DIRECT) { in generic_file_read_iter()
2846 struct file *file = iocb->ki_filp; in generic_file_read_iter()
2847 struct address_space *mapping = file->f_mapping; in generic_file_read_iter()
2848 struct inode *inode = mapping->host; in generic_file_read_iter()
2855 retval = mapping->a_ops->direct_IO(iocb, iter); in generic_file_read_iter()
2857 iocb->ki_pos += retval; in generic_file_read_iter()
2858 count -= retval; in generic_file_read_iter()
2860 if (retval != -EIOCBQUEUED) in generic_file_read_iter()
2861 iov_iter_revert(iter, count - iov_iter_count(iter)); in generic_file_read_iter()
2864 * Btrfs can have a short DIO read if we encounter in generic_file_read_iter()
2866 * we've already read everything we wanted to, or if in generic_file_read_iter()
2867 * there was a short read because we hit EOF, go ahead in generic_file_read_iter()
2869 * the rest of the read. Buffered reads will not work for in generic_file_read_iter()
2874 if (iocb->ki_pos >= i_size_read(inode)) in generic_file_read_iter()
2892 size = min(size, folio_size(folio) - offset); in splice_folio_into_pipe()
2896 !pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { in splice_folio_into_pipe()
2898 size_t part = min_t(size_t, PAGE_SIZE - offset, size - spliced); in splice_folio_into_pipe()
2907 pipe->head++; in splice_folio_into_pipe()
2917 * filemap_splice_read - Splice data from a file's pagecache into a pipe
2918 * @in: The file to read from
2919 * @ppos: Pointer to the file position to read from
2928 * Return: On success, the number of bytes read will be returned and *@ppos
2930 * to be read; -EAGAIN will be returned if the pipe had no space, and some
2931 * other negative error code will be returned on error. A short read may occur
2946 if (unlikely(*ppos >= in->f_mapping->host->i_sb->s_maxbytes)) in filemap_splice_read()
2953 used = pipe_occupancy(pipe->head, pipe->tail); in filemap_splice_read()
2954 npages = max_t(ssize_t, pipe->max_usage - used, 0); in filemap_splice_read()
2962 if (*ppos >= i_size_read(in->f_mapping->host)) in filemap_splice_read()
2974 * the correct value for "nr", which means the zero-filled in filemap_splice_read()
2976 * another truncate extends the file - this is desired though). in filemap_splice_read()
2978 isize = i_size_read(in->f_mapping->host); in filemap_splice_read()
2987 writably_mapped = mapping_writably_mapped(in->f_mapping); in filemap_splice_read()
3000 * before reading the folio on the kernel side. in filemap_splice_read()
3005 n = min_t(loff_t, len, isize - *ppos); in filemap_splice_read()
3009 len -= n; in filemap_splice_read()
3012 in->f_ra.prev_pos = *ppos; in filemap_splice_read()
3013 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) in filemap_splice_read()
3032 const struct address_space_operations *ops = mapping->a_ops; in folio_seek_hole_data()
3033 size_t offset, bsz = i_blocksize(mapping->host); in folio_seek_hole_data()
3037 if (!ops->is_partially_uptodate) in folio_seek_hole_data()
3043 if (unlikely(folio->mapping != mapping)) in folio_seek_hole_data()
3046 offset = offset_in_folio(folio, start) & ~(bsz - 1); in folio_seek_hole_data()
3049 if (ops->is_partially_uptodate(folio, offset, bsz) == in folio_seek_hole_data()
3052 start = (start + bsz) & ~((u64)bsz - 1); in folio_seek_hole_data()
3064 return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index); in seek_folio_size()
3069 * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page cache.
3078 * entirely memory-based such as tmpfs, and filesystems which support
3081 * Return: The requested offset on success, or -ENXIO if @whence specifies
3083 * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @start
3089 XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT); in mapping_seek_hole_data()
3090 pgoff_t max = (end - 1) >> PAGE_SHIFT; in mapping_seek_hole_data()
3095 return -ENXIO; in mapping_seek_hole_data()
3122 start = -ENXIO; in mapping_seek_hole_data()
3135 * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
3136 * @vmf - the vm_fault for this fault.
3137 * @folio - the folio to lock.
3138 * @fpin - the pointer to the file we may pin (or is already pinned).
3157 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) in lock_folio_maybe_drop_mmap()
3161 if (vmf->flags & FAULT_FLAG_KILLABLE) { in lock_folio_maybe_drop_mmap()
3170 mmap_read_unlock(vmf->vma->vm_mm); in lock_folio_maybe_drop_mmap()
3188 struct file *file = vmf->vma->vm_file; in do_sync_mmap_readahead()
3189 struct file_ra_state *ra = &file->f_ra; in do_sync_mmap_readahead()
3190 struct address_space *mapping = file->f_mapping; in do_sync_mmap_readahead()
3191 DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff); in do_sync_mmap_readahead()
3193 unsigned long vm_flags = vmf->vma->vm_flags; in do_sync_mmap_readahead()
3200 ractl._index &= ~((unsigned long)HPAGE_PMD_NR - 1); in do_sync_mmap_readahead()
3201 ra->size = HPAGE_PMD_NR; in do_sync_mmap_readahead()
3207 ra->size *= 2; in do_sync_mmap_readahead()
3208 ra->async_size = HPAGE_PMD_NR; in do_sync_mmap_readahead()
3214 /* If we don't want any read-ahead, don't bother */ in do_sync_mmap_readahead()
3217 if (!ra->ra_pages) in do_sync_mmap_readahead()
3222 page_cache_sync_ra(&ractl, ra->ra_pages); in do_sync_mmap_readahead()
3227 mmap_miss = READ_ONCE(ra->mmap_miss); in do_sync_mmap_readahead()
3229 WRITE_ONCE(ra->mmap_miss, ++mmap_miss); in do_sync_mmap_readahead()
3233 * stop bothering with read-ahead. It will only hurt. in do_sync_mmap_readahead()
3239 * mmap read-around in do_sync_mmap_readahead()
3242 ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2); in do_sync_mmap_readahead()
3243 ra->size = ra->ra_pages; in do_sync_mmap_readahead()
3244 ra->async_size = ra->ra_pages / 4; in do_sync_mmap_readahead()
3245 ractl._index = ra->start; in do_sync_mmap_readahead()
3258 struct file *file = vmf->vma->vm_file; in do_async_mmap_readahead()
3259 struct file_ra_state *ra = &file->f_ra; in do_async_mmap_readahead()
3260 DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff); in do_async_mmap_readahead()
3264 /* If we don't want any read-ahead, don't bother */ in do_async_mmap_readahead()
3265 if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages) in do_async_mmap_readahead()
3268 mmap_miss = READ_ONCE(ra->mmap_miss); in do_async_mmap_readahead()
3270 WRITE_ONCE(ra->mmap_miss, --mmap_miss); in do_async_mmap_readahead()
3274 page_cache_async_ra(&ractl, folio, ra->ra_pages); in do_async_mmap_readahead()
3280 * filemap_fault - read in file data for page fault handling
3284 * mapped memory region to read in file data during a page fault.
3290 * vma->vm_mm->mmap_lock must be held on entry.
3300 * Return: bitwise-OR of %VM_FAULT_ codes.
3305 struct file *file = vmf->vma->vm_file; in filemap_fault()
3307 struct address_space *mapping = file->f_mapping; in filemap_fault()
3308 struct inode *inode = mapping->host; in filemap_fault()
3309 pgoff_t max_idx, index = vmf->pgoff; in filemap_fault()
3327 if (!(vmf->flags & FAULT_FLAG_TRIED)) in filemap_fault()
3336 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); in filemap_fault()
3350 vmf->gfp_mask); in filemap_fault()
3363 if (unlikely(folio->mapping != mapping)) { in filemap_fault()
3372 * that it's up-to-date. If not, it is going to be due to an error. in filemap_fault()
3391 * time to return to the upper layer and have it re-find the vma and in filemap_fault()
3402 * Found the page and have a reference on it. in filemap_fault()
3412 vmf->page = folio_file_page(folio, index); in filemap_fault()
3417 * Umm, take care of errors if the page isn't up-to-date. in filemap_fault()
3418 * Try to re-read it _once_. We do this synchronously, in filemap_fault()
3423 error = filemap_read_folio(file, mapping->a_ops->read_folio, folio); in filemap_fault()
3437 * re-find the vma and come back and find our hopefully still populated in filemap_fault()
3453 struct mm_struct *mm = vmf->vma->vm_mm; in filemap_map_pmd()
3456 if (pmd_trans_huge(*vmf->pmd)) { in filemap_map_pmd()
3462 if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) { in filemap_map_pmd()
3472 if (pmd_none(*vmf->pmd) && vmf->prealloc_pte) in filemap_map_pmd()
3473 pmd_install(mm, vmf->pmd, &vmf->prealloc_pte); in filemap_map_pmd()
3502 if (folio->mapping != mapping) in next_uptodate_folio()
3506 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); in next_uptodate_folio()
3507 if (xas->xa_index >= max_idx) in next_uptodate_folio()
3531 pte_t *old_ptep = vmf->pte; in filemap_map_folio_range()
3542 * fault-around logic. in filemap_map_folio_range()
3544 if (!pte_none(vmf->pte[count])) in filemap_map_folio_range()
3553 if (in_range(vmf->address, addr, count * PAGE_SIZE)) in filemap_map_folio_range()
3559 vmf->pte += count; in filemap_map_folio_range()
3562 } while (--nr_pages > 0); in filemap_map_folio_range()
3567 if (in_range(vmf->address, addr, count * PAGE_SIZE)) in filemap_map_folio_range()
3571 vmf->pte = old_ptep; in filemap_map_folio_range()
3581 struct page *page = &folio->page; in filemap_map_order0_folio()
3591 * the fault-around logic. in filemap_map_order0_folio()
3593 if (!pte_none(ptep_get(vmf->pte))) in filemap_map_order0_folio()
3596 if (vmf->address == addr) in filemap_map_order0_folio()
3608 struct vm_area_struct *vma = vmf->vma; in filemap_map_pages()
3609 struct file *file = vma->vm_file; in filemap_map_pages()
3610 struct address_space *mapping = file->f_mapping; in filemap_map_pages()
3613 XA_STATE(xas, &mapping->i_pages, start_pgoff); in filemap_map_pages()
3628 addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT); in filemap_map_pages()
3629 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl); in filemap_map_pages()
3630 if (!vmf->pte) { in filemap_map_pages()
3638 addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT; in filemap_map_pages()
3639 vmf->pte += xas.xa_index - last_pgoff; in filemap_map_pages()
3641 end = folio->index + folio_nr_pages(folio) - 1; in filemap_map_pages()
3642 nr_pages = min(end, end_pgoff) - xas.xa_index + 1; in filemap_map_pages()
3649 xas.xa_index - folio->index, addr, in filemap_map_pages()
3655 pte_unmap_unlock(vmf->pte, vmf->ptl); in filemap_map_pages()
3659 mmap_miss_saved = READ_ONCE(file->f_ra.mmap_miss); in filemap_map_pages()
3661 WRITE_ONCE(file->f_ra.mmap_miss, 0); in filemap_map_pages()
3663 WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss_saved - mmap_miss); in filemap_map_pages()
3671 struct address_space *mapping = vmf->vma->vm_file->f_mapping; in filemap_page_mkwrite()
3672 struct folio *folio = page_folio(vmf->page); in filemap_page_mkwrite()
3675 sb_start_pagefault(mapping->host->i_sb); in filemap_page_mkwrite()
3676 file_update_time(vmf->vma->vm_file); in filemap_page_mkwrite()
3678 if (folio->mapping != mapping) { in filemap_page_mkwrite()
3691 sb_end_pagefault(mapping->host->i_sb); in filemap_page_mkwrite()
3705 struct address_space *mapping = file->f_mapping; in generic_file_mmap()
3707 if (!mapping->a_ops->read_folio) in generic_file_mmap()
3708 return -ENOEXEC; in generic_file_mmap()
3710 vma->vm_ops = &generic_file_vm_ops; in generic_file_mmap()
3715 * This is for filesystems which do not implement ->writepage.
3719 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) in generic_file_readonly_mmap()
3720 return -EINVAL; in generic_file_readonly_mmap()
3730 return -ENOSYS; in generic_file_mmap()
3734 return -ENOSYS; in generic_file_readonly_mmap()
3749 filler = mapping->a_ops->read_folio; in do_read_cache_folio()
3755 return ERR_PTR(-ENOMEM); in do_read_cache_folio()
3759 if (err == -EEXIST) in do_read_cache_folio()
3776 if (!folio->mapping) { in do_read_cache_folio()
3803 * read_cache_folio - Read into page cache, fill it if needed.
3804 * @mapping: The address_space to read from.
3805 * @index: The index to read.
3806 * @filler: Function to perform the read, or NULL to use aops->read_folio().
3809 * Read one page into the page cache. If it succeeds, the folio returned
3815 * Context: May sleep. Expects mapping->invalidate_lock to be held.
3816 * Return: An uptodate folio on success, ERR_PTR() on failure.
3827 * mapping_read_folio_gfp - Read into page cache, using specified allocation flags.
3836 * possible and so is EINTR. If ->read_folio returns another error,
3839 * The function expects mapping->invalidate_lock to be already held.
3841 * Return: Uptodate folio on success, ERR_PTR() on failure.
3857 return &folio->page; in do_read_cache_page()
3870 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
3878 * If the page does not get brought uptodate, return -EIO.
3880 * The function expects mapping->invalidate_lock to be already held.
3882 * Return: up to date page on success, ERR_PTR() on failure.
3901 errseq_set(&filp->f_mapping->wb_err, -EIO); in dio_warn_stale_pagecache()
3906 …pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision… in dio_warn_stale_pagecache()
3907 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid, in dio_warn_stale_pagecache()
3908 current->comm); in dio_warn_stale_pagecache()
3914 struct address_space *mapping = iocb->ki_filp->f_mapping; in kiocb_invalidate_post_direct_write()
3916 if (mapping->nrpages && in kiocb_invalidate_post_direct_write()
3918 iocb->ki_pos >> PAGE_SHIFT, in kiocb_invalidate_post_direct_write()
3919 (iocb->ki_pos + count - 1) >> PAGE_SHIFT)) in kiocb_invalidate_post_direct_write()
3920 dio_warn_stale_pagecache(iocb->ki_filp); in kiocb_invalidate_post_direct_write()
3926 struct address_space *mapping = iocb->ki_filp->f_mapping; in generic_file_direct_write()
3936 if (written == -EBUSY) in generic_file_direct_write()
3941 written = mapping->a_ops->direct_IO(iocb, from); in generic_file_direct_write()
3945 * cached by non-direct readahead, or faulted in by get_user_pages() in generic_file_direct_write()
3961 struct inode *inode = mapping->host; in generic_file_direct_write()
3962 loff_t pos = iocb->ki_pos; in generic_file_direct_write()
3966 write_len -= written; in generic_file_direct_write()
3967 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { in generic_file_direct_write()
3971 iocb->ki_pos = pos; in generic_file_direct_write()
3973 if (written != -EIOCBQUEUED) in generic_file_direct_write()
3974 iov_iter_revert(from, write_len - iov_iter_count(from)); in generic_file_direct_write()
3981 struct file *file = iocb->ki_filp; in generic_perform_write()
3982 loff_t pos = iocb->ki_pos; in generic_perform_write()
3983 struct address_space *mapping = file->f_mapping; in generic_perform_write()
3984 const struct address_space_operations *a_ops = mapping->a_ops; in generic_perform_write()
3995 offset = (pos & (PAGE_SIZE - 1)); in generic_perform_write()
3996 bytes = min_t(unsigned long, PAGE_SIZE - offset, in generic_perform_write()
4002 * Otherwise there's a nasty deadlock on copying from the in generic_perform_write()
4004 * up-to-date. in generic_perform_write()
4007 status = -EFAULT; in generic_perform_write()
4012 status = -EINTR; in generic_perform_write()
4016 status = a_ops->write_begin(file, mapping, pos, bytes, in generic_perform_write()
4027 status = a_ops->write_end(file, mapping, pos, bytes, copied, in generic_perform_write()
4030 iov_iter_revert(i, copied - max(status, 0L)); in generic_perform_write()
4038 * A short copy made ->write_end() reject the in generic_perform_write()
4055 iocb->ki_pos += written; in generic_perform_write()
4061 * __generic_file_write_iter - write data to a file
4067 * modification times and calls proper subroutines depending on whether we
4070 * It expects i_rwsem to be grabbed unless we work on a block device or similar
4083 struct file *file = iocb->ki_filp; in __generic_file_write_iter()
4084 struct address_space *mapping = file->f_mapping; in __generic_file_write_iter()
4085 struct inode *inode = mapping->host; in __generic_file_write_iter()
4096 if (iocb->ki_flags & IOCB_DIRECT) { in __generic_file_write_iter()
4103 * page-cache pages correctly). in __generic_file_write_iter()
4116 * generic_file_write_iter - write data to a file
4130 struct file *file = iocb->ki_filp; in generic_file_write_iter()
4131 struct inode *inode = file->f_mapping->host; in generic_file_write_iter()
4147 * filemap_release_folio() - Release fs-specific metadata on a folio.
4152 * (presumably at folio->private).
4154 * This will also be called if the private_2 flag is set on a page,
4165 struct address_space * const mapping = folio->mapping; in filemap_release_folio()
4173 if (mapping && mapping->a_ops->release_folio) in filemap_release_folio()
4174 return mapping->a_ops->release_folio(folio, gfp); in filemap_release_folio()
4181 * filemap_cachestat() - compute the page cache statistics of a mapping
4195 XA_STATE(xas, &mapping->i_pages, first_index); in filemap_cachestat()
4212 * the rcu-protected xarray. in filemap_cachestat()
4221 folio_last_index = folio_first_index + nr_pages - 1; in filemap_cachestat()
4225 nr_pages -= first_index - folio_first_index; in filemap_cachestat()
4228 nr_pages -= folio_last_index - last_index; in filemap_cachestat()
4235 cs->nr_evicted += nr_pages; in filemap_cachestat()
4239 /* shmem file - in swap cache */ in filemap_cachestat()
4262 cs->nr_recently_evicted += nr_pages; in filemap_cachestat()
4268 cs->nr_cache += nr_pages; in filemap_cachestat()
4271 cs->nr_dirty += nr_pages; in filemap_cachestat()
4274 cs->nr_writeback += nr_pages; in filemap_cachestat()
4292 if (f->f_mode & FMODE_WRITE) in can_do_cachestat()
4311 * there is memory pressure on the system.
4313 * `off` and `len` must be non-negative integers. If `len` > 0,
4327 * zero - success
4328 * -EFAULT - cstat or cstat_range points to an illegal address
4329 * -EINVAL - invalid flags
4330 * -EBADF - invalid file descriptor
4331 * -EOPNOTSUPP - file descriptor is of a hugetlbfs file
4344 return -EBADF; in SYSCALL_DEFINE4()
4349 return -EFAULT; in SYSCALL_DEFINE4()
4355 return -EOPNOTSUPP; in SYSCALL_DEFINE4()
4360 return -EPERM; in SYSCALL_DEFINE4()
4365 return -EINVAL; in SYSCALL_DEFINE4()
4370 csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT; in SYSCALL_DEFINE4()
4372 mapping = f.file->f_mapping; in SYSCALL_DEFINE4()
4377 return -EFAULT; in SYSCALL_DEFINE4()