Lines Matching +full:data +full:- +full:mapping

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)
126 static void page_cache_delete(struct address_space *mapping,
129 XA_STATE(xas, &mapping->i_pages, folio->index);
132 mapping_set_update(&xas, mapping);
136 xas_set_order(&xas, folio->index, folio_order(folio));
145 folio->mapping = NULL;
146 /* Leave page->index set: truncation lookup relies upon it */
147 mapping->nrpages -= nr;
150 static void filemap_unaccount_folio(struct address_space *mapping,
158 current->comm, folio_pfn(folio));
159 dump_page(&folio->page, "still mapped when deleted");
163 if (mapping_exiting(mapping) && !folio_test_large(folio)) {
164 int mapcount = page_mapcount(&folio->page);
173 page_mapcount_reset(&folio->page);
185 __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
187 __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr);
189 __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, -nr);
191 __lruvec_stat_mod_folio(folio, NR_FILE_THPS, -nr);
192 filemap_nr_thps_dec(mapping);
198 * unwritten data - on ordinary filesystems.
200 * But it's harmless on in-memory filesystems like tmpfs; and can
210 mapping_can_writeback(mapping)))
211 folio_account_cleaned(folio, inode_to_wb(mapping->host));
216 * sure the page is locked and that nobody else uses it - or that usage
221 struct address_space *mapping = folio->mapping;
224 filemap_unaccount_folio(mapping, folio);
225 page_cache_delete(mapping, folio, shadow);
228 void filemap_free_folio(struct address_space *mapping, struct folio *folio)
233 free_folio = mapping->a_ops->free_folio;
243 * filemap_remove_folio - Remove folio from page cache.
252 struct address_space *mapping = folio->mapping;
255 spin_lock(&mapping->host->i_lock);
256 xa_lock_irq(&mapping->i_pages);
258 xa_unlock_irq(&mapping->i_pages);
259 if (mapping_shrinkable(mapping))
260 inode_add_lru(mapping->host);
261 spin_unlock(&mapping->host->i_lock);
263 filemap_free_folio(mapping, folio);
267 * page_cache_delete_batch - delete several folios from page cache
268 * @mapping: the mapping to which folios belong
271 * The function walks over mapping->i_pages and removes folios passed in
272 * @fbatch from the mapping. The function expects @fbatch to be sorted
274 * It tolerates holes in @fbatch (mapping entries at those indices are not
279 static void page_cache_delete_batch(struct address_space *mapping,
282 XA_STATE(xas, &mapping->i_pages, fbatch->folios[0]->index);
287 mapping_set_update(&xas, mapping);
302 if (folio != fbatch->folios[i]) {
303 VM_BUG_ON_FOLIO(folio->index >
304 fbatch->folios[i]->index, folio);
310 folio->mapping = NULL;
311 /* Leave folio->index set: truncation lookup relies on it */
317 mapping->nrpages -= total_pages;
320 void delete_from_page_cache_batch(struct address_space *mapping,
328 spin_lock(&mapping->host->i_lock);
329 xa_lock_irq(&mapping->i_pages);
331 struct folio *folio = fbatch->folios[i];
334 filemap_unaccount_folio(mapping, folio);
336 page_cache_delete_batch(mapping, fbatch);
337 xa_unlock_irq(&mapping->i_pages);
338 if (mapping_shrinkable(mapping))
339 inode_add_lru(mapping->host);
340 spin_unlock(&mapping->host->i_lock);
343 filemap_free_folio(mapping, fbatch->folios[i]);
346 int filemap_check_errors(struct address_space *mapping)
350 if (test_bit(AS_ENOSPC, &mapping->flags) &&
351 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
352 ret = -ENOSPC;
353 if (test_bit(AS_EIO, &mapping->flags) &&
354 test_and_clear_bit(AS_EIO, &mapping->flags))
355 ret = -EIO;
360 static int filemap_check_and_keep_errors(struct address_space *mapping)
363 if (test_bit(AS_EIO, &mapping->flags))
364 return -EIO;
365 if (test_bit(AS_ENOSPC, &mapping->flags))
366 return -ENOSPC;
371 * filemap_fdatawrite_wbc - start writeback on mapping dirty pages in range
372 * @mapping: address space structure to write
375 * Call writepages on the mapping using the provided wbc to control the
380 int filemap_fdatawrite_wbc(struct address_space *mapping,
385 if (!mapping_can_writeback(mapping) ||
386 !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
389 wbc_attach_fdatawrite_inode(wbc, mapping->host);
390 ret = do_writepages(mapping, wbc);
397 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
398 * @mapping: address space structure to write
403 * Start writeback against all of a mapping's dirty pages that lie
406 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
413 int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
423 return filemap_fdatawrite_wbc(mapping, &wbc);
426 static inline int __filemap_fdatawrite(struct address_space *mapping,
429 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
432 int filemap_fdatawrite(struct address_space *mapping)
434 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
438 int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
441 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
446 * filemap_flush - mostly a non-blocking flush
447 * @mapping: target address_space
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.
454 int filemap_flush(struct address_space *mapping)
456 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
461 * filemap_range_has_page - check if a page exists in range.
462 * @mapping: address space within which to check
472 bool filemap_range_has_page(struct address_space *mapping,
476 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
503 static void __filemap_fdatawait_range(struct address_space *mapping,
516 nr_folios = filemap_get_folios_tag(mapping, &index, end,
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
549 int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
552 __filemap_fdatawait_range(mapping, start_byte, end_byte);
553 return filemap_check_errors(mapping);
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
568 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
571 int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
574 __filemap_fdatawait_range(mapping, start_byte, end_byte);
575 return filemap_check_and_keep_errors(mapping);
580 * file_fdatawait_range - wait for writeback to complete
585 * Walk the list of under-writeback pages of the address space that file
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;
599 __filemap_fdatawait_range(mapping, start_byte, end_byte);
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
613 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
618 int filemap_fdatawait_keep_errors(struct address_space *mapping)
620 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
621 return filemap_check_and_keep_errors(mapping);
626 static bool mapping_needs_writeback(struct address_space *mapping)
628 return mapping->nrpages;
631 bool filemap_range_has_writeback(struct address_space *mapping,
634 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
657 * filemap_write_and_wait_range - write out & wait on a file range
658 * @mapping: the address_space for the pages
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).
669 int filemap_write_and_wait_range(struct address_space *mapping,
677 if (mapping_needs_writeback(mapping)) {
678 err = __filemap_fdatawrite_range(mapping, lstart, lend,
682 * written partially (e.g. -ENOSPC), so we wait for it.
683 * But the -EIO is special case, it may indicate the worst
686 if (err != -EIO)
687 __filemap_fdatawait_range(mapping, lstart, lend);
689 err2 = filemap_check_errors(mapping);
696 void __filemap_set_wb_err(struct address_space *mapping, int err)
698 errseq_t eseq = errseq_set(&mapping->wb_err, err);
700 trace_filemap_set_wb_err(mapping, eseq);
705 * file_check_and_advance_wb_err - report wb error (if any) that was previously
713 * Grab the wb_err from the mapping. If it matches what we have in the file,
716 * If it doesn't match, then take the mapping value, set the "seen" flag in
722 * While we handle mapping->wb_err with atomic operations, the f_wb_err
731 errseq_t old = READ_ONCE(file->f_wb_err);
732 struct address_space *mapping = file->f_mapping;
735 if (errseq_check(&mapping->wb_err, old)) {
737 spin_lock(&file->f_lock);
738 old = file->f_wb_err;
739 err = errseq_check_and_advance(&mapping->wb_err,
740 &file->f_wb_err);
742 spin_unlock(&file->f_lock);
750 clear_bit(AS_EIO, &mapping->flags);
751 clear_bit(AS_ENOSPC, &mapping->flags);
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
775 struct address_space *mapping = file->f_mapping;
780 if (mapping_needs_writeback(mapping)) {
781 err = __filemap_fdatawrite_range(mapping, lstart, lend,
784 if (err != -EIO)
785 __filemap_fdatawait_range(mapping, lstart, lend);
795 * replace_page_cache_folio - replace a pagecache folio with a new one
809 struct address_space *mapping = old->mapping;
810 void (*free_folio)(struct folio *) = mapping->a_ops->free_folio;
811 pgoff_t offset = old->index;
812 XA_STATE(xas, &mapping->i_pages, offset);
816 VM_BUG_ON_FOLIO(new->mapping, new);
819 new->mapping = mapping;
820 new->index = offset;
827 old->mapping = NULL;
844 noinline int __filemap_add_folio(struct address_space *mapping,
847 XA_STATE(xas, &mapping->i_pages, index);
856 mapping_set_update(&xas, mapping);
860 VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio);
870 folio->mapping = mapping;
871 folio->index = xas.xa_index;
874 int order = -1, split_order = 0;
881 xas_set_err(&xas, -EEXIST);
888 if (order == -1)
892 /* entry may have changed before we re-acquire the lock */
901 BUG_ON(shmem_mapping(mapping));
917 mapping->nrpages += nr;
953 folio->mapping = NULL;
954 /* Leave page->index set: truncation relies upon it */
960 int filemap_add_folio(struct address_space *mapping, struct folio *folio,
967 ret = __filemap_add_folio(mapping, folio, index, gfp, &shadow);
976 * data from the working set, only to cache data that will
1010 * filemap_invalidate_lock_two - lock invalidate_lock for two mappings
1012 * Lock exclusively invalidate_lock of any passed mapping that is not NULL.
1014 * @mapping1: the first mapping to lock
1015 * @mapping2: the second mapping to lock
1023 down_write(&mapping1->invalidate_lock);
1025 down_write_nested(&mapping2->invalidate_lock, 1);
1030 * filemap_invalidate_unlock_two - unlock invalidate_lock for two mappings
1032 * Unlock exclusive invalidate_lock of any passed mapping that is not NULL.
1034 * @mapping1: the first mapping to unlock
1035 * @mapping2: the second mapping to unlock
1041 up_write(&mapping1->invalidate_lock);
1043 up_write(&mapping2->invalidate_lock);
1077 * The page wait code treats the "wait->flags" somewhat unusually, because
1124 flags = wait->flags;
1126 if (test_bit(key->bit_nr, &key->folio->flags))
1127 return -1;
1129 if (test_and_set_bit(key->bit_nr, &key->folio->flags))
1130 return -1;
1136 * We are holding the wait-queue lock, but the waiter that
1141 * afterwards to avoid any races. This store-release pairs
1142 * with the load-acquire in folio_wait_bit_common().
1144 smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN);
1145 wake_up_state(wait->private, mode);
1153 * After this list_del_init(&wait->entry) the wait entry
1154 * might be de-allocated and the process might even have
1157 list_del_init_careful(&wait->entry);
1177 spin_lock_irqsave(&q->lock, flags);
1187 spin_unlock_irqrestore(&q->lock, flags);
1189 spin_lock_irqsave(&q->lock, flags);
1205 spin_unlock_irqrestore(&q->lock, flags);
1237 if (wait->flags & WQ_FLAG_EXCLUSIVE) {
1238 if (test_and_set_bit(bit_nr, &folio->flags))
1240 } else if (test_bit(bit_nr, &folio->flags))
1243 wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE;
1269 wait->func = wake_page_function;
1274 wait->flags = 0;
1276 wait->flags = WQ_FLAG_EXCLUSIVE;
1277 if (--unfairness < 0)
1278 wait->flags |= WQ_FLAG_CUSTOM;
1295 spin_lock_irq(&q->lock);
1299 spin_unlock_irq(&q->lock);
1315 * be very careful with the 'wait->flags', because
1324 flags = smp_load_acquire(&wait->flags);
1333 /* If we were non-exclusive, we're done */
1350 wait->flags |= WQ_FLAG_DONE;
1356 * waiter from the wait-queues, but the folio waiters bit will remain
1368 * NOTE! The wait->flags weren't stable until we've done the
1377 * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
1381 return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
1383 return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
1388 * migration_entry_wait_on_locked - Wait for a migration entry to be removed
1422 wait->func = wake_page_function;
1425 wait->flags = 0;
1427 spin_lock_irq(&q->lock);
1431 spin_unlock_irq(&q->lock);
1446 flags = smp_load_acquire(&wait->flags);
1479 * folio_put_wait_locked - Drop a reference and wait for it to be unlocked
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
1508 spin_lock_irqsave(&q->lock, flags);
1511 spin_unlock_irqrestore(&q->lock, flags);
1539 * folio_unlock - Unlock a locked folio.
1559 * folio_end_private_2 - Clear PG_private_2 and wake any waiters.
1579 * folio_wait_private_2 - Wait for PG_private_2 to be cleared on a folio.
1592 * folio_wait_private_2_killable - Wait for PG_private_2 to be cleared on a folio.
1599 * - 0 if successful.
1600 * - -EINTR if a fatal signal was encountered.
1617 * folio_end_writeback - End writeback against a folio.
1652 * __folio_lock - Get a lock on the folio, assuming we need to sleep to get it.
1674 wait->folio = folio;
1675 wait->bit_nr = PG_locked;
1677 spin_lock_irq(&q->lock);
1678 __add_wait_queue_entry_tail(q, &wait->wait);
1688 __remove_wait_queue(q, &wait->wait);
1690 ret = -EIOCBQUEUED;
1691 spin_unlock_irq(&q->lock);
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;
1712 * CAUTION! In this case, mmap_lock/per-VMA lock is not
1741 * page_cache_next_miss() - Find the next gap in the page cache.
1742 * @mapping: Mapping.
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.
1759 pgoff_t page_cache_next_miss(struct address_space *mapping,
1762 XA_STATE(xas, &mapping->i_pages, index);
1764 while (max_scan--) {
1777 * page_cache_prev_miss() - Find the previous gap in the page cache.
1778 * @mapping: Mapping.
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.
1795 pgoff_t page_cache_prev_miss(struct address_space *mapping,
1798 XA_STATE(xas, &mapping->i_pages, index);
1800 while (max_scan--) {
1833 * filemap_get_entry - Get a page cache entry.
1834 * @mapping: the address_space to search
1837 * Looks up the page cache entry at @mapping & @index. If it is a folio,
1844 void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
1846 XA_STATE(xas, &mapping->i_pages, index);
1876 * __filemap_get_folio - Find and get a reference to a folio.
1877 * @mapping: The address_space to search.
1882 * Looks up the page cache entry at @mapping & @index.
1891 struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
1897 folio = filemap_get_entry(mapping, index);
1907 return ERR_PTR(-EAGAIN);
1914 if (unlikely(folio->mapping != mapping)) {
1937 if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
1948 if (!mapping_large_folio_support(mapping))
1953 if (index & ((1UL << order) - 1))
1959 err = -ENOMEM;
1970 err = filemap_add_folio(mapping, folio, index, gfp);
1975 } while (order-- > 0);
1977 if (err == -EEXIST)
1990 return ERR_PTR(-ENOENT);
2031 * find_get_entries - gang pagecache lookup
2032 * @mapping: The address_space to search
2039 * the mapping. The entries are placed in @fbatch. find_get_entries()
2043 * due to not-present entries or large folios.
2050 unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
2053 XA_STATE(xas, &mapping->i_pages, *start);
2058 indices[fbatch->nr] = xas.xa_index;
2066 int idx = folio_batch_count(fbatch) - 1;
2068 folio = fbatch->folios[idx];
2077 * find_lock_entries - Find a batch of pagecache entries.
2078 * @mapping: The address_space to search.
2084 * find_lock_entries() will return a batch of entries from @mapping.
2091 * due to not-present entries, large folios, folios which could not be
2096 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
2099 XA_STATE(xas, &mapping->i_pages, *start);
2105 if (folio->index < *start)
2107 if (folio_next_index(folio) - 1 > end)
2111 if (folio->mapping != mapping ||
2117 indices[fbatch->nr] = xas.xa_index;
2130 int idx = folio_batch_count(fbatch) - 1;
2132 folio = fbatch->folios[idx];
2141 * filemap_get_folios - Get a batch of folios
2142 * @mapping: The address_space to search
2147 * Search for and return a batch of folios in the mapping starting at
2161 unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start,
2164 XA_STATE(xas, &mapping->i_pages, *start);
2177 *start = folio->index + nr;
2185 * breaks the iteration when there is a page at index -1 but that is
2188 if (end == (pgoff_t)-1)
2189 *start = (pgoff_t)-1;
2200 * filemap_get_folios_contig - Get a batch of contiguous folios
2201 * @mapping: The address_space to search
2214 unsigned filemap_get_folios_contig(struct address_space *mapping,
2217 XA_STATE(xas, &mapping->i_pages, *start);
2245 *start = folio->index + nr;
2260 folio = fbatch->folios[nr - 1];
2262 *start = folio->index + 1;
2273 * filemap_get_folios_tag - Get a batch of folios matching @tag
2274 * @mapping: The address_space to search
2285 unsigned filemap_get_folios_tag(struct address_space *mapping, pgoff_t *start,
2288 XA_STATE(xas, &mapping->i_pages, *start);
2305 *start = folio->index + nr;
2312 * breaks the iteration when there is a page at index -1 but that is
2315 if (end == (pgoff_t)-1)
2316 *start = (pgoff_t)-1;
2330 * ---R__________________________________________B__________
2343 ra->ra_pages /= 4;
2347 * filemap_get_read_batch - Get a batch of folios for read
2355 static void filemap_get_read_batch(struct address_space *mapping,
2358 XA_STATE(xas, &mapping->i_pages, index);
2381 xas_advance(&xas, folio_next_index(folio) - 1);
2420 shrink_readahead_size_eio(&file->f_ra);
2421 return -EIO;
2424 static bool filemap_range_uptodate(struct address_space *mapping,
2433 if (!mapping->a_ops->is_partially_uptodate)
2435 if (mapping->host->i_blkbits >= folio_shift(folio))
2439 count -= folio_pos(folio) - pos;
2442 pos -= folio_pos(folio);
2445 return mapping->a_ops->is_partially_uptodate(folio, pos, count);
2449 struct address_space *mapping, size_t count,
2454 if (iocb->ki_flags & IOCB_NOWAIT) {
2455 if (!filemap_invalidate_trylock_shared(mapping))
2456 return -EAGAIN;
2458 filemap_invalidate_lock_shared(mapping);
2462 error = -EAGAIN;
2463 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
2465 if (!(iocb->ki_flags & IOCB_WAITQ)) {
2466 filemap_invalidate_unlock_shared(mapping);
2474 error = __folio_lock_async(folio, iocb->ki_waitq);
2480 if (!folio->mapping)
2484 if (filemap_range_uptodate(mapping, iocb->ki_pos, count, folio,
2488 error = -EAGAIN;
2489 if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
2492 error = filemap_read_folio(iocb->ki_filp, mapping->a_ops->read_folio,
2498 filemap_invalidate_unlock_shared(mapping);
2505 struct address_space *mapping, pgoff_t index,
2511 folio = filemap_alloc_folio(mapping_gfp_mask(mapping), 0);
2513 return -ENOMEM;
2524 * pages or ->readahead() that need to hold invalidate_lock
2525 * while mapping blocks for IO so let's hold the lock here as
2528 filemap_invalidate_lock_shared(mapping);
2529 error = filemap_add_folio(mapping, folio, index,
2530 mapping_gfp_constraint(mapping, GFP_KERNEL));
2531 if (error == -EEXIST)
2536 error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
2540 filemap_invalidate_unlock_shared(mapping);
2544 filemap_invalidate_unlock_shared(mapping);
2550 struct address_space *mapping, struct folio *folio,
2553 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, folio->index);
2555 if (iocb->ki_flags & IOCB_NOIO)
2556 return -EAGAIN;
2557 page_cache_async_ra(&ractl, folio, last_index - folio->index);
2564 struct file *filp = iocb->ki_filp;
2565 struct address_space *mapping = filp->f_mapping;
2566 struct file_ra_state *ra = &filp->f_ra;
2567 pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
2573 last_index = DIV_ROUND_UP(iocb->ki_pos + count, PAGE_SIZE);
2576 return -EINTR;
2578 filemap_get_read_batch(mapping, index, last_index - 1, fbatch);
2580 if (iocb->ki_flags & IOCB_NOIO)
2581 return -EAGAIN;
2582 page_cache_sync_readahead(mapping, ra, filp, index,
2583 last_index - index);
2584 filemap_get_read_batch(mapping, index, last_index - 1, fbatch);
2587 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
2588 return -EAGAIN;
2589 err = filemap_create_folio(filp, mapping,
2590 iocb->ki_pos >> PAGE_SHIFT, fbatch);
2596 folio = fbatch->folios[folio_batch_count(fbatch) - 1];
2598 err = filemap_readahead(iocb, filp, mapping, folio, last_index);
2603 if ((iocb->ki_flags & IOCB_WAITQ) &&
2605 iocb->ki_flags |= IOCB_NOWAIT;
2606 err = filemap_update_page(iocb, mapping, count, folio,
2616 if (likely(--fbatch->nr))
2631 * filemap_read - Read data from the page cache.
2633 * @iter: Destination for the data.
2636 * Copies data from the page cache. If the data is not currently present,
2646 struct file *filp = iocb->ki_filp;
2647 struct file_ra_state *ra = &filp->f_ra;
2648 struct address_space *mapping = filp->f_mapping;
2649 struct inode *inode = mapping->host;
2654 loff_t last_pos = ra->prev_pos;
2656 if (unlikely(iocb->ki_pos >= inode->i_sb->s_maxbytes))
2661 iov_iter_truncate(iter, inode->i_sb->s_maxbytes - iocb->ki_pos);
2668 * If we've already successfully copied some data, then we
2669 * can no longer safely return -EIOCBQUEUED. Hence mark
2672 if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
2673 iocb->ki_flags |= IOCB_NOWAIT;
2675 if (unlikely(iocb->ki_pos >= i_size_read(inode)))
2678 error = filemap_get_pages(iocb, iter->count, &fbatch, false);
2686 * the correct value for "nr", which means the zero-filled
2688 * another truncate extends the file - this is desired though).
2691 if (unlikely(iocb->ki_pos >= isize))
2693 end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
2697 * block_write_end()->mark_buffer_dirty() or other page
2705 * Once we start copying data, we don't want to be touching any
2708 writably_mapped = mapping_writably_mapped(mapping);
2714 if (!pos_same_folio(iocb->ki_pos, last_pos - 1,
2721 size_t offset = iocb->ki_pos & (fsize - 1);
2722 size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
2723 fsize - offset);
2741 iocb->ki_pos += copied;
2742 last_pos = iocb->ki_pos;
2745 error = -EFAULT;
2753 } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
2756 ra->prev_pos = last_pos;
2763 struct address_space *mapping = iocb->ki_filp->f_mapping;
2764 loff_t pos = iocb->ki_pos;
2765 loff_t end = pos + count - 1;
2767 if (iocb->ki_flags & IOCB_NOWAIT) {
2768 if (filemap_range_needs_writeback(mapping, pos, end))
2769 return -EAGAIN;
2773 return filemap_write_and_wait_range(mapping, pos, end);
2778 struct address_space *mapping = iocb->ki_filp->f_mapping;
2779 loff_t pos = iocb->ki_pos;
2780 loff_t end = pos + count - 1;
2783 if (iocb->ki_flags & IOCB_NOWAIT) {
2785 if (filemap_range_has_page(mapping, pos, end))
2786 return -EAGAIN;
2788 ret = filemap_write_and_wait_range(mapping, pos, end);
2795 * the new data. We invalidate clean cached page from the region we're
2797 * without clobbering -EIOCBQUEUED from ->direct_IO().
2799 return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
2804 * generic_file_read_iter - generic filesystem read routine
2806 * @iter: destination for the data read
2811 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2812 * be returned when no data can be read without waiting for I/O requests
2815 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2816 * requests shall be made for the read or for readahead. When no data
2817 * can be read, -EAGAIN shall be returned. When readahead would be
2833 if (iocb->ki_flags & IOCB_DIRECT) {
2834 struct file *file = iocb->ki_filp;
2835 struct address_space *mapping = file->f_mapping;
2836 struct inode *inode = mapping->host;
2843 retval = mapping->a_ops->direct_IO(iocb, iter);
2845 iocb->ki_pos += retval;
2846 count -= retval;
2848 if (retval != -EIOCBQUEUED)
2849 iov_iter_revert(iter, count - iov_iter_count(iter));
2862 if (iocb->ki_pos >= i_size_read(inode))
2880 size = min(size, folio_size(folio) - offset);
2884 !pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
2886 size_t part = min_t(size_t, PAGE_SIZE - offset, size - spliced);
2895 pipe->head++;
2905 * filemap_splice_read - Splice data from a file's pagecache into a pipe
2917 * will be updated if appropriate; 0 will be returned if there is no more data
2918 * to be read; -EAGAIN will be returned if the pipe had no space, and some
2920 * if the pipe has insufficient space, we reach the end of the data or we hit a
2934 if (unlikely(*ppos >= in->f_mapping->host->i_sb->s_maxbytes))
2940 /* Work out how much data we can actually add into the pipe */
2941 used = pipe_occupancy(pipe->head, pipe->tail);
2942 npages = max_t(ssize_t, pipe->max_usage - used, 0);
2950 if (*ppos >= i_size_read(in->f_mapping->host))
2962 * the correct value for "nr", which means the zero-filled
2964 * another truncate extends the file - this is desired though).
2966 isize = i_size_read(in->f_mapping->host);
2972 * Once we start copying data, we don't want to be touching any
2975 writably_mapped = mapping_writably_mapped(in->f_mapping);
2993 n = min_t(loff_t, len, isize - *ppos);
2997 len -= n;
3000 in->f_ra.prev_pos = *ppos;
3001 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
3017 struct address_space *mapping, struct folio *folio,
3020 const struct address_space_operations *ops = mapping->a_ops;
3021 size_t offset, bsz = i_blocksize(mapping->host);
3025 if (!ops->is_partially_uptodate)
3031 if (unlikely(folio->mapping != mapping))
3034 offset = offset_in_folio(folio, start) & ~(bsz - 1);
3037 if (ops->is_partially_uptodate(folio, offset, bsz) ==
3040 start = (start + bsz) & ~((u64)bsz - 1);
3052 return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index);
3057 * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page cache.
3058 * @mapping: Address space to search.
3064 * contain data, your filesystem can use this function to implement
3066 * entirely memory-based such as tmpfs, and filesystems which support
3069 * Return: The requested offset on success, or -ENXIO if @whence specifies
3070 * SEEK_DATA and there is no data after @start. There is an implicit hole
3071 * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @start
3072 * and @end contain data.
3074 loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
3077 XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT);
3078 pgoff_t max = (end - 1) >> PAGE_SHIFT;
3083 return -ENXIO;
3098 start = folio_seek_hole_data(&xas, mapping, folio, start, pos,
3110 start = -ENXIO;
3123 * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
3124 * @vmf - the vm_fault for this fault.
3125 * @folio - the folio to lock.
3126 * @fpin - the pointer to the file we may pin (or is already pinned).
3145 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
3149 if (vmf->flags & FAULT_FLAG_KILLABLE) {
3158 mmap_read_unlock(vmf->vma->vm_mm);
3176 struct file *file = vmf->vma->vm_file;
3177 struct file_ra_state *ra = &file->f_ra;
3178 struct address_space *mapping = file->f_mapping;
3179 DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
3181 unsigned long vm_flags = vmf->vma->vm_flags;
3188 ractl._index &= ~((unsigned long)HPAGE_PMD_NR - 1);
3189 ra->size = HPAGE_PMD_NR;
3195 ra->size *= 2;
3196 ra->async_size = HPAGE_PMD_NR;
3202 /* If we don't want any read-ahead, don't bother */
3205 if (!ra->ra_pages)
3210 page_cache_sync_ra(&ractl, ra->ra_pages);
3215 mmap_miss = READ_ONCE(ra->mmap_miss);
3217 WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
3221 * stop bothering with read-ahead. It will only hurt.
3227 * mmap read-around
3230 ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
3231 ra->size = ra->ra_pages;
3232 ra->async_size = ra->ra_pages / 4;
3233 ractl._index = ra->start;
3246 struct file *file = vmf->vma->vm_file;
3247 struct file_ra_state *ra = &file->f_ra;
3248 DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff);
3252 /* If we don't want any read-ahead, don't bother */
3253 if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
3256 mmap_miss = READ_ONCE(ra->mmap_miss);
3258 WRITE_ONCE(ra->mmap_miss, --mmap_miss);
3262 page_cache_async_ra(&ractl, folio, ra->ra_pages);
3268 * filemap_fault - read in file data for page fault handling
3272 * mapped memory region to read in file data during a page fault.
3278 * vma->vm_mm->mmap_lock must be held on entry.
3288 * Return: bitwise-OR of %VM_FAULT_ codes.
3293 struct file *file = vmf->vma->vm_file;
3295 struct address_space *mapping = file->f_mapping;
3296 struct inode *inode = mapping->host;
3297 pgoff_t max_idx, index = vmf->pgoff;
3309 folio = filemap_get_folio(mapping, index);
3315 if (!(vmf->flags & FAULT_FLAG_TRIED))
3318 filemap_invalidate_lock_shared(mapping);
3324 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
3333 filemap_invalidate_lock_shared(mapping);
3336 folio = __filemap_get_folio(mapping, index,
3338 vmf->gfp_mask);
3342 filemap_invalidate_unlock_shared(mapping);
3351 if (unlikely(folio->mapping != mapping)) {
3360 * that it's up-to-date. If not, it is going to be due to an error.
3379 * time to return to the upper layer and have it re-find the vma and
3387 filemap_invalidate_unlock_shared(mapping);
3400 vmf->page = folio_file_page(folio, index);
3405 * Umm, take care of errors if the page isn't up-to-date.
3406 * Try to re-read it _once_. We do this synchronously,
3411 error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
3418 filemap_invalidate_unlock_shared(mapping);
3425 * re-find the vma and come back and find our hopefully still populated
3431 filemap_invalidate_unlock_shared(mapping);
3441 struct mm_struct *mm = vmf->vma->vm_mm;
3444 if (pmd_trans_huge(*vmf->pmd)) {
3450 if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) {
3460 if (pmd_none(*vmf->pmd) && vmf->prealloc_pte)
3461 pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
3467 struct address_space *mapping, pgoff_t end_pgoff)
3490 if (folio->mapping != mapping)
3494 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
3495 if (xas->xa_index >= max_idx)
3519 pte_t *old_ptep = vmf->pte;
3530 * fault-around logic.
3532 if (!pte_none(vmf->pte[count]))
3541 if (in_range(vmf->address, addr, count * PAGE_SIZE))
3547 vmf->pte += count;
3550 } while (--nr_pages > 0);
3555 if (in_range(vmf->address, addr, count * PAGE_SIZE))
3559 vmf->pte = old_ptep;
3569 struct page *page = &folio->page;
3579 * the fault-around logic.
3581 if (!pte_none(ptep_get(vmf->pte)))
3584 if (vmf->address == addr)
3596 struct vm_area_struct *vma = vmf->vma;
3597 struct file *file = vma->vm_file;
3598 struct address_space *mapping = file->f_mapping;
3601 XA_STATE(xas, &mapping->i_pages, start_pgoff);
3607 folio = next_uptodate_folio(&xas, mapping, end_pgoff);
3616 addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
3617 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
3618 if (!vmf->pte) {
3626 addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
3627 vmf->pte += xas.xa_index - last_pgoff;
3629 end = folio->index + folio_nr_pages(folio) - 1;
3630 nr_pages = min(end, end_pgoff) - xas.xa_index + 1;
3637 xas.xa_index - folio->index, addr,
3642 } while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
3643 pte_unmap_unlock(vmf->pte, vmf->ptl);
3647 mmap_miss_saved = READ_ONCE(file->f_ra.mmap_miss);
3649 WRITE_ONCE(file->f_ra.mmap_miss, 0);
3651 WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss_saved - mmap_miss);
3659 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
3660 struct folio *folio = page_folio(vmf->page);
3663 sb_start_pagefault(mapping->host->i_sb);
3664 file_update_time(vmf->vma->vm_file);
3666 if (folio->mapping != mapping) {
3679 sb_end_pagefault(mapping->host->i_sb);
3693 struct address_space *mapping = file->f_mapping;
3695 if (!mapping->a_ops->read_folio)
3696 return -ENOEXEC;
3698 vma->vm_ops = &generic_file_vm_ops;
3703 * This is for filesystems which do not implement ->writepage.
3707 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
3708 return -EINVAL;
3718 return -ENOSYS;
3722 return -ENOSYS;
3730 static struct folio *do_read_cache_folio(struct address_space *mapping,
3737 filler = mapping->a_ops->read_folio;
3739 folio = filemap_get_folio(mapping, index);
3743 return ERR_PTR(-ENOMEM);
3744 err = filemap_add_folio(mapping, folio, index, gfp);
3747 if (err == -EEXIST)
3763 /* Folio was truncated from mapping */
3764 if (!folio->mapping) {
3791 * read_cache_folio - Read into page cache, fill it if needed.
3792 * @mapping: The address_space to read from.
3794 * @filler: Function to perform the read, or NULL to use aops->read_folio().
3803 * Context: May sleep. Expects mapping->invalidate_lock to be held.
3806 struct folio *read_cache_folio(struct address_space *mapping, pgoff_t index,
3809 return do_read_cache_folio(mapping, index, filler, file,
3810 mapping_gfp_mask(mapping));
3815 * mapping_read_folio_gfp - Read into page cache, using specified allocation flags.
3816 * @mapping: The address_space for the folio.
3820 * This is the same as "read_cache_folio(mapping, index, NULL, NULL)", but with
3824 * possible and so is EINTR. If ->read_folio returns another error,
3827 * The function expects mapping->invalidate_lock to be already held.
3831 struct folio *mapping_read_folio_gfp(struct address_space *mapping,
3834 return do_read_cache_folio(mapping, index, NULL, NULL, gfp);
3838 static struct page *do_read_cache_page(struct address_space *mapping,
3843 folio = do_read_cache_folio(mapping, index, filler, file, gfp);
3845 return &folio->page;
3849 struct page *read_cache_page(struct address_space *mapping,
3852 return do_read_cache_page(mapping, index, filler, file,
3853 mapping_gfp_mask(mapping));
3858 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
3859 * @mapping: the page's address_space
3863 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
3866 * If the page does not get brought uptodate, return -EIO.
3868 * The function expects mapping->invalidate_lock to be already held.
3872 struct page *read_cache_page_gfp(struct address_space *mapping,
3876 return do_read_cache_page(mapping, index, NULL, NULL, gfp);
3889 errseq_set(&filp->f_mapping->wb_err, -EIO);
3894 pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n");
3895 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
3896 current->comm);
3902 struct address_space *mapping = iocb->ki_filp->f_mapping;
3904 if (mapping->nrpages &&
3905 invalidate_inode_pages2_range(mapping,
3906 iocb->ki_pos >> PAGE_SHIFT,
3907 (iocb->ki_pos + count - 1) >> PAGE_SHIFT))
3908 dio_warn_stale_pagecache(iocb->ki_filp);
3914 struct address_space *mapping = iocb->ki_filp->f_mapping;
3924 if (written == -EBUSY)
3929 written = mapping->a_ops->direct_IO(iocb, from);
3933 * cached by non-direct readahead, or faulted in by get_user_pages()
3946 * Skip invalidation for async writes or if mapping has no pages.
3949 struct inode *inode = mapping->host;
3950 loff_t pos = iocb->ki_pos;
3954 write_len -= written;
3955 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3959 iocb->ki_pos = pos;
3961 if (written != -EIOCBQUEUED)
3962 iov_iter_revert(from, write_len - iov_iter_count(from));
3969 struct file *file = iocb->ki_filp;
3970 loff_t pos = iocb->ki_pos;
3971 struct address_space *mapping = file->f_mapping;
3972 const struct address_space_operations *a_ops = mapping->a_ops;
3983 offset = (pos & (PAGE_SIZE - 1));
3984 bytes = min_t(unsigned long, PAGE_SIZE - offset,
3992 * up-to-date.
3995 status = -EFAULT;
4000 status = -EINTR;
4004 status = a_ops->write_begin(file, mapping, pos, bytes,
4009 if (mapping_writably_mapped(mapping))
4015 status = a_ops->write_end(file, mapping, pos, bytes, copied,
4018 iov_iter_revert(i, copied - max(status, 0L));
4026 * A short copy made ->write_end() reject the
4038 balance_dirty_pages_ratelimited(mapping);
4043 iocb->ki_pos += written;
4049 * __generic_file_write_iter - write data to a file
4051 * @from: iov_iter with data to write
4053 * This function does all the work needed for actually writing data to a
4061 * This function does *not* take care of syncing data in case of O_SYNC write.
4067 * * negative error code if no data has been written at all
4071 struct file *file = iocb->ki_filp;
4072 struct address_space *mapping = file->f_mapping;
4073 struct inode *inode = mapping->host;
4084 if (iocb->ki_flags & IOCB_DIRECT) {
4091 * page-cache pages correctly).
4104 * generic_file_write_iter - write data to a file
4106 * @from: iov_iter with data to write
4112 * * negative error code if no data has been written at all of
4118 struct file *file = iocb->ki_filp;
4119 struct inode *inode = file->f_mapping->host;
4135 * filemap_release_folio() - Release fs-specific metadata on a folio.
4139 * The address_space is trying to release any data attached to a folio
4140 * (presumably at folio->private).
4153 struct address_space * const mapping = folio->mapping;
4161 if (mapping && mapping->a_ops->release_folio)
4162 return mapping->a_ops->release_folio(folio, gfp);
4169 * filemap_cachestat() - compute the page cache statistics of a mapping
4170 * @mapping: The mapping to compute the statistics for.
4175 * This will query the page cache statistics of a mapping in the
4180 static void filemap_cachestat(struct address_space *mapping,
4183 XA_STATE(xas, &mapping->i_pages, first_index);
4200 * the rcu-protected xarray.
4209 folio_last_index = folio_first_index + nr_pages - 1;
4213 nr_pages -= first_index - folio_first_index;
4216 nr_pages -= folio_last_index - last_index;
4223 cs->nr_evicted += nr_pages;
4226 if (shmem_mapping(mapping)) {
4227 /* shmem file - in swap cache */
4250 cs->nr_recently_evicted += nr_pages;
4256 cs->nr_cache += nr_pages;
4259 cs->nr_dirty += nr_pages;
4262 cs->nr_writeback += nr_pages;
4287 * `off` and `len` must be non-negative integers. If `len` > 0,
4301 * zero - success
4302 * -EFAULT - cstat or cstat_range points to an illegal address
4303 * -EINVAL - invalid flags
4304 * -EBADF - invalid file descriptor
4305 * -EOPNOTSUPP - file descriptor is of a hugetlbfs file
4312 struct address_space *mapping;
4318 return -EBADF;
4323 return -EFAULT;
4329 return -EOPNOTSUPP;
4334 return -EINVAL;
4339 csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT;
4341 mapping = f.file->f_mapping;
4342 filemap_cachestat(mapping, first_index, last_index, &cs);
4346 return -EFAULT;