swap.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #ifndef _LINUX_SWAP_H
  2. #define _LINUX_SWAP_H
  3. #include <linux/spinlock.h>
  4. #include <linux/linkage.h>
  5. #include <linux/mmzone.h>
  6. #include <linux/list.h>
  7. #include <linux/sched.h>
  8. #include <asm/atomic.h>
  9. #include <asm/page.h>
  10. struct notifier_block;
  11. struct bio;
  12. #define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */
  13. #define SWAP_FLAG_PRIO_MASK 0x7fff
  14. #define SWAP_FLAG_PRIO_SHIFT 0
  15. static inline int current_is_kswapd(void)
  16. {
  17. return current->flags & PF_KSWAPD;
  18. }
  19. /*
  20. * MAX_SWAPFILES defines the maximum number of swaptypes: things which can
  21. * be swapped to. The swap type and the offset into that swap type are
  22. * encoded into pte's and into pgoff_t's in the swapcache. Using five bits
  23. * for the type means that the maximum number of swapcache pages is 27 bits
  24. * on 32-bit-pgoff_t architectures. And that assumes that the architecture packs
  25. * the type/offset into the pte as 5/27 as well.
  26. */
  27. #define MAX_SWAPFILES_SHIFT 5
  28. #ifndef CONFIG_MIGRATION
  29. #define MAX_SWAPFILES (1 << MAX_SWAPFILES_SHIFT)
  30. #else
  31. /* Use last two entries for page migration swap entries */
  32. #define MAX_SWAPFILES ((1 << MAX_SWAPFILES_SHIFT)-2)
  33. #define SWP_MIGRATION_READ MAX_SWAPFILES
  34. #define SWP_MIGRATION_WRITE (MAX_SWAPFILES + 1)
  35. #endif
  36. /*
  37. * Magic header for a swap area. The first part of the union is
  38. * what the swap magic looks like for the old (limited to 128MB)
  39. * swap area format, the second part of the union adds - in the
  40. * old reserved area - some extra information. Note that the first
  41. * kilobyte is reserved for boot loader or disk label stuff...
  42. *
  43. * Having the magic at the end of the PAGE_SIZE makes detecting swap
  44. * areas somewhat tricky on machines that support multiple page sizes.
  45. * For 2.5 we'll probably want to move the magic to just beyond the
  46. * bootbits...
  47. */
  48. union swap_header {
  49. struct {
  50. char reserved[PAGE_SIZE - 10];
  51. char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
  52. } magic;
  53. struct {
  54. char bootbits[1024]; /* Space for disklabel etc. */
  55. __u32 version;
  56. __u32 last_page;
  57. __u32 nr_badpages;
  58. unsigned char sws_uuid[16];
  59. unsigned char sws_volume[16];
  60. __u32 padding[117];
  61. __u32 badpages[1];
  62. } info;
  63. };
  64. /* A swap entry has to fit into a "unsigned long", as
  65. * the entry is hidden in the "index" field of the
  66. * swapper address space.
  67. */
  68. typedef struct {
  69. unsigned long val;
  70. } swp_entry_t;
  71. /*
  72. * current->reclaim_state points to one of these when a task is running
  73. * memory reclaim
  74. */
  75. struct reclaim_state {
  76. unsigned long reclaimed_slab;
  77. };
  78. #ifdef __KERNEL__
  79. struct address_space;
  80. struct sysinfo;
  81. struct writeback_control;
  82. struct zone;
  83. /*
  84. * A swap extent maps a range of a swapfile's PAGE_SIZE pages onto a range of
  85. * disk blocks. A list of swap extents maps the entire swapfile. (Where the
  86. * term `swapfile' refers to either a blockdevice or an IS_REG file. Apart
  87. * from setup, they're handled identically.
  88. *
  89. * We always assume that blocks are of size PAGE_SIZE.
  90. */
  91. struct swap_extent {
  92. struct list_head list;
  93. pgoff_t start_page;
  94. pgoff_t nr_pages;
  95. sector_t start_block;
  96. };
  97. /*
  98. * Max bad pages in the new format..
  99. */
  100. #define __swapoffset(x) ((unsigned long)&((union swap_header *)0)->x)
  101. #define MAX_SWAP_BADPAGES \
  102. ((__swapoffset(magic.magic) - __swapoffset(info.badpages)) / sizeof(int))
  103. enum {
  104. SWP_USED = (1 << 0), /* is slot in swap_info[] used? */
  105. SWP_WRITEOK = (1 << 1), /* ok to write to this swap? */
  106. SWP_ACTIVE = (SWP_USED | SWP_WRITEOK),
  107. /* add others here before... */
  108. SWP_SCANNING = (1 << 8), /* refcount in scan_swap_map */
  109. };
  110. #define SWAP_CLUSTER_MAX 32
  111. #define SWAP_MAP_MAX 0x7fff
  112. #define SWAP_MAP_BAD 0x8000
  113. /*
  114. * The in-memory structure used to track swap areas.
  115. */
  116. struct swap_info_struct {
  117. unsigned int flags;
  118. int prio; /* swap priority */
  119. struct file *swap_file;
  120. struct block_device *bdev;
  121. struct list_head extent_list;
  122. struct swap_extent *curr_swap_extent;
  123. unsigned old_block_size;
  124. unsigned short * swap_map;
  125. unsigned int lowest_bit;
  126. unsigned int highest_bit;
  127. unsigned int cluster_next;
  128. unsigned int cluster_nr;
  129. unsigned int pages;
  130. unsigned int max;
  131. unsigned int inuse_pages;
  132. int next; /* next entry on swap list */
  133. };
  134. struct swap_list_t {
  135. int head; /* head of priority-ordered swapfile list */
  136. int next; /* swapfile to be used next */
  137. };
  138. /* Swap 50% full? Release swapcache more aggressively.. */
  139. #define vm_swap_full() (nr_swap_pages*2 < total_swap_pages)
  140. /* linux/mm/oom_kill.c */
  141. extern void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order);
  142. extern int register_oom_notifier(struct notifier_block *nb);
  143. extern int unregister_oom_notifier(struct notifier_block *nb);
  144. /* linux/mm/memory.c */
  145. extern void swapin_readahead(swp_entry_t, unsigned long, struct vm_area_struct *);
  146. /* linux/mm/page_alloc.c */
  147. extern unsigned long totalram_pages;
  148. extern unsigned long totalreserve_pages;
  149. extern long nr_swap_pages;
  150. extern unsigned int nr_free_buffer_pages(void);
  151. extern unsigned int nr_free_pagecache_pages(void);
  152. /* Definition of global_page_state not available yet */
  153. #define nr_free_pages() global_page_state(NR_FREE_PAGES)
  154. /* linux/mm/swap.c */
  155. extern void FASTCALL(lru_cache_add(struct page *));
  156. extern void FASTCALL(lru_cache_add_active(struct page *));
  157. extern void FASTCALL(activate_page(struct page *));
  158. extern void FASTCALL(mark_page_accessed(struct page *));
  159. extern void lru_add_drain(void);
  160. extern int lru_add_drain_all(void);
  161. extern int rotate_reclaimable_page(struct page *page);
  162. extern void swap_setup(void);
  163. /* linux/mm/vmscan.c */
  164. extern unsigned long try_to_free_pages(struct zone **, gfp_t);
  165. extern unsigned long shrink_all_memory(unsigned long nr_pages);
  166. extern int vm_swappiness;
  167. extern int remove_mapping(struct address_space *mapping, struct page *page);
  168. extern long vm_total_pages;
  169. #ifdef CONFIG_NUMA
  170. extern int zone_reclaim_mode;
  171. extern int sysctl_min_unmapped_ratio;
  172. extern int sysctl_min_slab_ratio;
  173. extern int zone_reclaim(struct zone *, gfp_t, unsigned int);
  174. #else
  175. #define zone_reclaim_mode 0
  176. static inline int zone_reclaim(struct zone *z, gfp_t mask, unsigned int order)
  177. {
  178. return 0;
  179. }
  180. #endif
  181. extern int kswapd_run(int nid);
  182. #ifdef CONFIG_MMU
  183. /* linux/mm/shmem.c */
  184. extern int shmem_unuse(swp_entry_t entry, struct page *page);
  185. #endif /* CONFIG_MMU */
  186. extern void swap_unplug_io_fn(struct backing_dev_info *, struct page *);
  187. #ifdef CONFIG_SWAP
  188. /* linux/mm/page_io.c */
  189. extern int swap_readpage(struct file *, struct page *);
  190. extern int swap_writepage(struct page *page, struct writeback_control *wbc);
  191. extern int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err);
  192. /* linux/mm/swap_state.c */
  193. extern struct address_space swapper_space;
  194. #define total_swapcache_pages swapper_space.nrpages
  195. extern void show_swap_cache_info(void);
  196. extern int add_to_swap(struct page *, gfp_t);
  197. extern void __delete_from_swap_cache(struct page *);
  198. extern void delete_from_swap_cache(struct page *);
  199. extern int move_to_swap_cache(struct page *, swp_entry_t);
  200. extern int move_from_swap_cache(struct page *, unsigned long,
  201. struct address_space *);
  202. extern void free_page_and_swap_cache(struct page *);
  203. extern void free_pages_and_swap_cache(struct page **, int);
  204. extern struct page * lookup_swap_cache(swp_entry_t);
  205. extern struct page * read_swap_cache_async(swp_entry_t, struct vm_area_struct *vma,
  206. unsigned long addr);
  207. /* linux/mm/swapfile.c */
  208. extern long total_swap_pages;
  209. extern unsigned int nr_swapfiles;
  210. extern void si_swapinfo(struct sysinfo *);
  211. extern swp_entry_t get_swap_page(void);
  212. extern swp_entry_t get_swap_page_of_type(int);
  213. extern int swap_duplicate(swp_entry_t);
  214. extern int valid_swaphandles(swp_entry_t, unsigned long *);
  215. extern void swap_free(swp_entry_t);
  216. extern void free_swap_and_cache(swp_entry_t);
  217. extern int swap_type_of(dev_t, sector_t, struct block_device **);
  218. extern unsigned int count_swap_pages(int, int);
  219. extern sector_t map_swap_page(struct swap_info_struct *, pgoff_t);
  220. extern sector_t swapdev_block(int, pgoff_t);
  221. extern struct swap_info_struct *get_swap_info_struct(unsigned);
  222. extern int can_share_swap_page(struct page *);
  223. extern int remove_exclusive_swap_page(struct page *);
  224. struct backing_dev_info;
  225. extern spinlock_t swap_lock;
  226. /* linux/mm/thrash.c */
  227. extern struct mm_struct * swap_token_mm;
  228. extern void grab_swap_token(void);
  229. extern void __put_swap_token(struct mm_struct *);
  230. static inline int has_swap_token(struct mm_struct *mm)
  231. {
  232. return (mm == swap_token_mm);
  233. }
  234. static inline void put_swap_token(struct mm_struct *mm)
  235. {
  236. if (has_swap_token(mm))
  237. __put_swap_token(mm);
  238. }
  239. static inline void disable_swap_token(void)
  240. {
  241. put_swap_token(swap_token_mm);
  242. }
  243. #else /* CONFIG_SWAP */
  244. #define total_swap_pages 0
  245. #define total_swapcache_pages 0UL
  246. #define si_swapinfo(val) \
  247. do { (val)->freeswap = (val)->totalswap = 0; } while (0)
  248. /* only sparc can not include linux/pagemap.h in this file
  249. * so leave page_cache_release and release_pages undeclared... */
  250. #define free_page_and_swap_cache(page) \
  251. page_cache_release(page)
  252. #define free_pages_and_swap_cache(pages, nr) \
  253. release_pages((pages), (nr), 0);
  254. static inline void show_swap_cache_info(void)
  255. {
  256. }
  257. static inline void free_swap_and_cache(swp_entry_t swp)
  258. {
  259. }
  260. static inline int swap_duplicate(swp_entry_t swp)
  261. {
  262. return 0;
  263. }
  264. static inline void swap_free(swp_entry_t swp)
  265. {
  266. }
  267. static inline struct page *read_swap_cache_async(swp_entry_t swp,
  268. struct vm_area_struct *vma, unsigned long addr)
  269. {
  270. return NULL;
  271. }
  272. static inline struct page *lookup_swap_cache(swp_entry_t swp)
  273. {
  274. return NULL;
  275. }
  276. static inline int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  277. {
  278. return 0;
  279. }
  280. #define can_share_swap_page(p) (page_mapcount(p) == 1)
  281. static inline int move_to_swap_cache(struct page *page, swp_entry_t entry)
  282. {
  283. return 1;
  284. }
  285. static inline int move_from_swap_cache(struct page *page, unsigned long index,
  286. struct address_space *mapping)
  287. {
  288. return 1;
  289. }
  290. static inline void __delete_from_swap_cache(struct page *page)
  291. {
  292. }
  293. static inline void delete_from_swap_cache(struct page *page)
  294. {
  295. }
  296. #define swap_token_default_timeout 0
  297. static inline int remove_exclusive_swap_page(struct page *p)
  298. {
  299. return 0;
  300. }
  301. static inline swp_entry_t get_swap_page(void)
  302. {
  303. swp_entry_t entry;
  304. entry.val = 0;
  305. return entry;
  306. }
  307. /* linux/mm/thrash.c */
  308. #define put_swap_token(x) do { } while(0)
  309. #define grab_swap_token() do { } while(0)
  310. #define has_swap_token(x) 0
  311. #define disable_swap_token() do { } while(0)
  312. #endif /* CONFIG_SWAP */
  313. #endif /* __KERNEL__*/
  314. #endif /* _LINUX_SWAP_H */