// SPDX-License-Identifier: GPL-2.0-or-later /* Daemon interface * * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com)
*/
/* * Make sure the following two operations won't be reordered. * 1) set CACHEFILES_DEAD bit * 2) flush requests in the xarray * Otherwise the request may be enqueued after xarray has been * flushed, leaving the orphan request never being completed. * * CPU 1 CPU 2 * ===== ===== * flush requests in the xarray * test CACHEFILES_DEAD bit * enqueue the request * set CACHEFILES_DEAD bit
*/
smp_mb();
/* * Take a command from cachefilesd, parse it and act on it.
*/ static ssize_t cachefiles_daemon_write(struct file *file, constchar __user *_data,
size_t datalen,
loff_t *pos)
{ conststruct 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 > PAGE_SIZE - 1) return -EOPNOTSUPP;
/* drag the command string into the kernel so we can parse it */
data = memdup_user_nul(_data, datalen); if (IS_ERR(data)) return PTR_ERR(data);
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';
args = skip_spaces(++args);
}
/* run the appropriate command handler */ for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++) if (strcmp(cmd->name, data) == 0) goto found_command;
if (cachefiles_in_ondemand_mode(cache)) { if (!xa_empty(&cache->reqs)) {
xas_lock(&xas);
xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { if (!cachefiles_ondemand_is_reopening_read(req)) {
mask |= EPOLLIN; break;
}
}
xas_unlock(&xas);
}
} else { if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
mask |= EPOLLIN;
}
if (test_bit(CACHEFILES_CULLING, &cache->flags))
mask |= EPOLLOUT;
return mask;
}
/* * Give a range error for cache space constraints * - can be tail-called
*/ staticint cachefiles_daemon_range_error(struct cachefiles_cache *cache, char *args)
{
pr_err("Free space limits must be in range 0%%<=stop);
return -EINVAL;
}
/* * Set the percentage of files at which to stop culling * - command: "frun <N>%"
*/ staticint cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
{ unsignedlong frun;
/* * Set the percentage of files at which to start culling * - command: "fcull <N>%"
*/ staticint cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
{ unsignedlong fcull;
/* * Set the percentage of files at which to stop allocating * - command: "fstop <N>%"
*/ staticint cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
{ unsignedlong fstop;
if (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>%"
*/ staticint cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
{ unsignedlong brun;
/* * Set the percentage of blocks at which to start culling * - command: "bcull <N>%"
*/ staticint cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
{ unsignedlong bcull;
/* * Set the percentage of blocks at which to stop allocating * - command: "bstop <N>%"
*/ staticint cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
{ unsignedlong bstop;
if (!*args) {
pr_err("Empty directory specified\n"); return -EINVAL;
}
if (cache->rootdirname) {
pr_err("Second cache directory specified\n"); return -EEXIST;
}
dir = kstrdup(args, GFP_KERNEL); if (!dir) return -ENOMEM;
cache->rootdirname = dir; return 0;
}
/* * Set the cache security context * - command: "secctx <ctx>"
*/ staticint cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
{ int err;
_enter(",%s", args);
if (!*args) {
pr_err("Empty security context specified\n"); return -EINVAL;
}
if (cache->have_secid) {
pr_err("Second security context specified\n"); return -EINVAL;
}
err = security_secctx_to_secid(args, strlen(args), &cache->secid); if (err) return err;
cache->have_secid = true; return 0;
}
/* * Set the cache tag * - command: "tag <name>"
*/ staticint cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
{ char *tag;
_enter(",%s", args);
if (!*args) {
pr_err("Empty tag specified\n"); 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>"
*/ staticint cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
{ struct path path; conststruct cred *saved_cred; int ret;
_enter(",%s", args);
if (strchr(args, '/')) goto inval;
if (!test_bit(CACHEFILES_READY, &cache->flags)) {
pr_err("cull applied to unready cache\n"); return -EIO;
}
if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
pr_err("cull applied to dead cache\n"); return -EIO;
}
get_fs_pwd(current->fs, &path);
if (!d_can_lookup(path.dentry)) goto notdir;
cachefiles_begin_secure(cache, &saved_cred);
ret = cachefiles_cull(cache, path.dentry, args);
cachefiles_end_secure(cache, saved_cred);
/* * Find out whether an object in the current working directory is in use or not * - command: "inuse <name>"
*/ staticint cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
{ struct path path; conststruct cred *saved_cred; int ret;
//_enter(",%s", args);
if (strchr(args, '/')) goto inval;
if (!test_bit(CACHEFILES_READY, &cache->flags)) {
pr_err("inuse applied to unready cache\n"); return -EIO;
}
if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
pr_err("inuse applied to dead cache\n"); return -EIO;
}
get_fs_pwd(current->fs, &path);
if (!d_can_lookup(path.dentry)) goto notdir;
cachefiles_begin_secure(cache, &saved_cred);
ret = cachefiles_check_in_use(cache, path.dentry, args);
cachefiles_end_secure(cache, saved_cred);
if (!cache->rootdirname) {
pr_err("No cache directory specified\n"); return -EINVAL;
}
/* Don't permit already bound caches to be re-bound */ if (test_bit(CACHEFILES_READY, &cache->flags)) {
pr_err("Cache already bound\n"); return -EBUSY;
}
if (IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND)) { if (!strcmp(args, "ondemand")) {
set_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
} elseif (*args) {
pr_err("Invalid argument to the 'bind' command\n"); return -EINVAL;
}
} elseif (*args) {
pr_err("'bind' command doesn't take an argument\n"); return -EINVAL;
}
/* Make sure we have copies of the tag string */ if (!cache->tag) { /* * The tag string is released by the fops->release() * function, so we don't release it on error here
*/
cache->tag = kstrdup("CacheFiles", GFP_KERNEL); if (!cache->tag) return -ENOMEM;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.