820c970fd24a5502023f7f6ecda09935743f96ee
[linux-2.6-microblaze.git] / include / linux / pagemap.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGEMAP_H
3 #define _LINUX_PAGEMAP_H
4
5 /*
6  * Copyright 1995 Linus Torvalds
7  */
8 #include <linux/mm.h>
9 #include <linux/fs.h>
10 #include <linux/list.h>
11 #include <linux/highmem.h>
12 #include <linux/compiler.h>
13 #include <linux/uaccess.h>
14 #include <linux/gfp.h>
15 #include <linux/bitops.h>
16 #include <linux/hardirq.h> /* for in_interrupt() */
17 #include <linux/hugetlb_inline.h>
18
19 struct pagevec;
20
21 /*
22  * Bits in mapping->flags.
23  */
24 enum 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 */
33 };
34
35 /**
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
39  *
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.
44  *
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).
48  */
49 static inline void mapping_set_error(struct address_space *mapping, int error)
50 {
51         if (likely(!error))
52                 return;
53
54         /* Record in wb_err for checkers using errseq_t based tracking */
55         __filemap_set_wb_err(mapping, error);
56
57         /* Record it in superblock */
58         if (mapping->host)
59                 errseq_set(&mapping->host->i_sb->s_wb_err, error);
60
61         /* Record it in flags for now, for legacy callers */
62         if (error == -ENOSPC)
63                 set_bit(AS_ENOSPC, &mapping->flags);
64         else
65                 set_bit(AS_EIO, &mapping->flags);
66 }
67
68 static inline void mapping_set_unevictable(struct address_space *mapping)
69 {
70         set_bit(AS_UNEVICTABLE, &mapping->flags);
71 }
72
73 static inline void mapping_clear_unevictable(struct address_space *mapping)
74 {
75         clear_bit(AS_UNEVICTABLE, &mapping->flags);
76 }
77
78 static inline bool mapping_unevictable(struct address_space *mapping)
79 {
80         return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags);
81 }
82
83 static inline void mapping_set_exiting(struct address_space *mapping)
84 {
85         set_bit(AS_EXITING, &mapping->flags);
86 }
87
88 static inline int mapping_exiting(struct address_space *mapping)
89 {
90         return test_bit(AS_EXITING, &mapping->flags);
91 }
92
93 static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
94 {
95         set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
96 }
97
98 static inline int mapping_use_writeback_tags(struct address_space *mapping)
99 {
100         return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
101 }
102
103 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
104 {
105         return mapping->gfp_mask;
106 }
107
108 /* Restricts the given gfp_mask to what the mapping allows. */
109 static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
110                 gfp_t gfp_mask)
111 {
112         return mapping_gfp_mask(mapping) & gfp_mask;
113 }
114
115 /*
116  * This is non-atomic.  Only to be used before the mapping is activated.
117  * Probably needs a barrier...
118  */
119 static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
120 {
121         m->gfp_mask = mask;
122 }
123
124 static inline bool mapping_thp_support(struct address_space *mapping)
125 {
126         return test_bit(AS_THP_SUPPORT, &mapping->flags);
127 }
128
129 void release_pages(struct page **pages, int nr);
130
131 /*
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.
135  *
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.
139  *
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
145  * reference).
146  *
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)
153  *
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
158  * C. free the page
159  *
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.
165  *
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.
170  *
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.
174  */
175 static inline int __page_cache_add_speculative(struct page *page, int count)
176 {
177 #ifdef CONFIG_TINY_RCU
178 # ifdef CONFIG_PREEMPT_COUNT
179         VM_BUG_ON(!in_atomic() && !irqs_disabled());
180 # endif
181         /*
182          * Preempt must be disabled here - we rely on rcu_read_lock doing
183          * this for us.
184          *
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
188          * SMP requires.
189          */
190         VM_BUG_ON_PAGE(page_count(page) == 0, page);
191         page_ref_add(page, count);
192
193 #else
194         if (unlikely(!page_ref_add_unless(page, count, 0))) {
195                 /*
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).
199                  */
200                 return 0;
201         }
202 #endif
203         VM_BUG_ON_PAGE(PageTail(page), page);
204
205         return 1;
206 }
207
208 static inline int page_cache_get_speculative(struct page *page)
209 {
210         return __page_cache_add_speculative(page, 1);
211 }
212
213 static inline int page_cache_add_speculative(struct page *page, int count)
214 {
215         return __page_cache_add_speculative(page, count);
216 }
217
218 /**
219  * attach_page_private - Attach private data to a page.
220  * @page: Page to attach data to.
221  * @data: Data to attach to page.
222  *
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.
225  */
226 static inline void attach_page_private(struct page *page, void *data)
227 {
228         get_page(page);
229         set_page_private(page, (unsigned long)data);
230         SetPagePrivate(page);
231 }
232
233 /**
234  * detach_page_private - Detach private data from a page.
235  * @page: Page to detach data from.
236  *
237  * Removes the data that was previously attached to the page and decrements
238  * the refcount on the page.
239  *
240  * Return: Data that was attached to the page.
241  */
242 static inline void *detach_page_private(struct page *page)
243 {
244         void *data = (void *)page_private(page);
245
246         if (!PagePrivate(page))
247                 return NULL;
248         ClearPagePrivate(page);
249         set_page_private(page, 0);
250         put_page(page);
251
252         return data;
253 }
254
255 #ifdef CONFIG_NUMA
256 extern struct page *__page_cache_alloc(gfp_t gfp);
257 #else
258 static inline struct page *__page_cache_alloc(gfp_t gfp)
259 {
260         return alloc_pages(gfp, 0);
261 }
262 #endif
263
264 static inline struct page *page_cache_alloc(struct address_space *x)
265 {
266         return __page_cache_alloc(mapping_gfp_mask(x));
267 }
268
269 static inline gfp_t readahead_gfp_mask(struct address_space *x)
270 {
271         return mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN;
272 }
273
274 typedef int filler_t(void *, struct page *);
275
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);
280
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
289
290 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
291                 int fgp_flags, gfp_t cache_gfp_mask);
292
293 /**
294  * find_get_page - find and get a page reference
295  * @mapping: the address_space to search
296  * @offset: the page index
297  *
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.
300  *
301  * Otherwise, %NULL is returned.
302  */
303 static inline struct page *find_get_page(struct address_space *mapping,
304                                         pgoff_t offset)
305 {
306         return pagecache_get_page(mapping, offset, 0, 0);
307 }
308
309 static inline struct page *find_get_page_flags(struct address_space *mapping,
310                                         pgoff_t offset, int fgp_flags)
311 {
312         return pagecache_get_page(mapping, offset, fgp_flags, 0);
313 }
314
315 /**
316  * find_lock_page - locate, pin and lock a pagecache page
317  * @mapping: the address_space to search
318  * @offset: the page index
319  *
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
322  * refcount.
323  *
324  * Context: May sleep.
325  * Return: A struct page or %NULL if there is no page in the cache for this
326  * index.
327  */
328 static inline struct page *find_lock_page(struct address_space *mapping,
329                                         pgoff_t index)
330 {
331         return pagecache_get_page(mapping, index, FGP_LOCK, 0);
332 }
333
334 /**
335  * find_lock_head - Locate, pin and lock a pagecache page.
336  * @mapping: The address_space to search.
337  * @offset: The page index.
338  *
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
341  * refcount.
342  *
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.
346  */
347 static inline struct page *find_lock_head(struct address_space *mapping,
348                                         pgoff_t index)
349 {
350         return pagecache_get_page(mapping, index, FGP_LOCK | FGP_HEAD, 0);
351 }
352
353 /**
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
358  *
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
361  * refcount.
362  *
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.
366  *
367  * On memory exhaustion, %NULL is returned.
368  *
369  * find_or_create_page() may sleep, even if @gfp_flags specifies an
370  * atomic allocation!
371  */
372 static inline struct page *find_or_create_page(struct address_space *mapping,
373                                         pgoff_t index, gfp_t gfp_mask)
374 {
375         return pagecache_get_page(mapping, index,
376                                         FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
377                                         gfp_mask);
378 }
379
380 /**
381  * grab_cache_page_nowait - returns locked page at given index in given cache
382  * @mapping: target address_space
383  * @index: the page index
384  *
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.
389  *
390  * Clear __GFP_FS when allocating the page to avoid recursion into the fs
391  * and deadlock against the caller's locked page.
392  */
393 static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
394                                 pgoff_t index)
395 {
396         return pagecache_get_page(mapping, index,
397                         FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
398                         mapping_gfp_mask(mapping));
399 }
400
401 /* Does this page contain this index? */
402 static inline bool thp_contains(struct page *head, pgoff_t index)
403 {
404         /* HugeTLBfs indexes the page cache in units of hpage_size */
405         if (PageHuge(head))
406                 return head->index == index;
407         return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL));
408 }
409
410 /*
411  * Given the page we found in the page cache, return the page corresponding
412  * to this index in the file
413  */
414 static inline struct page *find_subpage(struct page *head, pgoff_t index)
415 {
416         /* HugeTLBfs wants the head page regardless */
417         if (PageHuge(head))
418                 return head;
419
420         return head + (index & (thp_nr_pages(head) - 1));
421 }
422
423 unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
424                           unsigned int nr_entries, struct page **entries,
425                           pgoff_t *indices);
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,
431                         struct page **pages)
432 {
433         return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
434                                     pages);
435 }
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,
443                         struct page **pages)
444 {
445         return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
446                                         nr_pages, pages);
447 }
448
449 struct page *grab_cache_page_write_begin(struct address_space *mapping,
450                         pgoff_t index, unsigned flags);
451
452 /*
453  * Returns locked page at given index in given cache, creating it if needed.
454  */
455 static inline struct page *grab_cache_page(struct address_space *mapping,
456                                                                 pgoff_t index)
457 {
458         return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
459 }
460
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);
467
468 static inline struct page *read_mapping_page(struct address_space *mapping,
469                                 pgoff_t index, void *data)
470 {
471         return read_cache_page(mapping, index, NULL, data);
472 }
473
474 /*
475  * Get index of the page with in radix-tree
476  * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
477  */
478 static inline pgoff_t page_to_index(struct page *page)
479 {
480         pgoff_t pgoff;
481
482         if (likely(!PageTransTail(page)))
483                 return page->index;
484
485         /*
486          *  We don't initialize ->index for tail pages: calculate based on
487          *  head page
488          */
489         pgoff = compound_head(page)->index;
490         pgoff += page - compound_head(page);
491         return pgoff;
492 }
493
494 /*
495  * Get the offset in PAGE_SIZE.
496  * (TODO: hugepage should have ->index in PAGE_SIZE)
497  */
498 static inline pgoff_t page_to_pgoff(struct page *page)
499 {
500         if (unlikely(PageHeadHuge(page)))
501                 return page->index << compound_order(page);
502
503         return page_to_index(page);
504 }
505
506 /*
507  * Return byte-offset into filesystem object for page.
508  */
509 static inline loff_t page_offset(struct page *page)
510 {
511         return ((loff_t)page->index) << PAGE_SHIFT;
512 }
513
514 static inline loff_t page_file_offset(struct page *page)
515 {
516         return ((loff_t)page_index(page)) << PAGE_SHIFT;
517 }
518
519 extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
520                                      unsigned long address);
521
522 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
523                                         unsigned long address)
524 {
525         pgoff_t pgoff;
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;
530         return pgoff;
531 }
532
533 /* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
534 struct wait_page_key {
535         struct page *page;
536         int bit_nr;
537         int page_match;
538 };
539
540 struct wait_page_queue {
541         struct page *page;
542         int bit_nr;
543         wait_queue_entry_t wait;
544 };
545
546 static inline bool wake_page_match(struct wait_page_queue *wait_page,
547                                   struct wait_page_key *key)
548 {
549         if (wait_page->page != key->page)
550                return false;
551         key->page_match = 1;
552
553         if (wait_page->bit_nr != key->bit_nr)
554                 return false;
555
556         return true;
557 }
558
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,
563                                 unsigned int flags);
564 extern void unlock_page(struct page *page);
565
566 /*
567  * Return true if the page was successfully locked
568  */
569 static inline int trylock_page(struct page *page)
570 {
571         page = compound_head(page);
572         return (likely(!test_and_set_bit_lock(PG_locked, &page->flags)));
573 }
574
575 /*
576  * lock_page may only be called if we have the page's inode pinned.
577  */
578 static inline void lock_page(struct page *page)
579 {
580         might_sleep();
581         if (!trylock_page(page))
582                 __lock_page(page);
583 }
584
585 /*
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.
589  */
590 static inline int lock_page_killable(struct page *page)
591 {
592         might_sleep();
593         if (!trylock_page(page))
594                 return __lock_page_killable(page);
595         return 0;
596 }
597
598 /*
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.
602  *
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.
605  */
606 static inline int lock_page_async(struct page *page,
607                                   struct wait_page_queue *wait)
608 {
609         if (!trylock_page(page))
610                 return __lock_page_async(page, wait);
611         return 0;
612 }
613
614 /*
615  * lock_page_or_retry - Lock the page, unless this would block and the
616  * caller indicated that it can handle a retry.
617  *
618  * Return value and mmap_lock implications depend on flags; see
619  * __lock_page_or_retry().
620  */
621 static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
622                                      unsigned int flags)
623 {
624         might_sleep();
625         return trylock_page(page) || __lock_page_or_retry(page, mm, flags);
626 }
627
628 /*
629  * This is exported only for wait_on_page_locked/wait_on_page_writeback, etc.,
630  * and should not be used directly.
631  */
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);
634
635 /* 
636  * Wait for a page to be unlocked.
637  *
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..
641  */
642 static inline void wait_on_page_locked(struct page *page)
643 {
644         if (PageLocked(page))
645                 wait_on_page_bit(compound_head(page), PG_locked);
646 }
647
648 static inline int wait_on_page_locked_killable(struct page *page)
649 {
650         if (!PageLocked(page))
651                 return 0;
652         return wait_on_page_bit_killable(compound_head(page), PG_locked);
653 }
654
655 extern void put_and_wait_on_page_locked(struct page *page);
656
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);
660
661 void page_endio(struct page *page, bool is_write, int err);
662
663 /*
664  * Add an arbitrary waiter to a page's wait queue
665  */
666 extern void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter);
667
668 /*
669  * Fault everything in given userspace address range in.
670  */
671 static inline int fault_in_pages_writeable(char __user *uaddr, int size)
672 {
673         char __user *end = uaddr + size - 1;
674
675         if (unlikely(size == 0))
676                 return 0;
677
678         if (unlikely(uaddr > end))
679                 return -EFAULT;
680         /*
681          * Writing zeroes into userspace here is OK, because we know that if
682          * the zero gets there, we'll be overwriting it.
683          */
684         do {
685                 if (unlikely(__put_user(0, uaddr) != 0))
686                         return -EFAULT;
687                 uaddr += PAGE_SIZE;
688         } while (uaddr <= end);
689
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);
694
695         return 0;
696 }
697
698 static inline int fault_in_pages_readable(const char __user *uaddr, int size)
699 {
700         volatile char c;
701         const char __user *end = uaddr + size - 1;
702
703         if (unlikely(size == 0))
704                 return 0;
705
706         if (unlikely(uaddr > end))
707                 return -EFAULT;
708
709         do {
710                 if (unlikely(__get_user(c, uaddr) != 0))
711                         return -EFAULT;
712                 uaddr += PAGE_SIZE;
713         } while (uaddr <= end);
714
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);
719         }
720
721         (void)c;
722         return 0;
723 }
724
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);
734
735 #define VM_READAHEAD_PAGES      (SZ_128K / PAGE_SIZE)
736
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);
745
746 /*
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.
749  */
750 static inline int add_to_page_cache(struct page *page,
751                 struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
752 {
753         int error;
754
755         __SetPageLocked(page);
756         error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
757         if (unlikely(error))
758                 __ClearPageLocked(page);
759         return error;
760 }
761
762 /**
763  * struct readahead_control - Describes a readahead request.
764  *
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.
769  *
770  * Most of the fields in this struct are private and should be accessed
771  * by the functions below.
772  *
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.
776  */
777 struct readahead_control {
778         struct file *file;
779         struct address_space *mapping;
780 /* private: use the readahead_* accessors instead */
781         pgoff_t _index;
782         unsigned int _nr_pages;
783         unsigned int _batch_count;
784 };
785
786 /**
787  * readahead_page - Get the next page to read.
788  * @rac: The current readahead request.
789  *
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.
794  */
795 static inline struct page *readahead_page(struct readahead_control *rac)
796 {
797         struct page *page;
798
799         BUG_ON(rac->_batch_count > rac->_nr_pages);
800         rac->_nr_pages -= rac->_batch_count;
801         rac->_index += rac->_batch_count;
802
803         if (!rac->_nr_pages) {
804                 rac->_batch_count = 0;
805                 return NULL;
806         }
807
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);
811
812         return page;
813 }
814
815 static inline unsigned int __readahead_batch(struct readahead_control *rac,
816                 struct page **array, unsigned int array_sz)
817 {
818         unsigned int i = 0;
819         XA_STATE(xas, &rac->mapping->i_pages, 0);
820         struct page *page;
821
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;
826
827         xas_set(&xas, rac->_index);
828         rcu_read_lock();
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);
832                 array[i++] = page;
833                 rac->_batch_count += thp_nr_pages(page);
834
835                 /*
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
839                  * is converted.
840                  */
841                 if (PageHead(page))
842                         xas_set(&xas, rac->_index + rac->_batch_count);
843
844                 if (i == array_sz)
845                         break;
846         }
847         rcu_read_unlock();
848
849         return i;
850 }
851
852 /**
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.
856  *
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
861  * is complete.
862  */
863 #define readahead_page_batch(rac, array)                                \
864         __readahead_batch(rac, array, ARRAY_SIZE(array))
865
866 /**
867  * readahead_pos - The byte offset into the file of this readahead request.
868  * @rac: The readahead request.
869  */
870 static inline loff_t readahead_pos(struct readahead_control *rac)
871 {
872         return (loff_t)rac->_index * PAGE_SIZE;
873 }
874
875 /**
876  * readahead_length - The number of bytes in this readahead request.
877  * @rac: The readahead request.
878  */
879 static inline loff_t readahead_length(struct readahead_control *rac)
880 {
881         return (loff_t)rac->_nr_pages * PAGE_SIZE;
882 }
883
884 /**
885  * readahead_index - The index of the first page in this readahead request.
886  * @rac: The readahead request.
887  */
888 static inline pgoff_t readahead_index(struct readahead_control *rac)
889 {
890         return rac->_index;
891 }
892
893 /**
894  * readahead_count - The number of pages in this readahead request.
895  * @rac: The readahead request.
896  */
897 static inline unsigned int readahead_count(struct readahead_control *rac)
898 {
899         return rac->_nr_pages;
900 }
901
902 static inline unsigned long dir_pages(struct inode *inode)
903 {
904         return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
905                                PAGE_SHIFT;
906 }
907
908 /**
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
912  *
913  * Returns the number of bytes in the page up to EOF,
914  * or -EFAULT if the page was truncated.
915  */
916 static inline int page_mkwrite_check_truncate(struct page *page,
917                                               struct inode *inode)
918 {
919         loff_t size = i_size_read(inode);
920         pgoff_t index = size >> PAGE_SHIFT;
921         int offset = offset_in_page(size);
922
923         if (page->mapping != inode->i_mapping)
924                 return -EFAULT;
925
926         /* page is wholly inside EOF */
927         if (page->index < index)
928                 return PAGE_SIZE;
929         /* page is wholly past EOF */
930         if (page->index > index || !offset)
931                 return -EFAULT;
932         /* page is partially inside EOF */
933         return offset;
934 }
935
936 /**
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).
940  *
941  * If the block size is larger than the size of this page, return zero.
942  *
943  * Context: The caller should hold a refcount on the page to prevent it
944  * from being split.
945  * Return: The number of filesystem blocks covered by this page.
946  */
947 static inline
948 unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
949 {
950         return thp_size(page) >> inode->i_blkbits;
951 }
952 #endif /* _LINUX_PAGEMAP_H */