Blame view

fs/cachefiles/bind.c 6.94 KB
9ae326a69   David Howells   CacheFiles: A cac...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  /* Bind and unbind a cache from the filesystem backing it
   *
   * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public Licence
   * as published by the Free Software Foundation; either version
   * 2 of the Licence, or (at your option) any later version.
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/completion.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/file.h>
  #include <linux/namei.h>
  #include <linux/mount.h>
  #include <linux/statfs.h>
  #include <linux/ctype.h>
  #include "internal.h"
  
  static int cachefiles_daemon_add_cache(struct cachefiles_cache *caches);
  
  /*
   * bind a directory as a cache
   */
  int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
  {
  	_enter("{%u,%u,%u,%u,%u,%u},%s",
  	       cache->frun_percent,
  	       cache->fcull_percent,
  	       cache->fstop_percent,
  	       cache->brun_percent,
  	       cache->bcull_percent,
  	       cache->bstop_percent,
  	       args);
  
  	/* start by checking things over */
  	ASSERT(cache->fstop_percent >= 0 &&
  	       cache->fstop_percent < cache->fcull_percent &&
  	       cache->fcull_percent < cache->frun_percent &&
  	       cache->frun_percent  < 100);
  
  	ASSERT(cache->bstop_percent >= 0 &&
  	       cache->bstop_percent < cache->bcull_percent &&
  	       cache->bcull_percent < cache->brun_percent &&
  	       cache->brun_percent  < 100);
  
  	if (*args) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
53
54
  		pr_err("'bind' command doesn't take an argument
  ");
9ae326a69   David Howells   CacheFiles: A cac...
55
56
57
58
  		return -EINVAL;
  	}
  
  	if (!cache->rootdirname) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
59
60
  		pr_err("No cache directory specified
  ");
9ae326a69   David Howells   CacheFiles: A cac...
61
62
63
64
65
  		return -EINVAL;
  	}
  
  	/* don't permit already bound caches to be re-bound */
  	if (test_bit(CACHEFILES_READY, &cache->flags)) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
66
67
  		pr_err("Cache already bound
  ");
9ae326a69   David Howells   CacheFiles: A cac...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  		return -EBUSY;
  	}
  
  	/* make sure we have copies of the tag and dirname strings */
  	if (!cache->tag) {
  		/* the tag string is released by the fops->release()
  		 * function, so we don't release it on error here */
  		cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
  		if (!cache->tag)
  			return -ENOMEM;
  	}
  
  	/* add the cache */
  	return cachefiles_daemon_add_cache(cache);
  }
  
  /*
   * add a cache
   */
  static int cachefiles_daemon_add_cache(struct cachefiles_cache *cache)
  {
  	struct cachefiles_object *fsdef;
b0446be4b   Al Viro   switch cachefiles...
90
  	struct path path;
9ae326a69   David Howells   CacheFiles: A cac...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  	struct kstatfs stats;
  	struct dentry *graveyard, *cachedir, *root;
  	const struct cred *saved_cred;
  	int ret;
  
  	_enter("");
  
  	/* we want to work under the module's security ID */
  	ret = cachefiles_get_security_ID(cache);
  	if (ret < 0)
  		return ret;
  
  	cachefiles_begin_secure(cache, &saved_cred);
  
  	/* allocate the root index object */
  	ret = -ENOMEM;
  
  	fsdef = kmem_cache_alloc(cachefiles_object_jar, GFP_KERNEL);
  	if (!fsdef)
  		goto error_root_object;
  
  	ASSERTCMP(fsdef->backer, ==, NULL);
  
  	atomic_set(&fsdef->usage, 1);
  	fsdef->type = FSCACHE_COOKIE_TYPE_INDEX;
  
  	_debug("- fsdef %p", fsdef);
  
  	/* look up the directory at the root of the cache */
b0446be4b   Al Viro   switch cachefiles...
120
  	ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
9ae326a69   David Howells   CacheFiles: A cac...
121
122
  	if (ret < 0)
  		goto error_open_root;
b0446be4b   Al Viro   switch cachefiles...
123
124
  	cache->mnt = path.mnt;
  	root = path.dentry;
9ae326a69   David Howells   CacheFiles: A cac...
125
126
127
128
  
  	/* check parameters */
  	ret = -EOPNOTSUPP;
  	if (!root->d_inode ||
9ae326a69   David Howells   CacheFiles: A cac...
129
130
131
132
  	    !root->d_inode->i_op->lookup ||
  	    !root->d_inode->i_op->mkdir ||
  	    !root->d_inode->i_op->setxattr ||
  	    !root->d_inode->i_op->getxattr ||
9ae326a69   David Howells   CacheFiles: A cac...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  	    !root->d_sb->s_op->statfs ||
  	    !root->d_sb->s_op->sync_fs)
  		goto error_unsupported;
  
  	ret = -EROFS;
  	if (root->d_sb->s_flags & MS_RDONLY)
  		goto error_unsupported;
  
  	/* determine the security of the on-disk cache as this governs
  	 * security ID of files we create */
  	ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
  	if (ret < 0)
  		goto error_unsupported;
  
  	/* get the cache size and blocksize */
ebabe9a90   Christoph Hellwig   pass a struct pat...
148
  	ret = vfs_statfs(&path, &stats);
9ae326a69   David Howells   CacheFiles: A cac...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  	if (ret < 0)
  		goto error_unsupported;
  
  	ret = -ERANGE;
  	if (stats.f_bsize <= 0)
  		goto error_unsupported;
  
  	ret = -EOPNOTSUPP;
  	if (stats.f_bsize > PAGE_SIZE)
  		goto error_unsupported;
  
  	cache->bsize = stats.f_bsize;
  	cache->bshift = 0;
  	if (stats.f_bsize < PAGE_SIZE)
  		cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
  
  	_debug("blksize %u (shift %u)",
  	       cache->bsize, cache->bshift);
  
  	_debug("size %llu, avail %llu",
  	       (unsigned long long) stats.f_blocks,
  	       (unsigned long long) stats.f_bavail);
  
  	/* set up caching limits */
  	do_div(stats.f_files, 100);
  	cache->fstop = stats.f_files * cache->fstop_percent;
  	cache->fcull = stats.f_files * cache->fcull_percent;
  	cache->frun  = stats.f_files * cache->frun_percent;
  
  	_debug("limits {%llu,%llu,%llu} files",
  	       (unsigned long long) cache->frun,
  	       (unsigned long long) cache->fcull,
  	       (unsigned long long) cache->fstop);
  
  	stats.f_blocks >>= cache->bshift;
  	do_div(stats.f_blocks, 100);
  	cache->bstop = stats.f_blocks * cache->bstop_percent;
  	cache->bcull = stats.f_blocks * cache->bcull_percent;
  	cache->brun  = stats.f_blocks * cache->brun_percent;
  
  	_debug("limits {%llu,%llu,%llu} blocks",
  	       (unsigned long long) cache->brun,
  	       (unsigned long long) cache->bcull,
  	       (unsigned long long) cache->bstop);
  
  	/* get the cache directory and check its type */
  	cachedir = cachefiles_get_directory(cache, root, "cache");
  	if (IS_ERR(cachedir)) {
  		ret = PTR_ERR(cachedir);
  		goto error_unsupported;
  	}
  
  	fsdef->dentry = cachedir;
  	fsdef->fscache.cookie = NULL;
  
  	ret = cachefiles_check_object_type(fsdef);
  	if (ret < 0)
  		goto error_unsupported;
  
  	/* get the graveyard directory */
  	graveyard = cachefiles_get_directory(cache, root, "graveyard");
  	if (IS_ERR(graveyard)) {
  		ret = PTR_ERR(graveyard);
  		goto error_unsupported;
  	}
  
  	cache->graveyard = graveyard;
  
  	/* publish the cache */
  	fscache_init_cache(&cache->cache,
  			   &cachefiles_cache_ops,
  			   "%s",
  			   fsdef->dentry->d_sb->s_id);
  
  	fscache_object_init(&fsdef->fscache, NULL, &cache->cache);
  
  	ret = fscache_add_cache(&cache->cache, &fsdef->fscache, cache->tag);
  	if (ret < 0)
  		goto error_add_cache;
  
  	/* done */
  	set_bit(CACHEFILES_READY, &cache->flags);
  	dput(root);
0227d6abb   Fabian Frederick   fs/cachefiles: re...
232
233
  	pr_info("File cache on %s registered
  ", cache->cache.identifier);
9ae326a69   David Howells   CacheFiles: A cac...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  
  	/* check how much space the cache has */
  	cachefiles_has_space(cache, 0, 0);
  	cachefiles_end_secure(cache, saved_cred);
  	return 0;
  
  error_add_cache:
  	dput(cache->graveyard);
  	cache->graveyard = NULL;
  error_unsupported:
  	mntput(cache->mnt);
  	cache->mnt = NULL;
  	dput(fsdef->dentry);
  	fsdef->dentry = NULL;
  	dput(root);
  error_open_root:
  	kmem_cache_free(cachefiles_object_jar, fsdef);
  error_root_object:
  	cachefiles_end_secure(cache, saved_cred);
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
253
254
  	pr_err("Failed to register: %d
  ", ret);
9ae326a69   David Howells   CacheFiles: A cac...
255
256
257
258
259
260
261
262
263
264
265
  	return ret;
  }
  
  /*
   * unbind a cache on fd release
   */
  void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
  {
  	_enter("");
  
  	if (test_bit(CACHEFILES_READY, &cache->flags)) {
0227d6abb   Fabian Frederick   fs/cachefiles: re...
266
267
  		pr_info("File cache on %s unregistering
  ",
4e1eb8830   Fabian Frederick   FS/CACHEFILES: co...
268
  			cache->cache.identifier);
9ae326a69   David Howells   CacheFiles: A cac...
269
270
271
272
273
274
275
276
277
278
279
280
281
  
  		fscache_withdraw_cache(&cache->cache);
  	}
  
  	dput(cache->graveyard);
  	mntput(cache->mnt);
  
  	kfree(cache->rootdirname);
  	kfree(cache->secctx);
  	kfree(cache->tag);
  
  	_leave("");
  }