1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Manage high-level VFS aspects of a cache.
4 * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/slab.h>
9 #include <linux/statfs.h>
10 #include <linux/namei.h>
14 * Bring a cache online.
16 int cachefiles_add_cache(struct cachefiles_cache *cache)
18 struct fscache_cache *cache_cookie;
21 struct dentry *graveyard, *cachedir, *root;
22 const struct cred *saved_cred;
27 cache_cookie = fscache_acquire_cache(cache->tag);
28 if (IS_ERR(cache_cookie))
29 return PTR_ERR(cache_cookie);
31 /* we want to work under the module's security ID */
32 ret = cachefiles_get_security_ID(cache);
36 cachefiles_begin_secure(cache, &saved_cred);
38 /* look up the directory at the root of the cache */
39 ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
43 cache->mnt = path.mnt;
47 if (is_idmapped_mnt(path.mnt)) {
48 pr_warn("File cache on idmapped mounts not supported");
49 goto error_unsupported;
52 /* Check features of the backing filesystem:
53 * - Directories must support looking up and directory creation
54 * - We create tmpfiles to handle invalidation
55 * - We use xattrs to store metadata
56 * - We need to be able to query the amount of space available
57 * - We want to be able to sync the filesystem when stopping the cache
58 * - We use DIO to/from pages, so the blocksize mustn't be too big.
61 if (d_is_negative(root) ||
62 !d_backing_inode(root)->i_op->lookup ||
63 !d_backing_inode(root)->i_op->mkdir ||
64 !d_backing_inode(root)->i_op->tmpfile ||
65 !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
66 !root->d_sb->s_op->statfs ||
67 !root->d_sb->s_op->sync_fs ||
68 root->d_sb->s_blocksize > PAGE_SIZE)
69 goto error_unsupported;
72 if (sb_rdonly(root->d_sb))
73 goto error_unsupported;
75 /* determine the security of the on-disk cache as this governs
76 * security ID of files we create */
77 ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
79 goto error_unsupported;
81 /* get the cache size and blocksize */
82 ret = vfs_statfs(&path, &stats);
84 goto error_unsupported;
87 if (stats.f_bsize <= 0)
88 goto error_unsupported;
91 if (stats.f_bsize > PAGE_SIZE)
92 goto error_unsupported;
94 cache->bsize = stats.f_bsize;
95 cache->bshift = ilog2(stats.f_bsize);
97 _debug("blksize %u (shift %u)",
98 cache->bsize, cache->bshift);
100 _debug("size %llu, avail %llu",
101 (unsigned long long) stats.f_blocks,
102 (unsigned long long) stats.f_bavail);
104 /* set up caching limits */
105 do_div(stats.f_files, 100);
106 cache->fstop = stats.f_files * cache->fstop_percent;
107 cache->fcull = stats.f_files * cache->fcull_percent;
108 cache->frun = stats.f_files * cache->frun_percent;
110 _debug("limits {%llu,%llu,%llu} files",
111 (unsigned long long) cache->frun,
112 (unsigned long long) cache->fcull,
113 (unsigned long long) cache->fstop);
115 do_div(stats.f_blocks, 100);
116 cache->bstop = stats.f_blocks * cache->bstop_percent;
117 cache->bcull = stats.f_blocks * cache->bcull_percent;
118 cache->brun = stats.f_blocks * cache->brun_percent;
120 _debug("limits {%llu,%llu,%llu} blocks",
121 (unsigned long long) cache->brun,
122 (unsigned long long) cache->bcull,
123 (unsigned long long) cache->bstop);
125 /* get the cache directory and check its type */
126 cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
127 if (IS_ERR(cachedir)) {
128 ret = PTR_ERR(cachedir);
129 goto error_unsupported;
132 cache->store = cachedir;
134 /* get the graveyard directory */
135 graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
136 if (IS_ERR(graveyard)) {
137 ret = PTR_ERR(graveyard);
138 goto error_unsupported;
141 cache->graveyard = graveyard;
142 cache->cache = cache_cookie;
144 ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
146 goto error_add_cache;
149 set_bit(CACHEFILES_READY, &cache->flags);
152 pr_info("File cache on %s registered\n", cache_cookie->name);
154 /* check how much space the cache has */
155 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
156 cachefiles_end_secure(cache, saved_cred);
157 _leave(" = 0 [%px]", cache->cache);
161 cachefiles_put_directory(cache->graveyard);
162 cache->graveyard = NULL;
164 cachefiles_put_directory(cache->store);
170 cachefiles_end_secure(cache, saved_cred);
171 put_cred(cache->cache_cred);
172 cache->cache_cred = NULL;
174 fscache_relinquish_cache(cache_cookie);
176 pr_err("Failed to register: %d\n", ret);
181 * See if we have space for a number of pages and/or a number of files in the
184 int cachefiles_has_space(struct cachefiles_cache *cache,
185 unsigned fnr, unsigned bnr,
186 enum cachefiles_has_space_for reason)
188 struct kstatfs stats;
189 u64 b_avail, b_writing;
194 .dentry = cache->mnt->mnt_root,
197 //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
198 // (unsigned long long) cache->frun,
199 // (unsigned long long) cache->fcull,
200 // (unsigned long long) cache->fstop,
201 // (unsigned long long) cache->brun,
202 // (unsigned long long) cache->bcull,
203 // (unsigned long long) cache->bstop,
206 /* find out how many pages of blockdev are available */
207 memset(&stats, 0, sizeof(stats));
209 ret = vfs_statfs(&path, &stats);
211 trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
212 cachefiles_trace_statfs_error);
214 cachefiles_io_error(cache, "statfs failed");
215 _leave(" = %d", ret);
219 b_avail = stats.f_bavail;
220 b_writing = atomic_long_read(&cache->b_writing);
221 if (b_avail > b_writing)
222 b_avail -= b_writing;
226 //_debug("avail %llu,%llu",
227 // (unsigned long long)stats.f_ffree,
228 // (unsigned long long)b_avail);
230 /* see if there is sufficient space */
231 if (stats.f_ffree > fnr)
232 stats.f_ffree -= fnr;
242 if (stats.f_ffree < cache->fstop ||
243 b_avail < cache->bstop)
244 goto stop_and_begin_cull;
247 if (stats.f_ffree < cache->fcull ||
248 b_avail < cache->bcull)
251 if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
252 stats.f_ffree >= cache->frun &&
253 b_avail >= cache->brun &&
254 test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
256 _debug("cease culling");
257 cachefiles_state_changed(cache);
265 case cachefiles_has_space_for_write:
266 fscache_count_no_write_space();
268 case cachefiles_has_space_for_create:
269 fscache_count_no_create_space();
275 if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
276 _debug("### CULL CACHE ###");
277 cachefiles_state_changed(cache);
280 _leave(" = %d", ret);
285 * Mark all the objects as being out of service and queue them all for cleanup.
287 static void cachefiles_withdraw_objects(struct cachefiles_cache *cache)
289 struct cachefiles_object *object;
290 unsigned int count = 0;
294 spin_lock(&cache->object_list_lock);
296 while (!list_empty(&cache->object_list)) {
297 object = list_first_entry(&cache->object_list,
298 struct cachefiles_object, cache_link);
299 cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
300 list_del_init(&object->cache_link);
301 fscache_withdraw_cookie(object->cookie);
303 if ((count & 63) == 0) {
304 spin_unlock(&cache->object_list_lock);
306 spin_lock(&cache->object_list_lock);
310 spin_unlock(&cache->object_list_lock);
311 _leave(" [%u objs]", count);
317 static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
322 struct cachefiles_volume *volume = NULL;
324 spin_lock(&cache->object_list_lock);
325 if (!list_empty(&cache->volumes)) {
326 volume = list_first_entry(&cache->volumes,
327 struct cachefiles_volume, cache_link);
328 list_del_init(&volume->cache_link);
330 spin_unlock(&cache->object_list_lock);
334 cachefiles_withdraw_volume(volume);
341 * Sync a cache to backing disk.
343 static void cachefiles_sync_cache(struct cachefiles_cache *cache)
345 const struct cred *saved_cred;
348 _enter("%s", cache->cache->name);
350 /* make sure all pages pinned by operations on behalf of the netfs are
352 cachefiles_begin_secure(cache, &saved_cred);
353 down_read(&cache->mnt->mnt_sb->s_umount);
354 ret = sync_filesystem(cache->mnt->mnt_sb);
355 up_read(&cache->mnt->mnt_sb->s_umount);
356 cachefiles_end_secure(cache, saved_cred);
359 cachefiles_io_error(cache,
360 "Attempt to sync backing fs superblock returned error %d",
365 * Withdraw cache objects.
367 void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
369 struct fscache_cache *fscache = cache->cache;
371 pr_info("File cache on %s unregistering\n", fscache->name);
373 fscache_withdraw_cache(fscache);
375 /* we now have to destroy all the active objects pertaining to this
376 * cache - which we do by passing them off to thread pool to be
378 cachefiles_withdraw_objects(cache);
379 fscache_wait_for_objects(fscache);
381 cachefiles_withdraw_volumes(cache);
382 cachefiles_sync_cache(cache);
384 fscache_relinquish_cache(fscache);