Commit ea5dc046 authored by Jeffle Xu's avatar Jeffle Xu Committed by David Howells

cachefiles: unmark inode in use in error path

Unmark inode in use if error encountered. If the in-use flag leakage
occurs in cachefiles_open_file(), Cachefiles will complain "Inode
already in use" when later another cookie with the same index key is
looked up.

If the in-use flag leakage occurs in cachefiles_create_tmpfile(), though
the "Inode already in use" warning won't be triggered, fix the leakage
anyway.
Reported-by: default avatarGao Xiang <hsiangkao@linux.alibaba.com>
Fixes: 1f08c925 ("cachefiles: Implement backing file wrangling")
Signed-off-by: default avatarJeffle Xu <jefflexu@linux.alibaba.com>
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
cc: linux-cachefs@redhat.com
Link: https://listman.redhat.com/archives/linux-cachefs/2022-March/006615.html # v1
Link: https://listman.redhat.com/archives/linux-cachefs/2022-March/006618.html # v2
parent 42e7a03d
...@@ -57,6 +57,16 @@ static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object, ...@@ -57,6 +57,16 @@ static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
trace_cachefiles_mark_inactive(object, inode); trace_cachefiles_mark_inactive(object, inode);
} }
static void cachefiles_do_unmark_inode_in_use(struct cachefiles_object *object,
struct dentry *dentry)
{
struct inode *inode = d_backing_inode(dentry);
inode_lock(inode);
__cachefiles_unmark_inode_in_use(object, dentry);
inode_unlock(inode);
}
/* /*
* Unmark a backing inode and tell cachefilesd that there's something that can * Unmark a backing inode and tell cachefilesd that there's something that can
* be culled. * be culled.
...@@ -68,9 +78,7 @@ void cachefiles_unmark_inode_in_use(struct cachefiles_object *object, ...@@ -68,9 +78,7 @@ void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
if (inode) { if (inode) {
inode_lock(inode); cachefiles_do_unmark_inode_in_use(object, file->f_path.dentry);
__cachefiles_unmark_inode_in_use(object, file->f_path.dentry);
inode_unlock(inode);
if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) { if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
atomic_long_add(inode->i_blocks, &cache->b_released); atomic_long_add(inode->i_blocks, &cache->b_released);
...@@ -484,7 +492,7 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) ...@@ -484,7 +492,7 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
object, d_backing_inode(path.dentry), ret, object, d_backing_inode(path.dentry), ret,
cachefiles_trace_trunc_error); cachefiles_trace_trunc_error);
file = ERR_PTR(ret); file = ERR_PTR(ret);
goto out_dput; goto out_unuse;
} }
} }
...@@ -494,15 +502,20 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) ...@@ -494,15 +502,20 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry), trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry),
PTR_ERR(file), PTR_ERR(file),
cachefiles_trace_open_error); cachefiles_trace_open_error);
goto out_dput; goto out_unuse;
} }
if (unlikely(!file->f_op->read_iter) || if (unlikely(!file->f_op->read_iter) ||
unlikely(!file->f_op->write_iter)) { unlikely(!file->f_op->write_iter)) {
fput(file); fput(file);
pr_notice("Cache does not support read_iter and write_iter\n"); pr_notice("Cache does not support read_iter and write_iter\n");
file = ERR_PTR(-EINVAL); file = ERR_PTR(-EINVAL);
goto out_unuse;
} }
goto out_dput;
out_unuse:
cachefiles_do_unmark_inode_in_use(object, path.dentry);
out_dput: out_dput:
dput(path.dentry); dput(path.dentry);
out: out:
...@@ -590,14 +603,16 @@ static bool cachefiles_open_file(struct cachefiles_object *object, ...@@ -590,14 +603,16 @@ static bool cachefiles_open_file(struct cachefiles_object *object,
check_failed: check_failed:
fscache_cookie_lookup_negative(object->cookie); fscache_cookie_lookup_negative(object->cookie);
cachefiles_unmark_inode_in_use(object, file); cachefiles_unmark_inode_in_use(object, file);
if (ret == -ESTALE) { fput(file);
fput(file); dput(dentry);
dput(dentry); if (ret == -ESTALE)
return cachefiles_create_file(object); return cachefiles_create_file(object);
} return false;
error_fput: error_fput:
fput(file); fput(file);
error: error:
cachefiles_do_unmark_inode_in_use(object, dentry);
dput(dentry); dput(dentry);
return false; return false;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment