pagemap.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /*
  14. * Bits in mapping->flags. The lower __GFP_BITS_SHIFT bits are the page
  15. * allocation mode flags.
  16. */
  17. #define AS_EIO (__GFP_BITS_SHIFT + 0) /* IO error on async write */
  18. #define AS_ENOSPC (__GFP_BITS_SHIFT + 1) /* ENOSPC on async write */
  19. static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
  20. {
  21. return (__force gfp_t)mapping->flags & __GFP_BITS_MASK;
  22. }
  23. /*
  24. * This is non-atomic. Only to be used before the mapping is activated.
  25. * Probably needs a barrier...
  26. */
  27. static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
  28. {
  29. m->flags = (m->flags & ~(__force unsigned long)__GFP_BITS_MASK) |
  30. (__force unsigned long)mask;
  31. }
  32. /*
  33. * The page cache can done in larger chunks than
  34. * one page, because it allows for more efficient
  35. * throughput (it can then be mapped into user
  36. * space in smaller chunks for same flexibility).
  37. *
  38. * Or rather, it _will_ be done in larger chunks.
  39. */
  40. #define PAGE_CACHE_SHIFT PAGE_SHIFT
  41. #define PAGE_CACHE_SIZE PAGE_SIZE
  42. #define PAGE_CACHE_MASK PAGE_MASK
  43. #define PAGE_CACHE_ALIGN(addr) (((addr)+PAGE_CACHE_SIZE-1)&PAGE_CACHE_MASK)
  44. #define page_cache_get(page) get_page(page)
  45. #define page_cache_release(page) put_page(page)
  46. void release_pages(struct page **pages, int nr, int cold);
  47. #ifdef CONFIG_NUMA
  48. extern struct page *page_cache_alloc(struct address_space *x);
  49. extern struct page *page_cache_alloc_cold(struct address_space *x);
  50. #else
  51. static inline struct page *page_cache_alloc(struct address_space *x)
  52. {
  53. return alloc_pages(mapping_gfp_mask(x), 0);
  54. }
  55. static inline struct page *page_cache_alloc_cold(struct address_space *x)
  56. {
  57. return alloc_pages(mapping_gfp_mask(x)|__GFP_COLD, 0);
  58. }
  59. #endif
  60. typedef int filler_t(void *, struct page *);
  61. extern struct page * find_get_page(struct address_space *mapping,
  62. unsigned long index);
  63. extern struct page * find_lock_page(struct address_space *mapping,
  64. unsigned long index);
  65. extern __deprecated_for_modules struct page * find_trylock_page(
  66. struct address_space *mapping, unsigned long index);
  67. extern struct page * find_or_create_page(struct address_space *mapping,
  68. unsigned long index, gfp_t gfp_mask);
  69. unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
  70. unsigned int nr_pages, struct page **pages);
  71. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
  72. unsigned int nr_pages, struct page **pages);
  73. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  74. int tag, unsigned int nr_pages, struct page **pages);
  75. /*
  76. * Returns locked page at given index in given cache, creating it if needed.
  77. */
  78. static inline struct page *grab_cache_page(struct address_space *mapping, unsigned long index)
  79. {
  80. return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
  81. }
  82. extern struct page * grab_cache_page_nowait(struct address_space *mapping,
  83. unsigned long index);
  84. extern struct page * read_cache_page(struct address_space *mapping,
  85. unsigned long index, filler_t *filler,
  86. void *data);
  87. extern int read_cache_pages(struct address_space *mapping,
  88. struct list_head *pages, filler_t *filler, void *data);
  89. int add_to_page_cache(struct page *page, struct address_space *mapping,
  90. unsigned long index, gfp_t gfp_mask);
  91. int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
  92. unsigned long index, gfp_t gfp_mask);
  93. extern void remove_from_page_cache(struct page *page);
  94. extern void __remove_from_page_cache(struct page *page);
  95. extern atomic_t nr_pagecache;
  96. #ifdef CONFIG_SMP
  97. #define PAGECACHE_ACCT_THRESHOLD max(16, NR_CPUS * 2)
  98. DECLARE_PER_CPU(long, nr_pagecache_local);
  99. /*
  100. * pagecache_acct implements approximate accounting for pagecache.
  101. * vm_enough_memory() do not need high accuracy. Writers will keep
  102. * an offset in their per-cpu arena and will spill that into the
  103. * global count whenever the absolute value of the local count
  104. * exceeds the counter's threshold.
  105. *
  106. * MUST be protected from preemption.
  107. * current protection is mapping->page_lock.
  108. */
  109. static inline void pagecache_acct(int count)
  110. {
  111. long *local;
  112. local = &__get_cpu_var(nr_pagecache_local);
  113. *local += count;
  114. if (*local > PAGECACHE_ACCT_THRESHOLD || *local < -PAGECACHE_ACCT_THRESHOLD) {
  115. atomic_add(*local, &nr_pagecache);
  116. *local = 0;
  117. }
  118. }
  119. #else
  120. static inline void pagecache_acct(int count)
  121. {
  122. atomic_add(count, &nr_pagecache);
  123. }
  124. #endif
  125. static inline unsigned long get_page_cache_size(void)
  126. {
  127. int ret = atomic_read(&nr_pagecache);
  128. if (unlikely(ret < 0))
  129. ret = 0;
  130. return ret;
  131. }
  132. /*
  133. * Return byte-offset into filesystem object for page.
  134. */
  135. static inline loff_t page_offset(struct page *page)
  136. {
  137. return ((loff_t)page->index) << PAGE_CACHE_SHIFT;
  138. }
  139. static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
  140. unsigned long address)
  141. {
  142. pgoff_t pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
  143. pgoff += vma->vm_pgoff;
  144. return pgoff >> (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  145. }
  146. extern void FASTCALL(__lock_page(struct page *page));
  147. extern void FASTCALL(unlock_page(struct page *page));
  148. static inline void lock_page(struct page *page)
  149. {
  150. might_sleep();
  151. if (TestSetPageLocked(page))
  152. __lock_page(page);
  153. }
  154. /*
  155. * This is exported only for wait_on_page_locked/wait_on_page_writeback.
  156. * Never use this directly!
  157. */
  158. extern void FASTCALL(wait_on_page_bit(struct page *page, int bit_nr));
  159. /*
  160. * Wait for a page to be unlocked.
  161. *
  162. * This must be called with the caller "holding" the page,
  163. * ie with increased "page->count" so that the page won't
  164. * go away during the wait..
  165. */
  166. static inline void wait_on_page_locked(struct page *page)
  167. {
  168. if (PageLocked(page))
  169. wait_on_page_bit(page, PG_locked);
  170. }
  171. /*
  172. * Wait for a page to complete writeback
  173. */
  174. static inline void wait_on_page_writeback(struct page *page)
  175. {
  176. if (PageWriteback(page))
  177. wait_on_page_bit(page, PG_writeback);
  178. }
  179. extern void end_page_writeback(struct page *page);
  180. /*
  181. * Fault a userspace page into pagetables. Return non-zero on a fault.
  182. *
  183. * This assumes that two userspace pages are always sufficient. That's
  184. * not true if PAGE_CACHE_SIZE > PAGE_SIZE.
  185. */
  186. static inline int fault_in_pages_writeable(char __user *uaddr, int size)
  187. {
  188. int ret;
  189. /*
  190. * Writing zeroes into userspace here is OK, because we know that if
  191. * the zero gets there, we'll be overwriting it.
  192. */
  193. ret = __put_user(0, uaddr);
  194. if (ret == 0) {
  195. char __user *end = uaddr + size - 1;
  196. /*
  197. * If the page was already mapped, this will get a cache miss
  198. * for sure, so try to avoid doing it.
  199. */
  200. if (((unsigned long)uaddr & PAGE_MASK) !=
  201. ((unsigned long)end & PAGE_MASK))
  202. ret = __put_user(0, end);
  203. }
  204. return ret;
  205. }
  206. static inline void fault_in_pages_readable(const char __user *uaddr, int size)
  207. {
  208. volatile char c;
  209. int ret;
  210. ret = __get_user(c, uaddr);
  211. if (ret == 0) {
  212. const char __user *end = uaddr + size - 1;
  213. if (((unsigned long)uaddr & PAGE_MASK) !=
  214. ((unsigned long)end & PAGE_MASK))
  215. __get_user(c, end);
  216. }
  217. }
  218. #endif /* _LINUX_PAGEMAP_H */