blob: d463d89f5db8cc55d9c69af81a77ae2bc735aa05 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells9ae326a2009-04-03 16:42:41 +01002/* Bind and unbind a cache from the filesystem backing it
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells9ae326a2009-04-03 16:42:41 +01006 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/sched.h>
11#include <linux/completion.h>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/file.h>
15#include <linux/namei.h>
16#include <linux/mount.h>
17#include <linux/statfs.h>
18#include <linux/ctype.h>
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020019#include <linux/xattr.h>
David Howells9ae326a2009-04-03 16:42:41 +010020#include "internal.h"
21
22static int cachefiles_daemon_add_cache(struct cachefiles_cache *caches);
23
24/*
25 * bind a directory as a cache
26 */
27int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
28{
29 _enter("{%u,%u,%u,%u,%u,%u},%s",
30 cache->frun_percent,
31 cache->fcull_percent,
32 cache->fstop_percent,
33 cache->brun_percent,
34 cache->bcull_percent,
35 cache->bstop_percent,
36 args);
37
38 /* start by checking things over */
39 ASSERT(cache->fstop_percent >= 0 &&
40 cache->fstop_percent < cache->fcull_percent &&
41 cache->fcull_percent < cache->frun_percent &&
42 cache->frun_percent < 100);
43
44 ASSERT(cache->bstop_percent >= 0 &&
45 cache->bstop_percent < cache->bcull_percent &&
46 cache->bcull_percent < cache->brun_percent &&
47 cache->brun_percent < 100);
48
49 if (*args) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -070050 pr_err("'bind' command doesn't take an argument\n");
David Howells9ae326a2009-04-03 16:42:41 +010051 return -EINVAL;
52 }
53
54 if (!cache->rootdirname) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -070055 pr_err("No cache directory specified\n");
David Howells9ae326a2009-04-03 16:42:41 +010056 return -EINVAL;
57 }
58
59 /* don't permit already bound caches to be re-bound */
60 if (test_bit(CACHEFILES_READY, &cache->flags)) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -070061 pr_err("Cache already bound\n");
David Howells9ae326a2009-04-03 16:42:41 +010062 return -EBUSY;
63 }
64
65 /* make sure we have copies of the tag and dirname strings */
66 if (!cache->tag) {
67 /* the tag string is released by the fops->release()
68 * function, so we don't release it on error here */
69 cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
70 if (!cache->tag)
71 return -ENOMEM;
72 }
73
74 /* add the cache */
75 return cachefiles_daemon_add_cache(cache);
76}
77
78/*
79 * add a cache
80 */
81static int cachefiles_daemon_add_cache(struct cachefiles_cache *cache)
82{
83 struct cachefiles_object *fsdef;
Al Virob0446be2009-08-09 02:03:00 +040084 struct path path;
David Howells9ae326a2009-04-03 16:42:41 +010085 struct kstatfs stats;
86 struct dentry *graveyard, *cachedir, *root;
87 const struct cred *saved_cred;
88 int ret;
89
90 _enter("");
91
92 /* we want to work under the module's security ID */
93 ret = cachefiles_get_security_ID(cache);
94 if (ret < 0)
95 return ret;
96
97 cachefiles_begin_secure(cache, &saved_cred);
98
99 /* allocate the root index object */
100 ret = -ENOMEM;
101
102 fsdef = kmem_cache_alloc(cachefiles_object_jar, GFP_KERNEL);
103 if (!fsdef)
104 goto error_root_object;
105
106 ASSERTCMP(fsdef->backer, ==, NULL);
107
108 atomic_set(&fsdef->usage, 1);
109 fsdef->type = FSCACHE_COOKIE_TYPE_INDEX;
110
David Howells9ae326a2009-04-03 16:42:41 +0100111 /* look up the directory at the root of the cache */
Al Virob0446be2009-08-09 02:03:00 +0400112 ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
David Howells9ae326a2009-04-03 16:42:41 +0100113 if (ret < 0)
114 goto error_open_root;
115
Al Virob0446be2009-08-09 02:03:00 +0400116 cache->mnt = path.mnt;
117 root = path.dentry;
David Howells9ae326a2009-04-03 16:42:41 +0100118
Christian Braunerbf1c82a2021-03-24 08:51:10 +0000119 ret = -EINVAL;
120 if (mnt_user_ns(path.mnt) != &init_user_ns) {
121 pr_warn("File cache on idmapped mounts not supported");
122 goto error_unsupported;
123 }
124
David Howells9ae326a2009-04-03 16:42:41 +0100125 /* check parameters */
126 ret = -EOPNOTSUPP;
David Howells466b77b2015-03-17 22:26:21 +0000127 if (d_is_negative(root) ||
128 !d_backing_inode(root)->i_op->lookup ||
129 !d_backing_inode(root)->i_op->mkdir ||
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200130 !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
David Howells9ae326a2009-04-03 16:42:41 +0100131 !root->d_sb->s_op->statfs ||
132 !root->d_sb->s_op->sync_fs)
133 goto error_unsupported;
134
135 ret = -EROFS;
David Howellsbc98a422017-07-17 08:45:34 +0100136 if (sb_rdonly(root->d_sb))
David Howells9ae326a2009-04-03 16:42:41 +0100137 goto error_unsupported;
138
139 /* determine the security of the on-disk cache as this governs
140 * security ID of files we create */
141 ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
142 if (ret < 0)
143 goto error_unsupported;
144
145 /* get the cache size and blocksize */
Christoph Hellwigebabe9a2010-07-07 18:53:11 +0200146 ret = vfs_statfs(&path, &stats);
David Howells9ae326a2009-04-03 16:42:41 +0100147 if (ret < 0)
148 goto error_unsupported;
149
150 ret = -ERANGE;
151 if (stats.f_bsize <= 0)
152 goto error_unsupported;
153
154 ret = -EOPNOTSUPP;
155 if (stats.f_bsize > PAGE_SIZE)
156 goto error_unsupported;
157
158 cache->bsize = stats.f_bsize;
159 cache->bshift = 0;
160 if (stats.f_bsize < PAGE_SIZE)
161 cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
162
163 _debug("blksize %u (shift %u)",
164 cache->bsize, cache->bshift);
165
166 _debug("size %llu, avail %llu",
167 (unsigned long long) stats.f_blocks,
168 (unsigned long long) stats.f_bavail);
169
170 /* set up caching limits */
171 do_div(stats.f_files, 100);
172 cache->fstop = stats.f_files * cache->fstop_percent;
173 cache->fcull = stats.f_files * cache->fcull_percent;
174 cache->frun = stats.f_files * cache->frun_percent;
175
176 _debug("limits {%llu,%llu,%llu} files",
177 (unsigned long long) cache->frun,
178 (unsigned long long) cache->fcull,
179 (unsigned long long) cache->fstop);
180
181 stats.f_blocks >>= cache->bshift;
182 do_div(stats.f_blocks, 100);
183 cache->bstop = stats.f_blocks * cache->bstop_percent;
184 cache->bcull = stats.f_blocks * cache->bcull_percent;
185 cache->brun = stats.f_blocks * cache->brun_percent;
186
187 _debug("limits {%llu,%llu,%llu} blocks",
188 (unsigned long long) cache->brun,
189 (unsigned long long) cache->bcull,
190 (unsigned long long) cache->bstop);
191
192 /* get the cache directory and check its type */
193 cachedir = cachefiles_get_directory(cache, root, "cache");
194 if (IS_ERR(cachedir)) {
195 ret = PTR_ERR(cachedir);
196 goto error_unsupported;
197 }
198
199 fsdef->dentry = cachedir;
200 fsdef->fscache.cookie = NULL;
201
202 ret = cachefiles_check_object_type(fsdef);
203 if (ret < 0)
204 goto error_unsupported;
205
206 /* get the graveyard directory */
207 graveyard = cachefiles_get_directory(cache, root, "graveyard");
208 if (IS_ERR(graveyard)) {
209 ret = PTR_ERR(graveyard);
210 goto error_unsupported;
211 }
212
213 cache->graveyard = graveyard;
214
215 /* publish the cache */
216 fscache_init_cache(&cache->cache,
217 &cachefiles_cache_ops,
218 "%s",
219 fsdef->dentry->d_sb->s_id);
220
Kiran Kumar Modukurif29507c2018-06-21 13:31:44 -0700221 fscache_object_init(&fsdef->fscache, &fscache_fsdef_index,
222 &cache->cache);
David Howells9ae326a2009-04-03 16:42:41 +0100223
224 ret = fscache_add_cache(&cache->cache, &fsdef->fscache, cache->tag);
225 if (ret < 0)
226 goto error_add_cache;
227
228 /* done */
229 set_bit(CACHEFILES_READY, &cache->flags);
230 dput(root);
231
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700232 pr_info("File cache on %s registered\n", cache->cache.identifier);
David Howells9ae326a2009-04-03 16:42:41 +0100233
234 /* check how much space the cache has */
235 cachefiles_has_space(cache, 0, 0);
236 cachefiles_end_secure(cache, saved_cred);
237 return 0;
238
239error_add_cache:
240 dput(cache->graveyard);
241 cache->graveyard = NULL;
242error_unsupported:
243 mntput(cache->mnt);
244 cache->mnt = NULL;
245 dput(fsdef->dentry);
246 fsdef->dentry = NULL;
247 dput(root);
248error_open_root:
249 kmem_cache_free(cachefiles_object_jar, fsdef);
250error_root_object:
251 cachefiles_end_secure(cache, saved_cred);
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700252 pr_err("Failed to register: %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100253 return ret;
254}
255
256/*
257 * unbind a cache on fd release
258 */
259void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
260{
261 _enter("");
262
263 if (test_bit(CACHEFILES_READY, &cache->flags)) {
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700264 pr_info("File cache on %s unregistering\n",
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700265 cache->cache.identifier);
David Howells9ae326a2009-04-03 16:42:41 +0100266
267 fscache_withdraw_cache(&cache->cache);
268 }
269
270 dput(cache->graveyard);
271 mntput(cache->mnt);
272
273 kfree(cache->rootdirname);
274 kfree(cache->secctx);
275 kfree(cache->tag);
276
277 _leave("");
278}