Skip to contentSkip to conversation

fs: Don't create file when open() with O_DIRECT if fs not support

RFC Superseded weiping zhang 2 participants 4 messages Jul 28, 2017 VFS

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 fs: Don't create file when open() with O_DIRECT if fs not support

Some filesystems not support O_DIRECT, when user open with O_DIRECT , an errno -EINVAL return to userspace by open_check_o_direct, but the file has been created, it's a strange thing. Add a checking for that can avoid creating file if fs not support O_DIRECT.

---
 fs/namei.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
fs/namei.c +11 -0
index ddb6a7c..45c892b 100644
@@ -3201,6 +3201,17 @@ static int lookup_open(struct nameidata *nd, struct path *path,
 						open_flag & O_EXCL);
 		if (error)
 			goto out_dput;
+		/* if fs not support direct io, delete this inode */
+		if (unlikely(open_flag & O_DIRECT) &&
+			!(dentry->d_inode->i_mapping &&
+			dentry->d_inode->i_mapping->a_ops &&
+			dentry->d_inode->i_mapping->a_ops->direct_IO) &&
+			dir_inode->i_op->unlink) {
+			error = dir_inode->i_op->unlink(dir_inode, dentry);
+			if (error == 0)
+				error = -EINVAL;
+			goto out_dput;
+		}
 		fsnotify_create(dir_inode, dentry);
 	}
 	if (unlikely(create_error) && !dentry->d_inode) {
Signature · 1 line
2.9.4
#1 lore raw
Quoted 7 lines · weiping zhang

Some filesystems not support O_DIRECT, when user open with O_DIRECT , an errno -EINVAL return to userspace by open_check_o_direct, but the file has been created, it's a strange thing. Add a checking for that can avoid creating file if fs not support O_DIRECT.

fs/namei.c +0 -0
 		if (error)
 			goto out_dput;

fs/namei.c static int lookup_open(struct nameidata *nd, struct path *path, · line 3213

Quoted 10 lines
fs/namei.c +10 -0
+		/* if fs not support direct io, delete this inode */
+		if (unlikely(open_flag & O_DIRECT) &&
+			!(dentry->d_inode->i_mapping &&
+			dentry->d_inode->i_mapping->a_ops &&
+			dentry->d_inode->i_mapping->a_ops->direct_IO) &&
+			dir_inode->i_op->unlink) {
+			error = dir_inode->i_op->unlink(dir_inode, dentry);
+			if (error == 0)
+				error = -EINVAL;
+			goto out_dput;

Surely it's better to change the prototype of ->create to take open_flag (like atomic_open does) and then have the filesystem check for O_DIRECT? This solution seems quite inelegant to me.

#2 lore raw
Quoted 22 lines · Matthew Wilcox
Quoted 7 lines · weiping zhang

Some filesystems not support O_DIRECT, when user open with O_DIRECT , an errno -EINVAL return to userspace by open_check_o_direct, but the file has been created, it's a strange thing. Add a checking for that can avoid creating file if fs not support O_DIRECT.

fs/namei.c +0 -0
 		if (error)
 			goto out_dput;

fs/namei.c static int lookup_open(struct nameidata *nd, struct path *path, · line 3213

Quoted 10 lines
fs/namei.c +10 -0
+		/* if fs not support direct io, delete this inode */
+		if (unlikely(open_flag & O_DIRECT) &&
+			!(dentry->d_inode->i_mapping &&
+			dentry->d_inode->i_mapping->a_ops &&
+			dentry->d_inode->i_mapping->a_ops->direct_IO) &&
+			dir_inode->i_op->unlink) {
+			error = dir_inode->i_op->unlink(dir_inode, dentry);
+			if (error == 0)
+				error = -EINVAL;
+			goto out_dput;

Surely it's better to change the prototype of ->create to take open_flag (like atomic_open does) and then have the filesystem check for O_DIRECT? This solution seems quite inelegant to me.

Quoted 1 line · the only concern for me is that Viro

Not to mention the modules, or filesystems where that depends upon e.g. mount flags...

so I try to find a common way to do that. And I also the patch is not good enough. If you really cann't accept it , I will try to add a parameter for ->create and let the underlaying fs to check that flag.

#3 lore raw
In reply to Matthew Wilcox
Quoted 22 lines · Matthew Wilcox
Quoted 7 lines · weiping zhang

Some filesystems not support O_DIRECT, when user open with O_DIRECT , an errno -EINVAL return to userspace by open_check_o_direct, but the file has been created, it's a strange thing. Add a checking for that can avoid creating file if fs not support O_DIRECT.

fs/namei.c +0 -0
 		if (error)
 			goto out_dput;

fs/namei.c static int lookup_open(struct nameidata *nd, struct path *path, · line 3213

Quoted 10 lines
fs/namei.c +10 -0
+		/* if fs not support direct io, delete this inode */
+		if (unlikely(open_flag & O_DIRECT) &&
+			!(dentry->d_inode->i_mapping &&
+			dentry->d_inode->i_mapping->a_ops &&
+			dentry->d_inode->i_mapping->a_ops->direct_IO) &&
+			dir_inode->i_op->unlink) {
+			error = dir_inode->i_op->unlink(dir_inode, dentry);
+			if (error == 0)
+				error = -EINVAL;
+			goto out_dput;

Surely it's better to change the prototype of ->create to take open_flag (like atomic_open does) and then have the filesystem check for O_DIRECT? This solution seems quite inelegant to me.

Quoted 1 line · the only concern for me is Viro
Quoted 1 line

Not to mention the modules, or filesystems where that depends upon e.g. mount flags...

So I try to find a common way to do that, the positve: only modify 1 file, no affect any specific filysystems, the negtive: this patch seems ugly. if we add/change prototype of ->create every fs will be patched. And if you are in favor of me changing prototype of ->create ,I will send a new patch.