1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2007 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 *);
45 static unsigned long cachefiles_open
;
47 const struct file_operations cachefiles_daemon_fops
= {
49 .open
= cachefiles_daemon_open
,
50 .release
= cachefiles_daemon_release
,
51 .read
= cachefiles_daemon_read
,
52 .write
= cachefiles_daemon_write
,
53 .poll
= cachefiles_daemon_poll
,
54 .llseek
= noop_llseek
,
57 struct cachefiles_daemon_cmd
{
59 int (*handler
)(struct cachefiles_cache
*cache
, char *args
);
62 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds
[] = {
63 { "bind", cachefiles_daemon_bind
},
64 { "brun", cachefiles_daemon_brun
},
65 { "bcull", cachefiles_daemon_bcull
},
66 { "bstop", cachefiles_daemon_bstop
},
67 { "cull", cachefiles_daemon_cull
},
68 { "debug", cachefiles_daemon_debug
},
69 { "dir", cachefiles_daemon_dir
},
70 { "frun", cachefiles_daemon_frun
},
71 { "fcull", cachefiles_daemon_fcull
},
72 { "fstop", cachefiles_daemon_fstop
},
73 { "inuse", cachefiles_daemon_inuse
},
74 { "secctx", cachefiles_daemon_secctx
},
75 { "tag", cachefiles_daemon_tag
},
83 static int cachefiles_daemon_open(struct inode
*inode
, struct file
*file
)
85 struct cachefiles_cache
*cache
;
89 /* only the superuser may do this */
90 if (!capable(CAP_SYS_ADMIN
))
93 /* the cachefiles device may only be open once at a time */
94 if (xchg(&cachefiles_open
, 1) == 1)
97 /* allocate a cache record */
98 cache
= kzalloc(sizeof(struct cachefiles_cache
), GFP_KERNEL
);
104 mutex_init(&cache
->daemon_mutex
);
105 cache
->active_nodes
= RB_ROOT
;
106 rwlock_init(&cache
->active_lock
);
107 init_waitqueue_head(&cache
->daemon_pollwq
);
109 /* set default caching limits
110 * - limit at 1% free space and/or free files
111 * - cull below 5% free space and/or free files
112 * - cease culling above 7% free space and/or free files
114 cache
->frun_percent
= 7;
115 cache
->fcull_percent
= 5;
116 cache
->fstop_percent
= 1;
117 cache
->brun_percent
= 7;
118 cache
->bcull_percent
= 5;
119 cache
->bstop_percent
= 1;
121 file
->private_data
= cache
;
122 cache
->cachefilesd
= file
;
129 static int cachefiles_daemon_release(struct inode
*inode
, struct file
*file
)
131 struct cachefiles_cache
*cache
= file
->private_data
;
137 set_bit(CACHEFILES_DEAD
, &cache
->flags
);
139 cachefiles_daemon_unbind(cache
);
141 ASSERT(!cache
->active_nodes
.rb_node
);
143 /* clean up the control file interface */
144 cache
->cachefilesd
= NULL
;
145 file
->private_data
= NULL
;
155 * read the cache state
157 static ssize_t
cachefiles_daemon_read(struct file
*file
, char __user
*_buffer
,
158 size_t buflen
, loff_t
*pos
)
160 struct cachefiles_cache
*cache
= file
->private_data
;
161 unsigned long long b_released
;
166 //_enter(",,%zu,", buflen);
168 if (!test_bit(CACHEFILES_READY
, &cache
->flags
))
171 /* check how much space the cache has */
172 cachefiles_has_space(cache
, 0, 0);
175 f_released
= atomic_xchg(&cache
->f_released
, 0);
176 b_released
= atomic_long_xchg(&cache
->b_released
, 0);
177 clear_bit(CACHEFILES_STATE_CHANGED
, &cache
->flags
);
179 n
= snprintf(buffer
, sizeof(buffer
),
189 test_bit(CACHEFILES_CULLING
, &cache
->flags
) ? '1' : '0',
190 (unsigned long long) cache
->frun
,
191 (unsigned long long) cache
->fcull
,
192 (unsigned long long) cache
->fstop
,
193 (unsigned long long) cache
->brun
,
194 (unsigned long long) cache
->bcull
,
195 (unsigned long long) cache
->bstop
,
202 if (copy_to_user(_buffer
, buffer
, n
) != 0)
211 static ssize_t
cachefiles_daemon_write(struct file
*file
,
212 const char __user
*_data
,
216 const struct cachefiles_daemon_cmd
*cmd
;
217 struct cachefiles_cache
*cache
= file
->private_data
;
219 char *data
, *args
, *cp
;
221 //_enter(",,%zu,", datalen);
225 if (test_bit(CACHEFILES_DEAD
, &cache
->flags
))
228 if (datalen
< 0 || datalen
> PAGE_SIZE
- 1)
231 /* drag the command string into the kernel so we can parse it */
232 data
= memdup_user_nul(_data
, datalen
);
234 return PTR_ERR(data
);
237 if (memchr(data
, '\0', datalen
))
240 /* strip any newline */
241 cp
= memchr(data
, '\n', datalen
);
249 /* parse the command */
252 for (args
= data
; *args
; args
++)
259 args
= skip_spaces(++args
);
262 /* run the appropriate command handler */
263 for (cmd
= cachefiles_daemon_cmds
; cmd
->name
[0]; cmd
++)
264 if (strcmp(cmd
->name
, data
) == 0)
269 //_leave(" = %zd", ret);
273 mutex_lock(&cache
->daemon_mutex
);
276 if (!test_bit(CACHEFILES_DEAD
, &cache
->flags
))
277 ret
= cmd
->handler(cache
, args
);
279 mutex_unlock(&cache
->daemon_mutex
);
287 * poll for culling state
288 * - use EPOLLOUT to indicate culling state
290 static __poll_t
cachefiles_daemon_poll(struct file
*file
,
291 struct poll_table_struct
*poll
)
293 struct cachefiles_cache
*cache
= file
->private_data
;
296 poll_wait(file
, &cache
->daemon_pollwq
, poll
);
299 if (test_bit(CACHEFILES_STATE_CHANGED
, &cache
->flags
))
302 if (test_bit(CACHEFILES_CULLING
, &cache
->flags
))
309 * give a range error for cache space constraints
310 * - can be tail-called
312 static int cachefiles_daemon_range_error(struct cachefiles_cache
*cache
,
315 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
321 * set the percentage of files at which to stop culling
322 * - command: "frun <N>%"
324 static int cachefiles_daemon_frun(struct cachefiles_cache
*cache
, char *args
)
333 frun
= simple_strtoul(args
, &args
, 10);
334 if (args
[0] != '%' || args
[1] != '\0')
337 if (frun
<= cache
->fcull_percent
|| frun
>= 100)
338 return cachefiles_daemon_range_error(cache
, args
);
340 cache
->frun_percent
= frun
;
345 * set the percentage of files at which to start culling
346 * - command: "fcull <N>%"
348 static int cachefiles_daemon_fcull(struct cachefiles_cache
*cache
, char *args
)
357 fcull
= simple_strtoul(args
, &args
, 10);
358 if (args
[0] != '%' || args
[1] != '\0')
361 if (fcull
<= cache
->fstop_percent
|| fcull
>= cache
->frun_percent
)
362 return cachefiles_daemon_range_error(cache
, args
);
364 cache
->fcull_percent
= fcull
;
369 * set the percentage of files at which to stop allocating
370 * - command: "fstop <N>%"
372 static int cachefiles_daemon_fstop(struct cachefiles_cache
*cache
, char *args
)
381 fstop
= simple_strtoul(args
, &args
, 10);
382 if (args
[0] != '%' || args
[1] != '\0')
385 if (fstop
< 0 || fstop
>= cache
->fcull_percent
)
386 return cachefiles_daemon_range_error(cache
, args
);
388 cache
->fstop_percent
= fstop
;
393 * set the percentage of blocks at which to stop culling
394 * - command: "brun <N>%"
396 static int cachefiles_daemon_brun(struct cachefiles_cache
*cache
, char *args
)
405 brun
= simple_strtoul(args
, &args
, 10);
406 if (args
[0] != '%' || args
[1] != '\0')
409 if (brun
<= cache
->bcull_percent
|| brun
>= 100)
410 return cachefiles_daemon_range_error(cache
, args
);
412 cache
->brun_percent
= brun
;
417 * set the percentage of blocks at which to start culling
418 * - command: "bcull <N>%"
420 static int cachefiles_daemon_bcull(struct cachefiles_cache
*cache
, char *args
)
429 bcull
= simple_strtoul(args
, &args
, 10);
430 if (args
[0] != '%' || args
[1] != '\0')
433 if (bcull
<= cache
->bstop_percent
|| bcull
>= cache
->brun_percent
)
434 return cachefiles_daemon_range_error(cache
, args
);
436 cache
->bcull_percent
= bcull
;
441 * set the percentage of blocks at which to stop allocating
442 * - command: "bstop <N>%"
444 static int cachefiles_daemon_bstop(struct cachefiles_cache
*cache
, char *args
)
453 bstop
= simple_strtoul(args
, &args
, 10);
454 if (args
[0] != '%' || args
[1] != '\0')
457 if (bstop
< 0 || bstop
>= cache
->bcull_percent
)
458 return cachefiles_daemon_range_error(cache
, args
);
460 cache
->bstop_percent
= bstop
;
465 * set the cache directory
466 * - command: "dir <name>"
468 static int cachefiles_daemon_dir(struct cachefiles_cache
*cache
, char *args
)
475 pr_err("Empty directory specified\n");
479 if (cache
->rootdirname
) {
480 pr_err("Second cache directory specified\n");
484 dir
= kstrdup(args
, GFP_KERNEL
);
488 cache
->rootdirname
= dir
;
493 * set the cache security context
494 * - command: "secctx <ctx>"
496 static int cachefiles_daemon_secctx(struct cachefiles_cache
*cache
, char *args
)
503 pr_err("Empty security context specified\n");
508 pr_err("Second security context specified\n");
512 secctx
= kstrdup(args
, GFP_KERNEL
);
516 cache
->secctx
= secctx
;
522 * - command: "tag <name>"
524 static int cachefiles_daemon_tag(struct cachefiles_cache
*cache
, char *args
)
531 pr_err("Empty tag specified\n");
538 tag
= kstrdup(args
, GFP_KERNEL
);
547 * request a node in the cache be culled from the current working directory
548 * - command: "cull <name>"
550 static int cachefiles_daemon_cull(struct cachefiles_cache
*cache
, char *args
)
553 const struct cred
*saved_cred
;
558 if (strchr(args
, '/'))
561 if (!test_bit(CACHEFILES_READY
, &cache
->flags
)) {
562 pr_err("cull applied to unready cache\n");
566 if (test_bit(CACHEFILES_DEAD
, &cache
->flags
)) {
567 pr_err("cull applied to dead cache\n");
571 /* extract the directory dentry from the cwd */
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 /* extract the directory dentry from the cwd */
644 get_fs_pwd(current
->fs
, &path
);
646 if (!d_can_lookup(path
.dentry
))
649 cachefiles_begin_secure(cache
, &saved_cred
);
650 ret
= cachefiles_check_in_use(cache
, path
.dentry
, args
);
651 cachefiles_end_secure(cache
, saved_cred
);
654 //_leave(" = %d", ret);
659 pr_err("inuse command requires dirfd to be a directory\n");
663 pr_err("inuse command requires dirfd and filename\n");
668 * see if we have space for a number of pages and/or a number of files in the
671 int cachefiles_has_space(struct cachefiles_cache
*cache
,
672 unsigned fnr
, unsigned bnr
)
674 struct kstatfs stats
;
677 .dentry
= cache
->mnt
->mnt_root
,
681 //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
682 // (unsigned long long) cache->frun,
683 // (unsigned long long) cache->fcull,
684 // (unsigned long long) cache->fstop,
685 // (unsigned long long) cache->brun,
686 // (unsigned long long) cache->bcull,
687 // (unsigned long long) cache->bstop,
690 /* find out how many pages of blockdev are available */
691 memset(&stats
, 0, sizeof(stats
));
693 ret
= vfs_statfs(&path
, &stats
);
696 cachefiles_io_error(cache
, "statfs failed");
697 _leave(" = %d", ret
);
701 stats
.f_bavail
>>= cache
->bshift
;
703 //_debug("avail %llu,%llu",
704 // (unsigned long long) stats.f_ffree,
705 // (unsigned long long) stats.f_bavail);
707 /* see if there is sufficient space */
708 if (stats
.f_ffree
> fnr
)
709 stats
.f_ffree
-= fnr
;
713 if (stats
.f_bavail
> bnr
)
714 stats
.f_bavail
-= bnr
;
719 if (stats
.f_ffree
< cache
->fstop
||
720 stats
.f_bavail
< cache
->bstop
)
724 if (stats
.f_ffree
< cache
->fcull
||
725 stats
.f_bavail
< cache
->bcull
)
728 if (test_bit(CACHEFILES_CULLING
, &cache
->flags
) &&
729 stats
.f_ffree
>= cache
->frun
&&
730 stats
.f_bavail
>= cache
->brun
&&
731 test_and_clear_bit(CACHEFILES_CULLING
, &cache
->flags
)
733 _debug("cease culling");
734 cachefiles_state_changed(cache
);
741 if (!test_and_set_bit(CACHEFILES_CULLING
, &cache
->flags
)) {
742 _debug("### CULL CACHE ###");
743 cachefiles_state_changed(cache
);
746 _leave(" = %d", ret
);