1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
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 },
80 { "restore", cachefiles_ondemand_restore },
87 * Prepare a cache for caching.
89 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
91 struct cachefiles_cache *cache;
95 /* only the superuser may do this */
96 if (!capable(CAP_SYS_ADMIN))
99 /* the cachefiles device may only be open once at a time */
100 if (xchg(&cachefiles_open, 1) == 1)
103 /* allocate a cache record */
104 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
110 mutex_init(&cache->daemon_mutex);
111 init_waitqueue_head(&cache->daemon_pollwq);
112 INIT_LIST_HEAD(&cache->volumes);
113 INIT_LIST_HEAD(&cache->object_list);
114 spin_lock_init(&cache->object_list_lock);
115 refcount_set(&cache->unbind_pincount, 1);
116 xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
117 xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
119 /* set default caching limits
120 * - limit at 1% free space and/or free files
121 * - cull below 5% free space and/or free files
122 * - cease culling above 7% free space and/or free files
124 cache->frun_percent = 7;
125 cache->fcull_percent = 5;
126 cache->fstop_percent = 1;
127 cache->brun_percent = 7;
128 cache->bcull_percent = 5;
129 cache->bstop_percent = 1;
131 file->private_data = cache;
132 cache->cachefilesd = file;
136 static void cachefiles_flush_reqs(struct cachefiles_cache *cache)
138 struct xarray *xa = &cache->reqs;
139 struct cachefiles_req *req;
143 * Make sure the following two operations won't be reordered.
144 * 1) set CACHEFILES_DEAD bit
145 * 2) flush requests in the xarray
146 * Otherwise the request may be enqueued after xarray has been
147 * flushed, leaving the orphan request never being completed.
151 * flush requests in the xarray
152 * test CACHEFILES_DEAD bit
153 * enqueue the request
154 * set CACHEFILES_DEAD bit
159 xa_for_each(xa, index, req) {
161 complete(&req->done);
165 xa_destroy(&cache->reqs);
166 xa_destroy(&cache->ondemand_ids);
169 void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache)
171 if (refcount_dec_and_test(&cache->unbind_pincount)) {
172 cachefiles_daemon_unbind(cache);
178 void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache)
180 refcount_inc(&cache->unbind_pincount);
186 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
188 struct cachefiles_cache *cache = file->private_data;
194 set_bit(CACHEFILES_DEAD, &cache->flags);
196 if (cachefiles_in_ondemand_mode(cache))
197 cachefiles_flush_reqs(cache);
199 /* clean up the control file interface */
200 cache->cachefilesd = NULL;
201 file->private_data = NULL;
203 cachefiles_put_unbind_pincount(cache);
209 static ssize_t cachefiles_do_daemon_read(struct cachefiles_cache *cache,
210 char __user *_buffer, size_t buflen)
212 unsigned long long b_released;
217 /* check how much space the cache has */
218 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
221 f_released = atomic_xchg(&cache->f_released, 0);
222 b_released = atomic_long_xchg(&cache->b_released, 0);
223 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
225 n = snprintf(buffer, sizeof(buffer),
235 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
236 (unsigned long long) cache->frun,
237 (unsigned long long) cache->fcull,
238 (unsigned long long) cache->fstop,
239 (unsigned long long) cache->brun,
240 (unsigned long long) cache->bcull,
241 (unsigned long long) cache->bstop,
248 if (copy_to_user(_buffer, buffer, n) != 0)
255 * Read the cache state.
257 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
258 size_t buflen, loff_t *pos)
260 struct cachefiles_cache *cache = file->private_data;
262 //_enter(",,%zu,", buflen);
264 if (!test_bit(CACHEFILES_READY, &cache->flags))
267 if (cachefiles_in_ondemand_mode(cache))
268 return cachefiles_ondemand_daemon_read(cache, _buffer, buflen);
270 return cachefiles_do_daemon_read(cache, _buffer, buflen);
274 * Take a command from cachefilesd, parse it and act on it.
276 static ssize_t cachefiles_daemon_write(struct file *file,
277 const char __user *_data,
281 const struct cachefiles_daemon_cmd *cmd;
282 struct cachefiles_cache *cache = file->private_data;
284 char *data, *args, *cp;
286 //_enter(",,%zu,", datalen);
290 if (test_bit(CACHEFILES_DEAD, &cache->flags))
293 if (datalen > PAGE_SIZE - 1)
296 /* drag the command string into the kernel so we can parse it */
297 data = memdup_user_nul(_data, datalen);
299 return PTR_ERR(data);
302 if (memchr(data, '\0', datalen))
305 /* strip any newline */
306 cp = memchr(data, '\n', datalen);
314 /* parse the command */
317 for (args = data; *args; args++)
324 args = skip_spaces(++args);
327 /* run the appropriate command handler */
328 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
329 if (strcmp(cmd->name, data) == 0)
334 //_leave(" = %zd", ret);
338 mutex_lock(&cache->daemon_mutex);
341 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
342 ret = cmd->handler(cache, args);
344 mutex_unlock(&cache->daemon_mutex);
352 * Poll for culling state
353 * - use EPOLLOUT to indicate culling state
355 static __poll_t cachefiles_daemon_poll(struct file *file,
356 struct poll_table_struct *poll)
358 struct cachefiles_cache *cache = file->private_data;
359 XA_STATE(xas, &cache->reqs, 0);
360 struct cachefiles_req *req;
363 poll_wait(file, &cache->daemon_pollwq, poll);
366 if (cachefiles_in_ondemand_mode(cache)) {
367 if (!xa_empty(&cache->reqs)) {
369 xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
370 if (!cachefiles_ondemand_is_reopening_read(req)) {
378 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
382 if (test_bit(CACHEFILES_CULLING, &cache->flags))
389 * Give a range error for cache space constraints
390 * - can be tail-called
392 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
395 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
401 * Set the percentage of files at which to stop culling
402 * - command: "frun <N>%"
404 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
413 frun = simple_strtoul(args, &args, 10);
414 if (args[0] != '%' || args[1] != '\0')
417 if (frun <= cache->fcull_percent || frun >= 100)
418 return cachefiles_daemon_range_error(cache, args);
420 cache->frun_percent = frun;
425 * Set the percentage of files at which to start culling
426 * - command: "fcull <N>%"
428 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
437 fcull = simple_strtoul(args, &args, 10);
438 if (args[0] != '%' || args[1] != '\0')
441 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
442 return cachefiles_daemon_range_error(cache, args);
444 cache->fcull_percent = fcull;
449 * Set the percentage of files at which to stop allocating
450 * - command: "fstop <N>%"
452 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
461 fstop = simple_strtoul(args, &args, 10);
462 if (args[0] != '%' || args[1] != '\0')
465 if (fstop >= cache->fcull_percent)
466 return cachefiles_daemon_range_error(cache, args);
468 cache->fstop_percent = fstop;
473 * Set the percentage of blocks at which to stop culling
474 * - command: "brun <N>%"
476 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
485 brun = simple_strtoul(args, &args, 10);
486 if (args[0] != '%' || args[1] != '\0')
489 if (brun <= cache->bcull_percent || brun >= 100)
490 return cachefiles_daemon_range_error(cache, args);
492 cache->brun_percent = brun;
497 * Set the percentage of blocks at which to start culling
498 * - command: "bcull <N>%"
500 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
509 bcull = simple_strtoul(args, &args, 10);
510 if (args[0] != '%' || args[1] != '\0')
513 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
514 return cachefiles_daemon_range_error(cache, args);
516 cache->bcull_percent = bcull;
521 * Set the percentage of blocks at which to stop allocating
522 * - command: "bstop <N>%"
524 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
533 bstop = simple_strtoul(args, &args, 10);
534 if (args[0] != '%' || args[1] != '\0')
537 if (bstop >= cache->bcull_percent)
538 return cachefiles_daemon_range_error(cache, args);
540 cache->bstop_percent = bstop;
545 * Set the cache directory
546 * - command: "dir <name>"
548 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
555 pr_err("Empty directory specified\n");
559 if (cache->rootdirname) {
560 pr_err("Second cache directory specified\n");
564 dir = kstrdup(args, GFP_KERNEL);
568 cache->rootdirname = dir;
573 * Set the cache security context
574 * - command: "secctx <ctx>"
576 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
583 pr_err("Empty security context specified\n");
588 pr_err("Second security context specified\n");
592 secctx = kstrdup(args, GFP_KERNEL);
596 cache->secctx = secctx;
602 * - command: "tag <name>"
604 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
611 pr_err("Empty tag specified\n");
618 tag = kstrdup(args, GFP_KERNEL);
627 * Request a node in the cache be culled from the current working directory
628 * - command: "cull <name>"
630 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
633 const struct cred *saved_cred;
638 if (strchr(args, '/'))
641 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
642 pr_err("cull applied to unready cache\n");
646 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
647 pr_err("cull applied to dead cache\n");
651 get_fs_pwd(current->fs, &path);
653 if (!d_can_lookup(path.dentry))
656 cachefiles_begin_secure(cache, &saved_cred);
657 ret = cachefiles_cull(cache, path.dentry, args);
658 cachefiles_end_secure(cache, saved_cred);
661 _leave(" = %d", ret);
666 pr_err("cull command requires dirfd to be a directory\n");
670 pr_err("cull command requires dirfd and filename\n");
676 * - command: "debug <mask>"
678 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
684 mask = simple_strtoul(args, &args, 0);
688 cachefiles_debug = mask;
693 pr_err("debug command requires mask\n");
698 * Find out whether an object in the current working directory is in use or not
699 * - command: "inuse <name>"
701 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
704 const struct cred *saved_cred;
707 //_enter(",%s", args);
709 if (strchr(args, '/'))
712 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
713 pr_err("inuse applied to unready cache\n");
717 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
718 pr_err("inuse applied to dead cache\n");
722 get_fs_pwd(current->fs, &path);
724 if (!d_can_lookup(path.dentry))
727 cachefiles_begin_secure(cache, &saved_cred);
728 ret = cachefiles_check_in_use(cache, path.dentry, args);
729 cachefiles_end_secure(cache, saved_cred);
732 //_leave(" = %d", ret);
737 pr_err("inuse command requires dirfd to be a directory\n");
741 pr_err("inuse command requires dirfd and filename\n");
746 * Bind a directory as a cache
748 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
750 _enter("{%u,%u,%u,%u,%u,%u},%s",
752 cache->fcull_percent,
753 cache->fstop_percent,
755 cache->bcull_percent,
756 cache->bstop_percent,
759 if (cache->fstop_percent >= cache->fcull_percent ||
760 cache->fcull_percent >= cache->frun_percent ||
761 cache->frun_percent >= 100)
764 if (cache->bstop_percent >= cache->bcull_percent ||
765 cache->bcull_percent >= cache->brun_percent ||
766 cache->brun_percent >= 100)
769 if (!cache->rootdirname) {
770 pr_err("No cache directory specified\n");
774 /* Don't permit already bound caches to be re-bound */
775 if (test_bit(CACHEFILES_READY, &cache->flags)) {
776 pr_err("Cache already bound\n");
780 if (IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND)) {
781 if (!strcmp(args, "ondemand")) {
782 set_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
784 pr_err("Invalid argument to the 'bind' command\n");
788 pr_err("'bind' command doesn't take an argument\n");
792 /* Make sure we have copies of the tag string */
795 * The tag string is released by the fops->release()
796 * function, so we don't release it on error here
798 cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
803 return cachefiles_add_cache(cache);
809 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
813 if (test_bit(CACHEFILES_READY, &cache->flags))
814 cachefiles_withdraw_cache(cache);
816 cachefiles_put_directory(cache->graveyard);
817 cachefiles_put_directory(cache->store);
819 put_cred(cache->cache_cred);
821 kfree(cache->rootdirname);
822 kfree(cache->secctx);