// SPDX-License-Identifier: GPL-2.0-or-later /* CacheFiles path walking and related routines * * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com)
*/
/* * Mark the backing file as being a cache file if it's not already in use. The * mark tells the culling request command that it's not allowed to cull the * file or directory. The caller must hold the inode lock.
*/ staticbool __cachefiles_mark_inode_in_use(struct cachefiles_object *object, struct inode *inode)
{ bool can_use = false;
/* we need to create the subdir if it doesn't exist yet */ if (d_is_negative(subdir)) {
ret = cachefiles_has_space(cache, 1, 0,
cachefiles_has_space_for_create); if (ret < 0) goto mkdir_error;
_debug("attempt mkdir");
path.mnt = cache->mnt;
path.dentry = dir;
ret = security_path_mkdir(&path, subdir, 0700); if (ret < 0) goto mkdir_error;
ret = cachefiles_inject_write_error(); if (ret == 0)
subdir = vfs_mkdir(&nop_mnt_idmap, d_inode(dir), subdir, 0700); else
subdir = ERR_PTR(ret); if (IS_ERR(subdir)) {
trace_cachefiles_vfs_error(NULL, d_inode(dir), ret,
cachefiles_trace_mkdir_error); goto mkdir_error;
}
trace_cachefiles_mkdir(dir, subdir);
if (unlikely(d_unhashed(subdir) || d_is_negative(subdir))) {
dput(subdir); goto retry;
}
ASSERT(d_backing_inode(subdir));
/* non-directories can just be unlinked */ if (!d_is_dir(rep)) {
dget(rep); /* Stop the dentry being negated if it's only pinned * by a file struct.
*/
ret = cachefiles_unlink(cache, object, dir, rep, why);
dput(rep);
/* directories have to be moved to the graveyard */
_debug("move stale object to graveyard");
inode_unlock(d_inode(dir));
try_again: /* first step is to make up a grave dentry in the graveyard */
sprintf(nbuffer, "%08x%08x",
(uint32_t) ktime_get_real_seconds(),
(uint32_t) atomic_inc_return(&cache->gravecounter));
/* do the multiway lock magic */
trap = lock_rename(cache->graveyard, dir); if (IS_ERR(trap)) return PTR_ERR(trap);
/* do some checks before getting the grave dentry */ if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) { /* the entry was probably culled when we dropped the parent dir
* lock */
unlock_rename(cache->graveyard, dir);
_leave(" = 0 [culled?]"); return 0;
}
if (!d_can_lookup(cache->graveyard)) {
unlock_rename(cache->graveyard, dir);
cachefiles_io_error(cache, "Graveyard no longer a directory"); return -EIO;
}
if (trap == rep) {
unlock_rename(cache->graveyard, dir);
cachefiles_io_error(cache, "May not make directory loop"); return -EIO;
}
if (d_mountpoint(rep)) {
unlock_rename(cache->graveyard, dir);
cachefiles_io_error(cache, "Mountpoint in cache"); return -EIO;
}
if (d_mountpoint(grave)) {
unlock_rename(cache->graveyard, dir);
dput(grave);
cachefiles_io_error(cache, "Mountpoint in graveyard"); return -EIO;
}
/* target should not be an ancestor of source */ if (trap == grave) {
unlock_rename(cache->graveyard, dir);
dput(grave);
cachefiles_io_error(cache, "May not make directory loop"); return -EIO;
}
/* attempt the rename */
path.mnt = cache->mnt;
path.dentry = dir;
path_to_graveyard.mnt = cache->mnt;
path_to_graveyard.dentry = cache->graveyard;
ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0); if (ret < 0) {
cachefiles_io_error(cache, "Rename security error %d", ret);
} else { struct renamedata rd = {
.old_mnt_idmap = &nop_mnt_idmap,
.old_parent = dir,
.old_dentry = rep,
.new_mnt_idmap = &nop_mnt_idmap,
.new_parent = cache->graveyard,
.new_dentry = grave,
};
trace_cachefiles_rename(object, d_inode(rep)->i_ino, why);
ret = cachefiles_inject_read_error(); if (ret == 0)
ret = vfs_rename(&rd); if (ret != 0)
trace_cachefiles_vfs_error(object, d_inode(dir), ret,
cachefiles_trace_rename_error); if (ret != 0 && ret != -ENOMEM)
cachefiles_io_error(cache, "Rename failed with error %d", ret);
}
if (ni_size > 0) {
trace_cachefiles_trunc(object, file_inode(file), 0, ni_size,
cachefiles_trunc_expand_tmpfile);
ret = cachefiles_inject_write_error(); if (ret == 0)
ret = vfs_truncate(&file->f_path, ni_size); if (ret < 0) {
trace_cachefiles_vfs_error(
object, file_inode(file), ret,
cachefiles_trace_trunc_error); goto err_unuse;
}
}
ret = -EINVAL; if (unlikely(!file->f_op->read_iter) ||
unlikely(!file->f_op->write_iter)) {
fput(file);
pr_notice("Cache does not support read_iter and write_iter\n"); goto err_unuse;
}
out:
cachefiles_end_secure(cache, saved_cred); return file;
/* * Open an existing file, checking its attributes and replacing it if it is * stale.
*/ staticbool cachefiles_open_file(struct cachefiles_object *object, struct dentry *dentry)
{ struct cachefiles_cache *cache = object->volume->cache; struct file *file; struct path path; int ret;
_enter("%pd", dentry);
if (!cachefiles_mark_inode_in_use(object, d_inode(dentry))) {
pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n",
dentry, d_inode(dentry)->i_ino); returnfalse;
}
/* We need to open a file interface onto a data file now as we can't do * it on demand because writeback called from do_exit() sees * current->fs == NULL - which breaks d_path() called from ext4 open.
*/
path.mnt = cache->mnt;
path.dentry = dentry;
file = kernel_file_open(&path, O_RDWR | O_LARGEFILE | O_DIRECT, cache->cache_cred); if (IS_ERR(file)) {
trace_cachefiles_vfs_error(object, d_backing_inode(dentry),
PTR_ERR(file),
cachefiles_trace_open_error); goto error;
}
if (unlikely(!file->f_op->read_iter) ||
unlikely(!file->f_op->write_iter)) {
pr_notice("Cache does not support read_iter and write_iter\n"); goto error_fput;
}
_debug("file -> %pd positive", dentry);
ret = cachefiles_ondemand_init_object(object); if (ret < 0) goto error_fput;
ret = cachefiles_check_auxdata(object, file); if (ret < 0) goto check_failed;
/* Always update the atime on an object we've just looked up (this is * used to keep track of culling, and atimes are only updated by read, * write and readdir but not lookup or open).
*/
touch_atime(&file->f_path); returntrue;
/* * walk from the parent object to the child object through the backing * filesystem, creating directories as we go
*/ bool cachefiles_look_up_object(struct cachefiles_object *object)
{ struct cachefiles_volume *volume = object->volume; struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash]; int ret;
/* Look up path "cache/vol/fanout/file". */
ret = cachefiles_inject_read_error(); if (ret == 0)
dentry = lookup_one_positive_unlocked(&nop_mnt_idmap,
&QSTR(object->d_name), fan); else
dentry = ERR_PTR(ret);
trace_cachefiles_lookup(object, fan, dentry); if (IS_ERR(dentry)) { if (dentry == ERR_PTR(-ENOENT)) goto new_file; if (dentry == ERR_PTR(-EIO))
cachefiles_io_error_obj(object, "Lookup failed"); returnfalse;
}
if (!d_is_reg(dentry)) {
pr_err("%pd is not a file\n", dentry);
inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
ret = cachefiles_bury_object(volume->cache, object, fan, dentry,
FSCACHE_OBJECT_IS_WEIRD);
dput(dentry); if (ret < 0) returnfalse; goto new_file;
}
ret = cachefiles_open_file(object, dentry);
dput(dentry); if (!ret) returnfalse;
_leave(" = t [%lu]", file_inode(object->file)->i_ino); returntrue;
/* * Attempt to link a temporary file into its rightful place in the cache.
*/ bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache, struct cachefiles_object *object)
{ struct cachefiles_volume *volume = object->volume; struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash]; bool success = false; int ret;
ret = cachefiles_inject_read_error(); if (ret == 0)
ret = vfs_link(object->file->f_path.dentry, &nop_mnt_idmap,
d_inode(fan), dentry, NULL); if (ret < 0) {
trace_cachefiles_vfs_error(object, d_inode(fan), ret,
cachefiles_trace_link_error);
_debug("link fail %d", ret);
} else {
trace_cachefiles_link(object, file_inode(object->file));
spin_lock(&object->lock); /* TODO: Do we want to switch the file pointer to the new dentry? */
clear_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
spin_unlock(&object->lock);
success = true;
}
/* * Look up an inode to be checked or culled. Return -EBUSY if the inode is * marked in use.
*/ staticstruct dentry *cachefiles_lookup_for_cull(struct cachefiles_cache *cache, struct dentry *dir, char *filename)
{ struct dentry *victim; int ret = -ENOENT;
inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
victim = lookup_one(&nop_mnt_idmap, &QSTR(filename), dir); if (IS_ERR(victim)) goto lookup_error; if (d_is_negative(victim)) goto lookup_put; if (d_inode(victim)->i_flags & S_KERNEL_FILE) goto lookup_busy; return victim;
lookup_busy:
ret = -EBUSY;
lookup_put:
inode_unlock(d_inode(dir));
dput(victim); return ERR_PTR(ret);
lookup_error:
inode_unlock(d_inode(dir));
ret = PTR_ERR(victim); if (ret == -ENOENT) return ERR_PTR(-ESTALE); /* Probably got retired by the netfs */
/* * Cull an object if it's not in use * - called only by cache manager daemon
*/ int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir, char *filename)
{ struct dentry *victim; struct inode *inode; int ret;
_enter(",%pd/,%s", dir, filename);
victim = cachefiles_lookup_for_cull(cache, dir, filename); if (IS_ERR(victim)) return PTR_ERR(victim);
/* check to see if someone is using this object */
inode = d_inode(victim);
inode_lock(inode); if (inode->i_flags & S_KERNEL_FILE) {
ret = -EBUSY;
} else { /* Stop the cache from picking it back up */
inode->i_flags |= S_KERNEL_FILE;
ret = 0;
}
inode_unlock(inode); if (ret < 0) goto error_unlock;
ret = cachefiles_bury_object(cache, NULL, dir, victim,
FSCACHE_OBJECT_WAS_CULLED); if (ret < 0) goto error;
error_unlock:
inode_unlock(d_inode(dir));
error:
dput(victim); if (ret == -ENOENT) return -ESTALE; /* Probably got retired by the netfs */
if (ret != -ENOMEM) {
pr_err("Internal error: %d\n", ret);
ret = -EIO;
}
_leave(" = %d", ret); return ret;
}
/* * Find out if an object is in use or not * - called only by cache manager daemon * - returns -EBUSY or 0 to indicate whether an object is in use or not
*/ int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir, char *filename)
{ struct dentry *victim; int ret = 0;
victim = cachefiles_lookup_for_cull(cache, dir, filename); if (IS_ERR(victim)) return PTR_ERR(victim);
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.