]>
git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - include/linux/pagemap.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGEMAP_H
3 #define _LINUX_PAGEMAP_H
6 * Copyright 1995 Linus Torvalds
10 #include <linux/list.h>
11 #include <linux/highmem.h>
12 #include <linux/compiler.h>
13 #include <linux/uaccess.h>
14 #include <linux/gfp.h>
15 #include <linux/bitops.h>
16 #include <linux/hardirq.h> /* for in_interrupt() */
17 #include <linux/hugetlb_inline.h>
21 static inline bool mapping_empty(struct address_space
*mapping
)
23 return xa_empty(&mapping
->i_pages
);
27 * mapping_shrinkable - test if page cache state allows inode reclaim
28 * @mapping: the page cache mapping
30 * This checks the mapping's cache state for the pupose of inode
31 * reclaim and LRU management.
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.
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.
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.
47 static inline bool mapping_shrinkable(struct address_space
*mapping
)
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.
56 if (IS_ENABLED(CONFIG_HIGHMEM
))
59 /* Cache completely empty? Shrink away. */
60 head
= rcu_access_pointer(mapping
->i_pages
.xa_head
);
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.
70 if (!xa_is_node(head
) && xa_is_value(head
))
77 * Bits in 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 */
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
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.
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).
104 static inline void mapping_set_error(struct address_space
*mapping
, int error
)
109 /* Record in wb_err for checkers using errseq_t based tracking */
110 __filemap_set_wb_err(mapping
, error
);
112 /* Record it in superblock */
114 errseq_set(&mapping
->host
->i_sb
->s_wb_err
, error
);
116 /* Record it in flags for now, for legacy callers */
117 if (error
== -ENOSPC
)
118 set_bit(AS_ENOSPC
, &mapping
->flags
);
120 set_bit(AS_EIO
, &mapping
->flags
);
123 static inline void mapping_set_unevictable(struct address_space
*mapping
)
125 set_bit(AS_UNEVICTABLE
, &mapping
->flags
);
128 static inline void mapping_clear_unevictable(struct address_space
*mapping
)
130 clear_bit(AS_UNEVICTABLE
, &mapping
->flags
);
133 static inline bool mapping_unevictable(struct address_space
*mapping
)
135 return mapping
&& test_bit(AS_UNEVICTABLE
, &mapping
->flags
);
138 static inline void mapping_set_exiting(struct address_space
*mapping
)
140 set_bit(AS_EXITING
, &mapping
->flags
);
143 static inline int mapping_exiting(struct address_space
*mapping
)
145 return test_bit(AS_EXITING
, &mapping
->flags
);
148 static inline void mapping_set_no_writeback_tags(struct address_space
*mapping
)
150 set_bit(AS_NO_WRITEBACK_TAGS
, &mapping
->flags
);
153 static inline int mapping_use_writeback_tags(struct address_space
*mapping
)
155 return !test_bit(AS_NO_WRITEBACK_TAGS
, &mapping
->flags
);
158 static inline gfp_t
mapping_gfp_mask(struct address_space
* mapping
)
160 return mapping
->gfp_mask
;
163 /* Restricts the given gfp_mask to what the mapping allows. */
164 static inline gfp_t
mapping_gfp_constraint(struct address_space
*mapping
,
167 return mapping_gfp_mask(mapping
) & gfp_mask
;
171 * This is non-atomic. Only to be used before the mapping is activated.
172 * Probably needs a barrier...
174 static inline void mapping_set_gfp_mask(struct address_space
*m
, gfp_t mask
)
179 static inline bool mapping_thp_support(struct address_space
*mapping
)
181 return test_bit(AS_THP_SUPPORT
, &mapping
->flags
);
184 static inline int filemap_nr_thps(struct address_space
*mapping
)
186 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
187 return atomic_read(&mapping
->nr_thps
);
193 static inline void filemap_nr_thps_inc(struct address_space
*mapping
)
195 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
196 if (!mapping_thp_support(mapping
))
197 atomic_inc(&mapping
->nr_thps
);
203 static inline void filemap_nr_thps_dec(struct address_space
*mapping
)
205 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
206 if (!mapping_thp_support(mapping
))
207 atomic_dec(&mapping
->nr_thps
);
213 void release_pages(struct page
**pages
, int nr
);
216 * For file cache pages, return the address_space, otherwise return NULL
218 static inline struct address_space
*page_mapping_file(struct page
*page
)
220 if (unlikely(PageSwapCache(page
)))
222 return page_mapping(page
);
226 * speculatively take a reference to a page.
227 * If the page is free (_refcount == 0), then _refcount is untouched, and 0
228 * is returned. Otherwise, _refcount is incremented by 1 and 1 is returned.
230 * This function must be called inside the same rcu_read_lock() section as has
231 * been used to lookup the page in the pagecache radix-tree (or page table):
232 * this allows allocators to use a synchronize_rcu() to stabilize _refcount.
234 * Unless an RCU grace period has passed, the count of all pages coming out
235 * of the allocator must be considered unstable. page_count may return higher
236 * than expected, and put_page must be able to do the right thing when the
237 * page has been finished with, no matter what it is subsequently allocated
238 * for (because put_page is what is used here to drop an invalid speculative
241 * This is the interesting part of the lockless pagecache (and lockless
242 * get_user_pages) locking protocol, where the lookup-side (eg. find_get_page)
243 * has the following pattern:
244 * 1. find page in radix tree
245 * 2. conditionally increment refcount
246 * 3. check the page is still in pagecache (if no, goto 1)
248 * Remove-side that cares about stability of _refcount (eg. reclaim) has the
249 * following (with the i_pages lock held):
250 * A. atomically check refcount is correct and set it to 0 (atomic_cmpxchg)
251 * B. remove page from pagecache
254 * There are 2 critical interleavings that matter:
255 * - 2 runs before A: in this case, A sees elevated refcount and bails out
256 * - A runs before 2: in this case, 2 sees zero refcount and retries;
257 * subsequently, B will complete and 1 will find no page, causing the
258 * lookup to return NULL.
260 * It is possible that between 1 and 2, the page is removed then the exact same
261 * page is inserted into the same position in pagecache. That's OK: the
262 * old find_get_page using a lock could equally have run before or after
263 * such a re-insertion, depending on order that locks are granted.
265 * Lookups racing against pagecache insertion isn't a big problem: either 1
266 * will find the page or it will not. Likewise, the old find_get_page could run
267 * either before the insertion or afterwards, depending on timing.
269 static inline int __page_cache_add_speculative(struct page
*page
, int count
)
271 #ifdef CONFIG_TINY_RCU
272 # ifdef CONFIG_PREEMPT_COUNT
273 VM_BUG_ON(!in_atomic() && !irqs_disabled());
276 * Preempt must be disabled here - we rely on rcu_read_lock doing
279 * Pagecache won't be truncated from interrupt context, so if we have
280 * found a page in the radix tree here, we have pinned its refcount by
281 * disabling preempt, and hence no need for the "speculative get" that
284 VM_BUG_ON_PAGE(page_count(page
) == 0, page
);
285 page_ref_add(page
, count
);
288 if (unlikely(!page_ref_add_unless(page
, count
, 0))) {
290 * Either the page has been freed, or will be freed.
291 * In either case, retry here and the caller should
292 * do the right thing (see comments above).
297 VM_BUG_ON_PAGE(PageTail(page
), page
);
302 static inline int page_cache_get_speculative(struct page
*page
)
304 return __page_cache_add_speculative(page
, 1);
307 static inline int page_cache_add_speculative(struct page
*page
, int count
)
309 return __page_cache_add_speculative(page
, count
);
313 * attach_page_private - Attach private data to a page.
314 * @page: Page to attach data to.
315 * @data: Data to attach to page.
317 * Attaching private data to a page increments the page's reference count.
318 * The data must be detached before the page will be freed.
320 static inline void attach_page_private(struct page
*page
, void *data
)
323 set_page_private(page
, (unsigned long)data
);
324 SetPagePrivate(page
);
328 * detach_page_private - Detach private data from a page.
329 * @page: Page to detach data from.
331 * Removes the data that was previously attached to the page and decrements
332 * the refcount on the page.
334 * Return: Data that was attached to the page.
336 static inline void *detach_page_private(struct page
*page
)
338 void *data
= (void *)page_private(page
);
340 if (!PagePrivate(page
))
342 ClearPagePrivate(page
);
343 set_page_private(page
, 0);
350 extern struct page
*__page_cache_alloc(gfp_t gfp
);
352 static inline struct page
*__page_cache_alloc(gfp_t gfp
)
354 return alloc_pages(gfp
, 0);
358 static inline struct page
*page_cache_alloc(struct address_space
*x
)
360 return __page_cache_alloc(mapping_gfp_mask(x
));
363 static inline gfp_t
readahead_gfp_mask(struct address_space
*x
)
365 return mapping_gfp_mask(x
) | __GFP_NORETRY
| __GFP_NOWARN
;
368 typedef int filler_t(void *, struct page
*);
370 pgoff_t
page_cache_next_miss(struct address_space
*mapping
,
371 pgoff_t index
, unsigned long max_scan
);
372 pgoff_t
page_cache_prev_miss(struct address_space
*mapping
,
373 pgoff_t index
, unsigned long max_scan
);
375 #define FGP_ACCESSED 0x00000001
376 #define FGP_LOCK 0x00000002
377 #define FGP_CREAT 0x00000004
378 #define FGP_WRITE 0x00000008
379 #define FGP_NOFS 0x00000010
380 #define FGP_NOWAIT 0x00000020
381 #define FGP_FOR_MMAP 0x00000040
382 #define FGP_HEAD 0x00000080
383 #define FGP_ENTRY 0x00000100
385 struct page
*pagecache_get_page(struct address_space
*mapping
, pgoff_t offset
,
386 int fgp_flags
, gfp_t cache_gfp_mask
);
389 * find_get_page - find and get a page reference
390 * @mapping: the address_space to search
391 * @offset: the page index
393 * Looks up the page cache slot at @mapping & @offset. If there is a
394 * page cache page, it is returned with an increased refcount.
396 * Otherwise, %NULL is returned.
398 static inline struct page
*find_get_page(struct address_space
*mapping
,
401 return pagecache_get_page(mapping
, offset
, 0, 0);
404 static inline struct page
*find_get_page_flags(struct address_space
*mapping
,
405 pgoff_t offset
, int fgp_flags
)
407 return pagecache_get_page(mapping
, offset
, fgp_flags
, 0);
411 * find_lock_page - locate, pin and lock a pagecache page
412 * @mapping: the address_space to search
413 * @index: the page index
415 * Looks up the page cache entry at @mapping & @index. If there is a
416 * page cache page, it is returned locked and with an increased
419 * Context: May sleep.
420 * Return: A struct page or %NULL if there is no page in the cache for this
423 static inline struct page
*find_lock_page(struct address_space
*mapping
,
426 return pagecache_get_page(mapping
, index
, FGP_LOCK
, 0);
430 * find_lock_head - Locate, pin and lock a pagecache page.
431 * @mapping: The address_space to search.
432 * @index: The page index.
434 * Looks up the page cache entry at @mapping & @index. If there is a
435 * page cache page, its head page is returned locked and with an increased
438 * Context: May sleep.
439 * Return: A struct page which is !PageTail, or %NULL if there is no page
440 * in the cache for this index.
442 static inline struct page
*find_lock_head(struct address_space
*mapping
,
445 return pagecache_get_page(mapping
, index
, FGP_LOCK
| FGP_HEAD
, 0);
449 * find_or_create_page - locate or add a pagecache page
450 * @mapping: the page's address_space
451 * @index: the page's index into the mapping
452 * @gfp_mask: page allocation mode
454 * Looks up the page cache slot at @mapping & @offset. If there is a
455 * page cache page, it is returned locked and with an increased
458 * If the page is not present, a new page is allocated using @gfp_mask
459 * and added to the page cache and the VM's LRU list. The page is
460 * returned locked and with an increased refcount.
462 * On memory exhaustion, %NULL is returned.
464 * find_or_create_page() may sleep, even if @gfp_flags specifies an
467 static inline struct page
*find_or_create_page(struct address_space
*mapping
,
468 pgoff_t index
, gfp_t gfp_mask
)
470 return pagecache_get_page(mapping
, index
,
471 FGP_LOCK
|FGP_ACCESSED
|FGP_CREAT
,
476 * grab_cache_page_nowait - returns locked page at given index in given cache
477 * @mapping: target address_space
478 * @index: the page index
480 * Same as grab_cache_page(), but do not wait if the page is unavailable.
481 * This is intended for speculative data generators, where the data can
482 * be regenerated if the page couldn't be grabbed. This routine should
483 * be safe to call while holding the lock for another page.
485 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
486 * and deadlock against the caller's locked page.
488 static inline struct page
*grab_cache_page_nowait(struct address_space
*mapping
,
491 return pagecache_get_page(mapping
, index
,
492 FGP_LOCK
|FGP_CREAT
|FGP_NOFS
|FGP_NOWAIT
,
493 mapping_gfp_mask(mapping
));
496 /* Does this page contain this index? */
497 static inline bool thp_contains(struct page
*head
, pgoff_t index
)
499 /* HugeTLBfs indexes the page cache in units of hpage_size */
501 return head
->index
== index
;
502 return page_index(head
) == (index
& ~(thp_nr_pages(head
) - 1UL));
506 * Given the page we found in the page cache, return the page corresponding
507 * to this index in the file
509 static inline struct page
*find_subpage(struct page
*head
, pgoff_t index
)
511 /* HugeTLBfs wants the head page regardless */
515 return head
+ (index
& (thp_nr_pages(head
) - 1));
518 unsigned find_get_entries(struct address_space
*mapping
, pgoff_t start
,
519 pgoff_t end
, struct pagevec
*pvec
, pgoff_t
*indices
);
520 unsigned find_get_pages_range(struct address_space
*mapping
, pgoff_t
*start
,
521 pgoff_t end
, unsigned int nr_pages
,
522 struct page
**pages
);
523 static inline unsigned find_get_pages(struct address_space
*mapping
,
524 pgoff_t
*start
, unsigned int nr_pages
,
527 return find_get_pages_range(mapping
, start
, (pgoff_t
)-1, nr_pages
,
530 unsigned find_get_pages_contig(struct address_space
*mapping
, pgoff_t start
,
531 unsigned int nr_pages
, struct page
**pages
);
532 unsigned find_get_pages_range_tag(struct address_space
*mapping
, pgoff_t
*index
,
533 pgoff_t end
, xa_mark_t tag
, unsigned int nr_pages
,
534 struct page
**pages
);
535 static inline unsigned find_get_pages_tag(struct address_space
*mapping
,
536 pgoff_t
*index
, xa_mark_t tag
, unsigned int nr_pages
,
539 return find_get_pages_range_tag(mapping
, index
, (pgoff_t
)-1, tag
,
543 struct page
*grab_cache_page_write_begin(struct address_space
*mapping
,
544 pgoff_t index
, unsigned flags
);
547 * Returns locked page at given index in given cache, creating it if needed.
549 static inline struct page
*grab_cache_page(struct address_space
*mapping
,
552 return find_or_create_page(mapping
, index
, mapping_gfp_mask(mapping
));
555 extern struct page
* read_cache_page(struct address_space
*mapping
,
556 pgoff_t index
, filler_t
*filler
, void *data
);
557 extern struct page
* read_cache_page_gfp(struct address_space
*mapping
,
558 pgoff_t index
, gfp_t gfp_mask
);
559 extern int read_cache_pages(struct address_space
*mapping
,
560 struct list_head
*pages
, filler_t
*filler
, void *data
);
562 static inline struct page
*read_mapping_page(struct address_space
*mapping
,
563 pgoff_t index
, void *data
)
565 return read_cache_page(mapping
, index
, NULL
, data
);
569 * Get index of the page within radix-tree (but not for hugetlb pages).
570 * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
572 static inline pgoff_t
page_to_index(struct page
*page
)
576 if (likely(!PageTransTail(page
)))
579 head
= compound_head(page
);
581 * We don't initialize ->index for tail pages: calculate based on
584 return head
->index
+ page
- head
;
587 extern pgoff_t
hugetlb_basepage_index(struct page
*page
);
590 * Get the offset in PAGE_SIZE (even for hugetlb pages).
591 * (TODO: hugetlb pages should have ->index in PAGE_SIZE)
593 static inline pgoff_t
page_to_pgoff(struct page
*page
)
595 if (unlikely(PageHuge(page
)))
596 return hugetlb_basepage_index(page
);
597 return page_to_index(page
);
601 * Return byte-offset into filesystem object for page.
603 static inline loff_t
page_offset(struct page
*page
)
605 return ((loff_t
)page
->index
) << PAGE_SHIFT
;
608 static inline loff_t
page_file_offset(struct page
*page
)
610 return ((loff_t
)page_index(page
)) << PAGE_SHIFT
;
613 extern pgoff_t
linear_hugepage_index(struct vm_area_struct
*vma
,
614 unsigned long address
);
616 static inline pgoff_t
linear_page_index(struct vm_area_struct
*vma
,
617 unsigned long address
)
620 if (unlikely(is_vm_hugetlb_page(vma
)))
621 return linear_hugepage_index(vma
, address
);
622 pgoff
= (address
- vma
->vm_start
) >> PAGE_SHIFT
;
623 pgoff
+= vma
->vm_pgoff
;
627 struct wait_page_key
{
633 struct wait_page_queue
{
636 wait_queue_entry_t wait
;
639 static inline bool wake_page_match(struct wait_page_queue
*wait_page
,
640 struct wait_page_key
*key
)
642 if (wait_page
->page
!= key
->page
)
646 if (wait_page
->bit_nr
!= key
->bit_nr
)
652 extern void __lock_page(struct page
*page
);
653 extern int __lock_page_killable(struct page
*page
);
654 extern int __lock_page_async(struct page
*page
, struct wait_page_queue
*wait
);
655 extern int __lock_page_or_retry(struct page
*page
, struct mm_struct
*mm
,
657 extern void unlock_page(struct page
*page
);
660 * Return true if the page was successfully locked
662 static inline int trylock_page(struct page
*page
)
664 page
= compound_head(page
);
665 return (likely(!test_and_set_bit_lock(PG_locked
, &page
->flags
)));
669 * lock_page may only be called if we have the page's inode pinned.
671 static inline void lock_page(struct page
*page
)
674 if (!trylock_page(page
))
679 * lock_page_killable is like lock_page but can be interrupted by fatal
680 * signals. It returns 0 if it locked the page and -EINTR if it was
681 * killed while waiting.
683 static inline int lock_page_killable(struct page
*page
)
686 if (!trylock_page(page
))
687 return __lock_page_killable(page
);
692 * lock_page_async - Lock the page, unless this would block. If the page
693 * is already locked, then queue a callback when the page becomes unlocked.
694 * This callback can then retry the operation.
696 * Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page
697 * was already locked and the callback defined in 'wait' was queued.
699 static inline int lock_page_async(struct page
*page
,
700 struct wait_page_queue
*wait
)
702 if (!trylock_page(page
))
703 return __lock_page_async(page
, wait
);
708 * lock_page_or_retry - Lock the page, unless this would block and the
709 * caller indicated that it can handle a retry.
711 * Return value and mmap_lock implications depend on flags; see
712 * __lock_page_or_retry().
714 static inline int lock_page_or_retry(struct page
*page
, struct mm_struct
*mm
,
718 return trylock_page(page
) || __lock_page_or_retry(page
, mm
, flags
);
722 * This is exported only for wait_on_page_locked/wait_on_page_writeback, etc.,
723 * and should not be used directly.
725 extern void wait_on_page_bit(struct page
*page
, int bit_nr
);
726 extern int wait_on_page_bit_killable(struct page
*page
, int bit_nr
);
729 * Wait for a page to be unlocked.
731 * This must be called with the caller "holding" the page,
732 * ie with increased "page->count" so that the page won't
733 * go away during the wait..
735 static inline void wait_on_page_locked(struct page
*page
)
737 if (PageLocked(page
))
738 wait_on_page_bit(compound_head(page
), PG_locked
);
741 static inline int wait_on_page_locked_killable(struct page
*page
)
743 if (!PageLocked(page
))
745 return wait_on_page_bit_killable(compound_head(page
), PG_locked
);
748 int put_and_wait_on_page_locked(struct page
*page
, int state
);
749 void wait_on_page_writeback(struct page
*page
);
750 int wait_on_page_writeback_killable(struct page
*page
);
751 extern void end_page_writeback(struct page
*page
);
752 void wait_for_stable_page(struct page
*page
);
754 void __set_page_dirty(struct page
*, struct address_space
*, int warn
);
755 int __set_page_dirty_nobuffers(struct page
*page
);
756 int __set_page_dirty_no_writeback(struct page
*page
);
758 void page_endio(struct page
*page
, bool is_write
, int err
);
761 * set_page_private_2 - Set PG_private_2 on a page and take a ref
764 * Set the PG_private_2 flag on a page and take the reference needed for the VM
765 * to handle its lifetime correctly. This sets the flag and takes the
766 * reference unconditionally, so care must be taken not to set the flag again
767 * if it's already set.
769 static inline void set_page_private_2(struct page
*page
)
771 page
= compound_head(page
);
773 SetPagePrivate2(page
);
776 void end_page_private_2(struct page
*page
);
777 void wait_on_page_private_2(struct page
*page
);
778 int wait_on_page_private_2_killable(struct page
*page
);
781 * Add an arbitrary waiter to a page's wait queue
783 extern void add_page_wait_queue(struct page
*page
, wait_queue_entry_t
*waiter
);
786 * Fault everything in given userspace address range in.
788 static inline int fault_in_pages_writeable(char __user
*uaddr
, size_t size
)
790 char __user
*end
= uaddr
+ size
- 1;
792 if (unlikely(size
== 0))
795 if (unlikely(uaddr
> end
))
798 * Writing zeroes into userspace here is OK, because we know that if
799 * the zero gets there, we'll be overwriting it.
802 if (unlikely(__put_user(0, uaddr
) != 0))
805 } while (uaddr
<= end
);
807 /* Check whether the range spilled into the next page. */
808 if (((unsigned long)uaddr
& PAGE_MASK
) ==
809 ((unsigned long)end
& PAGE_MASK
))
810 return __put_user(0, end
);
815 static inline int fault_in_pages_readable(const char __user
*uaddr
, size_t size
)
818 const char __user
*end
= uaddr
+ size
- 1;
820 if (unlikely(size
== 0))
823 if (unlikely(uaddr
> end
))
827 if (unlikely(__get_user(c
, uaddr
) != 0))
830 } while (uaddr
<= end
);
832 /* Check whether the range spilled into the next page. */
833 if (((unsigned long)uaddr
& PAGE_MASK
) ==
834 ((unsigned long)end
& PAGE_MASK
)) {
835 return __get_user(c
, end
);
842 int add_to_page_cache_locked(struct page
*page
, struct address_space
*mapping
,
843 pgoff_t index
, gfp_t gfp_mask
);
844 int add_to_page_cache_lru(struct page
*page
, struct address_space
*mapping
,
845 pgoff_t index
, gfp_t gfp_mask
);
846 extern void delete_from_page_cache(struct page
*page
);
847 extern void __delete_from_page_cache(struct page
*page
, void *shadow
);
848 void replace_page_cache_page(struct page
*old
, struct page
*new);
849 void delete_from_page_cache_batch(struct address_space
*mapping
,
850 struct pagevec
*pvec
);
851 loff_t
mapping_seek_hole_data(struct address_space
*, loff_t start
, loff_t end
,
855 * Like add_to_page_cache_locked, but used to add newly allocated pages:
856 * the page is new, so we can just run __SetPageLocked() against it.
858 static inline int add_to_page_cache(struct page
*page
,
859 struct address_space
*mapping
, pgoff_t offset
, gfp_t gfp_mask
)
863 __SetPageLocked(page
);
864 error
= add_to_page_cache_locked(page
, mapping
, offset
, gfp_mask
);
866 __ClearPageLocked(page
);
871 * struct readahead_control - Describes a readahead request.
873 * A readahead request is for consecutive pages. Filesystems which
874 * implement the ->readahead method should call readahead_page() or
875 * readahead_page_batch() in a loop and attempt to start I/O against
876 * each page in the request.
878 * Most of the fields in this struct are private and should be accessed
879 * by the functions below.
881 * @file: The file, used primarily by network filesystems for authentication.
882 * May be NULL if invoked internally by the filesystem.
883 * @mapping: Readahead this filesystem object.
884 * @ra: File readahead state. May be NULL.
886 struct readahead_control
{
888 struct address_space
*mapping
;
889 struct file_ra_state
*ra
;
890 /* private: use the readahead_* accessors instead */
892 unsigned int _nr_pages
;
893 unsigned int _batch_count
;
896 #define DEFINE_READAHEAD(ractl, f, r, m, i) \
897 struct readahead_control ractl = { \
904 #define VM_READAHEAD_PAGES (SZ_128K / PAGE_SIZE)
906 void page_cache_ra_unbounded(struct readahead_control
*,
907 unsigned long nr_to_read
, unsigned long lookahead_count
);
908 void page_cache_sync_ra(struct readahead_control
*, unsigned long req_count
);
909 void page_cache_async_ra(struct readahead_control
*, struct page
*,
910 unsigned long req_count
);
911 void readahead_expand(struct readahead_control
*ractl
,
912 loff_t new_start
, size_t new_len
);
915 * page_cache_sync_readahead - generic file readahead
916 * @mapping: address_space which holds the pagecache and I/O vectors
917 * @ra: file_ra_state which holds the readahead state
918 * @file: Used by the filesystem for authentication.
919 * @index: Index of first page to be read.
920 * @req_count: Total number of pages being read by the caller.
922 * page_cache_sync_readahead() should be called when a cache miss happened:
923 * it will submit the read. The readahead logic may decide to piggyback more
924 * pages onto the read request if access patterns suggest it will improve
928 void page_cache_sync_readahead(struct address_space
*mapping
,
929 struct file_ra_state
*ra
, struct file
*file
, pgoff_t index
,
930 unsigned long req_count
)
932 DEFINE_READAHEAD(ractl
, file
, ra
, mapping
, index
);
933 page_cache_sync_ra(&ractl
, req_count
);
937 * page_cache_async_readahead - file readahead for marked pages
938 * @mapping: address_space which holds the pagecache and I/O vectors
939 * @ra: file_ra_state which holds the readahead state
940 * @file: Used by the filesystem for authentication.
941 * @page: The page at @index which triggered the readahead call.
942 * @index: Index of first page to be read.
943 * @req_count: Total number of pages being read by the caller.
945 * page_cache_async_readahead() should be called when a page is used which
946 * is marked as PageReadahead; this is a marker to suggest that the application
947 * has used up enough of the readahead window that we should start pulling in
951 void page_cache_async_readahead(struct address_space
*mapping
,
952 struct file_ra_state
*ra
, struct file
*file
,
953 struct page
*page
, pgoff_t index
, unsigned long req_count
)
955 DEFINE_READAHEAD(ractl
, file
, ra
, mapping
, index
);
956 page_cache_async_ra(&ractl
, page
, req_count
);
960 * readahead_page - Get the next page to read.
961 * @rac: The current readahead request.
963 * Context: The page is locked and has an elevated refcount. The caller
964 * should decreases the refcount once the page has been submitted for I/O
965 * and unlock the page once all I/O to that page has completed.
966 * Return: A pointer to the next page, or %NULL if we are done.
968 static inline struct page
*readahead_page(struct readahead_control
*rac
)
972 BUG_ON(rac
->_batch_count
> rac
->_nr_pages
);
973 rac
->_nr_pages
-= rac
->_batch_count
;
974 rac
->_index
+= rac
->_batch_count
;
976 if (!rac
->_nr_pages
) {
977 rac
->_batch_count
= 0;
981 page
= xa_load(&rac
->mapping
->i_pages
, rac
->_index
);
982 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
983 rac
->_batch_count
= thp_nr_pages(page
);
988 static inline unsigned int __readahead_batch(struct readahead_control
*rac
,
989 struct page
**array
, unsigned int array_sz
)
992 XA_STATE(xas
, &rac
->mapping
->i_pages
, 0);
995 BUG_ON(rac
->_batch_count
> rac
->_nr_pages
);
996 rac
->_nr_pages
-= rac
->_batch_count
;
997 rac
->_index
+= rac
->_batch_count
;
998 rac
->_batch_count
= 0;
1000 xas_set(&xas
, rac
->_index
);
1002 xas_for_each(&xas
, page
, rac
->_index
+ rac
->_nr_pages
- 1) {
1003 if (xas_retry(&xas
, page
))
1005 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
1006 VM_BUG_ON_PAGE(PageTail(page
), page
);
1008 rac
->_batch_count
+= thp_nr_pages(page
);
1011 * The page cache isn't using multi-index entries yet,
1012 * so the xas cursor needs to be manually moved to the
1013 * next index. This can be removed once the page cache
1017 xas_set(&xas
, rac
->_index
+ rac
->_batch_count
);
1028 * readahead_page_batch - Get a batch of pages to read.
1029 * @rac: The current readahead request.
1030 * @array: An array of pointers to struct page.
1032 * Context: The pages are locked and have an elevated refcount. The caller
1033 * should decreases the refcount once the page has been submitted for I/O
1034 * and unlock the page once all I/O to that page has completed.
1035 * Return: The number of pages placed in the array. 0 indicates the request
1038 #define readahead_page_batch(rac, array) \
1039 __readahead_batch(rac, array, ARRAY_SIZE(array))
1042 * readahead_pos - The byte offset into the file of this readahead request.
1043 * @rac: The readahead request.
1045 static inline loff_t
readahead_pos(struct readahead_control
*rac
)
1047 return (loff_t
)rac
->_index
* PAGE_SIZE
;
1051 * readahead_length - The number of bytes in this readahead request.
1052 * @rac: The readahead request.
1054 static inline size_t readahead_length(struct readahead_control
*rac
)
1056 return rac
->_nr_pages
* PAGE_SIZE
;
1060 * readahead_index - The index of the first page in this readahead request.
1061 * @rac: The readahead request.
1063 static inline pgoff_t
readahead_index(struct readahead_control
*rac
)
1069 * readahead_count - The number of pages in this readahead request.
1070 * @rac: The readahead request.
1072 static inline unsigned int readahead_count(struct readahead_control
*rac
)
1074 return rac
->_nr_pages
;
1078 * readahead_batch_length - The number of bytes in the current batch.
1079 * @rac: The readahead request.
1081 static inline size_t readahead_batch_length(struct readahead_control
*rac
)
1083 return rac
->_batch_count
* PAGE_SIZE
;
1086 static inline unsigned long dir_pages(struct inode
*inode
)
1088 return (unsigned long)(inode
->i_size
+ PAGE_SIZE
- 1) >>
1093 * page_mkwrite_check_truncate - check if page was truncated
1094 * @page: the page to check
1095 * @inode: the inode to check the page against
1097 * Returns the number of bytes in the page up to EOF,
1098 * or -EFAULT if the page was truncated.
1100 static inline int page_mkwrite_check_truncate(struct page
*page
,
1101 struct inode
*inode
)
1103 loff_t size
= i_size_read(inode
);
1104 pgoff_t index
= size
>> PAGE_SHIFT
;
1105 int offset
= offset_in_page(size
);
1107 if (page
->mapping
!= inode
->i_mapping
)
1110 /* page is wholly inside EOF */
1111 if (page
->index
< index
)
1113 /* page is wholly past EOF */
1114 if (page
->index
> index
|| !offset
)
1116 /* page is partially inside EOF */
1121 * i_blocks_per_page - How many blocks fit in this page.
1122 * @inode: The inode which contains the blocks.
1123 * @page: The page (head page if the page is a THP).
1125 * If the block size is larger than the size of this page, return zero.
1127 * Context: The caller should hold a refcount on the page to prevent it
1129 * Return: The number of filesystem blocks covered by this page.
1132 unsigned int i_blocks_per_page(struct inode
*inode
, struct page
*page
)
1134 return thp_size(page
) >> inode
->i_blkbits
;
1136 #endif /* _LINUX_PAGEMAP_H */