Btrfs: skip writeback of last page when truncating file to same size
[sfrench/cifs-2.6.git] / fs / cachefiles / daemon.c
1 /* Daemon interface
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11
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"
27
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 __poll_t 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 *);
48
49 static unsigned long cachefiles_open;
50
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,
59 };
60
61 struct cachefiles_daemon_cmd {
62         char name[8];
63         int (*handler)(struct cachefiles_cache *cache, char *args);
64 };
65
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                            }
81 };
82
83
84 /*
85  * do various checks
86  */
87 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
88 {
89         struct cachefiles_cache *cache;
90
91         _enter("");
92
93         /* only the superuser may do this */
94         if (!capable(CAP_SYS_ADMIN))
95                 return -EPERM;
96
97         /* the cachefiles device may only be open once at a time */
98         if (xchg(&cachefiles_open, 1) == 1)
99                 return -EBUSY;
100
101         /* allocate a cache record */
102         cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
103         if (!cache) {
104                 cachefiles_open = 0;
105                 return -ENOMEM;
106         }
107
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);
112
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
117          */
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;
124
125         file->private_data = cache;
126         cache->cachefilesd = file;
127         return 0;
128 }
129
130 /*
131  * release a cache
132  */
133 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
134 {
135         struct cachefiles_cache *cache = file->private_data;
136
137         _enter("");
138
139         ASSERT(cache);
140
141         set_bit(CACHEFILES_DEAD, &cache->flags);
142
143         cachefiles_daemon_unbind(cache);
144
145         ASSERT(!cache->active_nodes.rb_node);
146
147         /* clean up the control file interface */
148         cache->cachefilesd = NULL;
149         file->private_data = NULL;
150         cachefiles_open = 0;
151
152         kfree(cache);
153
154         _leave("");
155         return 0;
156 }
157
158 /*
159  * read the cache state
160  */
161 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
162                                       size_t buflen, loff_t *pos)
163 {
164         struct cachefiles_cache *cache = file->private_data;
165         unsigned long long b_released;
166         unsigned f_released;
167         char buffer[256];
168         int n;
169
170         //_enter(",,%zu,", buflen);
171
172         if (!test_bit(CACHEFILES_READY, &cache->flags))
173                 return 0;
174
175         /* check how much space the cache has */
176         cachefiles_has_space(cache, 0, 0);
177
178         /* summarise */
179         f_released = atomic_xchg(&cache->f_released, 0);
180         b_released = atomic_long_xchg(&cache->b_released, 0);
181         clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
182
183         n = snprintf(buffer, sizeof(buffer),
184                      "cull=%c"
185                      " frun=%llx"
186                      " fcull=%llx"
187                      " fstop=%llx"
188                      " brun=%llx"
189                      " bcull=%llx"
190                      " bstop=%llx"
191                      " freleased=%x"
192                      " breleased=%llx",
193                      test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
194                      (unsigned long long) cache->frun,
195                      (unsigned long long) cache->fcull,
196                      (unsigned long long) cache->fstop,
197                      (unsigned long long) cache->brun,
198                      (unsigned long long) cache->bcull,
199                      (unsigned long long) cache->bstop,
200                      f_released,
201                      b_released);
202
203         if (n > buflen)
204                 return -EMSGSIZE;
205
206         if (copy_to_user(_buffer, buffer, n) != 0)
207                 return -EFAULT;
208
209         return n;
210 }
211
212 /*
213  * command the cache
214  */
215 static ssize_t cachefiles_daemon_write(struct file *file,
216                                        const char __user *_data,
217                                        size_t datalen,
218                                        loff_t *pos)
219 {
220         const struct cachefiles_daemon_cmd *cmd;
221         struct cachefiles_cache *cache = file->private_data;
222         ssize_t ret;
223         char *data, *args, *cp;
224
225         //_enter(",,%zu,", datalen);
226
227         ASSERT(cache);
228
229         if (test_bit(CACHEFILES_DEAD, &cache->flags))
230                 return -EIO;
231
232         if (datalen < 0 || datalen > PAGE_SIZE - 1)
233                 return -EOPNOTSUPP;
234
235         /* drag the command string into the kernel so we can parse it */
236         data = memdup_user_nul(_data, datalen);
237         if (IS_ERR(data))
238                 return PTR_ERR(data);
239
240         ret = -EINVAL;
241         if (memchr(data, '\0', datalen))
242                 goto error;
243
244         /* strip any newline */
245         cp = memchr(data, '\n', datalen);
246         if (cp) {
247                 if (cp == data)
248                         goto error;
249
250                 *cp = '\0';
251         }
252
253         /* parse the command */
254         ret = -EOPNOTSUPP;
255
256         for (args = data; *args; args++)
257                 if (isspace(*args))
258                         break;
259         if (*args) {
260                 if (args == data)
261                         goto error;
262                 *args = '\0';
263                 args = skip_spaces(++args);
264         }
265
266         /* run the appropriate command handler */
267         for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
268                 if (strcmp(cmd->name, data) == 0)
269                         goto found_command;
270
271 error:
272         kfree(data);
273         //_leave(" = %zd", ret);
274         return ret;
275
276 found_command:
277         mutex_lock(&cache->daemon_mutex);
278
279         ret = -EIO;
280         if (!test_bit(CACHEFILES_DEAD, &cache->flags))
281                 ret = cmd->handler(cache, args);
282
283         mutex_unlock(&cache->daemon_mutex);
284
285         if (ret == 0)
286                 ret = datalen;
287         goto error;
288 }
289
290 /*
291  * poll for culling state
292  * - use EPOLLOUT to indicate culling state
293  */
294 static __poll_t cachefiles_daemon_poll(struct file *file,
295                                            struct poll_table_struct *poll)
296 {
297         struct cachefiles_cache *cache = file->private_data;
298         __poll_t mask;
299
300         poll_wait(file, &cache->daemon_pollwq, poll);
301         mask = 0;
302
303         if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
304                 mask |= EPOLLIN;
305
306         if (test_bit(CACHEFILES_CULLING, &cache->flags))
307                 mask |= EPOLLOUT;
308
309         return mask;
310 }
311
312 /*
313  * give a range error for cache space constraints
314  * - can be tail-called
315  */
316 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
317                                          char *args)
318 {
319         pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
320
321         return -EINVAL;
322 }
323
324 /*
325  * set the percentage of files at which to stop culling
326  * - command: "frun <N>%"
327  */
328 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
329 {
330         unsigned long frun;
331
332         _enter(",%s", args);
333
334         if (!*args)
335                 return -EINVAL;
336
337         frun = simple_strtoul(args, &args, 10);
338         if (args[0] != '%' || args[1] != '\0')
339                 return -EINVAL;
340
341         if (frun <= cache->fcull_percent || frun >= 100)
342                 return cachefiles_daemon_range_error(cache, args);
343
344         cache->frun_percent = frun;
345         return 0;
346 }
347
348 /*
349  * set the percentage of files at which to start culling
350  * - command: "fcull <N>%"
351  */
352 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
353 {
354         unsigned long fcull;
355
356         _enter(",%s", args);
357
358         if (!*args)
359                 return -EINVAL;
360
361         fcull = simple_strtoul(args, &args, 10);
362         if (args[0] != '%' || args[1] != '\0')
363                 return -EINVAL;
364
365         if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
366                 return cachefiles_daemon_range_error(cache, args);
367
368         cache->fcull_percent = fcull;
369         return 0;
370 }
371
372 /*
373  * set the percentage of files at which to stop allocating
374  * - command: "fstop <N>%"
375  */
376 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
377 {
378         unsigned long fstop;
379
380         _enter(",%s", args);
381
382         if (!*args)
383                 return -EINVAL;
384
385         fstop = simple_strtoul(args, &args, 10);
386         if (args[0] != '%' || args[1] != '\0')
387                 return -EINVAL;
388
389         if (fstop < 0 || fstop >= cache->fcull_percent)
390                 return cachefiles_daemon_range_error(cache, args);
391
392         cache->fstop_percent = fstop;
393         return 0;
394 }
395
396 /*
397  * set the percentage of blocks at which to stop culling
398  * - command: "brun <N>%"
399  */
400 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
401 {
402         unsigned long brun;
403
404         _enter(",%s", args);
405
406         if (!*args)
407                 return -EINVAL;
408
409         brun = simple_strtoul(args, &args, 10);
410         if (args[0] != '%' || args[1] != '\0')
411                 return -EINVAL;
412
413         if (brun <= cache->bcull_percent || brun >= 100)
414                 return cachefiles_daemon_range_error(cache, args);
415
416         cache->brun_percent = brun;
417         return 0;
418 }
419
420 /*
421  * set the percentage of blocks at which to start culling
422  * - command: "bcull <N>%"
423  */
424 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
425 {
426         unsigned long bcull;
427
428         _enter(",%s", args);
429
430         if (!*args)
431                 return -EINVAL;
432
433         bcull = simple_strtoul(args, &args, 10);
434         if (args[0] != '%' || args[1] != '\0')
435                 return -EINVAL;
436
437         if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
438                 return cachefiles_daemon_range_error(cache, args);
439
440         cache->bcull_percent = bcull;
441         return 0;
442 }
443
444 /*
445  * set the percentage of blocks at which to stop allocating
446  * - command: "bstop <N>%"
447  */
448 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
449 {
450         unsigned long bstop;
451
452         _enter(",%s", args);
453
454         if (!*args)
455                 return -EINVAL;
456
457         bstop = simple_strtoul(args, &args, 10);
458         if (args[0] != '%' || args[1] != '\0')
459                 return -EINVAL;
460
461         if (bstop < 0 || bstop >= cache->bcull_percent)
462                 return cachefiles_daemon_range_error(cache, args);
463
464         cache->bstop_percent = bstop;
465         return 0;
466 }
467
468 /*
469  * set the cache directory
470  * - command: "dir <name>"
471  */
472 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
473 {
474         char *dir;
475
476         _enter(",%s", args);
477
478         if (!*args) {
479                 pr_err("Empty directory specified\n");
480                 return -EINVAL;
481         }
482
483         if (cache->rootdirname) {
484                 pr_err("Second cache directory specified\n");
485                 return -EEXIST;
486         }
487
488         dir = kstrdup(args, GFP_KERNEL);
489         if (!dir)
490                 return -ENOMEM;
491
492         cache->rootdirname = dir;
493         return 0;
494 }
495
496 /*
497  * set the cache security context
498  * - command: "secctx <ctx>"
499  */
500 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
501 {
502         char *secctx;
503
504         _enter(",%s", args);
505
506         if (!*args) {
507                 pr_err("Empty security context specified\n");
508                 return -EINVAL;
509         }
510
511         if (cache->secctx) {
512                 pr_err("Second security context specified\n");
513                 return -EINVAL;
514         }
515
516         secctx = kstrdup(args, GFP_KERNEL);
517         if (!secctx)
518                 return -ENOMEM;
519
520         cache->secctx = secctx;
521         return 0;
522 }
523
524 /*
525  * set the cache tag
526  * - command: "tag <name>"
527  */
528 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
529 {
530         char *tag;
531
532         _enter(",%s", args);
533
534         if (!*args) {
535                 pr_err("Empty tag specified\n");
536                 return -EINVAL;
537         }
538
539         if (cache->tag)
540                 return -EEXIST;
541
542         tag = kstrdup(args, GFP_KERNEL);
543         if (!tag)
544                 return -ENOMEM;
545
546         cache->tag = tag;
547         return 0;
548 }
549
550 /*
551  * request a node in the cache be culled from the current working directory
552  * - command: "cull <name>"
553  */
554 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
555 {
556         struct path path;
557         const struct cred *saved_cred;
558         int ret;
559
560         _enter(",%s", args);
561
562         if (strchr(args, '/'))
563                 goto inval;
564
565         if (!test_bit(CACHEFILES_READY, &cache->flags)) {
566                 pr_err("cull applied to unready cache\n");
567                 return -EIO;
568         }
569
570         if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
571                 pr_err("cull applied to dead cache\n");
572                 return -EIO;
573         }
574
575         /* extract the directory dentry from the cwd */
576         get_fs_pwd(current->fs, &path);
577
578         if (!d_can_lookup(path.dentry))
579                 goto notdir;
580
581         cachefiles_begin_secure(cache, &saved_cred);
582         ret = cachefiles_cull(cache, path.dentry, args);
583         cachefiles_end_secure(cache, saved_cred);
584
585         path_put(&path);
586         _leave(" = %d", ret);
587         return ret;
588
589 notdir:
590         path_put(&path);
591         pr_err("cull command requires dirfd to be a directory\n");
592         return -ENOTDIR;
593
594 inval:
595         pr_err("cull command requires dirfd and filename\n");
596         return -EINVAL;
597 }
598
599 /*
600  * set debugging mode
601  * - command: "debug <mask>"
602  */
603 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
604 {
605         unsigned long mask;
606
607         _enter(",%s", args);
608
609         mask = simple_strtoul(args, &args, 0);
610         if (args[0] != '\0')
611                 goto inval;
612
613         cachefiles_debug = mask;
614         _leave(" = 0");
615         return 0;
616
617 inval:
618         pr_err("debug command requires mask\n");
619         return -EINVAL;
620 }
621
622 /*
623  * find out whether an object in the current working directory is in use or not
624  * - command: "inuse <name>"
625  */
626 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
627 {
628         struct path path;
629         const struct cred *saved_cred;
630         int ret;
631
632         //_enter(",%s", args);
633
634         if (strchr(args, '/'))
635                 goto inval;
636
637         if (!test_bit(CACHEFILES_READY, &cache->flags)) {
638                 pr_err("inuse applied to unready cache\n");
639                 return -EIO;
640         }
641
642         if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
643                 pr_err("inuse applied to dead cache\n");
644                 return -EIO;
645         }
646
647         /* extract the directory dentry from the cwd */
648         get_fs_pwd(current->fs, &path);
649
650         if (!d_can_lookup(path.dentry))
651                 goto notdir;
652
653         cachefiles_begin_secure(cache, &saved_cred);
654         ret = cachefiles_check_in_use(cache, path.dentry, args);
655         cachefiles_end_secure(cache, saved_cred);
656
657         path_put(&path);
658         //_leave(" = %d", ret);
659         return ret;
660
661 notdir:
662         path_put(&path);
663         pr_err("inuse command requires dirfd to be a directory\n");
664         return -ENOTDIR;
665
666 inval:
667         pr_err("inuse command requires dirfd and filename\n");
668         return -EINVAL;
669 }
670
671 /*
672  * see if we have space for a number of pages and/or a number of files in the
673  * cache
674  */
675 int cachefiles_has_space(struct cachefiles_cache *cache,
676                          unsigned fnr, unsigned bnr)
677 {
678         struct kstatfs stats;
679         struct path path = {
680                 .mnt    = cache->mnt,
681                 .dentry = cache->mnt->mnt_root,
682         };
683         int ret;
684
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);
693
694         /* find out how many pages of blockdev are available */
695         memset(&stats, 0, sizeof(stats));
696
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;
703         }
704
705         stats.f_bavail >>= cache->bshift;
706
707         //_debug("avail %llu,%llu",
708         //       (unsigned long long) stats.f_ffree,
709         //       (unsigned long long) stats.f_bavail);
710
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;
716
717         if (stats.f_bavail > bnr)
718                 stats.f_bavail -= bnr;
719         else
720                 stats.f_bavail = 0;
721
722         ret = -ENOBUFS;
723         if (stats.f_ffree < cache->fstop ||
724             stats.f_bavail < cache->bstop)
725                 goto begin_cull;
726
727         ret = 0;
728         if (stats.f_ffree < cache->fcull ||
729             stats.f_bavail < cache->bcull)
730                 goto begin_cull;
731
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)
736             ) {
737                 _debug("cease culling");
738                 cachefiles_state_changed(cache);
739         }
740
741         //_leave(" = 0");
742         return 0;
743
744 begin_cull:
745         if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
746                 _debug("### CULL CACHE ###");
747                 cachefiles_state_changed(cache);
748         }
749
750         _leave(" = %d", ret);
751         return ret;
752 }