1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/fs.h>
16#include <linux/exportfs.h>
17#include <linux/mm.h>
18#include <linux/debugfs.h>
19#include <linux/cleancache.h>
20
21
22
23
24
25static struct cleancache_ops *cleancache_ops __read_mostly;
26
27
28
29
30
31
32static u64 cleancache_succ_gets;
33static u64 cleancache_failed_gets;
34static u64 cleancache_puts;
35static u64 cleancache_invalidates;
36
37
38
39
40
41
42
43
44
45
46
47#define MAX_INITIALIZABLE_FS 32
48#define FAKE_FS_POOLID_OFFSET 1000
49#define FAKE_SHARED_FS_POOLID_OFFSET 2000
50
51#define FS_NO_BACKEND (-1)
52#define FS_UNKNOWN (-2)
53static int fs_poolid_map[MAX_INITIALIZABLE_FS];
54static int shared_fs_poolid_map[MAX_INITIALIZABLE_FS];
55static char *uuids[MAX_INITIALIZABLE_FS];
56
57
58
59
60
61static DEFINE_MUTEX(poolid_mutex);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112struct cleancache_ops *cleancache_register_ops(struct cleancache_ops *ops)
113{
114 struct cleancache_ops *old = cleancache_ops;
115 int i;
116
117 mutex_lock(&poolid_mutex);
118 for (i = 0; i < MAX_INITIALIZABLE_FS; i++) {
119 if (fs_poolid_map[i] == FS_NO_BACKEND)
120 fs_poolid_map[i] = ops->init_fs(PAGE_SIZE);
121 if (shared_fs_poolid_map[i] == FS_NO_BACKEND)
122 shared_fs_poolid_map[i] = ops->init_shared_fs
123 (uuids[i], PAGE_SIZE);
124 }
125
126
127
128
129
130 barrier();
131 cleancache_ops = ops;
132 mutex_unlock(&poolid_mutex);
133 return old;
134}
135EXPORT_SYMBOL(cleancache_register_ops);
136
137
138void __cleancache_init_fs(struct super_block *sb)
139{
140 int i;
141
142 mutex_lock(&poolid_mutex);
143 for (i = 0; i < MAX_INITIALIZABLE_FS; i++) {
144 if (fs_poolid_map[i] == FS_UNKNOWN) {
145 sb->cleancache_poolid = i + FAKE_FS_POOLID_OFFSET;
146 if (cleancache_ops)
147 fs_poolid_map[i] = cleancache_ops->init_fs(PAGE_SIZE);
148 else
149 fs_poolid_map[i] = FS_NO_BACKEND;
150 break;
151 }
152 }
153 mutex_unlock(&poolid_mutex);
154}
155EXPORT_SYMBOL(__cleancache_init_fs);
156
157
158void __cleancache_init_shared_fs(char *uuid, struct super_block *sb)
159{
160 int i;
161
162 mutex_lock(&poolid_mutex);
163 for (i = 0; i < MAX_INITIALIZABLE_FS; i++) {
164 if (shared_fs_poolid_map[i] == FS_UNKNOWN) {
165 sb->cleancache_poolid = i + FAKE_SHARED_FS_POOLID_OFFSET;
166 uuids[i] = uuid;
167 if (cleancache_ops)
168 shared_fs_poolid_map[i] = cleancache_ops->init_shared_fs
169 (uuid, PAGE_SIZE);
170 else
171 shared_fs_poolid_map[i] = FS_NO_BACKEND;
172 break;
173 }
174 }
175 mutex_unlock(&poolid_mutex);
176}
177EXPORT_SYMBOL(__cleancache_init_shared_fs);
178
179
180
181
182
183static int cleancache_get_key(struct inode *inode,
184 struct cleancache_filekey *key)
185{
186 int (*fhfn)(struct inode *, __u32 *fh, int *, struct inode *);
187 int len = 0, maxlen = CLEANCACHE_KEY_MAX;
188 struct super_block *sb = inode->i_sb;
189
190 key->u.ino = inode->i_ino;
191 if (sb->s_export_op != NULL) {
192 fhfn = sb->s_export_op->encode_fh;
193 if (fhfn) {
194 len = (*fhfn)(inode, &key->u.fh[0], &maxlen, NULL);
195 if (len <= FILEID_ROOT || len == FILEID_INVALID)
196 return -1;
197 if (maxlen > CLEANCACHE_KEY_MAX)
198 return -1;
199 }
200 }
201 return 0;
202}
203
204
205
206
207static int get_poolid_from_fake(int fake_pool_id)
208{
209 if (fake_pool_id >= FAKE_SHARED_FS_POOLID_OFFSET)
210 return shared_fs_poolid_map[fake_pool_id -
211 FAKE_SHARED_FS_POOLID_OFFSET];
212 else if (fake_pool_id >= FAKE_FS_POOLID_OFFSET)
213 return fs_poolid_map[fake_pool_id - FAKE_FS_POOLID_OFFSET];
214 return FS_NO_BACKEND;
215}
216
217
218
219
220
221
222
223
224
225
226
227
228int __cleancache_get_page(struct page *page)
229{
230 int ret = -1;
231 int pool_id;
232 int fake_pool_id;
233 struct cleancache_filekey key = { .u.key = { 0 } };
234
235 if (!cleancache_ops) {
236 cleancache_failed_gets++;
237 goto out;
238 }
239
240 VM_BUG_ON(!PageLocked(page));
241 fake_pool_id = page->mapping->host->i_sb->cleancache_poolid;
242 if (fake_pool_id < 0)
243 goto out;
244 pool_id = get_poolid_from_fake(fake_pool_id);
245
246 if (cleancache_get_key(page->mapping->host, &key) < 0)
247 goto out;
248
249 if (pool_id >= 0)
250 ret = cleancache_ops->get_page(pool_id,
251 key, page->index, page);
252 if (ret == 0)
253 cleancache_succ_gets++;
254 else
255 cleancache_failed_gets++;
256out:
257 return ret;
258}
259EXPORT_SYMBOL(__cleancache_get_page);
260
261
262
263
264
265
266
267
268
269
270
271void __cleancache_put_page(struct page *page)
272{
273 int pool_id;
274 int fake_pool_id;
275 struct cleancache_filekey key = { .u.key = { 0 } };
276
277 if (!cleancache_ops) {
278 cleancache_puts++;
279 return;
280 }
281
282 VM_BUG_ON(!PageLocked(page));
283 fake_pool_id = page->mapping->host->i_sb->cleancache_poolid;
284 if (fake_pool_id < 0)
285 return;
286
287 pool_id = get_poolid_from_fake(fake_pool_id);
288
289 if (pool_id >= 0 &&
290 cleancache_get_key(page->mapping->host, &key) >= 0) {
291 cleancache_ops->put_page(pool_id, key, page->index, page);
292 cleancache_puts++;
293 }
294}
295EXPORT_SYMBOL(__cleancache_put_page);
296
297
298
299
300
301
302
303
304
305void __cleancache_invalidate_page(struct address_space *mapping,
306 struct page *page)
307{
308
309 int pool_id;
310 int fake_pool_id = mapping->host->i_sb->cleancache_poolid;
311 struct cleancache_filekey key = { .u.key = { 0 } };
312
313 if (!cleancache_ops)
314 return;
315
316 if (fake_pool_id >= 0) {
317 pool_id = get_poolid_from_fake(fake_pool_id);
318 if (pool_id < 0)
319 return;
320
321 VM_BUG_ON(!PageLocked(page));
322 if (cleancache_get_key(mapping->host, &key) >= 0) {
323 cleancache_ops->invalidate_page(pool_id,
324 key, page->index);
325 cleancache_invalidates++;
326 }
327 }
328}
329EXPORT_SYMBOL(__cleancache_invalidate_page);
330
331
332
333
334
335
336
337
338
339
340void __cleancache_invalidate_inode(struct address_space *mapping)
341{
342 int pool_id;
343 int fake_pool_id = mapping->host->i_sb->cleancache_poolid;
344 struct cleancache_filekey key = { .u.key = { 0 } };
345
346 if (!cleancache_ops)
347 return;
348
349 if (fake_pool_id < 0)
350 return;
351
352 pool_id = get_poolid_from_fake(fake_pool_id);
353
354 if (pool_id >= 0 && cleancache_get_key(mapping->host, &key) >= 0)
355 cleancache_ops->invalidate_inode(pool_id, key);
356}
357EXPORT_SYMBOL(__cleancache_invalidate_inode);
358
359
360
361
362
363
364void __cleancache_invalidate_fs(struct super_block *sb)
365{
366 int index;
367 int fake_pool_id = sb->cleancache_poolid;
368 int old_poolid = fake_pool_id;
369
370 mutex_lock(&poolid_mutex);
371 if (fake_pool_id >= FAKE_SHARED_FS_POOLID_OFFSET) {
372 index = fake_pool_id - FAKE_SHARED_FS_POOLID_OFFSET;
373 old_poolid = shared_fs_poolid_map[index];
374 shared_fs_poolid_map[index] = FS_UNKNOWN;
375 uuids[index] = NULL;
376 } else if (fake_pool_id >= FAKE_FS_POOLID_OFFSET) {
377 index = fake_pool_id - FAKE_FS_POOLID_OFFSET;
378 old_poolid = fs_poolid_map[index];
379 fs_poolid_map[index] = FS_UNKNOWN;
380 }
381 sb->cleancache_poolid = -1;
382 if (cleancache_ops)
383 cleancache_ops->invalidate_fs(old_poolid);
384 mutex_unlock(&poolid_mutex);
385}
386EXPORT_SYMBOL(__cleancache_invalidate_fs);
387
388static int __init init_cleancache(void)
389{
390 int i;
391
392#ifdef CONFIG_DEBUG_FS
393 struct dentry *root = debugfs_create_dir("cleancache", NULL);
394 if (root == NULL)
395 return -ENXIO;
396 debugfs_create_u64("succ_gets", S_IRUGO, root, &cleancache_succ_gets);
397 debugfs_create_u64("failed_gets", S_IRUGO,
398 root, &cleancache_failed_gets);
399 debugfs_create_u64("puts", S_IRUGO, root, &cleancache_puts);
400 debugfs_create_u64("invalidates", S_IRUGO,
401 root, &cleancache_invalidates);
402#endif
403 for (i = 0; i < MAX_INITIALIZABLE_FS; i++) {
404 fs_poolid_map[i] = FS_UNKNOWN;
405 shared_fs_poolid_map[i] = FS_UNKNOWN;
406 }
407 return 0;
408}
409module_init(init_cleancache)
410