mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-16 09:02:21 -04:00
VFS: introduce start_removing_dentry()
start_removing_dentry() is similar to start_removing() but instead of providing a name for lookup, the target dentry is given. start_removing_dentry() checks that the dentry is still hashed and in the parent, and if so it locks and increases the refcount so that end_removing() can be used to finish the operation. This is used in cachefiles, overlayfs, smb/server, and apparmor. There will be other users including ecryptfs. As start_removing_dentry() takes an extra reference to the dentry (to be put by end_removing()), there is no need to explicitly take an extra reference to stop d_delete() from using dentry_unlink_inode() to negate the dentry - as in cachefiles_delete_object(), and ksmbd_vfs_unlink(). cachefiles_bury_object() now gets an extra ref to the victim, which is drops. As it includes the needed end_removing() calls, the caller doesn't need them. Reviewed-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Namjae Jeon <linkinjeon@kernel.org> Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: NeilBrown <neil@brown.name> Link: https://patch.msgid.link/20251113002050.676694-9-neilb@ownmail.net Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
committed by
Christian Brauner
parent
1ead2213dd
commit
7bb1eb45e4
@@ -9,6 +9,7 @@
|
|||||||
#include <linux/mount.h>
|
#include <linux/mount.h>
|
||||||
#include <linux/xattr.h>
|
#include <linux/xattr.h>
|
||||||
#include <linux/file.h>
|
#include <linux/file.h>
|
||||||
|
#include <linux/namei.h>
|
||||||
#include <linux/falloc.h>
|
#include <linux/falloc.h>
|
||||||
#include <trace/events/fscache.h>
|
#include <trace/events/fscache.h>
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
@@ -428,11 +429,13 @@ static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie)
|
|||||||
if (!old_tmpfile) {
|
if (!old_tmpfile) {
|
||||||
struct cachefiles_volume *volume = object->volume;
|
struct cachefiles_volume *volume = object->volume;
|
||||||
struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
|
struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
|
||||||
|
struct dentry *obj;
|
||||||
|
|
||||||
inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
|
obj = start_removing_dentry(fan, old_file->f_path.dentry);
|
||||||
cachefiles_bury_object(volume->cache, object, fan,
|
if (!IS_ERR(obj))
|
||||||
old_file->f_path.dentry,
|
cachefiles_bury_object(volume->cache, object,
|
||||||
FSCACHE_OBJECT_INVALIDATED);
|
fan, obj,
|
||||||
|
FSCACHE_OBJECT_INVALIDATED);
|
||||||
}
|
}
|
||||||
fput(old_file);
|
fput(old_file);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -261,6 +261,7 @@ static int cachefiles_unlink(struct cachefiles_cache *cache,
|
|||||||
* - Directory backed objects are stuffed into the graveyard for userspace to
|
* - Directory backed objects are stuffed into the graveyard for userspace to
|
||||||
* delete
|
* delete
|
||||||
* On entry dir must be locked. It will be unlocked on exit.
|
* On entry dir must be locked. It will be unlocked on exit.
|
||||||
|
* On entry there must be at least 2 refs on rep, one will be dropped on exit.
|
||||||
*/
|
*/
|
||||||
int cachefiles_bury_object(struct cachefiles_cache *cache,
|
int cachefiles_bury_object(struct cachefiles_cache *cache,
|
||||||
struct cachefiles_object *object,
|
struct cachefiles_object *object,
|
||||||
@@ -275,12 +276,6 @@ int cachefiles_bury_object(struct cachefiles_cache *cache,
|
|||||||
|
|
||||||
_enter(",'%pd','%pd'", dir, rep);
|
_enter(",'%pd','%pd'", dir, rep);
|
||||||
|
|
||||||
/* end_removing() will dput() @rep but we need to keep
|
|
||||||
* a ref, so take one now. This also stops the dentry
|
|
||||||
* being negated when unlinked which we need.
|
|
||||||
*/
|
|
||||||
dget(rep);
|
|
||||||
|
|
||||||
if (rep->d_parent != dir) {
|
if (rep->d_parent != dir) {
|
||||||
end_removing(rep);
|
end_removing(rep);
|
||||||
_leave(" = -ESTALE");
|
_leave(" = -ESTALE");
|
||||||
@@ -425,13 +420,12 @@ int cachefiles_delete_object(struct cachefiles_object *object,
|
|||||||
|
|
||||||
_enter(",OBJ%x{%pD}", object->debug_id, object->file);
|
_enter(",OBJ%x{%pD}", object->debug_id, object->file);
|
||||||
|
|
||||||
/* Stop the dentry being negated if it's only pinned by a file struct. */
|
dentry = start_removing_dentry(fan, dentry);
|
||||||
dget(dentry);
|
if (IS_ERR(dentry))
|
||||||
|
ret = PTR_ERR(dentry);
|
||||||
inode_lock_nested(d_backing_inode(fan), I_MUTEX_PARENT);
|
else
|
||||||
ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
|
ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
|
||||||
inode_unlock(d_backing_inode(fan));
|
end_removing(dentry);
|
||||||
dput(dentry);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -644,9 +638,13 @@ bool cachefiles_look_up_object(struct cachefiles_object *object)
|
|||||||
|
|
||||||
if (!d_is_reg(dentry)) {
|
if (!d_is_reg(dentry)) {
|
||||||
pr_err("%pd is not a file\n", dentry);
|
pr_err("%pd is not a file\n", dentry);
|
||||||
inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
|
struct dentry *de = start_removing_dentry(fan, dentry);
|
||||||
ret = cachefiles_bury_object(volume->cache, object, fan, dentry,
|
if (IS_ERR(de))
|
||||||
FSCACHE_OBJECT_IS_WEIRD);
|
ret = PTR_ERR(de);
|
||||||
|
else
|
||||||
|
ret = cachefiles_bury_object(volume->cache, object,
|
||||||
|
fan, de,
|
||||||
|
FSCACHE_OBJECT_IS_WEIRD);
|
||||||
dput(dentry);
|
dput(dentry);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <linux/slab.h>
|
#include <linux/slab.h>
|
||||||
|
#include <linux/namei.h>
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include <trace/events/fscache.h>
|
#include <trace/events/fscache.h>
|
||||||
|
|
||||||
@@ -58,9 +59,11 @@ void cachefiles_acquire_volume(struct fscache_volume *vcookie)
|
|||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (ret != -ESTALE)
|
if (ret != -ESTALE)
|
||||||
goto error_dir;
|
goto error_dir;
|
||||||
inode_lock_nested(d_inode(cache->store), I_MUTEX_PARENT);
|
vdentry = start_removing_dentry(cache->store, vdentry);
|
||||||
cachefiles_bury_object(cache, NULL, cache->store, vdentry,
|
if (!IS_ERR(vdentry))
|
||||||
FSCACHE_VOLUME_IS_WEIRD);
|
cachefiles_bury_object(cache, NULL, cache->store,
|
||||||
|
vdentry,
|
||||||
|
FSCACHE_VOLUME_IS_WEIRD);
|
||||||
cachefiles_put_directory(volume->dentry);
|
cachefiles_put_directory(volume->dentry);
|
||||||
cond_resched();
|
cond_resched();
|
||||||
goto retry;
|
goto retry;
|
||||||
|
|||||||
33
fs/namei.c
33
fs/namei.c
@@ -3323,6 +3323,39 @@ struct dentry *start_removing_noperm(struct dentry *parent,
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL(start_removing_noperm);
|
EXPORT_SYMBOL(start_removing_noperm);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* start_removing_dentry - prepare to remove a given dentry
|
||||||
|
* @parent: directory from which dentry should be removed
|
||||||
|
* @child: the dentry to be removed
|
||||||
|
*
|
||||||
|
* A lock is taken to protect the dentry again other dirops and
|
||||||
|
* the validity of the dentry is checked: correct parent and still hashed.
|
||||||
|
*
|
||||||
|
* If the dentry is valid and positive, a reference is taken and
|
||||||
|
* returned. If not an error is returned.
|
||||||
|
*
|
||||||
|
* end_removing() should be called when removal is complete, or aborted.
|
||||||
|
*
|
||||||
|
* Returns: the valid dentry, or an error.
|
||||||
|
*/
|
||||||
|
struct dentry *start_removing_dentry(struct dentry *parent,
|
||||||
|
struct dentry *child)
|
||||||
|
{
|
||||||
|
inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
|
||||||
|
if (unlikely(IS_DEADDIR(parent->d_inode) ||
|
||||||
|
child->d_parent != parent ||
|
||||||
|
d_unhashed(child))) {
|
||||||
|
inode_unlock(parent->d_inode);
|
||||||
|
return ERR_PTR(-EINVAL);
|
||||||
|
}
|
||||||
|
if (d_is_negative(child)) {
|
||||||
|
inode_unlock(parent->d_inode);
|
||||||
|
return ERR_PTR(-ENOENT);
|
||||||
|
}
|
||||||
|
return dget(child);
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL(start_removing_dentry);
|
||||||
|
|
||||||
#ifdef CONFIG_UNIX98_PTYS
|
#ifdef CONFIG_UNIX98_PTYS
|
||||||
int path_pts(struct path *path)
|
int path_pts(struct path *path)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -47,14 +47,12 @@ static int ovl_cleanup_locked(struct ovl_fs *ofs, struct inode *wdir,
|
|||||||
int ovl_cleanup(struct ovl_fs *ofs, struct dentry *workdir,
|
int ovl_cleanup(struct ovl_fs *ofs, struct dentry *workdir,
|
||||||
struct dentry *wdentry)
|
struct dentry *wdentry)
|
||||||
{
|
{
|
||||||
int err;
|
wdentry = start_removing_dentry(workdir, wdentry);
|
||||||
|
if (IS_ERR(wdentry))
|
||||||
err = ovl_parent_lock(workdir, wdentry);
|
return PTR_ERR(wdentry);
|
||||||
if (err)
|
|
||||||
return err;
|
|
||||||
|
|
||||||
ovl_cleanup_locked(ofs, workdir->d_inode, wdentry);
|
ovl_cleanup_locked(ofs, workdir->d_inode, wdentry);
|
||||||
ovl_parent_unlock(workdir);
|
end_removing(wdentry);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1242,11 +1242,11 @@ int ovl_workdir_cleanup(struct ovl_fs *ofs, struct dentry *parent,
|
|||||||
if (!d_is_dir(dentry) || level > 1)
|
if (!d_is_dir(dentry) || level > 1)
|
||||||
return ovl_cleanup(ofs, parent, dentry);
|
return ovl_cleanup(ofs, parent, dentry);
|
||||||
|
|
||||||
err = ovl_parent_lock(parent, dentry);
|
dentry = start_removing_dentry(parent, dentry);
|
||||||
if (err)
|
if (IS_ERR(dentry))
|
||||||
return err;
|
return PTR_ERR(dentry);
|
||||||
err = ovl_do_rmdir(ofs, parent->d_inode, dentry);
|
err = ovl_do_rmdir(ofs, parent->d_inode, dentry);
|
||||||
ovl_parent_unlock(parent);
|
end_removing(dentry);
|
||||||
if (err) {
|
if (err) {
|
||||||
struct path path = { .mnt = mnt, .dentry = dentry };
|
struct path path = { .mnt = mnt, .dentry = dentry };
|
||||||
|
|
||||||
|
|||||||
@@ -49,24 +49,6 @@ static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
|
|||||||
i_uid_write(inode, i_uid_read(parent_inode));
|
i_uid_write(inode, i_uid_read(parent_inode));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
|
|
||||||
* @parent: parent dentry
|
|
||||||
* @child: child dentry
|
|
||||||
*
|
|
||||||
* Returns: %0 on success, %-ENOENT if the parent dentry is not stable
|
|
||||||
*/
|
|
||||||
int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child)
|
|
||||||
{
|
|
||||||
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
|
|
||||||
if (child->d_parent != parent) {
|
|
||||||
inode_unlock(d_inode(parent));
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
|
static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
|
||||||
char *pathname, unsigned int flags,
|
char *pathname, unsigned int flags,
|
||||||
struct path *path, bool for_remove)
|
struct path *path, bool for_remove)
|
||||||
@@ -1082,18 +1064,17 @@ int ksmbd_vfs_unlink(struct file *filp)
|
|||||||
return err;
|
return err;
|
||||||
|
|
||||||
dir = dget_parent(dentry);
|
dir = dget_parent(dentry);
|
||||||
err = ksmbd_vfs_lock_parent(dir, dentry);
|
dentry = start_removing_dentry(dir, dentry);
|
||||||
if (err)
|
err = PTR_ERR(dentry);
|
||||||
|
if (IS_ERR(dentry))
|
||||||
goto out;
|
goto out;
|
||||||
dget(dentry);
|
|
||||||
|
|
||||||
if (S_ISDIR(d_inode(dentry)->i_mode))
|
if (S_ISDIR(d_inode(dentry)->i_mode))
|
||||||
err = vfs_rmdir(idmap, d_inode(dir), dentry);
|
err = vfs_rmdir(idmap, d_inode(dir), dentry);
|
||||||
else
|
else
|
||||||
err = vfs_unlink(idmap, d_inode(dir), dentry, NULL);
|
err = vfs_unlink(idmap, d_inode(dir), dentry, NULL);
|
||||||
|
|
||||||
dput(dentry);
|
end_removing(dentry);
|
||||||
inode_unlock(d_inode(dir));
|
|
||||||
if (err)
|
if (err)
|
||||||
ksmbd_debug(VFS, "failed to delete, err %d\n", err);
|
ksmbd_debug(VFS, "failed to delete, err %d\n", err);
|
||||||
out:
|
out:
|
||||||
|
|||||||
@@ -94,6 +94,8 @@ struct dentry *start_removing(struct mnt_idmap *idmap, struct dentry *parent,
|
|||||||
struct qstr *name);
|
struct qstr *name);
|
||||||
struct dentry *start_creating_noperm(struct dentry *parent, struct qstr *name);
|
struct dentry *start_creating_noperm(struct dentry *parent, struct qstr *name);
|
||||||
struct dentry *start_removing_noperm(struct dentry *parent, struct qstr *name);
|
struct dentry *start_removing_noperm(struct dentry *parent, struct qstr *name);
|
||||||
|
struct dentry *start_removing_dentry(struct dentry *parent,
|
||||||
|
struct dentry *child);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* end_creating - finish action started with start_creating
|
* end_creating - finish action started with start_creating
|
||||||
|
|||||||
@@ -355,17 +355,17 @@ static void aafs_remove(struct dentry *dentry)
|
|||||||
if (!dentry || IS_ERR(dentry))
|
if (!dentry || IS_ERR(dentry))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
/* ->d_parent is stable as rename is not supported */
|
||||||
dir = d_inode(dentry->d_parent);
|
dir = d_inode(dentry->d_parent);
|
||||||
inode_lock(dir);
|
dentry = start_removing_dentry(dentry->d_parent, dentry);
|
||||||
if (simple_positive(dentry)) {
|
if (!IS_ERR(dentry) && simple_positive(dentry)) {
|
||||||
if (d_is_dir(dentry))
|
if (d_is_dir(dentry))
|
||||||
simple_rmdir(dir, dentry);
|
simple_rmdir(dir, dentry);
|
||||||
else
|
else
|
||||||
simple_unlink(dir, dentry);
|
simple_unlink(dir, dentry);
|
||||||
d_delete(dentry);
|
d_delete(dentry);
|
||||||
dput(dentry);
|
|
||||||
}
|
}
|
||||||
inode_unlock(dir);
|
end_removing(dentry);
|
||||||
simple_release_fs(&aafs_mnt, &aafs_count);
|
simple_release_fs(&aafs_mnt, &aafs_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user