Skip to content

Commit a8104a9

Browse files
author
Al Viro
committed
pull mnt_want_write()/mnt_drop_write() into kern_path_create()/done_path_create() resp.
One side effect - attempt to create a cross-device link on a read-only fs fails with EROFS instead of EXDEV now. Makes more sense, POSIX allows, etc. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 8e4bfca commit a8104a9

3 files changed

Lines changed: 18 additions & 50 deletions

File tree

‎fs/namei.c‎

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,27 +2865,30 @@ struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path
28652865
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
28662866
dentry = lookup_hash(&nd);
28672867
if (IS_ERR(dentry))
2868-
goto fail;
2868+
goto unlock;
28692869

2870+
error = -EEXIST;
28702871
if (dentry->d_inode)
2871-
goto eexist;
2872+
goto fail;
28722873
/*
28732874
* Special case - lookup gave negative, but... we had foo/bar/
28742875
* From the vfs_mknod() POV we just have a negative dentry -
28752876
* all is fine. Let's be bastards - you had / on the end, you've
28762877
* been asking for (non-existent) directory. -ENOENT for you.
28772878
*/
28782879
if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2879-
dput(dentry);
2880-
dentry = ERR_PTR(-ENOENT);
2880+
error = -ENOENT;
28812881
goto fail;
28822882
}
2883+
error = mnt_want_write(nd.path.mnt);
2884+
if (error)
2885+
goto fail;
28832886
*path = nd.path;
28842887
return dentry;
2885-
eexist:
2886-
dput(dentry);
2887-
dentry = ERR_PTR(-EEXIST);
28882888
fail:
2889+
dput(dentry);
2890+
dentry = ERR_PTR(error);
2891+
unlock:
28892892
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28902893
out:
28912894
path_put(&nd.path);
@@ -2897,6 +2900,7 @@ void done_path_create(struct path *path, struct dentry *dentry)
28972900
{
28982901
dput(dentry);
28992902
mutex_unlock(&path->dentry->d_inode->i_mutex);
2903+
mnt_drop_write(path->mnt);
29002904
path_put(path);
29012905
}
29022906
EXPORT_SYMBOL(done_path_create);
@@ -2974,12 +2978,9 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
29742978

29752979
if (!IS_POSIXACL(path.dentry->d_inode))
29762980
mode &= ~current_umask();
2977-
error = mnt_want_write(path.mnt);
2978-
if (error)
2979-
goto out_dput;
29802981
error = security_path_mknod(&path, dentry, mode, dev);
29812982
if (error)
2982-
goto out_drop_write;
2983+
goto out;
29832984
switch (mode & S_IFMT) {
29842985
case 0: case S_IFREG:
29852986
error = vfs_create(path.dentry->d_inode,dentry,mode,true);
@@ -2992,11 +2993,8 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
29922993
error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
29932994
break;
29942995
}
2995-
out_drop_write:
2996-
mnt_drop_write(path.mnt);
2997-
out_dput:
2996+
out:
29982997
done_path_create(&path, dentry);
2999-
30002998
return error;
30012999
}
30023000

@@ -3042,16 +3040,9 @@ SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
30423040

30433041
if (!IS_POSIXACL(path.dentry->d_inode))
30443042
mode &= ~current_umask();
3045-
error = mnt_want_write(path.mnt);
3046-
if (error)
3047-
goto out_dput;
30483043
error = security_path_mkdir(&path, dentry, mode);
3049-
if (error)
3050-
goto out_drop_write;
3051-
error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3052-
out_drop_write:
3053-
mnt_drop_write(path.mnt);
3054-
out_dput:
3044+
if (!error)
3045+
error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
30553046
done_path_create(&path, dentry);
30563047
return error;
30573048
}
@@ -3326,16 +3317,9 @@ SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
33263317
if (IS_ERR(dentry))
33273318
goto out_putname;
33283319

3329-
error = mnt_want_write(path.mnt);
3330-
if (error)
3331-
goto out_dput;
33323320
error = security_path_symlink(&path, dentry, from);
3333-
if (error)
3334-
goto out_drop_write;
3335-
error = vfs_symlink(path.dentry->d_inode, dentry, from);
3336-
out_drop_write:
3337-
mnt_drop_write(path.mnt);
3338-
out_dput:
3321+
if (!error)
3322+
error = vfs_symlink(path.dentry->d_inode, dentry, from);
33393323
done_path_create(&path, dentry);
33403324
out_putname:
33413325
putname(from);
@@ -3436,15 +3420,10 @@ SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
34363420
error = -EXDEV;
34373421
if (old_path.mnt != new_path.mnt)
34383422
goto out_dput;
3439-
error = mnt_want_write(new_path.mnt);
3440-
if (error)
3441-
goto out_dput;
34423423
error = security_path_link(old_path.dentry, &new_path, new_dentry);
34433424
if (error)
3444-
goto out_drop_write;
3425+
goto out_dput;
34453426
error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3446-
out_drop_write:
3447-
mnt_drop_write(new_path.mnt);
34483427
out_dput:
34493428
done_path_create(&new_path, new_dentry);
34503429
out:

‎fs/ocfs2/refcounttree.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,16 +4466,9 @@ int ocfs2_reflink_ioctl(struct inode *inode,
44664466
goto out_dput;
44674467
}
44684468

4469-
error = mnt_want_write(new_path.mnt);
4470-
if (error) {
4471-
mlog_errno(error);
4472-
goto out_dput;
4473-
}
4474-
44754469
error = ocfs2_vfs_reflink(old_path.dentry,
44764470
new_path.dentry->d_inode,
44774471
new_dentry, preserve);
4478-
mnt_drop_write(new_path.mnt);
44794472
out_dput:
44804473
done_path_create(&new_path, new_dentry);
44814474
out:

‎net/unix/af_unix.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,15 +876,11 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
876876
*/
877877
mode = S_IFSOCK |
878878
(SOCK_INODE(sock)->i_mode & ~current_umask());
879-
err = mnt_want_write(path.mnt);
880-
if (err)
881-
goto out_mknod_dput;
882879
err = security_path_mknod(&path, dentry, mode, 0);
883880
if (err)
884881
goto out_mknod_drop_write;
885882
err = vfs_mknod(path.dentry->d_inode, dentry, mode, 0);
886883
out_mknod_drop_write:
887-
mnt_drop_write(path.mnt);
888884
if (err)
889885
goto out_mknod_dput;
890886
mntget(path.mnt);

0 commit comments

Comments
 (0)