Lines Matching +full:cache +full:-
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* FS-Cache cache handling
8 #define FSCACHE_DEBUG_LEVEL CACHE
21 * look up a cache tag
31 if (strcmp(tag->name, name) == 0) { in __fscache_lookup_cache_tag()
32 atomic_inc(&tag->usage); in __fscache_lookup_cache_tag()
40 /* the tag does not exist - create a candidate */ in __fscache_lookup_cache_tag()
44 return ERR_PTR(-ENOMEM); in __fscache_lookup_cache_tag()
46 atomic_set(&xtag->usage, 1); in __fscache_lookup_cache_tag()
47 strcpy(xtag->name, name); in __fscache_lookup_cache_tag()
53 if (strcmp(tag->name, name) == 0) { in __fscache_lookup_cache_tag()
54 atomic_inc(&tag->usage); in __fscache_lookup_cache_tag()
61 list_add_tail(&xtag->link, &fscache_cache_tag_list); in __fscache_lookup_cache_tag()
67 * release a reference to a cache tag
71 if (tag != ERR_PTR(-ENOMEM)) { in __fscache_release_cache_tag()
74 if (atomic_dec_and_test(&tag->usage)) in __fscache_release_cache_tag()
75 list_del_init(&tag->link); in __fscache_release_cache_tag()
86 * select a cache in which to store an object
87 * - the cache addremove semaphore must be at least read-locked by the caller
88 * - the object will never be an index
95 struct fscache_cache *cache; in fscache_select_cache_for_object() local
100 _leave(" = NULL [no cache]"); in fscache_select_cache_for_object()
104 /* we check the parent to determine the cache to use */ in fscache_select_cache_for_object()
105 spin_lock(&cookie->lock); in fscache_select_cache_for_object()
108 * cache */ in fscache_select_cache_for_object()
109 if (!hlist_empty(&cookie->backing_objects)) { in fscache_select_cache_for_object()
110 object = hlist_entry(cookie->backing_objects.first, in fscache_select_cache_for_object()
113 cache = object->cache; in fscache_select_cache_for_object()
115 test_bit(FSCACHE_IOERROR, &cache->flags)) in fscache_select_cache_for_object()
116 cache = NULL; in fscache_select_cache_for_object()
118 spin_unlock(&cookie->lock); in fscache_select_cache_for_object()
119 _leave(" = %p [parent]", cache); in fscache_select_cache_for_object()
120 return cache; in fscache_select_cache_for_object()
124 if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) { in fscache_select_cache_for_object()
126 spin_unlock(&cookie->lock); in fscache_select_cache_for_object()
131 spin_unlock(&cookie->lock); in fscache_select_cache_for_object()
133 if (!cookie->def->select_cache) in fscache_select_cache_for_object()
137 tag = cookie->def->select_cache(cookie->parent->netfs_data, in fscache_select_cache_for_object()
138 cookie->netfs_data); in fscache_select_cache_for_object()
142 if (tag == ERR_PTR(-ENOMEM)) { in fscache_select_cache_for_object()
147 if (!tag->cache) { in fscache_select_cache_for_object()
152 if (test_bit(FSCACHE_IOERROR, &tag->cache->flags)) in fscache_select_cache_for_object()
155 _leave(" = %p [specific]", tag->cache); in fscache_select_cache_for_object()
156 return tag->cache; in fscache_select_cache_for_object()
159 /* netfs has no preference - just select first cache */ in fscache_select_cache_for_object()
160 cache = list_entry(fscache_cache_list.next, in fscache_select_cache_for_object()
162 _leave(" = %p [first]", cache); in fscache_select_cache_for_object()
163 return cache; in fscache_select_cache_for_object()
167 * fscache_init_cache - Initialise a cache record
168 * @cache: The cache record to be initialised
169 * @ops: The cache operations to be installed in that record
171 * @...: sprintf-style arguments
173 * Initialise a record of a cache and fill in the name.
175 * See Documentation/filesystems/caching/backend-api.rst for a complete
178 void fscache_init_cache(struct fscache_cache *cache, in fscache_init_cache() argument
185 memset(cache, 0, sizeof(*cache)); in fscache_init_cache()
187 cache->ops = ops; in fscache_init_cache()
190 vsnprintf(cache->identifier, sizeof(cache->identifier), idfmt, va); in fscache_init_cache()
193 INIT_WORK(&cache->op_gc, fscache_operation_gc); in fscache_init_cache()
194 INIT_LIST_HEAD(&cache->link); in fscache_init_cache()
195 INIT_LIST_HEAD(&cache->object_list); in fscache_init_cache()
196 INIT_LIST_HEAD(&cache->op_gc_list); in fscache_init_cache()
197 spin_lock_init(&cache->object_list_lock); in fscache_init_cache()
198 spin_lock_init(&cache->op_gc_list_lock); in fscache_init_cache()
203 * fscache_add_cache - Declare a cache as being open for business
204 * @cache: The record describing the cache
205 * @ifsdef: The record of the cache object describing the top-level index
206 * @tagname: The tag describing this cache
208 * Add a cache to the system, making it available for netfs's to use.
210 * See Documentation/filesystems/caching/backend-api.rst for a complete
213 int fscache_add_cache(struct fscache_cache *cache, in fscache_add_cache() argument
219 ASSERTCMP(ifsdef->cookie, ==, &fscache_fsdef_index); in fscache_add_cache()
220 BUG_ON(!cache->ops); in fscache_add_cache()
223 cache->flags = 0; in fscache_add_cache()
224 ifsdef->event_mask = in fscache_add_cache()
225 ((1 << NR_FSCACHE_OBJECT_EVENTS) - 1) & in fscache_add_cache()
227 __set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &ifsdef->flags); in fscache_add_cache()
230 tagname = cache->identifier; in fscache_add_cache()
234 _enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname); in fscache_add_cache()
236 /* we use the cache tag to uniquely identify caches */ in fscache_add_cache()
241 if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags)) in fscache_add_cache()
244 cache->kobj = kobject_create_and_add(tagname, fscache_root); in fscache_add_cache()
245 if (!cache->kobj) in fscache_add_cache()
248 ifsdef->cache = cache; in fscache_add_cache()
249 cache->fsdef = ifsdef; in fscache_add_cache()
253 tag->cache = cache; in fscache_add_cache()
254 cache->tag = tag; in fscache_add_cache()
256 /* add the cache to the list */ in fscache_add_cache()
257 list_add(&cache->link, &fscache_cache_list); in fscache_add_cache()
259 /* add the cache's netfs definition index object to the cache's in fscache_add_cache()
261 spin_lock(&cache->object_list_lock); in fscache_add_cache()
262 list_add_tail(&ifsdef->cache_link, &cache->object_list); in fscache_add_cache()
263 spin_unlock(&cache->object_list_lock); in fscache_add_cache()
266 /* add the cache's netfs definition index object to the top level index in fscache_add_cache()
270 hlist_add_head(&ifsdef->cookie_link, in fscache_add_cache()
279 pr_notice("Cache \"%s\" added (type %s)\n", in fscache_add_cache()
280 cache->tag->name, cache->ops->name); in fscache_add_cache()
281 kobject_uevent(cache->kobj, KOBJ_ADD); in fscache_add_cache()
283 _leave(" = 0 [%s]", cache->identifier); in fscache_add_cache()
287 pr_err("Cache tag '%s' already in use\n", tagname); in fscache_add_cache()
289 _leave(" = -EXIST"); in fscache_add_cache()
290 return -EEXIST; in fscache_add_cache()
294 _leave(" = -EINVAL"); in fscache_add_cache()
295 return -EINVAL; in fscache_add_cache()
298 _leave(" = -ENOMEM"); in fscache_add_cache()
299 return -ENOMEM; in fscache_add_cache()
304 * fscache_io_error - Note a cache I/O error
305 * @cache: The record describing the cache
307 * Note that an I/O error occurred in a cache and that it should no longer be
310 * See Documentation/filesystems/caching/backend-api.rst for a complete
313 void fscache_io_error(struct fscache_cache *cache) in fscache_io_error() argument
315 if (!test_and_set_bit(FSCACHE_IOERROR, &cache->flags)) in fscache_io_error()
316 pr_err("Cache '%s' stopped due to I/O error\n", in fscache_io_error()
317 cache->ops->name); in fscache_io_error()
322 * request withdrawal of all the objects in a cache
323 * - all the objects being withdrawn are moved onto the supplied list
325 static void fscache_withdraw_all_objects(struct fscache_cache *cache, in fscache_withdraw_all_objects() argument
330 while (!list_empty(&cache->object_list)) { in fscache_withdraw_all_objects()
331 spin_lock(&cache->object_list_lock); in fscache_withdraw_all_objects()
333 if (!list_empty(&cache->object_list)) { in fscache_withdraw_all_objects()
334 object = list_entry(cache->object_list.next, in fscache_withdraw_all_objects()
336 list_move_tail(&object->cache_link, dying_objects); in fscache_withdraw_all_objects()
338 _debug("withdraw %p", object->cookie); in fscache_withdraw_all_objects()
346 spin_unlock(&cache->object_list_lock); in fscache_withdraw_all_objects()
352 * fscache_withdraw_cache - Withdraw a cache from the active service
353 * @cache: The record describing the cache
355 * Withdraw a cache from service, unbinding all its cache objects from the
358 * See Documentation/filesystems/caching/backend-api.rst for a complete
361 void fscache_withdraw_cache(struct fscache_cache *cache) in fscache_withdraw_cache() argument
367 pr_notice("Withdrawing cache \"%s\"\n", in fscache_withdraw_cache()
368 cache->tag->name); in fscache_withdraw_cache()
370 /* make the cache unavailable for cookie acquisition */ in fscache_withdraw_cache()
371 if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags)) in fscache_withdraw_cache()
375 list_del_init(&cache->link); in fscache_withdraw_cache()
376 cache->tag->cache = NULL; in fscache_withdraw_cache()
382 cache->ops->sync_cache(cache); in fscache_withdraw_cache()
385 /* dissociate all the netfs pages backed by this cache from the block in fscache_withdraw_cache()
386 * mappings in the cache */ in fscache_withdraw_cache()
388 cache->ops->dissociate_pages(cache); in fscache_withdraw_cache()
392 * cache - which we do by passing them off to thread pool to be in fscache_withdraw_cache()
396 fscache_withdraw_all_objects(cache, &dying_objects); in fscache_withdraw_cache()
402 atomic_read(&cache->object_count) == 0); in fscache_withdraw_cache()
405 list_empty(&cache->object_list)); in fscache_withdraw_cache()
409 kobject_put(cache->kobj); in fscache_withdraw_cache()
411 clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags); in fscache_withdraw_cache()
412 fscache_release_cache_tag(cache->tag); in fscache_withdraw_cache()
413 cache->tag = NULL; in fscache_withdraw_cache()