1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* FS-Cache cache handling
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define FSCACHE_DEBUG_LEVEL CACHE
9 #include <linux/module.h>
10 #include <linux/slab.h>
13 LIST_HEAD(fscache_cache_list
);
14 DECLARE_RWSEM(fscache_addremove_sem
);
15 DECLARE_WAIT_QUEUE_HEAD(fscache_cache_cleared_wq
);
16 EXPORT_SYMBOL(fscache_cache_cleared_wq
);
18 static LIST_HEAD(fscache_cache_tag_list
);
23 struct fscache_cache_tag
*__fscache_lookup_cache_tag(const char *name
)
25 struct fscache_cache_tag
*tag
, *xtag
;
27 /* firstly check for the existence of the tag under read lock */
28 down_read(&fscache_addremove_sem
);
30 list_for_each_entry(tag
, &fscache_cache_tag_list
, link
) {
31 if (strcmp(tag
->name
, name
) == 0) {
32 atomic_inc(&tag
->usage
);
33 up_read(&fscache_addremove_sem
);
38 up_read(&fscache_addremove_sem
);
40 /* the tag does not exist - create a candidate */
41 xtag
= kzalloc(sizeof(*xtag
) + strlen(name
) + 1, GFP_KERNEL
);
43 /* return a dummy tag if out of memory */
44 return ERR_PTR(-ENOMEM
);
46 atomic_set(&xtag
->usage
, 1);
47 strcpy(xtag
->name
, name
);
49 /* write lock, search again and add if still not present */
50 down_write(&fscache_addremove_sem
);
52 list_for_each_entry(tag
, &fscache_cache_tag_list
, link
) {
53 if (strcmp(tag
->name
, name
) == 0) {
54 atomic_inc(&tag
->usage
);
55 up_write(&fscache_addremove_sem
);
61 list_add_tail(&xtag
->link
, &fscache_cache_tag_list
);
62 up_write(&fscache_addremove_sem
);
67 * release a reference to a cache tag
69 void __fscache_release_cache_tag(struct fscache_cache_tag
*tag
)
71 if (tag
!= ERR_PTR(-ENOMEM
)) {
72 down_write(&fscache_addremove_sem
);
74 if (atomic_dec_and_test(&tag
->usage
))
75 list_del_init(&tag
->link
);
79 up_write(&fscache_addremove_sem
);
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
90 struct fscache_cache
*fscache_select_cache_for_object(
91 struct fscache_cookie
*cookie
)
93 struct fscache_cache_tag
*tag
;
94 struct fscache_object
*object
;
95 struct fscache_cache
*cache
;
99 if (list_empty(&fscache_cache_list
)) {
100 _leave(" = NULL [no cache]");
104 /* we check the parent to determine the cache to use */
105 spin_lock(&cookie
->lock
);
107 /* the first in the parent's backing list should be the preferred
109 if (!hlist_empty(&cookie
->backing_objects
)) {
110 object
= hlist_entry(cookie
->backing_objects
.first
,
111 struct fscache_object
, cookie_link
);
113 cache
= object
->cache
;
114 if (fscache_object_is_dying(object
) ||
115 test_bit(FSCACHE_IOERROR
, &cache
->flags
))
118 spin_unlock(&cookie
->lock
);
119 _leave(" = %p [parent]", cache
);
123 /* the parent is unbacked */
124 if (cookie
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
125 /* cookie not an index and is unbacked */
126 spin_unlock(&cookie
->lock
);
127 _leave(" = NULL [cookie ub,ni]");
131 spin_unlock(&cookie
->lock
);
133 if (!cookie
->def
->select_cache
)
136 /* ask the netfs for its preference */
137 tag
= cookie
->def
->select_cache(cookie
->parent
->netfs_data
,
142 if (tag
== ERR_PTR(-ENOMEM
)) {
143 _leave(" = NULL [nomem tag]");
148 _leave(" = NULL [unbacked tag]");
152 if (test_bit(FSCACHE_IOERROR
, &tag
->cache
->flags
))
155 _leave(" = %p [specific]", tag
->cache
);
159 /* netfs has no preference - just select first cache */
160 cache
= list_entry(fscache_cache_list
.next
,
161 struct fscache_cache
, link
);
162 _leave(" = %p [first]", cache
);
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
170 * @idfmt: Format string to define identifier
171 * @...: sprintf-style arguments
173 * Initialise a record of a cache and fill in the name.
175 * See Documentation/filesystems/caching/backend-api.txt for a complete
178 void fscache_init_cache(struct fscache_cache
*cache
,
179 const struct fscache_cache_ops
*ops
,
185 memset(cache
, 0, sizeof(*cache
));
190 vsnprintf(cache
->identifier
, sizeof(cache
->identifier
), idfmt
, va
);
193 INIT_WORK(&cache
->op_gc
, fscache_operation_gc
);
194 INIT_LIST_HEAD(&cache
->link
);
195 INIT_LIST_HEAD(&cache
->object_list
);
196 INIT_LIST_HEAD(&cache
->op_gc_list
);
197 spin_lock_init(&cache
->object_list_lock
);
198 spin_lock_init(&cache
->op_gc_list_lock
);
200 EXPORT_SYMBOL(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.txt for a complete
213 int fscache_add_cache(struct fscache_cache
*cache
,
214 struct fscache_object
*ifsdef
,
217 struct fscache_cache_tag
*tag
;
219 ASSERTCMP(ifsdef
->cookie
, ==, &fscache_fsdef_index
);
225 ((1 << NR_FSCACHE_OBJECT_EVENTS
) - 1) &
226 ~(1 << FSCACHE_OBJECT_EV_CLEARED
);
227 __set_bit(FSCACHE_OBJECT_IS_AVAILABLE
, &ifsdef
->flags
);
230 tagname
= cache
->identifier
;
234 _enter("{%s.%s},,%s", cache
->ops
->name
, cache
->identifier
, tagname
);
236 /* we use the cache tag to uniquely identify caches */
237 tag
= __fscache_lookup_cache_tag(tagname
);
241 if (test_and_set_bit(FSCACHE_TAG_RESERVED
, &tag
->flags
))
244 cache
->kobj
= kobject_create_and_add(tagname
, fscache_root
);
248 ifsdef
->cache
= cache
;
249 cache
->fsdef
= ifsdef
;
251 down_write(&fscache_addremove_sem
);
256 /* add the cache to the list */
257 list_add(&cache
->link
, &fscache_cache_list
);
259 /* add the cache's netfs definition index object to the cache's
261 spin_lock(&cache
->object_list_lock
);
262 list_add_tail(&ifsdef
->cache_link
, &cache
->object_list
);
263 spin_unlock(&cache
->object_list_lock
);
264 fscache_objlist_add(ifsdef
);
266 /* add the cache's netfs definition index object to the top level index
267 * cookie as a known backing object */
268 spin_lock(&fscache_fsdef_index
.lock
);
270 hlist_add_head(&ifsdef
->cookie_link
,
271 &fscache_fsdef_index
.backing_objects
);
273 atomic_inc(&fscache_fsdef_index
.usage
);
276 spin_unlock(&fscache_fsdef_index
.lock
);
277 up_write(&fscache_addremove_sem
);
279 pr_notice("Cache \"%s\" added (type %s)\n",
280 cache
->tag
->name
, cache
->ops
->name
);
281 kobject_uevent(cache
->kobj
, KOBJ_ADD
);
283 _leave(" = 0 [%s]", cache
->identifier
);
287 pr_err("Cache tag '%s' already in use\n", tagname
);
288 __fscache_release_cache_tag(tag
);
293 __fscache_release_cache_tag(tag
);
294 _leave(" = -EINVAL");
298 _leave(" = -ENOMEM");
301 EXPORT_SYMBOL(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
308 * used for anything. This also reports the error into the kernel log.
310 * See Documentation/filesystems/caching/backend-api.txt for a complete
313 void fscache_io_error(struct fscache_cache
*cache
)
315 if (!test_and_set_bit(FSCACHE_IOERROR
, &cache
->flags
))
316 pr_err("Cache '%s' stopped due to I/O error\n",
319 EXPORT_SYMBOL(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
,
326 struct list_head
*dying_objects
)
328 struct fscache_object
*object
;
330 while (!list_empty(&cache
->object_list
)) {
331 spin_lock(&cache
->object_list_lock
);
333 if (!list_empty(&cache
->object_list
)) {
334 object
= list_entry(cache
->object_list
.next
,
335 struct fscache_object
, cache_link
);
336 list_move_tail(&object
->cache_link
, dying_objects
);
338 _debug("withdraw %p", object
->cookie
);
340 /* This must be done under object_list_lock to prevent
341 * a race with fscache_drop_object().
343 fscache_raise_event(object
, FSCACHE_OBJECT_EV_KILL
);
346 spin_unlock(&cache
->object_list_lock
);
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
356 * netfs cookies they're currently representing.
358 * See Documentation/filesystems/caching/backend-api.txt for a complete
361 void fscache_withdraw_cache(struct fscache_cache
*cache
)
363 LIST_HEAD(dying_objects
);
367 pr_notice("Withdrawing cache \"%s\"\n",
370 /* make the cache unavailable for cookie acquisition */
371 if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN
, &cache
->flags
))
374 down_write(&fscache_addremove_sem
);
375 list_del_init(&cache
->link
);
376 cache
->tag
->cache
= NULL
;
377 up_write(&fscache_addremove_sem
);
379 /* make sure all pages pinned by operations on behalf of the netfs are
381 fscache_stat(&fscache_n_cop_sync_cache
);
382 cache
->ops
->sync_cache(cache
);
383 fscache_stat_d(&fscache_n_cop_sync_cache
);
385 /* dissociate all the netfs pages backed by this cache from the block
386 * mappings in the cache */
387 fscache_stat(&fscache_n_cop_dissociate_pages
);
388 cache
->ops
->dissociate_pages(cache
);
389 fscache_stat_d(&fscache_n_cop_dissociate_pages
);
391 /* we now have to destroy all the active objects pertaining to this
392 * cache - which we do by passing them off to thread pool to be
396 fscache_withdraw_all_objects(cache
, &dying_objects
);
398 /* wait for all extant objects to finish their outstanding operations
400 _debug("wait for finish");
401 wait_event(fscache_cache_cleared_wq
,
402 atomic_read(&cache
->object_count
) == 0);
403 _debug("wait for clearance");
404 wait_event(fscache_cache_cleared_wq
,
405 list_empty(&cache
->object_list
));
407 ASSERT(list_empty(&dying_objects
));
409 kobject_put(cache
->kobj
);
411 clear_bit(FSCACHE_TAG_RESERVED
, &cache
->tag
->flags
);
412 fscache_release_cache_tag(cache
->tag
);
417 EXPORT_SYMBOL(fscache_withdraw_cache
);