Skip to content

Commit 7bb1eb4

Browse files
neilbrownbrauner
authored andcommitted
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>
1 parent 1ead221 commit 7bb1eb4

9 files changed

Lines changed: 78 additions & 60 deletions

File tree

fs/cachefiles/interface.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/mount.h>
1010
#include <linux/xattr.h>
1111
#include <linux/file.h>
12+
#include <linux/namei.h>
1213
#include <linux/falloc.h>
1314
#include <trace/events/fscache.h>
1415
#include "internal.h"
@@ -428,11 +429,13 @@ static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie)
428429
if (!old_tmpfile) {
429430
struct cachefiles_volume *volume = object->volume;
430431
struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
432+
struct dentry *obj;
431433

432-
inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
433-
cachefiles_bury_object(volume->cache, object, fan,
434-
old_file->f_path.dentry,
435-
FSCACHE_OBJECT_INVALIDATED);
434+
obj = start_removing_dentry(fan, old_file->f_path.dentry);
435+
if (!IS_ERR(obj))
436+
cachefiles_bury_object(volume->cache, object,
437+
fan, obj,
438+
FSCACHE_OBJECT_INVALIDATED);
436439
}
437440
fput(old_file);
438441
}

fs/cachefiles/namei.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ static int cachefiles_unlink(struct cachefiles_cache *cache,
261261
* - Directory backed objects are stuffed into the graveyard for userspace to
262262
* delete
263263
* On entry dir must be locked. It will be unlocked on exit.
264+
* On entry there must be at least 2 refs on rep, one will be dropped on exit.
264265
*/
265266
int cachefiles_bury_object(struct cachefiles_cache *cache,
266267
struct cachefiles_object *object,
@@ -275,12 +276,6 @@ int cachefiles_bury_object(struct cachefiles_cache *cache,
275276

276277
_enter(",'%pd','%pd'", dir, rep);
277278

278-
/* end_removing() will dput() @rep but we need to keep
279-
* a ref, so take one now. This also stops the dentry
280-
* being negated when unlinked which we need.
281-
*/
282-
dget(rep);
283-
284279
if (rep->d_parent != dir) {
285280
end_removing(rep);
286281
_leave(" = -ESTALE");
@@ -425,13 +420,12 @@ int cachefiles_delete_object(struct cachefiles_object *object,
425420

426421
_enter(",OBJ%x{%pD}", object->debug_id, object->file);
427422

428-
/* Stop the dentry being negated if it's only pinned by a file struct. */
429-
dget(dentry);
430-
431-
inode_lock_nested(d_backing_inode(fan), I_MUTEX_PARENT);
432-
ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
433-
inode_unlock(d_backing_inode(fan));
434-
dput(dentry);
423+
dentry = start_removing_dentry(fan, dentry);
424+
if (IS_ERR(dentry))
425+
ret = PTR_ERR(dentry);
426+
else
427+
ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
428+
end_removing(dentry);
435429
return ret;
436430
}
437431

@@ -644,9 +638,13 @@ bool cachefiles_look_up_object(struct cachefiles_object *object)
644638

645639
if (!d_is_reg(dentry)) {
646640
pr_err("%pd is not a file\n", dentry);
647-
inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
648-
ret = cachefiles_bury_object(volume->cache, object, fan, dentry,
649-
FSCACHE_OBJECT_IS_WEIRD);
641+
struct dentry *de = start_removing_dentry(fan, dentry);
642+
if (IS_ERR(de))
643+
ret = PTR_ERR(de);
644+
else
645+
ret = cachefiles_bury_object(volume->cache, object,
646+
fan, de,
647+
FSCACHE_OBJECT_IS_WEIRD);
650648
dput(dentry);
651649
if (ret < 0)
652650
return false;

fs/cachefiles/volume.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <linux/fs.h>
99
#include <linux/slab.h>
10+
#include <linux/namei.h>
1011
#include "internal.h"
1112
#include <trace/events/fscache.h>
1213

@@ -58,9 +59,11 @@ void cachefiles_acquire_volume(struct fscache_volume *vcookie)
5859
if (ret < 0) {
5960
if (ret != -ESTALE)
6061
goto error_dir;
61-
inode_lock_nested(d_inode(cache->store), I_MUTEX_PARENT);
62-
cachefiles_bury_object(cache, NULL, cache->store, vdentry,
63-
FSCACHE_VOLUME_IS_WEIRD);
62+
vdentry = start_removing_dentry(cache->store, vdentry);
63+
if (!IS_ERR(vdentry))
64+
cachefiles_bury_object(cache, NULL, cache->store,
65+
vdentry,
66+
FSCACHE_VOLUME_IS_WEIRD);
6467
cachefiles_put_directory(volume->dentry);
6568
cond_resched();
6669
goto retry;

fs/namei.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,6 +3323,39 @@ struct dentry *start_removing_noperm(struct dentry *parent,
33233323
}
33243324
EXPORT_SYMBOL(start_removing_noperm);
33253325

3326+
/**
3327+
* start_removing_dentry - prepare to remove a given dentry
3328+
* @parent: directory from which dentry should be removed
3329+
* @child: the dentry to be removed
3330+
*
3331+
* A lock is taken to protect the dentry again other dirops and
3332+
* the validity of the dentry is checked: correct parent and still hashed.
3333+
*
3334+
* If the dentry is valid and positive, a reference is taken and
3335+
* returned. If not an error is returned.
3336+
*
3337+
* end_removing() should be called when removal is complete, or aborted.
3338+
*
3339+
* Returns: the valid dentry, or an error.
3340+
*/
3341+
struct dentry *start_removing_dentry(struct dentry *parent,
3342+
struct dentry *child)
3343+
{
3344+
inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
3345+
if (unlikely(IS_DEADDIR(parent->d_inode) ||
3346+
child->d_parent != parent ||
3347+
d_unhashed(child))) {
3348+
inode_unlock(parent->d_inode);
3349+
return ERR_PTR(-EINVAL);
3350+
}
3351+
if (d_is_negative(child)) {
3352+
inode_unlock(parent->d_inode);
3353+
return ERR_PTR(-ENOENT);
3354+
}
3355+
return dget(child);
3356+
}
3357+
EXPORT_SYMBOL(start_removing_dentry);
3358+
33263359
#ifdef CONFIG_UNIX98_PTYS
33273360
int path_pts(struct path *path)
33283361
{

fs/overlayfs/dir.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ static int ovl_cleanup_locked(struct ovl_fs *ofs, struct inode *wdir,
4747
int ovl_cleanup(struct ovl_fs *ofs, struct dentry *workdir,
4848
struct dentry *wdentry)
4949
{
50-
int err;
51-
52-
err = ovl_parent_lock(workdir, wdentry);
53-
if (err)
54-
return err;
50+
wdentry = start_removing_dentry(workdir, wdentry);
51+
if (IS_ERR(wdentry))
52+
return PTR_ERR(wdentry);
5553

5654
ovl_cleanup_locked(ofs, workdir->d_inode, wdentry);
57-
ovl_parent_unlock(workdir);
55+
end_removing(wdentry);
5856

5957
return 0;
6058
}

fs/overlayfs/readdir.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,11 +1242,11 @@ int ovl_workdir_cleanup(struct ovl_fs *ofs, struct dentry *parent,
12421242
if (!d_is_dir(dentry) || level > 1)
12431243
return ovl_cleanup(ofs, parent, dentry);
12441244

1245-
err = ovl_parent_lock(parent, dentry);
1246-
if (err)
1247-
return err;
1245+
dentry = start_removing_dentry(parent, dentry);
1246+
if (IS_ERR(dentry))
1247+
return PTR_ERR(dentry);
12481248
err = ovl_do_rmdir(ofs, parent->d_inode, dentry);
1249-
ovl_parent_unlock(parent);
1249+
end_removing(dentry);
12501250
if (err) {
12511251
struct path path = { .mnt = mnt, .dentry = dentry };
12521252

fs/smb/server/vfs.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
4949
i_uid_write(inode, i_uid_read(parent_inode));
5050
}
5151

52-
/**
53-
* ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
54-
* @parent: parent dentry
55-
* @child: child dentry
56-
*
57-
* Returns: %0 on success, %-ENOENT if the parent dentry is not stable
58-
*/
59-
int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child)
60-
{
61-
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
62-
if (child->d_parent != parent) {
63-
inode_unlock(d_inode(parent));
64-
return -ENOENT;
65-
}
66-
67-
return 0;
68-
}
69-
7052
static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
7153
char *pathname, unsigned int flags,
7254
struct path *path, bool for_remove)
@@ -1082,18 +1064,17 @@ int ksmbd_vfs_unlink(struct file *filp)
10821064
return err;
10831065

10841066
dir = dget_parent(dentry);
1085-
err = ksmbd_vfs_lock_parent(dir, dentry);
1086-
if (err)
1067+
dentry = start_removing_dentry(dir, dentry);
1068+
err = PTR_ERR(dentry);
1069+
if (IS_ERR(dentry))
10871070
goto out;
1088-
dget(dentry);
10891071

10901072
if (S_ISDIR(d_inode(dentry)->i_mode))
10911073
err = vfs_rmdir(idmap, d_inode(dir), dentry);
10921074
else
10931075
err = vfs_unlink(idmap, d_inode(dir), dentry, NULL);
10941076

1095-
dput(dentry);
1096-
inode_unlock(d_inode(dir));
1077+
end_removing(dentry);
10971078
if (err)
10981079
ksmbd_debug(VFS, "failed to delete, err %d\n", err);
10991080
out:

include/linux/namei.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ struct dentry *start_removing(struct mnt_idmap *idmap, struct dentry *parent,
9494
struct qstr *name);
9595
struct dentry *start_creating_noperm(struct dentry *parent, struct qstr *name);
9696
struct dentry *start_removing_noperm(struct dentry *parent, struct qstr *name);
97+
struct dentry *start_removing_dentry(struct dentry *parent,
98+
struct dentry *child);
9799

98100
/**
99101
* end_creating - finish action started with start_creating

security/apparmor/apparmorfs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,17 @@ static void aafs_remove(struct dentry *dentry)
355355
if (!dentry || IS_ERR(dentry))
356356
return;
357357

358+
/* ->d_parent is stable as rename is not supported */
358359
dir = d_inode(dentry->d_parent);
359-
inode_lock(dir);
360-
if (simple_positive(dentry)) {
360+
dentry = start_removing_dentry(dentry->d_parent, dentry);
361+
if (!IS_ERR(dentry) && simple_positive(dentry)) {
361362
if (d_is_dir(dentry))
362363
simple_rmdir(dir, dentry);
363364
else
364365
simple_unlink(dir, dentry);
365366
d_delete(dentry);
366-
dput(dentry);
367367
}
368-
inode_unlock(dir);
368+
end_removing(dentry);
369369
simple_release_fs(&aafs_mnt, &aafs_count);
370370
}
371371

0 commit comments

Comments
 (0)