blob: fbb08e97438d36eb0bc783386bf68d1521fb9a81 [file] [log] [blame]
David Howells9ae326a2009-04-03 16:42:41 +01001/* Bind and unbind a cache from the filesystem backing it
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/completion.h>
16#include <linux/slab.h>
17#include <linux/fs.h>
18#include <linux/file.h>
19#include <linux/namei.h>
20#include <linux/mount.h>
21#include <linux/statfs.h>
22#include <linux/ctype.h>
23#include "internal.h"
24
25static int cachefiles_daemon_add_cache(struct cachefiles_cache *caches);
26
27/*
28 * bind a directory as a cache
29 */
30int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
31{
32 _enter("{%u,%u,%u,%u,%u,%u},%s",
33 cache->frun_percent,
34 cache->fcull_percent,
35 cache->fstop_percent,
36 cache->brun_percent,
37 cache->bcull_percent,
38 cache->bstop_percent,
39 args);
40
41 /* start by checking things over */
42 ASSERT(cache->fstop_percent >= 0 &&
43 cache->fstop_percent < cache->fcull_percent &&
44 cache->fcull_percent < cache->frun_percent &&
45 cache->frun_percent < 100);
46
47 ASSERT(cache->bstop_percent >= 0 &&
48 cache->bstop_percent < cache->bcull_percent &&
49 cache->bcull_percent < cache->brun_percent &&
50 cache->brun_percent < 100);
51
52 if (*args) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -070053 pr_err("'bind' command doesn't take an argument\n");
David Howells9ae326a2009-04-03 16:42:41 +010054 return -EINVAL;
55 }
56
57 if (!cache->rootdirname) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -070058 pr_err("No cache directory specified\n");
David Howells9ae326a2009-04-03 16:42:41 +010059 return -EINVAL;
60 }
61
62 /* don't permit already bound caches to be re-bound */
63 if (test_bit(CACHEFILES_READY, &cache->flags)) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -070064 pr_err("Cache already bound\n");
David Howells9ae326a2009-04-03 16:42:41 +010065 return -EBUSY;
66 }
67
68 /* make sure we have copies of the tag and dirname strings */
69 if (!cache->tag) {
70 /* the tag string is released by the fops->release()
71 * function, so we don't release it on error here */
72 cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
73 if (!cache->tag)
74 return -ENOMEM;
75 }
76
77 /* add the cache */
78 return cachefiles_daemon_add_cache(cache);
79}
80
81/*
82 * add a cache
83 */
84static int cachefiles_daemon_add_cache(struct cachefiles_cache *cache)
85{
86 struct cachefiles_object *fsdef;
Al Virob0446be2009-08-09 02:03:00 +040087 struct path path;
David Howells9ae326a2009-04-03 16:42:41 +010088 struct kstatfs stats;
89 struct dentry *graveyard, *cachedir, *root;
90 const struct cred *saved_cred;
91 int ret;
92
93 _enter("");
94
95 /* we want to work under the module's security ID */
96 ret = cachefiles_get_security_ID(cache);
97 if (ret < 0)
98 return ret;
99
100 cachefiles_begin_secure(cache, &saved_cred);
101
102 /* allocate the root index object */
103 ret = -ENOMEM;
104
105 fsdef = kmem_cache_alloc(cachefiles_object_jar, GFP_KERNEL);
106 if (!fsdef)
107 goto error_root_object;
108
109 ASSERTCMP(fsdef->backer, ==, NULL);
110
111 atomic_set(&fsdef->usage, 1);
112 fsdef->type = FSCACHE_COOKIE_TYPE_INDEX;
113
114 _debug("- fsdef %p", fsdef);
115
116 /* look up the directory at the root of the cache */
Al Virob0446be2009-08-09 02:03:00 +0400117 ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
David Howells9ae326a2009-04-03 16:42:41 +0100118 if (ret < 0)
119 goto error_open_root;
120
Al Virob0446be2009-08-09 02:03:00 +0400121 cache->mnt = path.mnt;
122 root = path.dentry;
David Howells9ae326a2009-04-03 16:42:41 +0100123
124 /* check parameters */
125 ret = -EOPNOTSUPP;
126 if (!root->d_inode ||
David Howells9ae326a2009-04-03 16:42:41 +0100127 !root->d_inode->i_op->lookup ||
128 !root->d_inode->i_op->mkdir ||
129 !root->d_inode->i_op->setxattr ||
130 !root->d_inode->i_op->getxattr ||
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;
136 if (root->d_sb->s_flags & MS_RDONLY)
137 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
221 fscache_object_init(&fsdef->fscache, NULL, &cache->cache);
222
223 ret = fscache_add_cache(&cache->cache, &fsdef->fscache, cache->tag);
224 if (ret < 0)
225 goto error_add_cache;
226
227 /* done */
228 set_bit(CACHEFILES_READY, &cache->flags);
229 dput(root);
230
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700231 pr_info("File cache on %s registered\n", cache->cache.identifier);
David Howells9ae326a2009-04-03 16:42:41 +0100232
233 /* check how much space the cache has */
234 cachefiles_has_space(cache, 0, 0);
235 cachefiles_end_secure(cache, saved_cred);
236 return 0;
237
238error_add_cache:
239 dput(cache->graveyard);
240 cache->graveyard = NULL;
241error_unsupported:
242 mntput(cache->mnt);
243 cache->mnt = NULL;
244 dput(fsdef->dentry);
245 fsdef->dentry = NULL;
246 dput(root);
247error_open_root:
248 kmem_cache_free(cachefiles_object_jar, fsdef);
249error_root_object:
250 cachefiles_end_secure(cache, saved_cred);
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700251 pr_err("Failed to register: %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100252 return ret;
253}
254
255/*
256 * unbind a cache on fd release
257 */
258void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
259{
260 _enter("");
261
262 if (test_bit(CACHEFILES_READY, &cache->flags)) {
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700263 pr_info("File cache on %s unregistering\n",
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700264 cache->cache.identifier);
David Howells9ae326a2009-04-03 16:42:41 +0100265
266 fscache_withdraw_cache(&cache->cache);
267 }
268
269 dput(cache->graveyard);
270 mntput(cache->mnt);
271
272 kfree(cache->rootdirname);
273 kfree(cache->secctx);
274 kfree(cache->tag);
275
276 _leave("");
277}