Skip to content

Commit 4037d96

Browse files
neilbrownbrauner
authored andcommitted
VFS: introduce start_dirop() and end_dirop()
The fact that directory operations (create,remove,rename) are protected by a lock on the parent is known widely throughout the kernel. In order to change this - to instead lock the target dentry - it is best to centralise this knowledge so it can be changed in one place. This patch introduces start_dirop() which is local to VFS code. It performs the required locking for create and remove. Rename will be handled separately. Various functions with names like start_creating() or start_removing_path(), some of which already exist, will export this functionality beyond the VFS. end_dirop() is the partner of start_dirop(). It drops the lock and releases the reference on the dentry. It *is* exported so that various end_creating etc functions can be inline. As vfs_mkdir() drops the dentry on error we cannot use end_dirop() as that won't unlock when the dentry IS_ERR(). For now we need an explicit unlock when dentry IS_ERR(). I hope to change vfs_mkdir() to unlock when it drops a dentry so that explicit unlock can go away. end_dirop() can always be called on the result of start_dirop(), but not after vfs_mkdir(). After a vfs_mkdir() we still may need the explicit unlock as seen in end_creating_path(). As well as adding start_dirop() and end_dirop() this patch uses them in: - simple_start_creating (which requires sharing lookup_noperm_common() with libfs.c) - start_removing_path / start_removing_user_path_at - filename_create / end_creating_path() - do_rmdir(), do_unlinkat() Reviewed-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: NeilBrown <neil@brown.name> Link: https://patch.msgid.link/20251113002050.676694-3-neilb@ownmail.net Tested-by: syzbot@syzkaller.appspotmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 8b45b9a commit 4037d96

4 files changed

Lines changed: 95 additions & 44 deletions

File tree

fs/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ int vfs_tmpfile(struct mnt_idmap *idmap,
6767
const struct path *parentpath,
6868
struct file *file, umode_t mode);
6969
struct dentry *d_hash_and_lookup(struct dentry *, struct qstr *);
70+
struct dentry *start_dirop(struct dentry *parent, struct qstr *name,
71+
unsigned int lookup_flags);
72+
int lookup_noperm_common(struct qstr *qname, struct dentry *base);
7073

7174
/*
7275
* namespace.c

fs/libfs.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,27 +2289,25 @@ void stashed_dentry_prune(struct dentry *dentry)
22892289
cmpxchg(stashed, dentry, NULL);
22902290
}
22912291

2292-
/* parent must be held exclusive */
2292+
/**
2293+
* simple_start_creating - prepare to create a given name
2294+
* @parent: directory in which to prepare to create the name
2295+
* @name: the name to be created
2296+
*
2297+
* Required lock is taken and a lookup in performed prior to creating an
2298+
* object in a directory. No permission checking is performed.
2299+
*
2300+
* Returns: a negative dentry on which vfs_create() or similar may
2301+
* be attempted, or an error.
2302+
*/
22932303
struct dentry *simple_start_creating(struct dentry *parent, const char *name)
22942304
{
2295-
struct dentry *dentry;
2296-
struct inode *dir = d_inode(parent);
2305+
struct qstr qname = QSTR(name);
2306+
int err;
22972307

2298-
inode_lock(dir);
2299-
if (unlikely(IS_DEADDIR(dir))) {
2300-
inode_unlock(dir);
2301-
return ERR_PTR(-ENOENT);
2302-
}
2303-
dentry = lookup_noperm(&QSTR(name), parent);
2304-
if (IS_ERR(dentry)) {
2305-
inode_unlock(dir);
2306-
return dentry;
2307-
}
2308-
if (dentry->d_inode) {
2309-
dput(dentry);
2310-
inode_unlock(dir);
2311-
return ERR_PTR(-EEXIST);
2312-
}
2313-
return dentry;
2308+
err = lookup_noperm_common(&qname, parent);
2309+
if (err)
2310+
return ERR_PTR(err);
2311+
return start_dirop(parent, &qname, LOOKUP_CREATE | LOOKUP_EXCL);
23142312
}
23152313
EXPORT_SYMBOL(simple_start_creating);

fs/namei.c

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,6 +2765,48 @@ static int filename_parentat(int dfd, struct filename *name,
27652765
return __filename_parentat(dfd, name, flags, parent, last, type, NULL);
27662766
}
27672767

2768+
/**
2769+
* start_dirop - begin a create or remove dirop, performing locking and lookup
2770+
* @parent: the dentry of the parent in which the operation will occur
2771+
* @name: a qstr holding the name within that parent
2772+
* @lookup_flags: intent and other lookup flags.
2773+
*
2774+
* The lookup is performed and necessary locks are taken so that, on success,
2775+
* the returned dentry can be operated on safely.
2776+
* The qstr must already have the hash value calculated.
2777+
*
2778+
* Returns: a locked dentry, or an error.
2779+
*
2780+
*/
2781+
struct dentry *start_dirop(struct dentry *parent, struct qstr *name,
2782+
unsigned int lookup_flags)
2783+
{
2784+
struct dentry *dentry;
2785+
struct inode *dir = d_inode(parent);
2786+
2787+
inode_lock_nested(dir, I_MUTEX_PARENT);
2788+
dentry = lookup_one_qstr_excl(name, parent, lookup_flags);
2789+
if (IS_ERR(dentry))
2790+
inode_unlock(dir);
2791+
return dentry;
2792+
}
2793+
2794+
/**
2795+
* end_dirop - signal completion of a dirop
2796+
* @de: the dentry which was returned by start_dirop or similar.
2797+
*
2798+
* If the de is an error, nothing happens. Otherwise any lock taken to
2799+
* protect the dentry is dropped and the dentry itself is release (dput()).
2800+
*/
2801+
void end_dirop(struct dentry *de)
2802+
{
2803+
if (!IS_ERR(de)) {
2804+
inode_unlock(de->d_parent->d_inode);
2805+
dput(de);
2806+
}
2807+
}
2808+
EXPORT_SYMBOL(end_dirop);
2809+
27682810
/* does lookup, returns the object with parent locked */
27692811
static struct dentry *__start_removing_path(int dfd, struct filename *name,
27702812
struct path *path)
@@ -2781,21 +2823,19 @@ static struct dentry *__start_removing_path(int dfd, struct filename *name,
27812823
return ERR_PTR(-EINVAL);
27822824
/* don't fail immediately if it's r/o, at least try to report other errors */
27832825
error = mnt_want_write(parent_path.mnt);
2784-
inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT);
2785-
d = lookup_one_qstr_excl(&last, parent_path.dentry, 0);
2826+
d = start_dirop(parent_path.dentry, &last, 0);
27862827
if (IS_ERR(d))
2787-
goto unlock;
2828+
goto drop;
27882829
if (error)
27892830
goto fail;
27902831
path->dentry = no_free_ptr(parent_path.dentry);
27912832
path->mnt = no_free_ptr(parent_path.mnt);
27922833
return d;
27932834

27942835
fail:
2795-
dput(d);
2836+
end_dirop(d);
27962837
d = ERR_PTR(error);
2797-
unlock:
2798-
inode_unlock(parent_path.dentry->d_inode);
2838+
drop:
27992839
if (!error)
28002840
mnt_drop_write(parent_path.mnt);
28012841
return d;
@@ -2910,7 +2950,7 @@ int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
29102950
}
29112951
EXPORT_SYMBOL(vfs_path_lookup);
29122952

2913-
static int lookup_noperm_common(struct qstr *qname, struct dentry *base)
2953+
int lookup_noperm_common(struct qstr *qname, struct dentry *base)
29142954
{
29152955
const char *name = qname->name;
29162956
u32 len = qname->len;
@@ -4223,21 +4263,18 @@ static struct dentry *filename_create(int dfd, struct filename *name,
42234263
*/
42244264
if (last.name[last.len] && !want_dir)
42254265
create_flags &= ~LOOKUP_CREATE;
4226-
inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
4227-
dentry = lookup_one_qstr_excl(&last, path->dentry,
4228-
reval_flag | create_flags);
4266+
dentry = start_dirop(path->dentry, &last, reval_flag | create_flags);
42294267
if (IS_ERR(dentry))
4230-
goto unlock;
4268+
goto out_drop_write;
42314269

42324270
if (unlikely(error))
42334271
goto fail;
42344272

42354273
return dentry;
42364274
fail:
4237-
dput(dentry);
4275+
end_dirop(dentry);
42384276
dentry = ERR_PTR(error);
4239-
unlock:
4240-
inode_unlock(path->dentry->d_inode);
4277+
out_drop_write:
42414278
if (!error)
42424279
mnt_drop_write(path->mnt);
42434280
out:
@@ -4256,11 +4293,26 @@ struct dentry *start_creating_path(int dfd, const char *pathname,
42564293
}
42574294
EXPORT_SYMBOL(start_creating_path);
42584295

4296+
/**
4297+
* end_creating_path - finish a code section started by start_creating_path()
4298+
* @path: the path instantiated by start_creating_path()
4299+
* @dentry: the dentry returned by start_creating_path()
4300+
*
4301+
* end_creating_path() will unlock and locks taken by start_creating_path()
4302+
* and drop an references that were taken. It should only be called
4303+
* if start_creating_path() returned a non-error.
4304+
* If vfs_mkdir() was called and it returned an error, that error *should*
4305+
* be passed to end_creating_path() together with the path.
4306+
*/
42594307
void end_creating_path(const struct path *path, struct dentry *dentry)
42604308
{
4261-
if (!IS_ERR(dentry))
4262-
dput(dentry);
4263-
inode_unlock(path->dentry->d_inode);
4309+
if (IS_ERR(dentry))
4310+
/* The parent is still locked despite the error from
4311+
* vfs_mkdir() - must unlock it.
4312+
*/
4313+
inode_unlock(path->dentry->d_inode);
4314+
else
4315+
end_dirop(dentry);
42644316
mnt_drop_write(path->mnt);
42654317
path_put(path);
42664318
}
@@ -4592,8 +4644,7 @@ int do_rmdir(int dfd, struct filename *name)
45924644
if (error)
45934645
goto exit2;
45944646

4595-
inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4596-
dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags);
4647+
dentry = start_dirop(path.dentry, &last, lookup_flags);
45974648
error = PTR_ERR(dentry);
45984649
if (IS_ERR(dentry))
45994650
goto exit3;
@@ -4602,9 +4653,8 @@ int do_rmdir(int dfd, struct filename *name)
46024653
goto exit4;
46034654
error = vfs_rmdir(mnt_idmap(path.mnt), path.dentry->d_inode, dentry);
46044655
exit4:
4605-
dput(dentry);
4656+
end_dirop(dentry);
46064657
exit3:
4607-
inode_unlock(path.dentry->d_inode);
46084658
mnt_drop_write(path.mnt);
46094659
exit2:
46104660
path_put(&path);
@@ -4721,8 +4771,7 @@ int do_unlinkat(int dfd, struct filename *name)
47214771
if (error)
47224772
goto exit2;
47234773
retry_deleg:
4724-
inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4725-
dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags);
4774+
dentry = start_dirop(path.dentry, &last, lookup_flags);
47264775
error = PTR_ERR(dentry);
47274776
if (!IS_ERR(dentry)) {
47284777

@@ -4737,9 +4786,8 @@ int do_unlinkat(int dfd, struct filename *name)
47374786
error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode,
47384787
dentry, &delegated_inode);
47394788
exit3:
4740-
dput(dentry);
4789+
end_dirop(dentry);
47414790
}
4742-
inode_unlock(path.dentry->d_inode);
47434791
if (inode)
47444792
iput(inode); /* truncate the inode here */
47454793
inode = NULL;

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,6 +3609,8 @@ extern void iterate_supers_type(struct file_system_type *,
36093609
void filesystems_freeze(void);
36103610
void filesystems_thaw(void);
36113611

3612+
void end_dirop(struct dentry *de);
3613+
36123614
extern int dcache_dir_open(struct inode *, struct file *);
36133615
extern int dcache_dir_close(struct inode *, struct file *);
36143616
extern loff_t dcache_dir_lseek(struct file *, loff_t, int);

0 commit comments

Comments
 (0)