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 },
78 #ifdef CONFIG_CACHEFILES_ONDEMAND
79 { "copen", cachefiles_ondemand_copen },
86 * Prepare a cache for caching.
88 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
90 struct cachefiles_cache *cache;
94 /* only the superuser may do this */
95 if (!capable(CAP_SYS_ADMIN))
98 /* the cachefiles device may only be open once at a time */
99 if (xchg(&cachefiles_open, 1) == 1)
102 /* allocate a cache record */
103 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
109 mutex_init(&cache->daemon_mutex);
110 init_waitqueue_head(&cache->daemon_pollwq);
111 INIT_LIST_HEAD(&cache->volumes);
112 INIT_LIST_HEAD(&cache->object_list);
113 spin_lock_init(&cache->object_list_lock);
114 refcount_set(&cache->unbind_pincount, 1);
115 xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
116 xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
118 /* set default caching limits
119 * - limit at 1% free space and/or free files
120 * - cull below 5% free space and/or free files
121 * - cease culling above 7% free space and/or free files
123 cache->frun_percent = 7;
124 cache->fcull_percent = 5;
125 cache->fstop_percent = 1;
126 cache->brun_percent = 7;
127 cache->bcull_percent = 5;
128 cache->bstop_percent = 1;
130 file->private_data = cache;
131 cache->cachefilesd = file;
135 static void cachefiles_flush_reqs(struct cachefiles_cache *cache)
137 struct xarray *xa = &cache->reqs;
138 struct cachefiles_req *req;
142 * Make sure the following two operations won't be reordered.
143 * 1) set CACHEFILES_DEAD bit
144 * 2) flush requests in the xarray
145 * Otherwise the request may be enqueued after xarray has been
146 * flushed, leaving the orphan request never being completed.
150 * flush requests in the xarray
151 * test CACHEFILES_DEAD bit
152 * enqueue the request
153 * set CACHEFILES_DEAD bit
158 xa_for_each(xa, index, req) {
160 complete(&req->done);
164 xa_destroy(&cache->reqs);
165 xa_destroy(&cache->ondemand_ids);
168 void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache)
170 if (refcount_dec_and_test(&cache->unbind_pincount)) {
171 cachefiles_daemon_unbind(cache);
177 void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache)
179 refcount_inc(&cache->unbind_pincount);
185 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
187 struct cachefiles_cache *cache = file->private_data;
193 set_bit(CACHEFILES_DEAD, &cache->flags);
195 if (cachefiles_in_ondemand_mode(cache))
196 cachefiles_flush_reqs(cache);
198 /* clean up the control file interface */
199 cache->cachefilesd = NULL;
200 file->private_data = NULL;
202 cachefiles_put_unbind_pincount(cache);
208 static ssize_t cachefiles_do_daemon_read(struct cachefiles_cache *cache,
209 char __user *_buffer, size_t buflen)
211 unsigned long long b_released;
216 /* check how much space the cache has */
217 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
220 f_released = atomic_xchg(&cache->f_released, 0);
221 b_released = atomic_long_xchg(&cache->b_released, 0);
222 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
224 n = snprintf(buffer, sizeof(buffer),
234 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
235 (unsigned long long) cache->frun,
236 (unsigned long long) cache->fcull,
237 (unsigned long long) cache->fstop,
238 (unsigned long long) cache->brun,
239 (unsigned long long) cache->bcull,
240 (unsigned long long) cache->bstop,
247 if (copy_to_user(_buffer, buffer, n) != 0)
254 * Read the cache state.
256 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
257 size_t buflen, loff_t *pos)
259 struct cachefiles_cache *cache = file->private_data;
261 //_enter(",,%zu,", buflen);
263 if (!test_bit(CACHEFILES_READY, &cache->flags))
266 if (cachefiles_in_ondemand_mode(cache))
267 return cachefiles_ondemand_daemon_read(cache, _buffer, buflen);
269 return cachefiles_do_daemon_read(cache, _buffer, buflen);
273 * Take a command from cachefilesd, parse it and act on it.
275 static ssize_t cachefiles_daemon_write(struct file *file,
276 const char __user *_data,
280 const struct cachefiles_daemon_cmd *cmd;
281 struct cachefiles_cache *cache = file->private_data;
283 char *data, *args, *cp;
285 //_enter(",,%zu,", datalen);
289 if (test_bit(CACHEFILES_DEAD, &cache->flags))
292 if (datalen > PAGE_SIZE - 1)
295 /* drag the command string into the kernel so we can parse it */
296 data = memdup_user_nul(_data, datalen);
298 return PTR_ERR(data);
301 if (memchr(data, '\0', datalen))
304 /* strip any newline */
305 cp = memchr(data, '\n', datalen);
313 /* parse the command */
316 for (args = data; *args; args++)
323 args = skip_spaces(++args);
326 /* run the appropriate command handler */
327 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
328 if (strcmp(cmd->name, data) == 0)
333 //_leave(" = %zd", ret);
337 mutex_lock(&cache->daemon_mutex);
340 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
341 ret = cmd->handler(cache, args);
343 mutex_unlock(&cache->daemon_mutex);
351 * Poll for culling state
352 * - use EPOLLOUT to indicate culling state
354 static __poll_t cachefiles_daemon_poll(struct file *file,
355 struct poll_table_struct *poll)
357 struct cachefiles_cache *cache = file->private_data;
360 poll_wait(file, &cache->daemon_pollwq, poll);
363 if (cachefiles_in_ondemand_mode(cache)) {
364 if (!xa_empty(&cache->reqs))
367 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
371 if (test_bit(CACHEFILES_CULLING, &cache->flags))
378 * Give a range error for cache space constraints
379 * - can be tail-called
381 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
384 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
390 * Set the percentage of files at which to stop culling
391 * - command: "frun <N>%"
393 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
402 frun = simple_strtoul(args, &args, 10);
403 if (args[0] != '%' || args[1] != '\0')
406 if (frun <= cache->fcull_percent || frun >= 100)
407 return cachefiles_daemon_range_error(cache, args);
409 cache->frun_percent = frun;
414 * Set the percentage of files at which to start culling
415 * - command: "fcull <N>%"
417 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
426 fcull = simple_strtoul(args, &args, 10);
427 if (args[0] != '%' || args[1] != '\0')
430 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
431 return cachefiles_daemon_range_error(cache, args);
433 cache->fcull_percent = fcull;
438 * Set the percentage of files at which to stop allocating
439 * - command: "fstop <N>%"
441 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
450 fstop = simple_strtoul(args, &args, 10);
451 if (args[0] != '%' || args[1] != '\0')
454 if (fstop >= cache->fcull_percent)
455 return cachefiles_daemon_range_error(cache, args);
457 cache->fstop_percent = fstop;
462 * Set the percentage of blocks at which to stop culling
463 * - command: "brun <N>%"
465 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
474 brun = simple_strtoul(args, &args, 10);
475 if (args[0] != '%' || args[1] != '\0')
478 if (brun <= cache->bcull_percent || brun >= 100)
479 return cachefiles_daemon_range_error(cache, args);
481 cache->brun_percent = brun;
486 * Set the percentage of blocks at which to start culling
487 * - command: "bcull <N>%"
489 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
498 bcull = simple_strtoul(args, &args, 10);
499 if (args[0] != '%' || args[1] != '\0')
502 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
503 return cachefiles_daemon_range_error(cache, args);
505 cache->bcull_percent = bcull;
510 * Set the percentage of blocks at which to stop allocating
511 * - command: "bstop <N>%"
513 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
522 bstop = simple_strtoul(args, &args, 10);
523 if (args[0] != '%' || args[1] != '\0')
526 if (bstop >= cache->bcull_percent)
527 return cachefiles_daemon_range_error(cache, args);
529 cache->bstop_percent = bstop;
534 * Set the cache directory
535 * - command: "dir <name>"
537 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
544 pr_err("Empty directory specified\n");
548 if (cache->rootdirname) {
549 pr_err("Second cache directory specified\n");
553 dir = kstrdup(args, GFP_KERNEL);
557 cache->rootdirname = dir;
562 * Set the cache security context
563 * - command: "secctx <ctx>"
565 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
572 pr_err("Empty security context specified\n");
577 pr_err("Second security context specified\n");
581 secctx = kstrdup(args, GFP_KERNEL);
585 cache->secctx = secctx;
591 * - command: "tag <name>"
593 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
600 pr_err("Empty tag specified\n");
607 tag = kstrdup(args, GFP_KERNEL);
616 * Request a node in the cache be culled from the current working directory
617 * - command: "cull <name>"
619 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
622 const struct cred *saved_cred;
627 if (strchr(args, '/'))
630 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
631 pr_err("cull applied to unready cache\n");
635 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
636 pr_err("cull applied to dead cache\n");
640 get_fs_pwd(current->fs, &path);
642 if (!d_can_lookup(path.dentry))
645 cachefiles_begin_secure(cache, &saved_cred);
646 ret = cachefiles_cull(cache, path.dentry, args);
647 cachefiles_end_secure(cache, saved_cred);
650 _leave(" = %d", ret);
655 pr_err("cull command requires dirfd to be a directory\n");
659 pr_err("cull command requires dirfd and filename\n");
665 * - command: "debug <mask>"
667 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
673 mask = simple_strtoul(args, &args, 0);
677 cachefiles_debug = mask;
682 pr_err("debug command requires mask\n");
687 * Find out whether an object in the current working directory is in use or not
688 * - command: "inuse <name>"
690 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
693 const struct cred *saved_cred;
696 //_enter(",%s", args);
698 if (strchr(args, '/'))
701 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
702 pr_err("inuse applied to unready cache\n");
706 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
707 pr_err("inuse applied to dead cache\n");
711 get_fs_pwd(current->fs, &path);
713 if (!d_can_lookup(path.dentry))
716 cachefiles_begin_secure(cache, &saved_cred);
717 ret = cachefiles_check_in_use(cache, path.dentry, args);
718 cachefiles_end_secure(cache, saved_cred);
721 //_leave(" = %d", ret);
726 pr_err("inuse command requires dirfd to be a directory\n");
730 pr_err("inuse command requires dirfd and filename\n");
735 * Bind a directory as a cache
737 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
739 _enter("{%u,%u,%u,%u,%u,%u},%s",
741 cache->fcull_percent,
742 cache->fstop_percent,
744 cache->bcull_percent,
745 cache->bstop_percent,
748 if (cache->fstop_percent >= cache->fcull_percent ||
749 cache->fcull_percent >= cache->frun_percent ||
750 cache->frun_percent >= 100)
753 if (cache->bstop_percent >= cache->bcull_percent ||
754 cache->bcull_percent >= cache->brun_percent ||
755 cache->brun_percent >= 100)
758 if (!cache->rootdirname) {
759 pr_err("No cache directory specified\n");
763 /* Don't permit already bound caches to be re-bound */
764 if (test_bit(CACHEFILES_READY, &cache->flags)) {
765 pr_err("Cache already bound\n");
769 if (IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND)) {
770 if (!strcmp(args, "ondemand")) {
771 set_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
773 pr_err("Invalid argument to the 'bind' command\n");
777 pr_err("'bind' command doesn't take an argument\n");
781 /* Make sure we have copies of the tag string */
784 * The tag string is released by the fops->release()
785 * function, so we don't release it on error here
787 cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
792 return cachefiles_add_cache(cache);
798 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
802 if (test_bit(CACHEFILES_READY, &cache->flags))
803 cachefiles_withdraw_cache(cache);
805 cachefiles_put_directory(cache->graveyard);
806 cachefiles_put_directory(cache->store);
809 kfree(cache->rootdirname);
810 kfree(cache->secctx);