page allocator: do not setup zonelist cache when there is only one node
[linux/fpc-iii.git] / fs / cachefiles / daemon.c
blob4618516dd994b3c2da8a44c13d83a877d89fb220
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/fs_struct.h>
25 #include "internal.h"
27 static int cachefiles_daemon_open(struct inode *, struct file *);
28 static int cachefiles_daemon_release(struct inode *, struct file *);
29 static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
30 loff_t *);
31 static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
32 size_t, loff_t *);
33 static unsigned int cachefiles_daemon_poll(struct file *,
34 struct poll_table_struct *);
35 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
36 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
37 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
38 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
39 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
40 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
41 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
42 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
43 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
44 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
45 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
46 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
48 static unsigned long cachefiles_open;
50 const struct file_operations cachefiles_daemon_fops = {
51 .owner = THIS_MODULE,
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,
59 struct cachefiles_daemon_cmd {
60 char name[8];
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 { "", NULL }
83 * do various checks
85 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
87 struct cachefiles_cache *cache;
89 _enter("");
91 /* only the superuser may do this */
92 if (!capable(CAP_SYS_ADMIN))
93 return -EPERM;
95 /* the cachefiles device may only be open once at a time */
96 if (xchg(&cachefiles_open, 1) == 1)
97 return -EBUSY;
99 /* allocate a cache record */
100 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
101 if (!cache) {
102 cachefiles_open = 0;
103 return -ENOMEM;
106 mutex_init(&cache->daemon_mutex);
107 cache->active_nodes = RB_ROOT;
108 rwlock_init(&cache->active_lock);
109 init_waitqueue_head(&cache->daemon_pollwq);
111 /* set default caching limits
112 * - limit at 1% free space and/or free files
113 * - cull below 5% free space and/or free files
114 * - cease culling above 7% free space and/or free files
116 cache->frun_percent = 7;
117 cache->fcull_percent = 5;
118 cache->fstop_percent = 1;
119 cache->brun_percent = 7;
120 cache->bcull_percent = 5;
121 cache->bstop_percent = 1;
123 file->private_data = cache;
124 cache->cachefilesd = file;
125 return 0;
129 * release a cache
131 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
133 struct cachefiles_cache *cache = file->private_data;
135 _enter("");
137 ASSERT(cache);
139 set_bit(CACHEFILES_DEAD, &cache->flags);
141 cachefiles_daemon_unbind(cache);
143 ASSERT(!cache->active_nodes.rb_node);
145 /* clean up the control file interface */
146 cache->cachefilesd = NULL;
147 file->private_data = NULL;
148 cachefiles_open = 0;
150 kfree(cache);
152 _leave("");
153 return 0;
157 * read the cache state
159 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
160 size_t buflen, loff_t *pos)
162 struct cachefiles_cache *cache = file->private_data;
163 char buffer[256];
164 int n;
166 //_enter(",,%zu,", buflen);
168 if (!test_bit(CACHEFILES_READY, &cache->flags))
169 return 0;
171 /* check how much space the cache has */
172 cachefiles_has_space(cache, 0, 0);
174 /* summarise */
175 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
177 n = snprintf(buffer, sizeof(buffer),
178 "cull=%c"
179 " frun=%llx"
180 " fcull=%llx"
181 " fstop=%llx"
182 " brun=%llx"
183 " bcull=%llx"
184 " bstop=%llx",
185 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
186 (unsigned long long) cache->frun,
187 (unsigned long long) cache->fcull,
188 (unsigned long long) cache->fstop,
189 (unsigned long long) cache->brun,
190 (unsigned long long) cache->bcull,
191 (unsigned long long) cache->bstop
194 if (n > buflen)
195 return -EMSGSIZE;
197 if (copy_to_user(_buffer, buffer, n) != 0)
198 return -EFAULT;
200 return n;
204 * command the cache
206 static ssize_t cachefiles_daemon_write(struct file *file,
207 const char __user *_data,
208 size_t datalen,
209 loff_t *pos)
211 const struct cachefiles_daemon_cmd *cmd;
212 struct cachefiles_cache *cache = file->private_data;
213 ssize_t ret;
214 char *data, *args, *cp;
216 //_enter(",,%zu,", datalen);
218 ASSERT(cache);
220 if (test_bit(CACHEFILES_DEAD, &cache->flags))
221 return -EIO;
223 if (datalen < 0 || datalen > PAGE_SIZE - 1)
224 return -EOPNOTSUPP;
226 /* drag the command string into the kernel so we can parse it */
227 data = kmalloc(datalen + 1, GFP_KERNEL);
228 if (!data)
229 return -ENOMEM;
231 ret = -EFAULT;
232 if (copy_from_user(data, _data, datalen) != 0)
233 goto error;
235 data[datalen] = '\0';
237 ret = -EINVAL;
238 if (memchr(data, '\0', datalen))
239 goto error;
241 /* strip any newline */
242 cp = memchr(data, '\n', datalen);
243 if (cp) {
244 if (cp == data)
245 goto error;
247 *cp = '\0';
250 /* parse the command */
251 ret = -EOPNOTSUPP;
253 for (args = data; *args; args++)
254 if (isspace(*args))
255 break;
256 if (*args) {
257 if (args == data)
258 goto error;
259 *args = '\0';
260 for (args++; isspace(*args); args++)
261 continue;
264 /* run the appropriate command handler */
265 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
266 if (strcmp(cmd->name, data) == 0)
267 goto found_command;
269 error:
270 kfree(data);
271 //_leave(" = %zd", ret);
272 return ret;
274 found_command:
275 mutex_lock(&cache->daemon_mutex);
277 ret = -EIO;
278 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
279 ret = cmd->handler(cache, args);
281 mutex_unlock(&cache->daemon_mutex);
283 if (ret == 0)
284 ret = datalen;
285 goto error;
289 * poll for culling state
290 * - use POLLOUT to indicate culling state
292 static unsigned int cachefiles_daemon_poll(struct file *file,
293 struct poll_table_struct *poll)
295 struct cachefiles_cache *cache = file->private_data;
296 unsigned int mask;
298 poll_wait(file, &cache->daemon_pollwq, poll);
299 mask = 0;
301 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
302 mask |= POLLIN;
304 if (test_bit(CACHEFILES_CULLING, &cache->flags))
305 mask |= POLLOUT;
307 return mask;
311 * give a range error for cache space constraints
312 * - can be tail-called
314 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
315 char *args)
317 kerror("Free space limits must be in range"
318 " 0%%<=stop<cull<run<100%%");
320 return -EINVAL;
324 * set the percentage of files at which to stop culling
325 * - command: "frun <N>%"
327 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
329 unsigned long frun;
331 _enter(",%s", args);
333 if (!*args)
334 return -EINVAL;
336 frun = simple_strtoul(args, &args, 10);
337 if (args[0] != '%' || args[1] != '\0')
338 return -EINVAL;
340 if (frun <= cache->fcull_percent || frun >= 100)
341 return cachefiles_daemon_range_error(cache, args);
343 cache->frun_percent = frun;
344 return 0;
348 * set the percentage of files at which to start culling
349 * - command: "fcull <N>%"
351 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
353 unsigned long fcull;
355 _enter(",%s", args);
357 if (!*args)
358 return -EINVAL;
360 fcull = simple_strtoul(args, &args, 10);
361 if (args[0] != '%' || args[1] != '\0')
362 return -EINVAL;
364 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
365 return cachefiles_daemon_range_error(cache, args);
367 cache->fcull_percent = fcull;
368 return 0;
372 * set the percentage of files at which to stop allocating
373 * - command: "fstop <N>%"
375 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
377 unsigned long fstop;
379 _enter(",%s", args);
381 if (!*args)
382 return -EINVAL;
384 fstop = simple_strtoul(args, &args, 10);
385 if (args[0] != '%' || args[1] != '\0')
386 return -EINVAL;
388 if (fstop < 0 || fstop >= cache->fcull_percent)
389 return cachefiles_daemon_range_error(cache, args);
391 cache->fstop_percent = fstop;
392 return 0;
396 * set the percentage of blocks at which to stop culling
397 * - command: "brun <N>%"
399 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
401 unsigned long brun;
403 _enter(",%s", args);
405 if (!*args)
406 return -EINVAL;
408 brun = simple_strtoul(args, &args, 10);
409 if (args[0] != '%' || args[1] != '\0')
410 return -EINVAL;
412 if (brun <= cache->bcull_percent || brun >= 100)
413 return cachefiles_daemon_range_error(cache, args);
415 cache->brun_percent = brun;
416 return 0;
420 * set the percentage of blocks at which to start culling
421 * - command: "bcull <N>%"
423 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
425 unsigned long bcull;
427 _enter(",%s", args);
429 if (!*args)
430 return -EINVAL;
432 bcull = simple_strtoul(args, &args, 10);
433 if (args[0] != '%' || args[1] != '\0')
434 return -EINVAL;
436 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
437 return cachefiles_daemon_range_error(cache, args);
439 cache->bcull_percent = bcull;
440 return 0;
444 * set the percentage of blocks at which to stop allocating
445 * - command: "bstop <N>%"
447 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
449 unsigned long bstop;
451 _enter(",%s", args);
453 if (!*args)
454 return -EINVAL;
456 bstop = simple_strtoul(args, &args, 10);
457 if (args[0] != '%' || args[1] != '\0')
458 return -EINVAL;
460 if (bstop < 0 || bstop >= cache->bcull_percent)
461 return cachefiles_daemon_range_error(cache, args);
463 cache->bstop_percent = bstop;
464 return 0;
468 * set the cache directory
469 * - command: "dir <name>"
471 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
473 char *dir;
475 _enter(",%s", args);
477 if (!*args) {
478 kerror("Empty directory specified");
479 return -EINVAL;
482 if (cache->rootdirname) {
483 kerror("Second cache directory specified");
484 return -EEXIST;
487 dir = kstrdup(args, GFP_KERNEL);
488 if (!dir)
489 return -ENOMEM;
491 cache->rootdirname = dir;
492 return 0;
496 * set the cache security context
497 * - command: "secctx <ctx>"
499 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
501 char *secctx;
503 _enter(",%s", args);
505 if (!*args) {
506 kerror("Empty security context specified");
507 return -EINVAL;
510 if (cache->secctx) {
511 kerror("Second security context specified");
512 return -EINVAL;
515 secctx = kstrdup(args, GFP_KERNEL);
516 if (!secctx)
517 return -ENOMEM;
519 cache->secctx = secctx;
520 return 0;
524 * set the cache tag
525 * - command: "tag <name>"
527 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
529 char *tag;
531 _enter(",%s", args);
533 if (!*args) {
534 kerror("Empty tag specified");
535 return -EINVAL;
538 if (cache->tag)
539 return -EEXIST;
541 tag = kstrdup(args, GFP_KERNEL);
542 if (!tag)
543 return -ENOMEM;
545 cache->tag = tag;
546 return 0;
550 * request a node in the cache be culled from the current working directory
551 * - command: "cull <name>"
553 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
555 struct fs_struct *fs;
556 struct dentry *dir;
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 fs = current->fs;
577 read_lock(&fs->lock);
578 dir = dget(fs->pwd.dentry);
579 read_unlock(&fs->lock);
581 if (!S_ISDIR(dir->d_inode->i_mode))
582 goto notdir;
584 cachefiles_begin_secure(cache, &saved_cred);
585 ret = cachefiles_cull(cache, dir, args);
586 cachefiles_end_secure(cache, saved_cred);
588 dput(dir);
589 _leave(" = %d", ret);
590 return ret;
592 notdir:
593 dput(dir);
594 kerror("cull command requires dirfd to be a directory");
595 return -ENOTDIR;
597 inval:
598 kerror("cull command requires dirfd and filename");
599 return -EINVAL;
603 * set debugging mode
604 * - command: "debug <mask>"
606 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
608 unsigned long mask;
610 _enter(",%s", args);
612 mask = simple_strtoul(args, &args, 0);
613 if (args[0] != '\0')
614 goto inval;
616 cachefiles_debug = mask;
617 _leave(" = 0");
618 return 0;
620 inval:
621 kerror("debug command requires mask");
622 return -EINVAL;
626 * find out whether an object in the current working directory is in use or not
627 * - command: "inuse <name>"
629 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
631 struct fs_struct *fs;
632 struct dentry *dir;
633 const struct cred *saved_cred;
634 int ret;
636 //_enter(",%s", args);
638 if (strchr(args, '/'))
639 goto inval;
641 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
642 kerror("inuse applied to unready cache");
643 return -EIO;
646 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
647 kerror("inuse applied to dead cache");
648 return -EIO;
651 /* extract the directory dentry from the cwd */
652 fs = current->fs;
653 read_lock(&fs->lock);
654 dir = dget(fs->pwd.dentry);
655 read_unlock(&fs->lock);
657 if (!S_ISDIR(dir->d_inode->i_mode))
658 goto notdir;
660 cachefiles_begin_secure(cache, &saved_cred);
661 ret = cachefiles_check_in_use(cache, dir, args);
662 cachefiles_end_secure(cache, saved_cred);
664 dput(dir);
665 //_leave(" = %d", ret);
666 return ret;
668 notdir:
669 dput(dir);
670 kerror("inuse command requires dirfd to be a directory");
671 return -ENOTDIR;
673 inval:
674 kerror("inuse command requires dirfd and filename");
675 return -EINVAL;
679 * see if we have space for a number of pages and/or a number of files in the
680 * cache
682 int cachefiles_has_space(struct cachefiles_cache *cache,
683 unsigned fnr, unsigned bnr)
685 struct kstatfs stats;
686 int ret;
688 //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
689 // (unsigned long long) cache->frun,
690 // (unsigned long long) cache->fcull,
691 // (unsigned long long) cache->fstop,
692 // (unsigned long long) cache->brun,
693 // (unsigned long long) cache->bcull,
694 // (unsigned long long) cache->bstop,
695 // fnr, bnr);
697 /* find out how many pages of blockdev are available */
698 memset(&stats, 0, sizeof(stats));
700 ret = vfs_statfs(cache->mnt->mnt_root, &stats);
701 if (ret < 0) {
702 if (ret == -EIO)
703 cachefiles_io_error(cache, "statfs failed");
704 _leave(" = %d", ret);
705 return ret;
708 stats.f_bavail >>= cache->bshift;
710 //_debug("avail %llu,%llu",
711 // (unsigned long long) stats.f_ffree,
712 // (unsigned long long) stats.f_bavail);
714 /* see if there is sufficient space */
715 if (stats.f_ffree > fnr)
716 stats.f_ffree -= fnr;
717 else
718 stats.f_ffree = 0;
720 if (stats.f_bavail > bnr)
721 stats.f_bavail -= bnr;
722 else
723 stats.f_bavail = 0;
725 ret = -ENOBUFS;
726 if (stats.f_ffree < cache->fstop ||
727 stats.f_bavail < cache->bstop)
728 goto begin_cull;
730 ret = 0;
731 if (stats.f_ffree < cache->fcull ||
732 stats.f_bavail < cache->bcull)
733 goto begin_cull;
735 if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
736 stats.f_ffree >= cache->frun &&
737 stats.f_bavail >= cache->brun &&
738 test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
740 _debug("cease culling");
741 cachefiles_state_changed(cache);
744 //_leave(" = 0");
745 return 0;
747 begin_cull:
748 if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
749 _debug("### CULL CACHE ###");
750 cachefiles_state_changed(cache);
753 _leave(" = %d", ret);
754 return ret;