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