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 parameters */
54 if (d_is_negative(root) ||
55 !d_backing_inode(root)->i_op->lookup ||
56 !d_backing_inode(root)->i_op->mkdir ||
57 !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
58 !root->d_sb->s_op->statfs ||
59 !root->d_sb->s_op->sync_fs ||
60 root->d_sb->s_blocksize > PAGE_SIZE)
61 goto error_unsupported;
64 if (sb_rdonly(root->d_sb))
65 goto error_unsupported;
67 /* determine the security of the on-disk cache as this governs
68 * security ID of files we create */
69 ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
71 goto error_unsupported;
73 /* get the cache size and blocksize */
74 ret = vfs_statfs(&path, &stats);
76 goto error_unsupported;
79 if (stats.f_bsize <= 0)
80 goto error_unsupported;
83 if (stats.f_bsize > PAGE_SIZE)
84 goto error_unsupported;
86 cache->bsize = stats.f_bsize;
87 cache->bshift = ilog2(stats.f_bsize);
89 _debug("blksize %u (shift %u)",
90 cache->bsize, cache->bshift);
92 _debug("size %llu, avail %llu",
93 (unsigned long long) stats.f_blocks,
94 (unsigned long long) stats.f_bavail);
96 /* set up caching limits */
97 do_div(stats.f_files, 100);
98 cache->fstop = stats.f_files * cache->fstop_percent;
99 cache->fcull = stats.f_files * cache->fcull_percent;
100 cache->frun = stats.f_files * cache->frun_percent;
102 _debug("limits {%llu,%llu,%llu} files",
103 (unsigned long long) cache->frun,
104 (unsigned long long) cache->fcull,
105 (unsigned long long) cache->fstop);
107 do_div(stats.f_blocks, 100);
108 cache->bstop = stats.f_blocks * cache->bstop_percent;
109 cache->bcull = stats.f_blocks * cache->bcull_percent;
110 cache->brun = stats.f_blocks * cache->brun_percent;
112 _debug("limits {%llu,%llu,%llu} blocks",
113 (unsigned long long) cache->brun,
114 (unsigned long long) cache->bcull,
115 (unsigned long long) cache->bstop);
117 /* get the cache directory and check its type */
118 cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
119 if (IS_ERR(cachedir)) {
120 ret = PTR_ERR(cachedir);
121 goto error_unsupported;
124 cache->store = cachedir;
126 /* get the graveyard directory */
127 graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
128 if (IS_ERR(graveyard)) {
129 ret = PTR_ERR(graveyard);
130 goto error_unsupported;
133 cache->graveyard = graveyard;
134 cache->cache = cache_cookie;
136 ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
138 goto error_add_cache;
141 set_bit(CACHEFILES_READY, &cache->flags);
144 pr_info("File cache on %s registered\n", cache_cookie->name);
146 /* check how much space the cache has */
147 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
148 cachefiles_end_secure(cache, saved_cred);
149 _leave(" = 0 [%px]", cache->cache);
153 cachefiles_put_directory(cache->graveyard);
154 cache->graveyard = NULL;
156 cachefiles_put_directory(cache->store);
162 cachefiles_end_secure(cache, saved_cred);
164 fscache_relinquish_cache(cache_cookie);
166 pr_err("Failed to register: %d\n", ret);
171 * See if we have space for a number of pages and/or a number of files in the
174 int cachefiles_has_space(struct cachefiles_cache *cache,
175 unsigned fnr, unsigned bnr,
176 enum cachefiles_has_space_for reason)
178 struct kstatfs stats;
179 u64 b_avail, b_writing;
184 .dentry = cache->mnt->mnt_root,
187 //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
188 // (unsigned long long) cache->frun,
189 // (unsigned long long) cache->fcull,
190 // (unsigned long long) cache->fstop,
191 // (unsigned long long) cache->brun,
192 // (unsigned long long) cache->bcull,
193 // (unsigned long long) cache->bstop,
196 /* find out how many pages of blockdev are available */
197 memset(&stats, 0, sizeof(stats));
199 ret = vfs_statfs(&path, &stats);
201 trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
202 cachefiles_trace_statfs_error);
204 cachefiles_io_error(cache, "statfs failed");
205 _leave(" = %d", ret);
209 b_avail = stats.f_bavail;
210 b_writing = atomic_long_read(&cache->b_writing);
211 if (b_avail > b_writing)
212 b_avail -= b_writing;
216 //_debug("avail %llu,%llu",
217 // (unsigned long long)stats.f_ffree,
218 // (unsigned long long)b_avail);
220 /* see if there is sufficient space */
221 if (stats.f_ffree > fnr)
222 stats.f_ffree -= fnr;
232 if (stats.f_ffree < cache->fstop ||
233 b_avail < cache->bstop)
234 goto stop_and_begin_cull;
237 if (stats.f_ffree < cache->fcull ||
238 b_avail < cache->bcull)
241 if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
242 stats.f_ffree >= cache->frun &&
243 b_avail >= cache->brun &&
244 test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
246 _debug("cease culling");
247 cachefiles_state_changed(cache);
255 case cachefiles_has_space_for_write:
256 fscache_count_no_write_space();
258 case cachefiles_has_space_for_create:
259 fscache_count_no_create_space();
265 if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
266 _debug("### CULL CACHE ###");
267 cachefiles_state_changed(cache);
270 _leave(" = %d", ret);
275 * Mark all the objects as being out of service and queue them all for cleanup.
277 static void cachefiles_withdraw_objects(struct cachefiles_cache *cache)
279 struct cachefiles_object *object;
280 unsigned int count = 0;
284 spin_lock(&cache->object_list_lock);
286 while (!list_empty(&cache->object_list)) {
287 object = list_first_entry(&cache->object_list,
288 struct cachefiles_object, cache_link);
289 cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
290 list_del_init(&object->cache_link);
291 fscache_withdraw_cookie(object->cookie);
293 if ((count & 63) == 0) {
294 spin_unlock(&cache->object_list_lock);
296 spin_lock(&cache->object_list_lock);
300 spin_unlock(&cache->object_list_lock);
301 _leave(" [%u objs]", count);
307 static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
312 struct cachefiles_volume *volume = NULL;
314 spin_lock(&cache->object_list_lock);
315 if (!list_empty(&cache->volumes)) {
316 volume = list_first_entry(&cache->volumes,
317 struct cachefiles_volume, cache_link);
318 list_del_init(&volume->cache_link);
320 spin_unlock(&cache->object_list_lock);
324 cachefiles_withdraw_volume(volume);
331 * Sync a cache to backing disk.
333 static void cachefiles_sync_cache(struct cachefiles_cache *cache)
335 const struct cred *saved_cred;
338 _enter("%s", cache->cache->name);
340 /* make sure all pages pinned by operations on behalf of the netfs are
342 cachefiles_begin_secure(cache, &saved_cred);
343 down_read(&cache->mnt->mnt_sb->s_umount);
344 ret = sync_filesystem(cache->mnt->mnt_sb);
345 up_read(&cache->mnt->mnt_sb->s_umount);
346 cachefiles_end_secure(cache, saved_cred);
349 cachefiles_io_error(cache,
350 "Attempt to sync backing fs superblock returned error %d",
355 * Withdraw cache objects.
357 void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
359 struct fscache_cache *fscache = cache->cache;
361 pr_info("File cache on %s unregistering\n", fscache->name);
363 fscache_withdraw_cache(fscache);
365 /* we now have to destroy all the active objects pertaining to this
366 * cache - which we do by passing them off to thread pool to be
368 cachefiles_withdraw_objects(cache);
369 fscache_wait_for_objects(fscache);
371 cachefiles_withdraw_volumes(cache);
372 cachefiles_sync_cache(cache);
374 fscache_relinquish_cache(fscache);