1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGEMAP_H
3 #define _LINUX_PAGEMAP_H
6 * Copyright 1995 Linus Torvalds
10 #include <linux/list.h>
11 #include <linux/highmem.h>
12 #include <linux/compiler.h>
13 #include <linux/uaccess.h>
14 #include <linux/gfp.h>
15 #include <linux/bitops.h>
16 #include <linux/hardirq.h> /* for in_interrupt() */
17 #include <linux/hugetlb_inline.h>
21 static inline bool mapping_empty(struct address_space *mapping)
23 return xa_empty(&mapping->i_pages);
27 * Bits in mapping->flags.
30 AS_EIO = 0, /* IO error on async write */
31 AS_ENOSPC = 1, /* ENOSPC on async write */
32 AS_MM_ALL_LOCKS = 2, /* under mm_take_all_locks() */
33 AS_UNEVICTABLE = 3, /* e.g., ramdisk, SHM_LOCK */
34 AS_EXITING = 4, /* final truncate in progress */
35 /* writeback related tags are not used */
36 AS_NO_WRITEBACK_TAGS = 5,
37 AS_THP_SUPPORT = 6, /* THPs supported */
41 * mapping_set_error - record a writeback error in the address_space
42 * @mapping: the mapping in which an error should be set
43 * @error: the error to set in the mapping
45 * When writeback fails in some way, we must record that error so that
46 * userspace can be informed when fsync and the like are called. We endeavor
47 * to report errors on any file that was open at the time of the error. Some
48 * internal callers also need to know when writeback errors have occurred.
50 * When a writeback error occurs, most filesystems will want to call
51 * mapping_set_error to record the error in the mapping so that it can be
52 * reported when the application calls fsync(2).
54 static inline void mapping_set_error(struct address_space *mapping, int error)
59 /* Record in wb_err for checkers using errseq_t based tracking */
60 __filemap_set_wb_err(mapping, error);
62 /* Record it in superblock */
64 errseq_set(&mapping->host->i_sb->s_wb_err, error);
66 /* Record it in flags for now, for legacy callers */
68 set_bit(AS_ENOSPC, &mapping->flags);
70 set_bit(AS_EIO, &mapping->flags);
73 static inline void mapping_set_unevictable(struct address_space *mapping)
75 set_bit(AS_UNEVICTABLE, &mapping->flags);
78 static inline void mapping_clear_unevictable(struct address_space *mapping)
80 clear_bit(AS_UNEVICTABLE, &mapping->flags);
83 static inline bool mapping_unevictable(struct address_space *mapping)
85 return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags);
88 static inline void mapping_set_exiting(struct address_space *mapping)
90 set_bit(AS_EXITING, &mapping->flags);
93 static inline int mapping_exiting(struct address_space *mapping)
95 return test_bit(AS_EXITING, &mapping->flags);
98 static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
100 set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
103 static inline int mapping_use_writeback_tags(struct address_space *mapping)
105 return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
108 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
110 return mapping->gfp_mask;
113 /* Restricts the given gfp_mask to what the mapping allows. */
114 static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
117 return mapping_gfp_mask(mapping) & gfp_mask;
121 * This is non-atomic. Only to be used before the mapping is activated.
122 * Probably needs a barrier...
124 static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
129 static inline bool mapping_thp_support(struct address_space *mapping)
131 return test_bit(AS_THP_SUPPORT, &mapping->flags);
134 static inline int filemap_nr_thps(struct address_space *mapping)
136 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
137 return atomic_read(&mapping->nr_thps);
143 static inline void filemap_nr_thps_inc(struct address_space *mapping)
145 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
146 if (!mapping_thp_support(mapping))
147 atomic_inc(&mapping->nr_thps);
153 static inline void filemap_nr_thps_dec(struct address_space *mapping)
155 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
156 if (!mapping_thp_support(mapping))
157 atomic_dec(&mapping->nr_thps);
163 void release_pages(struct page **pages, int nr);
165 struct address_space *page_mapping(struct page *);
166 struct address_space *folio_mapping(struct folio *);
167 struct address_space *swapcache_mapping(struct folio *);
170 * folio_file_mapping - Find the mapping this folio belongs to.
173 * For folios which are in the page cache, return the mapping that this
174 * page belongs to. Folios in the swap cache return the mapping of the
175 * swap file or swap device where the data is stored. This is different
176 * from the mapping returned by folio_mapping(). The only reason to
177 * use it is if, like NFS, you return 0 from ->activate_swapfile.
179 * Do not call this for folios which aren't in the page cache or swap cache.
181 static inline struct address_space *folio_file_mapping(struct folio *folio)
183 if (unlikely(folio_test_swapcache(folio)))
184 return swapcache_mapping(folio);
186 return folio->mapping;
189 static inline struct address_space *page_file_mapping(struct page *page)
191 return folio_file_mapping(page_folio(page));
195 * For file cache pages, return the address_space, otherwise return NULL
197 static inline struct address_space *page_mapping_file(struct page *page)
199 struct folio *folio = page_folio(page);
201 if (unlikely(folio_test_swapcache(folio)))
203 return folio_mapping(folio);
206 static inline bool page_cache_add_speculative(struct page *page, int count)
208 VM_BUG_ON_PAGE(PageTail(page), page);
209 return folio_ref_try_add_rcu((struct folio *)page, count);
212 static inline bool page_cache_get_speculative(struct page *page)
214 return page_cache_add_speculative(page, 1);
218 * folio_attach_private - Attach private data to a folio.
219 * @folio: Folio to attach data to.
220 * @data: Data to attach to folio.
222 * Attaching private data to a folio increments the page's reference count.
223 * The data must be detached before the folio will be freed.
225 static inline void folio_attach_private(struct folio *folio, void *data)
228 folio->private = data;
229 folio_set_private(folio);
233 * folio_detach_private - Detach private data from a folio.
234 * @folio: Folio to detach data from.
236 * Removes the data that was previously attached to the folio and decrements
237 * the refcount on the page.
239 * Return: Data that was attached to the folio.
241 static inline void *folio_detach_private(struct folio *folio)
243 void *data = folio_get_private(folio);
245 if (!folio_test_private(folio))
247 folio_clear_private(folio);
248 folio->private = NULL;
254 static inline void attach_page_private(struct page *page, void *data)
256 folio_attach_private(page_folio(page), data);
259 static inline void *detach_page_private(struct page *page)
261 return folio_detach_private(page_folio(page));
265 extern struct page *__page_cache_alloc(gfp_t gfp);
267 static inline struct page *__page_cache_alloc(gfp_t gfp)
269 return alloc_pages(gfp, 0);
273 static inline struct page *page_cache_alloc(struct address_space *x)
275 return __page_cache_alloc(mapping_gfp_mask(x));
278 static inline gfp_t readahead_gfp_mask(struct address_space *x)
280 return mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN;
283 typedef int filler_t(void *, struct page *);
285 pgoff_t page_cache_next_miss(struct address_space *mapping,
286 pgoff_t index, unsigned long max_scan);
287 pgoff_t page_cache_prev_miss(struct address_space *mapping,
288 pgoff_t index, unsigned long max_scan);
290 #define FGP_ACCESSED 0x00000001
291 #define FGP_LOCK 0x00000002
292 #define FGP_CREAT 0x00000004
293 #define FGP_WRITE 0x00000008
294 #define FGP_NOFS 0x00000010
295 #define FGP_NOWAIT 0x00000020
296 #define FGP_FOR_MMAP 0x00000040
297 #define FGP_HEAD 0x00000080
298 #define FGP_ENTRY 0x00000100
300 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
301 int fgp_flags, gfp_t cache_gfp_mask);
304 * find_get_page - find and get a page reference
305 * @mapping: the address_space to search
306 * @offset: the page index
308 * Looks up the page cache slot at @mapping & @offset. If there is a
309 * page cache page, it is returned with an increased refcount.
311 * Otherwise, %NULL is returned.
313 static inline struct page *find_get_page(struct address_space *mapping,
316 return pagecache_get_page(mapping, offset, 0, 0);
319 static inline struct page *find_get_page_flags(struct address_space *mapping,
320 pgoff_t offset, int fgp_flags)
322 return pagecache_get_page(mapping, offset, fgp_flags, 0);
326 * find_lock_page - locate, pin and lock a pagecache page
327 * @mapping: the address_space to search
328 * @index: the page index
330 * Looks up the page cache entry at @mapping & @index. If there is a
331 * page cache page, it is returned locked and with an increased
334 * Context: May sleep.
335 * Return: A struct page or %NULL if there is no page in the cache for this
338 static inline struct page *find_lock_page(struct address_space *mapping,
341 return pagecache_get_page(mapping, index, FGP_LOCK, 0);
345 * find_lock_head - Locate, pin and lock a pagecache page.
346 * @mapping: The address_space to search.
347 * @index: The page index.
349 * Looks up the page cache entry at @mapping & @index. If there is a
350 * page cache page, its head page is returned locked and with an increased
353 * Context: May sleep.
354 * Return: A struct page which is !PageTail, or %NULL if there is no page
355 * in the cache for this index.
357 static inline struct page *find_lock_head(struct address_space *mapping,
360 return pagecache_get_page(mapping, index, FGP_LOCK | FGP_HEAD, 0);
364 * find_or_create_page - locate or add a pagecache page
365 * @mapping: the page's address_space
366 * @index: the page's index into the mapping
367 * @gfp_mask: page allocation mode
369 * Looks up the page cache slot at @mapping & @offset. If there is a
370 * page cache page, it is returned locked and with an increased
373 * If the page is not present, a new page is allocated using @gfp_mask
374 * and added to the page cache and the VM's LRU list. The page is
375 * returned locked and with an increased refcount.
377 * On memory exhaustion, %NULL is returned.
379 * find_or_create_page() may sleep, even if @gfp_flags specifies an
382 static inline struct page *find_or_create_page(struct address_space *mapping,
383 pgoff_t index, gfp_t gfp_mask)
385 return pagecache_get_page(mapping, index,
386 FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
391 * grab_cache_page_nowait - returns locked page at given index in given cache
392 * @mapping: target address_space
393 * @index: the page index
395 * Same as grab_cache_page(), but do not wait if the page is unavailable.
396 * This is intended for speculative data generators, where the data can
397 * be regenerated if the page couldn't be grabbed. This routine should
398 * be safe to call while holding the lock for another page.
400 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
401 * and deadlock against the caller's locked page.
403 static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
406 return pagecache_get_page(mapping, index,
407 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
408 mapping_gfp_mask(mapping));
411 /* Does this page contain this index? */
412 static inline bool thp_contains(struct page *head, pgoff_t index)
414 /* HugeTLBfs indexes the page cache in units of hpage_size */
416 return head->index == index;
417 return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL));
420 #define swapcache_index(folio) __page_file_index(&(folio)->page)
423 * folio_index - File index of a folio.
426 * For a folio which is either in the page cache or the swap cache,
427 * return its index within the address_space it belongs to. If you know
428 * the page is definitely in the page cache, you can look at the folio's
431 * Return: The index (offset in units of pages) of a folio in its file.
433 static inline pgoff_t folio_index(struct folio *folio)
435 if (unlikely(folio_test_swapcache(folio)))
436 return swapcache_index(folio);
441 * folio_next_index - Get the index of the next folio.
442 * @folio: The current folio.
444 * Return: The index of the folio which follows this folio in the file.
446 static inline pgoff_t folio_next_index(struct folio *folio)
448 return folio->index + folio_nr_pages(folio);
452 * folio_file_page - The page for a particular index.
453 * @folio: The folio which contains this index.
454 * @index: The index we want to look up.
456 * Sometimes after looking up a folio in the page cache, we need to
457 * obtain the specific page for an index (eg a page fault).
459 * Return: The page containing the file data for this index.
461 static inline struct page *folio_file_page(struct folio *folio, pgoff_t index)
463 /* HugeTLBfs indexes the page cache in units of hpage_size */
464 if (folio_test_hugetlb(folio))
466 return folio_page(folio, index & (folio_nr_pages(folio) - 1));
470 * folio_contains - Does this folio contain this index?
472 * @index: The page index within the file.
474 * Context: The caller should have the page locked in order to prevent
475 * (eg) shmem from moving the page between the page cache and swap cache
476 * and changing its index in the middle of the operation.
477 * Return: true or false.
479 static inline bool folio_contains(struct folio *folio, pgoff_t index)
481 /* HugeTLBfs indexes the page cache in units of hpage_size */
482 if (folio_test_hugetlb(folio))
483 return folio->index == index;
484 return index - folio_index(folio) < folio_nr_pages(folio);
488 * Given the page we found in the page cache, return the page corresponding
489 * to this index in the file
491 static inline struct page *find_subpage(struct page *head, pgoff_t index)
493 /* HugeTLBfs wants the head page regardless */
497 return head + (index & (thp_nr_pages(head) - 1));
500 unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
501 pgoff_t end, struct pagevec *pvec, pgoff_t *indices);
502 unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
503 pgoff_t end, unsigned int nr_pages,
504 struct page **pages);
505 static inline unsigned find_get_pages(struct address_space *mapping,
506 pgoff_t *start, unsigned int nr_pages,
509 return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
512 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
513 unsigned int nr_pages, struct page **pages);
514 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
515 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
516 struct page **pages);
517 static inline unsigned find_get_pages_tag(struct address_space *mapping,
518 pgoff_t *index, xa_mark_t tag, unsigned int nr_pages,
521 return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
525 struct page *grab_cache_page_write_begin(struct address_space *mapping,
526 pgoff_t index, unsigned flags);
529 * Returns locked page at given index in given cache, creating it if needed.
531 static inline struct page *grab_cache_page(struct address_space *mapping,
534 return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
537 extern struct page * read_cache_page(struct address_space *mapping,
538 pgoff_t index, filler_t *filler, void *data);
539 extern struct page * read_cache_page_gfp(struct address_space *mapping,
540 pgoff_t index, gfp_t gfp_mask);
541 extern int read_cache_pages(struct address_space *mapping,
542 struct list_head *pages, filler_t *filler, void *data);
544 static inline struct page *read_mapping_page(struct address_space *mapping,
545 pgoff_t index, void *data)
547 return read_cache_page(mapping, index, NULL, data);
551 * Get index of the page within radix-tree (but not for hugetlb pages).
552 * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
554 static inline pgoff_t page_to_index(struct page *page)
558 if (likely(!PageTransTail(page)))
561 head = compound_head(page);
563 * We don't initialize ->index for tail pages: calculate based on
566 return head->index + page - head;
569 extern pgoff_t hugetlb_basepage_index(struct page *page);
572 * Get the offset in PAGE_SIZE (even for hugetlb pages).
573 * (TODO: hugetlb pages should have ->index in PAGE_SIZE)
575 static inline pgoff_t page_to_pgoff(struct page *page)
577 if (unlikely(PageHuge(page)))
578 return hugetlb_basepage_index(page);
579 return page_to_index(page);
583 * Return byte-offset into filesystem object for page.
585 static inline loff_t page_offset(struct page *page)
587 return ((loff_t)page->index) << PAGE_SHIFT;
590 static inline loff_t page_file_offset(struct page *page)
592 return ((loff_t)page_index(page)) << PAGE_SHIFT;
596 * folio_pos - Returns the byte position of this folio in its file.
599 static inline loff_t folio_pos(struct folio *folio)
601 return page_offset(&folio->page);
605 * folio_file_pos - Returns the byte position of this folio in its file.
608 * This differs from folio_pos() for folios which belong to a swap file.
609 * NFS is the only filesystem today which needs to use folio_file_pos().
611 static inline loff_t folio_file_pos(struct folio *folio)
613 return page_file_offset(&folio->page);
616 extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
617 unsigned long address);
619 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
620 unsigned long address)
623 if (unlikely(is_vm_hugetlb_page(vma)))
624 return linear_hugepage_index(vma, address);
625 pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
626 pgoff += vma->vm_pgoff;
630 struct wait_page_key {
636 struct wait_page_queue {
639 wait_queue_entry_t wait;
642 static inline bool wake_page_match(struct wait_page_queue *wait_page,
643 struct wait_page_key *key)
645 if (wait_page->folio != key->folio)
649 if (wait_page->bit_nr != key->bit_nr)
655 void __folio_lock(struct folio *folio);
656 int __folio_lock_killable(struct folio *folio);
657 bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
659 void unlock_page(struct page *page);
660 void folio_unlock(struct folio *folio);
662 static inline bool folio_trylock(struct folio *folio)
664 return likely(!test_and_set_bit_lock(PG_locked, folio_flags(folio, 0)));
668 * Return true if the page was successfully locked
670 static inline int trylock_page(struct page *page)
672 return folio_trylock(page_folio(page));
675 static inline void folio_lock(struct folio *folio)
678 if (!folio_trylock(folio))
683 * lock_page may only be called if we have the page's inode pinned.
685 static inline void lock_page(struct page *page)
690 folio = page_folio(page);
691 if (!folio_trylock(folio))
695 static inline int folio_lock_killable(struct folio *folio)
698 if (!folio_trylock(folio))
699 return __folio_lock_killable(folio);
704 * lock_page_killable is like lock_page but can be interrupted by fatal
705 * signals. It returns 0 if it locked the page and -EINTR if it was
706 * killed while waiting.
708 static inline int lock_page_killable(struct page *page)
710 return folio_lock_killable(page_folio(page));
714 * lock_page_or_retry - Lock the page, unless this would block and the
715 * caller indicated that it can handle a retry.
717 * Return value and mmap_lock implications depend on flags; see
718 * __folio_lock_or_retry().
720 static inline bool lock_page_or_retry(struct page *page, struct mm_struct *mm,
726 folio = page_folio(page);
727 return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags);
731 * This is exported only for folio_wait_locked/folio_wait_writeback, etc.,
732 * and should not be used directly.
734 void folio_wait_bit(struct folio *folio, int bit_nr);
735 int folio_wait_bit_killable(struct folio *folio, int bit_nr);
738 * Wait for a folio to be unlocked.
740 * This must be called with the caller "holding" the folio,
741 * ie with increased "page->count" so that the folio won't
742 * go away during the wait..
744 static inline void folio_wait_locked(struct folio *folio)
746 if (folio_test_locked(folio))
747 folio_wait_bit(folio, PG_locked);
750 static inline int folio_wait_locked_killable(struct folio *folio)
752 if (!folio_test_locked(folio))
754 return folio_wait_bit_killable(folio, PG_locked);
757 static inline void wait_on_page_locked(struct page *page)
759 folio_wait_locked(page_folio(page));
762 static inline int wait_on_page_locked_killable(struct page *page)
764 return folio_wait_locked_killable(page_folio(page));
767 int put_and_wait_on_page_locked(struct page *page, int state);
768 void wait_on_page_writeback(struct page *page);
769 void folio_wait_writeback(struct folio *folio);
770 int folio_wait_writeback_killable(struct folio *folio);
771 void end_page_writeback(struct page *page);
772 void folio_end_writeback(struct folio *folio);
773 void wait_for_stable_page(struct page *page);
774 void folio_wait_stable(struct folio *folio);
775 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
776 static inline void __set_page_dirty(struct page *page,
777 struct address_space *mapping, int warn)
779 __folio_mark_dirty(page_folio(page), mapping, warn);
781 void folio_account_cleaned(struct folio *folio, struct address_space *mapping,
782 struct bdi_writeback *wb);
783 static inline void account_page_cleaned(struct page *page,
784 struct address_space *mapping, struct bdi_writeback *wb)
786 return folio_account_cleaned(page_folio(page), mapping, wb);
788 void __folio_cancel_dirty(struct folio *folio);
789 static inline void folio_cancel_dirty(struct folio *folio)
791 /* Avoid atomic ops, locking, etc. when not actually needed. */
792 if (folio_test_dirty(folio))
793 __folio_cancel_dirty(folio);
795 static inline void cancel_dirty_page(struct page *page)
797 folio_cancel_dirty(page_folio(page));
800 int __set_page_dirty_nobuffers(struct page *page);
801 int __set_page_dirty_no_writeback(struct page *page);
803 void page_endio(struct page *page, bool is_write, int err);
805 void folio_end_private_2(struct folio *folio);
806 void folio_wait_private_2(struct folio *folio);
807 int folio_wait_private_2_killable(struct folio *folio);
810 * Add an arbitrary waiter to a page's wait queue
812 void folio_add_wait_queue(struct folio *folio, wait_queue_entry_t *waiter);
815 * Fault everything in given userspace address range in.
817 static inline int fault_in_pages_writeable(char __user *uaddr, size_t size)
819 char __user *end = uaddr + size - 1;
821 if (unlikely(size == 0))
824 if (unlikely(uaddr > end))
827 * Writing zeroes into userspace here is OK, because we know that if
828 * the zero gets there, we'll be overwriting it.
831 if (unlikely(__put_user(0, uaddr) != 0))
834 } while (uaddr <= end);
836 /* Check whether the range spilled into the next page. */
837 if (((unsigned long)uaddr & PAGE_MASK) ==
838 ((unsigned long)end & PAGE_MASK))
839 return __put_user(0, end);
844 static inline int fault_in_pages_readable(const char __user *uaddr, size_t size)
847 const char __user *end = uaddr + size - 1;
849 if (unlikely(size == 0))
852 if (unlikely(uaddr > end))
856 if (unlikely(__get_user(c, uaddr) != 0))
859 } while (uaddr <= end);
861 /* Check whether the range spilled into the next page. */
862 if (((unsigned long)uaddr & PAGE_MASK) ==
863 ((unsigned long)end & PAGE_MASK)) {
864 return __get_user(c, end);
871 int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
872 pgoff_t index, gfp_t gfp_mask);
873 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
874 pgoff_t index, gfp_t gfp_mask);
875 extern void delete_from_page_cache(struct page *page);
876 extern void __delete_from_page_cache(struct page *page, void *shadow);
877 void replace_page_cache_page(struct page *old, struct page *new);
878 void delete_from_page_cache_batch(struct address_space *mapping,
879 struct pagevec *pvec);
880 loff_t mapping_seek_hole_data(struct address_space *, loff_t start, loff_t end,
884 * Like add_to_page_cache_locked, but used to add newly allocated pages:
885 * the page is new, so we can just run __SetPageLocked() against it.
887 static inline int add_to_page_cache(struct page *page,
888 struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
892 __SetPageLocked(page);
893 error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
895 __ClearPageLocked(page);
900 * struct readahead_control - Describes a readahead request.
902 * A readahead request is for consecutive pages. Filesystems which
903 * implement the ->readahead method should call readahead_page() or
904 * readahead_page_batch() in a loop and attempt to start I/O against
905 * each page in the request.
907 * Most of the fields in this struct are private and should be accessed
908 * by the functions below.
910 * @file: The file, used primarily by network filesystems for authentication.
911 * May be NULL if invoked internally by the filesystem.
912 * @mapping: Readahead this filesystem object.
913 * @ra: File readahead state. May be NULL.
915 struct readahead_control {
917 struct address_space *mapping;
918 struct file_ra_state *ra;
919 /* private: use the readahead_* accessors instead */
921 unsigned int _nr_pages;
922 unsigned int _batch_count;
925 #define DEFINE_READAHEAD(ractl, f, r, m, i) \
926 struct readahead_control ractl = { \
933 #define VM_READAHEAD_PAGES (SZ_128K / PAGE_SIZE)
935 void page_cache_ra_unbounded(struct readahead_control *,
936 unsigned long nr_to_read, unsigned long lookahead_count);
937 void page_cache_sync_ra(struct readahead_control *, unsigned long req_count);
938 void page_cache_async_ra(struct readahead_control *, struct page *,
939 unsigned long req_count);
940 void readahead_expand(struct readahead_control *ractl,
941 loff_t new_start, size_t new_len);
944 * page_cache_sync_readahead - generic file readahead
945 * @mapping: address_space which holds the pagecache and I/O vectors
946 * @ra: file_ra_state which holds the readahead state
947 * @file: Used by the filesystem for authentication.
948 * @index: Index of first page to be read.
949 * @req_count: Total number of pages being read by the caller.
951 * page_cache_sync_readahead() should be called when a cache miss happened:
952 * it will submit the read. The readahead logic may decide to piggyback more
953 * pages onto the read request if access patterns suggest it will improve
957 void page_cache_sync_readahead(struct address_space *mapping,
958 struct file_ra_state *ra, struct file *file, pgoff_t index,
959 unsigned long req_count)
961 DEFINE_READAHEAD(ractl, file, ra, mapping, index);
962 page_cache_sync_ra(&ractl, req_count);
966 * page_cache_async_readahead - file readahead for marked pages
967 * @mapping: address_space which holds the pagecache and I/O vectors
968 * @ra: file_ra_state which holds the readahead state
969 * @file: Used by the filesystem for authentication.
970 * @page: The page at @index which triggered the readahead call.
971 * @index: Index of first page to be read.
972 * @req_count: Total number of pages being read by the caller.
974 * page_cache_async_readahead() should be called when a page is used which
975 * is marked as PageReadahead; this is a marker to suggest that the application
976 * has used up enough of the readahead window that we should start pulling in
980 void page_cache_async_readahead(struct address_space *mapping,
981 struct file_ra_state *ra, struct file *file,
982 struct page *page, pgoff_t index, unsigned long req_count)
984 DEFINE_READAHEAD(ractl, file, ra, mapping, index);
985 page_cache_async_ra(&ractl, page, req_count);
989 * readahead_page - Get the next page to read.
990 * @rac: The current readahead request.
992 * Context: The page is locked and has an elevated refcount. The caller
993 * should decreases the refcount once the page has been submitted for I/O
994 * and unlock the page once all I/O to that page has completed.
995 * Return: A pointer to the next page, or %NULL if we are done.
997 static inline struct page *readahead_page(struct readahead_control *rac)
1001 BUG_ON(rac->_batch_count > rac->_nr_pages);
1002 rac->_nr_pages -= rac->_batch_count;
1003 rac->_index += rac->_batch_count;
1005 if (!rac->_nr_pages) {
1006 rac->_batch_count = 0;
1010 page = xa_load(&rac->mapping->i_pages, rac->_index);
1011 VM_BUG_ON_PAGE(!PageLocked(page), page);
1012 rac->_batch_count = thp_nr_pages(page);
1017 static inline unsigned int __readahead_batch(struct readahead_control *rac,
1018 struct page **array, unsigned int array_sz)
1021 XA_STATE(xas, &rac->mapping->i_pages, 0);
1024 BUG_ON(rac->_batch_count > rac->_nr_pages);
1025 rac->_nr_pages -= rac->_batch_count;
1026 rac->_index += rac->_batch_count;
1027 rac->_batch_count = 0;
1029 xas_set(&xas, rac->_index);
1031 xas_for_each(&xas, page, rac->_index + rac->_nr_pages - 1) {
1032 if (xas_retry(&xas, page))
1034 VM_BUG_ON_PAGE(!PageLocked(page), page);
1035 VM_BUG_ON_PAGE(PageTail(page), page);
1037 rac->_batch_count += thp_nr_pages(page);
1040 * The page cache isn't using multi-index entries yet,
1041 * so the xas cursor needs to be manually moved to the
1042 * next index. This can be removed once the page cache
1046 xas_set(&xas, rac->_index + rac->_batch_count);
1057 * readahead_page_batch - Get a batch of pages to read.
1058 * @rac: The current readahead request.
1059 * @array: An array of pointers to struct page.
1061 * Context: The pages are locked and have an elevated refcount. The caller
1062 * should decreases the refcount once the page has been submitted for I/O
1063 * and unlock the page once all I/O to that page has completed.
1064 * Return: The number of pages placed in the array. 0 indicates the request
1067 #define readahead_page_batch(rac, array) \
1068 __readahead_batch(rac, array, ARRAY_SIZE(array))
1071 * readahead_pos - The byte offset into the file of this readahead request.
1072 * @rac: The readahead request.
1074 static inline loff_t readahead_pos(struct readahead_control *rac)
1076 return (loff_t)rac->_index * PAGE_SIZE;
1080 * readahead_length - The number of bytes in this readahead request.
1081 * @rac: The readahead request.
1083 static inline size_t readahead_length(struct readahead_control *rac)
1085 return rac->_nr_pages * PAGE_SIZE;
1089 * readahead_index - The index of the first page in this readahead request.
1090 * @rac: The readahead request.
1092 static inline pgoff_t readahead_index(struct readahead_control *rac)
1098 * readahead_count - The number of pages in this readahead request.
1099 * @rac: The readahead request.
1101 static inline unsigned int readahead_count(struct readahead_control *rac)
1103 return rac->_nr_pages;
1107 * readahead_batch_length - The number of bytes in the current batch.
1108 * @rac: The readahead request.
1110 static inline size_t readahead_batch_length(struct readahead_control *rac)
1112 return rac->_batch_count * PAGE_SIZE;
1115 static inline unsigned long dir_pages(struct inode *inode)
1117 return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
1122 * page_mkwrite_check_truncate - check if page was truncated
1123 * @page: the page to check
1124 * @inode: the inode to check the page against
1126 * Returns the number of bytes in the page up to EOF,
1127 * or -EFAULT if the page was truncated.
1129 static inline int page_mkwrite_check_truncate(struct page *page,
1130 struct inode *inode)
1132 loff_t size = i_size_read(inode);
1133 pgoff_t index = size >> PAGE_SHIFT;
1134 int offset = offset_in_page(size);
1136 if (page->mapping != inode->i_mapping)
1139 /* page is wholly inside EOF */
1140 if (page->index < index)
1142 /* page is wholly past EOF */
1143 if (page->index > index || !offset)
1145 /* page is partially inside EOF */
1150 * i_blocks_per_page - How many blocks fit in this page.
1151 * @inode: The inode which contains the blocks.
1152 * @page: The page (head page if the page is a THP).
1154 * If the block size is larger than the size of this page, return zero.
1156 * Context: The caller should hold a refcount on the page to prevent it
1158 * Return: The number of filesystem blocks covered by this page.
1161 unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
1163 return thp_size(page) >> inode->i_blkbits;
1165 #endif /* _LINUX_PAGEMAP_H */