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/security.h>
19 #include <linux/statfs.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/fs_struct.h>
25 static int cachefiles_daemon_open(struct inode *, struct file *);
26 static int cachefiles_daemon_release(struct inode *, struct file *);
27 static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
29 static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
31 static __poll_t cachefiles_daemon_poll(struct file *,
32 struct poll_table_struct *);
33 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
34 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
35 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
36 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
37 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
38 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
39 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
40 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
41 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
42 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
43 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
44 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
45 static int cachefiles_daemon_bind(struct cachefiles_cache *, char *);
46 static void cachefiles_daemon_unbind(struct cachefiles_cache *);
48 static unsigned long cachefiles_open;
50 const struct file_operations cachefiles_daemon_fops = {
52 .open = cachefiles_daemon_open,
53 .release = cachefiles_daemon_release,
54 .read = cachefiles_daemon_read,
55 .write = cachefiles_daemon_write,
56 .poll = cachefiles_daemon_poll,
57 .llseek = noop_llseek,
60 struct cachefiles_daemon_cmd {
62 int (*handler)(struct cachefiles_cache *cache, char *args);
65 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
66 { "bind", cachefiles_daemon_bind },
67 { "brun", cachefiles_daemon_brun },
68 { "bcull", cachefiles_daemon_bcull },
69 { "bstop", cachefiles_daemon_bstop },
70 { "cull", cachefiles_daemon_cull },
71 { "debug", cachefiles_daemon_debug },
72 { "dir", cachefiles_daemon_dir },
73 { "frun", cachefiles_daemon_frun },
74 { "fcull", cachefiles_daemon_fcull },
75 { "fstop", cachefiles_daemon_fstop },
76 { "inuse", cachefiles_daemon_inuse },
77 { "secctx", cachefiles_daemon_secctx },
78 { "tag", cachefiles_daemon_tag },
79 #ifdef CONFIG_CACHEFILES_ONDEMAND
80 { "copen", cachefiles_ondemand_copen },
81 { "restore", cachefiles_ondemand_restore },
88 * Prepare a cache for caching.
90 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
92 struct cachefiles_cache *cache;
96 /* only the superuser may do this */
97 if (!capable(CAP_SYS_ADMIN))
100 /* the cachefiles device may only be open once at a time */
101 if (xchg(&cachefiles_open, 1) == 1)
104 /* allocate a cache record */
105 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
111 mutex_init(&cache->daemon_mutex);
112 init_waitqueue_head(&cache->daemon_pollwq);
113 INIT_LIST_HEAD(&cache->volumes);
114 INIT_LIST_HEAD(&cache->object_list);
115 spin_lock_init(&cache->object_list_lock);
116 refcount_set(&cache->unbind_pincount, 1);
117 xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
118 xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
120 /* set default caching limits
121 * - limit at 1% free space and/or free files
122 * - cull below 5% free space and/or free files
123 * - cease culling above 7% free space and/or free files
125 cache->frun_percent = 7;
126 cache->fcull_percent = 5;
127 cache->fstop_percent = 1;
128 cache->brun_percent = 7;
129 cache->bcull_percent = 5;
130 cache->bstop_percent = 1;
132 file->private_data = cache;
133 cache->cachefilesd = file;
137 void cachefiles_flush_reqs(struct cachefiles_cache *cache)
139 struct xarray *xa = &cache->reqs;
140 struct cachefiles_req *req;
144 * Make sure the following two operations won't be reordered.
145 * 1) set CACHEFILES_DEAD bit
146 * 2) flush requests in the xarray
147 * Otherwise the request may be enqueued after xarray has been
148 * flushed, leaving the orphan request never being completed.
152 * flush requests in the xarray
153 * test CACHEFILES_DEAD bit
154 * enqueue the request
155 * set CACHEFILES_DEAD bit
160 xa_for_each(xa, index, req) {
162 complete(&req->done);
163 __xa_erase(xa, index);
167 xa_destroy(&cache->reqs);
168 xa_destroy(&cache->ondemand_ids);
171 void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache)
173 if (refcount_dec_and_test(&cache->unbind_pincount)) {
174 cachefiles_daemon_unbind(cache);
180 void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache)
182 refcount_inc(&cache->unbind_pincount);
188 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
190 struct cachefiles_cache *cache = file->private_data;
196 set_bit(CACHEFILES_DEAD, &cache->flags);
198 if (cachefiles_in_ondemand_mode(cache))
199 cachefiles_flush_reqs(cache);
201 /* clean up the control file interface */
202 cache->cachefilesd = NULL;
203 file->private_data = NULL;
205 cachefiles_put_unbind_pincount(cache);
211 static ssize_t cachefiles_do_daemon_read(struct cachefiles_cache *cache,
212 char __user *_buffer, size_t buflen)
214 unsigned long long b_released;
219 /* check how much space the cache has */
220 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
223 f_released = atomic_xchg(&cache->f_released, 0);
224 b_released = atomic_long_xchg(&cache->b_released, 0);
225 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
227 n = snprintf(buffer, sizeof(buffer),
237 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
238 (unsigned long long) cache->frun,
239 (unsigned long long) cache->fcull,
240 (unsigned long long) cache->fstop,
241 (unsigned long long) cache->brun,
242 (unsigned long long) cache->bcull,
243 (unsigned long long) cache->bstop,
250 if (copy_to_user(_buffer, buffer, n) != 0)
257 * Read the cache state.
259 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
260 size_t buflen, loff_t *pos)
262 struct cachefiles_cache *cache = file->private_data;
264 //_enter(",,%zu,", buflen);
266 if (!test_bit(CACHEFILES_READY, &cache->flags))
269 if (cachefiles_in_ondemand_mode(cache))
270 return cachefiles_ondemand_daemon_read(cache, _buffer, buflen);
272 return cachefiles_do_daemon_read(cache, _buffer, buflen);
276 * Take a command from cachefilesd, parse it and act on it.
278 static ssize_t cachefiles_daemon_write(struct file *file,
279 const char __user *_data,
283 const struct cachefiles_daemon_cmd *cmd;
284 struct cachefiles_cache *cache = file->private_data;
286 char *data, *args, *cp;
288 //_enter(",,%zu,", datalen);
292 if (test_bit(CACHEFILES_DEAD, &cache->flags))
295 if (datalen > PAGE_SIZE - 1)
298 /* drag the command string into the kernel so we can parse it */
299 data = memdup_user_nul(_data, datalen);
301 return PTR_ERR(data);
304 if (memchr(data, '\0', datalen))
307 /* strip any newline */
308 cp = memchr(data, '\n', datalen);
316 /* parse the command */
319 for (args = data; *args; args++)
326 args = skip_spaces(++args);
329 /* run the appropriate command handler */
330 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
331 if (strcmp(cmd->name, data) == 0)
336 //_leave(" = %zd", ret);
340 mutex_lock(&cache->daemon_mutex);
343 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
344 ret = cmd->handler(cache, args);
346 mutex_unlock(&cache->daemon_mutex);
354 * Poll for culling state
355 * - use EPOLLOUT to indicate culling state
357 static __poll_t cachefiles_daemon_poll(struct file *file,
358 struct poll_table_struct *poll)
360 struct cachefiles_cache *cache = file->private_data;
361 XA_STATE(xas, &cache->reqs, 0);
362 struct cachefiles_req *req;
365 poll_wait(file, &cache->daemon_pollwq, poll);
368 if (cachefiles_in_ondemand_mode(cache)) {
369 if (!xa_empty(&cache->reqs)) {
371 xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
372 if (!cachefiles_ondemand_is_reopening_read(req)) {
380 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
384 if (test_bit(CACHEFILES_CULLING, &cache->flags))
391 * Give a range error for cache space constraints
392 * - can be tail-called
394 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
397 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
403 * Set the percentage of files at which to stop culling
404 * - command: "frun <N>%"
406 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
415 frun = simple_strtoul(args, &args, 10);
416 if (args[0] != '%' || args[1] != '\0')
419 if (frun <= cache->fcull_percent || frun >= 100)
420 return cachefiles_daemon_range_error(cache, args);
422 cache->frun_percent = frun;
427 * Set the percentage of files at which to start culling
428 * - command: "fcull <N>%"
430 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
439 fcull = simple_strtoul(args, &args, 10);
440 if (args[0] != '%' || args[1] != '\0')
443 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
444 return cachefiles_daemon_range_error(cache, args);
446 cache->fcull_percent = fcull;
451 * Set the percentage of files at which to stop allocating
452 * - command: "fstop <N>%"
454 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
463 fstop = simple_strtoul(args, &args, 10);
464 if (args[0] != '%' || args[1] != '\0')
467 if (fstop >= cache->fcull_percent)
468 return cachefiles_daemon_range_error(cache, args);
470 cache->fstop_percent = fstop;
475 * Set the percentage of blocks at which to stop culling
476 * - command: "brun <N>%"
478 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
487 brun = simple_strtoul(args, &args, 10);
488 if (args[0] != '%' || args[1] != '\0')
491 if (brun <= cache->bcull_percent || brun >= 100)
492 return cachefiles_daemon_range_error(cache, args);
494 cache->brun_percent = brun;
499 * Set the percentage of blocks at which to start culling
500 * - command: "bcull <N>%"
502 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
511 bcull = simple_strtoul(args, &args, 10);
512 if (args[0] != '%' || args[1] != '\0')
515 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
516 return cachefiles_daemon_range_error(cache, args);
518 cache->bcull_percent = bcull;
523 * Set the percentage of blocks at which to stop allocating
524 * - command: "bstop <N>%"
526 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
535 bstop = simple_strtoul(args, &args, 10);
536 if (args[0] != '%' || args[1] != '\0')
539 if (bstop >= cache->bcull_percent)
540 return cachefiles_daemon_range_error(cache, args);
542 cache->bstop_percent = bstop;
547 * Set the cache directory
548 * - command: "dir <name>"
550 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
557 pr_err("Empty directory specified\n");
561 if (cache->rootdirname) {
562 pr_err("Second cache directory specified\n");
566 dir = kstrdup(args, GFP_KERNEL);
570 cache->rootdirname = dir;
575 * Set the cache security context
576 * - command: "secctx <ctx>"
578 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
585 pr_err("Empty security context specified\n");
589 if (cache->have_secid) {
590 pr_err("Second security context specified\n");
594 err = security_secctx_to_secid(args, strlen(args), &cache->secid);
598 cache->have_secid = true;
604 * - command: "tag <name>"
606 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
613 pr_err("Empty tag specified\n");
620 tag = kstrdup(args, GFP_KERNEL);
629 * Request a node in the cache be culled from the current working directory
630 * - command: "cull <name>"
632 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
635 const struct cred *saved_cred;
640 if (strchr(args, '/'))
643 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
644 pr_err("cull applied to unready cache\n");
648 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
649 pr_err("cull applied to dead cache\n");
653 get_fs_pwd(current->fs, &path);
655 if (!d_can_lookup(path.dentry))
658 cachefiles_begin_secure(cache, &saved_cred);
659 ret = cachefiles_cull(cache, path.dentry, args);
660 cachefiles_end_secure(cache, saved_cred);
663 _leave(" = %d", ret);
668 pr_err("cull command requires dirfd to be a directory\n");
672 pr_err("cull command requires dirfd and filename\n");
678 * - command: "debug <mask>"
680 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
686 mask = simple_strtoul(args, &args, 0);
690 cachefiles_debug = mask;
695 pr_err("debug command requires mask\n");
700 * Find out whether an object in the current working directory is in use or not
701 * - command: "inuse <name>"
703 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
706 const struct cred *saved_cred;
709 //_enter(",%s", args);
711 if (strchr(args, '/'))
714 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
715 pr_err("inuse applied to unready cache\n");
719 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
720 pr_err("inuse applied to dead cache\n");
724 get_fs_pwd(current->fs, &path);
726 if (!d_can_lookup(path.dentry))
729 cachefiles_begin_secure(cache, &saved_cred);
730 ret = cachefiles_check_in_use(cache, path.dentry, args);
731 cachefiles_end_secure(cache, saved_cred);
734 //_leave(" = %d", ret);
739 pr_err("inuse command requires dirfd to be a directory\n");
743 pr_err("inuse command requires dirfd and filename\n");
748 * Bind a directory as a cache
750 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
752 _enter("{%u,%u,%u,%u,%u,%u},%s",
754 cache->fcull_percent,
755 cache->fstop_percent,
757 cache->bcull_percent,
758 cache->bstop_percent,
761 if (cache->fstop_percent >= cache->fcull_percent ||
762 cache->fcull_percent >= cache->frun_percent ||
763 cache->frun_percent >= 100)
766 if (cache->bstop_percent >= cache->bcull_percent ||
767 cache->bcull_percent >= cache->brun_percent ||
768 cache->brun_percent >= 100)
771 if (!cache->rootdirname) {
772 pr_err("No cache directory specified\n");
776 /* Don't permit already bound caches to be re-bound */
777 if (test_bit(CACHEFILES_READY, &cache->flags)) {
778 pr_err("Cache already bound\n");
782 if (IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND)) {
783 if (!strcmp(args, "ondemand")) {
784 set_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
786 pr_err("Invalid argument to the 'bind' command\n");
790 pr_err("'bind' command doesn't take an argument\n");
794 /* Make sure we have copies of the tag string */
797 * The tag string is released by the fops->release()
798 * function, so we don't release it on error here
800 cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
805 return cachefiles_add_cache(cache);
811 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
815 if (test_bit(CACHEFILES_READY, &cache->flags))
816 cachefiles_withdraw_cache(cache);
818 cachefiles_put_directory(cache->graveyard);
819 cachefiles_put_directory(cache->store);
821 put_cred(cache->cache_cred);
823 kfree(cache->rootdirname);