-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.c
231 lines (182 loc) · 5.02 KB
/
cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* cache.c
*
* Copyright (C) 2013
* Maxime Lorrillere <[email protected]>
* LIP6 - Laboratoire d'Informatique de Paris 6
*/
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/mempool.h>
#include <linux/slab.h>
#include <linux/bug.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/lockdep.h>
#include "cache.h"
static struct kmem_cache *remotecache_inode_cachep = NULL;
static void remotecache_inode_init(struct remotecache_inode *inode)
{
kref_init(&inode->kref);
INIT_HLIST_NODE(&inode->hash);
INIT_LIST_HEAD(&inode->list);
INIT_RADIX_TREE(&inode->pages_tree, GFP_ATOMIC);
spin_lock_init(&inode->lock);
inode->ino = 0;
}
struct remotecache_inode *__remotecache_lookup_inode(struct remotecache *cache,
ino_t ino)
{
struct remotecache_inode *i;
if (!rcu_read_lock_held())
lockdep_assert_held(&cache->lock);
rc_debug("%s: cache %d inode %lu\n", __func__, cache->pool_id, ino);
hash_for_each_possible_rcu(cache->inodes_hash, i, hash, ino)
if (ino == i->ino) {
rc_debug("%s: found inode %p ino %lu == %lu\n",
__func__, i, i->ino, ino);
remotecache_inode_get(i);
return i;
}
return NULL;
}
void __remotecache_insert_inode(struct remotecache *cache,
struct remotecache_inode *i)
{
lockdep_assert_held(&cache->lock);
rc_debug("%s: cache %d inode %p %lu\n", __func__, cache->pool_id,
i, i->ino);
remotecache_inode_get(i);
hash_add_rcu(cache->inodes_hash, &i->hash, i->ino);
i->cache = cache;
}
void __remotecache_remove_inode(struct remotecache_inode *i)
{
lockdep_assert_held(&i->cache->lock);
rc_debug("%s: cache %d inode %p %lu\n", __func__, i->cache->pool_id,
i, i->ino);
hash_del_rcu(&i->hash);
remotecache_inode_put(i);
}
void remotecache_inode_release(struct kref *ref)
{
struct remotecache_inode *inode =
container_of(ref, struct remotecache_inode, kref);
unsigned long pos = 0, flags;
struct page *pages[16];
int n;
rc_debug("%s %p %lu\n", __func__, inode, inode->ino);
do {
int i;
spin_lock_irqsave(&inode->lock, flags);
n = radix_tree_gang_lookup(&inode->pages_tree, (void **)pages,
pos, ARRAY_SIZE(pages));
for (i = 0; i < n; ++i) {
struct page *p = pages[i];
void *ret;
if (radix_tree_exceptional_entry(p))
continue;
BUG_ON(!get_page_unless_zero(p));
pos = p->index;
ret = radix_tree_delete(&inode->pages_tree, p->index);
BUG_ON(!ret || ret != p);
}
pos++;
spin_unlock_irqrestore(&inode->lock, flags);
/*
* There is no need to synchronize_rcu as __handle_get checks
* if the slot has changed after increasing the ref count on
* the page
*/
for (i = 0; i < n; ++i) {
struct page *p = pages[i];
if (radix_tree_exceptional_entry(p))
continue;
if (TestClearPageRemote(p)) {
ClearPagePrivate(p);
set_page_private(p, 0);
__dec_zone_page_state(p, NR_REMOTE);
__dec_zone_page_state(p, NR_FILE_PAGES);
/*
* Release page cache ref
*/
page_cache_release(p);
}
/*
* Drop last ref
*/
put_page(p);
}
atomic_sub(n, &inode->cache->size);
cond_resched();
} while (n > 0);
kmem_cache_free(remotecache_inode_cachep, inode);
}
/*
* Remove a remote cache page from system LRU.
* Caller should have a ref on p.
*/
void remotecache_page_release(struct page *p) {
struct remotecache_inode *inode = (void*)p->private;
struct remotecache *cache = inode->cache;
if (TestClearPageRemote(p)) {
ClearPagePrivate(p);
set_page_private(p, 0);
__dec_zone_page_state(p, NR_REMOTE);
__dec_zone_page_state(p, NR_FILE_PAGES);
/*
* Release page cache ref
*/
page_cache_release(p);
atomic_dec(&cache->size);
}
}
void remotecache_clear(struct remotecache *cache)
{
int bucket;
unsigned long flags;
struct remotecache_inode *ino, *next;
struct hlist_node *tmp;
LIST_HEAD(inodes_list);
spin_lock_irqsave(&cache->lock, flags);
hash_for_each_safe(cache->inodes_hash, bucket, tmp, ino, hash) {
hash_del_rcu(&ino->hash);
list_add(&ino->list, &inodes_list);
}
spin_unlock_irqrestore(&cache->lock, flags);
synchronize_rcu();
list_for_each_entry_safe(ino, next, &inodes_list, list) {
remotecache_inode_put(ino);
}
}
void remotecache_release(struct kref *ref)
{
struct remotecache *cache = container_of(ref, struct remotecache, kref);
rc_debug("%s: remotecache release %p", __func__, cache);
remotecache_clear(cache);
}
struct remotecache_inode *remotecache_inode_alloc(gfp_t gfp_mask)
{
struct remotecache_inode *i =
kmem_cache_zalloc(remotecache_inode_cachep, gfp_mask);
if (i)
remotecache_inode_init(i);
return i;
}
void remotecache_init(struct remotecache *cache)
{
/* TODO: destroy kmem_cache on module unload */
if (!remotecache_inode_cachep) {
remotecache_inode_cachep = kmem_cache_create("remotecache_inode",
sizeof(struct remotecache_inode), 0,
SLAB_MEM_SPREAD|SLAB_PANIC, NULL);
}
kref_init(&cache->kref);
INIT_LIST_HEAD(&cache->list);
cache->session = NULL;
memset(&cache->uuid, 0, sizeof(cache->uuid));
cache->pool_id = 0;
hash_init(cache->inodes_hash);
spin_lock_init(&cache->lock);
atomic_set(&cache->size, 0);
}