0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/sched.h>
0011 #include <linux/completion.h>
0012 #include <linux/slab.h>
0013 #include <linux/fs.h>
0014 #include <linux/file.h>
0015 #include <linux/namei.h>
0016 #include <linux/poll.h>
0017 #include <linux/mount.h>
0018 #include <linux/statfs.h>
0019 #include <linux/ctype.h>
0020 #include <linux/string.h>
0021 #include <linux/fs_struct.h>
0022 #include "internal.h"
0023
0024 static int cachefiles_daemon_open(struct inode *, struct file *);
0025 static int cachefiles_daemon_release(struct inode *, struct file *);
0026 static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
0027 loff_t *);
0028 static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
0029 size_t, loff_t *);
0030 static __poll_t cachefiles_daemon_poll(struct file *,
0031 struct poll_table_struct *);
0032 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
0033 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
0034 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
0035 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
0036 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
0037 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
0038 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
0039 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
0040 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
0041 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
0042 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
0043 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
0044 static int cachefiles_daemon_bind(struct cachefiles_cache *, char *);
0045 static void cachefiles_daemon_unbind(struct cachefiles_cache *);
0046
0047 static unsigned long cachefiles_open;
0048
0049 const struct file_operations cachefiles_daemon_fops = {
0050 .owner = THIS_MODULE,
0051 .open = cachefiles_daemon_open,
0052 .release = cachefiles_daemon_release,
0053 .read = cachefiles_daemon_read,
0054 .write = cachefiles_daemon_write,
0055 .poll = cachefiles_daemon_poll,
0056 .llseek = noop_llseek,
0057 };
0058
0059 struct cachefiles_daemon_cmd {
0060 char name[8];
0061 int (*handler)(struct cachefiles_cache *cache, char *args);
0062 };
0063
0064 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
0065 { "bind", cachefiles_daemon_bind },
0066 { "brun", cachefiles_daemon_brun },
0067 { "bcull", cachefiles_daemon_bcull },
0068 { "bstop", cachefiles_daemon_bstop },
0069 { "cull", cachefiles_daemon_cull },
0070 { "debug", cachefiles_daemon_debug },
0071 { "dir", cachefiles_daemon_dir },
0072 { "frun", cachefiles_daemon_frun },
0073 { "fcull", cachefiles_daemon_fcull },
0074 { "fstop", cachefiles_daemon_fstop },
0075 { "inuse", cachefiles_daemon_inuse },
0076 { "secctx", cachefiles_daemon_secctx },
0077 { "tag", cachefiles_daemon_tag },
0078 #ifdef CONFIG_CACHEFILES_ONDEMAND
0079 { "copen", cachefiles_ondemand_copen },
0080 #endif
0081 { "", NULL }
0082 };
0083
0084
0085
0086
0087
0088 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
0089 {
0090 struct cachefiles_cache *cache;
0091
0092 _enter("");
0093
0094
0095 if (!capable(CAP_SYS_ADMIN))
0096 return -EPERM;
0097
0098
0099 if (xchg(&cachefiles_open, 1) == 1)
0100 return -EBUSY;
0101
0102
0103 cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
0104 if (!cache) {
0105 cachefiles_open = 0;
0106 return -ENOMEM;
0107 }
0108
0109 mutex_init(&cache->daemon_mutex);
0110 init_waitqueue_head(&cache->daemon_pollwq);
0111 INIT_LIST_HEAD(&cache->volumes);
0112 INIT_LIST_HEAD(&cache->object_list);
0113 spin_lock_init(&cache->object_list_lock);
0114 refcount_set(&cache->unbind_pincount, 1);
0115 xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
0116 xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
0117
0118
0119
0120
0121
0122
0123 cache->frun_percent = 7;
0124 cache->fcull_percent = 5;
0125 cache->fstop_percent = 1;
0126 cache->brun_percent = 7;
0127 cache->bcull_percent = 5;
0128 cache->bstop_percent = 1;
0129
0130 file->private_data = cache;
0131 cache->cachefilesd = file;
0132 return 0;
0133 }
0134
0135 static void cachefiles_flush_reqs(struct cachefiles_cache *cache)
0136 {
0137 struct xarray *xa = &cache->reqs;
0138 struct cachefiles_req *req;
0139 unsigned long index;
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 smp_mb();
0156
0157 xa_lock(xa);
0158 xa_for_each(xa, index, req) {
0159 req->error = -EIO;
0160 complete(&req->done);
0161 }
0162 xa_unlock(xa);
0163
0164 xa_destroy(&cache->reqs);
0165 xa_destroy(&cache->ondemand_ids);
0166 }
0167
0168 void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache)
0169 {
0170 if (refcount_dec_and_test(&cache->unbind_pincount)) {
0171 cachefiles_daemon_unbind(cache);
0172 cachefiles_open = 0;
0173 kfree(cache);
0174 }
0175 }
0176
0177 void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache)
0178 {
0179 refcount_inc(&cache->unbind_pincount);
0180 }
0181
0182
0183
0184
0185 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
0186 {
0187 struct cachefiles_cache *cache = file->private_data;
0188
0189 _enter("");
0190
0191 ASSERT(cache);
0192
0193 set_bit(CACHEFILES_DEAD, &cache->flags);
0194
0195 if (cachefiles_in_ondemand_mode(cache))
0196 cachefiles_flush_reqs(cache);
0197
0198
0199 cache->cachefilesd = NULL;
0200 file->private_data = NULL;
0201
0202 cachefiles_put_unbind_pincount(cache);
0203
0204 _leave("");
0205 return 0;
0206 }
0207
0208 static ssize_t cachefiles_do_daemon_read(struct cachefiles_cache *cache,
0209 char __user *_buffer, size_t buflen)
0210 {
0211 unsigned long long b_released;
0212 unsigned f_released;
0213 char buffer[256];
0214 int n;
0215
0216
0217 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
0218
0219
0220 f_released = atomic_xchg(&cache->f_released, 0);
0221 b_released = atomic_long_xchg(&cache->b_released, 0);
0222 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
0223
0224 n = snprintf(buffer, sizeof(buffer),
0225 "cull=%c"
0226 " frun=%llx"
0227 " fcull=%llx"
0228 " fstop=%llx"
0229 " brun=%llx"
0230 " bcull=%llx"
0231 " bstop=%llx"
0232 " freleased=%x"
0233 " breleased=%llx",
0234 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
0235 (unsigned long long) cache->frun,
0236 (unsigned long long) cache->fcull,
0237 (unsigned long long) cache->fstop,
0238 (unsigned long long) cache->brun,
0239 (unsigned long long) cache->bcull,
0240 (unsigned long long) cache->bstop,
0241 f_released,
0242 b_released);
0243
0244 if (n > buflen)
0245 return -EMSGSIZE;
0246
0247 if (copy_to_user(_buffer, buffer, n) != 0)
0248 return -EFAULT;
0249
0250 return n;
0251 }
0252
0253
0254
0255
0256 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
0257 size_t buflen, loff_t *pos)
0258 {
0259 struct cachefiles_cache *cache = file->private_data;
0260
0261
0262
0263 if (!test_bit(CACHEFILES_READY, &cache->flags))
0264 return 0;
0265
0266 if (cachefiles_in_ondemand_mode(cache))
0267 return cachefiles_ondemand_daemon_read(cache, _buffer, buflen);
0268 else
0269 return cachefiles_do_daemon_read(cache, _buffer, buflen);
0270 }
0271
0272
0273
0274
0275 static ssize_t cachefiles_daemon_write(struct file *file,
0276 const char __user *_data,
0277 size_t datalen,
0278 loff_t *pos)
0279 {
0280 const struct cachefiles_daemon_cmd *cmd;
0281 struct cachefiles_cache *cache = file->private_data;
0282 ssize_t ret;
0283 char *data, *args, *cp;
0284
0285
0286
0287 ASSERT(cache);
0288
0289 if (test_bit(CACHEFILES_DEAD, &cache->flags))
0290 return -EIO;
0291
0292 if (datalen > PAGE_SIZE - 1)
0293 return -EOPNOTSUPP;
0294
0295
0296 data = memdup_user_nul(_data, datalen);
0297 if (IS_ERR(data))
0298 return PTR_ERR(data);
0299
0300 ret = -EINVAL;
0301 if (memchr(data, '\0', datalen))
0302 goto error;
0303
0304
0305 cp = memchr(data, '\n', datalen);
0306 if (cp) {
0307 if (cp == data)
0308 goto error;
0309
0310 *cp = '\0';
0311 }
0312
0313
0314 ret = -EOPNOTSUPP;
0315
0316 for (args = data; *args; args++)
0317 if (isspace(*args))
0318 break;
0319 if (*args) {
0320 if (args == data)
0321 goto error;
0322 *args = '\0';
0323 args = skip_spaces(++args);
0324 }
0325
0326
0327 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
0328 if (strcmp(cmd->name, data) == 0)
0329 goto found_command;
0330
0331 error:
0332 kfree(data);
0333
0334 return ret;
0335
0336 found_command:
0337 mutex_lock(&cache->daemon_mutex);
0338
0339 ret = -EIO;
0340 if (!test_bit(CACHEFILES_DEAD, &cache->flags))
0341 ret = cmd->handler(cache, args);
0342
0343 mutex_unlock(&cache->daemon_mutex);
0344
0345 if (ret == 0)
0346 ret = datalen;
0347 goto error;
0348 }
0349
0350
0351
0352
0353
0354 static __poll_t cachefiles_daemon_poll(struct file *file,
0355 struct poll_table_struct *poll)
0356 {
0357 struct cachefiles_cache *cache = file->private_data;
0358 __poll_t mask;
0359
0360 poll_wait(file, &cache->daemon_pollwq, poll);
0361 mask = 0;
0362
0363 if (cachefiles_in_ondemand_mode(cache)) {
0364 if (!xa_empty(&cache->reqs))
0365 mask |= EPOLLIN;
0366 } else {
0367 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
0368 mask |= EPOLLIN;
0369 }
0370
0371 if (test_bit(CACHEFILES_CULLING, &cache->flags))
0372 mask |= EPOLLOUT;
0373
0374 return mask;
0375 }
0376
0377
0378
0379
0380
0381 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
0382 char *args)
0383 {
0384 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
0385
0386 return -EINVAL;
0387 }
0388
0389
0390
0391
0392
0393 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
0394 {
0395 unsigned long frun;
0396
0397 _enter(",%s", args);
0398
0399 if (!*args)
0400 return -EINVAL;
0401
0402 frun = simple_strtoul(args, &args, 10);
0403 if (args[0] != '%' || args[1] != '\0')
0404 return -EINVAL;
0405
0406 if (frun <= cache->fcull_percent || frun >= 100)
0407 return cachefiles_daemon_range_error(cache, args);
0408
0409 cache->frun_percent = frun;
0410 return 0;
0411 }
0412
0413
0414
0415
0416
0417 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
0418 {
0419 unsigned long fcull;
0420
0421 _enter(",%s", args);
0422
0423 if (!*args)
0424 return -EINVAL;
0425
0426 fcull = simple_strtoul(args, &args, 10);
0427 if (args[0] != '%' || args[1] != '\0')
0428 return -EINVAL;
0429
0430 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
0431 return cachefiles_daemon_range_error(cache, args);
0432
0433 cache->fcull_percent = fcull;
0434 return 0;
0435 }
0436
0437
0438
0439
0440
0441 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
0442 {
0443 unsigned long fstop;
0444
0445 _enter(",%s", args);
0446
0447 if (!*args)
0448 return -EINVAL;
0449
0450 fstop = simple_strtoul(args, &args, 10);
0451 if (args[0] != '%' || args[1] != '\0')
0452 return -EINVAL;
0453
0454 if (fstop >= cache->fcull_percent)
0455 return cachefiles_daemon_range_error(cache, args);
0456
0457 cache->fstop_percent = fstop;
0458 return 0;
0459 }
0460
0461
0462
0463
0464
0465 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
0466 {
0467 unsigned long brun;
0468
0469 _enter(",%s", args);
0470
0471 if (!*args)
0472 return -EINVAL;
0473
0474 brun = simple_strtoul(args, &args, 10);
0475 if (args[0] != '%' || args[1] != '\0')
0476 return -EINVAL;
0477
0478 if (brun <= cache->bcull_percent || brun >= 100)
0479 return cachefiles_daemon_range_error(cache, args);
0480
0481 cache->brun_percent = brun;
0482 return 0;
0483 }
0484
0485
0486
0487
0488
0489 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
0490 {
0491 unsigned long bcull;
0492
0493 _enter(",%s", args);
0494
0495 if (!*args)
0496 return -EINVAL;
0497
0498 bcull = simple_strtoul(args, &args, 10);
0499 if (args[0] != '%' || args[1] != '\0')
0500 return -EINVAL;
0501
0502 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
0503 return cachefiles_daemon_range_error(cache, args);
0504
0505 cache->bcull_percent = bcull;
0506 return 0;
0507 }
0508
0509
0510
0511
0512
0513 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
0514 {
0515 unsigned long bstop;
0516
0517 _enter(",%s", args);
0518
0519 if (!*args)
0520 return -EINVAL;
0521
0522 bstop = simple_strtoul(args, &args, 10);
0523 if (args[0] != '%' || args[1] != '\0')
0524 return -EINVAL;
0525
0526 if (bstop >= cache->bcull_percent)
0527 return cachefiles_daemon_range_error(cache, args);
0528
0529 cache->bstop_percent = bstop;
0530 return 0;
0531 }
0532
0533
0534
0535
0536
0537 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
0538 {
0539 char *dir;
0540
0541 _enter(",%s", args);
0542
0543 if (!*args) {
0544 pr_err("Empty directory specified\n");
0545 return -EINVAL;
0546 }
0547
0548 if (cache->rootdirname) {
0549 pr_err("Second cache directory specified\n");
0550 return -EEXIST;
0551 }
0552
0553 dir = kstrdup(args, GFP_KERNEL);
0554 if (!dir)
0555 return -ENOMEM;
0556
0557 cache->rootdirname = dir;
0558 return 0;
0559 }
0560
0561
0562
0563
0564
0565 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
0566 {
0567 char *secctx;
0568
0569 _enter(",%s", args);
0570
0571 if (!*args) {
0572 pr_err("Empty security context specified\n");
0573 return -EINVAL;
0574 }
0575
0576 if (cache->secctx) {
0577 pr_err("Second security context specified\n");
0578 return -EINVAL;
0579 }
0580
0581 secctx = kstrdup(args, GFP_KERNEL);
0582 if (!secctx)
0583 return -ENOMEM;
0584
0585 cache->secctx = secctx;
0586 return 0;
0587 }
0588
0589
0590
0591
0592
0593 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
0594 {
0595 char *tag;
0596
0597 _enter(",%s", args);
0598
0599 if (!*args) {
0600 pr_err("Empty tag specified\n");
0601 return -EINVAL;
0602 }
0603
0604 if (cache->tag)
0605 return -EEXIST;
0606
0607 tag = kstrdup(args, GFP_KERNEL);
0608 if (!tag)
0609 return -ENOMEM;
0610
0611 cache->tag = tag;
0612 return 0;
0613 }
0614
0615
0616
0617
0618
0619 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
0620 {
0621 struct path path;
0622 const struct cred *saved_cred;
0623 int ret;
0624
0625 _enter(",%s", args);
0626
0627 if (strchr(args, '/'))
0628 goto inval;
0629
0630 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
0631 pr_err("cull applied to unready cache\n");
0632 return -EIO;
0633 }
0634
0635 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
0636 pr_err("cull applied to dead cache\n");
0637 return -EIO;
0638 }
0639
0640 get_fs_pwd(current->fs, &path);
0641
0642 if (!d_can_lookup(path.dentry))
0643 goto notdir;
0644
0645 cachefiles_begin_secure(cache, &saved_cred);
0646 ret = cachefiles_cull(cache, path.dentry, args);
0647 cachefiles_end_secure(cache, saved_cred);
0648
0649 path_put(&path);
0650 _leave(" = %d", ret);
0651 return ret;
0652
0653 notdir:
0654 path_put(&path);
0655 pr_err("cull command requires dirfd to be a directory\n");
0656 return -ENOTDIR;
0657
0658 inval:
0659 pr_err("cull command requires dirfd and filename\n");
0660 return -EINVAL;
0661 }
0662
0663
0664
0665
0666
0667 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
0668 {
0669 unsigned long mask;
0670
0671 _enter(",%s", args);
0672
0673 mask = simple_strtoul(args, &args, 0);
0674 if (args[0] != '\0')
0675 goto inval;
0676
0677 cachefiles_debug = mask;
0678 _leave(" = 0");
0679 return 0;
0680
0681 inval:
0682 pr_err("debug command requires mask\n");
0683 return -EINVAL;
0684 }
0685
0686
0687
0688
0689
0690 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
0691 {
0692 struct path path;
0693 const struct cred *saved_cred;
0694 int ret;
0695
0696
0697
0698 if (strchr(args, '/'))
0699 goto inval;
0700
0701 if (!test_bit(CACHEFILES_READY, &cache->flags)) {
0702 pr_err("inuse applied to unready cache\n");
0703 return -EIO;
0704 }
0705
0706 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
0707 pr_err("inuse applied to dead cache\n");
0708 return -EIO;
0709 }
0710
0711 get_fs_pwd(current->fs, &path);
0712
0713 if (!d_can_lookup(path.dentry))
0714 goto notdir;
0715
0716 cachefiles_begin_secure(cache, &saved_cred);
0717 ret = cachefiles_check_in_use(cache, path.dentry, args);
0718 cachefiles_end_secure(cache, saved_cred);
0719
0720 path_put(&path);
0721
0722 return ret;
0723
0724 notdir:
0725 path_put(&path);
0726 pr_err("inuse command requires dirfd to be a directory\n");
0727 return -ENOTDIR;
0728
0729 inval:
0730 pr_err("inuse command requires dirfd and filename\n");
0731 return -EINVAL;
0732 }
0733
0734
0735
0736
0737 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
0738 {
0739 _enter("{%u,%u,%u,%u,%u,%u},%s",
0740 cache->frun_percent,
0741 cache->fcull_percent,
0742 cache->fstop_percent,
0743 cache->brun_percent,
0744 cache->bcull_percent,
0745 cache->bstop_percent,
0746 args);
0747
0748 if (cache->fstop_percent >= cache->fcull_percent ||
0749 cache->fcull_percent >= cache->frun_percent ||
0750 cache->frun_percent >= 100)
0751 return -ERANGE;
0752
0753 if (cache->bstop_percent >= cache->bcull_percent ||
0754 cache->bcull_percent >= cache->brun_percent ||
0755 cache->brun_percent >= 100)
0756 return -ERANGE;
0757
0758 if (!cache->rootdirname) {
0759 pr_err("No cache directory specified\n");
0760 return -EINVAL;
0761 }
0762
0763
0764 if (test_bit(CACHEFILES_READY, &cache->flags)) {
0765 pr_err("Cache already bound\n");
0766 return -EBUSY;
0767 }
0768
0769 if (IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND)) {
0770 if (!strcmp(args, "ondemand")) {
0771 set_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
0772 } else if (*args) {
0773 pr_err("Invalid argument to the 'bind' command\n");
0774 return -EINVAL;
0775 }
0776 } else if (*args) {
0777 pr_err("'bind' command doesn't take an argument\n");
0778 return -EINVAL;
0779 }
0780
0781
0782 if (!cache->tag) {
0783
0784
0785
0786
0787 cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
0788 if (!cache->tag)
0789 return -ENOMEM;
0790 }
0791
0792 return cachefiles_add_cache(cache);
0793 }
0794
0795
0796
0797
0798 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
0799 {
0800 _enter("");
0801
0802 if (test_bit(CACHEFILES_READY, &cache->flags))
0803 cachefiles_withdraw_cache(cache);
0804
0805 cachefiles_put_directory(cache->graveyard);
0806 cachefiles_put_directory(cache->store);
0807 mntput(cache->mnt);
0808
0809 kfree(cache->rootdirname);
0810 kfree(cache->secctx);
0811 kfree(cache->tag);
0812
0813 _leave("");
0814 }