1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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>
14 #include <linux/file.h>
15 #include <linux/namei.h>
16 #include <linux/poll.h>
17 #include <linux/mount.h>
18 #include <linux/statfs.h>
19 #include <linux/ctype.h>
20 #include <linux/string.h>
21 #include <linux/fs_struct.h>
24 static int cachefiles_daemon_open(struct inode *, struct file *);
25 static int cachefiles_daemon_release(struct inode *, struct file *);
26 static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
28 static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
30 static __poll_t cachefiles_daemon_poll(struct file *,
31 struct poll_table_struct *);
32 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
33 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
34 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
35 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
36 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
37 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
38 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
39 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
40 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
41 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
42 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
43 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
44 static int cachefiles_daemon_bind(struct cachefiles_cache *, char *);
45 static void cachefiles_daemon_unbind(struct cachefiles_cache *);
47 static unsigned long cachefiles_open;
49 const struct file_operations cachefiles_daemon_fops = {
51 .open = cachefiles_daemon_open,
52 .release = cachefiles_daemon_release,
53 .read = cachefiles_daemon_read,
54 .write = cachefiles_daemon_write,
55 .poll = cachefiles_daemon_poll,
56 .llseek = noop_llseek,
59 struct cachefiles_daemon_cmd {
61 int (*handler)(struct cachefiles_cache *cache, char *args);
64 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
65 { "bind", cachefiles_daemon_bind },
66 { "brun", cachefiles_daemon_brun },
67 { "bcull", cachefiles_daemon_bcull },
68 { "bstop", cachefiles_daemon_bstop },
69 { "cull", cachefiles_daemon_cull },
70 { "debug", cachefiles_daemon_debug },
71 { "dir", cachefiles_daemon_dir },
72 { "frun", cachefiles_daemon_frun },
73 { "fcull", cachefiles_daemon_fcull },
74 { "fstop", cachefiles_daemon_fstop },
75 { "inuse", cachefiles_daemon_inuse },
76 { "secctx", cachefiles_daemon_secctx },
77 { "tag", cachefiles_daemon_tag },
83 * Prepare a cache for caching.
85 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
87 struct cachefiles_cache *cache;
91 /* only the superuser may do this */
92 if (!capable(CAP_SYS_ADMIN))
95 /* the cachefiles device may only be open once at a time */
96 if (xchg(&cachefiles_open, 1) == 1)
99 /* allocate a cache record */
100 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
106 mutex_init(&cache->daemon_mutex);
107 init_waitqueue_head(&cache->daemon_pollwq);
108 INIT_LIST_HEAD(&cache->volumes);
109 INIT_LIST_HEAD(&cache->object_list);
110 spin_lock_init(&cache->object_list_lock);
112 /* set default caching limits
113 * - limit at 1% free space and/or free files
114 * - cull below 5% free space and/or free files
115 * - cease culling above 7% free space and/or free files
117 cache->frun_percent = 7;
118 cache->fcull_percent = 5;
119 cache->fstop_percent = 1;
120 cache->brun_percent = 7;
121 cache->bcull_percent = 5;
122 cache->bstop_percent = 1;
124 file->private_data = cache;
125 cache->cachefilesd = file;
132 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
134 struct cachefiles_cache *cache = file->private_data;
140 set_bit(CACHEFILES_DEAD, &cache->flags);
142 cachefiles_daemon_unbind(cache);
144 /* clean up the control file interface */
145 cache->cachefilesd = NULL;
146 file->private_data = NULL;
156 * Read the cache state.
158 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
159 size_t buflen, loff_t *pos)
161 struct cachefiles_cache *cache = file->private_data;
162 unsigned long long b_released;
167 //_enter(",,%zu,", buflen);
169 if (!test_bit(CACHEFILES_READY, &cache->flags))
172 /* check how much space the cache has */
173 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
176 f_released = atomic_xchg(&cache->f_released, 0);
177 b_released = atomic_long_xchg(&cache->b_released, 0);
178 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
180 n = snprintf(buffer, sizeof(buffer),
190 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
191 (unsigned long long) cache->frun,
192 (unsigned long long) cache->fcull,
193 (unsigned long long) cache->fstop,
194 (unsigned long long) cache->brun,
195 (unsigned long long) cache->bcull,
196 (unsigned long long) cache->bstop,
203 if (copy_to_user(_buffer, buffer, n) != 0)
210 * Take a command from cachefilesd, parse it and act on it.
212 static ssize_t cachefiles_daemon_write(struct file *file,
213 const char __user *_data,
217 const struct cachefiles_daemon_cmd *cmd;
218 struct cachefiles_cache *cache = file->private_data;
220 char *data, *args, *cp;
222 //_enter(",,%zu,", datalen);
226 if (test_bit(CACHEFILES_DEAD, &cache->flags))
229 if (datalen > PAGE_SIZE - 1)
232 /* drag the command string into the kernel so we can parse it */
233 data = memdup_user_nul(_data, datalen);
235 return PTR_ERR(data);
238 if (memchr(data, '\0', datalen))
241 /* strip any newline */
242 cp = memchr(data, '\n', datalen);
250 /* parse the command */
253 for (args = data; *args; args++)
260 args = skip_spaces(++args);
263 /* run the appropriate command handler */
264 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
265 if (strcmp(cmd->name, data) == 0)
270 //_leave(" = %zd", ret);
274 mutex_lock(&cache->daemon_mutex);
277 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
278 ret = cmd->handler(cache, args);
280 mutex_unlock(&cache->daemon_mutex);
288 * Poll for culling state
289 * - use EPOLLOUT to indicate culling state
291 static __poll_t cachefiles_daemon_poll(struct file *file,
292 struct poll_table_struct *poll)
294 struct cachefiles_cache *cache = file->private_data;
297 poll_wait(file, &cache->daemon_pollwq, poll);
300 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
303 if (test_bit(CACHEFILES_CULLING, &cache->flags))
310 * Give a range error for cache space constraints
311 * - can be tail-called
313 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
316 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
322 * Set the percentage of files at which to stop culling
323 * - command: "frun <N>%"
325 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
334 frun = simple_strtoul(args, &args, 10);
335 if (args[0] != '%' || args[1] != '\0')
338 if (frun <= cache->fcull_percent || frun >= 100)
339 return cachefiles_daemon_range_error(cache, args);
341 cache->frun_percent = frun;
346 * Set the percentage of files at which to start culling
347 * - command: "fcull <N>%"
349 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
358 fcull = simple_strtoul(args, &args, 10);
359 if (args[0] != '%' || args[1] != '\0')
362 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
363 return cachefiles_daemon_range_error(cache, args);
365 cache->fcull_percent = fcull;
370 * Set the percentage of files at which to stop allocating
371 * - command: "fstop <N>%"
373 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
382 fstop = simple_strtoul(args, &args, 10);
383 if (args[0] != '%' || args[1] != '\0')
386 if (fstop >= cache->fcull_percent)
387 return cachefiles_daemon_range_error(cache, args);
389 cache->fstop_percent = fstop;
394 * Set the percentage of blocks at which to stop culling
395 * - command: "brun <N>%"
397 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
406 brun = simple_strtoul(args, &args, 10);
407 if (args[0] != '%' || args[1] != '\0')
410 if (brun <= cache->bcull_percent || brun >= 100)
411 return cachefiles_daemon_range_error(cache, args);
413 cache->brun_percent = brun;
418 * Set the percentage of blocks at which to start culling
419 * - command: "bcull <N>%"
421 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
430 bcull = simple_strtoul(args, &args, 10);
431 if (args[0] != '%' || args[1] != '\0')
434 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
435 return cachefiles_daemon_range_error(cache, args);
437 cache->bcull_percent = bcull;
442 * Set the percentage of blocks at which to stop allocating
443 * - command: "bstop <N>%"
445 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
454 bstop = simple_strtoul(args, &args, 10);
455 if (args[0] != '%' || args[1] != '\0')
458 if (bstop >= cache->bcull_percent)
459 return cachefiles_daemon_range_error(cache, args);
461 cache->bstop_percent = bstop;
466 * Set the cache directory
467 * - command: "dir <name>"
469 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
476 pr_err("Empty directory specified\n");
480 if (cache->rootdirname) {
481 pr_err("Second cache directory specified\n");
485 dir = kstrdup(args, GFP_KERNEL);
489 cache->rootdirname = dir;
494 * Set the cache security context
495 * - command: "secctx <ctx>"
497 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
504 pr_err("Empty security context specified\n");
509 pr_err("Second security context specified\n");
513 secctx = kstrdup(args, GFP_KERNEL);
517 cache->secctx = secctx;
523 * - command: "tag <name>"
525 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
532 pr_err("Empty tag specified\n");
539 tag = kstrdup(args, GFP_KERNEL);
548 * Request a node in the cache be culled from the current working directory
549 * - command: "cull <name>"
551 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
554 const struct cred *saved_cred;
559 if (strchr(args, '/'))
562 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
563 pr_err("cull applied to unready cache\n");
567 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
568 pr_err("cull applied to dead cache\n");
572 get_fs_pwd(current->fs, &path);
574 if (!d_can_lookup(path.dentry))
577 cachefiles_begin_secure(cache, &saved_cred);
578 ret = cachefiles_cull(cache, path.dentry, args);
579 cachefiles_end_secure(cache, saved_cred);
582 _leave(" = %d", ret);
587 pr_err("cull command requires dirfd to be a directory\n");
591 pr_err("cull command requires dirfd and filename\n");
597 * - command: "debug <mask>"
599 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
605 mask = simple_strtoul(args, &args, 0);
609 cachefiles_debug = mask;
614 pr_err("debug command requires mask\n");
619 * Find out whether an object in the current working directory is in use or not
620 * - command: "inuse <name>"
622 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
625 const struct cred *saved_cred;
628 //_enter(",%s", args);
630 if (strchr(args, '/'))
633 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
634 pr_err("inuse applied to unready cache\n");
638 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
639 pr_err("inuse applied to dead cache\n");
643 get_fs_pwd(current->fs, &path);
645 if (!d_can_lookup(path.dentry))
648 cachefiles_begin_secure(cache, &saved_cred);
649 ret = cachefiles_check_in_use(cache, path.dentry, args);
650 cachefiles_end_secure(cache, saved_cred);
653 //_leave(" = %d", ret);
658 pr_err("inuse command requires dirfd to be a directory\n");
662 pr_err("inuse command requires dirfd and filename\n");
667 * Bind a directory as a cache
669 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
671 _enter("{%u,%u,%u,%u,%u,%u},%s",
673 cache->fcull_percent,
674 cache->fstop_percent,
676 cache->bcull_percent,
677 cache->bstop_percent,
680 if (cache->fstop_percent >= cache->fcull_percent ||
681 cache->fcull_percent >= cache->frun_percent ||
682 cache->frun_percent >= 100)
685 if (cache->bstop_percent >= cache->bcull_percent ||
686 cache->bcull_percent >= cache->brun_percent ||
687 cache->brun_percent >= 100)
691 pr_err("'bind' command doesn't take an argument\n");
695 if (!cache->rootdirname) {
696 pr_err("No cache directory specified\n");
700 /* Don't permit already bound caches to be re-bound */
701 if (test_bit(CACHEFILES_READY, &cache->flags)) {
702 pr_err("Cache already bound\n");
706 return cachefiles_add_cache(cache);
712 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
716 if (test_bit(CACHEFILES_READY, &cache->flags))
717 cachefiles_withdraw_cache(cache);
719 cachefiles_put_directory(cache->graveyard);
720 cachefiles_put_directory(cache->store);
723 kfree(cache->rootdirname);
724 kfree(cache->secctx);