Merge branch 'linux-next' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad...
[zen-stable.git] / fs / cachefiles / daemon.c
blob0a1467b15516521569110c18e517a64ae07237e1
1 /* Daemon interface
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/completion.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/file.h>
19 #include <linux/namei.h>
20 #include <linux/poll.h>
21 #include <linux/mount.h>
22 #include <linux/statfs.h>
23 #include <linux/ctype.h>
24 #include <linux/string.h>
25 #include <linux/fs_struct.h>
26 #include "internal.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,
31 loff_t *);
32 static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
33 size_t, loff_t *);
34 static unsigned int cachefiles_daemon_poll(struct file *,
35 struct poll_table_struct *);
36 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
37 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
38 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
39 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
40 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
41 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
42 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
43 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
44 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
45 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
46 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
47 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
49 static unsigned long cachefiles_open;
51 const struct file_operations cachefiles_daemon_fops = {
52 .owner = THIS_MODULE,
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,
58 .llseek = noop_llseek,
61 struct cachefiles_daemon_cmd {
62 char name[8];
63 int (*handler)(struct cachefiles_cache *cache, char *args);
66 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
67 { "bind", cachefiles_daemon_bind },
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 },
80 { "", NULL }
85 * do various checks
87 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
89 struct cachefiles_cache *cache;
91 _enter("");
93 /* only the superuser may do this */
94 if (!capable(CAP_SYS_ADMIN))
95 return -EPERM;
97 /* the cachefiles device may only be open once at a time */
98 if (xchg(&cachefiles_open, 1) == 1)
99 return -EBUSY;
101 /* allocate a cache record */
102 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
103 if (!cache) {
104 cachefiles_open = 0;
105 return -ENOMEM;
108 mutex_init(&cache->daemon_mutex);
109 cache->active_nodes = RB_ROOT;
110 rwlock_init(&cache->active_lock);
111 init_waitqueue_head(&cache->daemon_pollwq);
113 /* set default caching limits
114 * - limit at 1% free space and/or free files
115 * - cull below 5% free space and/or free files
116 * - cease culling above 7% free space and/or free files
118 cache->frun_percent = 7;
119 cache->fcull_percent = 5;
120 cache->fstop_percent = 1;
121 cache->brun_percent = 7;
122 cache->bcull_percent = 5;
123 cache->bstop_percent = 1;
125 file->private_data = cache;
126 cache->cachefilesd = file;
127 return 0;
131 * release a cache
133 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
135 struct cachefiles_cache *cache = file->private_data;
137 _enter("");
139 ASSERT(cache);
141 set_bit(CACHEFILES_DEAD, &cache->flags);
143 cachefiles_daemon_unbind(cache);
145 ASSERT(!cache->active_nodes.rb_node);
147 /* clean up the control file interface */
148 cache->cachefilesd = NULL;
149 file->private_data = NULL;
150 cachefiles_open = 0;
152 kfree(cache);
154 _leave("");
155 return 0;
159 * read the cache state
161 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
162 size_t buflen, loff_t *pos)
164 struct cachefiles_cache *cache = file->private_data;
165 char buffer[256];
166 int n;
168 //_enter(",,%zu,", buflen);
170 if (!test_bit(CACHEFILES_READY, &cache->flags))
171 return 0;
173 /* check how much space the cache has */
174 cachefiles_has_space(cache, 0, 0);
176 /* summarise */
177 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
179 n = snprintf(buffer, sizeof(buffer),
180 "cull=%c"
181 " frun=%llx"
182 " fcull=%llx"
183 " fstop=%llx"
184 " brun=%llx"
185 " bcull=%llx"
186 " bstop=%llx",
187 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
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
196 if (n > buflen)
197 return -EMSGSIZE;
199 if (copy_to_user(_buffer, buffer, n) != 0)
200 return -EFAULT;
202 return n;
206 * command the cache
208 static ssize_t cachefiles_daemon_write(struct file *file,
209 const char __user *_data,
210 size_t datalen,
211 loff_t *pos)
213 const struct cachefiles_daemon_cmd *cmd;
214 struct cachefiles_cache *cache = file->private_data;
215 ssize_t ret;
216 char *data, *args, *cp;
218 //_enter(",,%zu,", datalen);
220 ASSERT(cache);
222 if (test_bit(CACHEFILES_DEAD, &cache->flags))
223 return -EIO;
225 if (datalen < 0 || datalen > PAGE_SIZE - 1)
226 return -EOPNOTSUPP;
228 /* drag the command string into the kernel so we can parse it */
229 data = kmalloc(datalen + 1, GFP_KERNEL);
230 if (!data)
231 return -ENOMEM;
233 ret = -EFAULT;
234 if (copy_from_user(data, _data, datalen) != 0)
235 goto error;
237 data[datalen] = '\0';
239 ret = -EINVAL;
240 if (memchr(data, '\0', datalen))
241 goto error;
243 /* strip any newline */
244 cp = memchr(data, '\n', datalen);
245 if (cp) {
246 if (cp == data)
247 goto error;
249 *cp = '\0';
252 /* parse the command */
253 ret = -EOPNOTSUPP;
255 for (args = data; *args; args++)
256 if (isspace(*args))
257 break;
258 if (*args) {
259 if (args == data)
260 goto error;
261 *args = '\0';
262 args = skip_spaces(++args);
265 /* run the appropriate command handler */
266 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
267 if (strcmp(cmd->name, data) == 0)
268 goto found_command;
270 error:
271 kfree(data);
272 //_leave(" = %zd", ret);
273 return ret;
275 found_command:
276 mutex_lock(&cache->daemon_mutex);
278 ret = -EIO;
279 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
280 ret = cmd->handler(cache, args);
282 mutex_unlock(&cache->daemon_mutex);
284 if (ret == 0)
285 ret = datalen;
286 goto error;
290 * poll for culling state
291 * - use POLLOUT to indicate culling state
293 static unsigned int cachefiles_daemon_poll(struct file *file,
294 struct poll_table_struct *poll)
296 struct cachefiles_cache *cache = file->private_data;
297 unsigned int mask;
299 poll_wait(file, &cache->daemon_pollwq, poll);
300 mask = 0;
302 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
303 mask |= POLLIN;
305 if (test_bit(CACHEFILES_CULLING, &cache->flags))
306 mask |= POLLOUT;
308 return mask;
312 * give a range error for cache space constraints
313 * - can be tail-called
315 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
316 char *args)
318 kerror("Free space limits must be in range"
319 " 0%%<=stop<cull<run<100%%");
321 return -EINVAL;
325 * set the percentage of files at which to stop culling
326 * - command: "frun <N>%"
328 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
330 unsigned long frun;
332 _enter(",%s", args);
334 if (!*args)
335 return -EINVAL;
337 frun = simple_strtoul(args, &args, 10);
338 if (args[0] != '%' || args[1] != '\0')
339 return -EINVAL;
341 if (frun <= cache->fcull_percent || frun >= 100)
342 return cachefiles_daemon_range_error(cache, args);
344 cache->frun_percent = frun;
345 return 0;
349 * set the percentage of files at which to start culling
350 * - command: "fcull <N>%"
352 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
354 unsigned long fcull;
356 _enter(",%s", args);
358 if (!*args)
359 return -EINVAL;
361 fcull = simple_strtoul(args, &args, 10);
362 if (args[0] != '%' || args[1] != '\0')
363 return -EINVAL;
365 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
366 return cachefiles_daemon_range_error(cache, args);
368 cache->fcull_percent = fcull;
369 return 0;
373 * set the percentage of files at which to stop allocating
374 * - command: "fstop <N>%"
376 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
378 unsigned long fstop;
380 _enter(",%s", args);
382 if (!*args)
383 return -EINVAL;
385 fstop = simple_strtoul(args, &args, 10);
386 if (args[0] != '%' || args[1] != '\0')
387 return -EINVAL;
389 if (fstop < 0 || fstop >= cache->fcull_percent)
390 return cachefiles_daemon_range_error(cache, args);
392 cache->fstop_percent = fstop;
393 return 0;
397 * set the percentage of blocks at which to stop culling
398 * - command: "brun <N>%"
400 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
402 unsigned long brun;
404 _enter(",%s", args);
406 if (!*args)
407 return -EINVAL;
409 brun = simple_strtoul(args, &args, 10);
410 if (args[0] != '%' || args[1] != '\0')
411 return -EINVAL;
413 if (brun <= cache->bcull_percent || brun >= 100)
414 return cachefiles_daemon_range_error(cache, args);
416 cache->brun_percent = brun;
417 return 0;
421 * set the percentage of blocks at which to start culling
422 * - command: "bcull <N>%"
424 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
426 unsigned long bcull;
428 _enter(",%s", args);
430 if (!*args)
431 return -EINVAL;
433 bcull = simple_strtoul(args, &args, 10);
434 if (args[0] != '%' || args[1] != '\0')
435 return -EINVAL;
437 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
438 return cachefiles_daemon_range_error(cache, args);
440 cache->bcull_percent = bcull;
441 return 0;
445 * set the percentage of blocks at which to stop allocating
446 * - command: "bstop <N>%"
448 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
450 unsigned long bstop;
452 _enter(",%s", args);
454 if (!*args)
455 return -EINVAL;
457 bstop = simple_strtoul(args, &args, 10);
458 if (args[0] != '%' || args[1] != '\0')
459 return -EINVAL;
461 if (bstop < 0 || bstop >= cache->bcull_percent)
462 return cachefiles_daemon_range_error(cache, args);
464 cache->bstop_percent = bstop;
465 return 0;
469 * set the cache directory
470 * - command: "dir <name>"
472 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
474 char *dir;
476 _enter(",%s", args);
478 if (!*args) {
479 kerror("Empty directory specified");
480 return -EINVAL;
483 if (cache->rootdirname) {
484 kerror("Second cache directory specified");
485 return -EEXIST;
488 dir = kstrdup(args, GFP_KERNEL);
489 if (!dir)
490 return -ENOMEM;
492 cache->rootdirname = dir;
493 return 0;
497 * set the cache security context
498 * - command: "secctx <ctx>"
500 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
502 char *secctx;
504 _enter(",%s", args);
506 if (!*args) {
507 kerror("Empty security context specified");
508 return -EINVAL;
511 if (cache->secctx) {
512 kerror("Second security context specified");
513 return -EINVAL;
516 secctx = kstrdup(args, GFP_KERNEL);
517 if (!secctx)
518 return -ENOMEM;
520 cache->secctx = secctx;
521 return 0;
525 * set the cache tag
526 * - command: "tag <name>"
528 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
530 char *tag;
532 _enter(",%s", args);
534 if (!*args) {
535 kerror("Empty tag specified");
536 return -EINVAL;
539 if (cache->tag)
540 return -EEXIST;
542 tag = kstrdup(args, GFP_KERNEL);
543 if (!tag)
544 return -ENOMEM;
546 cache->tag = tag;
547 return 0;
551 * request a node in the cache be culled from the current working directory
552 * - command: "cull <name>"
554 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
556 struct path path;
557 const struct cred *saved_cred;
558 int ret;
560 _enter(",%s", args);
562 if (strchr(args, '/'))
563 goto inval;
565 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
566 kerror("cull applied to unready cache");
567 return -EIO;
570 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
571 kerror("cull applied to dead cache");
572 return -EIO;
575 /* extract the directory dentry from the cwd */
576 get_fs_pwd(current->fs, &path);
578 if (!S_ISDIR(path.dentry->d_inode->i_mode))
579 goto notdir;
581 cachefiles_begin_secure(cache, &saved_cred);
582 ret = cachefiles_cull(cache, path.dentry, args);
583 cachefiles_end_secure(cache, saved_cred);
585 path_put(&path);
586 _leave(" = %d", ret);
587 return ret;
589 notdir:
590 path_put(&path);
591 kerror("cull command requires dirfd to be a directory");
592 return -ENOTDIR;
594 inval:
595 kerror("cull command requires dirfd and filename");
596 return -EINVAL;
600 * set debugging mode
601 * - command: "debug <mask>"
603 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
605 unsigned long mask;
607 _enter(",%s", args);
609 mask = simple_strtoul(args, &args, 0);
610 if (args[0] != '\0')
611 goto inval;
613 cachefiles_debug = mask;
614 _leave(" = 0");
615 return 0;
617 inval:
618 kerror("debug command requires mask");
619 return -EINVAL;
623 * find out whether an object in the current working directory is in use or not
624 * - command: "inuse <name>"
626 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
628 struct path path;
629 const struct cred *saved_cred;
630 int ret;
632 //_enter(",%s", args);
634 if (strchr(args, '/'))
635 goto inval;
637 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
638 kerror("inuse applied to unready cache");
639 return -EIO;
642 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
643 kerror("inuse applied to dead cache");
644 return -EIO;
647 /* extract the directory dentry from the cwd */
648 get_fs_pwd(current->fs, &path);
650 if (!S_ISDIR(path.dentry->d_inode->i_mode))
651 goto notdir;
653 cachefiles_begin_secure(cache, &saved_cred);
654 ret = cachefiles_check_in_use(cache, path.dentry, args);
655 cachefiles_end_secure(cache, saved_cred);
657 path_put(&path);
658 //_leave(" = %d", ret);
659 return ret;
661 notdir:
662 path_put(&path);
663 kerror("inuse command requires dirfd to be a directory");
664 return -ENOTDIR;
666 inval:
667 kerror("inuse command requires dirfd and filename");
668 return -EINVAL;
672 * see if we have space for a number of pages and/or a number of files in the
673 * cache
675 int cachefiles_has_space(struct cachefiles_cache *cache,
676 unsigned fnr, unsigned bnr)
678 struct kstatfs stats;
679 struct path path = {
680 .mnt = cache->mnt,
681 .dentry = cache->mnt->mnt_root,
683 int ret;
685 //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
686 // (unsigned long long) cache->frun,
687 // (unsigned long long) cache->fcull,
688 // (unsigned long long) cache->fstop,
689 // (unsigned long long) cache->brun,
690 // (unsigned long long) cache->bcull,
691 // (unsigned long long) cache->bstop,
692 // fnr, bnr);
694 /* find out how many pages of blockdev are available */
695 memset(&stats, 0, sizeof(stats));
697 ret = vfs_statfs(&path, &stats);
698 if (ret < 0) {
699 if (ret == -EIO)
700 cachefiles_io_error(cache, "statfs failed");
701 _leave(" = %d", ret);
702 return ret;
705 stats.f_bavail >>= cache->bshift;
707 //_debug("avail %llu,%llu",
708 // (unsigned long long) stats.f_ffree,
709 // (unsigned long long) stats.f_bavail);
711 /* see if there is sufficient space */
712 if (stats.f_ffree > fnr)
713 stats.f_ffree -= fnr;
714 else
715 stats.f_ffree = 0;
717 if (stats.f_bavail > bnr)
718 stats.f_bavail -= bnr;
719 else
720 stats.f_bavail = 0;
722 ret = -ENOBUFS;
723 if (stats.f_ffree < cache->fstop ||
724 stats.f_bavail < cache->bstop)
725 goto begin_cull;
727 ret = 0;
728 if (stats.f_ffree < cache->fcull ||
729 stats.f_bavail < cache->bcull)
730 goto begin_cull;
732 if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
733 stats.f_ffree >= cache->frun &&
734 stats.f_bavail >= cache->brun &&
735 test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
737 _debug("cease culling");
738 cachefiles_state_changed(cache);
741 //_leave(" = 0");
742 return 0;
744 begin_cull:
745 if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
746 _debug("### CULL CACHE ###");
747 cachefiles_state_changed(cache);
750 _leave(" = %d", ret);
751 return ret;