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