| 1 | |
| 2 | |
| 3 | *
|
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
| 5 | * Written by David Howells ([email protected])
|
| 6 |
|
| 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>
|
| 19 | #include <linux/xattr.h>
|
| 20 | #include "internal.h"
|
| 21 |
|
| 22 | static int cachefiles_daemon_add_cache(struct cachefiles_cache *caches);
|
| 23 |
|
| 24 | |
| 25 | * bind a directory as a cache
|
| 26 |
|
| 27 | int 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 |
|
| 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) {
|
| 50 | pr_err("'bind' command doesn't take an argument\n");
|
| 51 | return -EINVAL;
|
| 52 | }
|
| 53 |
|
| 54 | if (!cache->rootdirname) {
|
| 55 | pr_err("No cache directory specified\n");
|
| 56 | return -EINVAL;
|
| 57 | }
|
| 58 |
|
| 59 |
|
| 60 | if (test_bit(CACHEFILES_READY, &cache->flags)) {
|
| 61 | pr_err("Cache already bound\n");
|
| 62 | return -EBUSY;
|
| 63 | }
|
| 64 |
|
| 65 |
|
| 66 | if (!cache->tag) {
|
| 67 | |
| 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 |
|
| 75 | return cachefiles_daemon_add_cache(cache);
|
| 76 | }
|
| 77 |
|
| 78 | |
| 79 | * add a cache
|
| 80 |
|
| 81 | static int cachefiles_daemon_add_cache(struct cachefiles_cache *cache)
|
| 82 | {
|
| 83 | struct cachefiles_object *fsdef;
|
| 84 | struct path path;
|
| 85 | struct kstatfs stats;
|
| 86 | struct dentry *graveyard, *cachedir, *root;
|
| 87 | const struct cred *saved_cred;
|
| 88 | int ret;
|
| 89 |
|
| 90 | _enter("");
|
| 91 |
|
| 92 |
|
| 93 | ret = cachefiles_get_security_ID(cache);
|
| 94 | if (ret < 0)
|
| 95 | return ret;
|
| 96 |
|
| 97 | cachefiles_begin_secure(cache, &saved_cred);
|
| 98 |
|
| 99 |
|
| 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 |
|
| 111 | _debug("- fsdef %p", fsdef);
|
| 112 |
|
| 113 |
|
| 114 | ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
|
| 115 | if (ret < 0)
|
| 116 | goto error_open_root;
|
| 117 |
|
| 118 | cache->mnt = path.mnt;
|
| 119 | root = path.dentry;
|
| 120 |
|
| 121 | ret = -EINVAL;
|
| 122 | if (mnt_user_ns(path.mnt) != &init_user_ns) {
|
| 123 | pr_warn("File cache on idmapped mounts not supported");
|
| 124 | goto error_unsupported;
|
| 125 | }
|
| 126 |
|
| 127 |
|
| 128 | ret = -EOPNOTSUPP;
|
| 129 | if (d_is_negative(root) ||
|
| 130 | !d_backing_inode(root)->i_op->lookup ||
|
| 131 | !d_backing_inode(root)->i_op->mkdir ||
|
| 132 | !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
|
| 133 | !root->d_sb->s_op->statfs ||
|
| 134 | !root->d_sb->s_op->sync_fs)
|
| 135 | goto error_unsupported;
|
| 136 |
|
| 137 | ret = -EROFS;
|
| 138 | if (sb_rdonly(root->d_sb))
|
| 139 | goto error_unsupported;
|
| 140 |
|
| 141 | |
| 142 | * security ID of files we create
|
| 143 | ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
|
| 144 | if (ret < 0)
|
| 145 | goto error_unsupported;
|
| 146 |
|
| 147 |
|
| 148 | ret = vfs_statfs(&path, &stats);
|
| 149 | if (ret < 0)
|
| 150 | goto error_unsupported;
|
| 151 |
|
| 152 | ret = -ERANGE;
|
| 153 | if (stats.f_bsize <= 0)
|
| 154 | goto error_unsupported;
|
| 155 |
|
| 156 | ret = -EOPNOTSUPP;
|
| 157 | if (stats.f_bsize > PAGE_SIZE)
|
| 158 | goto error_unsupported;
|
| 159 |
|
| 160 | cache->bsize = stats.f_bsize;
|
| 161 | cache->bshift = 0;
|
| 162 | if (stats.f_bsize < PAGE_SIZE)
|
| 163 | cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
|
| 164 |
|
| 165 | _debug("blksize %u (shift %u)",
|
| 166 | cache->bsize, cache->bshift);
|
| 167 |
|
| 168 | _debug("size %llu, avail %llu",
|
| 169 | (unsigned long long) stats.f_blocks,
|
| 170 | (unsigned long long) stats.f_bavail);
|
| 171 |
|
| 172 |
|
| 173 | do_div(stats.f_files, 100);
|
| 174 | cache->fstop = stats.f_files * cache->fstop_percent;
|
| 175 | cache->fcull = stats.f_files * cache->fcull_percent;
|
| 176 | cache->frun = stats.f_files * cache->frun_percent;
|
| 177 |
|
| 178 | _debug("limits {%llu,%llu,%llu} files",
|
| 179 | (unsigned long long) cache->frun,
|
| 180 | (unsigned long long) cache->fcull,
|
| 181 | (unsigned long long) cache->fstop);
|
| 182 |
|
| 183 | stats.f_blocks >>= cache->bshift;
|
| 184 | do_div(stats.f_blocks, 100);
|
| 185 | cache->bstop = stats.f_blocks * cache->bstop_percent;
|
| 186 | cache->bcull = stats.f_blocks * cache->bcull_percent;
|
| 187 | cache->brun = stats.f_blocks * cache->brun_percent;
|
| 188 |
|
| 189 | _debug("limits {%llu,%llu,%llu} blocks",
|
| 190 | (unsigned long long) cache->brun,
|
| 191 | (unsigned long long) cache->bcull,
|
| 192 | (unsigned long long) cache->bstop);
|
| 193 |
|
| 194 |
|
| 195 | cachedir = cachefiles_get_directory(cache, root, "cache");
|
| 196 | if (IS_ERR(cachedir)) {
|
| 197 | ret = PTR_ERR(cachedir);
|
| 198 | goto error_unsupported;
|
| 199 | }
|
| 200 |
|
| 201 | fsdef->dentry = cachedir;
|
| 202 | fsdef->fscache.cookie = NULL;
|
| 203 |
|
| 204 | ret = cachefiles_check_object_type(fsdef);
|
| 205 | if (ret < 0)
|
| 206 | goto error_unsupported;
|
| 207 |
|
| 208 |
|
| 209 | graveyard = cachefiles_get_directory(cache, root, "graveyard");
|
| 210 | if (IS_ERR(graveyard)) {
|
| 211 | ret = PTR_ERR(graveyard);
|
| 212 | goto error_unsupported;
|
| 213 | }
|
| 214 |
|
| 215 | cache->graveyard = graveyard;
|
| 216 |
|
| 217 |
|
| 218 | fscache_init_cache(&cache->cache,
|
| 219 | &cachefiles_cache_ops,
|
| 220 | "%s",
|
| 221 | fsdef->dentry->d_sb->s_id);
|
| 222 |
|
| 223 | fscache_object_init(&fsdef->fscache, &fscache_fsdef_index,
|
| 224 | &cache->cache);
|
| 225 |
|
| 226 | ret = fscache_add_cache(&cache->cache, &fsdef->fscache, cache->tag);
|
| 227 | if (ret < 0)
|
| 228 | goto error_add_cache;
|
| 229 |
|
| 230 |
|
| 231 | set_bit(CACHEFILES_READY, &cache->flags);
|
| 232 | dput(root);
|
| 233 |
|
| 234 | pr_info("File cache on %s registered\n", cache->cache.identifier);
|
| 235 |
|
| 236 |
|
| 237 | cachefiles_has_space(cache, 0, 0);
|
| 238 | cachefiles_end_secure(cache, saved_cred);
|
| 239 | return 0;
|
| 240 |
|
| 241 | error_add_cache:
|
| 242 | dput(cache->graveyard);
|
| 243 | cache->graveyard = NULL;
|
| 244 | error_unsupported:
|
| 245 | mntput(cache->mnt);
|
| 246 | cache->mnt = NULL;
|
| 247 | dput(fsdef->dentry);
|
| 248 | fsdef->dentry = NULL;
|
| 249 | dput(root);
|
| 250 | error_open_root:
|
| 251 | kmem_cache_free(cachefiles_object_jar, fsdef);
|
| 252 | error_root_object:
|
| 253 | cachefiles_end_secure(cache, saved_cred);
|
| 254 | pr_err("Failed to register: %d\n", ret);
|
| 255 | return ret;
|
| 256 | }
|
| 257 |
|
| 258 | |
| 259 | * unbind a cache on fd release
|
| 260 |
|
| 261 | void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
|
| 262 | {
|
| 263 | _enter("");
|
| 264 |
|
| 265 | if (test_bit(CACHEFILES_READY, &cache->flags)) {
|
| 266 | pr_info("File cache on %s unregistering\n",
|
| 267 | cache->cache.identifier);
|
| 268 |
|
| 269 | fscache_withdraw_cache(&cache->cache);
|
| 270 | }
|
| 271 |
|
| 272 | dput(cache->graveyard);
|
| 273 | mntput(cache->mnt);
|
| 274 |
|
| 275 | kfree(cache->rootdirname);
|
| 276 | kfree(cache->secctx);
|
| 277 | kfree(cache->tag);
|
| 278 |
|
| 279 | _leave("");
|
| 280 | }
|