Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / fs / fscache / cache.c
blobf7cff367db7f2df8c358b5a723889b4bd8790f54
1 /* FS-Cache cache handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define FSCACHE_DEBUG_LEVEL CACHE
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include "internal.h"
17 LIST_HEAD(fscache_cache_list);
18 DECLARE_RWSEM(fscache_addremove_sem);
19 DECLARE_WAIT_QUEUE_HEAD(fscache_cache_cleared_wq);
20 EXPORT_SYMBOL(fscache_cache_cleared_wq);
22 static LIST_HEAD(fscache_cache_tag_list);
25 * look up a cache tag
27 struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *name)
29 struct fscache_cache_tag *tag, *xtag;
31 /* firstly check for the existence of the tag under read lock */
32 down_read(&fscache_addremove_sem);
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;
42 up_read(&fscache_addremove_sem);
44 /* the tag does not exist - create a candidate */
45 xtag = kzalloc(sizeof(*xtag) + strlen(name) + 1, GFP_KERNEL);
46 if (!xtag)
47 /* return a dummy tag if out of memory */
48 return ERR_PTR(-ENOMEM);
50 atomic_set(&xtag->usage, 1);
51 strcpy(xtag->name, name);
53 /* write lock, search again and add if still not present */
54 down_write(&fscache_addremove_sem);
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;
65 list_add_tail(&xtag->link, &fscache_cache_tag_list);
66 up_write(&fscache_addremove_sem);
67 return xtag;
71 * release a reference to a cache tag
73 void __fscache_release_cache_tag(struct fscache_cache_tag *tag)
75 if (tag != ERR_PTR(-ENOMEM)) {
76 down_write(&fscache_addremove_sem);
78 if (atomic_dec_and_test(&tag->usage))
79 list_del_init(&tag->link);
80 else
81 tag = NULL;
83 up_write(&fscache_addremove_sem);
85 kfree(tag);
90 * select a cache in which to store an object
91 * - the cache addremove semaphore must be at least read-locked by the caller
92 * - the object will never be an index
94 struct fscache_cache *fscache_select_cache_for_object(
95 struct fscache_cookie *cookie)
97 struct fscache_cache_tag *tag;
98 struct fscache_object *object;
99 struct fscache_cache *cache;
101 _enter("");
103 if (list_empty(&fscache_cache_list)) {
104 _leave(" = NULL [no cache]");
105 return NULL;
108 /* we check the parent to determine the cache to use */
109 spin_lock(&cookie->lock);
111 /* the first in the parent's backing list should be the preferred
112 * cache */
113 if (!hlist_empty(&cookie->backing_objects)) {
114 object = hlist_entry(cookie->backing_objects.first,
115 struct fscache_object, cookie_link);
117 cache = object->cache;
118 if (fscache_object_is_dying(object) ||
119 test_bit(FSCACHE_IOERROR, &cache->flags))
120 cache = NULL;
122 spin_unlock(&cookie->lock);
123 _leave(" = %p [parent]", cache);
124 return cache;
127 /* the parent is unbacked */
128 if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
129 /* cookie not an index and is unbacked */
130 spin_unlock(&cookie->lock);
131 _leave(" = NULL [cookie ub,ni]");
132 return NULL;
135 spin_unlock(&cookie->lock);
137 if (!cookie->def->select_cache)
138 goto no_preference;
140 /* ask the netfs for its preference */
141 tag = cookie->def->select_cache(cookie->parent->netfs_data,
142 cookie->netfs_data);
143 if (!tag)
144 goto no_preference;
146 if (tag == ERR_PTR(-ENOMEM)) {
147 _leave(" = NULL [nomem tag]");
148 return NULL;
151 if (!tag->cache) {
152 _leave(" = NULL [unbacked tag]");
153 return NULL;
156 if (test_bit(FSCACHE_IOERROR, &tag->cache->flags))
157 return NULL;
159 _leave(" = %p [specific]", tag->cache);
160 return tag->cache;
162 no_preference:
163 /* netfs has no preference - just select first cache */
164 cache = list_entry(fscache_cache_list.next,
165 struct fscache_cache, link);
166 _leave(" = %p [first]", cache);
167 return cache;
171 * fscache_init_cache - Initialise a cache record
172 * @cache: The cache record to be initialised
173 * @ops: The cache operations to be installed in that record
174 * @idfmt: Format string to define identifier
175 * @...: sprintf-style arguments
177 * Initialise a record of a cache and fill in the name.
179 * See Documentation/filesystems/caching/backend-api.txt for a complete
180 * description.
182 void fscache_init_cache(struct fscache_cache *cache,
183 const struct fscache_cache_ops *ops,
184 const char *idfmt,
185 ...)
187 va_list va;
189 memset(cache, 0, sizeof(*cache));
191 cache->ops = ops;
193 va_start(va, idfmt);
194 vsnprintf(cache->identifier, sizeof(cache->identifier), idfmt, va);
195 va_end(va);
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);
204 EXPORT_SYMBOL(fscache_init_cache);
207 * fscache_add_cache - Declare a cache as being open for business
208 * @cache: The record describing the cache
209 * @ifsdef: The record of the cache object describing the top-level index
210 * @tagname: The tag describing this cache
212 * Add a cache to the system, making it available for netfs's to use.
214 * See Documentation/filesystems/caching/backend-api.txt for a complete
215 * description.
217 int fscache_add_cache(struct fscache_cache *cache,
218 struct fscache_object *ifsdef,
219 const char *tagname)
221 struct fscache_cache_tag *tag;
223 BUG_ON(!cache->ops);
224 BUG_ON(!ifsdef);
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);
232 if (!tagname)
233 tagname = cache->identifier;
235 BUG_ON(!tagname[0]);
237 _enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname);
239 /* we use the cache tag to uniquely identify caches */
240 tag = __fscache_lookup_cache_tag(tagname);
241 if (IS_ERR(tag))
242 goto nomem;
244 if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags))
245 goto tag_in_use;
247 cache->kobj = kobject_create_and_add(tagname, fscache_root);
248 if (!cache->kobj)
249 goto error;
251 ifsdef->cookie = &fscache_fsdef_index;
252 ifsdef->cache = cache;
253 cache->fsdef = ifsdef;
255 down_write(&fscache_addremove_sem);
257 tag->cache = cache;
258 cache->tag = tag;
260 /* add the cache to the list */
261 list_add(&cache->link, &fscache_cache_list);
263 /* add the cache's netfs definition index object to the cache's
264 * list */
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);
270 /* add the cache's netfs definition index object to the top level index
271 * cookie as a known backing object */
272 spin_lock(&fscache_fsdef_index.lock);
274 hlist_add_head(&ifsdef->cookie_link,
275 &fscache_fsdef_index.backing_objects);
277 atomic_inc(&fscache_fsdef_index.usage);
279 /* done */
280 spin_unlock(&fscache_fsdef_index.lock);
281 up_write(&fscache_addremove_sem);
283 printk(KERN_NOTICE "FS-Cache: Cache \"%s\" added (type %s)\n",
284 cache->tag->name, cache->ops->name);
285 kobject_uevent(cache->kobj, KOBJ_ADD);
287 _leave(" = 0 [%s]", cache->identifier);
288 return 0;
290 tag_in_use:
291 printk(KERN_ERR "FS-Cache: Cache tag '%s' already in use\n", tagname);
292 __fscache_release_cache_tag(tag);
293 _leave(" = -EXIST");
294 return -EEXIST;
296 error:
297 __fscache_release_cache_tag(tag);
298 _leave(" = -EINVAL");
299 return -EINVAL;
301 nomem:
302 _leave(" = -ENOMEM");
303 return -ENOMEM;
305 EXPORT_SYMBOL(fscache_add_cache);
308 * fscache_io_error - Note a cache I/O error
309 * @cache: The record describing the cache
311 * Note that an I/O error occurred in a cache and that it should no longer be
312 * used for anything. This also reports the error into the kernel log.
314 * See Documentation/filesystems/caching/backend-api.txt for a complete
315 * description.
317 void fscache_io_error(struct fscache_cache *cache)
319 if (!test_and_set_bit(FSCACHE_IOERROR, &cache->flags))
320 printk(KERN_ERR "FS-Cache:"
321 " Cache '%s' stopped due to I/O error\n",
322 cache->ops->name);
324 EXPORT_SYMBOL(fscache_io_error);
327 * request withdrawal of all the objects in a cache
328 * - all the objects being withdrawn are moved onto the supplied list
330 static void fscache_withdraw_all_objects(struct fscache_cache *cache,
331 struct list_head *dying_objects)
333 struct fscache_object *object;
335 while (!list_empty(&cache->object_list)) {
336 spin_lock(&cache->object_list_lock);
338 if (!list_empty(&cache->object_list)) {
339 object = list_entry(cache->object_list.next,
340 struct fscache_object, cache_link);
341 list_move_tail(&object->cache_link, dying_objects);
343 _debug("withdraw %p", object->cookie);
345 /* This must be done under object_list_lock to prevent
346 * a race with fscache_drop_object().
348 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
351 spin_unlock(&cache->object_list_lock);
352 cond_resched();
357 * fscache_withdraw_cache - Withdraw a cache from the active service
358 * @cache: The record describing the cache
360 * Withdraw a cache from service, unbinding all its cache objects from the
361 * netfs cookies they're currently representing.
363 * See Documentation/filesystems/caching/backend-api.txt for a complete
364 * description.
366 void fscache_withdraw_cache(struct fscache_cache *cache)
368 LIST_HEAD(dying_objects);
370 _enter("");
372 printk(KERN_NOTICE "FS-Cache: Withdrawing cache \"%s\"\n",
373 cache->tag->name);
375 /* make the cache unavailable for cookie acquisition */
376 if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags))
377 BUG();
379 down_write(&fscache_addremove_sem);
380 list_del_init(&cache->link);
381 cache->tag->cache = NULL;
382 up_write(&fscache_addremove_sem);
384 /* make sure all pages pinned by operations on behalf of the netfs are
385 * written to disk */
386 fscache_stat(&fscache_n_cop_sync_cache);
387 cache->ops->sync_cache(cache);
388 fscache_stat_d(&fscache_n_cop_sync_cache);
390 /* dissociate all the netfs pages backed by this cache from the block
391 * mappings in the cache */
392 fscache_stat(&fscache_n_cop_dissociate_pages);
393 cache->ops->dissociate_pages(cache);
394 fscache_stat_d(&fscache_n_cop_dissociate_pages);
396 /* we now have to destroy all the active objects pertaining to this
397 * cache - which we do by passing them off to thread pool to be
398 * disposed of */
399 _debug("destroy");
401 fscache_withdraw_all_objects(cache, &dying_objects);
403 /* wait for all extant objects to finish their outstanding operations
404 * and go away */
405 _debug("wait for finish");
406 wait_event(fscache_cache_cleared_wq,
407 atomic_read(&cache->object_count) == 0);
408 _debug("wait for clearance");
409 wait_event(fscache_cache_cleared_wq,
410 list_empty(&cache->object_list));
411 _debug("cleared");
412 ASSERT(list_empty(&dying_objects));
414 kobject_put(cache->kobj);
416 clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
417 fscache_release_cache_tag(cache->tag);
418 cache->tag = NULL;
420 _leave("");
422 EXPORT_SYMBOL(fscache_withdraw_cache);