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>
22 * Bits in mapping->flags.
25 AS_EIO = 0, /* IO error on async write */
26 AS_ENOSPC = 1, /* ENOSPC on async write */
27 AS_MM_ALL_LOCKS = 2, /* under mm_take_all_locks() */
28 AS_UNEVICTABLE = 3, /* e.g., ramdisk, SHM_LOCK */
29 AS_EXITING = 4, /* final truncate in progress */
30 /* writeback related tags are not used */
31 AS_NO_WRITEBACK_TAGS = 5,
32 AS_THP_SUPPORT = 6, /* THPs supported */
36 * mapping_set_error - record a writeback error in the address_space
37 * @mapping: the mapping in which an error should be set
38 * @error: the error to set in the mapping
40 * When writeback fails in some way, we must record that error so that
41 * userspace can be informed when fsync and the like are called. We endeavor
42 * to report errors on any file that was open at the time of the error. Some
43 * internal callers also need to know when writeback errors have occurred.
45 * When a writeback error occurs, most filesystems will want to call
46 * mapping_set_error to record the error in the mapping so that it can be
47 * reported when the application calls fsync(2).
49 static inline void mapping_set_error(struct address_space *mapping, int error)
54 /* Record in wb_err for checkers using errseq_t based tracking */
55 __filemap_set_wb_err(mapping, error);
57 /* Record it in superblock */
59 errseq_set(&mapping->host->i_sb->s_wb_err, error);
61 /* Record it in flags for now, for legacy callers */
63 set_bit(AS_ENOSPC, &mapping->flags);
65 set_bit(AS_EIO, &mapping->flags);
68 static inline void mapping_set_unevictable(struct address_space *mapping)
70 set_bit(AS_UNEVICTABLE, &mapping->flags);
73 static inline void mapping_clear_unevictable(struct address_space *mapping)
75 clear_bit(AS_UNEVICTABLE, &mapping->flags);
78 static inline bool mapping_unevictable(struct address_space *mapping)
80 return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags);
83 static inline void mapping_set_exiting(struct address_space *mapping)
85 set_bit(AS_EXITING, &mapping->flags);
88 static inline int mapping_exiting(struct address_space *mapping)
90 return test_bit(AS_EXITING, &mapping->flags);
93 static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
95 set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
98 static inline int mapping_use_writeback_tags(struct address_space *mapping)
100 return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
103 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
105 return mapping->gfp_mask;
108 /* Restricts the given gfp_mask to what the mapping allows. */
109 static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
112 return mapping_gfp_mask(mapping) & gfp_mask;
116 * This is non-atomic. Only to be used before the mapping is activated.
117 * Probably needs a barrier...
119 static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
124 static inline bool mapping_thp_support(struct address_space *mapping)
126 return test_bit(AS_THP_SUPPORT, &mapping->flags);
129 void release_pages(struct page **pages, int nr);
132 * speculatively take a reference to a page.
133 * If the page is free (_refcount == 0), then _refcount is untouched, and 0
134 * is returned. Otherwise, _refcount is incremented by 1 and 1 is returned.
136 * This function must be called inside the same rcu_read_lock() section as has
137 * been used to lookup the page in the pagecache radix-tree (or page table):
138 * this allows allocators to use a synchronize_rcu() to stabilize _refcount.
140 * Unless an RCU grace period has passed, the count of all pages coming out
141 * of the allocator must be considered unstable. page_count may return higher
142 * than expected, and put_page must be able to do the right thing when the
143 * page has been finished with, no matter what it is subsequently allocated
144 * for (because put_page is what is used here to drop an invalid speculative
147 * This is the interesting part of the lockless pagecache (and lockless
148 * get_user_pages) locking protocol, where the lookup-side (eg. find_get_page)
149 * has the following pattern:
150 * 1. find page in radix tree
151 * 2. conditionally increment refcount
152 * 3. check the page is still in pagecache (if no, goto 1)
154 * Remove-side that cares about stability of _refcount (eg. reclaim) has the
155 * following (with the i_pages lock held):
156 * A. atomically check refcount is correct and set it to 0 (atomic_cmpxchg)
157 * B. remove page from pagecache
160 * There are 2 critical interleavings that matter:
161 * - 2 runs before A: in this case, A sees elevated refcount and bails out
162 * - A runs before 2: in this case, 2 sees zero refcount and retries;
163 * subsequently, B will complete and 1 will find no page, causing the
164 * lookup to return NULL.
166 * It is possible that between 1 and 2, the page is removed then the exact same
167 * page is inserted into the same position in pagecache. That's OK: the
168 * old find_get_page using a lock could equally have run before or after
169 * such a re-insertion, depending on order that locks are granted.
171 * Lookups racing against pagecache insertion isn't a big problem: either 1
172 * will find the page or it will not. Likewise, the old find_get_page could run
173 * either before the insertion or afterwards, depending on timing.
175 static inline int __page_cache_add_speculative(struct page *page, int count)
177 #ifdef CONFIG_TINY_RCU
178 # ifdef CONFIG_PREEMPT_COUNT
179 VM_BUG_ON(!in_atomic() && !irqs_disabled());
182 * Preempt must be disabled here - we rely on rcu_read_lock doing
185 * Pagecache won't be truncated from interrupt context, so if we have
186 * found a page in the radix tree here, we have pinned its refcount by
187 * disabling preempt, and hence no need for the "speculative get" that
190 VM_BUG_ON_PAGE(page_count(page) == 0, page);
191 page_ref_add(page, count);
194 if (unlikely(!page_ref_add_unless(page, count, 0))) {
196 * Either the page has been freed, or will be freed.
197 * In either case, retry here and the caller should
198 * do the right thing (see comments above).
203 VM_BUG_ON_PAGE(PageTail(page), page);
208 static inline int page_cache_get_speculative(struct page *page)
210 return __page_cache_add_speculative(page, 1);
213 static inline int page_cache_add_speculative(struct page *page, int count)
215 return __page_cache_add_speculative(page, count);
219 * attach_page_private - Attach private data to a page.
220 * @page: Page to attach data to.
221 * @data: Data to attach to page.
223 * Attaching private data to a page increments the page's reference count.
224 * The data must be detached before the page will be freed.
226 static inline void attach_page_private(struct page *page, void *data)
229 set_page_private(page, (unsigned long)data);
230 SetPagePrivate(page);
234 * detach_page_private - Detach private data from a page.
235 * @page: Page to detach data from.
237 * Removes the data that was previously attached to the page and decrements
238 * the refcount on the page.
240 * Return: Data that was attached to the page.
242 static inline void *detach_page_private(struct page *page)
244 void *data = (void *)page_private(page);
246 if (!PagePrivate(page))
248 ClearPagePrivate(page);
249 set_page_private(page, 0);
256 extern struct page *__page_cache_alloc(gfp_t gfp);
258 static inline struct page *__page_cache_alloc(gfp_t gfp)
260 return alloc_pages(gfp, 0);
264 static inline struct page *page_cache_alloc(struct address_space *x)
266 return __page_cache_alloc(mapping_gfp_mask(x));
269 static inline gfp_t readahead_gfp_mask(struct address_space *x)
271 return mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN;
274 typedef int filler_t(void *, struct page *);
276 pgoff_t page_cache_next_miss(struct address_space *mapping,
277 pgoff_t index, unsigned long max_scan);
278 pgoff_t page_cache_prev_miss(struct address_space *mapping,
279 pgoff_t index, unsigned long max_scan);
281 #define FGP_ACCESSED 0x00000001
282 #define FGP_LOCK 0x00000002
283 #define FGP_CREAT 0x00000004
284 #define FGP_WRITE 0x00000008
285 #define FGP_NOFS 0x00000010
286 #define FGP_NOWAIT 0x00000020
287 #define FGP_FOR_MMAP 0x00000040
288 #define FGP_HEAD 0x00000080
290 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
291 int fgp_flags, gfp_t cache_gfp_mask);
294 * find_get_page - find and get a page reference
295 * @mapping: the address_space to search
296 * @offset: the page index
298 * Looks up the page cache slot at @mapping & @offset. If there is a
299 * page cache page, it is returned with an increased refcount.
301 * Otherwise, %NULL is returned.
303 static inline struct page *find_get_page(struct address_space *mapping,
306 return pagecache_get_page(mapping, offset, 0, 0);
309 static inline struct page *find_get_page_flags(struct address_space *mapping,
310 pgoff_t offset, int fgp_flags)
312 return pagecache_get_page(mapping, offset, fgp_flags, 0);
316 * find_lock_page - locate, pin and lock a pagecache page
317 * @mapping: the address_space to search
318 * @offset: the page index
320 * Looks up the page cache entry at @mapping & @offset. If there is a
321 * page cache page, it is returned locked and with an increased
324 * Context: May sleep.
325 * Return: A struct page or %NULL if there is no page in the cache for this
328 static inline struct page *find_lock_page(struct address_space *mapping,
331 return pagecache_get_page(mapping, index, FGP_LOCK, 0);
335 * find_lock_head - Locate, pin and lock a pagecache page.
336 * @mapping: The address_space to search.
337 * @offset: The page index.
339 * Looks up the page cache entry at @mapping & @offset. If there is a
340 * page cache page, its head page is returned locked and with an increased
343 * Context: May sleep.
344 * Return: A struct page which is !PageTail, or %NULL if there is no page
345 * in the cache for this index.
347 static inline struct page *find_lock_head(struct address_space *mapping,
350 return pagecache_get_page(mapping, index, FGP_LOCK | FGP_HEAD, 0);
354 * find_or_create_page - locate or add a pagecache page
355 * @mapping: the page's address_space
356 * @index: the page's index into the mapping
357 * @gfp_mask: page allocation mode
359 * Looks up the page cache slot at @mapping & @offset. If there is a
360 * page cache page, it is returned locked and with an increased
363 * If the page is not present, a new page is allocated using @gfp_mask
364 * and added to the page cache and the VM's LRU list. The page is
365 * returned locked and with an increased refcount.
367 * On memory exhaustion, %NULL is returned.
369 * find_or_create_page() may sleep, even if @gfp_flags specifies an
372 static inline struct page *find_or_create_page(struct address_space *mapping,
373 pgoff_t index, gfp_t gfp_mask)
375 return pagecache_get_page(mapping, index,
376 FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
381 * grab_cache_page_nowait - returns locked page at given index in given cache
382 * @mapping: target address_space
383 * @index: the page index
385 * Same as grab_cache_page(), but do not wait if the page is unavailable.
386 * This is intended for speculative data generators, where the data can
387 * be regenerated if the page couldn't be grabbed. This routine should
388 * be safe to call while holding the lock for another page.
390 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
391 * and deadlock against the caller's locked page.
393 static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
396 return pagecache_get_page(mapping, index,
397 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
398 mapping_gfp_mask(mapping));
401 /* Does this page contain this index? */
402 static inline bool thp_contains(struct page *head, pgoff_t index)
404 /* HugeTLBfs indexes the page cache in units of hpage_size */
406 return head->index == index;
407 return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL));
411 * Given the page we found in the page cache, return the page corresponding
412 * to this index in the file
414 static inline struct page *find_subpage(struct page *head, pgoff_t index)
416 /* HugeTLBfs wants the head page regardless */
420 return head + (index & (thp_nr_pages(head) - 1));
423 unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
424 unsigned int nr_entries, struct page **entries,
426 unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
427 pgoff_t end, unsigned int nr_pages,
428 struct page **pages);
429 static inline unsigned find_get_pages(struct address_space *mapping,
430 pgoff_t *start, unsigned int nr_pages,
433 return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
436 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
437 unsigned int nr_pages, struct page **pages);
438 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
439 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
440 struct page **pages);
441 static inline unsigned find_get_pages_tag(struct address_space *mapping,
442 pgoff_t *index, xa_mark_t tag, unsigned int nr_pages,
445 return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
449 struct page *grab_cache_page_write_begin(struct address_space *mapping,
450 pgoff_t index, unsigned flags);
453 * Returns locked page at given index in given cache, creating it if needed.
455 static inline struct page *grab_cache_page(struct address_space *mapping,
458 return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
461 extern struct page * read_cache_page(struct address_space *mapping,
462 pgoff_t index, filler_t *filler, void *data);
463 extern struct page * read_cache_page_gfp(struct address_space *mapping,
464 pgoff_t index, gfp_t gfp_mask);
465 extern int read_cache_pages(struct address_space *mapping,
466 struct list_head *pages, filler_t *filler, void *data);
468 static inline struct page *read_mapping_page(struct address_space *mapping,
469 pgoff_t index, void *data)
471 return read_cache_page(mapping, index, NULL, data);
475 * Get index of the page with in radix-tree
476 * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
478 static inline pgoff_t page_to_index(struct page *page)
482 if (likely(!PageTransTail(page)))
486 * We don't initialize ->index for tail pages: calculate based on
489 pgoff = compound_head(page)->index;
490 pgoff += page - compound_head(page);
495 * Get the offset in PAGE_SIZE.
496 * (TODO: hugepage should have ->index in PAGE_SIZE)
498 static inline pgoff_t page_to_pgoff(struct page *page)
500 if (unlikely(PageHeadHuge(page)))
501 return page->index << compound_order(page);
503 return page_to_index(page);
507 * Return byte-offset into filesystem object for page.
509 static inline loff_t page_offset(struct page *page)
511 return ((loff_t)page->index) << PAGE_SHIFT;
514 static inline loff_t page_file_offset(struct page *page)
516 return ((loff_t)page_index(page)) << PAGE_SHIFT;
519 extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
520 unsigned long address);
522 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
523 unsigned long address)
526 if (unlikely(is_vm_hugetlb_page(vma)))
527 return linear_hugepage_index(vma, address);
528 pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
529 pgoff += vma->vm_pgoff;
533 /* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
534 struct wait_page_key {
540 struct wait_page_queue {
543 wait_queue_entry_t wait;
546 static inline bool wake_page_match(struct wait_page_queue *wait_page,
547 struct wait_page_key *key)
549 if (wait_page->page != key->page)
553 if (wait_page->bit_nr != key->bit_nr)
559 extern void __lock_page(struct page *page);
560 extern int __lock_page_killable(struct page *page);
561 extern int __lock_page_async(struct page *page, struct wait_page_queue *wait);
562 extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
564 extern void unlock_page(struct page *page);
567 * Return true if the page was successfully locked
569 static inline int trylock_page(struct page *page)
571 page = compound_head(page);
572 return (likely(!test_and_set_bit_lock(PG_locked, &page->flags)));
576 * lock_page may only be called if we have the page's inode pinned.
578 static inline void lock_page(struct page *page)
581 if (!trylock_page(page))
586 * lock_page_killable is like lock_page but can be interrupted by fatal
587 * signals. It returns 0 if it locked the page and -EINTR if it was
588 * killed while waiting.
590 static inline int lock_page_killable(struct page *page)
593 if (!trylock_page(page))
594 return __lock_page_killable(page);
599 * lock_page_async - Lock the page, unless this would block. If the page
600 * is already locked, then queue a callback when the page becomes unlocked.
601 * This callback can then retry the operation.
603 * Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page
604 * was already locked and the callback defined in 'wait' was queued.
606 static inline int lock_page_async(struct page *page,
607 struct wait_page_queue *wait)
609 if (!trylock_page(page))
610 return __lock_page_async(page, wait);
615 * lock_page_or_retry - Lock the page, unless this would block and the
616 * caller indicated that it can handle a retry.
618 * Return value and mmap_lock implications depend on flags; see
619 * __lock_page_or_retry().
621 static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
625 return trylock_page(page) || __lock_page_or_retry(page, mm, flags);
629 * This is exported only for wait_on_page_locked/wait_on_page_writeback, etc.,
630 * and should not be used directly.
632 extern void wait_on_page_bit(struct page *page, int bit_nr);
633 extern int wait_on_page_bit_killable(struct page *page, int bit_nr);
636 * Wait for a page to be unlocked.
638 * This must be called with the caller "holding" the page,
639 * ie with increased "page->count" so that the page won't
640 * go away during the wait..
642 static inline void wait_on_page_locked(struct page *page)
644 if (PageLocked(page))
645 wait_on_page_bit(compound_head(page), PG_locked);
648 static inline int wait_on_page_locked_killable(struct page *page)
650 if (!PageLocked(page))
652 return wait_on_page_bit_killable(compound_head(page), PG_locked);
655 extern void put_and_wait_on_page_locked(struct page *page);
657 void wait_on_page_writeback(struct page *page);
658 extern void end_page_writeback(struct page *page);
659 void wait_for_stable_page(struct page *page);
661 void page_endio(struct page *page, bool is_write, int err);
664 * Add an arbitrary waiter to a page's wait queue
666 extern void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter);
669 * Fault everything in given userspace address range in.
671 static inline int fault_in_pages_writeable(char __user *uaddr, int size)
673 char __user *end = uaddr + size - 1;
675 if (unlikely(size == 0))
678 if (unlikely(uaddr > end))
681 * Writing zeroes into userspace here is OK, because we know that if
682 * the zero gets there, we'll be overwriting it.
685 if (unlikely(__put_user(0, uaddr) != 0))
688 } while (uaddr <= end);
690 /* Check whether the range spilled into the next page. */
691 if (((unsigned long)uaddr & PAGE_MASK) ==
692 ((unsigned long)end & PAGE_MASK))
693 return __put_user(0, end);
698 static inline int fault_in_pages_readable(const char __user *uaddr, int size)
701 const char __user *end = uaddr + size - 1;
703 if (unlikely(size == 0))
706 if (unlikely(uaddr > end))
710 if (unlikely(__get_user(c, uaddr) != 0))
713 } while (uaddr <= end);
715 /* Check whether the range spilled into the next page. */
716 if (((unsigned long)uaddr & PAGE_MASK) ==
717 ((unsigned long)end & PAGE_MASK)) {
718 return __get_user(c, end);
725 int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
726 pgoff_t index, gfp_t gfp_mask);
727 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
728 pgoff_t index, gfp_t gfp_mask);
729 extern void delete_from_page_cache(struct page *page);
730 extern void __delete_from_page_cache(struct page *page, void *shadow);
731 int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask);
732 void delete_from_page_cache_batch(struct address_space *mapping,
733 struct pagevec *pvec);
735 #define VM_READAHEAD_PAGES (SZ_128K / PAGE_SIZE)
737 void page_cache_sync_readahead(struct address_space *, struct file_ra_state *,
738 struct file *, pgoff_t index, unsigned long req_count);
739 void page_cache_async_readahead(struct address_space *, struct file_ra_state *,
740 struct file *, struct page *, pgoff_t index,
741 unsigned long req_count);
742 void page_cache_readahead_unbounded(struct address_space *, struct file *,
743 pgoff_t index, unsigned long nr_to_read,
744 unsigned long lookahead_count);
747 * Like add_to_page_cache_locked, but used to add newly allocated pages:
748 * the page is new, so we can just run __SetPageLocked() against it.
750 static inline int add_to_page_cache(struct page *page,
751 struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
755 __SetPageLocked(page);
756 error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
758 __ClearPageLocked(page);
763 * struct readahead_control - Describes a readahead request.
765 * A readahead request is for consecutive pages. Filesystems which
766 * implement the ->readahead method should call readahead_page() or
767 * readahead_page_batch() in a loop and attempt to start I/O against
768 * each page in the request.
770 * Most of the fields in this struct are private and should be accessed
771 * by the functions below.
773 * @file: The file, used primarily by network filesystems for authentication.
774 * May be NULL if invoked internally by the filesystem.
775 * @mapping: Readahead this filesystem object.
777 struct readahead_control {
779 struct address_space *mapping;
780 /* private: use the readahead_* accessors instead */
782 unsigned int _nr_pages;
783 unsigned int _batch_count;
787 * readahead_page - Get the next page to read.
788 * @rac: The current readahead request.
790 * Context: The page is locked and has an elevated refcount. The caller
791 * should decreases the refcount once the page has been submitted for I/O
792 * and unlock the page once all I/O to that page has completed.
793 * Return: A pointer to the next page, or %NULL if we are done.
795 static inline struct page *readahead_page(struct readahead_control *rac)
799 BUG_ON(rac->_batch_count > rac->_nr_pages);
800 rac->_nr_pages -= rac->_batch_count;
801 rac->_index += rac->_batch_count;
803 if (!rac->_nr_pages) {
804 rac->_batch_count = 0;
808 page = xa_load(&rac->mapping->i_pages, rac->_index);
809 VM_BUG_ON_PAGE(!PageLocked(page), page);
810 rac->_batch_count = thp_nr_pages(page);
815 static inline unsigned int __readahead_batch(struct readahead_control *rac,
816 struct page **array, unsigned int array_sz)
819 XA_STATE(xas, &rac->mapping->i_pages, 0);
822 BUG_ON(rac->_batch_count > rac->_nr_pages);
823 rac->_nr_pages -= rac->_batch_count;
824 rac->_index += rac->_batch_count;
825 rac->_batch_count = 0;
827 xas_set(&xas, rac->_index);
829 xas_for_each(&xas, page, rac->_index + rac->_nr_pages - 1) {
830 VM_BUG_ON_PAGE(!PageLocked(page), page);
831 VM_BUG_ON_PAGE(PageTail(page), page);
833 rac->_batch_count += thp_nr_pages(page);
836 * The page cache isn't using multi-index entries yet,
837 * so the xas cursor needs to be manually moved to the
838 * next index. This can be removed once the page cache
842 xas_set(&xas, rac->_index + rac->_batch_count);
853 * readahead_page_batch - Get a batch of pages to read.
854 * @rac: The current readahead request.
855 * @array: An array of pointers to struct page.
857 * Context: The pages are locked and have an elevated refcount. The caller
858 * should decreases the refcount once the page has been submitted for I/O
859 * and unlock the page once all I/O to that page has completed.
860 * Return: The number of pages placed in the array. 0 indicates the request
863 #define readahead_page_batch(rac, array) \
864 __readahead_batch(rac, array, ARRAY_SIZE(array))
867 * readahead_pos - The byte offset into the file of this readahead request.
868 * @rac: The readahead request.
870 static inline loff_t readahead_pos(struct readahead_control *rac)
872 return (loff_t)rac->_index * PAGE_SIZE;
876 * readahead_length - The number of bytes in this readahead request.
877 * @rac: The readahead request.
879 static inline loff_t readahead_length(struct readahead_control *rac)
881 return (loff_t)rac->_nr_pages * PAGE_SIZE;
885 * readahead_index - The index of the first page in this readahead request.
886 * @rac: The readahead request.
888 static inline pgoff_t readahead_index(struct readahead_control *rac)
894 * readahead_count - The number of pages in this readahead request.
895 * @rac: The readahead request.
897 static inline unsigned int readahead_count(struct readahead_control *rac)
899 return rac->_nr_pages;
902 static inline unsigned long dir_pages(struct inode *inode)
904 return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
909 * page_mkwrite_check_truncate - check if page was truncated
910 * @page: the page to check
911 * @inode: the inode to check the page against
913 * Returns the number of bytes in the page up to EOF,
914 * or -EFAULT if the page was truncated.
916 static inline int page_mkwrite_check_truncate(struct page *page,
919 loff_t size = i_size_read(inode);
920 pgoff_t index = size >> PAGE_SHIFT;
921 int offset = offset_in_page(size);
923 if (page->mapping != inode->i_mapping)
926 /* page is wholly inside EOF */
927 if (page->index < index)
929 /* page is wholly past EOF */
930 if (page->index > index || !offset)
932 /* page is partially inside EOF */
937 * i_blocks_per_page - How many blocks fit in this page.
938 * @inode: The inode which contains the blocks.
939 * @page: The page (head page if the page is a THP).
941 * If the block size is larger than the size of this page, return zero.
943 * Context: The caller should hold a refcount on the page to prevent it
945 * Return: The number of filesystem blocks covered by this page.
948 unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
950 return thp_size(page) >> inode->i_blkbits;
952 #endif /* _LINUX_PAGEMAP_H */