[PATCH 21/67] cachefiles: Prevent inode from going away when burying a dentry

From: David Howells
Date: Mon Oct 18 2021 - 10:56:30 EST


When deleting a file, we want to make sure that the inode doesn't get
detached from it (leading to ->d_inode being cleared) as we may still want
to touch the inode afterwards (we want to clear the belongs-to-kernel flag
and we may have the dentry referred to by a file struct).

Do this by getting an extra ref on the dentry around the vfs_unlink() call
so that d_delete() doesn't see the refcount == 1.

Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
---

fs/cachefiles/namei.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c
index 4bd31be3be30..04c767624e3d 100644
--- a/fs/cachefiles/namei.c
+++ b/fs/cachefiles/namei.c
@@ -106,7 +106,9 @@ static int cachefiles_bury_object(struct cachefiles_cache *cache,
cachefiles_io_error(cache, "Unlink security error");
} else {
trace_cachefiles_unlink(object, rep, why);
+ dget(rep);
ret = vfs_unlink(&init_user_ns, d_inode(dir), rep, NULL);
+ dput(rep);
}

inode_unlock(d_inode(dir));