1
2
3
4
5
6
7
8
9
10
11
12#define FSCACHE_DEBUG_LEVEL CACHE
13#include <linux/module.h>
14#include <linux/slab.h>
15#include "internal.h"
16
17LIST_HEAD(fscache_cache_list);
18DECLARE_RWSEM(fscache_addremove_sem);
19DECLARE_WAIT_QUEUE_HEAD(fscache_cache_cleared_wq);
20EXPORT_SYMBOL(fscache_cache_cleared_wq);
21
22static LIST_HEAD(fscache_cache_tag_list);
23
24
25
26
27struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *name)
28{
29 struct fscache_cache_tag *tag, *xtag;
30
31
32 down_read(&fscache_addremove_sem);
33
34 list_for_each_entry(tag, &fscache_cache_tag_list, link) {
35 if (strcmp(tag->name, name) == 0) {
36 atomic_inc(&tag->usage);
37 up_read(&fscache_addremove_sem);
38 return tag;
39 }
40 }
41
42 up_read(&fscache_addremove_sem);
43
44
45 xtag = kzalloc(sizeof(*xtag) + strlen(name) + 1, GFP_KERNEL);
46 if (!xtag)
47
48 return ERR_PTR(-ENOMEM);
49
50 atomic_set(&xtag->usage, 1);
51 strcpy(xtag->name, name);
52
53
54 down_write(&fscache_addremove_sem);
55
56 list_for_each_entry(tag, &fscache_cache_tag_list, link) {
57 if (strcmp(tag->name, name) == 0) {
58 atomic_inc(&tag->usage);
59 up_write(&fscache_addremove_sem);
60 kfree(xtag);
61 return tag;
62 }
63 }
64
65 list_add_tail(&xtag->link, &fscache_cache_tag_list);
66 up_write(&fscache_addremove_sem);
67 return xtag;
68}
69
70
71
72
73void __fscache_release_cache_tag(struct fscache_cache_tag *tag)
74{
75 if (tag != ERR_PTR(-ENOMEM)) {
76 down_write(&fscache_addremove_sem);
77
78 if (atomic_dec_and_test(&tag->usage))
79 list_del_init(&tag->link);
80 else
81 tag = NULL;
82
83 up_write(&fscache_addremove_sem);
84
85 kfree(tag);
86 }
87}
88
89
90
91
92
93
94struct fscache_cache *fscache_select_cache_for_object(
95 struct fscache_cookie *cookie)
96{
97 struct fscache_cache_tag *tag;
98 struct fscache_object *object;
99 struct fscache_cache *cache;
100
101 _enter("");
102
103 if (list_empty(&fscache_cache_list)) {
104 _leave(" = NULL [no cache]");
105 return NULL;
106 }
107
108
109 spin_lock(&cookie->lock);
110
111
112
113 if (!hlist_empty(&cookie->backing_objects)) {
114 object = hlist_entry(cookie->backing_objects.first,
115 struct fscache_object, cookie_link);
116
117 cache = object->cache;
118 if (fscache_object_is_dying(object) ||
119 test_bit(FSCACHE_IOERROR, &cache->flags))
120 cache = NULL;
121
122 spin_unlock(&cookie->lock);
123 _leave(" = %p [parent]", cache);
124 return cache;
125 }
126
127
128 if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
129
130 spin_unlock(&cookie->lock);
131 _leave(" = NULL [cookie ub,ni]");
132 return NULL;
133 }
134
135 spin_unlock(&cookie->lock);
136
137 if (!cookie->def->select_cache)
138 goto no_preference;
139
140
141 tag = cookie->def->select_cache(cookie->parent->netfs_data,
142 cookie->netfs_data);
143 if (!tag)
144 goto no_preference;
145
146 if (tag == ERR_PTR(-ENOMEM)) {
147 _leave(" = NULL [nomem tag]");
148 return NULL;
149 }
150
151 if (!tag->cache) {
152 _leave(" = NULL [unbacked tag]");
153 return NULL;
154 }
155
156 if (test_bit(FSCACHE_IOERROR, &tag->cache->flags))
157 return NULL;
158
159 _leave(" = %p [specific]", tag->cache);
160 return tag->cache;
161
162no_preference:
163
164 cache = list_entry(fscache_cache_list.next,
165 struct fscache_cache, link);
166 _leave(" = %p [first]", cache);
167 return cache;
168}
169
170
171
172
173
174
175
176
177
178
179
180
181
182void fscache_init_cache(struct fscache_cache *cache,
183 const struct fscache_cache_ops *ops,
184 const char *idfmt,
185 ...)
186{
187 va_list va;
188
189 memset(cache, 0, sizeof(*cache));
190
191 cache->ops = ops;
192
193 va_start(va, idfmt);
194 vsnprintf(cache->identifier, sizeof(cache->identifier), idfmt, va);
195 va_end(va);
196
197 INIT_WORK(&cache->op_gc, fscache_operation_gc);
198 INIT_LIST_HEAD(&cache->link);
199 INIT_LIST_HEAD(&cache->object_list);
200 INIT_LIST_HEAD(&cache->op_gc_list);
201 spin_lock_init(&cache->object_list_lock);
202 spin_lock_init(&cache->op_gc_list_lock);
203}
204EXPORT_SYMBOL(fscache_init_cache);
205
206
207
208
209
210
211
212
213
214
215
216
217int fscache_add_cache(struct fscache_cache *cache,
218 struct fscache_object *ifsdef,
219 const char *tagname)
220{
221 struct fscache_cache_tag *tag;
222
223 BUG_ON(!cache->ops);
224 BUG_ON(!ifsdef);
225
226 cache->flags = 0;
227 ifsdef->event_mask =
228 ((1 << NR_FSCACHE_OBJECT_EVENTS) - 1) &
229 ~(1 << FSCACHE_OBJECT_EV_CLEARED);
230 __set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &ifsdef->flags);
231
232 if (!tagname)
233 tagname = cache->identifier;
234
235 BUG_ON(!tagname[0]);
236
237 _enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname);
238
239
240 tag = __fscache_lookup_cache_tag(tagname);
241 if (IS_ERR(tag))
242 goto nomem;
243
244 if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags))
245 goto tag_in_use;
246
247 cache->kobj = kobject_create_and_add(tagname, fscache_root);
248 if (!cache->kobj)
249 goto error;
250
251 ifsdef->cookie = &fscache_fsdef_index;
252 ifsdef->cache = cache;
253 cache->fsdef = ifsdef;
254
255 down_write(&fscache_addremove_sem);
256
257 tag->cache = cache;
258 cache->tag = tag;
259
260
261 list_add(&cache->link, &fscache_cache_list);
262
263
264
265 spin_lock(&cache->object_list_lock);
266 list_add_tail(&ifsdef->cache_link, &cache->object_list);
267 spin_unlock(&cache->object_list_lock);
268 fscache_objlist_add(ifsdef);
269
270
271
272 spin_lock(&fscache_fsdef_index.lock);
273
274 hlist_add_head(&ifsdef->cookie_link,
275 &fscache_fsdef_index.backing_objects);
276
277 atomic_inc(&fscache_fsdef_index.usage);
278
279
280 spin_unlock(&fscache_fsdef_index.lock);
281 up_write(&fscache_addremove_sem);
282
283 pr_notice("Cache \"%s\" added (type %s)\n",
284 cache->tag->name, cache->ops->name);
285 kobject_uevent(cache->kobj, KOBJ_ADD);
286
287 _leave(" = 0 [%s]", cache->identifier);
288 return 0;
289
290tag_in_use:
291 pr_err("Cache tag '%s' already in use\n", tagname);
292 __fscache_release_cache_tag(tag);
293 _leave(" = -EXIST");
294 return -EEXIST;
295
296error:
297 __fscache_release_cache_tag(tag);
298 _leave(" = -EINVAL");
299 return -EINVAL;
300
301nomem:
302 _leave(" = -ENOMEM");
303 return -ENOMEM;
304}
305EXPORT_SYMBOL(fscache_add_cache);
306
307
308
309
310
311
312
313
314
315
316
317void fscache_io_error(struct fscache_cache *cache)
318{
319 if (!test_and_set_bit(FSCACHE_IOERROR, &cache->flags))
320 pr_err("Cache '%s' stopped due to I/O error\n",
321 cache->ops->name);
322}
323EXPORT_SYMBOL(fscache_io_error);
324
325
326
327
328
329static void fscache_withdraw_all_objects(struct fscache_cache *cache,
330 struct list_head *dying_objects)
331{
332 struct fscache_object *object;
333
334 while (!list_empty(&cache->object_list)) {
335 spin_lock(&cache->object_list_lock);
336
337 if (!list_empty(&cache->object_list)) {
338 object = list_entry(cache->object_list.next,
339 struct fscache_object, cache_link);
340 list_move_tail(&object->cache_link, dying_objects);
341
342 _debug("withdraw %p", object->cookie);
343
344
345
346
347 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
348 }
349
350 spin_unlock(&cache->object_list_lock);
351 cond_resched();
352 }
353}
354
355
356
357
358
359
360
361
362
363
364
365void fscache_withdraw_cache(struct fscache_cache *cache)
366{
367 LIST_HEAD(dying_objects);
368
369 _enter("");
370
371 pr_notice("Withdrawing cache \"%s\"\n",
372 cache->tag->name);
373
374
375 if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags))
376 BUG();
377
378 down_write(&fscache_addremove_sem);
379 list_del_init(&cache->link);
380 cache->tag->cache = NULL;
381 up_write(&fscache_addremove_sem);
382
383
384
385 fscache_stat(&fscache_n_cop_sync_cache);
386 cache->ops->sync_cache(cache);
387 fscache_stat_d(&fscache_n_cop_sync_cache);
388
389
390
391 fscache_stat(&fscache_n_cop_dissociate_pages);
392 cache->ops->dissociate_pages(cache);
393 fscache_stat_d(&fscache_n_cop_dissociate_pages);
394
395
396
397
398 _debug("destroy");
399
400 fscache_withdraw_all_objects(cache, &dying_objects);
401
402
403
404 _debug("wait for finish");
405 wait_event(fscache_cache_cleared_wq,
406 atomic_read(&cache->object_count) == 0);
407 _debug("wait for clearance");
408 wait_event(fscache_cache_cleared_wq,
409 list_empty(&cache->object_list));
410 _debug("cleared");
411 ASSERT(list_empty(&dying_objects));
412
413 kobject_put(cache->kobj);
414
415 clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
416 fscache_release_cache_tag(cache->tag);
417 cache->tag = NULL;
418
419 _leave("");
420}
421EXPORT_SYMBOL(fscache_withdraw_cache);
422