1
2
3
4
5
6
7
8
9
10#include <linux/mm.h>
11#include <linux/kernel_stat.h>
12#include <linux/swap.h>
13#include <linux/swapctl.h>
14#include <linux/init.h>
15#include <linux/pagemap.h>
16#include <linux/smp_lock.h>
17
18#include <asm/pgtable.h>
19
20
21
22
23
24static int swap_writepage(struct page *page)
25{
26 if (remove_exclusive_swap_page(page)) {
27 UnlockPage(page);
28 return 0;
29 }
30 rw_swap_page(WRITE, page);
31 return 0;
32}
33
34static struct address_space_operations swap_aops = {
35 writepage: swap_writepage,
36 sync_page: block_sync_page,
37};
38
39struct address_space swapper_space = {
40 LIST_HEAD_INIT(swapper_space.clean_pages),
41 LIST_HEAD_INIT(swapper_space.dirty_pages),
42 LIST_HEAD_INIT(swapper_space.locked_pages),
43 0,
44 &swap_aops,
45};
46
47#ifdef SWAP_CACHE_INFO
48#define INC_CACHE_INFO(x) (swap_cache_info.x++)
49
50static struct {
51 unsigned long add_total;
52 unsigned long del_total;
53 unsigned long find_success;
54 unsigned long find_total;
55 unsigned long noent_race;
56 unsigned long exist_race;
57} swap_cache_info;
58
59void show_swap_cache_info(void)
60{
61 printk("Swap cache: add %lu, delete %lu, find %lu/%lu, race %lu+%lu\n",
62 swap_cache_info.add_total, swap_cache_info.del_total,
63 swap_cache_info.find_success, swap_cache_info.find_total,
64 swap_cache_info.noent_race, swap_cache_info.exist_race);
65}
66#else
67#define INC_CACHE_INFO(x) do { } while (0)
68#endif
69
70int add_to_swap_cache(struct page *page, swp_entry_t entry)
71{
72 if (page->mapping)
73 BUG();
74 if (!swap_duplicate(entry)) {
75 INC_CACHE_INFO(noent_race);
76 return -ENOENT;
77 }
78 if (add_to_page_cache_unique(page, &swapper_space, entry.val,
79 page_hash(&swapper_space, entry.val)) != 0) {
80 swap_free(entry);
81 INC_CACHE_INFO(exist_race);
82 return -EEXIST;
83 }
84 if (!PageLocked(page))
85 BUG();
86 if (!PageSwapCache(page))
87 BUG();
88 INC_CACHE_INFO(add_total);
89 return 0;
90}
91
92
93
94
95
96void __delete_from_swap_cache(struct page *page)
97{
98 if (!PageLocked(page))
99 BUG();
100 if (!PageSwapCache(page))
101 BUG();
102 ClearPageDirty(page);
103 __remove_inode_page(page);
104 INC_CACHE_INFO(del_total);
105}
106
107
108
109
110
111
112
113void delete_from_swap_cache(struct page *page)
114{
115 swp_entry_t entry;
116
117 if (!PageLocked(page))
118 BUG();
119
120 if (unlikely(!block_flushpage(page, 0)))
121 BUG();
122
123 entry.val = page->index;
124
125 spin_lock(&pagecache_lock);
126 __delete_from_swap_cache(page);
127 spin_unlock(&pagecache_lock);
128
129 swap_free(entry);
130 page_cache_release(page);
131}
132
133
134
135
136
137
138void free_page_and_swap_cache(struct page *page)
139{
140
141
142
143
144
145
146
147
148 if (PageSwapCache(page) && !TryLockPage(page)) {
149 remove_exclusive_swap_page(page);
150 UnlockPage(page);
151 }
152 page_cache_release(page);
153}
154
155
156
157
158
159
160
161struct page * lookup_swap_cache(swp_entry_t entry)
162{
163 struct page *found;
164
165 found = find_get_page(&swapper_space, entry.val);
166
167
168
169
170
171
172 INC_CACHE_INFO(find_total);
173 if (found)
174 INC_CACHE_INFO(find_success);
175 return found;
176}
177
178
179
180
181
182
183
184struct page * read_swap_cache_async(swp_entry_t entry)
185{
186 struct page *found_page, *new_page = NULL;
187 int err;
188
189 do {
190
191
192
193
194
195
196 found_page = find_get_page(&swapper_space, entry.val);
197 if (found_page)
198 break;
199
200
201
202
203 if (!new_page) {
204 new_page = alloc_page(GFP_HIGHUSER);
205 if (!new_page)
206 break;
207 }
208
209
210
211
212
213
214
215
216
217
218 err = add_to_swap_cache(new_page, entry);
219 if (!err) {
220
221
222
223 rw_swap_page(READ, new_page);
224 return new_page;
225 }
226 } while (err != -ENOENT);
227
228 if (new_page)
229 page_cache_release(new_page);
230 return found_page;
231}
232