mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-22 08:49:45 +00:00
![David Howells](/assets/img/avatar_default.png)
Add an FS-Cache cache-backend that permits a mounted filesystem to be used as a backing store for the cache. CacheFiles uses a userspace daemon to do some of the cache management - such as reaping stale nodes and culling. This is called cachefilesd and lives in /sbin. The source for the daemon can be downloaded from: http://people.redhat.com/~dhowells/cachefs/cachefilesd.c And an example configuration from: http://people.redhat.com/~dhowells/cachefs/cachefilesd.conf The filesystem and data integrity of the cache are only as good as those of the filesystem providing the backing services. Note that CacheFiles does not attempt to journal anything since the journalling interfaces of the various filesystems are very specific in nature. CacheFiles creates a misc character device - "/dev/cachefiles" - that is used to communication with the daemon. Only one thing may have this open at once, and whilst it is open, a cache is at least partially in existence. The daemon opens this and sends commands down it to control the cache. CacheFiles is currently limited to a single cache. CacheFiles attempts to maintain at least a certain percentage of free space on the filesystem, shrinking the cache by culling the objects it contains to make space if necessary - see the "Cache Culling" section. This means it can be placed on the same medium as a live set of data, and will expand to make use of spare space and automatically contract when the set of data requires more space. ============ REQUIREMENTS ============ The use of CacheFiles and its daemon requires the following features to be available in the system and in the cache filesystem: - dnotify. - extended attributes (xattrs). - openat() and friends. - bmap() support on files in the filesystem (FIBMAP ioctl). - The use of bmap() to detect a partial page at the end of the file. It is strongly recommended that the "dir_index" option is enabled on Ext3 filesystems being used as a cache. ============= CONFIGURATION ============= The cache is configured by a script in /etc/cachefilesd.conf. These commands set up cache ready for use. The following script commands are available: (*) brun <N>% (*) bcull <N>% (*) bstop <N>% (*) frun <N>% (*) fcull <N>% (*) fstop <N>% Configure the culling limits. Optional. See the section on culling The defaults are 7% (run), 5% (cull) and 1% (stop) respectively. The commands beginning with a 'b' are file space (block) limits, those beginning with an 'f' are file count limits. (*) dir <path> Specify the directory containing the root of the cache. Mandatory. (*) tag <name> Specify a tag to FS-Cache to use in distinguishing multiple caches. Optional. The default is "CacheFiles". (*) debug <mask> Specify a numeric bitmask to control debugging in the kernel module. Optional. The default is zero (all off). The following values can be OR'd into the mask to collect various information: 1 Turn on trace of function entry (_enter() macros) 2 Turn on trace of function exit (_leave() macros) 4 Turn on trace of internal debug points (_debug()) This mask can also be set through sysfs, eg: echo 5 >/sys/modules/cachefiles/parameters/debug ================== STARTING THE CACHE ================== The cache is started by running the daemon. The daemon opens the cache device, configures the cache and tells it to begin caching. At that point the cache binds to fscache and the cache becomes live. The daemon is run as follows: /sbin/cachefilesd [-d]* [-s] [-n] [-f <configfile>] The flags are: (*) -d Increase the debugging level. This can be specified multiple times and is cumulative with itself. (*) -s Send messages to stderr instead of syslog. (*) -n Don't daemonise and go into background. (*) -f <configfile> Use an alternative configuration file rather than the default one. =============== THINGS TO AVOID =============== Do not mount other things within the cache as this will cause problems. The kernel module contains its own very cut-down path walking facility that ignores mountpoints, but the daemon can't avoid them. Do not create, rename or unlink files and directories in the cache whilst the cache is active, as this may cause the state to become uncertain. Renaming files in the cache might make objects appear to be other objects (the filename is part of the lookup key). Do not change or remove the extended attributes attached to cache files by the cache as this will cause the cache state management to get confused. Do not create files or directories in the cache, lest the cache get confused or serve incorrect data. Do not chmod files in the cache. The module creates things with minimal permissions to prevent random users being able to access them directly. ============= CACHE CULLING ============= The cache may need culling occasionally to make space. This involves discarding objects from the cache that have been used less recently than anything else. Culling is based on the access time of data objects. Empty directories are culled if not in use. Cache culling is done on the basis of the percentage of blocks and the percentage of files available in the underlying filesystem. There are six "limits": (*) brun (*) frun If the amount of free space and the number of available files in the cache rises above both these limits, then culling is turned off. (*) bcull (*) fcull If the amount of available space or the number of available files in the cache falls below either of these limits, then culling is started. (*) bstop (*) fstop If the amount of available space or the number of available files in the cache falls below either of these limits, then no further allocation of disk space or files is permitted until culling has raised things above these limits again. These must be configured thusly: 0 <= bstop < bcull < brun < 100 0 <= fstop < fcull < frun < 100 Note that these are percentages of available space and available files, and do _not_ appear as 100 minus the percentage displayed by the "df" program. The userspace daemon scans the cache to build up a table of cullable objects. These are then culled in least recently used order. A new scan of the cache is started as soon as space is made in the table. Objects will be skipped if their atimes have changed or if the kernel module says it is still using them. =============== CACHE STRUCTURE =============== The CacheFiles module will create two directories in the directory it was given: (*) cache/ (*) graveyard/ The active cache objects all reside in the first directory. The CacheFiles kernel module moves any retired or culled objects that it can't simply unlink to the graveyard from which the daemon will actually delete them. The daemon uses dnotify to monitor the graveyard directory, and will delete anything that appears therein. The module represents index objects as directories with the filename "I..." or "J...". Note that the "cache/" directory is itself a special index. Data objects are represented as files if they have no children, or directories if they do. Their filenames all begin "D..." or "E...". If represented as a directory, data objects will have a file in the directory called "data" that actually holds the data. Special objects are similar to data objects, except their filenames begin "S..." or "T...". If an object has children, then it will be represented as a directory. Immediately in the representative directory are a collection of directories named for hash values of the child object keys with an '@' prepended. Into this directory, if possible, will be placed the representations of the child objects: INDEX INDEX INDEX DATA FILES ========= ========== ================================= ================ cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400 cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...DB1ry cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...N22ry cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...FP1ry If the key is so long that it exceeds NAME_MAX with the decorations added on to it, then it will be cut into pieces, the first few of which will be used to make a nest of directories, and the last one of which will be the objects inside the last directory. The names of the intermediate directories will have '+' prepended: J1223/@23/+xy...z/+kl...m/Epqr Note that keys are raw data, and not only may they exceed NAME_MAX in size, they may also contain things like '/' and NUL characters, and so they may not be suitable for turning directly into a filename. To handle this, CacheFiles will use a suitably printable filename directly and "base-64" encode ones that aren't directly suitable. The two versions of object filenames indicate the encoding: OBJECT TYPE PRINTABLE ENCODED =============== =============== =============== Index "I..." "J..." Data "D..." "E..." Special "S..." "T..." Intermediate directories are always "@" or "+" as appropriate. Each object in the cache has an extended attribute label that holds the object type ID (required to distinguish special objects) and the auxiliary data from the netfs. The latter is used to detect stale objects in the cache and update or retire them. Note that CacheFiles will erase from the cache any file it doesn't recognise or any file of an incorrect type (such as a FIFO file or a device file). ========================== SECURITY MODEL AND SELINUX ========================== CacheFiles is implemented to deal properly with the LSM security features of the Linux kernel and the SELinux facility. One of the problems that CacheFiles faces is that it is generally acting on behalf of a process, and running in that process's context, and that includes a security context that is not appropriate for accessing the cache - either because the files in the cache are inaccessible to that process, or because if the process creates a file in the cache, that file may be inaccessible to other processes. The way CacheFiles works is to temporarily change the security context (fsuid, fsgid and actor security label) that the process acts as - without changing the security context of the process when it the target of an operation performed by some other process (so signalling and suchlike still work correctly). When the CacheFiles module is asked to bind to its cache, it: (1) Finds the security label attached to the root cache directory and uses that as the security label with which it will create files. By default, this is: cachefiles_var_t (2) Finds the security label of the process which issued the bind request (presumed to be the cachefilesd daemon), which by default will be: cachefilesd_t and asks LSM to supply a security ID as which it should act given the daemon's label. By default, this will be: cachefiles_kernel_t SELinux transitions the daemon's security ID to the module's security ID based on a rule of this form in the policy. type_transition <daemon's-ID> kernel_t : process <module's-ID>; For instance: type_transition cachefilesd_t kernel_t : process cachefiles_kernel_t; The module's security ID gives it permission to create, move and remove files and directories in the cache, to find and access directories and files in the cache, to set and access extended attributes on cache objects, and to read and write files in the cache. The daemon's security ID gives it only a very restricted set of permissions: it may scan directories, stat files and erase files and directories. It may not read or write files in the cache, and so it is precluded from accessing the data cached therein; nor is it permitted to create new files in the cache. There are policy source files available in: http://people.redhat.com/~dhowells/fscache/cachefilesd-0.8.tar.bz2 and later versions. In that tarball, see the files: cachefilesd.te cachefilesd.fc cachefilesd.if They are built and installed directly by the RPM. If a non-RPM based system is being used, then copy the above files to their own directory and run: make -f /usr/share/selinux/devel/Makefile semodule -i cachefilesd.pp You will need checkpolicy and selinux-policy-devel installed prior to the build. By default, the cache is located in /var/fscache, but if it is desirable that it should be elsewhere, than either the above policy files must be altered, or an auxiliary policy must be installed to label the alternate location of the cache. For instructions on how to add an auxiliary policy to enable the cache to be located elsewhere when SELinux is in enforcing mode, please see: /usr/share/doc/cachefilesd-*/move-cache.txt When the cachefilesd rpm is installed; alternatively, the document can be found in the sources. ================== A NOTE ON SECURITY ================== CacheFiles makes use of the split security in the task_struct. It allocates its own task_security structure, and redirects current->act_as to point to it when it acts on behalf of another process, in that process's context. The reason it does this is that it calls vfs_mkdir() and suchlike rather than bypassing security and calling inode ops directly. Therefore the VFS and LSM may deny the CacheFiles access to the cache data because under some circumstances the caching code is running in the security context of whatever process issued the original syscall on the netfs. Furthermore, should CacheFiles create a file or directory, the security parameters with that object is created (UID, GID, security label) would be derived from that process that issued the system call, thus potentially preventing other processes from accessing the cache - including CacheFiles's cache management daemon (cachefilesd). What is required is to temporarily override the security of the process that issued the system call. We can't, however, just do an in-place change of the security data as that affects the process as an object, not just as a subject. This means it may lose signals or ptrace events for example, and affects what the process looks like in /proc. So CacheFiles makes use of a logical split in the security between the objective security (task->sec) and the subjective security (task->act_as). The objective security holds the intrinsic security properties of a process and is never overridden. This is what appears in /proc, and is what is used when a process is the target of an operation by some other process (SIGKILL for example). The subjective security holds the active security properties of a process, and may be overridden. This is not seen externally, and is used whan a process acts upon another object, for example SIGKILLing another process or opening a file. LSM hooks exist that allow SELinux (or Smack or whatever) to reject a request for CacheFiles to run in a context of a specific security label, or to create files and directories with another security label. This documentation is added by the patch to: Documentation/filesystems/caching/cachefiles.txt Signed-Off-By: David Howells <dhowells@redhat.com> Acked-by: Steve Dickson <steved@redhat.com> Acked-by: Trond Myklebust <Trond.Myklebust@netapp.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Tested-by: Daire Byrne <Daire.Byrne@framestore.com>
755 lines
16 KiB
C
755 lines
16 KiB
C
/* Daemon interface
|
|
*
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public Licence
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the Licence, or (at your option) any later version.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/completion.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/file.h>
|
|
#include <linux/namei.h>
|
|
#include <linux/poll.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/statfs.h>
|
|
#include <linux/ctype.h>
|
|
#include <linux/fs_struct.h>
|
|
#include "internal.h"
|
|
|
|
static int cachefiles_daemon_open(struct inode *, struct file *);
|
|
static int cachefiles_daemon_release(struct inode *, struct file *);
|
|
static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
|
|
loff_t *);
|
|
static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
|
|
size_t, loff_t *);
|
|
static unsigned int cachefiles_daemon_poll(struct file *,
|
|
struct poll_table_struct *);
|
|
static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
|
|
static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
|
|
|
|
static unsigned long cachefiles_open;
|
|
|
|
const struct file_operations cachefiles_daemon_fops = {
|
|
.owner = THIS_MODULE,
|
|
.open = cachefiles_daemon_open,
|
|
.release = cachefiles_daemon_release,
|
|
.read = cachefiles_daemon_read,
|
|
.write = cachefiles_daemon_write,
|
|
.poll = cachefiles_daemon_poll,
|
|
};
|
|
|
|
struct cachefiles_daemon_cmd {
|
|
char name[8];
|
|
int (*handler)(struct cachefiles_cache *cache, char *args);
|
|
};
|
|
|
|
static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
|
|
{ "bind", cachefiles_daemon_bind },
|
|
{ "brun", cachefiles_daemon_brun },
|
|
{ "bcull", cachefiles_daemon_bcull },
|
|
{ "bstop", cachefiles_daemon_bstop },
|
|
{ "cull", cachefiles_daemon_cull },
|
|
{ "debug", cachefiles_daemon_debug },
|
|
{ "dir", cachefiles_daemon_dir },
|
|
{ "frun", cachefiles_daemon_frun },
|
|
{ "fcull", cachefiles_daemon_fcull },
|
|
{ "fstop", cachefiles_daemon_fstop },
|
|
{ "inuse", cachefiles_daemon_inuse },
|
|
{ "secctx", cachefiles_daemon_secctx },
|
|
{ "tag", cachefiles_daemon_tag },
|
|
{ "", NULL }
|
|
};
|
|
|
|
|
|
/*
|
|
* do various checks
|
|
*/
|
|
static int cachefiles_daemon_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct cachefiles_cache *cache;
|
|
|
|
_enter("");
|
|
|
|
/* only the superuser may do this */
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
|
|
/* the cachefiles device may only be open once at a time */
|
|
if (xchg(&cachefiles_open, 1) == 1)
|
|
return -EBUSY;
|
|
|
|
/* allocate a cache record */
|
|
cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
|
|
if (!cache) {
|
|
cachefiles_open = 0;
|
|
return -ENOMEM;
|
|
}
|
|
|
|
mutex_init(&cache->daemon_mutex);
|
|
cache->active_nodes = RB_ROOT;
|
|
rwlock_init(&cache->active_lock);
|
|
init_waitqueue_head(&cache->daemon_pollwq);
|
|
|
|
/* set default caching limits
|
|
* - limit at 1% free space and/or free files
|
|
* - cull below 5% free space and/or free files
|
|
* - cease culling above 7% free space and/or free files
|
|
*/
|
|
cache->frun_percent = 7;
|
|
cache->fcull_percent = 5;
|
|
cache->fstop_percent = 1;
|
|
cache->brun_percent = 7;
|
|
cache->bcull_percent = 5;
|
|
cache->bstop_percent = 1;
|
|
|
|
file->private_data = cache;
|
|
cache->cachefilesd = file;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* release a cache
|
|
*/
|
|
static int cachefiles_daemon_release(struct inode *inode, struct file *file)
|
|
{
|
|
struct cachefiles_cache *cache = file->private_data;
|
|
|
|
_enter("");
|
|
|
|
ASSERT(cache);
|
|
|
|
set_bit(CACHEFILES_DEAD, &cache->flags);
|
|
|
|
cachefiles_daemon_unbind(cache);
|
|
|
|
ASSERT(!cache->active_nodes.rb_node);
|
|
|
|
/* clean up the control file interface */
|
|
cache->cachefilesd = NULL;
|
|
file->private_data = NULL;
|
|
cachefiles_open = 0;
|
|
|
|
kfree(cache);
|
|
|
|
_leave("");
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* read the cache state
|
|
*/
|
|
static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
|
|
size_t buflen, loff_t *pos)
|
|
{
|
|
struct cachefiles_cache *cache = file->private_data;
|
|
char buffer[256];
|
|
int n;
|
|
|
|
//_enter(",,%zu,", buflen);
|
|
|
|
if (!test_bit(CACHEFILES_READY, &cache->flags))
|
|
return 0;
|
|
|
|
/* check how much space the cache has */
|
|
cachefiles_has_space(cache, 0, 0);
|
|
|
|
/* summarise */
|
|
clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
|
|
|
|
n = snprintf(buffer, sizeof(buffer),
|
|
"cull=%c"
|
|
" frun=%llx"
|
|
" fcull=%llx"
|
|
" fstop=%llx"
|
|
" brun=%llx"
|
|
" bcull=%llx"
|
|
" bstop=%llx",
|
|
test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
|
|
(unsigned long long) cache->frun,
|
|
(unsigned long long) cache->fcull,
|
|
(unsigned long long) cache->fstop,
|
|
(unsigned long long) cache->brun,
|
|
(unsigned long long) cache->bcull,
|
|
(unsigned long long) cache->bstop
|
|
);
|
|
|
|
if (n > buflen)
|
|
return -EMSGSIZE;
|
|
|
|
if (copy_to_user(_buffer, buffer, n) != 0)
|
|
return -EFAULT;
|
|
|
|
return n;
|
|
}
|
|
|
|
/*
|
|
* command the cache
|
|
*/
|
|
static ssize_t cachefiles_daemon_write(struct file *file,
|
|
const char __user *_data,
|
|
size_t datalen,
|
|
loff_t *pos)
|
|
{
|
|
const struct cachefiles_daemon_cmd *cmd;
|
|
struct cachefiles_cache *cache = file->private_data;
|
|
ssize_t ret;
|
|
char *data, *args, *cp;
|
|
|
|
//_enter(",,%zu,", datalen);
|
|
|
|
ASSERT(cache);
|
|
|
|
if (test_bit(CACHEFILES_DEAD, &cache->flags))
|
|
return -EIO;
|
|
|
|
if (datalen < 0 || datalen > PAGE_SIZE - 1)
|
|
return -EOPNOTSUPP;
|
|
|
|
/* drag the command string into the kernel so we can parse it */
|
|
data = kmalloc(datalen + 1, GFP_KERNEL);
|
|
if (!data)
|
|
return -ENOMEM;
|
|
|
|
ret = -EFAULT;
|
|
if (copy_from_user(data, _data, datalen) != 0)
|
|
goto error;
|
|
|
|
data[datalen] = '\0';
|
|
|
|
ret = -EINVAL;
|
|
if (memchr(data, '\0', datalen))
|
|
goto error;
|
|
|
|
/* strip any newline */
|
|
cp = memchr(data, '\n', datalen);
|
|
if (cp) {
|
|
if (cp == data)
|
|
goto error;
|
|
|
|
*cp = '\0';
|
|
}
|
|
|
|
/* parse the command */
|
|
ret = -EOPNOTSUPP;
|
|
|
|
for (args = data; *args; args++)
|
|
if (isspace(*args))
|
|
break;
|
|
if (*args) {
|
|
if (args == data)
|
|
goto error;
|
|
*args = '\0';
|
|
for (args++; isspace(*args); args++)
|
|
continue;
|
|
}
|
|
|
|
/* run the appropriate command handler */
|
|
for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
|
|
if (strcmp(cmd->name, data) == 0)
|
|
goto found_command;
|
|
|
|
error:
|
|
kfree(data);
|
|
//_leave(" = %zd", ret);
|
|
return ret;
|
|
|
|
found_command:
|
|
mutex_lock(&cache->daemon_mutex);
|
|
|
|
ret = -EIO;
|
|
if (!test_bit(CACHEFILES_DEAD, &cache->flags))
|
|
ret = cmd->handler(cache, args);
|
|
|
|
mutex_unlock(&cache->daemon_mutex);
|
|
|
|
if (ret == 0)
|
|
ret = datalen;
|
|
goto error;
|
|
}
|
|
|
|
/*
|
|
* poll for culling state
|
|
* - use POLLOUT to indicate culling state
|
|
*/
|
|
static unsigned int cachefiles_daemon_poll(struct file *file,
|
|
struct poll_table_struct *poll)
|
|
{
|
|
struct cachefiles_cache *cache = file->private_data;
|
|
unsigned int mask;
|
|
|
|
poll_wait(file, &cache->daemon_pollwq, poll);
|
|
mask = 0;
|
|
|
|
if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
|
|
mask |= POLLIN;
|
|
|
|
if (test_bit(CACHEFILES_CULLING, &cache->flags))
|
|
mask |= POLLOUT;
|
|
|
|
return mask;
|
|
}
|
|
|
|
/*
|
|
* give a range error for cache space constraints
|
|
* - can be tail-called
|
|
*/
|
|
static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
|
|
char *args)
|
|
{
|
|
kerror("Free space limits must be in range"
|
|
" 0%%<=stop<cull<run<100%%");
|
|
|
|
return -EINVAL;
|
|
}
|
|
|
|
/*
|
|
* set the percentage of files at which to stop culling
|
|
* - command: "frun <N>%"
|
|
*/
|
|
static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long frun;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args)
|
|
return -EINVAL;
|
|
|
|
frun = simple_strtoul(args, &args, 10);
|
|
if (args[0] != '%' || args[1] != '\0')
|
|
return -EINVAL;
|
|
|
|
if (frun <= cache->fcull_percent || frun >= 100)
|
|
return cachefiles_daemon_range_error(cache, args);
|
|
|
|
cache->frun_percent = frun;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the percentage of files at which to start culling
|
|
* - command: "fcull <N>%"
|
|
*/
|
|
static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long fcull;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args)
|
|
return -EINVAL;
|
|
|
|
fcull = simple_strtoul(args, &args, 10);
|
|
if (args[0] != '%' || args[1] != '\0')
|
|
return -EINVAL;
|
|
|
|
if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
|
|
return cachefiles_daemon_range_error(cache, args);
|
|
|
|
cache->fcull_percent = fcull;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the percentage of files at which to stop allocating
|
|
* - command: "fstop <N>%"
|
|
*/
|
|
static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long fstop;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args)
|
|
return -EINVAL;
|
|
|
|
fstop = simple_strtoul(args, &args, 10);
|
|
if (args[0] != '%' || args[1] != '\0')
|
|
return -EINVAL;
|
|
|
|
if (fstop < 0 || fstop >= cache->fcull_percent)
|
|
return cachefiles_daemon_range_error(cache, args);
|
|
|
|
cache->fstop_percent = fstop;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the percentage of blocks at which to stop culling
|
|
* - command: "brun <N>%"
|
|
*/
|
|
static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long brun;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args)
|
|
return -EINVAL;
|
|
|
|
brun = simple_strtoul(args, &args, 10);
|
|
if (args[0] != '%' || args[1] != '\0')
|
|
return -EINVAL;
|
|
|
|
if (brun <= cache->bcull_percent || brun >= 100)
|
|
return cachefiles_daemon_range_error(cache, args);
|
|
|
|
cache->brun_percent = brun;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the percentage of blocks at which to start culling
|
|
* - command: "bcull <N>%"
|
|
*/
|
|
static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long bcull;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args)
|
|
return -EINVAL;
|
|
|
|
bcull = simple_strtoul(args, &args, 10);
|
|
if (args[0] != '%' || args[1] != '\0')
|
|
return -EINVAL;
|
|
|
|
if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
|
|
return cachefiles_daemon_range_error(cache, args);
|
|
|
|
cache->bcull_percent = bcull;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the percentage of blocks at which to stop allocating
|
|
* - command: "bstop <N>%"
|
|
*/
|
|
static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long bstop;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args)
|
|
return -EINVAL;
|
|
|
|
bstop = simple_strtoul(args, &args, 10);
|
|
if (args[0] != '%' || args[1] != '\0')
|
|
return -EINVAL;
|
|
|
|
if (bstop < 0 || bstop >= cache->bcull_percent)
|
|
return cachefiles_daemon_range_error(cache, args);
|
|
|
|
cache->bstop_percent = bstop;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the cache directory
|
|
* - command: "dir <name>"
|
|
*/
|
|
static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
char *dir;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args) {
|
|
kerror("Empty directory specified");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (cache->rootdirname) {
|
|
kerror("Second cache directory specified");
|
|
return -EEXIST;
|
|
}
|
|
|
|
dir = kstrdup(args, GFP_KERNEL);
|
|
if (!dir)
|
|
return -ENOMEM;
|
|
|
|
cache->rootdirname = dir;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the cache security context
|
|
* - command: "secctx <ctx>"
|
|
*/
|
|
static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
char *secctx;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args) {
|
|
kerror("Empty security context specified");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (cache->secctx) {
|
|
kerror("Second security context specified");
|
|
return -EINVAL;
|
|
}
|
|
|
|
secctx = kstrdup(args, GFP_KERNEL);
|
|
if (!secctx)
|
|
return -ENOMEM;
|
|
|
|
cache->secctx = secctx;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* set the cache tag
|
|
* - command: "tag <name>"
|
|
*/
|
|
static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
char *tag;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (!*args) {
|
|
kerror("Empty tag specified");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (cache->tag)
|
|
return -EEXIST;
|
|
|
|
tag = kstrdup(args, GFP_KERNEL);
|
|
if (!tag)
|
|
return -ENOMEM;
|
|
|
|
cache->tag = tag;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* request a node in the cache be culled from the current working directory
|
|
* - command: "cull <name>"
|
|
*/
|
|
static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
struct fs_struct *fs;
|
|
struct dentry *dir;
|
|
const struct cred *saved_cred;
|
|
int ret;
|
|
|
|
_enter(",%s", args);
|
|
|
|
if (strchr(args, '/'))
|
|
goto inval;
|
|
|
|
if (!test_bit(CACHEFILES_READY, &cache->flags)) {
|
|
kerror("cull applied to unready cache");
|
|
return -EIO;
|
|
}
|
|
|
|
if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
|
|
kerror("cull applied to dead cache");
|
|
return -EIO;
|
|
}
|
|
|
|
/* extract the directory dentry from the cwd */
|
|
fs = current->fs;
|
|
read_lock(&fs->lock);
|
|
dir = dget(fs->pwd.dentry);
|
|
read_unlock(&fs->lock);
|
|
|
|
if (!S_ISDIR(dir->d_inode->i_mode))
|
|
goto notdir;
|
|
|
|
cachefiles_begin_secure(cache, &saved_cred);
|
|
ret = cachefiles_cull(cache, dir, args);
|
|
cachefiles_end_secure(cache, saved_cred);
|
|
|
|
dput(dir);
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
|
|
notdir:
|
|
dput(dir);
|
|
kerror("cull command requires dirfd to be a directory");
|
|
return -ENOTDIR;
|
|
|
|
inval:
|
|
kerror("cull command requires dirfd and filename");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/*
|
|
* set debugging mode
|
|
* - command: "debug <mask>"
|
|
*/
|
|
static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
unsigned long mask;
|
|
|
|
_enter(",%s", args);
|
|
|
|
mask = simple_strtoul(args, &args, 0);
|
|
if (args[0] != '\0')
|
|
goto inval;
|
|
|
|
cachefiles_debug = mask;
|
|
_leave(" = 0");
|
|
return 0;
|
|
|
|
inval:
|
|
kerror("debug command requires mask");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/*
|
|
* find out whether an object in the current working directory is in use or not
|
|
* - command: "inuse <name>"
|
|
*/
|
|
static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
|
|
{
|
|
struct fs_struct *fs;
|
|
struct dentry *dir;
|
|
const struct cred *saved_cred;
|
|
int ret;
|
|
|
|
//_enter(",%s", args);
|
|
|
|
if (strchr(args, '/'))
|
|
goto inval;
|
|
|
|
if (!test_bit(CACHEFILES_READY, &cache->flags)) {
|
|
kerror("inuse applied to unready cache");
|
|
return -EIO;
|
|
}
|
|
|
|
if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
|
|
kerror("inuse applied to dead cache");
|
|
return -EIO;
|
|
}
|
|
|
|
/* extract the directory dentry from the cwd */
|
|
fs = current->fs;
|
|
read_lock(&fs->lock);
|
|
dir = dget(fs->pwd.dentry);
|
|
read_unlock(&fs->lock);
|
|
|
|
if (!S_ISDIR(dir->d_inode->i_mode))
|
|
goto notdir;
|
|
|
|
cachefiles_begin_secure(cache, &saved_cred);
|
|
ret = cachefiles_check_in_use(cache, dir, args);
|
|
cachefiles_end_secure(cache, saved_cred);
|
|
|
|
dput(dir);
|
|
//_leave(" = %d", ret);
|
|
return ret;
|
|
|
|
notdir:
|
|
dput(dir);
|
|
kerror("inuse command requires dirfd to be a directory");
|
|
return -ENOTDIR;
|
|
|
|
inval:
|
|
kerror("inuse command requires dirfd and filename");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/*
|
|
* see if we have space for a number of pages and/or a number of files in the
|
|
* cache
|
|
*/
|
|
int cachefiles_has_space(struct cachefiles_cache *cache,
|
|
unsigned fnr, unsigned bnr)
|
|
{
|
|
struct kstatfs stats;
|
|
int ret;
|
|
|
|
//_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
|
|
// (unsigned long long) cache->frun,
|
|
// (unsigned long long) cache->fcull,
|
|
// (unsigned long long) cache->fstop,
|
|
// (unsigned long long) cache->brun,
|
|
// (unsigned long long) cache->bcull,
|
|
// (unsigned long long) cache->bstop,
|
|
// fnr, bnr);
|
|
|
|
/* find out how many pages of blockdev are available */
|
|
memset(&stats, 0, sizeof(stats));
|
|
|
|
ret = vfs_statfs(cache->mnt->mnt_root, &stats);
|
|
if (ret < 0) {
|
|
if (ret == -EIO)
|
|
cachefiles_io_error(cache, "statfs failed");
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|
|
|
|
stats.f_bavail >>= cache->bshift;
|
|
|
|
//_debug("avail %llu,%llu",
|
|
// (unsigned long long) stats.f_ffree,
|
|
// (unsigned long long) stats.f_bavail);
|
|
|
|
/* see if there is sufficient space */
|
|
if (stats.f_ffree > fnr)
|
|
stats.f_ffree -= fnr;
|
|
else
|
|
stats.f_ffree = 0;
|
|
|
|
if (stats.f_bavail > bnr)
|
|
stats.f_bavail -= bnr;
|
|
else
|
|
stats.f_bavail = 0;
|
|
|
|
ret = -ENOBUFS;
|
|
if (stats.f_ffree < cache->fstop ||
|
|
stats.f_bavail < cache->bstop)
|
|
goto begin_cull;
|
|
|
|
ret = 0;
|
|
if (stats.f_ffree < cache->fcull ||
|
|
stats.f_bavail < cache->bcull)
|
|
goto begin_cull;
|
|
|
|
if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
|
|
stats.f_ffree >= cache->frun &&
|
|
stats.f_bavail >= cache->brun &&
|
|
test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
|
|
) {
|
|
_debug("cease culling");
|
|
cachefiles_state_changed(cache);
|
|
}
|
|
|
|
//_leave(" = 0");
|
|
return 0;
|
|
|
|
begin_cull:
|
|
if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
|
|
_debug("### CULL CACHE ###");
|
|
cachefiles_state_changed(cache);
|
|
}
|
|
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|