mm/filemap: Add filemap_get_folio
[linux-2.6-microblaze.git] / include / linux / pagemap.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGEMAP_H
3 #define _LINUX_PAGEMAP_H
4
5 /*
6  * Copyright 1995 Linus Torvalds
7  */
8 #include <linux/mm.h>
9 #include <linux/fs.h>
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>
18
19 struct pagevec;
20
21 static inline bool mapping_empty(struct address_space *mapping)
22 {
23         return xa_empty(&mapping->i_pages);
24 }
25
26 /*
27  * Bits in mapping->flags.
28  */
29 enum 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 */
38 };
39
40 /**
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
44  *
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.
49  *
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).
53  */
54 static inline void mapping_set_error(struct address_space *mapping, int error)
55 {
56         if (likely(!error))
57                 return;
58
59         /* Record in wb_err for checkers using errseq_t based tracking */
60         __filemap_set_wb_err(mapping, error);
61
62         /* Record it in superblock */
63         if (mapping->host)
64                 errseq_set(&mapping->host->i_sb->s_wb_err, error);
65
66         /* Record it in flags for now, for legacy callers */
67         if (error == -ENOSPC)
68                 set_bit(AS_ENOSPC, &mapping->flags);
69         else
70                 set_bit(AS_EIO, &mapping->flags);
71 }
72
73 static inline void mapping_set_unevictable(struct address_space *mapping)
74 {
75         set_bit(AS_UNEVICTABLE, &mapping->flags);
76 }
77
78 static inline void mapping_clear_unevictable(struct address_space *mapping)
79 {
80         clear_bit(AS_UNEVICTABLE, &mapping->flags);
81 }
82
83 static inline bool mapping_unevictable(struct address_space *mapping)
84 {
85         return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags);
86 }
87
88 static inline void mapping_set_exiting(struct address_space *mapping)
89 {
90         set_bit(AS_EXITING, &mapping->flags);
91 }
92
93 static inline int mapping_exiting(struct address_space *mapping)
94 {
95         return test_bit(AS_EXITING, &mapping->flags);
96 }
97
98 static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
99 {
100         set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
101 }
102
103 static inline int mapping_use_writeback_tags(struct address_space *mapping)
104 {
105         return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
106 }
107
108 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
109 {
110         return mapping->gfp_mask;
111 }
112
113 /* Restricts the given gfp_mask to what the mapping allows. */
114 static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
115                 gfp_t gfp_mask)
116 {
117         return mapping_gfp_mask(mapping) & gfp_mask;
118 }
119
120 /*
121  * This is non-atomic.  Only to be used before the mapping is activated.
122  * Probably needs a barrier...
123  */
124 static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
125 {
126         m->gfp_mask = mask;
127 }
128
129 static inline bool mapping_thp_support(struct address_space *mapping)
130 {
131         return test_bit(AS_THP_SUPPORT, &mapping->flags);
132 }
133
134 static inline int filemap_nr_thps(struct address_space *mapping)
135 {
136 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
137         return atomic_read(&mapping->nr_thps);
138 #else
139         return 0;
140 #endif
141 }
142
143 static inline void filemap_nr_thps_inc(struct address_space *mapping)
144 {
145 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
146         if (!mapping_thp_support(mapping))
147                 atomic_inc(&mapping->nr_thps);
148 #else
149         WARN_ON_ONCE(1);
150 #endif
151 }
152
153 static inline void filemap_nr_thps_dec(struct address_space *mapping)
154 {
155 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
156         if (!mapping_thp_support(mapping))
157                 atomic_dec(&mapping->nr_thps);
158 #else
159         WARN_ON_ONCE(1);
160 #endif
161 }
162
163 void release_pages(struct page **pages, int nr);
164
165 struct address_space *page_mapping(struct page *);
166 struct address_space *folio_mapping(struct folio *);
167 struct address_space *swapcache_mapping(struct folio *);
168
169 /**
170  * folio_file_mapping - Find the mapping this folio belongs to.
171  * @folio: The folio.
172  *
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.
178  *
179  * Do not call this for folios which aren't in the page cache or swap cache.
180  */
181 static inline struct address_space *folio_file_mapping(struct folio *folio)
182 {
183         if (unlikely(folio_test_swapcache(folio)))
184                 return swapcache_mapping(folio);
185
186         return folio->mapping;
187 }
188
189 static inline struct address_space *page_file_mapping(struct page *page)
190 {
191         return folio_file_mapping(page_folio(page));
192 }
193
194 /*
195  * For file cache pages, return the address_space, otherwise return NULL
196  */
197 static inline struct address_space *page_mapping_file(struct page *page)
198 {
199         struct folio *folio = page_folio(page);
200
201         if (unlikely(folio_test_swapcache(folio)))
202                 return NULL;
203         return folio_mapping(folio);
204 }
205
206 static inline bool page_cache_add_speculative(struct page *page, int count)
207 {
208         VM_BUG_ON_PAGE(PageTail(page), page);
209         return folio_ref_try_add_rcu((struct folio *)page, count);
210 }
211
212 static inline bool page_cache_get_speculative(struct page *page)
213 {
214         return page_cache_add_speculative(page, 1);
215 }
216
217 /**
218  * folio_attach_private - Attach private data to a folio.
219  * @folio: Folio to attach data to.
220  * @data: Data to attach to folio.
221  *
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.
224  */
225 static inline void folio_attach_private(struct folio *folio, void *data)
226 {
227         folio_get(folio);
228         folio->private = data;
229         folio_set_private(folio);
230 }
231
232 /**
233  * folio_detach_private - Detach private data from a folio.
234  * @folio: Folio to detach data from.
235  *
236  * Removes the data that was previously attached to the folio and decrements
237  * the refcount on the page.
238  *
239  * Return: Data that was attached to the folio.
240  */
241 static inline void *folio_detach_private(struct folio *folio)
242 {
243         void *data = folio_get_private(folio);
244
245         if (!folio_test_private(folio))
246                 return NULL;
247         folio_clear_private(folio);
248         folio->private = NULL;
249         folio_put(folio);
250
251         return data;
252 }
253
254 static inline void attach_page_private(struct page *page, void *data)
255 {
256         folio_attach_private(page_folio(page), data);
257 }
258
259 static inline void *detach_page_private(struct page *page)
260 {
261         return folio_detach_private(page_folio(page));
262 }
263
264 #ifdef CONFIG_NUMA
265 struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order);
266 #else
267 static inline struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order)
268 {
269         return folio_alloc(gfp, order);
270 }
271 #endif
272
273 static inline struct page *__page_cache_alloc(gfp_t gfp)
274 {
275         return &filemap_alloc_folio(gfp, 0)->page;
276 }
277
278 static inline struct page *page_cache_alloc(struct address_space *x)
279 {
280         return __page_cache_alloc(mapping_gfp_mask(x));
281 }
282
283 static inline gfp_t readahead_gfp_mask(struct address_space *x)
284 {
285         return mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN;
286 }
287
288 typedef int filler_t(void *, struct page *);
289
290 pgoff_t page_cache_next_miss(struct address_space *mapping,
291                              pgoff_t index, unsigned long max_scan);
292 pgoff_t page_cache_prev_miss(struct address_space *mapping,
293                              pgoff_t index, unsigned long max_scan);
294
295 #define FGP_ACCESSED            0x00000001
296 #define FGP_LOCK                0x00000002
297 #define FGP_CREAT               0x00000004
298 #define FGP_WRITE               0x00000008
299 #define FGP_NOFS                0x00000010
300 #define FGP_NOWAIT              0x00000020
301 #define FGP_FOR_MMAP            0x00000040
302 #define FGP_HEAD                0x00000080
303 #define FGP_ENTRY               0x00000100
304
305 struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
306                 int fgp_flags, gfp_t gfp);
307 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
308                 int fgp_flags, gfp_t gfp);
309
310 /**
311  * filemap_get_folio - Find and get a folio.
312  * @mapping: The address_space to search.
313  * @index: The page index.
314  *
315  * Looks up the page cache entry at @mapping & @index.  If a folio is
316  * present, it is returned with an increased refcount.
317  *
318  * Otherwise, %NULL is returned.
319  */
320 static inline struct folio *filemap_get_folio(struct address_space *mapping,
321                                         pgoff_t index)
322 {
323         return __filemap_get_folio(mapping, index, 0, 0);
324 }
325
326 /**
327  * find_get_page - find and get a page reference
328  * @mapping: the address_space to search
329  * @offset: the page index
330  *
331  * Looks up the page cache slot at @mapping & @offset.  If there is a
332  * page cache page, it is returned with an increased refcount.
333  *
334  * Otherwise, %NULL is returned.
335  */
336 static inline struct page *find_get_page(struct address_space *mapping,
337                                         pgoff_t offset)
338 {
339         return pagecache_get_page(mapping, offset, 0, 0);
340 }
341
342 static inline struct page *find_get_page_flags(struct address_space *mapping,
343                                         pgoff_t offset, int fgp_flags)
344 {
345         return pagecache_get_page(mapping, offset, fgp_flags, 0);
346 }
347
348 /**
349  * find_lock_page - locate, pin and lock a pagecache page
350  * @mapping: the address_space to search
351  * @index: the page index
352  *
353  * Looks up the page cache entry at @mapping & @index.  If there is a
354  * page cache page, it is returned locked and with an increased
355  * refcount.
356  *
357  * Context: May sleep.
358  * Return: A struct page or %NULL if there is no page in the cache for this
359  * index.
360  */
361 static inline struct page *find_lock_page(struct address_space *mapping,
362                                         pgoff_t index)
363 {
364         return pagecache_get_page(mapping, index, FGP_LOCK, 0);
365 }
366
367 /**
368  * find_or_create_page - locate or add a pagecache page
369  * @mapping: the page's address_space
370  * @index: the page's index into the mapping
371  * @gfp_mask: page allocation mode
372  *
373  * Looks up the page cache slot at @mapping & @offset.  If there is a
374  * page cache page, it is returned locked and with an increased
375  * refcount.
376  *
377  * If the page is not present, a new page is allocated using @gfp_mask
378  * and added to the page cache and the VM's LRU list.  The page is
379  * returned locked and with an increased refcount.
380  *
381  * On memory exhaustion, %NULL is returned.
382  *
383  * find_or_create_page() may sleep, even if @gfp_flags specifies an
384  * atomic allocation!
385  */
386 static inline struct page *find_or_create_page(struct address_space *mapping,
387                                         pgoff_t index, gfp_t gfp_mask)
388 {
389         return pagecache_get_page(mapping, index,
390                                         FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
391                                         gfp_mask);
392 }
393
394 /**
395  * grab_cache_page_nowait - returns locked page at given index in given cache
396  * @mapping: target address_space
397  * @index: the page index
398  *
399  * Same as grab_cache_page(), but do not wait if the page is unavailable.
400  * This is intended for speculative data generators, where the data can
401  * be regenerated if the page couldn't be grabbed.  This routine should
402  * be safe to call while holding the lock for another page.
403  *
404  * Clear __GFP_FS when allocating the page to avoid recursion into the fs
405  * and deadlock against the caller's locked page.
406  */
407 static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
408                                 pgoff_t index)
409 {
410         return pagecache_get_page(mapping, index,
411                         FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
412                         mapping_gfp_mask(mapping));
413 }
414
415 /* Does this page contain this index? */
416 static inline bool thp_contains(struct page *head, pgoff_t index)
417 {
418         /* HugeTLBfs indexes the page cache in units of hpage_size */
419         if (PageHuge(head))
420                 return head->index == index;
421         return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL));
422 }
423
424 #define swapcache_index(folio)  __page_file_index(&(folio)->page)
425
426 /**
427  * folio_index - File index of a folio.
428  * @folio: The folio.
429  *
430  * For a folio which is either in the page cache or the swap cache,
431  * return its index within the address_space it belongs to.  If you know
432  * the page is definitely in the page cache, you can look at the folio's
433  * index directly.
434  *
435  * Return: The index (offset in units of pages) of a folio in its file.
436  */
437 static inline pgoff_t folio_index(struct folio *folio)
438 {
439         if (unlikely(folio_test_swapcache(folio)))
440                 return swapcache_index(folio);
441         return folio->index;
442 }
443
444 /**
445  * folio_next_index - Get the index of the next folio.
446  * @folio: The current folio.
447  *
448  * Return: The index of the folio which follows this folio in the file.
449  */
450 static inline pgoff_t folio_next_index(struct folio *folio)
451 {
452         return folio->index + folio_nr_pages(folio);
453 }
454
455 /**
456  * folio_file_page - The page for a particular index.
457  * @folio: The folio which contains this index.
458  * @index: The index we want to look up.
459  *
460  * Sometimes after looking up a folio in the page cache, we need to
461  * obtain the specific page for an index (eg a page fault).
462  *
463  * Return: The page containing the file data for this index.
464  */
465 static inline struct page *folio_file_page(struct folio *folio, pgoff_t index)
466 {
467         /* HugeTLBfs indexes the page cache in units of hpage_size */
468         if (folio_test_hugetlb(folio))
469                 return &folio->page;
470         return folio_page(folio, index & (folio_nr_pages(folio) - 1));
471 }
472
473 /**
474  * folio_contains - Does this folio contain this index?
475  * @folio: The folio.
476  * @index: The page index within the file.
477  *
478  * Context: The caller should have the page locked in order to prevent
479  * (eg) shmem from moving the page between the page cache and swap cache
480  * and changing its index in the middle of the operation.
481  * Return: true or false.
482  */
483 static inline bool folio_contains(struct folio *folio, pgoff_t index)
484 {
485         /* HugeTLBfs indexes the page cache in units of hpage_size */
486         if (folio_test_hugetlb(folio))
487                 return folio->index == index;
488         return index - folio_index(folio) < folio_nr_pages(folio);
489 }
490
491 /*
492  * Given the page we found in the page cache, return the page corresponding
493  * to this index in the file
494  */
495 static inline struct page *find_subpage(struct page *head, pgoff_t index)
496 {
497         /* HugeTLBfs wants the head page regardless */
498         if (PageHuge(head))
499                 return head;
500
501         return head + (index & (thp_nr_pages(head) - 1));
502 }
503
504 unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
505                 pgoff_t end, struct pagevec *pvec, pgoff_t *indices);
506 unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
507                         pgoff_t end, unsigned int nr_pages,
508                         struct page **pages);
509 static inline unsigned find_get_pages(struct address_space *mapping,
510                         pgoff_t *start, unsigned int nr_pages,
511                         struct page **pages)
512 {
513         return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
514                                     pages);
515 }
516 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
517                                unsigned int nr_pages, struct page **pages);
518 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
519                         pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
520                         struct page **pages);
521 static inline unsigned find_get_pages_tag(struct address_space *mapping,
522                         pgoff_t *index, xa_mark_t tag, unsigned int nr_pages,
523                         struct page **pages)
524 {
525         return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
526                                         nr_pages, pages);
527 }
528
529 struct page *grab_cache_page_write_begin(struct address_space *mapping,
530                         pgoff_t index, unsigned flags);
531
532 /*
533  * Returns locked page at given index in given cache, creating it if needed.
534  */
535 static inline struct page *grab_cache_page(struct address_space *mapping,
536                                                                 pgoff_t index)
537 {
538         return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
539 }
540
541 extern struct page * read_cache_page(struct address_space *mapping,
542                                 pgoff_t index, filler_t *filler, void *data);
543 extern struct page * read_cache_page_gfp(struct address_space *mapping,
544                                 pgoff_t index, gfp_t gfp_mask);
545 extern int read_cache_pages(struct address_space *mapping,
546                 struct list_head *pages, filler_t *filler, void *data);
547
548 static inline struct page *read_mapping_page(struct address_space *mapping,
549                                 pgoff_t index, void *data)
550 {
551         return read_cache_page(mapping, index, NULL, data);
552 }
553
554 /*
555  * Get index of the page within radix-tree (but not for hugetlb pages).
556  * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
557  */
558 static inline pgoff_t page_to_index(struct page *page)
559 {
560         struct page *head;
561
562         if (likely(!PageTransTail(page)))
563                 return page->index;
564
565         head = compound_head(page);
566         /*
567          *  We don't initialize ->index for tail pages: calculate based on
568          *  head page
569          */
570         return head->index + page - head;
571 }
572
573 extern pgoff_t hugetlb_basepage_index(struct page *page);
574
575 /*
576  * Get the offset in PAGE_SIZE (even for hugetlb pages).
577  * (TODO: hugetlb pages should have ->index in PAGE_SIZE)
578  */
579 static inline pgoff_t page_to_pgoff(struct page *page)
580 {
581         if (unlikely(PageHuge(page)))
582                 return hugetlb_basepage_index(page);
583         return page_to_index(page);
584 }
585
586 /*
587  * Return byte-offset into filesystem object for page.
588  */
589 static inline loff_t page_offset(struct page *page)
590 {
591         return ((loff_t)page->index) << PAGE_SHIFT;
592 }
593
594 static inline loff_t page_file_offset(struct page *page)
595 {
596         return ((loff_t)page_index(page)) << PAGE_SHIFT;
597 }
598
599 /**
600  * folio_pos - Returns the byte position of this folio in its file.
601  * @folio: The folio.
602  */
603 static inline loff_t folio_pos(struct folio *folio)
604 {
605         return page_offset(&folio->page);
606 }
607
608 /**
609  * folio_file_pos - Returns the byte position of this folio in its file.
610  * @folio: The folio.
611  *
612  * This differs from folio_pos() for folios which belong to a swap file.
613  * NFS is the only filesystem today which needs to use folio_file_pos().
614  */
615 static inline loff_t folio_file_pos(struct folio *folio)
616 {
617         return page_file_offset(&folio->page);
618 }
619
620 extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
621                                      unsigned long address);
622
623 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
624                                         unsigned long address)
625 {
626         pgoff_t pgoff;
627         if (unlikely(is_vm_hugetlb_page(vma)))
628                 return linear_hugepage_index(vma, address);
629         pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
630         pgoff += vma->vm_pgoff;
631         return pgoff;
632 }
633
634 struct wait_page_key {
635         struct folio *folio;
636         int bit_nr;
637         int page_match;
638 };
639
640 struct wait_page_queue {
641         struct folio *folio;
642         int bit_nr;
643         wait_queue_entry_t wait;
644 };
645
646 static inline bool wake_page_match(struct wait_page_queue *wait_page,
647                                   struct wait_page_key *key)
648 {
649         if (wait_page->folio != key->folio)
650                return false;
651         key->page_match = 1;
652
653         if (wait_page->bit_nr != key->bit_nr)
654                 return false;
655
656         return true;
657 }
658
659 void __folio_lock(struct folio *folio);
660 int __folio_lock_killable(struct folio *folio);
661 bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
662                                 unsigned int flags);
663 void unlock_page(struct page *page);
664 void folio_unlock(struct folio *folio);
665
666 static inline bool folio_trylock(struct folio *folio)
667 {
668         return likely(!test_and_set_bit_lock(PG_locked, folio_flags(folio, 0)));
669 }
670
671 /*
672  * Return true if the page was successfully locked
673  */
674 static inline int trylock_page(struct page *page)
675 {
676         return folio_trylock(page_folio(page));
677 }
678
679 static inline void folio_lock(struct folio *folio)
680 {
681         might_sleep();
682         if (!folio_trylock(folio))
683                 __folio_lock(folio);
684 }
685
686 /*
687  * lock_page may only be called if we have the page's inode pinned.
688  */
689 static inline void lock_page(struct page *page)
690 {
691         struct folio *folio;
692         might_sleep();
693
694         folio = page_folio(page);
695         if (!folio_trylock(folio))
696                 __folio_lock(folio);
697 }
698
699 static inline int folio_lock_killable(struct folio *folio)
700 {
701         might_sleep();
702         if (!folio_trylock(folio))
703                 return __folio_lock_killable(folio);
704         return 0;
705 }
706
707 /*
708  * lock_page_killable is like lock_page but can be interrupted by fatal
709  * signals.  It returns 0 if it locked the page and -EINTR if it was
710  * killed while waiting.
711  */
712 static inline int lock_page_killable(struct page *page)
713 {
714         return folio_lock_killable(page_folio(page));
715 }
716
717 /*
718  * lock_page_or_retry - Lock the page, unless this would block and the
719  * caller indicated that it can handle a retry.
720  *
721  * Return value and mmap_lock implications depend on flags; see
722  * __folio_lock_or_retry().
723  */
724 static inline bool lock_page_or_retry(struct page *page, struct mm_struct *mm,
725                                      unsigned int flags)
726 {
727         struct folio *folio;
728         might_sleep();
729
730         folio = page_folio(page);
731         return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags);
732 }
733
734 /*
735  * This is exported only for folio_wait_locked/folio_wait_writeback, etc.,
736  * and should not be used directly.
737  */
738 void folio_wait_bit(struct folio *folio, int bit_nr);
739 int folio_wait_bit_killable(struct folio *folio, int bit_nr);
740
741 /* 
742  * Wait for a folio to be unlocked.
743  *
744  * This must be called with the caller "holding" the folio,
745  * ie with increased "page->count" so that the folio won't
746  * go away during the wait..
747  */
748 static inline void folio_wait_locked(struct folio *folio)
749 {
750         if (folio_test_locked(folio))
751                 folio_wait_bit(folio, PG_locked);
752 }
753
754 static inline int folio_wait_locked_killable(struct folio *folio)
755 {
756         if (!folio_test_locked(folio))
757                 return 0;
758         return folio_wait_bit_killable(folio, PG_locked);
759 }
760
761 static inline void wait_on_page_locked(struct page *page)
762 {
763         folio_wait_locked(page_folio(page));
764 }
765
766 static inline int wait_on_page_locked_killable(struct page *page)
767 {
768         return folio_wait_locked_killable(page_folio(page));
769 }
770
771 int put_and_wait_on_page_locked(struct page *page, int state);
772 void wait_on_page_writeback(struct page *page);
773 void folio_wait_writeback(struct folio *folio);
774 int folio_wait_writeback_killable(struct folio *folio);
775 void end_page_writeback(struct page *page);
776 void folio_end_writeback(struct folio *folio);
777 void wait_for_stable_page(struct page *page);
778 void folio_wait_stable(struct folio *folio);
779 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
780 static inline void __set_page_dirty(struct page *page,
781                 struct address_space *mapping, int warn)
782 {
783         __folio_mark_dirty(page_folio(page), mapping, warn);
784 }
785 void folio_account_cleaned(struct folio *folio, struct address_space *mapping,
786                           struct bdi_writeback *wb);
787 static inline void account_page_cleaned(struct page *page,
788                 struct address_space *mapping, struct bdi_writeback *wb)
789 {
790         return folio_account_cleaned(page_folio(page), mapping, wb);
791 }
792 void __folio_cancel_dirty(struct folio *folio);
793 static inline void folio_cancel_dirty(struct folio *folio)
794 {
795         /* Avoid atomic ops, locking, etc. when not actually needed. */
796         if (folio_test_dirty(folio))
797                 __folio_cancel_dirty(folio);
798 }
799 static inline void cancel_dirty_page(struct page *page)
800 {
801         folio_cancel_dirty(page_folio(page));
802 }
803 bool folio_clear_dirty_for_io(struct folio *folio);
804 bool clear_page_dirty_for_io(struct page *page);
805
806 int __set_page_dirty_nobuffers(struct page *page);
807 int __set_page_dirty_no_writeback(struct page *page);
808
809 void page_endio(struct page *page, bool is_write, int err);
810
811 void folio_end_private_2(struct folio *folio);
812 void folio_wait_private_2(struct folio *folio);
813 int folio_wait_private_2_killable(struct folio *folio);
814
815 /*
816  * Add an arbitrary waiter to a page's wait queue
817  */
818 void folio_add_wait_queue(struct folio *folio, wait_queue_entry_t *waiter);
819
820 /*
821  * Fault everything in given userspace address range in.
822  */
823 static inline int fault_in_pages_writeable(char __user *uaddr, size_t size)
824 {
825         char __user *end = uaddr + size - 1;
826
827         if (unlikely(size == 0))
828                 return 0;
829
830         if (unlikely(uaddr > end))
831                 return -EFAULT;
832         /*
833          * Writing zeroes into userspace here is OK, because we know that if
834          * the zero gets there, we'll be overwriting it.
835          */
836         do {
837                 if (unlikely(__put_user(0, uaddr) != 0))
838                         return -EFAULT;
839                 uaddr += PAGE_SIZE;
840         } while (uaddr <= end);
841
842         /* Check whether the range spilled into the next page. */
843         if (((unsigned long)uaddr & PAGE_MASK) ==
844                         ((unsigned long)end & PAGE_MASK))
845                 return __put_user(0, end);
846
847         return 0;
848 }
849
850 static inline int fault_in_pages_readable(const char __user *uaddr, size_t size)
851 {
852         volatile char c;
853         const char __user *end = uaddr + size - 1;
854
855         if (unlikely(size == 0))
856                 return 0;
857
858         if (unlikely(uaddr > end))
859                 return -EFAULT;
860
861         do {
862                 if (unlikely(__get_user(c, uaddr) != 0))
863                         return -EFAULT;
864                 uaddr += PAGE_SIZE;
865         } while (uaddr <= end);
866
867         /* Check whether the range spilled into the next page. */
868         if (((unsigned long)uaddr & PAGE_MASK) ==
869                         ((unsigned long)end & PAGE_MASK)) {
870                 return __get_user(c, end);
871         }
872
873         (void)c;
874         return 0;
875 }
876
877 int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
878                 pgoff_t index, gfp_t gfp);
879 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
880                 pgoff_t index, gfp_t gfp);
881 int filemap_add_folio(struct address_space *mapping, struct folio *folio,
882                 pgoff_t index, gfp_t gfp);
883 extern void delete_from_page_cache(struct page *page);
884 extern void __delete_from_page_cache(struct page *page, void *shadow);
885 void replace_page_cache_page(struct page *old, struct page *new);
886 void delete_from_page_cache_batch(struct address_space *mapping,
887                                   struct pagevec *pvec);
888 loff_t mapping_seek_hole_data(struct address_space *, loff_t start, loff_t end,
889                 int whence);
890
891 /*
892  * Like add_to_page_cache_locked, but used to add newly allocated pages:
893  * the page is new, so we can just run __SetPageLocked() against it.
894  */
895 static inline int add_to_page_cache(struct page *page,
896                 struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
897 {
898         int error;
899
900         __SetPageLocked(page);
901         error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
902         if (unlikely(error))
903                 __ClearPageLocked(page);
904         return error;
905 }
906
907 /* Must be non-static for BPF error injection */
908 int __filemap_add_folio(struct address_space *mapping, struct folio *folio,
909                 pgoff_t index, gfp_t gfp, void **shadowp);
910
911 /**
912  * struct readahead_control - Describes a readahead request.
913  *
914  * A readahead request is for consecutive pages.  Filesystems which
915  * implement the ->readahead method should call readahead_page() or
916  * readahead_page_batch() in a loop and attempt to start I/O against
917  * each page in the request.
918  *
919  * Most of the fields in this struct are private and should be accessed
920  * by the functions below.
921  *
922  * @file: The file, used primarily by network filesystems for authentication.
923  *        May be NULL if invoked internally by the filesystem.
924  * @mapping: Readahead this filesystem object.
925  * @ra: File readahead state.  May be NULL.
926  */
927 struct readahead_control {
928         struct file *file;
929         struct address_space *mapping;
930         struct file_ra_state *ra;
931 /* private: use the readahead_* accessors instead */
932         pgoff_t _index;
933         unsigned int _nr_pages;
934         unsigned int _batch_count;
935 };
936
937 #define DEFINE_READAHEAD(ractl, f, r, m, i)                             \
938         struct readahead_control ractl = {                              \
939                 .file = f,                                              \
940                 .mapping = m,                                           \
941                 .ra = r,                                                \
942                 ._index = i,                                            \
943         }
944
945 #define VM_READAHEAD_PAGES      (SZ_128K / PAGE_SIZE)
946
947 void page_cache_ra_unbounded(struct readahead_control *,
948                 unsigned long nr_to_read, unsigned long lookahead_count);
949 void page_cache_sync_ra(struct readahead_control *, unsigned long req_count);
950 void page_cache_async_ra(struct readahead_control *, struct page *,
951                 unsigned long req_count);
952 void readahead_expand(struct readahead_control *ractl,
953                       loff_t new_start, size_t new_len);
954
955 /**
956  * page_cache_sync_readahead - generic file readahead
957  * @mapping: address_space which holds the pagecache and I/O vectors
958  * @ra: file_ra_state which holds the readahead state
959  * @file: Used by the filesystem for authentication.
960  * @index: Index of first page to be read.
961  * @req_count: Total number of pages being read by the caller.
962  *
963  * page_cache_sync_readahead() should be called when a cache miss happened:
964  * it will submit the read.  The readahead logic may decide to piggyback more
965  * pages onto the read request if access patterns suggest it will improve
966  * performance.
967  */
968 static inline
969 void page_cache_sync_readahead(struct address_space *mapping,
970                 struct file_ra_state *ra, struct file *file, pgoff_t index,
971                 unsigned long req_count)
972 {
973         DEFINE_READAHEAD(ractl, file, ra, mapping, index);
974         page_cache_sync_ra(&ractl, req_count);
975 }
976
977 /**
978  * page_cache_async_readahead - file readahead for marked pages
979  * @mapping: address_space which holds the pagecache and I/O vectors
980  * @ra: file_ra_state which holds the readahead state
981  * @file: Used by the filesystem for authentication.
982  * @page: The page at @index which triggered the readahead call.
983  * @index: Index of first page to be read.
984  * @req_count: Total number of pages being read by the caller.
985  *
986  * page_cache_async_readahead() should be called when a page is used which
987  * is marked as PageReadahead; this is a marker to suggest that the application
988  * has used up enough of the readahead window that we should start pulling in
989  * more pages.
990  */
991 static inline
992 void page_cache_async_readahead(struct address_space *mapping,
993                 struct file_ra_state *ra, struct file *file,
994                 struct page *page, pgoff_t index, unsigned long req_count)
995 {
996         DEFINE_READAHEAD(ractl, file, ra, mapping, index);
997         page_cache_async_ra(&ractl, page, req_count);
998 }
999
1000 static inline struct folio *__readahead_folio(struct readahead_control *ractl)
1001 {
1002         struct folio *folio;
1003
1004         BUG_ON(ractl->_batch_count > ractl->_nr_pages);
1005         ractl->_nr_pages -= ractl->_batch_count;
1006         ractl->_index += ractl->_batch_count;
1007
1008         if (!ractl->_nr_pages) {
1009                 ractl->_batch_count = 0;
1010                 return NULL;
1011         }
1012
1013         folio = xa_load(&ractl->mapping->i_pages, ractl->_index);
1014         VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
1015         ractl->_batch_count = folio_nr_pages(folio);
1016
1017         return folio;
1018 }
1019
1020 /**
1021  * readahead_page - Get the next page to read.
1022  * @ractl: The current readahead request.
1023  *
1024  * Context: The page is locked and has an elevated refcount.  The caller
1025  * should decreases the refcount once the page has been submitted for I/O
1026  * and unlock the page once all I/O to that page has completed.
1027  * Return: A pointer to the next page, or %NULL if we are done.
1028  */
1029 static inline struct page *readahead_page(struct readahead_control *ractl)
1030 {
1031         struct folio *folio = __readahead_folio(ractl);
1032
1033         return &folio->page;
1034 }
1035
1036 /**
1037  * readahead_folio - Get the next folio to read.
1038  * @ractl: The current readahead request.
1039  *
1040  * Context: The folio is locked.  The caller should unlock the folio once
1041  * all I/O to that folio has completed.
1042  * Return: A pointer to the next folio, or %NULL if we are done.
1043  */
1044 static inline struct folio *readahead_folio(struct readahead_control *ractl)
1045 {
1046         struct folio *folio = __readahead_folio(ractl);
1047
1048         if (folio)
1049                 folio_put(folio);
1050         return folio;
1051 }
1052
1053 static inline unsigned int __readahead_batch(struct readahead_control *rac,
1054                 struct page **array, unsigned int array_sz)
1055 {
1056         unsigned int i = 0;
1057         XA_STATE(xas, &rac->mapping->i_pages, 0);
1058         struct page *page;
1059
1060         BUG_ON(rac->_batch_count > rac->_nr_pages);
1061         rac->_nr_pages -= rac->_batch_count;
1062         rac->_index += rac->_batch_count;
1063         rac->_batch_count = 0;
1064
1065         xas_set(&xas, rac->_index);
1066         rcu_read_lock();
1067         xas_for_each(&xas, page, rac->_index + rac->_nr_pages - 1) {
1068                 if (xas_retry(&xas, page))
1069                         continue;
1070                 VM_BUG_ON_PAGE(!PageLocked(page), page);
1071                 VM_BUG_ON_PAGE(PageTail(page), page);
1072                 array[i++] = page;
1073                 rac->_batch_count += thp_nr_pages(page);
1074
1075                 /*
1076                  * The page cache isn't using multi-index entries yet,
1077                  * so the xas cursor needs to be manually moved to the
1078                  * next index.  This can be removed once the page cache
1079                  * is converted.
1080                  */
1081                 if (PageHead(page))
1082                         xas_set(&xas, rac->_index + rac->_batch_count);
1083
1084                 if (i == array_sz)
1085                         break;
1086         }
1087         rcu_read_unlock();
1088
1089         return i;
1090 }
1091
1092 /**
1093  * readahead_page_batch - Get a batch of pages to read.
1094  * @rac: The current readahead request.
1095  * @array: An array of pointers to struct page.
1096  *
1097  * Context: The pages are locked and have an elevated refcount.  The caller
1098  * should decreases the refcount once the page has been submitted for I/O
1099  * and unlock the page once all I/O to that page has completed.
1100  * Return: The number of pages placed in the array.  0 indicates the request
1101  * is complete.
1102  */
1103 #define readahead_page_batch(rac, array)                                \
1104         __readahead_batch(rac, array, ARRAY_SIZE(array))
1105
1106 /**
1107  * readahead_pos - The byte offset into the file of this readahead request.
1108  * @rac: The readahead request.
1109  */
1110 static inline loff_t readahead_pos(struct readahead_control *rac)
1111 {
1112         return (loff_t)rac->_index * PAGE_SIZE;
1113 }
1114
1115 /**
1116  * readahead_length - The number of bytes in this readahead request.
1117  * @rac: The readahead request.
1118  */
1119 static inline size_t readahead_length(struct readahead_control *rac)
1120 {
1121         return rac->_nr_pages * PAGE_SIZE;
1122 }
1123
1124 /**
1125  * readahead_index - The index of the first page in this readahead request.
1126  * @rac: The readahead request.
1127  */
1128 static inline pgoff_t readahead_index(struct readahead_control *rac)
1129 {
1130         return rac->_index;
1131 }
1132
1133 /**
1134  * readahead_count - The number of pages in this readahead request.
1135  * @rac: The readahead request.
1136  */
1137 static inline unsigned int readahead_count(struct readahead_control *rac)
1138 {
1139         return rac->_nr_pages;
1140 }
1141
1142 /**
1143  * readahead_batch_length - The number of bytes in the current batch.
1144  * @rac: The readahead request.
1145  */
1146 static inline size_t readahead_batch_length(struct readahead_control *rac)
1147 {
1148         return rac->_batch_count * PAGE_SIZE;
1149 }
1150
1151 static inline unsigned long dir_pages(struct inode *inode)
1152 {
1153         return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
1154                                PAGE_SHIFT;
1155 }
1156
1157 /**
1158  * folio_mkwrite_check_truncate - check if folio was truncated
1159  * @folio: the folio to check
1160  * @inode: the inode to check the folio against
1161  *
1162  * Return: the number of bytes in the folio up to EOF,
1163  * or -EFAULT if the folio was truncated.
1164  */
1165 static inline ssize_t folio_mkwrite_check_truncate(struct folio *folio,
1166                                               struct inode *inode)
1167 {
1168         loff_t size = i_size_read(inode);
1169         pgoff_t index = size >> PAGE_SHIFT;
1170         size_t offset = offset_in_folio(folio, size);
1171
1172         if (!folio->mapping)
1173                 return -EFAULT;
1174
1175         /* folio is wholly inside EOF */
1176         if (folio_next_index(folio) - 1 < index)
1177                 return folio_size(folio);
1178         /* folio is wholly past EOF */
1179         if (folio->index > index || !offset)
1180                 return -EFAULT;
1181         /* folio is partially inside EOF */
1182         return offset;
1183 }
1184
1185 /**
1186  * page_mkwrite_check_truncate - check if page was truncated
1187  * @page: the page to check
1188  * @inode: the inode to check the page against
1189  *
1190  * Returns the number of bytes in the page up to EOF,
1191  * or -EFAULT if the page was truncated.
1192  */
1193 static inline int page_mkwrite_check_truncate(struct page *page,
1194                                               struct inode *inode)
1195 {
1196         loff_t size = i_size_read(inode);
1197         pgoff_t index = size >> PAGE_SHIFT;
1198         int offset = offset_in_page(size);
1199
1200         if (page->mapping != inode->i_mapping)
1201                 return -EFAULT;
1202
1203         /* page is wholly inside EOF */
1204         if (page->index < index)
1205                 return PAGE_SIZE;
1206         /* page is wholly past EOF */
1207         if (page->index > index || !offset)
1208                 return -EFAULT;
1209         /* page is partially inside EOF */
1210         return offset;
1211 }
1212
1213 /**
1214  * i_blocks_per_folio - How many blocks fit in this folio.
1215  * @inode: The inode which contains the blocks.
1216  * @folio: The folio.
1217  *
1218  * If the block size is larger than the size of this folio, return zero.
1219  *
1220  * Context: The caller should hold a refcount on the folio to prevent it
1221  * from being split.
1222  * Return: The number of filesystem blocks covered by this folio.
1223  */
1224 static inline
1225 unsigned int i_blocks_per_folio(struct inode *inode, struct folio *folio)
1226 {
1227         return folio_size(folio) >> inode->i_blkbits;
1228 }
1229
1230 static inline
1231 unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
1232 {
1233         return i_blocks_per_folio(inode, page_folio(page));
1234 }
1235 #endif /* _LINUX_PAGEMAP_H */