12 #include <linux/module.h>
14 #include <linux/sched.h>
16 #include <linux/slab.h>
20 #include <linux/poll.h>
23 #include <linux/ctype.h>
24 #include <linux/string.h>
28 static int cachefiles_daemon_open(
struct inode *,
struct file *);
29 static int cachefiles_daemon_release(
struct inode *,
struct file *);
30 static ssize_t cachefiles_daemon_read(
struct file *,
char __user *,
size_t,
32 static ssize_t cachefiles_daemon_write(
struct file *,
const char __user *,
34 static unsigned int cachefiles_daemon_poll(
struct file *,
49 static unsigned long cachefiles_open;
53 .open = cachefiles_daemon_open,
54 .release = cachefiles_daemon_release,
55 .read = cachefiles_daemon_read,
56 .write = cachefiles_daemon_write,
57 .poll = cachefiles_daemon_poll,
68 {
"brun", cachefiles_daemon_brun },
69 {
"bcull", cachefiles_daemon_bcull },
70 {
"bstop", cachefiles_daemon_bstop },
71 {
"cull", cachefiles_daemon_cull },
72 {
"debug", cachefiles_daemon_debug },
73 {
"dir", cachefiles_daemon_dir },
74 {
"frun", cachefiles_daemon_frun },
75 {
"fcull", cachefiles_daemon_fcull },
76 {
"fstop", cachefiles_daemon_fstop },
77 {
"inuse", cachefiles_daemon_inuse },
78 {
"secctx", cachefiles_daemon_secctx },
79 {
"tag", cachefiles_daemon_tag },
98 if (
xchg(&cachefiles_open, 1) == 1)
133 static int cachefiles_daemon_release(
struct inode *inode,
struct file *file)
161 static ssize_t cachefiles_daemon_read(
struct file *file,
char __user *
_buffer,
179 n =
snprintf(buffer,
sizeof(buffer),
188 (
unsigned long long) cache->
frun,
189 (
unsigned long long) cache->
fcull,
190 (
unsigned long long) cache->
fstop,
191 (
unsigned long long) cache->
brun,
192 (
unsigned long long) cache->
bcull,
193 (
unsigned long long) cache->
bstop
208 static ssize_t cachefiles_daemon_write(
struct file *file,
209 const char __user *_data,
225 if (datalen < 0 || datalen >
PAGE_SIZE - 1)
240 if (
memchr(data,
'\0', datalen))
244 cp =
memchr(data,
'\n', datalen);
255 for (args = data; *args; args++)
266 for (cmd = cachefiles_daemon_cmds; cmd->
name[0]; cmd++)
280 ret = cmd->
handler(cache, args);
293 static unsigned int cachefiles_daemon_poll(
struct file *file,
318 kerror(
"Free space limits must be in range"
319 " 0%%<=stop<cull<run<100%%");
328 static int cachefiles_daemon_frun(
struct cachefiles_cache *cache,
char *args)
338 if (args[0] !=
'%' || args[1] !=
'\0')
342 return cachefiles_daemon_range_error(cache, args);
352 static int cachefiles_daemon_fcull(
struct cachefiles_cache *cache,
char *args)
362 if (args[0] !=
'%' || args[1] !=
'\0')
366 return cachefiles_daemon_range_error(cache, args);
376 static int cachefiles_daemon_fstop(
struct cachefiles_cache *cache,
char *args)
386 if (args[0] !=
'%' || args[1] !=
'\0')
390 return cachefiles_daemon_range_error(cache, args);
400 static int cachefiles_daemon_brun(
struct cachefiles_cache *cache,
char *args)
410 if (args[0] !=
'%' || args[1] !=
'\0')
414 return cachefiles_daemon_range_error(cache, args);
424 static int cachefiles_daemon_bcull(
struct cachefiles_cache *cache,
char *args)
434 if (args[0] !=
'%' || args[1] !=
'\0')
438 return cachefiles_daemon_range_error(cache, args);
448 static int cachefiles_daemon_bstop(
struct cachefiles_cache *cache,
char *args)
458 if (args[0] !=
'%' || args[1] !=
'\0')
462 return cachefiles_daemon_range_error(cache, args);
472 static int cachefiles_daemon_dir(
struct cachefiles_cache *cache,
char *args)
479 kerror(
"Empty directory specified");
484 kerror(
"Second cache directory specified");
500 static int cachefiles_daemon_secctx(
struct cachefiles_cache *cache,
char *args)
507 kerror(
"Empty security context specified");
512 kerror(
"Second security context specified");
528 static int cachefiles_daemon_tag(
struct cachefiles_cache *cache,
char *args)
535 kerror(
"Empty tag specified");
554 static int cachefiles_daemon_cull(
struct cachefiles_cache *cache,
char *args)
557 const struct cred *saved_cred;
566 kerror(
"cull applied to unready cache");
571 kerror(
"cull applied to dead cache");
581 cachefiles_begin_secure(cache, &saved_cred);
583 cachefiles_end_secure(cache, saved_cred);
591 kerror(
"cull command requires dirfd to be a directory");
595 kerror(
"cull command requires dirfd and filename");
603 static int cachefiles_daemon_debug(
struct cachefiles_cache *cache,
char *args)
618 kerror(
"debug command requires mask");
626 static int cachefiles_daemon_inuse(
struct cachefiles_cache *cache,
char *args)
629 const struct cred *saved_cred;
638 kerror(
"inuse applied to unready cache");
643 kerror(
"inuse applied to dead cache");
653 cachefiles_begin_secure(cache, &saved_cred);
655 cachefiles_end_secure(cache, saved_cred);
663 kerror(
"inuse command requires dirfd to be a directory");
667 kerror(
"inuse command requires dirfd and filename");
676 unsigned fnr,
unsigned bnr)
681 .dentry = cache->
mnt->mnt_root,
695 memset(&stats, 0,
sizeof(stats));
738 cachefiles_state_changed(cache);
746 _debug(
"### CULL CACHE ###");
747 cachefiles_state_changed(cache);