1
2
3
4
5
6
7
8#include <linux/mm.h>
9#include <linux/sched.h>
10#include <linux/head.h>
11#include <linux/kernel.h>
12#include <linux/kernel_stat.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/stat.h>
16#include <linux/swap.h>
17#include <linux/fs.h>
18#include <linux/swapctl.h>
19#include <linux/init.h>
20
21#include <asm/dma.h>
22#include <asm/system.h>
23#include <asm/uaccess.h>
24#include <asm/bitops.h>
25#include <asm/pgtable.h>
26
27#ifdef SWAP_CACHE_INFO
28unsigned long swap_cache_add_total = 0;
29unsigned long swap_cache_add_success = 0;
30unsigned long swap_cache_del_total = 0;
31unsigned long swap_cache_del_success = 0;
32unsigned long swap_cache_find_total = 0;
33unsigned long swap_cache_find_success = 0;
34
35void show_swap_cache_info(void)
36{
37 printk("Swap cache: add %ld/%ld, delete %ld/%ld, find %ld/%ld\n",
38 swap_cache_add_total, swap_cache_add_success,
39 swap_cache_del_total, swap_cache_del_success,
40 swap_cache_find_total, swap_cache_find_success);
41}
42#endif
43
44int add_to_swap_cache(struct page *page, unsigned long entry)
45{
46 struct swap_info_struct * p = &swap_info[SWP_TYPE(entry)];
47
48#ifdef SWAP_CACHE_INFO
49 swap_cache_add_total++;
50#endif
51 if ((p->flags & SWP_WRITEOK) == SWP_WRITEOK) {
52 page->pg_swap_entry = entry;
53 if (PageTestandSetSwapCache(page))
54 printk("swap_cache: replacing non-empty entry\n");
55#ifdef SWAP_CACHE_INFO
56 swap_cache_add_success++;
57#endif
58 return 1;
59 }
60 return 0;
61}
62
63void swap_duplicate(unsigned long entry)
64{
65 struct swap_info_struct * p;
66 unsigned long offset, type;
67
68 if (!entry)
69 return;
70 offset = SWP_OFFSET(entry);
71 type = SWP_TYPE(entry);
72 if (type & SHM_SWP_TYPE)
73 return;
74 if (type >= nr_swapfiles) {
75 printk("Trying to duplicate nonexistent swap-page\n");
76 return;
77 }
78 p = type + swap_info;
79 if (offset >= p->max) {
80 printk("swap_duplicate: weirdness\n");
81 return;
82 }
83 if (!p->swap_map[offset]) {
84 printk("swap_duplicate: trying to duplicate unused page\n");
85 return;
86 }
87 p->swap_map[offset]++;
88 return;
89}
90
91