pagemap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #ifndef _LINUX_PAGEMAP_H
  2. #define _LINUX_PAGEMAP_H
  3. /*
  4. * Copyright 1995 Linus Torvalds
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/fs.h>
  8. #include <linux/list.h>
  9. #include <linux/highmem.h>
  10. #include <linux/compiler.h>
  11. #include <asm/uaccess.h>
  12. #include <linux/gfp.h>
  13. #include <linux/bitops.h>
  14. #include <linux/hardirq.h> /* for in_interrupt() */
  15. /*
  16. * Bits in mapping->flags. The lower __GFP_BITS_SHIFT bits are the page
  17. * allocation mode flags.
  18. */
  19. #define AS_EIO (__GFP_BITS_SHIFT + 0) /* IO error on async write */
  20. #define AS_ENOSPC (__GFP_BITS_SHIFT + 1) /* ENOSPC on async write */
  21. static inline void mapping_set_error(struct address_space *mapping, int error)
  22. {
  23. if (unlikely(error)) {
  24. if (error == -ENOSPC)
  25. set_bit(AS_ENOSPC, &mapping->flags);
  26. else
  27. set_bit(AS_EIO, &mapping->flags);
  28. }
  29. }
  30. static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
  31. {
  32. return (__force gfp_t)mapping->flags & __GFP_BITS_MASK;
  33. }
  34. /*
  35. * This is non-atomic. Only to be used before the mapping is activated.
  36. * Probably needs a barrier...
  37. */
  38. static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
  39. {
  40. m->flags = (m->flags & ~(__force unsigned long)__GFP_BITS_MASK) |
  41. (__force unsigned long)mask;
  42. }
  43. /*
  44. * The page cache can done in larger chunks than
  45. * one page, because it allows for more efficient
  46. * throughput (it can then be mapped into user
  47. * space in smaller chunks for same flexibility).
  48. *
  49. * Or rather, it _will_ be done in larger chunks.
  50. */
  51. #define PAGE_CACHE_SHIFT PAGE_SHIFT
  52. #define PAGE_CACHE_SIZE PAGE_SIZE
  53. #define PAGE_CACHE_MASK PAGE_MASK
  54. #define PAGE_CACHE_ALIGN(addr) (((addr)+PAGE_CACHE_SIZE-1)&PAGE_CACHE_MASK)
  55. #define page_cache_get(page) get_page(page)
  56. #define page_cache_release(page) put_page(page)
  57. void release_pages(struct page **pages, int nr, int cold);
  58. /*
  59. * speculatively take a reference to a page.
  60. * If the page is free (_count == 0), then _count is untouched, and 0
  61. * is returned. Otherwise, _count is incremented by 1 and 1 is returned.
  62. *
  63. * This function must be called inside the same rcu_read_lock() section as has
  64. * been used to lookup the page in the pagecache radix-tree (or page table):
  65. * this allows allocators to use a synchronize_rcu() to stabilize _count.
  66. *
  67. * Unless an RCU grace period has passed, the count of all pages coming out
  68. * of the allocator must be considered unstable. page_count may return higher
  69. * than expected, and put_page must be able to do the right thing when the
  70. * page has been finished with, no matter what it is subsequently allocated
  71. * for (because put_page is what is used here to drop an invalid speculative
  72. * reference).
  73. *
  74. * This is the interesting part of the lockless pagecache (and lockless
  75. * get_user_pages) locking protocol, where the lookup-side (eg. find_get_page)
  76. * has the following pattern:
  77. * 1. find page in radix tree
  78. * 2. conditionally increment refcount
  79. * 3. check the page is still in pagecache (if no, goto 1)
  80. *
  81. * Remove-side that cares about stability of _count (eg. reclaim) has the
  82. * following (with tree_lock held for write):
  83. * A. atomically check refcount is correct and set it to 0 (atomic_cmpxchg)
  84. * B. remove page from pagecache
  85. * C. free the page
  86. *
  87. * There are 2 critical interleavings that matter:
  88. * - 2 runs before A: in this case, A sees elevated refcount and bails out
  89. * - A runs before 2: in this case, 2 sees zero refcount and retries;
  90. * subsequently, B will complete and 1 will find no page, causing the
  91. * lookup to return NULL.
  92. *
  93. * It is possible that between 1 and 2, the page is removed then the exact same
  94. * page is inserted into the same position in pagecache. That's OK: the
  95. * old find_get_page using tree_lock could equally have run before or after
  96. * such a re-insertion, depending on order that locks are granted.
  97. *
  98. * Lookups racing against pagecache insertion isn't a big problem: either 1
  99. * will find the page or it will not. Likewise, the old find_get_page could run
  100. * either before the insertion or afterwards, depending on timing.
  101. */
  102. static inline int page_cache_get_speculative(struct page *page)
  103. {
  104. VM_BUG_ON(in_interrupt());
  105. #if !defined(CONFIG_SMP) && defined(CONFIG_CLASSIC_RCU)
  106. # ifdef CONFIG_PREEMPT
  107. VM_BUG_ON(!in_atomic());
  108. # endif
  109. /*
  110. * Preempt must be disabled here - we rely on rcu_read_lock doing
  111. * this for us.
  112. *
  113. * Pagecache won't be truncated from interrupt context, so if we have
  114. * found a page in the radix tree here, we have pinned its refcount by
  115. * disabling preempt, and hence no need for the "speculative get" that
  116. * SMP requires.
  117. */
  118. VM_BUG_ON(page_count(page) == 0);
  119. atomic_inc(&page->_count);
  120. #else
  121. if (unlikely(!get_page_unless_zero(page))) {
  122. /*
  123. * Either the page has been freed, or will be freed.
  124. * In either case, retry here and the caller should
  125. * do the right thing (see comments above).
  126. */
  127. return 0;
  128. }
  129. #endif
  130. VM_BUG_ON(PageTail(page));
  131. return 1;
  132. }
  133. static inline int page_freeze_refs(struct page *page, int count)
  134. {
  135. return likely(atomic_cmpxchg(&page->_count, count, 0) == count);
  136. }
  137. static inline void page_unfreeze_refs(struct page *page, int count)
  138. {
  139. VM_BUG_ON(page_count(page) != 0);
  140. VM_BUG_ON(count == 0);
  141. atomic_set(&page->_count, count);
  142. }
  143. #ifdef CONFIG_NUMA
  144. extern struct page *__page_cache_alloc(gfp_t gfp);
  145. #else
  146. static inline struct page *__page_cache_alloc(gfp_t gfp)
  147. {
  148. return alloc_pages(gfp, 0);
  149. }
  150. #endif
  151. static inline struct page *page_cache_alloc(struct address_space *x)
  152. {
  153. return __page_cache_alloc(mapping_gfp_mask(x));
  154. }
  155. static inline struct page *page_cache_alloc_cold(struct address_space *x)
  156. {
  157. return __page_cache_alloc(mapping_gfp_mask(x)|__GFP_COLD);
  158. }
  159. typedef int filler_t(void *, struct page *);
  160. extern struct page * find_get_page(struct address_space *mapping,
  161. pgoff_t index);
  162. extern struct page * find_lock_page(struct address_space *mapping,
  163. pgoff_t index);
  164. extern struct page * find_or_create_page(struct address_space *mapping,
  165. pgoff_t index, gfp_t gfp_mask);
  166. unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
  167. unsigned int nr_pages, struct page **pages);
  168. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
  169. unsigned int nr_pages, struct page **pages);
  170. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  171. int tag, unsigned int nr_pages, struct page **pages);
  172. struct page *__grab_cache_page(struct address_space *mapping, pgoff_t index);
  173. /*
  174. * Returns locked page at given index in given cache, creating it if needed.
  175. */
  176. static inline struct page *grab_cache_page(struct address_space *mapping,
  177. pgoff_t index)
  178. {
  179. return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
  180. }
  181. extern struct page * grab_cache_page_nowait(struct address_space *mapping,
  182. pgoff_t index);
  183. extern struct page * read_cache_page_async(struct address_space *mapping,
  184. pgoff_t index, filler_t *filler,
  185. void *data);
  186. extern struct page * read_cache_page(struct address_space *mapping,
  187. pgoff_t index, filler_t *filler,
  188. void *data);
  189. extern int read_cache_pages(struct address_space *mapping,
  190. struct list_head *pages, filler_t *filler, void *data);
  191. static inline struct page *read_mapping_page_async(
  192. struct address_space *mapping,
  193. pgoff_t index, void *data)
  194. {
  195. filler_t *filler = (filler_t *)mapping->a_ops->readpage;
  196. return read_cache_page_async(mapping, index, filler, data);
  197. }
  198. static inline struct page *read_mapping_page(struct address_space *mapping,
  199. pgoff_t index, void *data)
  200. {
  201. filler_t *filler = (filler_t *)mapping->a_ops->readpage;
  202. return read_cache_page(mapping, index, filler, data);
  203. }
  204. int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
  205. pgoff_t index, gfp_t gfp_mask);
  206. int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
  207. pgoff_t index, gfp_t gfp_mask);
  208. extern void remove_from_page_cache(struct page *page);
  209. extern void __remove_from_page_cache(struct page *page);
  210. /*
  211. * Like add_to_page_cache_locked, but used to add newly allocated pages:
  212. * the page is new, so we can just run SetPageLocked() against it.
  213. */
  214. static inline int add_to_page_cache(struct page *page,
  215. struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
  216. {
  217. int error;
  218. SetPageLocked(page);
  219. error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
  220. if (unlikely(error))
  221. ClearPageLocked(page);
  222. return error;
  223. }
  224. /*
  225. * Return byte-offset into filesystem object for page.
  226. */
  227. static inline loff_t page_offset(struct page *page)
  228. {
  229. return ((loff_t)page->index) << PAGE_CACHE_SHIFT;
  230. }
  231. static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
  232. unsigned long address)
  233. {
  234. pgoff_t pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
  235. pgoff += vma->vm_pgoff;
  236. return pgoff >> (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  237. }
  238. extern void __lock_page(struct page *page);
  239. extern int __lock_page_killable(struct page *page);
  240. extern void __lock_page_nosync(struct page *page);
  241. extern void unlock_page(struct page *page);
  242. /*
  243. * lock_page may only be called if we have the page's inode pinned.
  244. */
  245. static inline void lock_page(struct page *page)
  246. {
  247. might_sleep();
  248. if (TestSetPageLocked(page))
  249. __lock_page(page);
  250. }
  251. /*
  252. * lock_page_killable is like lock_page but can be interrupted by fatal
  253. * signals. It returns 0 if it locked the page and -EINTR if it was
  254. * killed while waiting.
  255. */
  256. static inline int lock_page_killable(struct page *page)
  257. {
  258. might_sleep();
  259. if (TestSetPageLocked(page))
  260. return __lock_page_killable(page);
  261. return 0;
  262. }
  263. /*
  264. * lock_page_nosync should only be used if we can't pin the page's inode.
  265. * Doesn't play quite so well with block device plugging.
  266. */
  267. static inline void lock_page_nosync(struct page *page)
  268. {
  269. might_sleep();
  270. if (TestSetPageLocked(page))
  271. __lock_page_nosync(page);
  272. }
  273. /*
  274. * This is exported only for wait_on_page_locked/wait_on_page_writeback.
  275. * Never use this directly!
  276. */
  277. extern void wait_on_page_bit(struct page *page, int bit_nr);
  278. /*
  279. * Wait for a page to be unlocked.
  280. *
  281. * This must be called with the caller "holding" the page,
  282. * ie with increased "page->count" so that the page won't
  283. * go away during the wait..
  284. */
  285. static inline void wait_on_page_locked(struct page *page)
  286. {
  287. if (PageLocked(page))
  288. wait_on_page_bit(page, PG_locked);
  289. }
  290. /*
  291. * Wait for a page to complete writeback
  292. */
  293. static inline void wait_on_page_writeback(struct page *page)
  294. {
  295. if (PageWriteback(page))
  296. wait_on_page_bit(page, PG_writeback);
  297. }
  298. extern void end_page_writeback(struct page *page);
  299. /*
  300. * Fault a userspace page into pagetables. Return non-zero on a fault.
  301. *
  302. * This assumes that two userspace pages are always sufficient. That's
  303. * not true if PAGE_CACHE_SIZE > PAGE_SIZE.
  304. */
  305. static inline int fault_in_pages_writeable(char __user *uaddr, int size)
  306. {
  307. int ret;
  308. if (unlikely(size == 0))
  309. return 0;
  310. /*
  311. * Writing zeroes into userspace here is OK, because we know that if
  312. * the zero gets there, we'll be overwriting it.
  313. */
  314. ret = __put_user(0, uaddr);
  315. if (ret == 0) {
  316. char __user *end = uaddr + size - 1;
  317. /*
  318. * If the page was already mapped, this will get a cache miss
  319. * for sure, so try to avoid doing it.
  320. */
  321. if (((unsigned long)uaddr & PAGE_MASK) !=
  322. ((unsigned long)end & PAGE_MASK))
  323. ret = __put_user(0, end);
  324. }
  325. return ret;
  326. }
  327. static inline int fault_in_pages_readable(const char __user *uaddr, int size)
  328. {
  329. volatile char c;
  330. int ret;
  331. if (unlikely(size == 0))
  332. return 0;
  333. ret = __get_user(c, uaddr);
  334. if (ret == 0) {
  335. const char __user *end = uaddr + size - 1;
  336. if (((unsigned long)uaddr & PAGE_MASK) !=
  337. ((unsigned long)end & PAGE_MASK))
  338. ret = __get_user(c, end);
  339. }
  340. return ret;
  341. }
  342. #endif /* _LINUX_PAGEMAP_H */