Skip to content

Commit d1065b0

Browse files
committed
cachefiles: Implement cache registration and withdrawal
Do the following: (1) Fill out cachefiles_daemon_add_cache() so that it sets up the cache directories and registers the cache with cachefiles. (2) Add a function to do the top-level part of cache withdrawal and unregistration. (3) Add a function to sync a cache. Signed-off-by: David Howells <[email protected]> Reviewed-by: Jeff Layton <[email protected]> cc: [email protected] Link: https://lore.kernel.org/r/163819633175.215744.10857127598041268340.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/163906935445.143852.15545194974036410029.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/163967142904.1823006.244055483596047072.stgit@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/164021543872.640689.14370017789605073222.stgit@warthog.procyon.org.uk/ # v4
1 parent 32759f7 commit d1065b0

File tree

5 files changed

+240
-3
lines changed

5 files changed

+240
-3
lines changed

fs/cachefiles/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
cachefiles-y := \
77
cache.o \
88
daemon.o \
9+
interface.o \
910
main.o \
1011
namei.o \
1112
security.o

fs/cachefiles/cache.c

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,166 @@
1010
#include <linux/namei.h>
1111
#include "internal.h"
1212

13+
/*
14+
* Bring a cache online.
15+
*/
16+
int cachefiles_add_cache(struct cachefiles_cache *cache)
17+
{
18+
struct fscache_cache *cache_cookie;
19+
struct path path;
20+
struct kstatfs stats;
21+
struct dentry *graveyard, *cachedir, *root;
22+
const struct cred *saved_cred;
23+
int ret;
24+
25+
_enter("");
26+
27+
cache_cookie = fscache_acquire_cache(cache->tag);
28+
if (IS_ERR(cache_cookie))
29+
return PTR_ERR(cache_cookie);
30+
31+
/* we want to work under the module's security ID */
32+
ret = cachefiles_get_security_ID(cache);
33+
if (ret < 0)
34+
goto error_getsec;
35+
36+
cachefiles_begin_secure(cache, &saved_cred);
37+
38+
/* look up the directory at the root of the cache */
39+
ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
40+
if (ret < 0)
41+
goto error_open_root;
42+
43+
cache->mnt = path.mnt;
44+
root = path.dentry;
45+
46+
ret = -EINVAL;
47+
if (mnt_user_ns(path.mnt) != &init_user_ns) {
48+
pr_warn("File cache on idmapped mounts not supported");
49+
goto error_unsupported;
50+
}
51+
52+
/* check parameters */
53+
ret = -EOPNOTSUPP;
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;
62+
63+
ret = -EROFS;
64+
if (sb_rdonly(root->d_sb))
65+
goto error_unsupported;
66+
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);
70+
if (ret < 0)
71+
goto error_unsupported;
72+
73+
/* get the cache size and blocksize */
74+
ret = vfs_statfs(&path, &stats);
75+
if (ret < 0)
76+
goto error_unsupported;
77+
78+
ret = -ERANGE;
79+
if (stats.f_bsize <= 0)
80+
goto error_unsupported;
81+
82+
ret = -EOPNOTSUPP;
83+
if (stats.f_bsize > PAGE_SIZE)
84+
goto error_unsupported;
85+
86+
cache->bsize = stats.f_bsize;
87+
cache->bshift = 0;
88+
if (stats.f_bsize < PAGE_SIZE)
89+
cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
90+
91+
_debug("blksize %u (shift %u)",
92+
cache->bsize, cache->bshift);
93+
94+
_debug("size %llu, avail %llu",
95+
(unsigned long long) stats.f_blocks,
96+
(unsigned long long) stats.f_bavail);
97+
98+
/* set up caching limits */
99+
do_div(stats.f_files, 100);
100+
cache->fstop = stats.f_files * cache->fstop_percent;
101+
cache->fcull = stats.f_files * cache->fcull_percent;
102+
cache->frun = stats.f_files * cache->frun_percent;
103+
104+
_debug("limits {%llu,%llu,%llu} files",
105+
(unsigned long long) cache->frun,
106+
(unsigned long long) cache->fcull,
107+
(unsigned long long) cache->fstop);
108+
109+
stats.f_blocks >>= cache->bshift;
110+
do_div(stats.f_blocks, 100);
111+
cache->bstop = stats.f_blocks * cache->bstop_percent;
112+
cache->bcull = stats.f_blocks * cache->bcull_percent;
113+
cache->brun = stats.f_blocks * cache->brun_percent;
114+
115+
_debug("limits {%llu,%llu,%llu} blocks",
116+
(unsigned long long) cache->brun,
117+
(unsigned long long) cache->bcull,
118+
(unsigned long long) cache->bstop);
119+
120+
/* get the cache directory and check its type */
121+
cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
122+
if (IS_ERR(cachedir)) {
123+
ret = PTR_ERR(cachedir);
124+
goto error_unsupported;
125+
}
126+
127+
cache->store = cachedir;
128+
129+
/* get the graveyard directory */
130+
graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
131+
if (IS_ERR(graveyard)) {
132+
ret = PTR_ERR(graveyard);
133+
goto error_unsupported;
134+
}
135+
136+
cache->graveyard = graveyard;
137+
cache->cache = cache_cookie;
138+
139+
ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
140+
if (ret < 0)
141+
goto error_add_cache;
142+
143+
/* done */
144+
set_bit(CACHEFILES_READY, &cache->flags);
145+
dput(root);
146+
147+
pr_info("File cache on %s registered\n", cache_cookie->name);
148+
149+
/* check how much space the cache has */
150+
cachefiles_has_space(cache, 0, 0);
151+
cachefiles_end_secure(cache, saved_cred);
152+
_leave(" = 0 [%px]", cache->cache);
153+
return 0;
154+
155+
error_add_cache:
156+
cachefiles_put_directory(cache->graveyard);
157+
cache->graveyard = NULL;
158+
error_unsupported:
159+
cachefiles_put_directory(cache->store);
160+
cache->store = NULL;
161+
mntput(cache->mnt);
162+
cache->mnt = NULL;
163+
dput(root);
164+
error_open_root:
165+
cachefiles_end_secure(cache, saved_cred);
166+
error_getsec:
167+
fscache_relinquish_cache(cache_cookie);
168+
cache->cache = NULL;
169+
pr_err("Failed to register: %d\n", ret);
170+
return ret;
171+
}
172+
13173
/*
14174
* See if we have space for a number of pages and/or a number of files in the
15175
* cache
@@ -101,3 +261,50 @@ int cachefiles_has_space(struct cachefiles_cache *cache,
101261
_leave(" = %d", ret);
102262
return ret;
103263
}
264+
265+
/*
266+
* Sync a cache to backing disk.
267+
*/
268+
static void cachefiles_sync_cache(struct cachefiles_cache *cache)
269+
{
270+
const struct cred *saved_cred;
271+
int ret;
272+
273+
_enter("%s", cache->cache->name);
274+
275+
/* make sure all pages pinned by operations on behalf of the netfs are
276+
* written to disc */
277+
cachefiles_begin_secure(cache, &saved_cred);
278+
down_read(&cache->mnt->mnt_sb->s_umount);
279+
ret = sync_filesystem(cache->mnt->mnt_sb);
280+
up_read(&cache->mnt->mnt_sb->s_umount);
281+
cachefiles_end_secure(cache, saved_cred);
282+
283+
if (ret == -EIO)
284+
cachefiles_io_error(cache,
285+
"Attempt to sync backing fs superblock returned error %d",
286+
ret);
287+
}
288+
289+
/*
290+
* Withdraw cache objects.
291+
*/
292+
void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
293+
{
294+
struct fscache_cache *fscache = cache->cache;
295+
296+
pr_info("File cache on %s unregistering\n", fscache->name);
297+
298+
fscache_withdraw_cache(fscache);
299+
300+
/* we now have to destroy all the active objects pertaining to this
301+
* cache - which we do by passing them off to thread pool to be
302+
* disposed of */
303+
// PLACEHOLDER: Withdraw objects
304+
fscache_wait_for_objects(fscache);
305+
306+
// PLACEHOLDER: Withdraw volume
307+
cachefiles_sync_cache(cache);
308+
cache->cache = NULL;
309+
fscache_relinquish_cache(fscache);
310+
}

fs/cachefiles/daemon.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
702702

703703
pr_warn("Cache is disabled for development\n");
704704
return -ENOANO; // Don't allow the cache to operate yet
705+
//return cachefiles_add_cache(cache);
705706
}
706707

707708
/*
@@ -711,10 +712,11 @@ static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
711712
{
712713
_enter("");
713714

714-
if (test_bit(CACHEFILES_READY, &cache->flags)) {
715-
// PLACEHOLDER: Withdraw cache
716-
}
715+
if (test_bit(CACHEFILES_READY, &cache->flags))
716+
cachefiles_withdraw_cache(cache);
717717

718+
cachefiles_put_directory(cache->graveyard);
719+
cachefiles_put_directory(cache->store);
718720
mntput(cache->mnt);
719721

720722
kfree(cache->rootdirname);

fs/cachefiles/interface.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/* FS-Cache interface to CacheFiles
3+
*
4+
* Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5+
* Written by David Howells ([email protected])
6+
*/
7+
8+
#include <linux/slab.h>
9+
#include <linux/mount.h>
10+
#include <linux/xattr.h>
11+
#include <linux/file.h>
12+
#include <linux/falloc.h>
13+
#include <trace/events/fscache.h>
14+
#include "internal.h"
15+
16+
const struct fscache_cache_ops cachefiles_cache_ops = {
17+
.name = "cachefiles",
18+
};

fs/cachefiles/internal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct cachefiles_object {
3232
struct cachefiles_cache {
3333
struct fscache_cache *cache; /* Cache cookie */
3434
struct vfsmount *mnt; /* mountpoint holding the cache */
35+
struct dentry *store; /* Directory into which live objects go */
36+
struct dentry *graveyard; /* directory into which dead objects go */
3537
struct file *cachefilesd; /* manager daemon handle */
3638
const struct cred *cache_cred; /* security override for accessing cache */
3739
struct mutex daemon_mutex; /* command serialisation mutex */
@@ -78,8 +80,10 @@ static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
7880
/*
7981
* cache.c
8082
*/
83+
extern int cachefiles_add_cache(struct cachefiles_cache *cache);
8184
extern int cachefiles_has_space(struct cachefiles_cache *cache,
8285
unsigned fnr, unsigned bnr);
86+
extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
8387

8488
/*
8589
* daemon.c
@@ -125,6 +129,11 @@ static inline int cachefiles_inject_remove_error(void)
125129
return cachefiles_error_injection_state & 2 ? -EIO : 0;
126130
}
127131

132+
/*
133+
* interface.c
134+
*/
135+
extern const struct fscache_cache_ops cachefiles_cache_ops;
136+
128137
/*
129138
* namei.c
130139
*/

0 commit comments

Comments
 (0)