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